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;
61 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);
85 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
88 int dlightdivtable[32768];
93 int R_AddDynamicLights (msurface_t *surf)
95 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
100 // LordHavoc: use 64bit integer... shame it's not very standardized...
101 #if _MSC_VER || __BORLANDC__
109 if (!dlightdivtable[1])
111 dlightdivtable[0] = 4194304;
112 for (s = 1; s < 32768; s++)
113 dlightdivtable[s] = 4194304 / (s << 7);
116 smax = (surf->extents[0] >> 4) + 1;
117 tmax = (surf->extents[1] >> 4) + 1;
119 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
121 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
122 continue; // not lit by this light
124 VectorSubtract (cl_dlights[lnum].origin, currententity->origin, local);
125 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
127 // for comparisons to minimum acceptable light
128 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
130 // clamp radius to avoid exceeding 32768 entry division table
131 if (maxdist > 4194304)
135 if (dist2 >= maxdist)
138 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
139 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
140 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
142 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
143 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
145 s = bound(0, impacts, smax * 16) - impacts;
146 t = bound(0, impactt, tmax * 16) - impactt;
147 i = s * s + t * t + dist2;
151 // reduce calculations
152 for (s = 0, i = impacts; s < smax; s++, i -= 16)
153 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
155 maxdist3 = maxdist - (int) (dist * dist);
157 // convert to 8.8 blocklights format and scale up by radius
158 red = cl_dlights[lnum].color[0] * maxdist;
159 green = cl_dlights[lnum].color[1] * maxdist;
160 blue = cl_dlights[lnum].color[2] * maxdist;
164 for (t = 0; t < tmax; t++, i -= 16)
167 // make sure some part of it is visible on this line
170 maxdist2 = maxdist - td;
171 for (s = 0; s < smax; s++)
173 if (sdtable[s] < maxdist2)
175 k = dlightdivtable[(sdtable[s] + td) >> 7];
176 bl[0] += (red * k) >> 9;
177 bl[1] += (green * k) >> 9;
178 bl[2] += (blue * k) >> 9;
192 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
195 stride -= (width*lightmapbytes);
198 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
199 // the image is brightened as a processing pass
202 for (i = 0;i < height;i++, out += stride)
204 for (j = 0;j < width;j++, in += 3, out += 4)
206 out[0] = min(in[0] >> 8, 255);
207 out[1] = min(in[1] >> 8, 255);
208 out[2] = min(in[2] >> 8, 255);
215 for (i = 0;i < height;i++, out += stride)
217 for (j = 0;j < width;j++, in += 3, out += 3)
219 out[0] = min(in[0] >> 8, 255);
220 out[1] = min(in[1] >> 8, 255);
221 out[2] = min(in[2] >> 8, 255);
230 for (i = 0;i < height;i++, out += stride)
232 for (j = 0;j < width;j++, in += 3, out += 4)
234 out[0] = min(in[0] >> 7, 255);
235 out[1] = min(in[1] >> 7, 255);
236 out[2] = min(in[2] >> 7, 255);
243 for (i = 0;i < height;i++, out += stride)
245 for (j = 0;j < width;j++, in += 3, out += 3)
247 out[0] = min(in[0] >> 7, 255);
248 out[1] = min(in[1] >> 7, 255);
249 out[2] = min(in[2] >> 7, 255);
260 Combine and scale multiple lightmaps into the 8.8 format in blocklights
263 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
266 int i, j, size, size3;
272 surf->cached_dlight = 0;
273 surf->cached_lighthalf = lighthalf;
274 surf->cached_ambient = r_ambient.value;
276 smax = (surf->extents[0]>>4)+1;
277 tmax = (surf->extents[1]>>4)+1;
280 lightmap = surf->samples;
282 // set to full bright if no light data
283 if ((currententity && (currententity->effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
286 for (i=0 ; i<size ; i++)
296 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
300 for (i = 0;i < size3;i++)
304 memset(&blocklights[0], 0, size*3*sizeof(int));
306 // add all the lightmaps
309 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
311 scale = d_lightstylevalue[surf->styles[maps]];
312 surf->cached_light[maps] = scale; // 8.8 fraction
314 for (i = 0;i < size3;i++)
315 *bl++ += *lightmap++ * scale;
318 if (r_dlightmap.value && surf->dlightframe == r_dlightframecount)
319 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
322 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
325 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
327 void R_UpdateLightmap(msurface_t *s, int lnum)
330 // upload the new lightmap texture fragment
332 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
333 if (nosubimage || nosubimagefragments)
335 if (lightmapupdate[lnum][0] > s->light_t)
336 lightmapupdate[lnum][0] = s->light_t;
337 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
338 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
340 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
342 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
346 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
347 tmax = (s->extents[1]>>4)+1;
350 R_BuildLightMap (s, templight, smax * 4);
352 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
356 R_BuildLightMap (s, templight, smax * 3);
358 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
368 Returns the proper texture for a given time and base texture
371 texture_t *R_TextureAnimation (texture_t *base)
377 if (currententity->frame)
379 if (base->alternate_anims)
380 base = base->alternate_anims;
383 if (!base->anim_total)
388 relative = (int)(cl.time*10) % base->anim_total;
391 while (base->anim_min > relative || base->anim_max <= relative)
393 base = base->anim_next;
396 Con_Printf("R_TextureAnimation: broken cycle");
401 Con_Printf("R_TextureAnimation: infinite cycle");
411 =============================================================
415 =============================================================
419 extern int solidskytexture;
420 extern int alphaskytexture;
421 extern float speedscale; // for top sky and bottom sky
423 extern char skyname[];
427 #include "gl_warp_sin.h"
429 #define TURBSCALE (256.0 / (2 * M_PI))
432 void UploadLightmaps()
435 if (nosubimage || nosubimagefragments)
437 for (i = 0;i < MAX_LIGHTMAPS;i++)
439 if (lightmapupdate[i][0] < lightmapupdate[i][1])
443 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
447 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
449 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
454 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]));
456 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]));
460 lightmapupdate[i][0] = BLOCK_HEIGHT;
461 lightmapupdate[i][1] = 0;
466 float wvert[1024*6]; // used by the following functions
468 void RSurf_DrawSky(msurface_t *s, int transform)
473 for (p=s->polys ; p ; p=p->next)
475 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
477 skypoly[currentskypoly].firstvert = currentskyvert;
478 skypoly[currentskypoly++].verts = p->numverts;
481 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
483 softwaretransform(v, skyvert[currentskyvert].v);
489 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
491 VectorCopy(v, skyvert[currentskyvert].v);
499 int RSurf_Light(int *dlightbits, glpoly_t *polys)
501 float cr, cg, cb, radius, radius2, f, *v, *wv;
502 int i, a, b, lit = false;
507 for (a = 0;a < 8;a++)
509 if ((c = dlightbits[a]))
511 for (b = 0, d = 1;c;b++, d <<= 1)
516 light = &cl_dlights[a * 32 + b];
517 lightorigin = light->origin;
518 cr = light->color[0];
519 cg = light->color[1];
520 cb = light->color[2];
521 radius = light->radius*light->radius;
522 radius2 = radius * 256.0f;
524 for (p = polys;p;p = p->next)
526 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
528 f = VectorDistance2(wv, lightorigin);
531 f = radius2 / (f + LIGHTOFFSET);
547 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
550 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
553 // FIXME: make fog texture if water texture is transparent?
555 if (s->dlightframe != r_dlightframecount)
558 // LordHavoc: fast path for no vertex lighting cases
561 if (r_waterripple.value)
563 for (p=s->polys ; p ; p=p->next)
565 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
566 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
568 softwaretransform(v, temp);
569 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);
576 for (p=s->polys ; p ; p=p->next)
578 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
579 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
581 softwaretransform(v, temp);
582 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);
590 if (r_waterripple.value)
592 for (p=s->polys ; p ; p=p->next)
594 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
595 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
596 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);
602 for (p=s->polys ; p ; p=p->next)
604 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
605 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
606 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);
616 for (p = s->polys;p;p = p->next)
618 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
621 softwaretransform(v, wv);
624 if (r_waterripple.value)
625 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);
626 wv[3] = wv[4] = wv[5] = 128.0f;
630 if (s->dlightframe == r_dlightframecount)
631 RSurf_Light(s->dlightbits, s->polys);
633 for (p=s->polys ; p ; p=p->next)
635 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
636 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
637 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);
643 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
645 int i, lit = false, polys = 0, verts = 0;
650 wallvertcolor_t *outcolor;
651 // check for lightmap modification
653 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_dlightframecount)
654 || r_ambient.value != s->cached_ambient
655 || lighthalf != s->cached_lighthalf
657 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
658 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
659 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
660 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
661 R_UpdateLightmap(s, s->lightmaptexturenum);
662 if (r_dlightmap.value || s->dlightframe != r_dlightframecount)
664 // LordHavoc: fast path version for no vertex lighting cases
665 wp = &wallpoly[currentwallpoly];
666 out = &wallvert[currentwallvert];
667 for (p = s->polys;p;p = p->next)
669 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
671 wp->texnum = (unsigned short) R_GetTexture(t->texture);
672 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
673 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
674 wp->firstvert = currentwallvert;
675 wp->numverts = p->numverts;
679 currentwallvert += p->numverts;
683 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
685 softwaretransform(v, out->vert);
695 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
697 VectorCopy(v, out->vert);
704 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
713 for (p = s->polys;p;p = p->next)
715 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
718 softwaretransform(v, wv);
721 wv[3] = wv[4] = wv[5] = 0.0f;
724 verts += p->numverts;
727 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
729 if ((!r_dlightmap.value) && s->dlightframe == r_dlightframecount)
730 lit = RSurf_Light(s->dlightbits, s->polys);
732 wp = &wallpoly[currentwallpoly];
733 out = &wallvert[currentwallvert];
734 outcolor = &wallvertcolor[currentwallvert];
735 currentwallpoly += polys;
736 for (p = s->polys;p;p = p->next)
739 wp->texnum = (unsigned short) R_GetTexture(t->texture);
740 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
741 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
742 wp->firstvert = currentwallvert;
743 wp->numverts = p->numverts;
746 currentwallvert += p->numverts;
747 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
753 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
754 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
755 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
760 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
761 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
762 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
766 out->vert[0] = wv[0];
767 out->vert[1] = wv[1];
768 out->vert[2] = wv[2];
778 // LordHavoc: transparent brush models
779 extern float modelalpha;
781 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
784 float *v, *wv, scale;
787 alpha = (int) (modelalpha * 255.0f);
788 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
790 for (p = s->polys;p;p = p->next)
792 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
795 softwaretransform(v, wv);
798 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
799 if (s->styles[0] != 255)
801 lm = (byte *)((long) s->samples + (int) v[7]);
802 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;
803 if (s->styles[1] != 255)
805 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;
806 if (s->styles[2] != 255)
808 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;
809 if (s->styles[3] != 255)
811 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;
819 if (s->dlightframe == r_dlightframecount)
820 RSurf_Light(s->dlightbits, s->polys);
822 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
824 for (p = s->polys;p;p = p->next)
827 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
828 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
829 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);
835 for (p = s->polys;p;p = p->next)
838 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
839 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
840 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
851 extern qboolean hlbsp;
852 extern char skyname[];
853 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
856 if (s->flags & SURF_DRAWSKY)
859 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
860 RSurf_DrawSky(s, false);
863 t = R_TextureAnimation (s->texinfo->texture);
864 if (s->flags & SURF_DRAWTURB)
866 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
870 RSurf_DrawWallVertex(s, t, false, false);
872 RSurf_DrawWall(s, t, false);
875 void DrawTextureChains (void)
881 for (n = 0;n < cl.worldmodel->numtextures;n++)
883 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
885 cl.worldmodel->textures[n]->texturechain = NULL;
886 // for (;s;s = s->texturechain)
887 // R_DrawSurf(s, false, gl_vertex.value);
888 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
890 if (s->flags & SURF_DRAWSKY)
893 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
894 for (;s;s = s->texturechain)
895 RSurf_DrawSky(s, false);
898 t = R_TextureAnimation (cl.worldmodel->textures[n]);
899 // subdivided water surface warp
900 if (s->flags & SURF_DRAWTURB)
902 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
903 for (;s;s = s->texturechain)
904 RSurf_DrawWater(s, t, false, alpha);
908 for (;s;s = s->texturechain)
909 RSurf_DrawWallVertex(s, t, false, false);
911 for (;s;s = s->texturechain)
912 RSurf_DrawWall(s, t, false);
916 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
923 void R_DrawBrushModel (entity_t *e)
929 int rotated, vertexlit = false;
937 if (e->angles[0] || e->angles[1] || e->angles[2])
940 for (i=0 ; i<3 ; i++)
942 mins[i] = e->origin[i] - clmodel->radius;
943 maxs[i] = e->origin[i] + clmodel->radius;
949 VectorAdd (e->origin, clmodel->mins, mins);
950 VectorAdd (e->origin, clmodel->maxs, maxs);
953 if (R_CullBox (mins, maxs))
958 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
962 vec3_t forward, right, up;
964 VectorCopy (modelorg, temp);
965 AngleVectors (e->angles, forward, right, up);
966 modelorg[0] = DotProduct (temp, forward);
967 modelorg[1] = -DotProduct (temp, right);
968 modelorg[2] = DotProduct (temp, up);
971 s = &clmodel->surfaces[clmodel->firstmodelsurface];
973 // calculate dynamic lighting for bmodel if it's not an
975 for (i = 0;i < MAX_DLIGHTS;i++)
977 if (!cl_dlights[i].radius)
980 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
981 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
983 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
985 e->angles[0] = -e->angles[0]; // stupid quake bug
986 softwaretransformforentity (e);
987 e->angles[0] = -e->angles[0]; // stupid quake bug
990 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
992 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
994 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
995 if (s->flags & SURF_DRAWSKY)
997 RSurf_DrawSky(s, true);
1000 t = R_TextureAnimation (s->texinfo->texture);
1001 if (s->flags & SURF_DRAWTURB)
1003 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
1006 if (vertexlit || s->texinfo->texture->transparent)
1007 RSurf_DrawWallVertex(s, t, true, true);
1009 RSurf_DrawWall(s, t, true);
1016 =============================================================
1020 =============================================================
1023 void R_StoreEfrags (efrag_t **ppefrag);
1025 void R_NewWorldNode ()
1027 int l, texsort = gl_texsort.value, vertex = gl_vertex.value;
1029 msurface_t *surf, **mark, **endmark;
1031 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
1033 if ((leaf->visframe == r_visframecount) && (leaf->efrags || leaf->nummarksurfaces))
1035 if (R_CullBox(leaf->minmaxs, leaf->minmaxs+3))
1040 // deal with model fragments in this leaf
1042 R_StoreEfrags (&leaf->efrags);
1044 if (leaf->nummarksurfaces)
1046 mark = leaf->firstmarksurface;
1047 endmark = mark + leaf->nummarksurfaces;
1051 // make sure surfaces are only processed once
1052 if (surf->worldnodeframe == r_framecount)
1054 surf->worldnodeframe = r_framecount;
1055 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1057 if ( (surf->flags & SURF_PLANEBACK))
1059 surf->visframe = r_framecount;
1063 surf->texturechain = surf->texinfo->texture->texturechain;
1064 surf->texinfo->texture->texturechain = surf;
1067 R_DrawSurf(surf, false, vertex);
1072 if (!(surf->flags & SURF_PLANEBACK))
1074 surf->visframe = r_framecount;
1078 surf->texturechain = surf->texinfo->texture->texturechain;
1079 surf->texinfo->texture->texturechain = surf;
1082 R_DrawSurf(surf, false, vertex);
1086 while (mark < endmark);
1106 int side, texsort = gl_texsort.value, vertex = gl_vertex.value, ca, cb, cc, cd, noclipping = false, oldclip = r_oldclip.value;
1107 struct nodestack_s *nstack;
1110 msurface_t *surf, *endsurf, **mark, **endmark;
1113 if (!(node = cl.worldmodel->nodes))
1120 if (R_CullBox(node->minmaxs, node->minmaxs+3))
1123 if (nstack <= nodestack)
1126 node = nstack->node;
1127 side = nstack->side;
1128 noclipping = nstack->noclipping;
1135 ca = frustum[0].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[0]);if (ca == 2) goto backupstack; // completely clipped away
1136 cb = frustum[1].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[1]);if (cb == 2) goto backupstack; // completely clipped away
1137 cc = frustum[2].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[2]);if (cc == 2) goto backupstack; // completely clipped away
1138 cd = frustum[3].BoxOnPlaneSideFunc(node->minmaxs, node->minmaxs+3, &frustum[3]);if (cd == 2) goto backupstack; // completely clipped away
1139 if (ca == 0 && cb == 0 && cc == 0 && cd == 0)
1140 noclipping = true; // not clipped at all, no need to clip any children of this node
1141 // partially clipped node
1143 // if a leaf node, draw stuff
1144 if (node->contents < 0)
1146 if (node->contents != CONTENTS_SOLID)
1148 pleaf = (mleaf_t *)node;
1151 if (pleaf->nummarksurfaces)
1153 mark = pleaf->firstmarksurface;
1154 endmark = mark + pleaf->nummarksurfaces;
1157 (*mark)->visframe = r_framecount;
1160 while (mark < endmark);
1163 // deal with model fragments in this leaf
1165 R_StoreEfrags (&pleaf->efrags);
1168 if (nstack <= nodestack)
1171 node = nstack->node;
1172 side = nstack->side;
1173 noclipping = nstack->noclipping;
1179 // node is just a decision point, so go down the apropriate sides
1181 // find which side of the node we are on
1182 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
1184 // recurse down the children, front side first
1185 if (node->children[side]->visframe == r_visframecount)
1187 nstack->node = node;
1188 nstack->side = !side; // go down back side when we come back up
1189 nstack->noclipping = noclipping;
1191 node = node->children[side];
1198 if (node->numsurfaces)
1200 surf = cl.worldmodel->surfaces + node->firstsurface;
1201 endsurf = surf + node->numsurfaces;
1209 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1212 surf->texturechain = surf->texinfo->texture->texturechain;
1213 surf->texinfo->texture->texturechain = surf;
1216 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1219 while (surf < endsurf);
1225 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1228 surf->texturechain = surf->texinfo->texture->texturechain;
1229 surf->texinfo->texture->texturechain = surf;
1232 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1235 while (surf < endsurf);
1244 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
1247 R_DrawSurf(surf, false, vertex);
1250 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1253 while (surf < endsurf);
1259 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
1262 R_DrawSurf(surf, false, vertex);
1265 surf->visframe = -1; // LordHavoc: mark as not visible, so lighting will not touch it
1268 while (surf < endsurf);
1273 // recurse down the back side
1274 if (node->children[side]->visframe == r_visframecount)
1276 node = node->children[side];
1280 if (nstack <= nodestack)
1283 node = nstack->node;
1284 side = nstack->side;
1285 noclipping = nstack->noclipping;
1296 void R_DrawWorld (void)
1300 memset (&ent, 0, sizeof(ent));
1301 ent.model = cl.worldmodel;
1302 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
1303 modelalpha = ent.alpha = 1;
1306 VectorCopy (r_refdef.vieworg, modelorg);
1308 currententity = &ent;
1310 softwaretransformidentity(); // LordHavoc: clear transform
1314 if (r_newworldnode.value)
1320 R_PushDlights (); // now mark the lit surfaces
1322 DrawTextureChains ();
1331 void R_MarkLeaves (void)
1337 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1341 r_oldviewleaf = r_viewleaf;
1345 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1347 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1350 if (node->visframe == r_visframecount)
1352 node->visframe = r_visframecount;
1353 node = node->parent;
1359 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1361 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1363 if (vis[i>>3] & (1<<(i&7)))
1365 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1368 if (node->visframe == r_visframecount)
1370 node->visframe = r_visframecount;
1371 node = node->parent;
1381 =============================================================================
1385 =============================================================================
1388 // returns a texture number and the position inside it
1389 int AllocBlock (int w, int h, short *x, short *y)
1395 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1397 best = BLOCK_HEIGHT;
1399 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1403 for (j=0 ; j<w ; j++)
1405 if (allocated[texnum][i+j] >= best)
1407 if (allocated[texnum][i+j] > best2)
1408 best2 = allocated[texnum][i+j];
1411 { // this is a valid spot
1417 if (best + h > BLOCK_HEIGHT)
1420 if (nosubimagefragments || nosubimage)
1422 if (!lightmaps[texnum])
1424 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1425 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1428 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1429 else if (!allocated[texnum][0])
1431 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
1432 memset(blank, 0, sizeof(blank));
1435 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1436 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1437 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1439 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1441 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1445 for (i=0 ; i<w ; i++)
1446 allocated[texnum][*x + i] = best + h;
1451 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1456 mvertex_t *r_pcurrentvertbase;
1457 model_t *currentmodel;
1463 BuildSurfaceDisplayList
1466 void BuildSurfaceDisplayList (msurface_t *fa)
1468 int i, j, lindex, lnumverts;
1469 medge_t *pedges, *r_pedge;
1475 // reconstruct the polygon
1476 pedges = currentmodel->edges;
1477 lnumverts = fa->numedges;
1483 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1484 poly->next = fa->polys;
1485 poly->flags = fa->flags;
1487 poly->numverts = lnumverts;
1489 for (i=0 ; i<lnumverts ; i++)
1491 lindex = currentmodel->surfedges[fa->firstedge + i];
1495 r_pedge = &pedges[lindex];
1496 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1500 r_pedge = &pedges[-lindex];
1501 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1503 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1504 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1506 VectorCopy (vec, poly->verts[i]);
1507 poly->verts[i][3] = s / fa->texinfo->texture->width;
1508 poly->verts[i][4] = t / fa->texinfo->texture->height;
1511 // lightmap texture coordinates
1513 s -= fa->texturemins[0];
1514 t -= fa->texturemins[1];
1517 // LordHavoc: calc lightmap data offset
1518 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;
1519 poly->verts[i][7] = j;
1520 s += fa->light_s*16;
1521 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1523 t += fa->light_t*16;
1524 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1526 poly->verts[i][5] = s;
1527 poly->verts[i][6] = t;
1531 // remove co-linear points - Ed
1534 if (!gl_keeptjunctions.value)
1536 for (i = 0 ; i < lnumverts ; ++i)
1539 float *prev, *this, *next;
1541 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1542 this = poly->verts[i];
1543 next = poly->verts[(i + 1) % lnumverts];
1545 VectorSubtract( this, prev, v1 );
1546 VectorNormalize( v1 );
1547 VectorSubtract( next, prev, v2 );
1548 VectorNormalize( v2 );
1550 // skip co-linear points
1551 #define COLINEAR_EPSILON 0.001
1552 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1553 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1554 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1557 for (j = i + 1; j < lnumverts; ++j)
1560 for (k = 0; k < VERTEXSIZE; ++k)
1561 poly->verts[j - 1][k] = poly->verts[j][k];
1565 // retry next vertex next time, which is now current vertex
1571 poly->numverts = lnumverts;
1575 ========================
1576 GL_CreateSurfaceLightmap
1577 ========================
1579 void GL_CreateSurfaceLightmap (msurface_t *surf)
1583 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1586 smax = (surf->extents[0]>>4)+1;
1587 tmax = (surf->extents[1]>>4)+1;
1589 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1590 if (nosubimage || nosubimagefragments)
1592 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1593 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1596 R_BuildLightMap (surf, templight, smax * 4);
1598 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1602 R_BuildLightMap (surf, templight, smax * 3);
1604 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1613 Builds the lightmap texture
1614 with all the surfaces from all brush models
1617 void GL_BuildLightmaps (void)
1622 memset (allocated, 0, sizeof(allocated));
1624 r_framecount = 1; // no dlightcache
1626 if (gl_nosubimagefragments.value)
1627 nosubimagefragments = 1;
1629 nosubimagefragments = 0;
1631 if (gl_nosubimage.value)
1636 if (gl_lightmaprgba.value)
1638 lightmaprgba = true;
1643 lightmaprgba = false;
1647 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1648 // it needs to be aligned on 4 pixel boundaries...
1649 // so I implemented an adjustable lightmap alignment
1650 if (gl_lightmapalign.value < 1)
1651 gl_lightmapalign.value = 1;
1652 if (gl_lightmapalign.value > 16)
1653 gl_lightmapalign.value = 16;
1655 while (lightmapalign < gl_lightmapalign.value)
1656 lightmapalign <<= 1;
1657 gl_lightmapalign.value = lightmapalign;
1658 lightmapalignmask = ~(lightmapalign - 1);
1659 if (nosubimagefragments || nosubimage)
1662 lightmapalignmask = ~0;
1665 if (!lightmap_textures)
1666 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
1668 for (j=1 ; j<MAX_MODELS ; j++)
1670 m = cl.model_precache[j];
1673 if (m->name[0] == '*')
1675 r_pcurrentvertbase = m->vertexes;
1677 for (i=0 ; i<m->numsurfaces ; i++)
1679 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1681 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1683 GL_CreateSurfaceLightmap (m->surfaces + i);
1684 BuildSurfaceDisplayList (m->surfaces + i);
1688 if (nosubimage || nosubimagefragments)
1692 qglSelectTexture(gl_mtex_enum+1);
1693 for (i = 0;i < MAX_LIGHTMAPS;i++)
1695 if (!allocated[i][0])
1697 lightmapupdate[i][0] = BLOCK_HEIGHT;
1698 lightmapupdate[i][1] = 0;
1701 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1702 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1703 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1705 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1707 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1712 qglSelectTexture(gl_mtex_enum+0);