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", "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"};
53 cvar_t r_dlightmap = {"r_dlightmap", "1"};
55 qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
58 extern qboolean gl_arrays;
60 extern int r_dlightframecount;
66 void gl_surf_shutdown()
73 for (i = 0;i < MAX_LIGHTMAPS;i++)
75 Cvar_RegisterVariable(&gl_lightmapalign);
76 Cvar_RegisterVariable(&gl_lightmaprgba);
77 Cvar_RegisterVariable(&gl_nosubimagefragments);
78 Cvar_RegisterVariable(&gl_nosubimage);
79 Cvar_RegisterVariable(&r_ambient);
80 // Cvar_RegisterVariable(&gl_funnywalls);
81 Cvar_RegisterVariable(&gl_vertex);
82 Cvar_RegisterVariable(&gl_texsort);
83 Cvar_RegisterVariable(&r_newworldnode);
84 Cvar_RegisterVariable(&r_oldclip);
85 Cvar_RegisterVariable(&r_dlightmap);
86 // check if it's the glquake minigl driver
87 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
90 // Cvar_SetValue("gl_nosubimagefragments", 1);
91 // Cvar_SetValue("gl_nosubimage", 1);
92 Cvar_SetValue("gl_lightmode", 0);
95 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
98 int dlightdivtable[32768];
103 int R_AddDynamicLights (msurface_t *surf)
105 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, j, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
108 vec3_t impact, local;
110 // LordHavoc: use 64bit integer... shame it's not very standardized...
111 //#if _MSC_VER || __BORLANDC__
117 // LordHavoc: later note: MSVC and hopefully all other C compilers use a 64bit result for 32bit*32bit multiply, so that was not necessary
122 if (!dlightdivtable[1])
124 dlightdivtable[0] = 4194304;
125 for (s = 1; s < 32768; s++)
126 dlightdivtable[s] = 4194304 / (s << 7);
129 smax = (surf->extents[0] >> 4) + 1;
130 tmax = (surf->extents[1] >> 4) + 1;
132 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
134 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
135 continue; // not lit by this light
137 VectorSubtract (cl_dlights[lnum].origin, currententity->origin, local);
138 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
140 // for comparisons to minimum acceptable light
141 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius) * LIGHTSCALE);
143 // clamp radius to avoid exceeding 32768 entry division table
144 if (maxdist > 4194304)
148 if (dist2 >= maxdist)
151 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
152 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
153 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
155 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
156 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
158 s = bound(0, impacts, smax * 16) - impacts;
159 t = bound(0, impactt, tmax * 16) - impactt;
160 i = s * s + t * t + dist2;
164 // reduce calculations
165 for (s = 0, i = impacts; s < smax; s++, i -= 16)
166 sdtable[s] = i * i + dist2;
168 maxdist3 = maxdist - (int) (dist * dist);
170 // convert to 8.8 blocklights format and scale up by radius
171 red = cl_dlights[lnum].color[0] * maxdist;
172 green = cl_dlights[lnum].color[1] * maxdist;
173 blue = cl_dlights[lnum].color[2] * maxdist;
177 for (t = 0; t < tmax; t++, i -= 16)
180 // make sure some part of it is visible on this line
183 maxdist2 = maxdist - td;
184 for (s = 0; s < smax; s++)
186 if (sdtable[s] < maxdist2)
188 k = dlightdivtable[(sdtable[s] + td) >> 7];
189 j = (red * k) >> 9;bl[0] += j;
190 j = (green * k) >> 9;bl[1] += j;
191 j = (blue * k) >> 9;bl[2] += j;
209 Combine and scale multiple lightmaps into the 8.8 format in blocklights
212 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
215 int i, j, size, size3;
221 surf->cached_dlight = 0;
222 surf->cached_lighthalf = lighthalf;
223 surf->cached_ambient = r_ambient.value;
225 smax = (surf->extents[0]>>4)+1;
226 tmax = (surf->extents[1]>>4)+1;
229 lightmap = surf->samples;
231 // set to full bright if no light data
232 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
235 for (i=0 ; i<size ; i++)
245 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
249 for (i = 0;i < size3;i++)
253 memset(&blocklights[0], 0, size*3*sizeof(int));
255 // add all the lightmaps
258 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
260 scale = d_lightstylevalue[surf->styles[maps]];
261 surf->cached_light[maps] = scale; // 8.8 fraction
263 for (i = 0;i < size3;i++)
264 *bl++ += *lightmap++ * scale;
267 if (r_dlightmap.value && surf->dlightframe == r_dlightframecount)
268 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
271 stride -= (smax*lightmapbytes);
275 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
276 // the image is brightened as a processing pass
279 for (i = 0;i < tmax;i++, dest += stride)
281 for (j = 0;j < smax;j++, bl += 3, dest += 4)
283 dest[0] = min(bl[0] >> 8, 255);
284 dest[1] = min(bl[1] >> 8, 255);
285 dest[2] = min(bl[2] >> 8, 255);
292 for (i = 0;i < tmax;i++, dest += stride)
294 for (j = 0;j < smax;j++, bl += 3, dest += 3)
296 dest[0] = min(bl[0] >> 8, 255);
297 dest[1] = min(bl[1] >> 8, 255);
298 dest[2] = min(bl[2] >> 8, 255);
307 for (i = 0;i < tmax;i++, dest += stride)
309 for (j = 0;j < smax;j++, bl += 3, dest += 4)
311 dest[0] = min(bl[0] >> 7, 255);
312 dest[1] = min(bl[1] >> 7, 255);
313 dest[2] = min(bl[2] >> 7, 255);
320 for (i = 0;i < tmax;i++, dest += stride)
322 for (j = 0;j < smax;j++, bl += 3, dest += 3)
324 dest[0] = min(bl[0] >> 7, 255);
325 dest[1] = min(bl[1] >> 7, 255);
326 dest[2] = min(bl[2] >> 7, 255);
333 byte templight[32*32*4];
335 void R_UpdateLightmap(msurface_t *s, int lnum)
338 // upload the new lightmap texture fragment
340 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
341 if (nosubimage || nosubimagefragments)
343 if (lightmapupdate[lnum][0] > s->light_t)
344 lightmapupdate[lnum][0] = s->light_t;
345 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
346 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
348 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
350 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
354 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
355 tmax = (s->extents[1]>>4)+1;
358 R_BuildLightMap (s, templight, smax * 4);
360 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
364 R_BuildLightMap (s, templight, smax * 3);
366 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
376 Returns the proper texture for a given time and base texture
379 texture_t *R_TextureAnimation (texture_t *base)
385 if (currententity->frame)
387 if (base->alternate_anims)
388 base = base->alternate_anims;
391 if (!base->anim_total)
396 relative = (int)(cl.time*10) % base->anim_total;
399 while (base->anim_min > relative || base->anim_max <= relative)
401 base = base->anim_next;
404 Con_Printf("R_TextureAnimation: broken cycle");
409 Con_Printf("R_TextureAnimation: infinite cycle");
419 =============================================================
423 =============================================================
427 extern int solidskytexture;
428 extern int alphaskytexture;
429 extern float speedscale; // for top sky and bottom sky
431 extern char skyname[];
433 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
436 #include "gl_warp_sin.h"
438 #define TURBSCALE (256.0 / (2 * M_PI))
441 void UploadLightmaps()
444 if (nosubimage || nosubimagefragments)
446 for (i = 0;i < MAX_LIGHTMAPS;i++)
448 if (lightmapupdate[i][0] < lightmapupdate[i][1])
452 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
456 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
458 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
463 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]));
465 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]));
469 lightmapupdate[i][0] = BLOCK_HEIGHT;
470 lightmapupdate[i][1] = 0;
475 float wvert[1024*6]; // used by the following functions
477 void RSurf_DrawSky(msurface_t *s, int transform)
482 for (p=s->polys ; p ; p=p->next)
484 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
486 skypoly[currentskypoly].firstvert = currentskyvert;
487 skypoly[currentskypoly++].verts = p->numverts;
490 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
492 softwaretransform(v, skyvert[currentskyvert].v);
498 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
500 skyvert[currentskyvert].v[0] = v[0];
501 skyvert[currentskyvert].v[1] = v[1];
502 skyvert[currentskyvert++].v[2] = v[2];
509 int RSurf_Light(int *dlightbits, glpoly_t *polys)
511 float cr, cg, cb, radius, radius2, f, *v, *wv;
512 int i, a, b, lit = false;
517 for (a = 0;a < 8;a++)
519 if ((c = dlightbits[a]))
521 for (b = 0, d = 1;c;b++, d <<= 1)
526 light = &cl_dlights[a * 32 + b];
527 lightorigin = light->origin;
528 cr = light->color[0];
529 cg = light->color[1];
530 cb = light->color[2];
531 radius = light->radius*light->radius*LIGHTSCALE;
532 radius2 = radius * (256.0f / LIGHTSCALE2);
534 for (p = polys;p;p = p->next)
536 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
538 f = VectorDistance2(wv, lightorigin);
541 f = radius2 / (f + LIGHTOFFSET);
557 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
560 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
564 for (p = s->polys;p;p = p->next)
566 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
569 softwaretransform(v, wv);
572 if (r_waterripple.value)
573 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);
574 wv[3] = wv[4] = wv[5] = 128.0f;
578 if (s->dlightframe == r_dlightframecount)
579 RSurf_Light(s->dlightbits, s->polys);
581 // FIXME: make fog texture if water texture is transparent?
582 for (p=s->polys ; p ; p=p->next)
584 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
585 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
586 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);
591 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
593 int i, lit = false, polys = 0, verts = 0;
598 // check for lightmap modification
602 || (r_dlightmap.value && s->dlightframe == r_dlightframecount)
603 || r_ambient.value != s->cached_ambient
604 || lighthalf != s->cached_lighthalf
605 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
606 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
607 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
608 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
609 R_UpdateLightmap(s, s->lightmaptexturenum);
612 for (p = s->polys;p;p = p->next)
614 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
617 softwaretransform(v, wv);
620 wv[3] = wv[4] = wv[5] = 0.0f;
623 verts += p->numverts;
626 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
628 if ((!r_dlightmap.value) && s->dlightframe == r_dlightframecount)
629 lit = RSurf_Light(s->dlightbits, s->polys);
631 wp = &wallpoly[currentwallpoly];
632 out = &wallvert[currentwallvert];
633 currentwallpoly += polys;
634 for (p = s->polys;p;p = p->next)
637 wp->texnum = (unsigned short) t->gl_texturenum;
638 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
639 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
640 wp->firstvert = currentwallvert;
641 wp->numverts = p->numverts;
644 currentwallvert += p->numverts;
645 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
651 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
652 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
653 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
658 out->r = (byte) (bound(0, (int) wv[3], 255));
659 out->g = (byte) (bound(0, (int) wv[4], 255));
660 out->b = (byte) (bound(0, (int) wv[5], 255));
664 out->vert[0] = wv[0];
665 out->vert[1] = wv[1];
666 out->vert[2] = wv[2];
675 // LordHavoc: transparent brush models
676 extern int r_dlightframecount;
677 extern float modelalpha;
679 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
682 float *v, *wv, scale;
685 alpha = (int) (modelalpha * 255.0f);
686 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
688 for (p = s->polys;p;p = p->next)
690 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
693 softwaretransform(v, wv);
696 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
697 if (s->styles[0] != 255)
699 lm = (byte *)((long) s->samples + (int) v[7]);
700 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;
701 if (s->styles[1] != 255)
703 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;
704 if (s->styles[2] != 255)
706 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;
707 if (s->styles[3] != 255)
709 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;
717 if (s->dlightframe == r_dlightframecount)
718 RSurf_Light(s->dlightbits, s->polys);
720 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
722 for (p = s->polys;p;p = p->next)
725 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
726 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
727 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);
733 for (p = s->polys;p;p = p->next)
736 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
737 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
738 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
749 extern qboolean hlbsp;
750 extern char skyname[];
751 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
754 if (s->flags & SURF_DRAWSKY)
757 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
758 RSurf_DrawSky(s, false);
761 t = R_TextureAnimation (s->texinfo->texture);
762 if (s->flags & SURF_DRAWTURB)
764 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
768 RSurf_DrawWallVertex(s, t, false, false);
770 RSurf_DrawWall(s, t, false);
773 void DrawTextureChains (void)
779 for (n = 0;n < cl.worldmodel->numtextures;n++)
781 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
783 cl.worldmodel->textures[n]->texturechain = NULL;
784 // for (;s;s = s->texturechain)
785 // R_DrawSurf(s, false, gl_vertex.value);
786 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
788 if (s->flags & SURF_DRAWSKY)
791 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
792 for (;s;s = s->texturechain)
793 RSurf_DrawSky(s, false);
796 t = R_TextureAnimation (cl.worldmodel->textures[n]);
797 // subdivided water surface warp
798 if (s->flags & SURF_DRAWTURB)
800 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
801 for (;s;s = s->texturechain)
802 RSurf_DrawWater(s, t, false, alpha);
806 for (;s;s = s->texturechain)
807 RSurf_DrawWallVertex(s, t, false, false);
809 for (;s;s = s->texturechain)
810 RSurf_DrawWall(s, t, false);
814 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
821 void R_DrawBrushModel (entity_t *e)
827 int rotated, vertexlit = false;
835 if (e->angles[0] || e->angles[1] || e->angles[2])
838 for (i=0 ; i<3 ; i++)
840 mins[i] = e->origin[i] - clmodel->radius;
841 maxs[i] = e->origin[i] + clmodel->radius;
847 VectorAdd (e->origin, clmodel->mins, mins);
848 VectorAdd (e->origin, clmodel->maxs, maxs);
851 if (R_CullBox (mins, maxs))
856 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
860 vec3_t forward, right, up;
862 VectorCopy (modelorg, temp);
863 AngleVectors (e->angles, forward, right, up);
864 modelorg[0] = DotProduct (temp, forward);
865 modelorg[1] = -DotProduct (temp, right);
866 modelorg[2] = DotProduct (temp, up);
869 s = &clmodel->surfaces[clmodel->firstmodelsurface];
871 // calculate dynamic lighting for bmodel if it's not an
873 for (i = 0;i < MAX_DLIGHTS;i++)
875 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
878 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
879 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
881 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
883 e->angles[0] = -e->angles[0]; // stupid quake bug
884 softwaretransformforentity (e);
885 e->angles[0] = -e->angles[0]; // stupid quake bug
888 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
890 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
892 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
893 if (s->flags & SURF_DRAWSKY)
895 RSurf_DrawSky(s, true);
898 t = R_TextureAnimation (s->texinfo->texture);
899 if (s->flags & SURF_DRAWTURB)
901 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
904 if (vertexlit || s->texinfo->texture->transparent)
905 RSurf_DrawWallVertex(s, t, true, true);
907 RSurf_DrawWall(s, t, true);
914 =============================================================
918 =============================================================
921 void R_StoreEfrags (efrag_t **ppefrag);
923 void R_NewWorldNode ()
925 int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
927 msurface_t *surf, **mark, **endmark;
929 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
931 if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
933 if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
938 // deal with model fragments in this leaf
940 R_StoreEfrags (&leaf->efrags);
942 if (leaf->nummarksurfaces)
944 mark = leaf->firstmarksurface;
945 endmark = mark + leaf->nummarksurfaces;
949 // make sure surfaces are only processed once
950 if (surf->worldnodeframe == r_framecount)
952 surf->worldnodeframe = r_framecount;
953 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
955 if ( (surf->flags & SURF_PLANEBACK))
957 surf->visframe = r_framecount;
961 surf->texturechain = surf->texinfo->texture->texturechain;
962 surf->texinfo->texture->texturechain = surf;
965 R_DrawSurf(surf, false, vertex);
970 if (!(surf->flags & SURF_PLANEBACK))
972 surf->visframe = r_framecount;
976 surf->texturechain = surf->texinfo->texture->texturechain;
977 surf->texinfo->texture->texturechain = surf;
980 R_DrawSurf(surf, false, vertex);
984 while (mark < endmark);
1004 int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
1005 struct nodestack_s *nstack;
1008 msurface_t *surf, *endsurf, **mark, **endmark;
1011 if (!(node = cl.worldmodel->nodes))
1018 if (R_CullBox(node->minmaxs, node->minmaxs+3))
1021 if (nstack <= nodestack)
1024 node = nstack->node;
1025 side = nstack->side;
1026 noclipping = nstack->noclipping;
1033 ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
1034 cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
1035 cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
1036 cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
1037 if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
1038 noclipping = true; // not clipped at all, no need to clip any children of this node
1039 // partially clipped node
1041 // if a leaf node, draw stuff
1042 if (node->contents < 0)
1044 if (node->contents != CONTENTS_SOLID)
1046 pleaf = (mleaf_t *)node;
1049 if (pleaf->nummarksurfaces)
1051 mark = pleaf->firstmarksurface;
1052 endmark = mark + pleaf->nummarksurfaces;
1055 (*mark)->visframe = r_framecount;
1058 while (mark < endmark);
1061 // deal with model fragments in this leaf
1063 R_StoreEfrags (&pleaf->efrags);
1066 if (nstack <= nodestack)
1069 node = nstack->node;
1070 side = nstack->side;
1071 noclipping = nstack->noclipping;
1077 // node is just a decision point, so go down the apropriate sides
1079 // find which side of the node we are on
1080 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
1082 // recurse down the children, front side first
1083 if (node->children[side]->visframe == r_visframecount)
1085 nstack->node = node;
1086 nstack->side = !side; // go down back side when we come back up
1087 nstack->noclipping = noclipping;
1089 node = node->children[side];
1096 if (node->numsurfaces)
1098 surf = cl.worldmodel->surfaces + node->firstsurface;
1099 endsurf = surf + node->numsurfaces;
1107 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1110 surf->texturechain = surf->texinfo->texture->texturechain;
1111 surf->texinfo->texture->texturechain = surf;
1114 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1117 while (surf < endsurf);
1123 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1126 surf->texturechain = surf->texinfo->texture->texturechain;
1127 surf->texinfo->texture->texturechain = surf;
1130 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1133 while (surf < endsurf);
1142 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1145 R_DrawSurf(surf, false, vertex);
1148 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1151 while (surf < endsurf);
1157 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1160 R_DrawSurf(surf, false, vertex);
1163 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1166 while (surf < endsurf);
1171 // recurse down the back side
1172 if (node->children[side]->visframe == r_visframecount)
1174 node = node->children[side];
1178 if (nstack <= nodestack)
1181 node = nstack->node;
1182 side = nstack->side;
1183 noclipping = nstack->noclipping;
1194 void R_DrawWorld (void)
1198 memset (&ent, 0, sizeof(ent));
1199 ent.model = cl.worldmodel;
1200 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1201 modelalpha = ent.alpha = 1;
1204 VectorCopy (r_refdef.vieworg, modelorg);
1206 currententity = &ent;
1208 softwaretransformidentity(); // LordHavoc: clear transform
1212 if (r_newworldnode.value)
1218 R_PushDlights (); // now mark the lit surfaces
1220 DrawTextureChains ();
1229 void R_MarkLeaves (void)
1235 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1239 r_oldviewleaf = r_viewleaf;
1243 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1245 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1248 if (node->visframe == r_visframecount)
1250 node->visframe = r_visframecount;
1251 node = node->parent;
1257 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1259 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1261 if (vis[i>>3] & (1<<(i&7)))
1263 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1266 if (node->visframe == r_visframecount)
1268 node->visframe = r_visframecount;
1269 node = node->parent;
1279 =============================================================================
1283 =============================================================================
1286 // returns a texture number and the position inside it
1287 int AllocBlock (int w, int h, short *x, short *y)
1293 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1295 best = BLOCK_HEIGHT;
1297 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1301 for (j=0 ; j<w ; j++)
1303 if (allocated[texnum][i+j] >= best)
1305 if (allocated[texnum][i+j] > best2)
1306 best2 = allocated[texnum][i+j];
1309 { // this is a valid spot
1315 if (best + h > BLOCK_HEIGHT)
1318 if (nosubimagefragments || nosubimage)
1320 if (!lightmaps[texnum])
1322 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1323 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1326 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1327 else if (!allocated[texnum][0])
1329 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1330 memset(blank, 0, sizeof(blank));
1333 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1334 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1335 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1337 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1339 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1343 for (i=0 ; i<w ; i++)
1344 allocated[texnum][*x + i] = best + h;
1349 Sys_Error ("AllocBlock: full");
1354 mvertex_t *r_pcurrentvertbase;
1355 model_t *currentmodel;
1361 BuildSurfaceDisplayList
1364 void BuildSurfaceDisplayList (msurface_t *fa)
1366 int i, j, lindex, lnumverts;
1367 medge_t *pedges, *r_pedge;
1373 // reconstruct the polygon
1374 pedges = currentmodel->edges;
1375 lnumverts = fa->numedges;
1381 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1382 poly->next = fa->polys;
1383 poly->flags = fa->flags;
1385 poly->numverts = lnumverts;
1387 for (i=0 ; i<lnumverts ; i++)
1389 lindex = currentmodel->surfedges[fa->firstedge + i];
1393 r_pedge = &pedges[lindex];
1394 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1398 r_pedge = &pedges[-lindex];
1399 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1401 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1402 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1404 VectorCopy (vec, poly->verts[i]);
1405 poly->verts[i][3] = s / fa->texinfo->texture->width;
1406 poly->verts[i][4] = t / fa->texinfo->texture->height;
1409 // lightmap texture coordinates
1411 s -= fa->texturemins[0];
1412 t -= fa->texturemins[1];
1415 // LordHavoc: calc lightmap data offset
1416 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;
1417 poly->verts[i][7] = j;
1418 s += fa->light_s*16;
1419 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1421 t += fa->light_t*16;
1422 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1424 poly->verts[i][5] = s;
1425 poly->verts[i][6] = t;
1429 // remove co-linear points - Ed
1432 if (!gl_keeptjunctions.value)
1434 for (i = 0 ; i < lnumverts ; ++i)
1437 float *prev, *this, *next;
1439 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1440 this = poly->verts[i];
1441 next = poly->verts[(i + 1) % lnumverts];
1443 VectorSubtract( this, prev, v1 );
1444 VectorNormalize( v1 );
1445 VectorSubtract( next, prev, v2 );
1446 VectorNormalize( v2 );
1448 // skip co-linear points
1449 #define COLINEAR_EPSILON 0.001
1450 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1451 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1452 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1455 for (j = i + 1; j < lnumverts; ++j)
1458 for (k = 0; k < VERTEXSIZE; ++k)
1459 poly->verts[j - 1][k] = poly->verts[j][k];
1463 // retry next vertex next time, which is now current vertex
1469 poly->numverts = lnumverts;
1473 ========================
1474 GL_CreateSurfaceLightmap
1475 ========================
1477 void GL_CreateSurfaceLightmap (msurface_t *surf)
1481 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1484 smax = (surf->extents[0]>>4)+1;
1485 tmax = (surf->extents[1]>>4)+1;
1487 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1488 if (nosubimage || nosubimagefragments)
1490 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1491 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1494 R_BuildLightMap (surf, templight, smax * 4);
1496 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1500 R_BuildLightMap (surf, templight, smax * 3);
1502 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1511 Builds the lightmap texture
1512 with all the surfaces from all brush models
1515 void GL_BuildLightmaps (void)
1520 memset (allocated, 0, sizeof(allocated));
1522 r_framecount = 1; // no dlightcache
1524 if (gl_nosubimagefragments.value)
1525 nosubimagefragments = 1;
1527 nosubimagefragments = 0;
1529 if (gl_nosubimage.value)
1534 if (gl_lightmaprgba.value)
1536 lightmaprgba = true;
1541 lightmaprgba = false;
1545 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1546 // it needs to be aligned on 4 pixel boundaries...
1547 // so I implemented an adjustable lightmap alignment
1548 if (gl_lightmapalign.value < 1)
1549 gl_lightmapalign.value = 1;
1550 if (gl_lightmapalign.value > 16)
1551 gl_lightmapalign.value = 16;
1553 while (lightmapalign < gl_lightmapalign.value)
1554 lightmapalign <<= 1;
1555 gl_lightmapalign.value = lightmapalign;
1556 lightmapalignmask = ~(lightmapalign - 1);
1557 if (nosubimagefragments || nosubimage)
1560 lightmapalignmask = ~0;
1563 if (!lightmap_textures)
1565 lightmap_textures = texture_extension_number;
1566 texture_extension_number += MAX_LIGHTMAPS;
1569 for (j=1 ; j<MAX_MODELS ; j++)
1571 m = cl.model_precache[j];
1574 if (m->name[0] == '*')
1576 r_pcurrentvertbase = m->vertexes;
1578 for (i=0 ; i<m->numsurfaces ; i++)
1580 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1582 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1584 GL_CreateSurfaceLightmap (m->surfaces + i);
1585 BuildSurfaceDisplayList (m->surfaces + i);
1589 if (nosubimage || nosubimagefragments)
1593 qglSelectTexture(gl_mtex_enum+1);
1594 for (i = 0;i < MAX_LIGHTMAPS;i++)
1596 if (!allocated[i][0])
1598 lightmapupdate[i][0] = BLOCK_HEIGHT;
1599 lightmapupdate[i][1] = 0;
1602 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1603 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1604 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1606 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1608 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1613 qglSelectTexture(gl_mtex_enum+0);