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 int lightmap_textures;
26 signed blocklights[18*18*3]; // LordHavoc: *3 for colored lighting
28 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
29 #define BLOCK_WIDTH 128
30 #define BLOCK_HEIGHT 128
31 // LordHavoc: increased lightmap limit from 64 to 1024
32 #define MAX_LIGHTMAPS 1024
33 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
37 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
39 byte *lightmaps[MAX_LIGHTMAPS];
40 short lightmapupdate[MAX_LIGHTMAPS][2];
42 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
43 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
44 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "0"};
45 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
46 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
47 cvar_t r_ambient = {"r_ambient", "0"};
48 cvar_t gl_vertex = {"gl_vertex", "0"};
49 cvar_t gl_texsort = {"gl_texsort", "1"};
50 //cvar_t gl_funnywalls = {"gl_funnywalls", "0"}; // LordHavoc: see BuildSurfaceDisplayList
52 qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
55 extern qboolean gl_arrays;
57 extern int r_dlightframecount;
63 void gl_surf_shutdown()
70 for (i = 0;i < MAX_LIGHTMAPS;i++)
72 Cvar_RegisterVariable(&gl_lightmapalign);
73 Cvar_RegisterVariable(&gl_lightmaprgba);
74 Cvar_RegisterVariable(&gl_nosubimagefragments);
75 Cvar_RegisterVariable(&gl_nosubimage);
76 Cvar_RegisterVariable(&r_ambient);
77 // Cvar_RegisterVariable(&gl_funnywalls);
78 Cvar_RegisterVariable(&gl_vertex);
79 Cvar_RegisterVariable(&gl_texsort);
80 // check if it's the glquake minigl driver
81 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
84 // Cvar_SetValue("gl_nosubimagefragments", 1);
85 // Cvar_SetValue("gl_nosubimage", 1);
86 Cvar_SetValue("gl_lightmode", 0);
89 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
96 Combine and scale multiple lightmaps into the 8.8 format in blocklights
99 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
109 surf->cached_lighthalf = lighthalf;
110 surf->cached_ambient = r_ambient.value;
112 smax = (surf->extents[0]>>4)+1;
113 tmax = (surf->extents[1]>>4)+1;
115 lightmap = surf->samples;
117 // set to full bright if no light data
118 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
121 for (i=0 ; i<size ; i++)
132 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
133 for (i=0 ; i<size ; i++)
140 // add all the lightmaps
142 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
144 scale = d_lightstylevalue[surf->styles[maps]];
145 surf->cached_light[maps] = scale; // 8.8 fraction
147 for (i=0 ; i<size ; i++)
149 *bl++ += *lightmap++ * scale;
150 *bl++ += *lightmap++ * scale;
151 *bl++ += *lightmap++ * scale;
155 stride -= (smax*lightmapbytes);
159 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
160 // the image is brightened as a processing pass
163 for (i=0 ; i<tmax ; i++, dest += stride)
165 for (j=0 ; j<smax ; j++)
167 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
168 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
169 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
176 for (i=0 ; i<tmax ; i++, dest += stride)
178 for (j=0 ; j<smax ; j++)
180 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
181 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
182 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
191 for (i=0 ; i<tmax ; i++, dest += stride)
193 for (j=0 ; j<smax ; j++)
195 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
196 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
197 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
204 for (i=0 ; i<tmax ; i++, dest += stride)
206 for (j=0 ; j<smax ; j++)
208 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
209 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
210 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
217 byte templight[32*32*4];
219 void R_UpdateLightmap(msurface_t *s, int lnum)
222 // upload the new lightmap texture fragment
224 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
225 if (nosubimage || nosubimagefragments)
227 if (lightmapupdate[lnum][0] > s->light_t)
228 lightmapupdate[lnum][0] = s->light_t;
229 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
230 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
232 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
234 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
238 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
239 tmax = (s->extents[1]>>4)+1;
242 R_BuildLightMap (s, templight, smax * 4);
244 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
248 R_BuildLightMap (s, templight, smax * 3);
250 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
260 Returns the proper texture for a given time and base texture
263 texture_t *R_TextureAnimation (texture_t *base)
269 if (currententity->frame)
271 if (base->alternate_anims)
272 base = base->alternate_anims;
275 if (!base->anim_total)
280 relative = (int)(cl.time*10) % base->anim_total;
283 while (base->anim_min > relative || base->anim_max <= relative)
285 base = base->anim_next;
288 Con_Printf("R_TextureAnimation: broken cycle");
293 Con_Printf("R_TextureAnimation: infinite cycle");
303 =============================================================
307 =============================================================
311 extern int solidskytexture;
312 extern int alphaskytexture;
313 extern float speedscale; // for top sky and bottom sky
315 extern char skyname[];
317 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
320 #include "gl_warp_sin.h"
322 #define TURBSCALE (256.0 / (2 * M_PI))
325 void UploadLightmaps()
328 if (nosubimage || nosubimagefragments)
330 for (i = 0;i < MAX_LIGHTMAPS;i++)
332 if (lightmapupdate[i][0] < lightmapupdate[i][1])
336 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
340 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
342 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
347 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]));
349 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]));
353 lightmapupdate[i][0] = BLOCK_HEIGHT;
354 lightmapupdate[i][1] = 0;
359 float wvert[1024*6]; // used by the following functions
361 void RSurf_DrawSky(msurface_t *s, int transform)
366 for (p=s->polys ; p ; p=p->next)
368 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
370 skypoly[currentskypoly].firstvert = currentskyvert;
371 skypoly[currentskypoly++].verts = p->numverts;
374 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
376 softwaretransform(v, skyvert[currentskyvert].v);
382 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
384 skyvert[currentskyvert].v[0] = v[0];
385 skyvert[currentskyvert].v[1] = v[1];
386 skyvert[currentskyvert++].v[2] = v[2];
393 int RSurf_Light(int *dlightbits, glpoly_t *polys)
395 float cr, cg, cb, radius, radius2, f, *v, *wv;
396 int i, a, b, lit = false;
401 for (a = 0;a < 8;a++)
403 if ((c = dlightbits[a]))
405 for (b = 0, d = 1;c;b++, d <<= 1)
410 light = &cl_dlights[a * 32 + b];
411 lightorigin = light->origin;
412 cr = light->color[0];
413 cg = light->color[1];
414 cb = light->color[2];
415 radius = light->radius*light->radius*LIGHTSCALE;
416 radius2 = radius * (256.0f / LIGHTSCALE);
418 for (p = polys;p;p = p->next)
420 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
422 f = VectorDistance2(wv, lightorigin);
425 f = radius2 / (f + LIGHTOFFSET);
441 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
444 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
448 for (p = s->polys;p;p = p->next)
450 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
453 softwaretransform(v, wv);
456 if (r_waterripple.value)
457 wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
458 wv[3] = wv[4] = wv[5] = 128.0f;
462 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
463 RSurf_Light(s->dlightbits, s->polys);
465 // FIXME: make fog texture if water texture is transparent?
466 for (p=s->polys ; p ; p=p->next)
468 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
469 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
470 transpolyvert(wv[0], wv[1], wv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), wv[3], wv[4], wv[5], alpha);
475 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
477 int i, lit = false, polys = 0, verts = 0;
482 // check for lightmap modification
485 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
486 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
487 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
488 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
489 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
490 R_UpdateLightmap(s, s->lightmaptexturenum);
493 for (p = s->polys;p;p = p->next)
495 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
498 softwaretransform(v, wv);
501 wv[3] = wv[4] = wv[5] = 0.0f;
504 verts += p->numverts;
507 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
509 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
510 lit = RSurf_Light(s->dlightbits, s->polys);
512 wp = &wallpoly[currentwallpoly];
513 out = &wallvert[currentwallvert];
514 currentwallpoly += polys;
515 for (p = s->polys;p;p = p->next)
518 wp->texnum = (unsigned short) t->gl_texturenum;
519 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
520 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
521 wp->firstvert = currentwallvert;
522 wp->numverts = p->numverts;
525 currentwallvert += p->numverts;
526 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
532 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
533 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
534 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
539 out->r = (byte) (bound(0, (int) wv[3], 255));
540 out->g = (byte) (bound(0, (int) wv[4], 255));
541 out->b = (byte) (bound(0, (int) wv[5], 255));
545 out->vert[0] = wv[0];
546 out->vert[1] = wv[1];
547 out->vert[2] = wv[2];
556 // LordHavoc: transparent brush models
557 extern int r_dlightframecount;
558 extern float modelalpha;
560 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
563 float *v, *wv, scale;
566 alpha = (int) (modelalpha * 255.0f);
567 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
569 for (p = s->polys;p;p = p->next)
571 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
574 softwaretransform(v, wv);
577 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
578 if (s->styles[0] != 255)
580 lm = (byte *)((long) s->samples + (int) v[7]);
581 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 128.0f);wv[3] += lm[size3*0+0] * scale;wv[4] += lm[size3*0+1] * scale;wv[5] += lm[size3*0+2] * scale;
582 if (s->styles[1] != 255)
584 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 128.0f);wv[3] += lm[size3*1+0] * scale;wv[4] += lm[size3*1+1] * scale;wv[5] += lm[size3*1+2] * scale;
585 if (s->styles[2] != 255)
587 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 128.0f);wv[3] += lm[size3*2+0] * scale;wv[4] += lm[size3*2+1] * scale;wv[5] += lm[size3*2+2] * scale;
588 if (s->styles[3] != 255)
590 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 128.0f);wv[3] += lm[size3*3+0] * scale;wv[4] += lm[size3*3+1] * scale;wv[5] += lm[size3*3+2] * scale;
598 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
599 RSurf_Light(s->dlightbits, s->polys);
601 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
603 for (p = s->polys;p;p = p->next)
606 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
607 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
608 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->colormod[0], wv[4] * currententity->colormod[1], wv[5] * currententity->colormod[2], alpha);
614 for (p = s->polys;p;p = p->next)
617 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
618 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
619 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
630 extern qboolean hlbsp;
631 extern char skyname[];
632 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
635 if (s->flags & SURF_DRAWSKY)
638 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
639 RSurf_DrawSky(s, false);
642 t = R_TextureAnimation (s->texinfo->texture);
643 if (s->flags & SURF_DRAWTURB)
645 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
649 RSurf_DrawWallVertex(s, t, false, false);
651 RSurf_DrawWall(s, t, false);
654 void DrawTextureChains (void)
660 for (n = 0;n < cl.worldmodel->numtextures;n++)
662 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
664 cl.worldmodel->textures[n]->texturechain = NULL;
665 // for (;s;s = s->texturechain)
666 // R_DrawSurf(s, false, gl_vertex.value);
667 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
669 if (s->flags & SURF_DRAWSKY)
672 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
673 for (;s;s = s->texturechain)
674 RSurf_DrawSky(s, false);
677 t = R_TextureAnimation (cl.worldmodel->textures[n]);
678 // subdivided water surface warp
679 if (s->flags & SURF_DRAWTURB)
681 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
682 for (;s;s = s->texturechain)
683 RSurf_DrawWater(s, t, false, alpha);
687 for (;s;s = s->texturechain)
688 RSurf_DrawWallVertex(s, t, false, false);
690 for (;s;s = s->texturechain)
691 RSurf_DrawWall(s, t, false);
695 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
702 void R_DrawBrushModel (entity_t *e)
708 int rotated, vertexlit = false;
716 if (e->angles[0] || e->angles[1] || e->angles[2])
719 for (i=0 ; i<3 ; i++)
721 mins[i] = e->origin[i] - clmodel->radius;
722 maxs[i] = e->origin[i] + clmodel->radius;
728 VectorAdd (e->origin, clmodel->mins, mins);
729 VectorAdd (e->origin, clmodel->maxs, maxs);
732 if (R_CullBox (mins, maxs))
735 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
739 vec3_t forward, right, up;
741 VectorCopy (modelorg, temp);
742 AngleVectors (e->angles, forward, right, up);
743 modelorg[0] = DotProduct (temp, forward);
744 modelorg[1] = -DotProduct (temp, right);
745 modelorg[2] = DotProduct (temp, up);
748 s = &clmodel->surfaces[clmodel->firstmodelsurface];
750 // calculate dynamic lighting for bmodel if it's not an
752 for (i = 0;i < MAX_DLIGHTS;i++)
754 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
757 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
758 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
760 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
762 e->angles[0] = -e->angles[0]; // stupid quake bug
763 softwaretransformforentity (e);
764 e->angles[0] = -e->angles[0]; // stupid quake bug
767 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
769 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
771 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
772 if (s->flags & SURF_DRAWSKY)
774 RSurf_DrawSky(s, true);
777 t = R_TextureAnimation (s->texinfo->texture);
778 if (s->flags & SURF_DRAWTURB)
780 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
783 if (vertexlit || s->texinfo->texture->transparent)
784 RSurf_DrawWallVertex(s, t, true, true);
786 RSurf_DrawWall(s, t, true);
793 =============================================================
797 =============================================================
800 void R_StoreEfrags (efrag_t **ppefrag);
815 int side, texsort, vertex;
816 struct nodestack_s *nstack;
819 msurface_t *surf, *endsurf, **mark, **endmark;
821 texsort = gl_texsort.value;
822 vertex = gl_vertex.value;
824 if (!(node = cl.worldmodel->nodes))
829 // if a leaf node, draw stuff
830 if (node->contents < 0)
832 if (node->contents != CONTENTS_SOLID)
834 pleaf = (mleaf_t *)node;
837 if (pleaf->nummarksurfaces)
839 mark = pleaf->firstmarksurface;
840 endmark = mark + pleaf->nummarksurfaces;
843 (*mark)->visframe = r_framecount;
846 while (mark < endmark);
849 // deal with model fragments in this leaf
851 R_StoreEfrags (&pleaf->efrags);
854 if (nstack <= nodestack)
864 // node is just a decision point, so go down the apropriate sides
866 // find which side of the node we are on
867 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
869 // recurse down the children, front side first
870 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
873 nstack->side = !side; // go down back side when we come back up
875 node = node->children[side];
882 if (node->numsurfaces)
884 surf = cl.worldmodel->surfaces + node->firstsurface;
885 endsurf = surf + node->numsurfaces;
893 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
895 surf->texturechain = surf->texinfo->texture->texturechain;
896 surf->texinfo->texture->texturechain = surf;
900 while (surf < endsurf);
906 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
908 surf->texturechain = surf->texinfo->texture->texturechain;
909 surf->texinfo->texture->texturechain = surf;
913 while (surf < endsurf);
922 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
923 R_DrawSurf(surf, false, vertex);
926 while (surf < endsurf);
932 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
933 R_DrawSurf(surf, false, vertex);
936 while (surf < endsurf);
941 // recurse down the back side
942 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
944 node = node->children[side];
948 if (nstack <= nodestack)
963 void R_DrawWorld (void)
967 memset (&ent, 0, sizeof(ent));
968 ent.model = cl.worldmodel;
969 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
970 modelalpha = ent.alpha = 1;
973 VectorCopy (r_refdef.vieworg, modelorg);
975 currententity = &ent;
977 softwaretransformidentity(); // LordHavoc: clear transform
982 R_PushDlights (); // now mark the lit surfaces
984 DrawTextureChains ();
993 void R_MarkLeaves (void)
999 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1003 r_oldviewleaf = r_viewleaf;
1007 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1009 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1012 if (node->visframe == r_visframecount)
1014 node->visframe = r_visframecount;
1015 node = node->parent;
1021 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1023 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1025 if (vis[i>>3] & (1<<(i&7)))
1027 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1030 if (node->visframe == r_visframecount)
1032 node->visframe = r_visframecount;
1033 node = node->parent;
1043 =============================================================================
1047 =============================================================================
1050 // returns a texture number and the position inside it
1051 int AllocBlock (int w, int h, short *x, short *y)
1057 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1059 best = BLOCK_HEIGHT;
1061 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1065 for (j=0 ; j<w ; j++)
1067 if (allocated[texnum][i+j] >= best)
1069 if (allocated[texnum][i+j] > best2)
1070 best2 = allocated[texnum][i+j];
1073 { // this is a valid spot
1079 if (best + h > BLOCK_HEIGHT)
1082 if (nosubimagefragments || nosubimage)
1084 if (!lightmaps[texnum])
1085 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1087 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1088 else if (!allocated[texnum][0])
1090 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1091 memset(blank, 0, sizeof(blank));
1094 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1095 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1096 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1098 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1100 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1104 for (i=0 ; i<w ; i++)
1105 allocated[texnum][*x + i] = best + h;
1110 Sys_Error ("AllocBlock: full");
1115 mvertex_t *r_pcurrentvertbase;
1116 model_t *currentmodel;
1122 BuildSurfaceDisplayList
1125 void BuildSurfaceDisplayList (msurface_t *fa)
1127 int i, j, lindex, lnumverts;
1128 medge_t *pedges, *r_pedge;
1134 // reconstruct the polygon
1135 pedges = currentmodel->edges;
1136 lnumverts = fa->numedges;
1142 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1143 poly->next = fa->polys;
1144 poly->flags = fa->flags;
1146 poly->numverts = lnumverts;
1148 for (i=0 ; i<lnumverts ; i++)
1150 lindex = currentmodel->surfedges[fa->firstedge + i];
1154 r_pedge = &pedges[lindex];
1155 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1159 r_pedge = &pedges[-lindex];
1160 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1162 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1163 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1165 VectorCopy (vec, poly->verts[i]);
1166 poly->verts[i][3] = s / fa->texinfo->texture->width;
1167 poly->verts[i][4] = t / fa->texinfo->texture->height;
1170 // lightmap texture coordinates
1172 s -= fa->texturemins[0];
1173 t -= fa->texturemins[1];
1176 // LordHavoc: calc lightmap data offset
1177 j = (bound(0l, (int)t>>4, fa->extents[1]>>4) * ((fa->extents[0]>>4)+1) + bound(0l, (int)s>>4, fa->extents[0]>>4)) * 3;
1178 poly->verts[i][7] = j;
1179 s += fa->light_s*16;
1180 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1182 t += fa->light_t*16;
1183 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1185 poly->verts[i][5] = s;
1186 poly->verts[i][6] = t;
1190 // remove co-linear points - Ed
1193 if (!gl_keeptjunctions.value)
1195 for (i = 0 ; i < lnumverts ; ++i)
1198 float *prev, *this, *next;
1200 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1201 this = poly->verts[i];
1202 next = poly->verts[(i + 1) % lnumverts];
1204 VectorSubtract( this, prev, v1 );
1205 VectorNormalize( v1 );
1206 VectorSubtract( next, prev, v2 );
1207 VectorNormalize( v2 );
1209 // skip co-linear points
1210 #define COLINEAR_EPSILON 0.001
1211 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1212 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1213 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1216 for (j = i + 1; j < lnumverts; ++j)
1219 for (k = 0; k < VERTEXSIZE; ++k)
1220 poly->verts[j - 1][k] = poly->verts[j][k];
1224 // retry next vertex next time, which is now current vertex
1230 poly->numverts = lnumverts;
1234 ========================
1235 GL_CreateSurfaceLightmap
1236 ========================
1238 void GL_CreateSurfaceLightmap (msurface_t *surf)
1242 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1245 smax = (surf->extents[0]>>4)+1;
1246 tmax = (surf->extents[1]>>4)+1;
1248 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1249 if (nosubimage || nosubimagefragments)
1251 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1252 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1255 R_BuildLightMap (surf, templight, smax * 4);
1257 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1261 R_BuildLightMap (surf, templight, smax * 3);
1263 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1272 Builds the lightmap texture
1273 with all the surfaces from all brush models
1276 void GL_BuildLightmaps (void)
1281 memset (allocated, 0, sizeof(allocated));
1283 r_framecount = 1; // no dlightcache
1285 if (gl_nosubimagefragments.value)
1286 nosubimagefragments = 1;
1288 nosubimagefragments = 0;
1290 if (gl_nosubimage.value)
1295 if (gl_lightmaprgba.value)
1297 lightmaprgba = true;
1302 lightmaprgba = false;
1306 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1307 // it needs to be aligned on 4 pixel boundaries...
1308 // so I implemented an adjustable lightmap alignment
1309 if (gl_lightmapalign.value < 1)
1310 gl_lightmapalign.value = 1;
1311 if (gl_lightmapalign.value > 16)
1312 gl_lightmapalign.value = 16;
1314 while (lightmapalign < gl_lightmapalign.value)
1315 lightmapalign <<= 1;
1316 gl_lightmapalign.value = lightmapalign;
1317 lightmapalignmask = ~(lightmapalign - 1);
1318 if (nosubimagefragments || nosubimage)
1321 lightmapalignmask = ~0;
1324 if (!lightmap_textures)
1326 lightmap_textures = texture_extension_number;
1327 texture_extension_number += MAX_LIGHTMAPS;
1330 for (j=1 ; j<MAX_MODELS ; j++)
1332 m = cl.model_precache[j];
1335 if (m->name[0] == '*')
1337 r_pcurrentvertbase = m->vertexes;
1339 for (i=0 ; i<m->numsurfaces ; i++)
1341 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1343 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1345 GL_CreateSurfaceLightmap (m->surfaces + i);
1346 BuildSurfaceDisplayList (m->surfaces + i);
1350 if (nosubimage || nosubimagefragments)
1354 qglSelectTexture(gl_mtex_enum+1);
1355 for (i = 0;i < MAX_LIGHTMAPS;i++)
1357 if (!allocated[i][0])
1359 lightmapupdate[i][0] = BLOCK_HEIGHT;
1360 lightmapupdate[i][1] = 0;
1363 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1364 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1365 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1367 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1369 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1374 qglSelectTexture(gl_mtex_enum+0);