2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 extern int skytexturenum;
26 int lightmap_textures;
28 signed blocklights[18*18*3]; // LordHavoc: *3 for colored lighting
30 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
31 #define BLOCK_WIDTH 128
32 #define BLOCK_HEIGHT 128
33 // LordHavoc: increased lightmap limit from 64 to 1024
34 #define MAX_LIGHTMAPS 1024
35 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
39 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
41 byte *lightmaps[MAX_LIGHTMAPS];
42 short lightmapupdate[MAX_LIGHTMAPS][2];
44 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
45 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
46 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "1"};
47 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
48 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
49 cvar_t r_ambient = {"r_ambient", "0"};
50 //cvar_t gl_funnywalls = {"gl_funnywalls", "0"}; // LordHavoc: see BuildSurfaceDisplayList
52 qboolean lightmaprgba, nosubimagefragments, nosubimage;
55 qboolean skyisvisible;
56 extern qboolean gl_arrays;
58 extern int r_dlightframecount;
63 for (i = 0;i < MAX_LIGHTMAPS;i++)
65 Cvar_RegisterVariable(&gl_lightmapalign);
66 Cvar_RegisterVariable(&gl_lightmaprgba);
67 Cvar_RegisterVariable(&gl_nosubimagefragments);
68 Cvar_RegisterVariable(&gl_nosubimage);
69 Cvar_RegisterVariable(&r_ambient);
70 // Cvar_RegisterVariable(&gl_funnywalls);
71 // check if it's the glquake minigl driver
72 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
75 // Cvar_SetValue("gl_nosubimagefragments", 1);
76 // Cvar_SetValue("gl_nosubimage", 1);
77 Cvar_SetValue("gl_lightmode", 0);
81 extern qboolean lighthalf;
86 Combine and scale multiple lightmaps into the 8.8 format in blocklights
89 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
99 surf->cached_lighthalf = lighthalf;
100 surf->cached_ambient = r_ambient.value;
102 smax = (surf->extents[0]>>4)+1;
103 tmax = (surf->extents[1]>>4)+1;
105 lightmap = surf->samples;
107 // set to full bright if no light data
108 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
111 for (i=0 ; i<size ; i++)
122 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
123 for (i=0 ; i<size ; i++)
130 // add all the lightmaps
132 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
134 scale = d_lightstylevalue[surf->styles[maps]];
135 surf->cached_light[maps] = scale; // 8.8 fraction
137 for (i=0 ; i<size ; i++)
139 *bl++ += *lightmap++ * scale;
140 *bl++ += *lightmap++ * scale;
141 *bl++ += *lightmap++ * scale;
145 stride -= (smax*lightmapbytes);
149 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
150 // the image is brightened as a processing pass
153 for (i=0 ; i<tmax ; i++, dest += stride)
155 for (j=0 ; j<smax ; j++)
157 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
158 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
159 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
166 for (i=0 ; i<tmax ; i++, dest += stride)
168 for (j=0 ; j<smax ; j++)
170 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
171 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
172 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
181 for (i=0 ; i<tmax ; i++, dest += stride)
183 for (j=0 ; j<smax ; j++)
185 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
186 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
187 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
194 for (i=0 ; i<tmax ; i++, dest += stride)
196 for (j=0 ; j<smax ; j++)
198 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
199 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
200 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
207 byte templight[32*32*4];
209 void R_UpdateLightmap(msurface_t *s, int lnum)
212 // upload the new lightmap texture fragment
213 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
214 if (nosubimage || nosubimagefragments)
216 if (lightmapupdate[lnum][0] > s->light_t)
217 lightmapupdate[lnum][0] = s->light_t;
218 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
219 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
221 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
223 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
227 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
228 tmax = (s->extents[1]>>4)+1;
231 R_BuildLightMap (s, templight, smax * 4);
232 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
236 R_BuildLightMap (s, templight, smax * 3);
237 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
247 Returns the proper texture for a given time and base texture
250 texture_t *R_TextureAnimation (texture_t *base)
256 if (currententity->frame)
258 if (base->alternate_anims)
259 base = base->alternate_anims;
262 if (!base->anim_total)
267 relative = (int)(cl.time*10) % base->anim_total;
270 while (base->anim_min > relative || base->anim_max <= relative)
272 base = base->anim_next;
275 Con_Printf("R_TextureAnimation: broken cycle");
280 Con_Printf("R_TextureAnimation: infinite cycle");
290 =============================================================
294 =============================================================
298 extern int solidskytexture;
299 extern int alphaskytexture;
300 extern float speedscale; // for top sky and bottom sky
302 qboolean mtexenabled = false;
304 extern char skyname[];
306 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
307 //extern cvar_t r_dynamicwater;
310 #include "gl_warp_sin.h"
312 #define TURBSCALE (256.0 / (2 * M_PI))
315 void UploadLightmaps()
318 if (nosubimage || nosubimagefragments)
320 for (i = 0;i < MAX_LIGHTMAPS;i++)
322 if (lightmapupdate[i][0] < lightmapupdate[i][1])
324 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
328 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
330 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
335 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 4 * lightmapupdate[i][0]));
337 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 3 * lightmapupdate[i][0]));
340 lightmapupdate[i][0] = BLOCK_HEIGHT;
341 lightmapupdate[i][1] = 0;
351 extern qboolean hlbsp;
353 extern char skyname[];
354 //extern qboolean SV_TestLine (hull_t *hull, int num, vec3_t p1, vec3_t p2);
355 void DrawTextureChains (void)
362 float *v, os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
363 // vec3_t shadecolor;
367 for (j = 0;j < cl.worldmodel->numtextures;j++)
369 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
371 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
372 if (s->flags & SURF_DRAWSKY)
374 cl.worldmodel->textures[j]->texturechain = NULL;
375 t = R_TextureAnimation (cl.worldmodel->textures[j]);
377 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
379 for (;s;s = s->texturechain)
381 for (p=s->polys ; p ; p=p->next)
383 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
385 skypoly[currentskypoly].firstvert = currentskyvert;
386 skypoly[currentskypoly++].verts = p->numverts;
387 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
389 skyvert[currentskyvert].v[0] = v[0];
390 skyvert[currentskyvert].v[1] = v[1];
391 skyvert[currentskyvert++].v[2] = v[2];
399 skypolyrender(); // fogged sky polys, affects depth
401 if (skyname[0] && skyisvisible && !fogenabled)
402 R_Sky(); // does not affect depth, draws over the sky polys
406 for (j = 0;j < cl.worldmodel->numtextures;j++)
408 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
410 // subdivided water surface warp
411 if (!(s->flags & SURF_DRAWTURB))
413 cl.worldmodel->textures[j]->texturechain = NULL;
414 t = R_TextureAnimation (cl.worldmodel->textures[j]);
415 for (;s;s = s->texturechain)
417 // check for lightmap modification
420 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
421 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
422 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
423 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
424 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
425 R_UpdateLightmap(s, s->lightmaptexturenum);
427 for (p = s->polys;p;p = p->next)
429 if (currentwallpoly >= MAX_WALLPOLYS)
431 v = &s->polys->verts[0][0];
432 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
433 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
434 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
435 wallpoly[currentwallpoly].firstvert = currentwallvert;
436 wallpoly[currentwallpoly].numverts = p->numverts;
437 if (wallpoly[currentwallpoly++].lit = s->dlightframe == r_dlightframecount && r_dynamic.value)
439 for (i = 0;i<p->numverts;i++, v += VERTEXSIZE)
443 shadecolor[0] = shadecolor[1] = shadecolor[2] = 0;
444 for (dj = 0;dj < (MAX_DLIGHTS >> 5);dj++)
446 if (s->dlightbits[dj])
449 for (di=0 ; di<32 ; di++)
451 if ((1 << (di&31)) & s->dlightbits[di>>5])
458 dl = &cl_dlights[(dj<<5)+di];
459 VectorSubtract(dl->origin, v, ddist);
460 df = DotProduct(ddist, ddist) + 65536.0f;
461 dr = dl->radius * dl->radius * 16.0f;
464 VectorNormalize(ddist);
465 dt = DotProduct(ddist, s->plane->normal);
466 if (s->flags & SURF_PLANEBACK)
470 dr *= (dt * 0.5f + 0.5f);
476 VectorSubtract(v, ddist, v3); // pull off surface
477 if (s->flags & SURF_PLANEBACK)
479 VectorSubtract(dl->origin, s->plane->normal, v2);
480 VectorSubtract(v3, s->plane->normal, v3);
484 VectorAdd(dl->origin, s->plane->normal, v2);
485 VectorAdd(v3, s->plane->normal, v3);
487 if (SV_TestLine(&cl.worldmodel->hulls[0], 0, v2, v3))
488 // if (SV_TestLine(&cl.worldmodel->hulls[0], 0, dl->origin, v))
492 float dbrightness = dr * 16.0f / df;
493 shadecolor[0] += dbrightness * dl->color[0];
494 shadecolor[1] += dbrightness * dl->color[1];
495 shadecolor[2] += dbrightness * dl->color[2];
504 //R_DynamicLightPoint(shadecolor, v, s->dlightbits);
507 shadecolor[0] *= 0.5f;
508 shadecolor[1] *= 0.5f;
509 shadecolor[2] *= 0.5f;
511 wallvert[currentwallvert].r = (byte) (bound(0, (int) shadecolor[0], 255));
512 wallvert[currentwallvert].g = (byte) (bound(0, (int) shadecolor[1], 255));
513 wallvert[currentwallvert].b = (byte) (bound(0, (int) shadecolor[2], 255));
515 wallvert[currentwallvert].r = (byte) (bound(0, (int) v[9], 255));
516 wallvert[currentwallvert].g = (byte) (bound(0, (int) v[10], 255));
517 wallvert[currentwallvert].b = (byte) (bound(0, (int) v[11], 255));
518 wallvert[currentwallvert].a = 255;
519 wallvert[currentwallvert].vert[0] = v[0];
520 wallvert[currentwallvert].vert[1] = v[1];
521 wallvert[currentwallvert].vert[2] = v[2];
522 wallvert[currentwallvert].s = v[3];
523 wallvert[currentwallvert].t = v[4];
524 wallvert[currentwallvert].u = v[5];
525 wallvert[currentwallvert++].v = v[6];
530 for (i = 0;i<p->numverts;i++, v += VERTEXSIZE)
532 wallvert[currentwallvert].vert[0] = v[0];
533 wallvert[currentwallvert].vert[1] = v[1];
534 wallvert[currentwallvert].vert[2] = v[2];
535 wallvert[currentwallvert].s = v[3];
536 wallvert[currentwallvert].t = v[4];
537 wallvert[currentwallvert].u = v[5];
538 wallvert[currentwallvert++].v = v[6];
548 // then water (water gets diverted to transpoly list)
549 for (j = 0;j < cl.worldmodel->numtextures;j++)
551 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
553 cl.worldmodel->textures[j]->texturechain = NULL;
554 t = R_TextureAnimation (cl.worldmodel->textures[j]);
555 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
556 // subdivided water surface warp
557 if (s->flags & SURF_DRAWTURB)
559 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
560 // FIXME: make fog texture if water texture is transparent?
561 if (r_waterripple.value)
565 for (;s;s = s->texturechain)
567 for (p=s->polys ; p ; p=p->next)
569 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
570 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
571 transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128) >> 1,(int) (v[10]+128) >> 1,(int) (v[11]+128) >> 1,alpha);
578 for (;s;s = s->texturechain)
580 for (p=s->polys ; p ; p=p->next)
582 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
583 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
584 transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128),(int) (v[10]+128),(int) (v[11]+128),alpha);
594 for (;s;s = s->texturechain)
596 for (p=s->polys ; p ; p=p->next)
598 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
599 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
600 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128) >> 1,(int) (v[10]+128) >> 1,(int) (v[11]+128) >> 1,alpha);
607 for (;s;s = s->texturechain)
609 for (p=s->polys ; p ; p=p->next)
611 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
612 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
613 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128),(int) (v[10]+128),(int) (v[11]+128),alpha);
620 int light, alpha, r = 0, g = 0, b = 0;
621 vec3_t nv, shadecolor;
622 alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
624 if (s->flags & SURF_DRAWFULLBRIGHT)
625 r = g = b = lighthalf ? 128 : 255;
626 else if (s->dlightframe == r_dlightframecount && r_dynamic.value)
629 r = g = b = (lighthalf ? 64 : 128) + (int) (r_ambient.value * 2.0f);
630 if (r_waterripple.value)
636 for (;s;s = s->texturechain)
638 for (p=s->polys ; p ; p=p->next)
640 // FIXME: could be a transparent water texture
641 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
642 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
646 nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
647 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
648 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
649 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) shadecolor[0] >> 1,(int) shadecolor[1] >> 1,(int) shadecolor[2] >> 1,alpha);
657 for (;s;s = s->texturechain)
659 for (p=s->polys ; p ; p=p->next)
661 // FIXME: could be a transparent water texture
662 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
663 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
667 nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
668 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
679 for (;s;s = s->texturechain)
681 for (p=s->polys ; p ; p=p->next)
683 // FIXME: could be a transparent water texture
684 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
685 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
689 nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
690 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
691 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
692 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), shadecolor[0],shadecolor[1],shadecolor[2],alpha);
700 for (;s;s = s->texturechain)
702 for (p=s->polys ; p ; p=p->next)
704 // FIXME: could be a transparent water texture
705 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
706 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
710 nv[2] = v[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
711 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
725 for (;s;s = s->texturechain)
727 for (p=s->polys ; p ; p=p->next)
729 // FIXME: could be a transparent water texture
730 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
731 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
733 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
734 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
735 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) shadecolor[0] >> 1,(int) shadecolor[1] >> 1,(int) shadecolor[2] >> 1,alpha);
743 for (;s;s = s->texturechain)
745 for (p=s->polys ; p ; p=p->next)
747 // FIXME: could be a transparent water texture
748 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
749 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
751 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
762 for (;s;s = s->texturechain)
764 for (p=s->polys ; p ; p=p->next)
766 // FIXME: could be a transparent water texture
767 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
768 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
770 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
771 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
772 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), shadecolor[0],shadecolor[1],shadecolor[2],alpha);
780 for (;s;s = s->texturechain)
782 for (p=s->polys ; p ; p=p->next)
784 // FIXME: could be a transparent water texture
785 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
786 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
788 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
801 // LordHavoc: transparent brush models
802 extern int r_dlightframecount;
803 extern float modelalpha;
804 extern vec3_t shadecolor;
805 //qboolean R_CullBox (vec3_t mins, vec3_t maxs);
806 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
807 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
808 void EmitWaterPolys (msurface_t *fa);
809 void R_OldMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
816 void R_DrawBrushModel (entity_t *e)
818 int i, j/*, l*/, smax, tmax, size3, maps;
819 vec3_t mins, maxs, nv;
823 qboolean rotated, vertexlit = false;
824 float dot, *v, scale;
827 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
834 if (e->angles[0] || e->angles[1] || e->angles[2])
837 for (i=0 ; i<3 ; i++)
839 mins[i] = e->origin[i] - clmodel->radius;
840 maxs[i] = e->origin[i] + clmodel->radius;
846 VectorAdd (e->origin, clmodel->mins, mins);
847 VectorAdd (e->origin, clmodel->maxs, maxs);
850 if (R_CullBox (mins, maxs))
853 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
857 vec3_t forward, right, up;
859 VectorCopy (modelorg, temp);
860 AngleVectors (e->angles, forward, right, up);
861 modelorg[0] = DotProduct (temp, forward);
862 modelorg[1] = -DotProduct (temp, right);
863 modelorg[2] = DotProduct (temp, up);
866 s = &clmodel->surfaces[clmodel->firstmodelsurface];
868 // calculate dynamic lighting for bmodel if it's not an
870 if (modelalpha == 1 && clmodel->firstmodelsurface != 0 && !(currententity->effects & EF_FULLBRIGHT) && currententity->colormod[0] == 1 && currententity->colormod[2] == 1 && currententity->colormod[2] == 1)
874 for (k=0 ; k<MAX_DLIGHTS ; k++)
876 if ((cl_dlights[k].die < cl.time) || (!cl_dlights[k].radius))
879 VectorSubtract(cl_dlights[k].origin, currententity->origin, org);
880 R_OldMarkLights (org, &cl_dlights[k], 1<<(k&31), k >> 5, clmodel); //, clmodel->nodes + clmodel->hulls[0].firstclipnode);
886 e->angles[0] = -e->angles[0]; // stupid quake bug
887 softwaretransformforentity (e);
888 e->angles[0] = -e->angles[0]; // stupid quake bug
891 for (j = 0;j < clmodel->nummodelsurfaces;j++, s++)
893 // find which side of the node we are on
896 dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
899 if (((s->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
900 (!(s->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
902 if (s->flags & SURF_DRAWSKY)
904 if (s->flags & SURF_DRAWTURB)
906 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
907 // FIXME: make fog texture if water texture is transparent?
908 if (r_waterripple.value)
912 for (p=s->polys ; p ; p=p->next)
914 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
915 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
917 softwaretransform(v, nv);
918 transpolyvert(nv[0], nv[1], nv[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128) >> 1,(int) (v[10]+128) >> 1,(int) (v[11]+128) >> 1,alpha);
925 for (p=s->polys ; p ; p=p->next)
927 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
928 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
930 softwaretransform(v, nv);
931 transpolyvert(nv[0], nv[1], nv[2] + r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128),(int) (v[10]+128),(int) (v[11]+128),alpha);
941 for (p=s->polys ; p ; p=p->next)
943 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
944 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
946 softwaretransform(v, nv);
947 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128) >> 1,(int) (v[10]+128) >> 1,(int) (v[11]+128) >> 1,alpha);
954 for (p=s->polys ; p ; p=p->next)
956 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
957 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
959 softwaretransform(v, nv);
960 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), (int) (v[9]+128),(int) (v[10]+128),(int) (v[11]+128),alpha);
968 int light, alpha, r = 0, g = 0, b = 0;
971 if (s->flags & SURF_DRAWNOALPHA)
972 alpha = modelalpha*255.0f;
974 alpha = r_wateralpha.value*modelalpha*255.0f;
976 if (s->flags & SURF_DRAWFULLBRIGHT || currententity->effects & EF_FULLBRIGHT)
980 r = 128.0f * currententity->colormod[0];
981 g = 128.0f * currententity->colormod[1];
982 b = 128.0f * currententity->colormod[2];
986 r = 255.0f * currententity->colormod[0];
987 g = 255.0f * currententity->colormod[1];
988 b = 255.0f * currententity->colormod[2];
991 else if (s->dlightframe == r_dlightframecount && r_dynamic.value)
997 r = 64.0f * currententity->colormod[0] + (int) r_ambient.value;
998 g = 64.0f * currententity->colormod[1] + (int) r_ambient.value;
999 b = 64.0f * currententity->colormod[2] + (int) r_ambient.value;
1003 r = 128.0f * currententity->colormod[0] + (int) (r_ambient.value * 2.0f);
1004 g = 128.0f * currententity->colormod[1] + (int) (r_ambient.value * 2.0f);
1005 b = 128.0f * currententity->colormod[2] + (int) (r_ambient.value * 2.0f);
1008 for (p=s->polys ; p ; p=p->next)
1010 // FIXME: could be a transparent water texture
1011 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
1012 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
1014 softwaretransform(v, nv);
1015 if (r_waterripple.value)
1016 nv[2] += r_waterripple.value * turbsin[(int)((v[3]*0.125f+realtime) * TURBSCALE) & 255] * turbsin[(int)((v[4]*0.125f+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
1019 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
1020 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
1023 r = (int) ((float) (shadecolor[0] * currententity->colormod[0])) >> 1;
1024 g = (int) ((float) (shadecolor[1] * currententity->colormod[1])) >> 1;
1025 b = (int) ((float) (shadecolor[2] * currententity->colormod[2])) >> 1;
1029 r = (int) ((float) (shadecolor[0] * currententity->colormod[0]));
1030 g = (int) ((float) (shadecolor[1] * currententity->colormod[1]));
1031 b = (int) ((float) (shadecolor[2] * currententity->colormod[2]));
1034 transpolyvert(nv[0], nv[1], nv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), r,g,b,alpha);
1041 t = R_TextureAnimation (s->texinfo->texture);
1042 if (vertexlit || s->texinfo->texture->transparent)
1044 // FIXME: could be a transparent water texture
1045 if ((currententity->effects & EF_FULLBRIGHT) || !s->samples)
1047 for (p = s->polys;p;p = p->next)
1049 if (currenttranspoly >= MAX_TRANSPOLYS)
1051 v = &p->verts[0][0];
1052 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
1053 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE)
1055 softwaretransform(v, nv);
1056 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], 255,255,255,modelalpha*255.0f);
1063 smax = (s->extents[0]>>4)+1;
1064 tmax = (s->extents[1]>>4)+1;
1065 size3 = smax*tmax*3; // *3 for colored lighting
1066 for (p = s->polys;p;p = p->next)
1068 if (currenttranspoly >= MAX_TRANSPOLYS)
1070 v = &p->verts[0][0];
1071 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
1072 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE)
1074 shadecolor[0] = shadecolor[1] = shadecolor[2] = r_ambient.value * 2.0f;
1075 lm = (byte *)((long) s->samples + ((int) v[8] * smax + (int) v[7]) * 3); // LordHavoc: *3 for colored lighting
1076 for (maps = 0;maps < MAXLIGHTMAPS && s->styles[maps] != 255;maps++)
1078 scale = d_lightstylevalue[s->styles[maps]] * (1.0 / 128.0);
1079 shadecolor[0] += lm[0] * scale;
1080 shadecolor[1] += lm[1] * scale;
1081 shadecolor[2] += lm[2] * scale;
1082 lm += size3; // LordHavoc: *3 for colored lighting
1084 softwaretransform(v, nv);
1085 R_DynamicLightPointNoMask(shadecolor, nv); // LordHavoc: dynamic lighting
1088 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], (int) shadecolor[0] >> 1, (int) shadecolor[1] >> 1, (int) shadecolor[2] >> 1, modelalpha*255.0f);
1092 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], shadecolor[0], shadecolor[1], shadecolor[2], modelalpha*255.0f);
1101 // check for lightmap modification
1102 if (r_dynamic.value)
1104 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
1105 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
1106 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
1107 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
1108 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
1109 R_UpdateLightmap(s, s->lightmaptexturenum);
1111 for (p = s->polys;p;p = p->next)
1113 if (currentwallpoly >= MAX_WALLPOLYS)
1115 v = &s->polys->verts[0][0];
1116 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
1117 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
1118 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
1119 wallpoly[currentwallpoly].firstvert = currentwallvert;
1120 wallpoly[currentwallpoly].numverts = p->numverts;
1121 if (wallpoly[currentwallpoly++].lit = s->dlightframe == r_dlightframecount && r_dynamic.value)
1123 for (i = 0;i<p->numverts;i++, v += VERTEXSIZE)
1126 softwaretransform(v, nv);
1127 shadecolor[0] = shadecolor[1] = shadecolor[2] = 0;
1128 // R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
1129 R_DynamicLightPointNoMask(shadecolor, nv);
1132 shadecolor[0] *= 0.5f;
1133 shadecolor[1] *= 0.5f;
1134 shadecolor[2] *= 0.5f;
1136 wallvert[currentwallvert].r = (byte) (bound(0, (int) shadecolor[0], 255));
1137 wallvert[currentwallvert].g = (byte) (bound(0, (int) shadecolor[1], 255));
1138 wallvert[currentwallvert].b = (byte) (bound(0, (int) shadecolor[2], 255));
1139 wallvert[currentwallvert].a = 255;
1140 wallvert[currentwallvert].vert[0] = nv[0];
1141 wallvert[currentwallvert].vert[1] = nv[1];
1142 wallvert[currentwallvert].vert[2] = nv[2];
1144 softwaretransform(v, wallvert[currentwallvert].vert);
1145 wallvert[currentwallvert].r = (byte) (bound(0, (int) v[9], 255));
1146 wallvert[currentwallvert].g = (byte) (bound(0, (int) v[10], 255));
1147 wallvert[currentwallvert].b = (byte) (bound(0, (int) v[11], 255));
1148 wallvert[currentwallvert].a = 255;
1149 wallvert[currentwallvert].s = v[3];
1150 wallvert[currentwallvert].t = v[4];
1151 wallvert[currentwallvert].u = v[5];
1152 wallvert[currentwallvert++].v = v[6];
1157 for (i = 0;i<p->numverts;i++, v += VERTEXSIZE)
1159 softwaretransform(v, wallvert[currentwallvert].vert);
1160 wallvert[currentwallvert].s = v[3];
1161 wallvert[currentwallvert].t = v[4];
1162 wallvert[currentwallvert].u = v[5];
1163 wallvert[currentwallvert++].v = v[6];
1174 =============================================================
1178 =============================================================
1181 void R_StoreEfrags (efrag_t **ppefrag);
1199 if (!(node = cl.worldmodel->nodes))
1204 // if a leaf node, draw stuff
1205 if (node->contents < 0)
1207 if (node->contents != CONTENTS_SOLID)
1210 pleaf = (mleaf_t *)node;
1213 if ((c = pleaf->nummarksurfaces))
1216 mark = pleaf->firstmarksurface;
1219 (*mark)->visframe = r_framecount;
1224 // deal with model fragments in this leaf
1226 R_StoreEfrags (&pleaf->efrags);
1231 node = nodestack[--s].node;
1232 dot = nodestack[s].dot;
1238 // node is just a decision point, so go down the apropriate sides
1240 // find which side of the node we are on
1241 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1243 // recurse down the children, front side first
1245 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1247 nodestack[s].node = node;
1248 nodestack[s++].dot = dot;
1249 node = node->children[side];
1257 if ((c = node->numsurfaces))
1260 surf = cl.worldmodel->surfaces + node->firstsurface;
1264 for (;c;c--, surf++)
1266 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1268 surf->texturechain = surf->texinfo->texture->texturechain;
1269 surf->texinfo->texture->texturechain = surf;
1275 for (;c;c--, surf++)
1277 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1279 surf->texturechain = surf->texinfo->texture->texturechain;
1280 surf->texinfo->texture->texturechain = surf;
1286 // recurse down the back side
1287 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1289 node = node->children[side];
1295 node = nodestack[--s].node;
1296 dot = nodestack[s].dot;
1307 void R_DrawWorld (void)
1311 memset (&ent, 0, sizeof(ent));
1312 ent.model = cl.worldmodel;
1313 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1314 modelalpha = ent.alpha = 1;
1317 VectorCopy (r_refdef.vieworg, modelorg);
1319 currententity = &ent;
1321 softwaretransformidentity(); // LordHavoc: clear transform
1322 skyisvisible = false;
1327 glClear (GL_DEPTH_BUFFER_BIT);
1329 R_PushDlights (); // now mark the lit surfaces
1331 DrawTextureChains ();
1333 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1343 void R_MarkLeaves (void)
1349 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1353 r_oldviewleaf = r_viewleaf;
1357 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1359 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1362 if (node->visframe == r_visframecount)
1364 node->visframe = r_visframecount;
1365 node = node->parent;
1371 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1373 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1375 if (vis[i>>3] & (1<<(i&7)))
1377 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1380 if (node->visframe == r_visframecount)
1382 node->visframe = r_visframecount;
1383 node = node->parent;
1393 =============================================================================
1397 =============================================================================
1400 // returns a texture number and the position inside it
1401 int AllocBlock (int w, int h, int *x, int *y)
1407 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1409 best = BLOCK_HEIGHT;
1411 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1415 for (j=0 ; j<w ; j++)
1417 if (allocated[texnum][i+j] >= best)
1419 if (allocated[texnum][i+j] > best2)
1420 best2 = allocated[texnum][i+j];
1423 { // this is a valid spot
1429 if (best + h > BLOCK_HEIGHT)
1432 if (nosubimagefragments || nosubimage)
1434 if (!lightmaps[texnum])
1435 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1437 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1438 else if (!allocated[texnum][0])
1440 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1441 memset(blank, 0, sizeof(blank));
1442 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1443 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1444 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1446 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1448 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1451 for (i=0 ; i<w ; i++)
1452 allocated[texnum][*x + i] = best + h;
1457 Sys_Error ("AllocBlock: full");
1462 mvertex_t *r_pcurrentvertbase;
1463 model_t *currentmodel;
1469 BuildSurfaceDisplayList
1472 void BuildSurfaceDisplayList (msurface_t *fa)
1474 int i, lindex, lnumverts;
1475 medge_t *pedges, *r_pedge;
1481 // reconstruct the polygon
1482 pedges = currentmodel->edges;
1483 lnumverts = fa->numedges;
1489 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1490 poly->next = fa->polys;
1491 poly->flags = fa->flags;
1493 poly->numverts = lnumverts;
1495 for (i=0 ; i<lnumverts ; i++)
1497 lindex = currentmodel->surfedges[fa->firstedge + i];
1501 r_pedge = &pedges[lindex];
1502 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1506 r_pedge = &pedges[-lindex];
1507 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1509 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1510 s /= fa->texinfo->texture->width;
1512 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1513 t /= fa->texinfo->texture->height;
1515 VectorCopy (vec, poly->verts[i]);
1516 poly->verts[i][3] = s;
1517 poly->verts[i][4] = t;
1520 // lightmap texture coordinates
1522 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1523 s -= fa->texturemins[0];
1524 poly->verts[i][7] = bound(0l, ((int)s>>4), (fa->extents[0]>>4)); // LordHavoc: raw lightmap coordinates
1525 s += fa->light_s*16;
1527 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1529 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1530 t -= fa->texturemins[1];
1531 poly->verts[i][8] = bound(0l, ((int)t>>4), (fa->extents[1]>>4)); // LordHavoc: raw lightmap coordinates
1532 t += fa->light_t*16;
1534 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1536 poly->verts[i][5] = s;
1537 poly->verts[i][6] = t;
1541 // remove co-linear points - Ed
1544 if (!gl_keeptjunctions.value)
1546 for (i = 0 ; i < lnumverts ; ++i)
1549 float *prev, *this, *next;
1551 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1552 this = poly->verts[i];
1553 next = poly->verts[(i + 1) % lnumverts];
1555 VectorSubtract( this, prev, v1 );
1556 VectorNormalize( v1 );
1557 VectorSubtract( next, prev, v2 );
1558 VectorNormalize( v2 );
1560 // skip co-linear points
1561 #define COLINEAR_EPSILON 0.001
1562 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1563 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1564 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1567 for (j = i + 1; j < lnumverts; ++j)
1570 for (k = 0; k < VERTEXSIZE; ++k)
1571 poly->verts[j - 1][k] = poly->verts[j][k];
1575 // retry next vertex next time, which is now current vertex
1581 poly->numverts = lnumverts;
1584 int i, k, lindex, lnumverts;
1585 medge_t *pedges, *r_pedge;
1586 int vertpage, points;
1590 float point1[1024][VERTEXSIZE], point[1024][VERTEXSIZE];
1592 // reconstruct the polygon
1593 pedges = currentmodel->edges;
1594 lnumverts = fa->numedges;
1600 for (i=0 ; i<lnumverts ; i++)
1602 lindex = currentmodel->surfedges[fa->firstedge + i];
1606 r_pedge = &pedges[lindex];
1607 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1611 r_pedge = &pedges[-lindex];
1612 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1614 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1615 s /= fa->texinfo->texture->width;
1617 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1618 t /= fa->texinfo->texture->height;
1620 VectorCopy (vec, point1[i]);
1625 // lightmap texture coordinates
1627 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1628 s -= fa->texturemins[0];
1629 point1[i][7] = bound(0l, ((int)s>>4), (fa->extents[0]>>4)); // LordHavoc: raw lightmap coordinates
1630 s += fa->light_s*16;
1632 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1634 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1635 t -= fa->texturemins[1];
1636 point1[i][8] = bound(0l, ((int)t>>4), (fa->extents[1]>>4)); // LordHavoc: raw lightmap coordinates
1637 t += fa->light_t*16;
1639 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1645 if (fa->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1647 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1648 poly->next = fa->polys;
1649 poly->flags = fa->flags;
1651 poly->numverts = lnumverts;
1652 memcpy(poly->verts, &point1[0][0], lnumverts*VERTEXSIZE*sizeof(float));
1656 #define VectorCopy9(a,b) {for(k = 0;k < VERTEXSIZE;k++) b[k]=a[k];}
1660 float center[VERTEXSIZE];
1661 // subdivide by placing a point at the center (more tris)
1663 // you, the reader, have stumbled upon the most amusing visual artifact I have
1664 // encountered to date, saved here for historical/hysterical reasons :)
1665 if (gl_funnywalls.value)
1666 for (j = 0;j < 5;j++)
1669 for (j = 0;j < VERTEXSIZE;j++)
1671 for (i = 0;i < lnumverts;i++)
1672 for (j = 0;j < VERTEXSIZE;j++)
1673 center[j] += point1[i][j];
1674 s = 1.0f / lnumverts;
1675 for (i = 0;i < VERTEXSIZE;i++)
1677 for (i = 0;i < lnumverts;i++)
1679 VectorCopy9(center, point[points]);points++;
1680 VectorCopy9(point1[i], point[points]);points++;
1681 VectorCopy9(point1[(i+1)%lnumverts], point[points]);points++;
1684 // subdivide by turning it into a fan (less tris)
1685 for (i = 1;i < lnumverts-1;i++)
1687 VectorCopy9(point1[0], point[points]);points++;
1688 VectorCopy9(point1[i], point[points]);points++;
1689 VectorCopy9(point1[i+1], point[points]);points++;
1694 float p1[VERTEXSIZE], p2[VERTEXSIZE], p3[VERTEXSIZE], p4[VERTEXSIZE], p5[VERTEXSIZE], p6[VERTEXSIZE]
1695 // now subdivide any large triangles
1696 for (j = 0;j < points;j+=3)
1698 if (points > (1024-9))
1700 while ((max(point[j][0], max(point[j+1][0], point[j+2][0])) - min(point[j][0], min(point[j+1][0], point[j+2][0]))) > 128
1701 || (max(point[j][1], max(point[j+1][1], point[j+2][1])) - min(point[j][1], min(point[j+1][1], point[j+2][1]))) > 128
1702 || (max(point[j][2], max(point[j+1][2], point[j+2][2])) - min(point[j][2], min(point[j+1][2], point[j+2][2]))) > 128)
1704 if (points > (1024-9))
1706 #define halfway(v, a, b) for (k = 0;k < VERTEXSIZE;k++) v[k] = (a[k] + b[k]) * 0.5f;
1707 VectorCopy9(point[j+0], p1);
1708 VectorCopy9(point[j+1], p3);
1709 VectorCopy9(point[j+2], p5);
1710 halfway(p2, p1, p3);
1711 halfway(p4, p3, p5);
1712 halfway(p6, p5, p1);
1713 // build tri 1 (top middle)
1714 VectorCopy9(p1, point[j+0]);
1715 VectorCopy9(p2, point[j+1]);
1716 VectorCopy9(p6, point[j+2]);
1717 // build tri 2 (bottom right)
1718 VectorCopy9(p2, point[points+0]);
1719 VectorCopy9(p3, point[points+1]);
1720 VectorCopy9(p4, point[points+2]);
1721 // build tri 3 (bottom left)
1722 VectorCopy9(p4, point[points+3]);
1723 VectorCopy9(p5, point[points+4]);
1724 VectorCopy9(p6, point[points+5]);
1725 // build tri 4 (middle)
1726 VectorCopy9(p2, point[points+6]);
1727 VectorCopy9(p4, point[points+7]);
1728 VectorCopy9(p6, point[points+8]);
1734 poly = Hunk_Alloc (sizeof(glpoly_t) + (points-4) * VERTEXSIZE*sizeof(float));
1735 poly->next = fa->polys;
1736 poly->flags = fa->flags;
1739 poly->numtris = points / 3;
1740 memcpy(&poly->verts[0][0], &point[0][0], points * VERTEXSIZE*sizeof(float));
1745 ========================
1746 GL_CreateSurfaceLightmap
1747 ========================
1749 void GL_CreateSurfaceLightmap (msurface_t *surf)
1753 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1756 smax = (surf->extents[0]>>4)+1;
1757 tmax = (surf->extents[1]>>4)+1;
1759 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1760 if (nosubimage || nosubimagefragments)
1762 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1763 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1766 R_BuildLightMap (surf, templight, smax * 4);
1767 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1771 R_BuildLightMap (surf, templight, smax * 3);
1772 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1781 Builds the lightmap texture
1782 with all the surfaces from all brush models
1785 void GL_BuildLightmaps (void)
1790 memset (allocated, 0, sizeof(allocated));
1792 r_framecount = 1; // no dlightcache
1794 if (gl_nosubimagefragments.value)
1795 nosubimagefragments = 1;
1797 nosubimagefragments = 0;
1799 if (gl_nosubimage.value)
1804 if (gl_lightmaprgba.value)
1806 lightmaprgba = true;
1811 lightmaprgba = false;
1815 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1816 // it needs to be aligned on 4 pixel boundaries...
1817 // so I implemented an adjustable lightmap alignment
1818 if (gl_lightmapalign.value < 1)
1819 gl_lightmapalign.value = 1;
1820 if (gl_lightmapalign.value > 16)
1821 gl_lightmapalign.value = 16;
1823 while (lightmapalign < gl_lightmapalign.value)
1824 lightmapalign <<= 1;
1825 gl_lightmapalign.value = lightmapalign;
1826 lightmapalignmask = ~(lightmapalign - 1);
1827 if (nosubimagefragments || nosubimage)
1830 lightmapalignmask = ~0;
1833 if (!lightmap_textures)
1835 lightmap_textures = texture_extension_number;
1836 texture_extension_number += MAX_LIGHTMAPS;
1839 for (j=1 ; j<MAX_MODELS ; j++)
1841 m = cl.model_precache[j];
1844 if (m->name[0] == '*')
1846 r_pcurrentvertbase = m->vertexes;
1848 for (i=0 ; i<m->numsurfaces ; i++)
1850 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1852 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1854 GL_CreateSurfaceLightmap (m->surfaces + i);
1855 BuildSurfaceDisplayList (m->surfaces + i);
1859 if (nosubimage || nosubimagefragments)
1862 qglSelectTexture(gl_mtex_enum+1);
1863 for (i = 0;i < MAX_LIGHTMAPS;i++)
1865 if (!allocated[i][0])
1867 lightmapupdate[i][0] = BLOCK_HEIGHT;
1868 lightmapupdate[i][1] = 0;
1869 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1870 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1871 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1873 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1875 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1878 qglSelectTexture(gl_mtex_enum+0);