2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 extern int skytexturenum;
26 int lightmap_textures;
28 signed blocklights[18*18*3]; // LordHavoc: *3 for colored lighting
30 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
31 #define BLOCK_WIDTH 128
32 #define BLOCK_HEIGHT 128
33 // LordHavoc: increased lightmap limit from 64 to 1024
34 #define MAX_LIGHTMAPS 1024
35 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
39 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
41 byte *lightmaps[MAX_LIGHTMAPS];
42 short lightmapupdate[MAX_LIGHTMAPS][2];
44 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
45 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
46 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "0"};
47 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
48 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
49 cvar_t r_ambient = {"r_ambient", "0"};
50 cvar_t gl_vertex = {"gl_vertex", "0"};
51 cvar_t gl_texsort = {"gl_texsort", "1"};
52 //cvar_t gl_funnywalls = {"gl_funnywalls", "0"}; // LordHavoc: see BuildSurfaceDisplayList
54 qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
57 extern qboolean gl_arrays;
59 extern int r_dlightframecount;
65 void gl_surf_shutdown()
72 for (i = 0;i < MAX_LIGHTMAPS;i++)
74 Cvar_RegisterVariable(&gl_lightmapalign);
75 Cvar_RegisterVariable(&gl_lightmaprgba);
76 Cvar_RegisterVariable(&gl_nosubimagefragments);
77 Cvar_RegisterVariable(&gl_nosubimage);
78 Cvar_RegisterVariable(&r_ambient);
79 // Cvar_RegisterVariable(&gl_funnywalls);
80 Cvar_RegisterVariable(&gl_vertex);
81 Cvar_RegisterVariable(&gl_texsort);
82 // check if it's the glquake minigl driver
83 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
86 // Cvar_SetValue("gl_nosubimagefragments", 1);
87 // Cvar_SetValue("gl_nosubimage", 1);
88 Cvar_SetValue("gl_lightmode", 0);
91 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
94 extern qboolean lighthalf;
99 Combine and scale multiple lightmaps into the 8.8 format in blocklights
102 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
112 surf->cached_lighthalf = lighthalf;
113 surf->cached_ambient = r_ambient.value;
115 smax = (surf->extents[0]>>4)+1;
116 tmax = (surf->extents[1]>>4)+1;
118 lightmap = surf->samples;
120 // set to full bright if no light data
121 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
124 for (i=0 ; i<size ; i++)
135 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
136 for (i=0 ; i<size ; i++)
143 // add all the lightmaps
145 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
147 scale = d_lightstylevalue[surf->styles[maps]];
148 surf->cached_light[maps] = scale; // 8.8 fraction
150 for (i=0 ; i<size ; i++)
152 *bl++ += *lightmap++ * scale;
153 *bl++ += *lightmap++ * scale;
154 *bl++ += *lightmap++ * scale;
158 stride -= (smax*lightmapbytes);
162 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
163 // the image is brightened as a processing pass
166 for (i=0 ; i<tmax ; i++, dest += stride)
168 for (j=0 ; j<smax ; j++)
170 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
171 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
172 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
179 for (i=0 ; i<tmax ; i++, dest += stride)
181 for (j=0 ; j<smax ; j++)
183 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
184 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
185 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
194 for (i=0 ; i<tmax ; i++, dest += stride)
196 for (j=0 ; j<smax ; j++)
198 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
199 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
200 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
207 for (i=0 ; i<tmax ; i++, dest += stride)
209 for (j=0 ; j<smax ; j++)
211 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
212 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
213 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
220 byte templight[32*32*4];
222 void R_UpdateLightmap(msurface_t *s, int lnum)
225 // upload the new lightmap texture fragment
227 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
228 if (nosubimage || nosubimagefragments)
230 if (lightmapupdate[lnum][0] > s->light_t)
231 lightmapupdate[lnum][0] = s->light_t;
232 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
233 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
235 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
237 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
241 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
242 tmax = (s->extents[1]>>4)+1;
245 R_BuildLightMap (s, templight, smax * 4);
247 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
251 R_BuildLightMap (s, templight, smax * 3);
253 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
263 Returns the proper texture for a given time and base texture
266 texture_t *R_TextureAnimation (texture_t *base)
272 if (currententity->frame)
274 if (base->alternate_anims)
275 base = base->alternate_anims;
278 if (!base->anim_total)
283 relative = (int)(cl.time*10) % base->anim_total;
286 while (base->anim_min > relative || base->anim_max <= relative)
288 base = base->anim_next;
291 Con_Printf("R_TextureAnimation: broken cycle");
296 Con_Printf("R_TextureAnimation: infinite cycle");
306 =============================================================
310 =============================================================
314 extern int solidskytexture;
315 extern int alphaskytexture;
316 extern float speedscale; // for top sky and bottom sky
318 extern char skyname[];
320 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
323 #include "gl_warp_sin.h"
325 #define TURBSCALE (256.0 / (2 * M_PI))
328 void UploadLightmaps()
331 if (nosubimage || nosubimagefragments)
333 for (i = 0;i < MAX_LIGHTMAPS;i++)
335 if (lightmapupdate[i][0] < lightmapupdate[i][1])
339 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
343 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
345 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
350 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]));
352 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]));
356 lightmapupdate[i][0] = BLOCK_HEIGHT;
357 lightmapupdate[i][1] = 0;
362 float wvert[1024*6]; // used by the following functions
364 void RSurf_DrawSky(msurface_t *s, int transform)
369 for (p=s->polys ; p ; p=p->next)
371 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
373 skypoly[currentskypoly].firstvert = currentskyvert;
374 skypoly[currentskypoly++].verts = p->numverts;
377 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
379 softwaretransform(v, skyvert[currentskyvert].v);
385 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
387 skyvert[currentskyvert].v[0] = v[0];
388 skyvert[currentskyvert].v[1] = v[1];
389 skyvert[currentskyvert++].v[2] = v[2];
396 int RSurf_Light(int *dlightbits, glpoly_t *polys)
398 float cr, cg, cb, radius, radius2, f, *v, *wv;
399 int i, a, b, lit = false;
404 for (a = 0;a < 8;a++)
406 if ((c = dlightbits[a]))
408 for (b = 0, d = 1;c;b++, d <<= 1)
413 light = &cl_dlights[a * 32 + b];
414 lightorigin = light->origin;
415 cr = light->color[0];
416 cg = light->color[1];
417 cb = light->color[2];
418 radius = light->radius*light->radius*LIGHTSCALE;
419 radius2 = radius * (256.0f / LIGHTSCALE);
421 for (p = polys;p;p = p->next)
423 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
425 f = VectorDistance2(wv, lightorigin);
428 f = radius2 / (f + LIGHTOFFSET);
444 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
447 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
451 for (p = s->polys;p;p = p->next)
453 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
456 softwaretransform(v, wv);
459 if (r_waterripple.value)
460 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);
461 wv[3] = wv[4] = wv[5] = 128.0f;
465 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
466 RSurf_Light(s->dlightbits, s->polys);
468 // FIXME: make fog texture if water texture is transparent?
469 for (p=s->polys ; p ; p=p->next)
471 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
472 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
473 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);
478 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
480 int i, lit = false, polys = 0, verts = 0;
485 // check for lightmap modification
488 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
489 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
490 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
491 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
492 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
493 R_UpdateLightmap(s, s->lightmaptexturenum);
496 for (p = s->polys;p;p = p->next)
498 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
501 softwaretransform(v, wv);
504 wv[3] = wv[4] = wv[5] = 0.0f;
507 verts += p->numverts;
510 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
512 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
513 lit = RSurf_Light(s->dlightbits, s->polys);
515 wp = &wallpoly[currentwallpoly];
516 out = &wallvert[currentwallvert];
517 currentwallpoly += polys;
518 for (p = s->polys;p;p = p->next)
521 wp->texnum = (unsigned short) t->gl_texturenum;
522 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
523 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
524 wp->firstvert = currentwallvert;
525 wp->numverts = p->numverts;
528 currentwallvert += p->numverts;
529 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
535 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
536 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
537 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
542 out->r = (byte) (bound(0, (int) wv[3], 255));
543 out->g = (byte) (bound(0, (int) wv[4], 255));
544 out->b = (byte) (bound(0, (int) wv[5], 255));
548 out->vert[0] = wv[0];
549 out->vert[1] = wv[1];
550 out->vert[2] = wv[2];
559 // LordHavoc: transparent brush models
560 extern int r_dlightframecount;
561 extern float modelalpha;
563 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
566 float *v, *wv, scale;
569 alpha = (int) (modelalpha * 255.0f);
570 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
572 for (p = s->polys;p;p = p->next)
574 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
577 softwaretransform(v, wv);
580 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
581 if (s->styles[0] != 255)
583 lm = (byte *)((long) s->samples + (int) v[7]);
584 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;
585 if (s->styles[1] != 255)
587 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;
588 if (s->styles[2] != 255)
590 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;
591 if (s->styles[3] != 255)
593 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;
601 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
602 RSurf_Light(s->dlightbits, s->polys);
604 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
606 for (p = s->polys;p;p = p->next)
609 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
610 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
611 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);
617 for (p = s->polys;p;p = p->next)
620 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
621 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
622 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
633 extern qboolean hlbsp;
634 extern char skyname[];
635 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
638 if (s->flags & SURF_DRAWSKY)
641 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
642 RSurf_DrawSky(s, false);
645 t = R_TextureAnimation (s->texinfo->texture);
646 if (s->flags & SURF_DRAWTURB)
648 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
652 RSurf_DrawWallVertex(s, t, false, false);
654 RSurf_DrawWall(s, t, false);
657 void DrawTextureChains (void)
663 for (n = 0;n < cl.worldmodel->numtextures;n++)
665 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
667 cl.worldmodel->textures[n]->texturechain = NULL;
668 // for (;s;s = s->texturechain)
669 // R_DrawSurf(s, false, gl_vertex.value);
670 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
672 if (s->flags & SURF_DRAWSKY)
675 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
676 for (;s;s = s->texturechain)
677 RSurf_DrawSky(s, false);
680 t = R_TextureAnimation (cl.worldmodel->textures[n]);
681 // subdivided water surface warp
682 if (s->flags & SURF_DRAWTURB)
684 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
685 for (;s;s = s->texturechain)
686 RSurf_DrawWater(s, t, false, alpha);
690 for (;s;s = s->texturechain)
691 RSurf_DrawWallVertex(s, t, false, false);
693 for (;s;s = s->texturechain)
694 RSurf_DrawWall(s, t, false);
698 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
705 void R_DrawBrushModel (entity_t *e)
711 int rotated, vertexlit = false;
719 if (e->angles[0] || e->angles[1] || e->angles[2])
722 for (i=0 ; i<3 ; i++)
724 mins[i] = e->origin[i] - clmodel->radius;
725 maxs[i] = e->origin[i] + clmodel->radius;
731 VectorAdd (e->origin, clmodel->mins, mins);
732 VectorAdd (e->origin, clmodel->maxs, maxs);
735 if (R_CullBox (mins, maxs))
738 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
742 vec3_t forward, right, up;
744 VectorCopy (modelorg, temp);
745 AngleVectors (e->angles, forward, right, up);
746 modelorg[0] = DotProduct (temp, forward);
747 modelorg[1] = -DotProduct (temp, right);
748 modelorg[2] = DotProduct (temp, up);
751 s = &clmodel->surfaces[clmodel->firstmodelsurface];
753 // calculate dynamic lighting for bmodel if it's not an
755 for (i = 0;i < MAX_DLIGHTS;i++)
757 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
760 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
761 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
763 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
765 e->angles[0] = -e->angles[0]; // stupid quake bug
766 softwaretransformforentity (e);
767 e->angles[0] = -e->angles[0]; // stupid quake bug
770 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
772 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
774 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
775 if (s->flags & SURF_DRAWSKY)
777 RSurf_DrawSky(s, true);
780 t = R_TextureAnimation (s->texinfo->texture);
781 if (s->flags & SURF_DRAWTURB)
783 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
786 if (vertexlit || s->texinfo->texture->transparent)
787 RSurf_DrawWallVertex(s, t, true, true);
789 RSurf_DrawWall(s, t, true);
796 =============================================================
800 =============================================================
803 void R_StoreEfrags (efrag_t **ppefrag);
818 int side, texsort, vertex;
819 struct nodestack_s *nstack;
822 msurface_t *surf, *endsurf, **mark, **endmark;
824 texsort = gl_texsort.value;
825 vertex = gl_vertex.value;
827 if (!(node = cl.worldmodel->nodes))
832 // if a leaf node, draw stuff
833 if (node->contents < 0)
835 if (node->contents != CONTENTS_SOLID)
837 pleaf = (mleaf_t *)node;
840 if (pleaf->nummarksurfaces)
842 mark = pleaf->firstmarksurface;
843 endmark = mark + pleaf->nummarksurfaces;
846 (*mark)->visframe = r_framecount;
849 while (mark < endmark);
852 // deal with model fragments in this leaf
854 R_StoreEfrags (&pleaf->efrags);
857 if (nstack <= nodestack)
867 // node is just a decision point, so go down the apropriate sides
869 // find which side of the node we are on
870 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
872 // recurse down the children, front side first
873 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
876 nstack->side = !side; // go down back side when we come back up
878 node = node->children[side];
885 if (node->numsurfaces)
887 surf = cl.worldmodel->surfaces + node->firstsurface;
888 endsurf = surf + node->numsurfaces;
896 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
898 surf->texturechain = surf->texinfo->texture->texturechain;
899 surf->texinfo->texture->texturechain = surf;
903 while (surf < endsurf);
909 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
911 surf->texturechain = surf->texinfo->texture->texturechain;
912 surf->texinfo->texture->texturechain = surf;
916 while (surf < endsurf);
925 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
926 R_DrawSurf(surf, false, vertex);
929 while (surf < endsurf);
935 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
936 R_DrawSurf(surf, false, vertex);
939 while (surf < endsurf);
944 // recurse down the back side
945 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
947 node = node->children[side];
951 if (nstack <= nodestack)
966 void R_DrawWorld (void)
970 memset (&ent, 0, sizeof(ent));
971 ent.model = cl.worldmodel;
972 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
973 modelalpha = ent.alpha = 1;
976 VectorCopy (r_refdef.vieworg, modelorg);
978 currententity = &ent;
980 softwaretransformidentity(); // LordHavoc: clear transform
985 R_PushDlights (); // now mark the lit surfaces
987 DrawTextureChains ();
996 void R_MarkLeaves (void)
1002 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1006 r_oldviewleaf = r_viewleaf;
1010 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1012 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1015 if (node->visframe == r_visframecount)
1017 node->visframe = r_visframecount;
1018 node = node->parent;
1024 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1026 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1028 if (vis[i>>3] & (1<<(i&7)))
1030 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1033 if (node->visframe == r_visframecount)
1035 node->visframe = r_visframecount;
1036 node = node->parent;
1046 =============================================================================
1050 =============================================================================
1053 // returns a texture number and the position inside it
1054 int AllocBlock (int w, int h, short *x, short *y)
1060 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1062 best = BLOCK_HEIGHT;
1064 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1068 for (j=0 ; j<w ; j++)
1070 if (allocated[texnum][i+j] >= best)
1072 if (allocated[texnum][i+j] > best2)
1073 best2 = allocated[texnum][i+j];
1076 { // this is a valid spot
1082 if (best + h > BLOCK_HEIGHT)
1085 if (nosubimagefragments || nosubimage)
1087 if (!lightmaps[texnum])
1088 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1090 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1091 else if (!allocated[texnum][0])
1093 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1094 memset(blank, 0, sizeof(blank));
1097 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1098 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1099 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1101 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1103 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1107 for (i=0 ; i<w ; i++)
1108 allocated[texnum][*x + i] = best + h;
1113 Sys_Error ("AllocBlock: full");
1118 mvertex_t *r_pcurrentvertbase;
1119 model_t *currentmodel;
1125 BuildSurfaceDisplayList
1128 void BuildSurfaceDisplayList (msurface_t *fa)
1130 int i, j, lindex, lnumverts;
1131 medge_t *pedges, *r_pedge;
1137 // reconstruct the polygon
1138 pedges = currentmodel->edges;
1139 lnumverts = fa->numedges;
1145 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1146 poly->next = fa->polys;
1147 poly->flags = fa->flags;
1149 poly->numverts = lnumverts;
1151 for (i=0 ; i<lnumverts ; i++)
1153 lindex = currentmodel->surfedges[fa->firstedge + i];
1157 r_pedge = &pedges[lindex];
1158 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1162 r_pedge = &pedges[-lindex];
1163 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1165 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1166 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1168 VectorCopy (vec, poly->verts[i]);
1169 poly->verts[i][3] = s / fa->texinfo->texture->width;
1170 poly->verts[i][4] = t / fa->texinfo->texture->height;
1173 // lightmap texture coordinates
1175 s -= fa->texturemins[0];
1176 t -= fa->texturemins[1];
1179 // LordHavoc: calc lightmap data offset
1180 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;
1181 poly->verts[i][7] = j;
1182 s += fa->light_s*16;
1183 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1185 t += fa->light_t*16;
1186 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1188 poly->verts[i][5] = s;
1189 poly->verts[i][6] = t;
1193 // remove co-linear points - Ed
1196 if (!gl_keeptjunctions.value)
1198 for (i = 0 ; i < lnumverts ; ++i)
1201 float *prev, *this, *next;
1203 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1204 this = poly->verts[i];
1205 next = poly->verts[(i + 1) % lnumverts];
1207 VectorSubtract( this, prev, v1 );
1208 VectorNormalize( v1 );
1209 VectorSubtract( next, prev, v2 );
1210 VectorNormalize( v2 );
1212 // skip co-linear points
1213 #define COLINEAR_EPSILON 0.001
1214 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1215 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1216 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1219 for (j = i + 1; j < lnumverts; ++j)
1222 for (k = 0; k < VERTEXSIZE; ++k)
1223 poly->verts[j - 1][k] = poly->verts[j][k];
1227 // retry next vertex next time, which is now current vertex
1233 poly->numverts = lnumverts;
1237 ========================
1238 GL_CreateSurfaceLightmap
1239 ========================
1241 void GL_CreateSurfaceLightmap (msurface_t *surf)
1245 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1248 smax = (surf->extents[0]>>4)+1;
1249 tmax = (surf->extents[1]>>4)+1;
1251 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1252 if (nosubimage || nosubimagefragments)
1254 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1255 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1258 R_BuildLightMap (surf, templight, smax * 4);
1260 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1264 R_BuildLightMap (surf, templight, smax * 3);
1266 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1275 Builds the lightmap texture
1276 with all the surfaces from all brush models
1279 void GL_BuildLightmaps (void)
1284 memset (allocated, 0, sizeof(allocated));
1286 r_framecount = 1; // no dlightcache
1288 if (gl_nosubimagefragments.value)
1289 nosubimagefragments = 1;
1291 nosubimagefragments = 0;
1293 if (gl_nosubimage.value)
1298 if (gl_lightmaprgba.value)
1300 lightmaprgba = true;
1305 lightmaprgba = false;
1309 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1310 // it needs to be aligned on 4 pixel boundaries...
1311 // so I implemented an adjustable lightmap alignment
1312 if (gl_lightmapalign.value < 1)
1313 gl_lightmapalign.value = 1;
1314 if (gl_lightmapalign.value > 16)
1315 gl_lightmapalign.value = 16;
1317 while (lightmapalign < gl_lightmapalign.value)
1318 lightmapalign <<= 1;
1319 gl_lightmapalign.value = lightmapalign;
1320 lightmapalignmask = ~(lightmapalign - 1);
1321 if (nosubimagefragments || nosubimage)
1324 lightmapalignmask = ~0;
1327 if (!lightmap_textures)
1329 lightmap_textures = texture_extension_number;
1330 texture_extension_number += MAX_LIGHTMAPS;
1333 for (j=1 ; j<MAX_MODELS ; j++)
1335 m = cl.model_precache[j];
1338 if (m->name[0] == '*')
1340 r_pcurrentvertbase = m->vertexes;
1342 for (i=0 ; i<m->numsurfaces ; i++)
1344 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1346 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1348 GL_CreateSurfaceLightmap (m->surfaces + i);
1349 BuildSurfaceDisplayList (m->surfaces + i);
1353 if (nosubimage || nosubimagefragments)
1357 qglSelectTexture(gl_mtex_enum+1);
1358 for (i = 0;i < MAX_LIGHTMAPS;i++)
1360 if (!allocated[i][0])
1362 lightmapupdate[i][0] = BLOCK_HEIGHT;
1363 lightmapupdate[i][1] = 0;
1366 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1367 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1368 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1370 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1372 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1377 qglSelectTexture(gl_mtex_enum+0);