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
51 cvar_t r_newworldnode = {"r_newworldnode", "0"};
52 cvar_t r_oldclip = {"r_oldclip", "1"};
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 Cvar_RegisterVariable(&r_newworldnode);
83 Cvar_RegisterVariable(&r_oldclip);
84 // check if it's the glquake minigl driver
85 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
88 // Cvar_SetValue("gl_nosubimagefragments", 1);
89 // Cvar_SetValue("gl_nosubimage", 1);
90 Cvar_SetValue("gl_lightmode", 0);
93 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
100 Combine and scale multiple lightmaps into the 8.8 format in blocklights
103 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
146 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
148 scale = d_lightstylevalue[surf->styles[maps]];
149 surf->cached_light[maps] = scale; // 8.8 fraction
151 for (i=0 ; i<size ; i++)
153 *bl++ += *lightmap++ * scale;
154 *bl++ += *lightmap++ * scale;
155 *bl++ += *lightmap++ * scale;
160 stride -= (smax*lightmapbytes);
164 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
165 // the image is brightened as a processing pass
168 for (i = 0;i < tmax;i++, dest += stride)
170 for (j = 0;j < smax;j++, bl += 3, dest += 4)
172 dest[0] = min(bl[0] >> 8, 255);
173 dest[1] = min(bl[1] >> 8, 255);
174 dest[2] = min(bl[2] >> 8, 255);
181 for (i = 0;i < tmax;i++, dest += stride)
183 for (j = 0;j < smax;j++, bl += 3, dest += 3)
185 dest[0] = min(bl[0] >> 8, 255);
186 dest[1] = min(bl[1] >> 8, 255);
187 dest[2] = min(bl[2] >> 8, 255);
196 for (i = 0;i < tmax;i++, dest += stride)
198 for (j = 0;j < smax;j++, bl += 3, dest += 4)
200 dest[0] = min(bl[0] >> 7, 255);
201 dest[1] = min(bl[1] >> 7, 255);
202 dest[2] = min(bl[2] >> 7, 255);
209 for (i = 0;i < tmax;i++, dest += stride)
211 for (j = 0;j < smax;j++, bl += 3, dest += 3)
213 dest[0] = min(bl[0] >> 7, 255);
214 dest[1] = min(bl[1] >> 7, 255);
215 dest[2] = min(bl[2] >> 7, 255);
222 byte templight[32*32*4];
224 void R_UpdateLightmap(msurface_t *s, int lnum)
227 // upload the new lightmap texture fragment
229 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
230 if (nosubimage || nosubimagefragments)
232 if (lightmapupdate[lnum][0] > s->light_t)
233 lightmapupdate[lnum][0] = s->light_t;
234 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
235 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
237 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
239 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
243 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
244 tmax = (s->extents[1]>>4)+1;
247 R_BuildLightMap (s, templight, smax * 4);
249 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
253 R_BuildLightMap (s, templight, smax * 3);
255 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
265 Returns the proper texture for a given time and base texture
268 texture_t *R_TextureAnimation (texture_t *base)
274 if (currententity->frame)
276 if (base->alternate_anims)
277 base = base->alternate_anims;
280 if (!base->anim_total)
285 relative = (int)(cl.time*10) % base->anim_total;
288 while (base->anim_min > relative || base->anim_max <= relative)
290 base = base->anim_next;
293 Con_Printf("R_TextureAnimation: broken cycle");
298 Con_Printf("R_TextureAnimation: infinite cycle");
308 =============================================================
312 =============================================================
316 extern int solidskytexture;
317 extern int alphaskytexture;
318 extern float speedscale; // for top sky and bottom sky
320 extern char skyname[];
322 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
325 #include "gl_warp_sin.h"
327 #define TURBSCALE (256.0 / (2 * M_PI))
330 void UploadLightmaps()
333 if (nosubimage || nosubimagefragments)
335 for (i = 0;i < MAX_LIGHTMAPS;i++)
337 if (lightmapupdate[i][0] < lightmapupdate[i][1])
341 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
345 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
347 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
352 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]));
354 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]));
358 lightmapupdate[i][0] = BLOCK_HEIGHT;
359 lightmapupdate[i][1] = 0;
364 float wvert[1024*6]; // used by the following functions
366 void RSurf_DrawSky(msurface_t *s, int transform)
371 for (p=s->polys ; p ; p=p->next)
373 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
375 skypoly[currentskypoly].firstvert = currentskyvert;
376 skypoly[currentskypoly++].verts = p->numverts;
379 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
381 softwaretransform(v, skyvert[currentskyvert].v);
387 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
389 skyvert[currentskyvert].v[0] = v[0];
390 skyvert[currentskyvert].v[1] = v[1];
391 skyvert[currentskyvert++].v[2] = v[2];
398 int RSurf_Light(int *dlightbits, glpoly_t *polys)
400 float cr, cg, cb, radius, radius2, f, *v, *wv;
401 int i, a, b, lit = false;
406 for (a = 0;a < 8;a++)
408 if ((c = dlightbits[a]))
410 for (b = 0, d = 1;c;b++, d <<= 1)
415 light = &cl_dlights[a * 32 + b];
416 lightorigin = light->origin;
417 cr = light->color[0];
418 cg = light->color[1];
419 cb = light->color[2];
420 radius = light->radius*light->radius*LIGHTSCALE;
421 radius2 = radius * (256.0f / LIGHTSCALE2);
423 for (p = polys;p;p = p->next)
425 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
427 f = VectorDistance2(wv, lightorigin);
430 f = radius2 / (f + LIGHTOFFSET);
446 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
449 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
453 for (p = s->polys;p;p = p->next)
455 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
458 softwaretransform(v, wv);
461 if (r_waterripple.value)
462 wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
463 wv[3] = wv[4] = wv[5] = 128.0f;
467 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
468 RSurf_Light(s->dlightbits, s->polys);
470 // FIXME: make fog texture if water texture is transparent?
471 for (p=s->polys ; p ; p=p->next)
473 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
474 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
475 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);
480 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
482 int i, lit = false, polys = 0, verts = 0;
487 // check for lightmap modification
490 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
491 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
492 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
493 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
494 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
495 R_UpdateLightmap(s, s->lightmaptexturenum);
498 for (p = s->polys;p;p = p->next)
500 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
503 softwaretransform(v, wv);
506 wv[3] = wv[4] = wv[5] = 0.0f;
509 verts += p->numverts;
512 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
514 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
515 lit = RSurf_Light(s->dlightbits, s->polys);
517 wp = &wallpoly[currentwallpoly];
518 out = &wallvert[currentwallvert];
519 currentwallpoly += polys;
520 for (p = s->polys;p;p = p->next)
523 wp->texnum = (unsigned short) t->gl_texturenum;
524 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
525 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
526 wp->firstvert = currentwallvert;
527 wp->numverts = p->numverts;
530 currentwallvert += p->numverts;
531 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
537 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
538 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
539 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
544 out->r = (byte) (bound(0, (int) wv[3], 255));
545 out->g = (byte) (bound(0, (int) wv[4], 255));
546 out->b = (byte) (bound(0, (int) wv[5], 255));
550 out->vert[0] = wv[0];
551 out->vert[1] = wv[1];
552 out->vert[2] = wv[2];
561 // LordHavoc: transparent brush models
562 extern int r_dlightframecount;
563 extern float modelalpha;
565 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
568 float *v, *wv, scale;
571 alpha = (int) (modelalpha * 255.0f);
572 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
574 for (p = s->polys;p;p = p->next)
576 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
579 softwaretransform(v, wv);
582 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
583 if (s->styles[0] != 255)
585 lm = (byte *)((long) s->samples + (int) v[7]);
586 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;
587 if (s->styles[1] != 255)
589 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;
590 if (s->styles[2] != 255)
592 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;
593 if (s->styles[3] != 255)
595 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;
603 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
604 RSurf_Light(s->dlightbits, s->polys);
606 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
608 for (p = s->polys;p;p = p->next)
611 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
612 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
613 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);
619 for (p = s->polys;p;p = p->next)
622 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
623 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
624 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
635 extern qboolean hlbsp;
636 extern char skyname[];
637 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
640 if (s->flags & SURF_DRAWSKY)
643 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
644 RSurf_DrawSky(s, false);
647 t = R_TextureAnimation (s->texinfo->texture);
648 if (s->flags & SURF_DRAWTURB)
650 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
654 RSurf_DrawWallVertex(s, t, false, false);
656 RSurf_DrawWall(s, t, false);
659 void DrawTextureChains (void)
665 for (n = 0;n < cl.worldmodel->numtextures;n++)
667 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
669 cl.worldmodel->textures[n]->texturechain = NULL;
670 // for (;s;s = s->texturechain)
671 // R_DrawSurf(s, false, gl_vertex.value);
672 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
674 if (s->flags & SURF_DRAWSKY)
677 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
678 for (;s;s = s->texturechain)
679 RSurf_DrawSky(s, false);
682 t = R_TextureAnimation (cl.worldmodel->textures[n]);
683 // subdivided water surface warp
684 if (s->flags & SURF_DRAWTURB)
686 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
687 for (;s;s = s->texturechain)
688 RSurf_DrawWater(s, t, false, alpha);
692 for (;s;s = s->texturechain)
693 RSurf_DrawWallVertex(s, t, false, false);
695 for (;s;s = s->texturechain)
696 RSurf_DrawWall(s, t, false);
700 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
707 void R_DrawBrushModel (entity_t *e)
713 int rotated, vertexlit = false;
721 if (e->angles[0] || e->angles[1] || e->angles[2])
724 for (i=0 ; i<3 ; i++)
726 mins[i] = e->origin[i] - clmodel->radius;
727 maxs[i] = e->origin[i] + clmodel->radius;
733 VectorAdd (e->origin, clmodel->mins, mins);
734 VectorAdd (e->origin, clmodel->maxs, maxs);
737 if (R_CullBox (mins, maxs))
742 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
746 vec3_t forward, right, up;
748 VectorCopy (modelorg, temp);
749 AngleVectors (e->angles, forward, right, up);
750 modelorg[0] = DotProduct (temp, forward);
751 modelorg[1] = -DotProduct (temp, right);
752 modelorg[2] = DotProduct (temp, up);
755 s = &clmodel->surfaces[clmodel->firstmodelsurface];
757 // calculate dynamic lighting for bmodel if it's not an
759 for (i = 0;i < MAX_DLIGHTS;i++)
761 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
764 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
765 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
767 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
769 e->angles[0] = -e->angles[0]; // stupid quake bug
770 softwaretransformforentity (e);
771 e->angles[0] = -e->angles[0]; // stupid quake bug
774 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
776 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
778 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
779 if (s->flags & SURF_DRAWSKY)
781 RSurf_DrawSky(s, true);
784 t = R_TextureAnimation (s->texinfo->texture);
785 if (s->flags & SURF_DRAWTURB)
787 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
790 if (vertexlit || s->texinfo->texture->transparent)
791 RSurf_DrawWallVertex(s, t, true, true);
793 RSurf_DrawWall(s, t, true);
800 =============================================================
804 =============================================================
807 void R_StoreEfrags (efrag_t **ppefrag);
809 void R_NewWorldNode ()
811 int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
813 msurface_t *surf, **mark, **endmark;
815 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
817 if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
819 if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
824 // deal with model fragments in this leaf
826 R_StoreEfrags (&leaf->efrags);
828 if (leaf->nummarksurfaces)
830 mark = leaf->firstmarksurface;
831 endmark = mark + leaf->nummarksurfaces;
835 // make sure surfaces are only processed once
836 if (surf->worldnodeframe == r_framecount)
838 surf->worldnodeframe = r_framecount;
839 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
841 if ( (surf->flags & SURF_PLANEBACK))
843 surf->visframe = r_framecount;
847 surf->texturechain = surf->texinfo->texture->texturechain;
848 surf->texinfo->texture->texturechain = surf;
851 R_DrawSurf(surf, false, vertex);
856 if (!(surf->flags & SURF_PLANEBACK))
858 surf->visframe = r_framecount;
862 surf->texturechain = surf->texinfo->texture->texturechain;
863 surf->texinfo->texture->texturechain = surf;
866 R_DrawSurf(surf, false, vertex);
870 while (mark < endmark);
890 int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
891 struct nodestack_s *nstack;
894 msurface_t *surf, *endsurf, **mark, **endmark;
897 if (!(node = cl.worldmodel->nodes))
904 if (R_CullBox(node->minmaxs, node->minmaxs+3))
907 if (nstack <= nodestack)
912 noclipping = nstack->noclipping;
919 ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
920 cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
921 cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
922 cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
923 if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
924 noclipping = true; // not clipped at all, no need to clip any children of this node
925 // partially clipped node
927 // if a leaf node, draw stuff
928 if (node->contents < 0)
930 if (node->contents != CONTENTS_SOLID)
932 pleaf = (mleaf_t *)node;
935 if (pleaf->nummarksurfaces)
937 mark = pleaf->firstmarksurface;
938 endmark = mark + pleaf->nummarksurfaces;
941 (*mark)->visframe = r_framecount;
944 while (mark < endmark);
947 // deal with model fragments in this leaf
949 R_StoreEfrags (&pleaf->efrags);
952 if (nstack <= nodestack)
957 noclipping = nstack->noclipping;
963 // node is just a decision point, so go down the apropriate sides
965 // find which side of the node we are on
966 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
968 // recurse down the children, front side first
969 if (node->children[side]->visframe == r_visframecount)
972 nstack->side = !side; // go down back side when we come back up
973 nstack->noclipping = noclipping;
975 node = node->children[side];
982 if (node->numsurfaces)
984 surf = cl.worldmodel->surfaces + node->firstsurface;
985 endsurf = surf + node->numsurfaces;
993 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
996 surf->texturechain = surf->texinfo->texture->texturechain;
997 surf->texinfo->texture->texturechain = surf;
1000 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1003 while (surf < endsurf);
1009 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1012 surf->texturechain = surf->texinfo->texture->texturechain;
1013 surf->texinfo->texture->texturechain = surf;
1016 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1019 while (surf < endsurf);
1028 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1031 R_DrawSurf(surf, false, vertex);
1034 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1037 while (surf < endsurf);
1043 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1046 R_DrawSurf(surf, false, vertex);
1049 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1052 while (surf < endsurf);
1057 // recurse down the back side
1058 if (node->children[side]->visframe == r_visframecount)
1060 node = node->children[side];
1064 if (nstack <= nodestack)
1067 node = nstack->node;
1068 side = nstack->side;
1069 noclipping = nstack->noclipping;
1080 void R_DrawWorld (void)
1084 memset (&ent, 0, sizeof(ent));
1085 ent.model = cl.worldmodel;
1086 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1087 modelalpha = ent.alpha = 1;
1090 VectorCopy (r_refdef.vieworg, modelorg);
1092 currententity = &ent;
1094 softwaretransformidentity(); // LordHavoc: clear transform
1098 if (r_newworldnode.value)
1104 R_PushDlights (); // now mark the lit surfaces
1106 DrawTextureChains ();
1115 void R_MarkLeaves (void)
1121 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1125 r_oldviewleaf = r_viewleaf;
1129 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1131 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1134 if (node->visframe == r_visframecount)
1136 node->visframe = r_visframecount;
1137 node = node->parent;
1143 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1145 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1147 if (vis[i>>3] & (1<<(i&7)))
1149 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1152 if (node->visframe == r_visframecount)
1154 node->visframe = r_visframecount;
1155 node = node->parent;
1165 =============================================================================
1169 =============================================================================
1172 // returns a texture number and the position inside it
1173 int AllocBlock (int w, int h, short *x, short *y)
1179 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1181 best = BLOCK_HEIGHT;
1183 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1187 for (j=0 ; j<w ; j++)
1189 if (allocated[texnum][i+j] >= best)
1191 if (allocated[texnum][i+j] > best2)
1192 best2 = allocated[texnum][i+j];
1195 { // this is a valid spot
1201 if (best + h > BLOCK_HEIGHT)
1204 if (nosubimagefragments || nosubimage)
1206 if (!lightmaps[texnum])
1208 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1209 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1212 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1213 else if (!allocated[texnum][0])
1215 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1216 memset(blank, 0, sizeof(blank));
1219 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1220 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1221 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1223 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1225 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1229 for (i=0 ; i<w ; i++)
1230 allocated[texnum][*x + i] = best + h;
1235 Sys_Error ("AllocBlock: full");
1240 mvertex_t *r_pcurrentvertbase;
1241 model_t *currentmodel;
1247 BuildSurfaceDisplayList
1250 void BuildSurfaceDisplayList (msurface_t *fa)
1252 int i, j, lindex, lnumverts;
1253 medge_t *pedges, *r_pedge;
1259 // reconstruct the polygon
1260 pedges = currentmodel->edges;
1261 lnumverts = fa->numedges;
1267 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1268 poly->next = fa->polys;
1269 poly->flags = fa->flags;
1271 poly->numverts = lnumverts;
1273 for (i=0 ; i<lnumverts ; i++)
1275 lindex = currentmodel->surfedges[fa->firstedge + i];
1279 r_pedge = &pedges[lindex];
1280 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1284 r_pedge = &pedges[-lindex];
1285 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1287 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1288 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1290 VectorCopy (vec, poly->verts[i]);
1291 poly->verts[i][3] = s / fa->texinfo->texture->width;
1292 poly->verts[i][4] = t / fa->texinfo->texture->height;
1295 // lightmap texture coordinates
1297 s -= fa->texturemins[0];
1298 t -= fa->texturemins[1];
1301 // LordHavoc: calc lightmap data offset
1302 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;
1303 poly->verts[i][7] = j;
1304 s += fa->light_s*16;
1305 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1307 t += fa->light_t*16;
1308 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1310 poly->verts[i][5] = s;
1311 poly->verts[i][6] = t;
1315 // remove co-linear points - Ed
1318 if (!gl_keeptjunctions.value)
1320 for (i = 0 ; i < lnumverts ; ++i)
1323 float *prev, *this, *next;
1325 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1326 this = poly->verts[i];
1327 next = poly->verts[(i + 1) % lnumverts];
1329 VectorSubtract( this, prev, v1 );
1330 VectorNormalize( v1 );
1331 VectorSubtract( next, prev, v2 );
1332 VectorNormalize( v2 );
1334 // skip co-linear points
1335 #define COLINEAR_EPSILON 0.001
1336 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1337 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1338 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1341 for (j = i + 1; j < lnumverts; ++j)
1344 for (k = 0; k < VERTEXSIZE; ++k)
1345 poly->verts[j - 1][k] = poly->verts[j][k];
1349 // retry next vertex next time, which is now current vertex
1355 poly->numverts = lnumverts;
1359 ========================
1360 GL_CreateSurfaceLightmap
1361 ========================
1363 void GL_CreateSurfaceLightmap (msurface_t *surf)
1367 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1370 smax = (surf->extents[0]>>4)+1;
1371 tmax = (surf->extents[1]>>4)+1;
1373 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1374 if (nosubimage || nosubimagefragments)
1376 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1377 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1380 R_BuildLightMap (surf, templight, smax * 4);
1382 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1386 R_BuildLightMap (surf, templight, smax * 3);
1388 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1397 Builds the lightmap texture
1398 with all the surfaces from all brush models
1401 void GL_BuildLightmaps (void)
1406 memset (allocated, 0, sizeof(allocated));
1408 r_framecount = 1; // no dlightcache
1410 if (gl_nosubimagefragments.value)
1411 nosubimagefragments = 1;
1413 nosubimagefragments = 0;
1415 if (gl_nosubimage.value)
1420 if (gl_lightmaprgba.value)
1422 lightmaprgba = true;
1427 lightmaprgba = false;
1431 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1432 // it needs to be aligned on 4 pixel boundaries...
1433 // so I implemented an adjustable lightmap alignment
1434 if (gl_lightmapalign.value < 1)
1435 gl_lightmapalign.value = 1;
1436 if (gl_lightmapalign.value > 16)
1437 gl_lightmapalign.value = 16;
1439 while (lightmapalign < gl_lightmapalign.value)
1440 lightmapalign <<= 1;
1441 gl_lightmapalign.value = lightmapalign;
1442 lightmapalignmask = ~(lightmapalign - 1);
1443 if (nosubimagefragments || nosubimage)
1446 lightmapalignmask = ~0;
1449 if (!lightmap_textures)
1451 lightmap_textures = texture_extension_number;
1452 texture_extension_number += MAX_LIGHTMAPS;
1455 for (j=1 ; j<MAX_MODELS ; j++)
1457 m = cl.model_precache[j];
1460 if (m->name[0] == '*')
1462 r_pcurrentvertbase = m->vertexes;
1464 for (i=0 ; i<m->numsurfaces ; i++)
1466 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1468 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1470 GL_CreateSurfaceLightmap (m->surfaces + i);
1471 BuildSurfaceDisplayList (m->surfaces + i);
1475 if (nosubimage || nosubimagefragments)
1479 qglSelectTexture(gl_mtex_enum+1);
1480 for (i = 0;i < MAX_LIGHTMAPS;i++)
1482 if (!allocated[i][0])
1484 lightmapupdate[i][0] = BLOCK_HEIGHT;
1485 lightmapupdate[i][1] = 0;
1488 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1489 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1490 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1492 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1494 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1499 qglSelectTexture(gl_mtex_enum+0);