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"};
49 cvar_t r_ambient = {"r_ambient", "0"};
51 qboolean lightmaprgba, nosubimagefragments, nosubimage;
54 qboolean skyisvisible;
55 extern qboolean gl_arrays;
60 for (i = 0;i < MAX_LIGHTMAPS;i++)
62 Cvar_RegisterVariable(&gl_lightmapalign);
63 Cvar_RegisterVariable(&gl_lightmaprgba);
64 Cvar_RegisterVariable(&gl_nosubimagefragments);
65 Cvar_RegisterVariable(&gl_nosubimage);
66 Cvar_RegisterVariable(&r_ambient);
67 // check if it's the glquake minigl driver
68 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
71 // Cvar_SetValue("gl_nosubimagefragments", 1);
72 // Cvar_SetValue("gl_nosubimage", 1);
73 Cvar_SetValue("gl_lightmode", 0);
77 int dlightdivtable[8192];
78 int dlightdivtableinitialized = 0;
85 void R_AddDynamicLights (msurface_t *surf)
87 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, j;
91 // use 64bit integer... shame it's not very standardized...
92 #if _MSC_VER || __BORLANDC__
98 if (!dlightdivtableinitialized)
100 dlightdivtable[0] = 1048576 >> 7;
101 for (s = 1;s < 8192;s++)
102 dlightdivtable[s] = 1048576 / (s << 7);
103 dlightdivtableinitialized = 1;
106 smax = (surf->extents[0]>>4)+1;
107 tmax = (surf->extents[1]>>4)+1;
109 for (lnum=0 ; lnum<MAX_DLIGHTS ; lnum++)
111 if ( !(surf->dlightbits[lnum >> 5] & (1<<(lnum&31)) ) )
112 continue; // not lit by this light
114 VectorSubtract(cl_dlights[lnum].origin, currententity->origin, local);
115 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
116 for (i=0 ; i<3 ; i++)
117 impact[i] = cl_dlights[lnum].origin[i] - surf->plane->normal[i]*dist;
119 f = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
122 // reduce calculations
124 for (s = 0;s < smax;s++, i -= 16)
125 sdtable[s] = i*i + t;
127 f = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
130 maxdist = (int) (cl_dlights[lnum].radius*cl_dlights[lnum].radius); // for comparisons to minimum acceptable light
131 // clamp radius to avoid exceeding 8192 entry division table
132 if (maxdist > 1048576)
134 maxdist3 = maxdist - (int) (dist*dist);
135 // convert to 8.8 blocklights format
136 if (!cl_dlights[lnum].dark)
138 f = cl_dlights[lnum].color[0] * maxdist;red = f;
139 f = cl_dlights[lnum].color[1] * maxdist;green = f;
140 f = cl_dlights[lnum].color[2] * maxdist;blue = f;
142 else // negate for darklight
144 f = cl_dlights[lnum].color[0] * -maxdist;red = f;
145 f = cl_dlights[lnum].color[1] * -maxdist;green = f;
146 f = cl_dlights[lnum].color[2] * -maxdist;blue = f;
149 for (t = 0;t < tmax;t++,i -= 16)
152 if (td < maxdist3) // make sure some part of it is visible on this line
154 maxdist2 = maxdist - td;
155 for (s = 0;s < smax;s++)
157 if (sdtable[s] < maxdist2)
159 j = dlightdivtable[(sdtable[s]+td) >> 7];
160 k = (red * j) >> 8;bl[0] += k;
161 k = (green * j) >> 8;bl[1] += k;
162 k = (blue * j) >> 8;bl[2] += k;
168 bl+=smax*3; // skip line
173 extern qboolean lighthalf;
178 Combine and scale multiple lightmaps into the 8.8 format in blocklights
181 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
191 surf->cached_dlight = (surf->dlightframe == r_framecount);
192 surf->cached_lighthalf = lighthalf;
193 surf->cached_ambient = r_ambient.value;
195 smax = (surf->extents[0]>>4)+1;
196 tmax = (surf->extents[1]>>4)+1;
198 lightmap = surf->samples;
200 // set to full bright if no light data
201 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
204 for (i=0 ; i<size ; i++)
215 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
216 for (i=0 ; i<size ; i++)
223 // add all the lightmaps
225 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
227 scale = d_lightstylevalue[surf->styles[maps]];
228 surf->cached_light[maps] = scale; // 8.8 fraction
230 for (i=0 ; i<size ; i++)
232 *bl++ += *lightmap++ * scale;
233 *bl++ += *lightmap++ * scale;
234 *bl++ += *lightmap++ * scale;
238 // add all the dynamic lights
239 if (surf->dlightframe == r_framecount)
240 R_AddDynamicLights (surf);
242 stride -= (smax*lightmapbytes);
246 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
247 // the image is brightened as a processing pass
250 for (i=0 ; i<tmax ; i++, dest += stride)
252 for (j=0 ; j<smax ; j++)
254 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
255 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
256 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
263 for (i=0 ; i<tmax ; i++, dest += stride)
265 for (j=0 ; j<smax ; j++)
267 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
268 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
269 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
278 for (i=0 ; i<tmax ; i++, dest += stride)
280 for (j=0 ; j<smax ; j++)
282 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
283 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
284 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
291 for (i=0 ; i<tmax ; i++, dest += stride)
293 for (j=0 ; j<smax ; j++)
295 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
296 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
297 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
304 byte templight[32*32*4];
306 void R_UpdateLightmap(msurface_t *s, int lnum)
309 // upload the new lightmap texture fragment
310 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
311 if (nosubimage || nosubimagefragments)
313 if (lightmapupdate[lnum][0] > s->light_t)
314 lightmapupdate[lnum][0] = s->light_t;
315 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
316 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
318 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
320 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
324 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
325 tmax = (s->extents[1]>>4)+1;
328 R_BuildLightMap (s, templight, smax * 4);
329 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
333 R_BuildLightMap (s, templight, smax * 3);
334 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
344 Returns the proper texture for a given time and base texture
347 texture_t *R_TextureAnimation (texture_t *base)
353 if (currententity->frame)
355 if (base->alternate_anims)
356 base = base->alternate_anims;
359 if (!base->anim_total)
364 relative = (int)(cl.time*10) % base->anim_total;
367 while (base->anim_min > relative || base->anim_max <= relative)
369 base = base->anim_next;
372 Con_Printf("R_TextureAnimation: broken cycle");
377 Con_Printf("R_TextureAnimation: infinite cycle");
387 =============================================================
391 =============================================================
395 extern int solidskytexture;
396 extern int alphaskytexture;
397 extern float speedscale; // for top sky and bottom sky
399 qboolean mtexenabled = false;
401 extern char skyname[];
403 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
404 //extern cvar_t r_dynamicwater;
405 extern int r_dlightframecount;
408 #include "gl_warp_sin.h"
410 #define TURBSCALE (256.0 / (2 * M_PI))
413 void UploadLightmaps()
416 if (nosubimage || nosubimagefragments)
418 for (i = 0;i < MAX_LIGHTMAPS;i++)
420 if (lightmapupdate[i][0] < lightmapupdate[i][1])
422 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
426 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
428 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
433 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]));
435 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]));
438 lightmapupdate[i][0] = BLOCK_HEIGHT;
439 lightmapupdate[i][1] = 0;
449 extern qboolean hlbsp;
451 extern char skyname[];
452 void DrawTextureChains (void)
459 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
463 for (j = 0;j < cl.worldmodel->numtextures;j++)
465 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
467 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
468 // subdivided water surface warp
469 if (s->flags & SURF_DRAWSKY)
471 cl.worldmodel->textures[j]->texturechain = NULL;
472 t = R_TextureAnimation (cl.worldmodel->textures[j]);
474 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
476 for (;s;s = s->texturechain)
478 for (p=s->polys ; p ; p=p->next)
480 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
482 skypoly[currentskypoly].firstvert = currentskyvert;
483 skypoly[currentskypoly++].verts = p->numverts;
484 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
486 skyvert[currentskyvert].v[0] = v[0];
487 skyvert[currentskyvert].v[1] = v[1];
488 skyvert[currentskyvert++].v[2] = v[2];
496 skypolyrender(); // fogged sky polys, affects depth
498 if (skyname[0] && skyisvisible && !fogenabled)
499 R_Sky(); // does not affect depth, draws over the sky polys
503 for (j = 0;j < cl.worldmodel->numtextures;j++)
505 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
507 if (!(s->flags & SURF_DRAWTURB))
509 cl.worldmodel->textures[j]->texturechain = NULL;
510 t = R_TextureAnimation (cl.worldmodel->textures[j]);
511 for (;s;s = s->texturechain)
513 if (currentwallpoly < MAX_WALLPOLYS && currentwallvert < MAX_WALLVERTS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
515 // check for lightmap modification
518 if (s->dlightframe == r_framecount || s->cached_dlight || r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed, or r_ambient changed
519 R_UpdateLightmap(s, s->lightmaptexturenum);
521 for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
522 if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
524 R_UpdateLightmap(s, s->lightmaptexturenum);
528 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
529 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
530 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
531 wallpoly[currentwallpoly].firstvert = currentwallvert;
532 wallpoly[currentwallpoly++].verts = s->polys->numverts;
533 for (i = 0,v = s->polys->verts[0];i<s->polys->numverts;i++, v += VERTEXSIZE)
535 wallvert[currentwallvert].vert[0] = v[0];
536 wallvert[currentwallvert].vert[1] = v[1];
537 wallvert[currentwallvert].vert[2] = v[2];
538 wallvert[currentwallvert].s = v[3];
539 wallvert[currentwallvert].t = v[4];
540 wallvert[currentwallvert].u = v[5];
541 wallvert[currentwallvert++].v = v[6];
550 // then water (water gets diverted to transpoly list)
551 for (j = 0;j < cl.worldmodel->numtextures;j++)
553 if (!cl.worldmodel->textures[j] || !(s = cl.worldmodel->textures[j]->texturechain))
555 cl.worldmodel->textures[j]->texturechain = NULL;
556 t = R_TextureAnimation (cl.worldmodel->textures[j]);
557 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
558 // subdivided water surface warp
559 if (s->flags & SURF_DRAWTURB)
561 int light, alpha, r = 0, g = 0, b = 0;
562 vec3_t nv, shadecolor;
563 alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
565 if (s->flags & SURF_DRAWFULLBRIGHT)
566 r = g = b = lighthalf ? 128 : 255;
567 else if (s->dlightframe == r_dlightframecount && r_dynamic.value)
570 r = g = b = (lighthalf ? 64 : 128) + (int) (r_ambient.value * 2.0f);
571 if (r_waterripple.value)
577 for (;s;s = s->texturechain)
579 for (p=s->polys ; p ; p=p->next)
581 // FIXME: could be a transparent water texture
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)
587 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);
588 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
589 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
590 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);
598 for (;s;s = s->texturechain)
600 for (p=s->polys ; p ; p=p->next)
602 // FIXME: could be a transparent water texture
603 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
604 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
608 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);
609 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);
620 for (;s;s = s->texturechain)
622 for (p=s->polys ; p ; p=p->next)
624 // FIXME: could be a transparent water texture
625 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
626 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
630 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);
631 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
632 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
633 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);
641 for (;s;s = s->texturechain)
643 for (p=s->polys ; p ; p=p->next)
645 // FIXME: could be a transparent water texture
646 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
647 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
651 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);
652 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);
666 for (;s;s = s->texturechain)
668 for (p=s->polys ; p ; p=p->next)
670 // FIXME: could be a transparent water texture
671 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
672 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
674 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
675 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
676 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);
684 for (;s;s = s->texturechain)
686 for (p=s->polys ; p ; p=p->next)
688 // FIXME: could be a transparent water texture
689 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
690 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
692 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);
703 for (;s;s = s->texturechain)
705 for (p=s->polys ; p ; p=p->next)
707 // FIXME: could be a transparent water texture
708 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
709 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
711 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
712 R_DynamicLightPoint(shadecolor, v, s->dlightbits);
713 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);
721 for (;s;s = s->texturechain)
723 for (p=s->polys ; p ; p=p->next)
725 // FIXME: could be a transparent water texture
726 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
727 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
729 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);
741 // LordHavoc: transparent brush models
742 extern int r_dlightframecount;
743 extern float modelalpha;
744 extern vec3_t shadecolor;
745 //qboolean R_CullBox (vec3_t mins, vec3_t maxs);
746 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
747 void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
748 void EmitWaterPolys (msurface_t *fa);
749 void R_MarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, mnode_t *node);
756 void R_DrawBrushModel (entity_t *e)
758 int i, j, k, smax, tmax, size3, maps;
759 vec3_t mins, maxs, nv;
763 qboolean rotated, vertexlit = false;
764 float dot, *v, scale;
767 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
773 if (e->angles[0] || e->angles[1] || e->angles[2])
776 for (i=0 ; i<3 ; i++)
778 mins[i] = e->origin[i] - clmodel->radius;
779 maxs[i] = e->origin[i] + clmodel->radius;
785 VectorAdd (e->origin, clmodel->mins, mins);
786 VectorAdd (e->origin, clmodel->maxs, maxs);
789 if (R_CullBox (mins, maxs))
792 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
796 vec3_t forward, right, up;
798 VectorCopy (modelorg, temp);
799 AngleVectors (e->angles, forward, right, up);
800 modelorg[0] = DotProduct (temp, forward);
801 modelorg[1] = -DotProduct (temp, right);
802 modelorg[2] = DotProduct (temp, up);
805 s = &clmodel->surfaces[clmodel->firstmodelsurface];
807 // calculate dynamic lighting for bmodel if it's not an
809 if (modelalpha == 1 && clmodel->firstmodelsurface != 0 && !(currententity->effects & EF_FULLBRIGHT) && currententity->colormod[0] == 1 && currententity->colormod[2] == 1 && currententity->colormod[2] == 1)
811 // if (!gl_flashblend.value)
814 for (k=0 ; k<MAX_DLIGHTS ; k++)
816 if ((cl_dlights[k].die < cl.time) || (!cl_dlights[k].radius))
819 VectorSubtract(cl_dlights[k].origin, currententity->origin, org);
820 R_MarkLights (org, &cl_dlights[k], 1<<(k&31), k >> 5, clmodel->nodes + clmodel->hulls[0].firstclipnode);
827 e->angles[0] = -e->angles[0]; // stupid quake bug
828 softwaretransformforentity (e);
829 e->angles[0] = -e->angles[0]; // stupid quake bug
832 for (j = 0;j < clmodel->nummodelsurfaces;j++, s++)
834 // find which side of the node we are on
837 dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
840 if (((s->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
841 (!(s->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON)))
843 if (s->flags & SURF_DRAWSKY)
845 if (s->flags & SURF_DRAWTURB)
848 int light, alpha, r = 0, g = 0, b = 0;
851 if (s->flags & SURF_DRAWNOALPHA)
852 alpha = modelalpha*255.0f;
854 alpha = r_wateralpha.value*modelalpha*255.0f;
856 if (s->flags & SURF_DRAWFULLBRIGHT || currententity->effects & EF_FULLBRIGHT)
860 r = 128.0f * currententity->colormod[0];
861 g = 128.0f * currententity->colormod[1];
862 b = 128.0f * currententity->colormod[2];
866 r = 255.0f * currententity->colormod[0];
867 g = 255.0f * currententity->colormod[1];
868 b = 255.0f * currententity->colormod[2];
871 else if (s->dlightframe == r_dlightframecount && r_dynamic.value)
877 r = 64.0f * currententity->colormod[0] + (int) r_ambient.value;
878 g = 64.0f * currententity->colormod[1] + (int) r_ambient.value;
879 b = 64.0f * currententity->colormod[2] + (int) r_ambient.value;
883 r = 128.0f * currententity->colormod[0] + (int) (r_ambient.value * 2.0f);
884 g = 128.0f * currententity->colormod[1] + (int) (r_ambient.value * 2.0f);
885 b = 128.0f * currententity->colormod[2] + (int) (r_ambient.value * 2.0f);
888 for (p=s->polys ; p ; p=p->next)
890 // FIXME: could be a transparent water texture
891 transpolybegin(s->texinfo->texture->gl_texturenum, s->texinfo->texture->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
892 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
894 softwaretransform(v, nv);
895 if (r_waterripple.value)
896 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);
899 shadecolor[0] = shadecolor[1] = shadecolor[2] = 128 + (int) (r_ambient.value * 2.0f);
900 R_DynamicLightPoint(shadecolor, nv, s->dlightbits);
903 r = (int) ((float) (shadecolor[0] * currententity->colormod[0])) >> 1;
904 g = (int) ((float) (shadecolor[1] * currententity->colormod[1])) >> 1;
905 b = (int) ((float) (shadecolor[2] * currententity->colormod[2])) >> 1;
909 r = (int) ((float) (shadecolor[0] * currententity->colormod[0]));
910 g = (int) ((float) (shadecolor[1] * currententity->colormod[1]));
911 b = (int) ((float) (shadecolor[2] * currententity->colormod[2]));
914 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);
920 t = R_TextureAnimation (s->texinfo->texture);
921 v = s->polys->verts[0];
922 if (vertexlit || s->texinfo->texture->transparent)
924 // FIXME: could be a transparent water texture
925 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
926 if ((currententity->effects & EF_FULLBRIGHT) || !s->samples)
928 for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
930 softwaretransform(v, nv);
931 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], 255,255,255,modelalpha*255.0f);
936 smax = (s->extents[0]>>4)+1;
937 tmax = (s->extents[1]>>4)+1;
938 size3 = smax*tmax*3; // *3 for colored lighting
939 for (i = 0;i < s->polys->numverts;i++, v += VERTEXSIZE)
941 shadecolor[0] = shadecolor[1] = shadecolor[2] = r_ambient.value * 2.0f;
942 lm = (byte *)((long) s->samples + ((int) v[8] * smax + (int) v[7]) * 3); // LordHavoc: *3 for colored lighting
943 for (maps = 0;maps < MAXLIGHTMAPS && s->styles[maps] != 255;maps++)
945 scale = d_lightstylevalue[s->styles[maps]] * (1.0 / 128.0);
946 shadecolor[0] += lm[0] * scale;
947 shadecolor[1] += lm[1] * scale;
948 shadecolor[2] += lm[2] * scale;
949 lm += size3; // LordHavoc: *3 for colored lighting
951 softwaretransform(v, nv);
952 R_DynamicLightPointNoMask(shadecolor, nv); // LordHavoc: dynamic lighting
955 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);
959 transpolyvert(nv[0], nv[1], nv[2], v[3], v[4], shadecolor[0], shadecolor[1], shadecolor[2], modelalpha*255.0f);
967 // check for lightmap modification
970 if (s->dlightframe == r_framecount || s->cached_dlight || r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf) // dynamic this frame or previously, or lighthalf changed
971 R_UpdateLightmap(s, s->lightmaptexturenum);
973 for (maps = 0 ; maps < MAXLIGHTMAPS && s->styles[maps] != 255 ; maps++)
974 if (d_lightstylevalue[s->styles[maps]] != s->cached_light[maps])
976 R_UpdateLightmap(s, s->lightmaptexturenum);
980 if (currentwallpoly < MAX_WALLPOLYS && (currentwallvert + s->polys->numverts) <= MAX_WALLVERTS)
982 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
983 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
984 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
985 wallpoly[currentwallpoly].firstvert = currentwallvert;
986 wallpoly[currentwallpoly++].verts = s->polys->numverts;
987 for (i = 0;i<s->polys->numverts;i++, v += VERTEXSIZE)
989 softwaretransform(v, wallvert[currentwallvert].vert);
990 wallvert[currentwallvert].s = v[3];
991 wallvert[currentwallvert].t = v[4];
992 wallvert[currentwallvert].u = v[5];
993 wallvert[currentwallvert++].v = v[6];
1003 =============================================================
1007 =============================================================
1010 void R_StoreEfrags (efrag_t **ppefrag);
1014 R_RecursiveWorldNode
1017 //extern qboolean R_CullBox (vec3_t mins, vec3_t maxs);
1019 void R_RecursiveWorldNode (mnode_t *node)
1025 // if a leaf node, draw stuff
1026 if (node->contents < 0)
1029 pleaf = (mleaf_t *)node;
1031 if (c = pleaf->nummarksurfaces)
1034 mark = pleaf->firstmarksurface;
1037 (*mark)->visframe = r_framecount;
1042 // deal with model fragments in this leaf
1044 R_StoreEfrags (&pleaf->efrags);
1049 // node is just a decision point, so go down the apropriate sides
1051 // find which side of the node we are on
1052 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1054 // recurse down the children, front side first
1056 // LordHavoc: save a stack frame by avoiding a call
1057 // if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1058 // LordHavoc: inlined further to reduce conditions
1059 if (node->children[side]->contents != CONTENTS_SOLID
1060 && node->children[side]->visframe == r_visframecount
1061 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1062 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1063 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1064 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1065 R_RecursiveWorldNode (node->children[side]);
1070 if (c = node->numsurfaces)
1073 surf = cl.worldmodel->surfaces + node->firstsurface;
1075 // LordHavoc: caused a crash due to texsort (it could render twice...)
1077 //side = dot >= -BACKFACE_EPSILON;
1080 for (;c;c--, surf++)
1082 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1084 surf->texturechain = surf->texinfo->texture->texturechain;
1085 surf->texinfo->texture->texturechain = surf;
1091 for (;c;c--, surf++)
1093 if (surf->visframe == r_framecount && (!(surf->flags & SURF_PLANEBACK)))
1095 surf->texturechain = surf->texinfo->texture->texturechain;
1096 surf->texinfo->texture->texturechain = surf;
1102 // recurse down the back side
1103 // LordHavoc: save a stack frame by avoiding a call
1104 // if (node->children[side]->contents != CONTENTS_SOLID && node->children[side]->visframe == r_visframecount && !R_CullBox (node->children[side]->minmaxs, node->children[side]->minmaxs+3))
1105 // LordHavoc: inlined further to reduce conditions
1106 if (node->children[side]->contents != CONTENTS_SOLID
1107 && node->children[side]->visframe == r_visframecount
1108 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1109 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1110 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1111 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1113 node = node->children[side];
1116 // R_RecursiveWorldNode (node->children[side]);
1133 if (!(node = cl.worldmodel->nodes))
1138 // if a leaf node, draw stuff
1140 if (node->contents < 0)
1142 if (node->contents != CONTENTS_SOLID)
1145 pleaf = (mleaf_t *)node;
1147 if ((c = pleaf->nummarksurfaces))
1150 mark = pleaf->firstmarksurface;
1153 (*mark)->visframe = r_framecount;
1158 // deal with model fragments in this leaf
1160 R_StoreEfrags (&pleaf->efrags);
1165 node = nodestack[--s].node;
1166 dot = nodestack[s].dot;
1170 // node is just a decision point, so go down the apropriate sides
1172 // find which side of the node we are on
1173 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
1175 // recurse down the children, front side first
1177 if (node->children[side]->visframe == r_visframecount
1178 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1179 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1180 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1181 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1183 nodestack[s].node = node;
1184 nodestack[s++].dot = dot;
1185 node = node->children[side];
1193 if ((c = node->numsurfaces))
1196 surf = cl.worldmodel->surfaces + node->firstsurface;
1200 for (;c;c--, surf++)
1202 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1204 surf->texturechain = surf->texinfo->texture->texturechain;
1205 surf->texinfo->texture->texturechain = surf;
1211 for (;c;c--, surf++)
1213 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1215 surf->texturechain = surf->texinfo->texture->texturechain;
1216 surf->texinfo->texture->texturechain = surf;
1222 // recurse down the back side
1223 if (node->children[side]->visframe == r_visframecount
1224 && frustum[0].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[0]) != 2
1225 && frustum[1].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[1]) != 2
1226 && frustum[2].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[2]) != 2
1227 && frustum[3].BoxOnPlaneSideFunc(node->children[side]->minmaxs, node->children[side]->minmaxs+3, &frustum[3]) != 2)
1229 node = node->children[side];
1235 node = nodestack[--s].node;
1236 dot = nodestack[s].dot;
1247 void R_DrawWorld (void)
1251 memset (&ent, 0, sizeof(ent));
1252 ent.model = cl.worldmodel;
1253 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1254 modelalpha = ent.alpha = 1;
1257 VectorCopy (r_refdef.vieworg, modelorg);
1259 currententity = &ent;
1261 softwaretransformidentity(); // LordHavoc: clear transform
1262 skyisvisible = false;
1267 glClear (GL_DEPTH_BUFFER_BIT);
1269 DrawTextureChains ();
1271 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
1272 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1281 void R_MarkLeaves (void)
1287 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1291 r_oldviewleaf = r_viewleaf;
1295 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1297 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1300 if (node->visframe == r_visframecount)
1302 node->visframe = r_visframecount;
1303 node = node->parent;
1309 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1311 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1313 if (vis[i>>3] & (1<<(i&7)))
1315 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1318 if (node->visframe == r_visframecount)
1320 node->visframe = r_visframecount;
1321 node = node->parent;
1331 =============================================================================
1335 =============================================================================
1338 // returns a texture number and the position inside it
1339 int AllocBlock (int w, int h, int *x, int *y)
1345 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1347 best = BLOCK_HEIGHT;
1349 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1353 for (j=0 ; j<w ; j++)
1355 if (allocated[texnum][i+j] >= best)
1357 if (allocated[texnum][i+j] > best2)
1358 best2 = allocated[texnum][i+j];
1361 { // this is a valid spot
1367 if (best + h > BLOCK_HEIGHT)
1370 if (nosubimagefragments || nosubimage)
1372 if (!lightmaps[texnum])
1373 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1375 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1376 else if (!allocated[texnum][0])
1378 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1379 memset(blank, 0, sizeof(blank));
1380 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1381 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1382 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1384 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1386 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1389 for (i=0 ; i<w ; i++)
1390 allocated[texnum][*x + i] = best + h;
1395 Sys_Error ("AllocBlock: full");
1400 mvertex_t *r_pcurrentvertbase;
1401 model_t *currentmodel;
1407 BuildSurfaceDisplayList
1410 void BuildSurfaceDisplayList (msurface_t *fa)
1412 int i, lindex, lnumverts;
1413 medge_t *pedges, *r_pedge;
1419 // reconstruct the polygon
1420 pedges = currentmodel->edges;
1421 lnumverts = fa->numedges;
1427 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1428 poly->next = fa->polys;
1429 poly->flags = fa->flags;
1431 poly->numverts = lnumverts;
1433 for (i=0 ; i<lnumverts ; i++)
1435 lindex = currentmodel->surfedges[fa->firstedge + i];
1439 r_pedge = &pedges[lindex];
1440 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1444 r_pedge = &pedges[-lindex];
1445 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1447 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1448 s /= fa->texinfo->texture->width;
1450 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1451 t /= fa->texinfo->texture->height;
1453 VectorCopy (vec, poly->verts[i]);
1454 poly->verts[i][3] = s;
1455 poly->verts[i][4] = t;
1458 // lightmap texture coordinates
1460 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1461 s -= fa->texturemins[0];
1462 poly->verts[i][7] = bound(0l, ((int)s>>4), (fa->extents[0]>>4)); // LordHavoc: raw lightmap coordinates
1463 s += fa->light_s*16;
1465 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1467 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1468 t -= fa->texturemins[1];
1469 poly->verts[i][8] = bound(0l, ((int)t>>4), (fa->extents[1]>>4)); // LordHavoc: raw lightmap coordinates
1470 t += fa->light_t*16;
1472 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1474 poly->verts[i][5] = s;
1475 poly->verts[i][6] = t;
1479 // remove co-linear points - Ed
1482 if (!gl_keeptjunctions.value)
1484 for (i = 0 ; i < lnumverts ; ++i)
1487 float *prev, *this, *next;
1489 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1490 this = poly->verts[i];
1491 next = poly->verts[(i + 1) % lnumverts];
1493 VectorSubtract( this, prev, v1 );
1494 VectorNormalize( v1 );
1495 VectorSubtract( next, prev, v2 );
1496 VectorNormalize( v2 );
1498 // skip co-linear points
1499 #define COLINEAR_EPSILON 0.001
1500 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1501 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1502 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1505 for (j = i + 1; j < lnumverts; ++j)
1508 for (k = 0; k < VERTEXSIZE; ++k)
1509 poly->verts[j - 1][k] = poly->verts[j][k];
1513 // retry next vertex next time, which is now current vertex
1519 poly->numverts = lnumverts;
1524 ========================
1525 GL_CreateSurfaceLightmap
1526 ========================
1528 void GL_CreateSurfaceLightmap (msurface_t *surf)
1532 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1535 smax = (surf->extents[0]>>4)+1;
1536 tmax = (surf->extents[1]>>4)+1;
1538 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1539 if (nosubimage || nosubimagefragments)
1541 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1542 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1545 R_BuildLightMap (surf, templight, smax * 4);
1546 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1550 R_BuildLightMap (surf, templight, smax * 3);
1551 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1560 Builds the lightmap texture
1561 with all the surfaces from all brush models
1564 void GL_BuildLightmaps (void)
1569 memset (allocated, 0, sizeof(allocated));
1571 r_framecount = 1; // no dlightcache
1573 if (gl_nosubimagefragments.value)
1574 nosubimagefragments = 1;
1576 nosubimagefragments = 0;
1578 if (gl_nosubimage.value)
1583 if (gl_lightmaprgba.value)
1585 lightmaprgba = true;
1590 lightmaprgba = false;
1594 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1595 // it needs to be aligned on 4 pixel boundaries...
1596 // so I implemented an adjustable lightmap alignment
1597 if (gl_lightmapalign.value < 1)
1598 gl_lightmapalign.value = 1;
1599 if (gl_lightmapalign.value > 16)
1600 gl_lightmapalign.value = 16;
1602 while (lightmapalign < gl_lightmapalign.value)
1603 lightmapalign <<= 1;
1604 gl_lightmapalign.value = lightmapalign;
1605 lightmapalignmask = ~(lightmapalign - 1);
1606 if (nosubimagefragments || nosubimage)
1609 lightmapalignmask = ~0;
1612 if (!lightmap_textures)
1614 lightmap_textures = texture_extension_number;
1615 texture_extension_number += MAX_LIGHTMAPS;
1618 for (j=1 ; j<MAX_MODELS ; j++)
1620 m = cl.model_precache[j];
1623 if (m->name[0] == '*')
1625 r_pcurrentvertbase = m->vertexes;
1627 for (i=0 ; i<m->numsurfaces ; i++)
1629 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1631 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1633 GL_CreateSurfaceLightmap (m->surfaces + i);
1634 BuildSurfaceDisplayList (m->surfaces + i);
1638 if (nosubimage || nosubimagefragments)
1641 qglSelectTexture(gl_mtex_enum+1);
1642 for (i = 0;i < MAX_LIGHTMAPS;i++)
1644 if (!allocated[i][0])
1646 lightmapupdate[i][0] = BLOCK_HEIGHT;
1647 lightmapupdate[i][1] = 0;
1648 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1649 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1650 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1652 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1654 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1657 qglSelectTexture(gl_mtex_enum+0);