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*3)
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"};
50 qboolean lightmaprgba, nosubimagefragments, nosubimage;
53 qboolean skyisvisible;
54 extern qboolean gl_arrays;
59 for (i = 0;i < MAX_LIGHTMAPS;i++)
61 Cvar_RegisterVariable(&gl_lightmapalign);
62 Cvar_RegisterVariable(&gl_lightmaprgba);
63 Cvar_RegisterVariable(&gl_nosubimagefragments);
64 Cvar_RegisterVariable(&gl_nosubimage);
65 // check if it's the glquake minigl driver
66 if (strnicmp(gl_vendor,"3Dfx",4)==0)
69 Cvar_SetValue("gl_nosubimagefragments", 1);
70 // Cvar_SetValue("gl_nosubimage", 1);
71 Cvar_SetValue("gl_lightmode", 0);
75 int dlightdivtable[8192];
76 int dlightdivtableinitialized = 0;
83 void R_AddDynamicLights (msurface_t *surf)
85 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, j;
89 // use 64bit integer... shame it's not very standardized...
90 #if _MSC_VER || __BORLANDC__
96 if (!dlightdivtableinitialized)
98 dlightdivtable[0] = 1048576 >> 7;
99 for (s = 1;s < 8192;s++)
100 dlightdivtable[s] = 1048576 / (s << 7);
101 dlightdivtableinitialized = 1;
104 smax = (surf->extents[0]>>4)+1;
105 tmax = (surf->extents[1]>>4)+1;
107 for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
109 if ( !(surf->dlightbits[lnum >> 5] & (1<<(lnum&31)) ) )
110 continue; // not lit by this light
112 VectorSubtract(cl_dlights[lnum].origin, currententity->origin, local);
113 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
114 for (i=0 ; i<3 ; i++)
115 impact[i] = cl_dlights[lnum].origin[i] - surf->plane->normal[i]*dist;
117 f = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
120 // reduce calculations
122 for (s = 0;s < smax;s++, i -= 16)
123 sdtable[s] = i*i + t;
125 f = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
128 maxdist = (int) (cl_dlights[lnum].radius*cl_dlights[lnum].radius); // for comparisons to minimum acceptable light
129 // clamp radius to avoid exceeding 8192 entry division table
130 if (maxdist > 1048576)
132 maxdist3 = maxdist - (int) (dist*dist);
133 // convert to 8.8 blocklights format
134 if (!cl_dlights[lnum].dark)
136 f = cl_dlights[lnum].color[0] * maxdist;red = f;
137 f = cl_dlights[lnum].color[1] * maxdist;green = f;
138 f = cl_dlights[lnum].color[2] * maxdist;blue = f;
140 else // negate for darklight
142 f = cl_dlights[lnum].color[0] * -maxdist;red = f;
143 f = cl_dlights[lnum].color[1] * -maxdist;green = f;
144 f = cl_dlights[lnum].color[2] * -maxdist;blue = f;
147 for (t = 0;t < tmax;t++,i -= 16)
150 if (td < maxdist3) // make sure some part of it is visible on this line
152 maxdist2 = maxdist - td;
153 for (s = 0;s < smax;s++)
155 if (sdtable[s] < maxdist2)
157 j = dlightdivtable[(sdtable[s]+td) >> 7];
158 k = (red * j) >> 8;bl[0] += k;
159 k = (green * j) >> 8;bl[1] += k;
160 k = (blue * j) >> 8;bl[2] += k;
166 bl+=smax*3; // skip line
171 extern qboolean lighthalf;
176 Combine and scale multiple lightmaps into the 8.8 format in blocklights
179 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
189 surf->cached_dlight = (surf->dlightframe == r_framecount);
190 surf->cached_lighthalf = lighthalf;
192 smax = (surf->extents[0]>>4)+1;
193 tmax = (surf->extents[1]>>4)+1;
195 lightmap = surf->samples;
197 // set to full bright if no light data
198 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
201 for (i=0 ; i<size ; i++)
212 for (i=0 ; i<size ; i++)
219 // add all the lightmaps
221 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
223 scale = d_lightstylevalue[surf->styles[maps]];
224 surf->cached_light[maps] = scale; // 8.8 fraction
226 for (i=0 ; i<size ; i++)
228 *bl++ += *lightmap++ * scale;
229 *bl++ += *lightmap++ * scale;
230 *bl++ += *lightmap++ * scale;
234 // add all the dynamic lights
235 if (surf->dlightframe == r_framecount)
236 R_AddDynamicLights (surf);
238 stride -= (smax*lightmapbytes);
242 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
243 // the image is brightened as a processing pass
246 for (i=0 ; i<tmax ; i++, dest += stride)
248 for (j=0 ; j<smax ; j++)
250 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
251 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
252 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
259 for (i=0 ; i<tmax ; i++, dest += stride)
261 for (j=0 ; j<smax ; j++)
263 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
264 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
265 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
274 for (i=0 ; i<tmax ; i++, dest += stride)
276 for (j=0 ; j<smax ; j++)
278 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
279 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
280 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
287 for (i=0 ; i<tmax ; i++, dest += stride)
289 for (j=0 ; j<smax ; j++)
291 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
292 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
293 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
300 byte templight[32*32*4];
302 void R_UpdateLightmap(msurface_t *s, int lnum)
305 // upload the new lightmap texture fragment
306 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
307 if (nosubimage || nosubimagefragments)
309 if (lightmapupdate[lnum][0] > s->light_t)
310 lightmapupdate[lnum][0] = s->light_t;
311 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
312 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
314 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
316 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
320 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
321 tmax = (s->extents[1]>>4)+1;
324 R_BuildLightMap (s, templight, smax * 4);
325 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
329 R_BuildLightMap (s, templight, smax * 3);
330 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
340 Returns the proper texture for a given time and base texture
343 texture_t *R_TextureAnimation (texture_t *base)
349 if (currententity->frame)
351 if (base->alternate_anims)
352 base = base->alternate_anims;
355 if (!base->anim_total)
360 relative = (int)(cl.time*10) % base->anim_total;
363 while (base->anim_min > relative || base->anim_max <= relative)
365 base = base->anim_next;
368 Con_Printf("R_TextureAnimation: broken cycle");
373 Con_Printf("R_TextureAnimation: infinite cycle");
383 =============================================================
387 =============================================================
391 extern int solidskytexture;
392 extern int alphaskytexture;
393 extern float speedscale; // for top sky and bottom sky
395 qboolean mtexenabled = false;
397 extern char skyname[];
399 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
400 //extern cvar_t r_dynamicwater;
401 extern int r_dlightframecount;
404 #include "gl_warp_sin.h"
406 #define TURBSCALE (256.0 / (2 * M_PI))
409 void UploadLightmaps()
412 if (nosubimage || nosubimagefragments)
414 for (i = 0;i < MAX_LIGHTMAPS;i++)
416 if (lightmapupdate[i][0] < lightmapupdate[i][1])
418 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
422 glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
424 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
429 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]));
431 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]));
434 lightmapupdate[i][0] = BLOCK_HEIGHT;
435 lightmapupdate[i][1] = 0;
445 extern qboolean hlbsp;
447 extern char skyname[];
448 void DrawTextureChains (void)
455 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
459 for (j = 0;j < cl.worldmodel->numtextures;j++)
461 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
463 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
464 // subdivided water surface warp
465 if (s->flags & SURF_DRAWSKY)
467 cl.worldmodel->textures[j]->texturechain = NULL;
468 t = R_TextureAnimation (cl.worldmodel->textures[j]);
470 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
472 for (;s;s = s->texturechain)
474 for (p=s->polys ; p ; p=p->next)
476 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
478 skypoly[currentskypoly].firstvert = currentskyvert;
479 skypoly[currentskypoly++].verts = p->numverts;
480 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
482 skyvert[currentskyvert].v[0] = v[0];
483 skyvert[currentskyvert].v[1] = v[1];
484 skyvert[currentskyvert++].v[2] = v[2];
492 skypolyrender(); // fogged sky polys, affects depth
494 if (skyname[0] && skyisvisible && !fogenabled)
495 R_Sky(); // does not affect depth, draws over the sky polys
499 for (j = 0;j < cl.worldmodel->numtextures;j++)
501 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
503 if (!(s->flags & SURF_DRAWTURB))
505 cl.worldmodel->textures[j]->texturechain = NULL;
506 t = R_TextureAnimation (cl.worldmodel->textures[j]);
508 for (;s;s = s->texturechain)
510 if (currentwallpoly < MAX_WALLPOLYS && currentwallvert < MAX_WALLVERTS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
512 // check for lightmap modification
513 // if (r_dynamic.value)
515 if (s->dlightframe == r_framecount || s->cached_dlight || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed
516 R_UpdateLightmap(s, s->lightmaptexturenum);
518 for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
519 if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
521 R_UpdateLightmap(s, s->lightmaptexturenum);
525 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
526 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
527 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
528 wallpoly[currentwallpoly].firstvert = currentwallvert;
529 wallpoly[currentwallpoly++].verts = s->polys->numverts;
530 for (i = 0,v = s->polys->verts[0];i<s->polys->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];
547 // then water (water gets diverted to transpoly list)
548 for (j = 0;j < cl.worldmodel->numtextures;j++)
550 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
552 cl.worldmodel->textures[j]->texturechain = NULL;
553 t = R_TextureAnimation (cl.worldmodel->textures[j]);
554 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
555 // subdivided water surface warp
556 if (s->flags & SURF_DRAWTURB)
558 int light, alpha, r, g, b;
559 vec3_t nv, shadecolor;
560 alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
562 if (s->flags & SURF_DRAWFULLBRIGHT)
563 r = g = b = lighthalf ? 128 : 255;
564 else if (s->dlightframe == r_dlightframecount/* && r_dynamicwater.value*/)
567 r = g = b = lighthalf ? 64 : 128;
568 if (r_waterripple.value)
574 for (;s;s = s->texturechain)
576 for (p=s->polys ; p ; p=p->next)
578 // FIXME: could be a transparent water texture
579 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
580 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
584 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);
585 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
586 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
587 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);
595 for (;s;s = s->texturechain)
597 for (p=s->polys ; p ; p=p->next)
599 // FIXME: could be a transparent water texture
600 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
601 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
605 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);
606 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);
617 for (;s;s = s->texturechain)
619 for (p=s->polys ; p ; p=p->next)
621 // FIXME: could be a transparent water texture
622 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
623 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
627 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);
628 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
629 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
630 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);
638 for (;s;s = s->texturechain)
640 for (p=s->polys ; p ; p=p->next)
642 // FIXME: could be a transparent water texture
643 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
644 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
648 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);
649 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);
663 for (;s;s = s->texturechain)
665 for (p=s->polys ; p ; p=p->next)
667 // FIXME: could be a transparent water texture
668 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
669 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
671 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
672 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
673 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);
681 for (;s;s = s->texturechain)
683 for (p=s->polys ; p ; p=p->next)
685 // FIXME: could be a transparent water texture
686 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
687 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
689 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);
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)
708 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
709 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
710 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);
718 for (;s;s = s->texturechain)
720 for (p=s->polys ; p ; p=p->next)
722 // FIXME: could be a transparent water texture
723 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
724 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
726 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);
738 // LordHavoc: transparent brush models
739 extern int r_dlightframecount;
740 extern float modelalpha;
741 extern vec3_t shadecolor;
742 //qboolean R_CullBox (vec3_t mins, vec3_t maxs);
743 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
744 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
745 void EmitWaterPolys (msurface_t *fa);
746 void R_MarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node);
753 void R_DrawBrushModel (entity_t *e)
755 int i, j, k, smax, tmax, size3, maps;
756 vec3_t mins, maxs, nv;
760 qboolean rotated, vertexlit = false;
761 float dot, *v, scale;
764 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
770 if (e->angles[0] || e->angles[1] || e->angles[2])
773 for (i=0 ; i<3 ; i++)
775 mins[i] = e->origin[i] - clmodel->radius;
776 maxs[i] = e->origin[i] + clmodel->radius;
782 VectorAdd (e->origin, clmodel->mins, mins);
783 VectorAdd (e->origin, clmodel->maxs, maxs);
786 if (R_CullBox (mins, maxs))
789 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
793 vec3_t forward, right, up;
795 VectorCopy (modelorg, temp);
796 AngleVectors (e->angles, forward, right, up);
797 modelorg[0] = DotProduct (temp, forward);
798 modelorg[1] = -DotProduct (temp, right);
799 modelorg[2] = DotProduct (temp, up);
802 s = &clmodel->surfaces[clmodel->firstmodelsurface];
804 // calculate dynamic lighting for bmodel if it's not an
806 if (modelalpha == 1 && clmodel->firstmodelsurface != 0 && !(currententity->effects & EF_FULLBRIGHT) && currententity->colormod[0] == 1 && currententity->colormod[2] == 1 && currententity->colormod[2] == 1)
808 // if (!gl_flashblend.value)
811 for (k=0 ; k<MAX_DLIGHTS ; k++)
813 if ((cl_dlights[k].die < cl.time) || (!cl_dlights[k].radius))
816 VectorSubtract(cl_dlights[k].origin, currententity->origin, org);
817 R_MarkLights (org, &cl_dlights[k], 1<<(k&31), k >> 5, clmodel->nodes + clmodel->hulls[0].firstclipnode);
824 e->angles[0] = -e->angles[0]; // stupid quake bug
825 softwaretransformforentity (e);
826 e->angles[0] = -e->angles[0]; // stupid quake bug
829 for (j = 0;j < clmodel->nummodelsurfaces;j++, s++)
831 // find which side of the node we are on
834 dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
837 if (((s->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
838 (!(s->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
840 if (s->flags & SURF_DRAWSKY)
842 if (s->flags & SURF_DRAWTURB)
845 int light, alpha, r, g, b;
848 if (s->flags & SURF_DRAWNOALPHA)
849 alpha = modelalpha*255.0f;
851 alpha = r_wateralpha.value*modelalpha*255.0f;
853 if (s->flags & SURF_DRAWFULLBRIGHT || currententity->effects & EF_FULLBRIGHT)
857 r = 128.0f * currententity->colormod[0];
858 g = 128.0f * currententity->colormod[1];
859 b = 128.0f * currententity->colormod[2];
863 r = 255.0f * currententity->colormod[0];
864 g = 255.0f * currententity->colormod[1];
865 b = 255.0f * currententity->colormod[2];
868 else if (s->dlightframe == r_dlightframecount/* && r_dynamicwater.value*/)
874 r = 64.0f * currententity->colormod[0];
875 g = 64.0f * currententity->colormod[1];
876 b = 64.0f * currententity->colormod[2];
880 r = 128.0f * currententity->colormod[0];
881 g = 128.0f * currententity->colormod[1];
882 b = 128.0f * currententity->colormod[2];
885 for (p=s->polys ; p ; p=p->next)
887 // FIXME: could be a transparent water texture
888 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
889 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
891 softwaretransform(v, nv);
892 if (r_waterripple.value)
893 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);
896 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128;
897 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
900 r = (int) ((float) (shadecolor[0] * currententity->colormod[0])) >> 1;
901 g = (int) ((float) (shadecolor[1] * currententity->colormod[1])) >> 1;
902 b = (int) ((float) (shadecolor[2] * currententity->colormod[2])) >> 1;
906 r = (int) ((float) (shadecolor[0] * currententity->colormod[0]));
907 g = (int) ((float) (shadecolor[1] * currententity->colormod[1]));
908 b = (int) ((float) (shadecolor[2] * currententity->colormod[2]));
911 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);
918 t = R_TextureAnimation (s->texinfo->texture);
919 v = s->polys->verts[0];
920 if (vertexlit || s->texinfo->texture->transparent)
922 // FIXME: could be a transparent water texture
923 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
924 if ((currententity->effects & EF_FULLBRIGHT) || !s->samples)
926 for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
928 softwaretransform(v, nv);
929 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], 255,255,255,modelalpha*255.0f);
934 smax = (s->extents[0]>>4)+1;
935 tmax = (s->extents[1]>>4)+1;
936 size3 = smax*tmax*3; // *3 for colored lighting
937 for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
939 shadecolor[0] = shadecolor[1] = shadecolor[2] = 0;
940 lm = (byte *)((long) s->samples + ((int) v[8] * smax + (int) v[7]) * 3); // LordHavoc: *3 for colored lighting
941 for (maps = 0;maps < MAXLIGHTMAPS && s->styles[maps] != 255;maps++)
943 scale = d_lightstylevalue[s->styles[maps]] * (1.0 / 128.0);
944 shadecolor[0] += lm[0] * scale;
945 shadecolor[1] += lm[1] * scale;
946 shadecolor[2] += lm[2] * scale;
947 lm += size3; // LordHavoc: *3 for colored lighting
949 softwaretransform(v, nv);
950 R_DynamicLightPointNoMask(shadecolor, nv); // LordHavoc: dynamic lighting
953 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);
957 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], shadecolor[0], shadecolor[1], shadecolor[2], modelalpha*255.0f);
965 // check for lightmap modification
966 // if (r_dynamic.value)
968 if (s->dlightframe == r_framecount || s->cached_dlight || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed
969 R_UpdateLightmap(s, s->lightmaptexturenum);
971 for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
972 if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
974 R_UpdateLightmap(s, s->lightmaptexturenum);
978 if (currentwallpoly < MAX_WALLPOLYS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
980 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
981 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
982 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
983 wallpoly[currentwallpoly].firstvert = currentwallvert;
984 wallpoly[currentwallpoly++].verts = s->polys->numverts;
985 for (i = 0;i<s->polys->numverts;i++, v += VERTEXSIZE)
987 softwaretransform(v, wallvert[currentwallvert].vert);
988 wallvert[currentwallvert].s = v[3];
989 wallvert[currentwallvert].t = v[4];
990 wallvert[currentwallvert].u = v[5];
991 wallvert[currentwallvert++].v = v[6];
1001 =============================================================
1005 =============================================================
1008 void R_StoreEfrags (efrag_t **ppefrag);
1012 R_RecursiveWorldNode
1015 //extern qboolean R_CullBox (vec3_t mins, vec3_t maxs);
1017 void R_RecursiveWorldNode (mnode_t *node)
1023 // if a leaf node, draw stuff
1024 if (node->contents < 0)
1027 pleaf = (mleaf_t *)node;
1029 if (c = pleaf->nummarksurfaces)
1032 mark = pleaf->firstmarksurface;
1035 (*mark)->visframe = r_framecount;
1040 // deal with model fragments in this leaf
1042 R_StoreEfrags (&pleaf->efrags);
1047 // node is just a decision point, so go down the apropriate sides
1049 // find which side of the node we are on
1050 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1052 // recurse down the children, front side first
1054 // LordHavoc: save a stack frame by avoiding a call
1055 // if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1056 // LordHavoc: inlined further to reduce conditions
1057 if (node->children[side]->contents != CONTENTS_SOLID
1058 && node->children[side]->visframe == r_visframecount
1059 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1060 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1061 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1062 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1063 R_RecursiveWorldNode (node->children[side]);
1068 if (c = node->numsurfaces)
1071 surf = cl.worldmodel->surfaces + node->firstsurface;
1073 // LordHavoc: caused a crash due to texsort (it could render twice...)
1075 //side = dot >= -BACKFACE_EPSILON;
1078 for (;c;c--, surf++)
1080 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1082 surf->texturechain = surf->texinfo->texture->texturechain;
1083 surf->texinfo->texture->texturechain = surf;
1089 for (;c;c--, surf++)
1091 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1093 surf->texturechain = surf->texinfo->texture->texturechain;
1094 surf->texinfo->texture->texturechain = surf;
1100 // recurse down the back side
1101 // LordHavoc: save a stack frame by avoiding a call
1102 // if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1103 // LordHavoc: inlined further to reduce conditions
1104 if (node->children[side]->contents != CONTENTS_SOLID
1105 && node->children[side]->visframe == r_visframecount
1106 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1107 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1108 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1109 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1111 node = node->children[side];
1114 // R_RecursiveWorldNode (node->children[side]);
1131 if (!(node = cl.worldmodel->nodes))
1136 // if a leaf node, draw stuff
1138 if (node->contents < 0)
1140 if (node->contents != CONTENTS_SOLID)
1143 pleaf = (mleaf_t *)node;
1145 if (c = pleaf->nummarksurfaces)
1148 mark = pleaf->firstmarksurface;
1151 (*mark)->visframe = r_framecount;
1156 // deal with model fragments in this leaf
1158 R_StoreEfrags (&pleaf->efrags);
1163 node = nodestack[--s].node;
1164 dot = nodestack[s].dot;
1168 // node is just a decision point, so go down the apropriate sides
1170 // find which side of the node we are on
1171 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1173 // recurse down the children, front side first
1175 if (node->children[side]->visframe == r_visframecount
1176 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1177 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1178 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1179 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1181 nodestack[s].node = node;
1182 nodestack[s++].dot = dot;
1183 node = node->children[side];
1191 if (c = node->numsurfaces)
1194 surf = cl.worldmodel->surfaces + node->firstsurface;
1198 for (;c;c--, surf++)
1200 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1202 surf->texturechain = surf->texinfo->texture->texturechain;
1203 surf->texinfo->texture->texturechain = surf;
1209 for (;c;c--, surf++)
1211 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1213 surf->texturechain = surf->texinfo->texture->texturechain;
1214 surf->texinfo->texture->texturechain = surf;
1220 // recurse down the back side
1221 if (node->children[side]->visframe == r_visframecount
1222 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1223 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1224 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1225 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1227 node = node->children[side];
1233 node = nodestack[--s].node;
1234 dot = nodestack[s].dot;
1245 void R_DrawWorld (void)
1249 memset (&ent, 0, sizeof(ent));
1250 ent.model = cl.worldmodel;
1251 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1252 modelalpha = ent.alpha = 1;
1255 VectorCopy (r_refdef.vieworg, modelorg);
1257 currententity = &ent;
1259 softwaretransformidentity(); // LordHavoc: clear transform
1260 skyisvisible = false;
1265 glClear (GL_DEPTH_BUFFER_BIT);
1267 DrawTextureChains ();
1269 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1270 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1279 void R_MarkLeaves (void)
1285 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1289 r_oldviewleaf = r_viewleaf;
1293 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1295 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1298 if (node->visframe == r_visframecount)
1300 node->visframe = r_visframecount;
1301 node = node->parent;
1307 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1309 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1311 if (vis[i>>3] & (1<<(i&7)))
1313 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1316 if (node->visframe == r_visframecount)
1318 node->visframe = r_visframecount;
1319 node = node->parent;
1329 =============================================================================
1333 =============================================================================
1336 // returns a texture number and the position inside it
1337 int AllocBlock (int w, int h, int *x, int *y)
1343 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1345 best = BLOCK_HEIGHT;
1347 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1351 for (j=0 ; j<w ; j++)
1353 if (allocated[texnum][i+j] >= best)
1355 if (allocated[texnum][i+j] > best2)
1356 best2 = allocated[texnum][i+j];
1359 { // this is a valid spot
1365 if (best + h > BLOCK_HEIGHT)
1368 if (nosubimagefragments || nosubimage)
1370 if (!lightmaps[texnum])
1371 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1373 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1374 else if (!allocated[texnum][0])
1376 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1377 memset(blank, 0, sizeof(blank));
1378 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1379 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1380 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1382 glTexImage2D (GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1384 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1387 for (i=0 ; i<w ; i++)
1388 allocated[texnum][*x + i] = best + h;
1393 Sys_Error ("AllocBlock: full");
1397 mvertex_t *r_pcurrentvertbase;
1398 model_t *currentmodel;
1404 BuildSurfaceDisplayList
1407 void BuildSurfaceDisplayList (msurface_t *fa)
1409 int i, lindex, lnumverts;
1410 medge_t *pedges, *r_pedge;
1416 // reconstruct the polygon
1417 pedges = currentmodel->edges;
1418 lnumverts = fa->numedges;
1424 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1425 poly->next = fa->polys;
1426 poly->flags = fa->flags;
1428 poly->numverts = lnumverts;
1430 for (i=0 ; i<lnumverts ; i++)
1432 lindex = currentmodel->surfedges[fa->firstedge + i];
1436 r_pedge = &pedges[lindex];
1437 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1441 r_pedge = &pedges[-lindex];
1442 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1444 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1445 s /= fa->texinfo->texture->width;
1447 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1448 t /= fa->texinfo->texture->height;
1450 VectorCopy (vec, poly->verts[i]);
1451 poly->verts[i][3] = s;
1452 poly->verts[i][4] = t;
1455 // lightmap texture coordinates
1457 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1458 s -= fa->texturemins[0];
1459 poly->verts[i][7] = bound(0l, ((int)s>>4), (fa->extents[0]>>4)); // LordHavoc: raw lightmap coordinates
1460 s += fa->light_s*16;
1462 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1464 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1465 t -= fa->texturemins[1];
1466 poly->verts[i][8] = bound(0l, ((int)t>>4), (fa->extents[1]>>4)); // LordHavoc: raw lightmap coordinates
1467 t += fa->light_t*16;
1469 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1471 poly->verts[i][5] = s;
1472 poly->verts[i][6] = t;
1476 // remove co-linear points - Ed
1479 if (!gl_keeptjunctions.value)
1481 for (i = 0 ; i < lnumverts ; ++i)
1484 float *prev, *this, *next;
1486 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1487 this = poly->verts[i];
1488 next = poly->verts[(i + 1) % lnumverts];
1490 VectorSubtract( this, prev, v1 );
1491 VectorNormalize( v1 );
1492 VectorSubtract( next, prev, v2 );
1493 VectorNormalize( v2 );
1495 // skip co-linear points
1496 #define COLINEAR_EPSILON 0.001
1497 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1498 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1499 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1502 for (j = i + 1; j < lnumverts; ++j)
1505 for (k = 0; k < VERTEXSIZE; ++k)
1506 poly->verts[j - 1][k] = poly->verts[j][k];
1510 // retry next vertex next time, which is now current vertex
1516 poly->numverts = lnumverts;
1521 ========================
1522 GL_CreateSurfaceLightmap
1523 ========================
1525 void GL_CreateSurfaceLightmap (msurface_t *surf)
1529 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1532 smax = (surf->extents[0]>>4)+1;
1533 tmax = (surf->extents[1]>>4)+1;
1535 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1536 if (nosubimage || nosubimagefragments)
1538 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1539 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1542 R_BuildLightMap (surf, templight, smax * 4);
1543 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1547 R_BuildLightMap (surf, templight, smax * 3);
1548 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1557 Builds the lightmap texture
1558 with all the surfaces from all brush models
1561 void GL_BuildLightmaps (void)
1566 memset (allocated, 0, sizeof(allocated));
1568 r_framecount = 1; // no dlightcache
1570 if (gl_nosubimagefragments.value)
1571 nosubimagefragments = 1;
1573 nosubimagefragments = 0;
1575 if (gl_nosubimage.value)
1580 if (gl_lightmaprgba.value)
1582 lightmaprgba = true;
1587 lightmaprgba = false;
1591 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1592 // it needs to be aligned on 4 pixel boundaries...
1593 // so I implemented an adjustable lightmap alignment
1594 if (gl_lightmapalign.value < 1)
1595 gl_lightmapalign.value = 1;
1596 if (gl_lightmapalign.value > 16)
1597 gl_lightmapalign.value = 16;
1599 while (lightmapalign < gl_lightmapalign.value)
1600 lightmapalign <<= 1;
1601 gl_lightmapalign.value = lightmapalign;
1602 lightmapalignmask = ~(lightmapalign - 1);
1603 if (nosubimagefragments || nosubimage)
1606 lightmapalignmask = ~0;
1609 if (!lightmap_textures)
1611 lightmap_textures = texture_extension_number;
1612 texture_extension_number += MAX_LIGHTMAPS;
1615 for (j=1 ; j<MAX_MODELS ; j++)
1617 m = cl.model_precache[j];
1620 if (m->name[0] == '*')
1622 r_pcurrentvertbase = m->vertexes;
1624 for (i=0 ; i<m->numsurfaces ; i++)
1626 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1628 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1630 GL_CreateSurfaceLightmap (m->surfaces + i);
1631 BuildSurfaceDisplayList (m->surfaces + i);
1635 if (nosubimage || nosubimagefragments)
1638 qglSelectTexture(gl_mtex_enum+1);
1639 for (i = 0;i < MAX_LIGHTMAPS;i++)
1641 if (!allocated[i][0])
1643 lightmapupdate[i][0] = BLOCK_HEIGHT;
1644 lightmapupdate[i][1] = 0;
1645 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1646 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1647 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1649 glTexImage2D(GL_TEXTURE_2D, 0, 4, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1651 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1654 qglSelectTexture(gl_mtex_enum+0);