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 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
27 #define BLOCK_WIDTH 256
28 #define BLOCK_HEIGHT 256
29 // LordHavoc: increased lightmap limit from 64 to 1024
30 #define MAX_LIGHTMAPS 1024
31 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
35 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
37 byte *lightmaps[MAX_LIGHTMAPS];
38 short lightmapupdate[MAX_LIGHTMAPS][2];
40 signed int blocklights[BLOCK_WIDTH*BLOCK_HEIGHT*3]; // LordHavoc: *3 for colored lighting
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 int r_dlightframecount;
63 void gl_surf_shutdown()
70 for (i = 0;i < MAX_LIGHTMAPS;i++)
72 Cvar_RegisterVariable(&gl_lightmapalign);
73 Cvar_RegisterVariable(&gl_lightmaprgba);
74 Cvar_RegisterVariable(&gl_nosubimagefragments);
75 Cvar_RegisterVariable(&gl_nosubimage);
76 Cvar_RegisterVariable(&r_ambient);
77 Cvar_RegisterVariable(&gl_vertex);
78 Cvar_RegisterVariable(&gl_texsort);
79 Cvar_RegisterVariable(&r_newworldnode);
80 Cvar_RegisterVariable(&r_oldclip);
81 Cvar_RegisterVariable(&r_dlightmap);
83 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
86 int dlightdivtable[32768];
91 int R_AddDynamicLights (msurface_t *surf)
93 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
98 // LordHavoc: use 64bit integer... shame it's not very standardized...
99 #if _MSC_VER || __BORLANDC__
107 if (!dlightdivtable[1])
109 dlightdivtable[0] = 4194304;
110 for (s = 1; s < 32768; s++)
111 dlightdivtable[s] = 4194304 / (s << 7);
114 smax = (surf->extents[0] >> 4) + 1;
115 tmax = (surf->extents[1] >> 4) + 1;
117 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
119 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
120 continue; // not lit by this light
122 VectorSubtract (cl_dlights[lnum].origin, currententity->origin, local);
123 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
125 // for comparisons to minimum acceptable light
126 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
128 // clamp radius to avoid exceeding 32768 entry division table
129 if (maxdist > 4194304)
133 if (dist2 >= maxdist)
136 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
137 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
138 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
140 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
141 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
143 s = bound(0, impacts, smax * 16) - impacts;
144 t = bound(0, impactt, tmax * 16) - impactt;
145 i = s * s + t * t + dist2;
149 // reduce calculations
150 for (s = 0, i = impacts; s < smax; s++, i -= 16)
151 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
153 maxdist3 = maxdist - (int) (dist * dist);
155 // convert to 8.8 blocklights format and scale up by radius
156 red = cl_dlights[lnum].color[0] * maxdist;
157 green = cl_dlights[lnum].color[1] * maxdist;
158 blue = cl_dlights[lnum].color[2] * maxdist;
162 for (t = 0; t < tmax; t++, i -= 16)
165 // make sure some part of it is visible on this line
168 maxdist2 = maxdist - td;
169 for (s = 0; s < smax; s++)
171 if (sdtable[s] < maxdist2)
173 k = dlightdivtable[(sdtable[s] + td) >> 7];
174 bl[0] += (red * k) >> 9;
175 bl[1] += (green * k) >> 9;
176 bl[2] += (blue * k) >> 9;
190 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
193 stride -= (width*lightmapbytes);
196 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
197 // the image is brightened as a processing pass
200 for (i = 0;i < height;i++, out += stride)
202 for (j = 0;j < width;j++, in += 3, out += 4)
204 out[0] = min(in[0] >> 8, 255);
205 out[1] = min(in[1] >> 8, 255);
206 out[2] = min(in[2] >> 8, 255);
213 for (i = 0;i < height;i++, out += stride)
215 for (j = 0;j < width;j++, in += 3, out += 3)
217 out[0] = min(in[0] >> 8, 255);
218 out[1] = min(in[1] >> 8, 255);
219 out[2] = min(in[2] >> 8, 255);
228 for (i = 0;i < height;i++, out += stride)
230 for (j = 0;j < width;j++, in += 3, out += 4)
232 out[0] = min(in[0] >> 7, 255);
233 out[1] = min(in[1] >> 7, 255);
234 out[2] = min(in[2] >> 7, 255);
241 for (i = 0;i < height;i++, out += stride)
243 for (j = 0;j < width;j++, in += 3, out += 3)
245 out[0] = min(in[0] >> 7, 255);
246 out[1] = min(in[1] >> 7, 255);
247 out[2] = min(in[2] >> 7, 255);
258 Combine and scale multiple lightmaps into the 8.8 format in blocklights
261 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
264 int i, j, size, size3;
270 surf->cached_dlight = 0;
271 surf->cached_lighthalf = lighthalf;
272 surf->cached_ambient = r_ambient.value;
274 smax = (surf->extents[0]>>4)+1;
275 tmax = (surf->extents[1]>>4)+1;
278 lightmap = surf->samples;
280 // set to full bright if no light data
281 if (currententity && currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
284 for (i=0 ; i<size ; i++)
294 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
298 for (i = 0;i < size3;i++)
302 memset(&blocklights[0], 0, size*3*sizeof(int));
304 // add all the lightmaps
307 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
309 scale = d_lightstylevalue[surf->styles[maps]];
310 surf->cached_light[maps] = scale; // 8.8 fraction
312 for (i = 0;i < size3;i++)
313 *bl++ += *lightmap++ * scale;
316 if (r_dlightmap.value && surf->dlightframe == r_dlightframecount)
317 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
320 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
323 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
325 void R_UpdateLightmap(msurface_t *s, int lnum)
328 // upload the new lightmap texture fragment
330 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
331 if (nosubimage || nosubimagefragments)
333 if (lightmapupdate[lnum][0] > s->light_t)
334 lightmapupdate[lnum][0] = s->light_t;
335 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
336 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
338 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
340 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
344 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
345 tmax = (s->extents[1]>>4)+1;
348 R_BuildLightMap (s, templight, smax * 4);
350 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
354 R_BuildLightMap (s, templight, smax * 3);
356 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
366 Returns the proper texture for a given time and base texture
369 texture_t *R_TextureAnimation (texture_t *base)
375 if (currententity->frame)
377 if (base->alternate_anims)
378 base = base->alternate_anims;
381 if (!base->anim_total)
386 relative = (int)(cl.time*10) % base->anim_total;
389 while (base->anim_min > relative || base->anim_max <= relative)
391 base = base->anim_next;
394 Con_Printf("R_TextureAnimation: broken cycle");
399 Con_Printf("R_TextureAnimation: infinite cycle");
409 =============================================================
413 =============================================================
417 extern int solidskytexture;
418 extern int alphaskytexture;
419 extern float speedscale; // for top sky and bottom sky
421 extern char skyname[];
425 #include "gl_warp_sin.h"
427 #define TURBSCALE (256.0 / (2 * M_PI))
430 void UploadLightmaps()
433 if (nosubimage || nosubimagefragments)
435 for (i = 0;i < MAX_LIGHTMAPS;i++)
437 if (lightmapupdate[i][0] < lightmapupdate[i][1])
441 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
445 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
447 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
452 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]));
454 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]));
458 lightmapupdate[i][0] = BLOCK_HEIGHT;
459 lightmapupdate[i][1] = 0;
464 float wvert[1024*6]; // used by the following functions
466 void RSurf_DrawSky(msurface_t *s, int transform)
471 for (p=s->polys ; p ; p=p->next)
473 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
475 skypoly[currentskypoly].firstvert = currentskyvert;
476 skypoly[currentskypoly++].verts = p->numverts;
479 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
481 softwaretransform(v, skyvert[currentskyvert].v);
487 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
489 VectorCopy(v, skyvert[currentskyvert].v);
497 int RSurf_Light(int *dlightbits, glpoly_t *polys)
499 float cr, cg, cb, radius, radius2, f, *v, *wv;
500 int i, a, b, lit = false;
505 for (a = 0;a < 8;a++)
507 if ((c = dlightbits[a]))
509 for (b = 0, d = 1;c;b++, d <<= 1)
514 light = &cl_dlights[a * 32 + b];
515 lightorigin = light->origin;
516 cr = light->color[0];
517 cg = light->color[1];
518 cb = light->color[2];
519 radius = light->radius*light->radius;
520 radius2 = radius * 256.0f;
522 for (p = polys;p;p = p->next)
524 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
526 f = VectorDistance2(wv, lightorigin);
529 f = radius2 / (f + LIGHTOFFSET);
545 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
548 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
551 // FIXME: make fog texture if water texture is transparent?
553 if (s->dlightframe != r_dlightframecount)
556 // LordHavoc: fast path for no vertex lighting cases
559 if (r_waterripple.value)
561 for (p=s->polys ; p ; p=p->next)
563 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
564 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
566 softwaretransform(v, temp);
567 transpolyvert(temp[0], temp[1], temp[2] + r_waterripple.value * turbsin[(int)((temp[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((temp[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
574 for (p=s->polys ; p ; p=p->next)
576 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
577 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
579 softwaretransform(v, temp);
580 transpolyvert(temp[0], temp[1], temp[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
588 if (r_waterripple.value)
590 for (p=s->polys ; p ; p=p->next)
592 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
593 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
594 transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
600 for (p=s->polys ; p ; p=p->next)
602 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
603 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
604 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
614 for (p = s->polys;p;p = p->next)
616 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
619 softwaretransform(v, wv);
622 if (r_waterripple.value)
623 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);
624 wv[3] = wv[4] = wv[5] = 128.0f;
628 if (s->dlightframe == r_dlightframecount)
629 RSurf_Light(s->dlightbits, s->polys);
631 for (p=s->polys ; p ; p=p->next)
633 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
634 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
635 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);
641 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
643 int i, lit = false, polys = 0, verts = 0;
648 wallvertcolor_t *outcolor;
649 // check for lightmap modification
651 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_dlightframecount)
652 || r_ambient.value != s->cached_ambient
653 || lighthalf != s->cached_lighthalf
655 && (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
656 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
657 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
658 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3])))
659 R_UpdateLightmap(s, s->lightmaptexturenum);
660 if (r_dlightmap.value || s->dlightframe != r_dlightframecount)
662 // LordHavoc: fast path version for no vertex lighting cases
663 wp = &wallpoly[currentwallpoly];
664 out = &wallvert[currentwallvert];
665 for (p = s->polys;p;p = p->next)
667 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
669 wp->texnum = (unsigned short) R_GetTexture(t->texture);
670 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
671 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
672 wp->firstvert = currentwallvert;
673 wp->numverts = p->numverts;
677 currentwallvert += p->numverts;
681 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
683 softwaretransform(v, out->vert);
693 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
695 VectorCopy(v, out->vert);
702 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
711 for (p = s->polys;p;p = p->next)
713 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
716 softwaretransform(v, wv);
719 wv[3] = wv[4] = wv[5] = 0.0f;
722 verts += p->numverts;
725 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
727 if ((!r_dlightmap.value) && s->dlightframe == r_dlightframecount)
728 lit = RSurf_Light(s->dlightbits, s->polys);
730 wp = &wallpoly[currentwallpoly];
731 out = &wallvert[currentwallvert];
732 outcolor = &wallvertcolor[currentwallvert];
733 currentwallpoly += polys;
734 for (p = s->polys;p;p = p->next)
737 wp->texnum = (unsigned short) R_GetTexture(t->texture);
738 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
739 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
740 wp->firstvert = currentwallvert;
741 wp->numverts = p->numverts;
744 currentwallvert += p->numverts;
745 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
751 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
752 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
753 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
758 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
759 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
760 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
764 out->vert[0] = wv[0];
765 out->vert[1] = wv[1];
766 out->vert[2] = wv[2];
776 // LordHavoc: transparent brush models
777 extern int r_dlightframecount;
778 extern float modelalpha;
780 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
783 float *v, *wv, scale;
786 alpha = (int) (modelalpha * 255.0f);
787 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
789 for (p = s->polys;p;p = p->next)
791 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
794 softwaretransform(v, wv);
797 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
798 if (s->styles[0] != 255)
800 lm = (byte *)((long) s->samples + (int) v[7]);
801 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;
802 if (s->styles[1] != 255)
804 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;
805 if (s->styles[2] != 255)
807 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;
808 if (s->styles[3] != 255)
810 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;
818 if (s->dlightframe == r_dlightframecount)
819 RSurf_Light(s->dlightbits, s->polys);
821 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
823 for (p = s->polys;p;p = p->next)
826 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
827 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
828 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);
834 for (p = s->polys;p;p = p->next)
837 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
838 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
839 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
850 extern qboolean hlbsp;
851 extern char skyname[];
852 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
855 if (s->flags & SURF_DRAWSKY)
858 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
859 RSurf_DrawSky(s, false);
862 t = R_TextureAnimation (s->texinfo->texture);
863 if (s->flags & SURF_DRAWTURB)
865 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
869 RSurf_DrawWallVertex(s, t, false, false);
871 RSurf_DrawWall(s, t, false);
874 void DrawTextureChains (void)
880 for (n = 0;n < cl.worldmodel->numtextures;n++)
882 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
884 cl.worldmodel->textures[n]->texturechain = NULL;
885 // for (;s;s = s->texturechain)
886 // R_DrawSurf(s, false, gl_vertex.value);
887 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
889 if (s->flags & SURF_DRAWSKY)
892 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
893 for (;s;s = s->texturechain)
894 RSurf_DrawSky(s, false);
897 t = R_TextureAnimation (cl.worldmodel->textures[n]);
898 // subdivided water surface warp
899 if (s->flags & SURF_DRAWTURB)
901 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
902 for (;s;s = s->texturechain)
903 RSurf_DrawWater(s, t, false, alpha);
907 for (;s;s = s->texturechain)
908 RSurf_DrawWallVertex(s, t, false, false);
910 for (;s;s = s->texturechain)
911 RSurf_DrawWall(s, t, false);
915 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
922 void R_DrawBrushModel (entity_t *e)
928 int rotated, vertexlit = false;
936 if (e->angles[0] || e->angles[1] || e->angles[2])
939 for (i=0 ; i<3 ; i++)
941 mins[i] = e->origin[i] - clmodel->radius;
942 maxs[i] = e->origin[i] + clmodel->radius;
948 VectorAdd (e->origin, clmodel->mins, mins);
949 VectorAdd (e->origin, clmodel->maxs, maxs);
952 if (R_CullBox (mins, maxs))
957 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
961 vec3_t forward, right, up;
963 VectorCopy (modelorg, temp);
964 AngleVectors (e->angles, forward, right, up);
965 modelorg[0] = DotProduct (temp, forward);
966 modelorg[1] = -DotProduct (temp, right);
967 modelorg[2] = DotProduct (temp, up);
970 s = &clmodel->surfaces[clmodel->firstmodelsurface];
972 // calculate dynamic lighting for bmodel if it's not an
974 for (i = 0;i < MAX_DLIGHTS;i++)
976 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
979 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
980 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
982 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
984 e->angles[0] = -e->angles[0]; // stupid quake bug
985 softwaretransformforentity (e);
986 e->angles[0] = -e->angles[0]; // stupid quake bug
989 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
991 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
993 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
994 if (s->flags & SURF_DRAWSKY)
996 RSurf_DrawSky(s, true);
999 t = R_TextureAnimation (s->texinfo->texture);
1000 if (s->flags & SURF_DRAWTURB)
1002 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
1005 if (vertexlit || s->texinfo->texture->transparent)
1006 RSurf_DrawWallVertex(s, t, true, true);
1008 RSurf_DrawWall(s, t, true);
1015 =============================================================
1019 =============================================================
1022 void R_StoreEfrags (efrag_t **ppefrag);
1024 void R_NewWorldNode ()
1026 int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
1028 msurface_t *surf, **mark, **endmark;
1030 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
1032 if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
1034 if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
1039 // deal with model fragments in this leaf
1041 R_StoreEfrags (&leaf->efrags);
1043 if (leaf->nummarksurfaces)
1045 mark = leaf->firstmarksurface;
1046 endmark = mark + leaf->nummarksurfaces;
1050 // make sure surfaces are only processed once
1051 if (surf->worldnodeframe == r_framecount)
1053 surf->worldnodeframe = r_framecount;
1054 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1056 if ( (surf->flags & SURF_PLANEBACK))
1058 surf->visframe = r_framecount;
1062 surf->texturechain = surf->texinfo->texture->texturechain;
1063 surf->texinfo->texture->texturechain = surf;
1066 R_DrawSurf(surf, false, vertex);
1071 if (!(surf->flags & SURF_PLANEBACK))
1073 surf->visframe = r_framecount;
1077 surf->texturechain = surf->texinfo->texture->texturechain;
1078 surf->texinfo->texture->texturechain = surf;
1081 R_DrawSurf(surf, false, vertex);
1085 while (mark < endmark);
1105 int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
1106 struct nodestack_s *nstack;
1109 msurface_t *surf, *endsurf, **mark, **endmark;
1112 if (!(node = cl.worldmodel->nodes))
1119 if (R_CullBox(node->minmaxs, node->minmaxs+3))
1122 if (nstack <= nodestack)
1125 node = nstack->node;
1126 side = nstack->side;
1127 noclipping = nstack->noclipping;
1134 ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
1135 cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
1136 cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
1137 cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
1138 if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
1139 noclipping = true; // not clipped at all, no need to clip any children of this node
1140 // partially clipped node
1142 // if a leaf node, draw stuff
1143 if (node->contents < 0)
1145 if (node->contents != CONTENTS_SOLID)
1147 pleaf = (mleaf_t *)node;
1150 if (pleaf->nummarksurfaces)
1152 mark = pleaf->firstmarksurface;
1153 endmark = mark + pleaf->nummarksurfaces;
1156 (*mark)->visframe = r_framecount;
1159 while (mark < endmark);
1162 // deal with model fragments in this leaf
1164 R_StoreEfrags (&pleaf->efrags);
1167 if (nstack <= nodestack)
1170 node = nstack->node;
1171 side = nstack->side;
1172 noclipping = nstack->noclipping;
1178 // node is just a decision point, so go down the apropriate sides
1180 // find which side of the node we are on
1181 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
1183 // recurse down the children, front side first
1184 if (node->children[side]->visframe == r_visframecount)
1186 nstack->node = node;
1187 nstack->side = !side; // go down back side when we come back up
1188 nstack->noclipping = noclipping;
1190 node = node->children[side];
1197 if (node->numsurfaces)
1199 surf = cl.worldmodel->surfaces + node->firstsurface;
1200 endsurf = surf + node->numsurfaces;
1208 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1211 surf->texturechain = surf->texinfo->texture->texturechain;
1212 surf->texinfo->texture->texturechain = surf;
1215 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1218 while (surf < endsurf);
1224 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1227 surf->texturechain = surf->texinfo->texture->texturechain;
1228 surf->texinfo->texture->texturechain = surf;
1231 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1234 while (surf < endsurf);
1243 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1246 R_DrawSurf(surf, false, vertex);
1249 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1252 while (surf < endsurf);
1258 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1261 R_DrawSurf(surf, false, vertex);
1264 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1267 while (surf < endsurf);
1272 // recurse down the back side
1273 if (node->children[side]->visframe == r_visframecount)
1275 node = node->children[side];
1279 if (nstack <= nodestack)
1282 node = nstack->node;
1283 side = nstack->side;
1284 noclipping = nstack->noclipping;
1295 void R_DrawWorld (void)
1299 memset (&ent, 0, sizeof(ent));
1300 ent.model = cl.worldmodel;
1301 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1302 modelalpha = ent.alpha = 1;
1305 VectorCopy (r_refdef.vieworg, modelorg);
1307 currententity = &ent;
1309 softwaretransformidentity(); // LordHavoc: clear transform
1313 if (r_newworldnode.value)
1319 R_PushDlights (); // now mark the lit surfaces
1321 DrawTextureChains ();
1330 void R_MarkLeaves (void)
1336 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1340 r_oldviewleaf = r_viewleaf;
1344 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1346 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1349 if (node->visframe == r_visframecount)
1351 node->visframe = r_visframecount;
1352 node = node->parent;
1358 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1360 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1362 if (vis[i>>3] & (1<<(i&7)))
1364 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1367 if (node->visframe == r_visframecount)
1369 node->visframe = r_visframecount;
1370 node = node->parent;
1380 =============================================================================
1384 =============================================================================
1387 // returns a texture number and the position inside it
1388 int AllocBlock (int w, int h, short *x, short *y)
1394 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1396 best = BLOCK_HEIGHT;
1398 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1402 for (j=0 ; j<w ; j++)
1404 if (allocated[texnum][i+j] >= best)
1406 if (allocated[texnum][i+j] > best2)
1407 best2 = allocated[texnum][i+j];
1410 { // this is a valid spot
1416 if (best + h > BLOCK_HEIGHT)
1419 if (nosubimagefragments || nosubimage)
1421 if (!lightmaps[texnum])
1423 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1424 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1427 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1428 else if (!allocated[texnum][0])
1430 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
1431 memset(blank, 0, sizeof(blank));
1434 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1435 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1436 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1438 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1440 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1444 for (i=0 ; i<w ; i++)
1445 allocated[texnum][*x + i] = best + h;
1450 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1455 mvertex_t *r_pcurrentvertbase;
1456 model_t *currentmodel;
1462 BuildSurfaceDisplayList
1465 void BuildSurfaceDisplayList (msurface_t *fa)
1467 int i, j, lindex, lnumverts;
1468 medge_t *pedges, *r_pedge;
1474 // reconstruct the polygon
1475 pedges = currentmodel->edges;
1476 lnumverts = fa->numedges;
1482 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1483 poly->next = fa->polys;
1484 poly->flags = fa->flags;
1486 poly->numverts = lnumverts;
1488 for (i=0 ; i<lnumverts ; i++)
1490 lindex = currentmodel->surfedges[fa->firstedge + i];
1494 r_pedge = &pedges[lindex];
1495 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1499 r_pedge = &pedges[-lindex];
1500 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1502 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1503 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1505 VectorCopy (vec, poly->verts[i]);
1506 poly->verts[i][3] = s / fa->texinfo->texture->width;
1507 poly->verts[i][4] = t / fa->texinfo->texture->height;
1510 // lightmap texture coordinates
1512 s -= fa->texturemins[0];
1513 t -= fa->texturemins[1];
1516 // LordHavoc: calc lightmap data offset
1517 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;
1518 poly->verts[i][7] = j;
1519 s += fa->light_s*16;
1520 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1522 t += fa->light_t*16;
1523 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1525 poly->verts[i][5] = s;
1526 poly->verts[i][6] = t;
1530 // remove co-linear points - Ed
1533 if (!gl_keeptjunctions.value)
1535 for (i = 0 ; i < lnumverts ; ++i)
1538 float *prev, *this, *next;
1540 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1541 this = poly->verts[i];
1542 next = poly->verts[(i + 1) % lnumverts];
1544 VectorSubtract( this, prev, v1 );
1545 VectorNormalize( v1 );
1546 VectorSubtract( next, prev, v2 );
1547 VectorNormalize( v2 );
1549 // skip co-linear points
1550 #define COLINEAR_EPSILON 0.001
1551 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1552 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1553 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1556 for (j = i + 1; j < lnumverts; ++j)
1559 for (k = 0; k < VERTEXSIZE; ++k)
1560 poly->verts[j - 1][k] = poly->verts[j][k];
1564 // retry next vertex next time, which is now current vertex
1570 poly->numverts = lnumverts;
1574 ========================
1575 GL_CreateSurfaceLightmap
1576 ========================
1578 void GL_CreateSurfaceLightmap (msurface_t *surf)
1582 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1585 smax = (surf->extents[0]>>4)+1;
1586 tmax = (surf->extents[1]>>4)+1;
1588 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1589 if (nosubimage || nosubimagefragments)
1591 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1592 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1595 R_BuildLightMap (surf, templight, smax * 4);
1597 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1601 R_BuildLightMap (surf, templight, smax * 3);
1603 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1612 Builds the lightmap texture
1613 with all the surfaces from all brush models
1616 void GL_BuildLightmaps (void)
1621 memset (allocated, 0, sizeof(allocated));
1623 r_framecount = 1; // no dlightcache
1625 if (gl_nosubimagefragments.value)
1626 nosubimagefragments = 1;
1628 nosubimagefragments = 0;
1630 if (gl_nosubimage.value)
1635 if (gl_lightmaprgba.value)
1637 lightmaprgba = true;
1642 lightmaprgba = false;
1646 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1647 // it needs to be aligned on 4 pixel boundaries...
1648 // so I implemented an adjustable lightmap alignment
1649 if (gl_lightmapalign.value < 1)
1650 gl_lightmapalign.value = 1;
1651 if (gl_lightmapalign.value > 16)
1652 gl_lightmapalign.value = 16;
1654 while (lightmapalign < gl_lightmapalign.value)
1655 lightmapalign <<= 1;
1656 gl_lightmapalign.value = lightmapalign;
1657 lightmapalignmask = ~(lightmapalign - 1);
1658 if (nosubimagefragments || nosubimage)
1661 lightmapalignmask = ~0;
1664 if (!lightmap_textures)
1665 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
1667 for (j=1 ; j<MAX_MODELS ; j++)
1669 m = cl.model_precache[j];
1672 if (m->name[0] == '*')
1674 r_pcurrentvertbase = m->vertexes;
1676 for (i=0 ; i<m->numsurfaces ; i++)
1678 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1680 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1682 GL_CreateSurfaceLightmap (m->surfaces + i);
1683 BuildSurfaceDisplayList (m->surfaces + i);
1687 if (nosubimage || nosubimagefragments)
1691 qglSelectTexture(gl_mtex_enum+1);
1692 for (i = 0;i < MAX_LIGHTMAPS;i++)
1694 if (!allocated[i][0])
1696 lightmapupdate[i][0] = BLOCK_HEIGHT;
1697 lightmapupdate[i][1] = 0;
1700 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1701 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1702 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1704 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1706 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1711 qglSelectTexture(gl_mtex_enum+0);