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 int 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", "1"};
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 r_newworldnode = {"r_newworldnode", "0"};
51 cvar_t r_oldclip = {"r_oldclip", "1"};
52 cvar_t r_dlightmap = {"r_dlightmap", "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_vertex);
80 Cvar_RegisterVariable(&gl_texsort);
81 Cvar_RegisterVariable(&r_newworldnode);
82 Cvar_RegisterVariable(&r_oldclip);
83 Cvar_RegisterVariable(&r_dlightmap);
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);
96 int dlightdivtable[32768];
101 int R_AddDynamicLights (msurface_t *surf)
103 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, j, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
106 vec3_t impact, local;
108 // LordHavoc: use 64bit integer... shame it's not very standardized...
109 //#if _MSC_VER || __BORLANDC__
115 // LordHavoc: later note: MSVC and hopefully all other C compilers use a 64bit result for 32bit*32bit multiply, so that was not necessary
120 if (!dlightdivtable[1])
122 dlightdivtable[0] = 4194304;
123 for (s = 1; s < 32768; s++)
124 dlightdivtable[s] = 4194304 / (s << 7);
127 smax = (surf->extents[0] >> 4) + 1;
128 tmax = (surf->extents[1] >> 4) + 1;
130 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
132 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
133 continue; // not lit by this light
135 VectorSubtract (cl_dlights[lnum].origin, currententity->origin, local);
136 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
138 // for comparisons to minimum acceptable light
139 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius) * LIGHTSCALE);
141 // clamp radius to avoid exceeding 32768 entry division table
142 if (maxdist > 4194304)
146 if (dist2 >= maxdist)
149 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
150 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
151 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
153 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
154 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
156 s = bound(0, impacts, smax * 16) - impacts;
157 t = bound(0, impactt, tmax * 16) - impactt;
158 i = s * s + t * t + dist2;
162 // reduce calculations
163 for (s = 0, i = impacts; s < smax; s++, i -= 16)
164 sdtable[s] = i * i + dist2;
166 maxdist3 = maxdist - (int) (dist * dist);
168 // convert to 8.8 blocklights format and scale up by radius
169 red = cl_dlights[lnum].color[0] * maxdist;
170 green = cl_dlights[lnum].color[1] * maxdist;
171 blue = cl_dlights[lnum].color[2] * maxdist;
175 for (t = 0; t < tmax; t++, i -= 16)
178 // make sure some part of it is visible on this line
181 maxdist2 = maxdist - td;
182 for (s = 0; s < smax; s++)
184 if (sdtable[s] < maxdist2)
186 k = dlightdivtable[(sdtable[s] + td) >> 7];
187 j = (red * k) >> 9;bl[0] += j;
188 j = (green * k) >> 9;bl[1] += j;
189 j = (blue * k) >> 9;bl[2] += j;
207 Combine and scale multiple lightmaps into the 8.8 format in blocklights
210 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
213 int i, j, size, size3;
219 surf->cached_dlight = 0;
220 surf->cached_lighthalf = lighthalf;
221 surf->cached_ambient = r_ambient.value;
223 smax = (surf->extents[0]>>4)+1;
224 tmax = (surf->extents[1]>>4)+1;
227 lightmap = surf->samples;
229 // set to full bright if no light data
230 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
233 for (i=0 ; i<size ; i++)
243 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
247 for (i = 0;i < size3;i++)
251 memset(&blocklights[0], 0, size*3*sizeof(int));
253 // add all the lightmaps
256 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
258 scale = d_lightstylevalue[surf->styles[maps]];
259 surf->cached_light[maps] = scale; // 8.8 fraction
261 for (i = 0;i < size3;i++)
262 *bl++ += *lightmap++ * scale;
265 if (r_dlightmap.value && surf->dlightframe == r_dlightframecount)
266 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
269 stride -= (smax*lightmapbytes);
273 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
274 // the image is brightened as a processing pass
277 for (i = 0;i < tmax;i++, dest += stride)
279 for (j = 0;j < smax;j++, bl += 3, dest += 4)
281 dest[0] = min(bl[0] >> 8, 255);
282 dest[1] = min(bl[1] >> 8, 255);
283 dest[2] = min(bl[2] >> 8, 255);
290 for (i = 0;i < tmax;i++, dest += stride)
292 for (j = 0;j < smax;j++, bl += 3, dest += 3)
294 dest[0] = min(bl[0] >> 8, 255);
295 dest[1] = min(bl[1] >> 8, 255);
296 dest[2] = min(bl[2] >> 8, 255);
305 for (i = 0;i < tmax;i++, dest += stride)
307 for (j = 0;j < smax;j++, bl += 3, dest += 4)
309 dest[0] = min(bl[0] >> 7, 255);
310 dest[1] = min(bl[1] >> 7, 255);
311 dest[2] = min(bl[2] >> 7, 255);
318 for (i = 0;i < tmax;i++, dest += stride)
320 for (j = 0;j < smax;j++, bl += 3, dest += 3)
322 dest[0] = min(bl[0] >> 7, 255);
323 dest[1] = min(bl[1] >> 7, 255);
324 dest[2] = min(bl[2] >> 7, 255);
331 byte templight[32*32*4];
333 void R_UpdateLightmap(msurface_t *s, int lnum)
336 // upload the new lightmap texture fragment
338 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
339 if (nosubimage || nosubimagefragments)
341 if (lightmapupdate[lnum][0] > s->light_t)
342 lightmapupdate[lnum][0] = s->light_t;
343 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
344 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
346 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
348 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
352 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
353 tmax = (s->extents[1]>>4)+1;
356 R_BuildLightMap (s, templight, smax * 4);
358 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
362 R_BuildLightMap (s, templight, smax * 3);
364 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
374 Returns the proper texture for a given time and base texture
377 texture_t *R_TextureAnimation (texture_t *base)
383 if (currententity->frame)
385 if (base->alternate_anims)
386 base = base->alternate_anims;
389 if (!base->anim_total)
394 relative = (int)(cl.time*10) % base->anim_total;
397 while (base->anim_min > relative || base->anim_max <= relative)
399 base = base->anim_next;
402 Con_Printf("R_TextureAnimation: broken cycle");
407 Con_Printf("R_TextureAnimation: infinite cycle");
417 =============================================================
421 =============================================================
425 extern int solidskytexture;
426 extern int alphaskytexture;
427 extern float speedscale; // for top sky and bottom sky
429 extern char skyname[];
431 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
434 #include "gl_warp_sin.h"
436 #define TURBSCALE (256.0 / (2 * M_PI))
439 void UploadLightmaps()
442 if (nosubimage || nosubimagefragments)
444 for (i = 0;i < MAX_LIGHTMAPS;i++)
446 if (lightmapupdate[i][0] < lightmapupdate[i][1])
450 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
454 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
456 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
461 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]));
463 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]));
467 lightmapupdate[i][0] = BLOCK_HEIGHT;
468 lightmapupdate[i][1] = 0;
473 float wvert[1024*6]; // used by the following functions
475 void RSurf_DrawSky(msurface_t *s, int transform)
480 for (p=s->polys ; p ; p=p->next)
482 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
484 skypoly[currentskypoly].firstvert = currentskyvert;
485 skypoly[currentskypoly++].verts = p->numverts;
488 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
490 softwaretransform(v, skyvert[currentskyvert].v);
496 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
498 skyvert[currentskyvert].v[0] = v[0];
499 skyvert[currentskyvert].v[1] = v[1];
500 skyvert[currentskyvert++].v[2] = v[2];
507 int RSurf_Light(int *dlightbits, glpoly_t *polys)
509 float cr, cg, cb, radius, radius2, f, *v, *wv;
510 int i, a, b, lit = false;
515 for (a = 0;a < 8;a++)
517 if ((c = dlightbits[a]))
519 for (b = 0, d = 1;c;b++, d <<= 1)
524 light = &cl_dlights[a * 32 + b];
525 lightorigin = light->origin;
526 cr = light->color[0];
527 cg = light->color[1];
528 cb = light->color[2];
529 radius = light->radius*light->radius*LIGHTSCALE;
530 radius2 = radius * (256.0f / LIGHTSCALE2);
532 for (p = polys;p;p = p->next)
534 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
536 f = VectorDistance2(wv, lightorigin);
539 f = radius2 / (f + LIGHTOFFSET);
555 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
558 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
562 for (p = s->polys;p;p = p->next)
564 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
567 softwaretransform(v, wv);
570 if (r_waterripple.value)
571 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);
572 wv[3] = wv[4] = wv[5] = 128.0f;
576 if (s->dlightframe == r_dlightframecount)
577 RSurf_Light(s->dlightbits, s->polys);
579 // FIXME: make fog texture if water texture is transparent?
580 for (p=s->polys ; p ; p=p->next)
582 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
583 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
584 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);
589 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
591 int i, lit = false, polys = 0, verts = 0;
596 // check for lightmap modification
600 || (r_dlightmap.value && s->dlightframe == r_dlightframecount)
601 || r_ambient.value != s->cached_ambient
602 || lighthalf != s->cached_lighthalf
603 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
604 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
605 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
606 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
607 R_UpdateLightmap(s, s->lightmaptexturenum);
610 for (p = s->polys;p;p = p->next)
612 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
615 softwaretransform(v, wv);
618 wv[3] = wv[4] = wv[5] = 0.0f;
621 verts += p->numverts;
624 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
626 if ((!r_dlightmap.value) && s->dlightframe == r_dlightframecount)
627 lit = RSurf_Light(s->dlightbits, s->polys);
629 wp = &wallpoly[currentwallpoly];
630 out = &wallvert[currentwallvert];
631 currentwallpoly += polys;
632 for (p = s->polys;p;p = p->next)
635 wp->texnum = (unsigned short) t->gl_texturenum;
636 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
637 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
638 wp->firstvert = currentwallvert;
639 wp->numverts = p->numverts;
642 currentwallvert += p->numverts;
643 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
649 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
650 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
651 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
656 out->r = (byte) (bound(0, (int) wv[3], 255));
657 out->g = (byte) (bound(0, (int) wv[4], 255));
658 out->b = (byte) (bound(0, (int) wv[5], 255));
662 out->vert[0] = wv[0];
663 out->vert[1] = wv[1];
664 out->vert[2] = wv[2];
673 // LordHavoc: transparent brush models
674 extern int r_dlightframecount;
675 extern float modelalpha;
677 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
680 float *v, *wv, scale;
683 alpha = (int) (modelalpha * 255.0f);
684 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
686 for (p = s->polys;p;p = p->next)
688 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
691 softwaretransform(v, wv);
694 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
695 if (s->styles[0] != 255)
697 lm = (byte *)((long) s->samples + (int) v[7]);
698 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;
699 if (s->styles[1] != 255)
701 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;
702 if (s->styles[2] != 255)
704 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;
705 if (s->styles[3] != 255)
707 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;
715 if (s->dlightframe == r_dlightframecount)
716 RSurf_Light(s->dlightbits, s->polys);
718 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
720 for (p = s->polys;p;p = p->next)
723 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
724 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
725 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);
731 for (p = s->polys;p;p = p->next)
734 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
735 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
736 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
747 extern qboolean hlbsp;
748 extern char skyname[];
749 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
752 if (s->flags & SURF_DRAWSKY)
755 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
756 RSurf_DrawSky(s, false);
759 t = R_TextureAnimation (s->texinfo->texture);
760 if (s->flags & SURF_DRAWTURB)
762 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
766 RSurf_DrawWallVertex(s, t, false, false);
768 RSurf_DrawWall(s, t, false);
771 void DrawTextureChains (void)
777 for (n = 0;n < cl.worldmodel->numtextures;n++)
779 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
781 cl.worldmodel->textures[n]->texturechain = NULL;
782 // for (;s;s = s->texturechain)
783 // R_DrawSurf(s, false, gl_vertex.value);
784 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
786 if (s->flags & SURF_DRAWSKY)
789 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
790 for (;s;s = s->texturechain)
791 RSurf_DrawSky(s, false);
794 t = R_TextureAnimation (cl.worldmodel->textures[n]);
795 // subdivided water surface warp
796 if (s->flags & SURF_DRAWTURB)
798 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
799 for (;s;s = s->texturechain)
800 RSurf_DrawWater(s, t, false, alpha);
804 for (;s;s = s->texturechain)
805 RSurf_DrawWallVertex(s, t, false, false);
807 for (;s;s = s->texturechain)
808 RSurf_DrawWall(s, t, false);
812 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
819 void R_DrawBrushModel (entity_t *e)
825 int rotated, vertexlit = false;
833 if (e->angles[0] || e->angles[1] || e->angles[2])
836 for (i=0 ; i<3 ; i++)
838 mins[i] = e->origin[i] - clmodel->radius;
839 maxs[i] = e->origin[i] + clmodel->radius;
845 VectorAdd (e->origin, clmodel->mins, mins);
846 VectorAdd (e->origin, clmodel->maxs, maxs);
849 if (R_CullBox (mins, maxs))
854 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
858 vec3_t forward, right, up;
860 VectorCopy (modelorg, temp);
861 AngleVectors (e->angles, forward, right, up);
862 modelorg[0] = DotProduct (temp, forward);
863 modelorg[1] = -DotProduct (temp, right);
864 modelorg[2] = DotProduct (temp, up);
867 s = &clmodel->surfaces[clmodel->firstmodelsurface];
869 // calculate dynamic lighting for bmodel if it's not an
871 for (i = 0;i < MAX_DLIGHTS;i++)
873 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
876 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
877 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
879 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
881 e->angles[0] = -e->angles[0]; // stupid quake bug
882 softwaretransformforentity (e);
883 e->angles[0] = -e->angles[0]; // stupid quake bug
886 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
888 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
890 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
891 if (s->flags & SURF_DRAWSKY)
893 RSurf_DrawSky(s, true);
896 t = R_TextureAnimation (s->texinfo->texture);
897 if (s->flags & SURF_DRAWTURB)
899 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
902 if (vertexlit || s->texinfo->texture->transparent)
903 RSurf_DrawWallVertex(s, t, true, true);
905 RSurf_DrawWall(s, t, true);
912 =============================================================
916 =============================================================
919 void R_StoreEfrags (efrag_t **ppefrag);
921 void R_NewWorldNode ()
923 int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
925 msurface_t *surf, **mark, **endmark;
927 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
929 if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
931 if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
936 // deal with model fragments in this leaf
938 R_StoreEfrags (&leaf->efrags);
940 if (leaf->nummarksurfaces)
942 mark = leaf->firstmarksurface;
943 endmark = mark + leaf->nummarksurfaces;
947 // make sure surfaces are only processed once
948 if (surf->worldnodeframe == r_framecount)
950 surf->worldnodeframe = r_framecount;
951 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
953 if ( (surf->flags & SURF_PLANEBACK))
955 surf->visframe = r_framecount;
959 surf->texturechain = surf->texinfo->texture->texturechain;
960 surf->texinfo->texture->texturechain = surf;
963 R_DrawSurf(surf, false, vertex);
968 if (!(surf->flags & SURF_PLANEBACK))
970 surf->visframe = r_framecount;
974 surf->texturechain = surf->texinfo->texture->texturechain;
975 surf->texinfo->texture->texturechain = surf;
978 R_DrawSurf(surf, false, vertex);
982 while (mark < endmark);
1002 int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
1003 struct nodestack_s *nstack;
1006 msurface_t *surf, *endsurf, **mark, **endmark;
1009 if (!(node = cl.worldmodel->nodes))
1016 if (R_CullBox(node->minmaxs, node->minmaxs+3))
1019 if (nstack <= nodestack)
1022 node = nstack->node;
1023 side = nstack->side;
1024 noclipping = nstack->noclipping;
1031 ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
1032 cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
1033 cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
1034 cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
1035 if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
1036 noclipping = true; // not clipped at all, no need to clip any children of this node
1037 // partially clipped node
1039 // if a leaf node, draw stuff
1040 if (node->contents < 0)
1042 if (node->contents != CONTENTS_SOLID)
1044 pleaf = (mleaf_t *)node;
1047 if (pleaf->nummarksurfaces)
1049 mark = pleaf->firstmarksurface;
1050 endmark = mark + pleaf->nummarksurfaces;
1053 (*mark)->visframe = r_framecount;
1056 while (mark < endmark);
1059 // deal with model fragments in this leaf
1061 R_StoreEfrags (&pleaf->efrags);
1064 if (nstack <= nodestack)
1067 node = nstack->node;
1068 side = nstack->side;
1069 noclipping = nstack->noclipping;
1075 // node is just a decision point, so go down the apropriate sides
1077 // find which side of the node we are on
1078 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
1080 // recurse down the children, front side first
1081 if (node->children[side]->visframe == r_visframecount)
1083 nstack->node = node;
1084 nstack->side = !side; // go down back side when we come back up
1085 nstack->noclipping = noclipping;
1087 node = node->children[side];
1094 if (node->numsurfaces)
1096 surf = cl.worldmodel->surfaces + node->firstsurface;
1097 endsurf = surf + node->numsurfaces;
1105 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1108 surf->texturechain = surf->texinfo->texture->texturechain;
1109 surf->texinfo->texture->texturechain = surf;
1112 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1115 while (surf < endsurf);
1121 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1124 surf->texturechain = surf->texinfo->texture->texturechain;
1125 surf->texinfo->texture->texturechain = surf;
1128 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1131 while (surf < endsurf);
1140 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1143 R_DrawSurf(surf, false, vertex);
1146 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1149 while (surf < endsurf);
1155 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1158 R_DrawSurf(surf, false, vertex);
1161 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1164 while (surf < endsurf);
1169 // recurse down the back side
1170 if (node->children[side]->visframe == r_visframecount)
1172 node = node->children[side];
1176 if (nstack <= nodestack)
1179 node = nstack->node;
1180 side = nstack->side;
1181 noclipping = nstack->noclipping;
1192 void R_DrawWorld (void)
1196 memset (&ent, 0, sizeof(ent));
1197 ent.model = cl.worldmodel;
1198 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1199 modelalpha = ent.alpha = 1;
1202 VectorCopy (r_refdef.vieworg, modelorg);
1204 currententity = &ent;
1206 softwaretransformidentity(); // LordHavoc: clear transform
1210 if (r_newworldnode.value)
1216 R_PushDlights (); // now mark the lit surfaces
1218 DrawTextureChains ();
1227 void R_MarkLeaves (void)
1233 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1237 r_oldviewleaf = r_viewleaf;
1241 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1243 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1246 if (node->visframe == r_visframecount)
1248 node->visframe = r_visframecount;
1249 node = node->parent;
1255 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1257 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1259 if (vis[i>>3] & (1<<(i&7)))
1261 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1264 if (node->visframe == r_visframecount)
1266 node->visframe = r_visframecount;
1267 node = node->parent;
1277 =============================================================================
1281 =============================================================================
1284 // returns a texture number and the position inside it
1285 int AllocBlock (int w, int h, short *x, short *y)
1291 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1293 best = BLOCK_HEIGHT;
1295 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1299 for (j=0 ; j<w ; j++)
1301 if (allocated[texnum][i+j] >= best)
1303 if (allocated[texnum][i+j] > best2)
1304 best2 = allocated[texnum][i+j];
1307 { // this is a valid spot
1313 if (best + h > BLOCK_HEIGHT)
1316 if (nosubimagefragments || nosubimage)
1318 if (!lightmaps[texnum])
1320 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1321 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1324 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1325 else if (!allocated[texnum][0])
1327 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1328 memset(blank, 0, sizeof(blank));
1331 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1332 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1333 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1335 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1337 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1341 for (i=0 ; i<w ; i++)
1342 allocated[texnum][*x + i] = best + h;
1347 Sys_Error ("AllocBlock: full");
1352 mvertex_t *r_pcurrentvertbase;
1353 model_t *currentmodel;
1359 BuildSurfaceDisplayList
1362 void BuildSurfaceDisplayList (msurface_t *fa)
1364 int i, j, lindex, lnumverts;
1365 medge_t *pedges, *r_pedge;
1371 // reconstruct the polygon
1372 pedges = currentmodel->edges;
1373 lnumverts = fa->numedges;
1379 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1380 poly->next = fa->polys;
1381 poly->flags = fa->flags;
1383 poly->numverts = lnumverts;
1385 for (i=0 ; i<lnumverts ; i++)
1387 lindex = currentmodel->surfedges[fa->firstedge + i];
1391 r_pedge = &pedges[lindex];
1392 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1396 r_pedge = &pedges[-lindex];
1397 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1399 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1400 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1402 VectorCopy (vec, poly->verts[i]);
1403 poly->verts[i][3] = s / fa->texinfo->texture->width;
1404 poly->verts[i][4] = t / fa->texinfo->texture->height;
1407 // lightmap texture coordinates
1409 s -= fa->texturemins[0];
1410 t -= fa->texturemins[1];
1413 // LordHavoc: calc lightmap data offset
1414 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;
1415 poly->verts[i][7] = j;
1416 s += fa->light_s*16;
1417 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1419 t += fa->light_t*16;
1420 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1422 poly->verts[i][5] = s;
1423 poly->verts[i][6] = t;
1427 // remove co-linear points - Ed
1430 if (!gl_keeptjunctions.value)
1432 for (i = 0 ; i < lnumverts ; ++i)
1435 float *prev, *this, *next;
1437 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1438 this = poly->verts[i];
1439 next = poly->verts[(i + 1) % lnumverts];
1441 VectorSubtract( this, prev, v1 );
1442 VectorNormalize( v1 );
1443 VectorSubtract( next, prev, v2 );
1444 VectorNormalize( v2 );
1446 // skip co-linear points
1447 #define COLINEAR_EPSILON 0.001
1448 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1449 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1450 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1453 for (j = i + 1; j < lnumverts; ++j)
1456 for (k = 0; k < VERTEXSIZE; ++k)
1457 poly->verts[j - 1][k] = poly->verts[j][k];
1461 // retry next vertex next time, which is now current vertex
1467 poly->numverts = lnumverts;
1471 ========================
1472 GL_CreateSurfaceLightmap
1473 ========================
1475 void GL_CreateSurfaceLightmap (msurface_t *surf)
1479 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1482 smax = (surf->extents[0]>>4)+1;
1483 tmax = (surf->extents[1]>>4)+1;
1485 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1486 if (nosubimage || nosubimagefragments)
1488 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1489 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1492 R_BuildLightMap (surf, templight, smax * 4);
1494 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1498 R_BuildLightMap (surf, templight, smax * 3);
1500 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1509 Builds the lightmap texture
1510 with all the surfaces from all brush models
1513 void GL_BuildLightmaps (void)
1518 memset (allocated, 0, sizeof(allocated));
1520 r_framecount = 1; // no dlightcache
1522 if (gl_nosubimagefragments.value)
1523 nosubimagefragments = 1;
1525 nosubimagefragments = 0;
1527 if (gl_nosubimage.value)
1532 if (gl_lightmaprgba.value)
1534 lightmaprgba = true;
1539 lightmaprgba = false;
1543 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1544 // it needs to be aligned on 4 pixel boundaries...
1545 // so I implemented an adjustable lightmap alignment
1546 if (gl_lightmapalign.value < 1)
1547 gl_lightmapalign.value = 1;
1548 if (gl_lightmapalign.value > 16)
1549 gl_lightmapalign.value = 16;
1551 while (lightmapalign < gl_lightmapalign.value)
1552 lightmapalign <<= 1;
1553 gl_lightmapalign.value = lightmapalign;
1554 lightmapalignmask = ~(lightmapalign - 1);
1555 if (nosubimagefragments || nosubimage)
1558 lightmapalignmask = ~0;
1561 if (!lightmap_textures)
1563 lightmap_textures = texture_extension_number;
1564 texture_extension_number += MAX_LIGHTMAPS;
1567 for (j=1 ; j<MAX_MODELS ; j++)
1569 m = cl.model_precache[j];
1572 if (m->name[0] == '*')
1574 r_pcurrentvertbase = m->vertexes;
1576 for (i=0 ; i<m->numsurfaces ; i++)
1578 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1580 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1582 GL_CreateSurfaceLightmap (m->surfaces + i);
1583 BuildSurfaceDisplayList (m->surfaces + i);
1587 if (nosubimage || nosubimagefragments)
1591 qglSelectTexture(gl_mtex_enum+1);
1592 for (i = 0;i < MAX_LIGHTMAPS;i++)
1594 if (!allocated[i][0])
1596 lightmapupdate[i][0] = BLOCK_HEIGHT;
1597 lightmapupdate[i][1] = 0;
1600 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1601 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1602 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1604 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1606 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1611 qglSelectTexture(gl_mtex_enum+0);