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 r_dlightmap = {"r_dlightmap", "1"};
50 cvar_t r_drawportals = {"r_drawportals", "0"};
52 qboolean lightmaprgba, nosubimagefragments, nosubimage;
57 void gl_surf_start(void)
61 void gl_surf_shutdown(void)
65 void gl_surf_newmap(void)
69 void GL_Surf_Init(void)
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(&r_dlightmap);
81 Cvar_RegisterVariable(&r_drawportals);
83 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
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->render.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->render.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_framecount)
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)
371 // texture_t *original;
375 if (currententity->render.frame)
377 if (base->alternate_anims)
378 base = base->alternate_anims;
381 if (!base->anim_total)
384 return base->anim_frames[(int)(cl.time*5) % base->anim_total];
389 relative = (int)(cl.time*5) % base->anim_total;
392 while (base->anim_min > relative || base->anim_max <= relative)
394 base = base->anim_next;
397 Con_Printf("R_TextureAnimation: broken cycle");
402 Con_Printf("R_TextureAnimation: infinite cycle");
413 =============================================================
417 =============================================================
423 #include "gl_warp_sin.h"
425 #define TURBSCALE (256.0 / (2 * M_PI))
428 void UploadLightmaps(void)
431 if (nosubimage || nosubimagefragments)
433 for (i = 0;i < MAX_LIGHTMAPS;i++)
435 if (lightmapupdate[i][0] < lightmapupdate[i][1])
439 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
443 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
445 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
450 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]));
452 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]));
456 lightmapupdate[i][0] = BLOCK_HEIGHT;
457 lightmapupdate[i][1] = 0;
462 float wvert[1024*6]; // used by the following functions
464 void RSurf_DrawSky(msurface_t *s, int transform)
470 // LordHavoc: HalfLife maps have freaky skypolys...
474 for (p=s->polys ; p ; p=p->next)
476 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
478 skypoly[currentskypoly].firstvert = currentskyvert;
479 skypoly[currentskypoly++].verts = p->numverts;
482 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
484 softwaretransform(v, skyvert[currentskyvert].v);
490 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
492 VectorCopy(v, skyvert[currentskyvert].v);
500 int RSurf_Light(int *dlightbits, glpoly_t *polys)
502 float cr, cg, cb, radius, radius2, f, *v, *wv;
503 int i, a, b, lit = false;
508 for (a = 0;a < 8;a++)
510 if ((c = dlightbits[a]))
512 for (b = 0, d = 1;c;b++, d <<= 1)
517 light = &cl_dlights[a * 32 + b];
518 lightorigin = light->origin;
519 cr = light->color[0];
520 cg = light->color[1];
521 cb = light->color[2];
522 radius = light->radius*light->radius;
523 radius2 = radius * 256.0f;
525 for (p = polys;p;p = p->next)
527 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
529 f = VectorDistance2(wv, lightorigin);
532 f = radius2 / (f + LIGHTOFFSET);
548 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
551 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
554 // FIXME: make fog texture if water texture is transparent?
556 if (s->dlightframe != r_framecount)
559 // LordHavoc: fast path for no vertex lighting cases
562 if (r_waterripple.value)
564 for (p=s->polys ; p ; p=p->next)
566 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
567 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
569 softwaretransform(v, temp);
570 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)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
577 for (p=s->polys ; p ; p=p->next)
579 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
580 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
582 softwaretransform(v, temp);
583 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);
591 if (r_waterripple.value)
593 for (p=s->polys ; p ; p=p->next)
595 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
596 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
597 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)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
603 for (p=s->polys ; p ; p=p->next)
605 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
606 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
607 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);
617 for (p = s->polys;p;p = p->next)
619 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
622 softwaretransform(v, wv);
625 if (r_waterripple.value)
626 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)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f);
627 wv[3] = wv[4] = wv[5] = 128.0f;
631 if (s->dlightframe == r_framecount)
632 RSurf_Light(s->dlightbits, s->polys);
634 for (p=s->polys ; p ; p=p->next)
636 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
637 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
638 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);
644 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
646 int i, lit = false, polys = 0, verts = 0;
651 wallvertcolor_t *outcolor;
652 // check for lightmap modification
654 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
655 || r_ambient.value != s->cached_ambient
656 || lighthalf != s->cached_lighthalf
658 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
659 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
660 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
661 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
662 R_UpdateLightmap(s, s->lightmaptexturenum);
663 if (r_dlightmap.value || s->dlightframe != r_framecount)
665 // LordHavoc: fast path version for no vertex lighting cases
666 wp = &wallpoly[currentwallpoly];
667 out = &wallvert[currentwallvert];
668 for (p = s->polys;p;p = p->next)
670 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
672 wp->texnum = (unsigned short) R_GetTexture(t->texture);
673 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
674 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
675 wp->firstvert = currentwallvert;
676 wp->numverts = p->numverts;
680 currentwallvert += p->numverts;
684 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
686 softwaretransform(v, out->vert);
696 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
698 VectorCopy(v, out->vert);
705 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
714 for (p = s->polys;p;p = p->next)
716 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
719 softwaretransform(v, wv);
722 wv[3] = wv[4] = wv[5] = 0.0f;
725 verts += p->numverts;
728 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
730 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
731 lit = RSurf_Light(s->dlightbits, s->polys);
733 wp = &wallpoly[currentwallpoly];
734 out = &wallvert[currentwallvert];
735 outcolor = &wallvertcolor[currentwallvert];
736 currentwallpoly += polys;
737 for (p = s->polys;p;p = p->next)
740 wp->texnum = (unsigned short) R_GetTexture(t->texture);
741 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
742 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
743 wp->firstvert = currentwallvert;
744 wp->numverts = p->numverts;
747 currentwallvert += p->numverts;
748 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
754 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
755 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
756 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
761 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
762 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
763 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
767 out->vert[0] = wv[0];
768 out->vert[1] = wv[1];
769 out->vert[2] = wv[2];
779 // LordHavoc: transparent brush models
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_framecount)
819 RSurf_Light(s->dlightbits, s->polys);
821 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
823 for (p = s->polys;p;p = p->next)
826 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.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->render.colormod[0], wv[4] * currententity->render.colormod[1], wv[5] * currententity->render.colormod[2], alpha);
834 for (p = s->polys;p;p = p->next)
837 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.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);
845 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
852 void R_DrawBrushModel (entity_t *e)
858 int rotated, vertexlit = false;
863 clmodel = e->render.model;
865 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
868 for (i=0 ; i<3 ; i++)
870 mins[i] = e->render.origin[i] - clmodel->radius;
871 maxs[i] = e->render.origin[i] + clmodel->radius;
877 VectorAdd (e->render.origin, clmodel->mins, mins);
878 VectorAdd (e->render.origin, clmodel->maxs, maxs);
881 if (R_VisibleCullBox (mins, maxs))
886 VectorSubtract (r_origin, e->render.origin, modelorg);
890 vec3_t forward, right, up;
892 VectorCopy (modelorg, temp);
893 AngleVectors (e->render.angles, forward, right, up);
894 modelorg[0] = DotProduct (temp, forward);
895 modelorg[1] = -DotProduct (temp, right);
896 modelorg[2] = DotProduct (temp, up);
899 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
901 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
902 s->visframe = r_framecount;
907 // calculate dynamic lighting for bmodel if it's not an
909 for (i = 0;i < MAX_DLIGHTS;i++)
911 if (!cl_dlights[i].radius)
914 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
915 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
917 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->render.effects & EF_FULLBRIGHT) || currententity->render.colormod[0] != 1 || currententity->render.colormod[2] != 1 || currententity->render.colormod[2] != 1;
919 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
920 softwaretransformforentity (e);
921 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
924 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
926 if (s->visframe == r_framecount)
928 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
929 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
931 // sky and liquid don't need sorting (skypoly/transpoly)
932 if (s->flags & SURF_DRAWSKY)
933 RSurf_DrawSky(s, true);
935 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
939 texture_t *t = R_TextureAnimation(s->texinfo->texture);
940 if (vertexlit || s->texinfo->texture->transparent)
941 RSurf_DrawWallVertex(s, t, true, true);
943 RSurf_DrawWall(s, t, true);
951 =============================================================
955 =============================================================
958 static byte *worldvis;
960 void R_MarkLeaves (void)
962 static float noviscache;
963 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
966 r_oldviewleaf = r_viewleaf;
967 noviscache = r_novis.value;
969 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
972 void R_SolidWorldNode (void)
976 msurface_t *surf, **mark, **endmark;
978 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
980 if (leaf->nummarksurfaces)
982 if (R_CullBox(leaf->mins, leaf->maxs))
987 leaf->visframe = r_framecount;
989 if (leaf->nummarksurfaces)
991 mark = leaf->firstmarksurface;
992 endmark = mark + leaf->nummarksurfaces;
996 // make sure surfaces are only processed once
997 if (surf->worldnodeframe == r_framecount)
999 surf->worldnodeframe = r_framecount;
1000 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1002 if (surf->flags & SURF_PLANEBACK)
1003 surf->visframe = r_framecount;
1007 if (!(surf->flags & SURF_PLANEBACK))
1008 surf->visframe = r_framecount;
1011 while (mark < endmark);
1018 // experimental and inferior to the other in recursion depth allowances
1019 void R_PortalWorldNode (void)
1023 msurface_t *surf, **mark, **endmark;
1024 mleaf_t *leaf, *llistbuffer[8192], **l, **llist;
1027 leaf->worldnodeframe = r_framecount;
1028 l = llist = &llistbuffer[0];
1029 *llist++ = r_viewleaf;
1036 leaf->visframe = r_framecount;
1038 if (leaf->nummarksurfaces)
1040 mark = leaf->firstmarksurface;
1041 endmark = mark + leaf->nummarksurfaces;
1045 // make sure surfaces are only processed once
1046 if (surf->worldnodeframe == r_framecount)
1048 surf->worldnodeframe = r_framecount;
1049 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1051 if (surf->flags & SURF_PLANEBACK)
1052 surf->visframe = r_framecount;
1056 if (!(surf->flags & SURF_PLANEBACK))
1057 surf->visframe = r_framecount;
1060 while (mark < endmark);
1063 // follow portals into other leafs
1065 for (;p;p = p->next)
1068 if (leaf->worldnodeframe != r_framecount)
1070 leaf->worldnodeframe = r_framecount;
1071 i = (leaf - cl.worldmodel->leafs) - 1;
1072 if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1080 p = r_viewleaf->portals;
1081 for (;p;p = p->next)
1084 if (p->past->worldnodeframe != r_framecount)
1088 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1092 void R_PortalWorldNode (void)
1095 mportal_t *p, *pstack[8192];
1096 msurface_t *surf, **mark, **endmark;
1100 leaf->worldnodeframe = r_framecount;
1105 leaf->visframe = r_framecount;
1107 if (leaf->nummarksurfaces)
1109 mark = leaf->firstmarksurface;
1110 endmark = mark + leaf->nummarksurfaces;
1114 // make sure surfaces are only processed once
1115 if (surf->worldnodeframe == r_framecount)
1117 surf->worldnodeframe = r_framecount;
1118 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1120 if (surf->flags & SURF_PLANEBACK)
1121 surf->visframe = r_framecount;
1125 if (!(surf->flags & SURF_PLANEBACK))
1126 surf->visframe = r_framecount;
1129 while (mark < endmark);
1132 // follow portals into other leafs
1134 for (;p;p = p->next)
1137 if (leaf->worldnodeframe != r_framecount)
1139 leaf->worldnodeframe = r_framecount;
1140 if (leaf->contents != CONTENTS_SOLID)
1142 i = (leaf - cl.worldmodel->leafs) - 1;
1143 if (worldvis[i>>3] & (1<<(i&7)))
1145 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1147 pstack[portalstack++] = p;
1151 p = pstack[--portalstack];
1163 p = r_viewleaf->portals;
1164 for (;p;p = p->next)
1167 if (p->past->worldnodeframe != r_framecount)
1171 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
1174 void R_DrawSurfaces (void)
1176 msurface_t *surf, *endsurf;
1178 int vertex = gl_vertex.value;
1180 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1181 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1182 for (;surf < endsurf;surf++)
1184 if (surf->visframe == r_framecount)
1187 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1189 // sky and liquid don't need sorting (skypoly/transpoly)
1190 if (surf->flags & SURF_DRAWSKY)
1191 RSurf_DrawSky(surf, false);
1193 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1197 t = R_TextureAnimation(surf->texinfo->texture);
1199 RSurf_DrawWallVertex(surf, t, false, false);
1201 RSurf_DrawWall(surf, t, false);
1207 void R_DrawPortals(void)
1209 int drawportals, i, r, g, b;
1210 mleaf_t *leaf, *endleaf;
1212 mvertex_t *point, *endpoint;
1213 drawportals = (int)r_drawportals.value;
1214 if (drawportals < 1)
1216 leaf = cl.worldmodel->leafs;
1217 endleaf = leaf + cl.worldmodel->numleafs;
1218 for (;leaf < endleaf;leaf++)
1220 if (leaf->visframe == r_framecount && leaf->portals)
1222 i = leaf - cl.worldmodel->leafs;
1223 r = (i & 0x0007) << 5;
1224 g = (i & 0x0038) << 2;
1225 b = (i & 0x01C0) >> 1;
1226 portal = leaf->portals;
1229 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1230 point = portal->points + portal->numpoints - 1;
1231 endpoint = portal->points;
1232 for (;point >= endpoint;point--)
1233 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1235 portal = portal->next;
1246 void R_DrawWorld (void)
1250 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1252 memset (&ent, 0, sizeof(ent));
1253 ent.render.model = cl.worldmodel;
1254 ent.render.colormod[0] = ent.render.colormod[1] = ent.render.colormod[2] = 1;
1255 modelalpha = ent.render.alpha = 1;
1256 ent.render.scale = 1;
1258 VectorCopy (r_origin, modelorg);
1260 currententity = &ent;
1262 softwaretransformidentity(); // LordHavoc: clear transform
1266 if (r_viewleaf->contents == CONTENTS_SOLID)
1267 R_SolidWorldNode ();
1271 R_PortalWorldNode ();
1275 R_PushDlights (); // now mark the lit surfaces
1283 =============================================================================
1287 =============================================================================
1290 // returns a texture number and the position inside it
1291 int AllocBlock (int w, int h, short *x, short *y)
1297 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
1299 best = BLOCK_HEIGHT;
1301 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1305 for (j=0 ; j<w ; j++)
1307 if (allocated[texnum][i+j] >= best)
1309 if (allocated[texnum][i+j] > best2)
1310 best2 = allocated[texnum][i+j];
1313 { // this is a valid spot
1319 if (best + h > BLOCK_HEIGHT)
1322 if (nosubimagefragments || nosubimage)
1324 if (!lightmaps[texnum])
1326 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1327 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1330 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1331 else if (!allocated[texnum][0])
1333 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
1334 memset(blank, 0, sizeof(blank));
1337 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1338 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1339 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1341 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1343 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1347 for (i = 0;i < w;i++)
1348 allocated[texnum][*x + i] = best + h;
1353 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1358 mvertex_t *r_pcurrentvertbase;
1359 model_t *currentmodel;
1365 BuildSurfaceDisplayList
1368 void BuildSurfaceDisplayList (msurface_t *fa)
1370 int i, j, lindex, lnumverts;
1371 medge_t *pedges, *r_pedge;
1377 // reconstruct the polygon
1378 pedges = currentmodel->edges;
1379 lnumverts = fa->numedges;
1385 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1386 poly->next = fa->polys;
1387 poly->flags = fa->flags;
1389 poly->numverts = lnumverts;
1391 for (i=0 ; i<lnumverts ; i++)
1393 lindex = currentmodel->surfedges[fa->firstedge + i];
1397 r_pedge = &pedges[lindex];
1398 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1402 r_pedge = &pedges[-lindex];
1403 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1405 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1406 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1408 VectorCopy (vec, poly->verts[i]);
1409 poly->verts[i][3] = s / fa->texinfo->texture->width;
1410 poly->verts[i][4] = t / fa->texinfo->texture->height;
1413 // lightmap texture coordinates
1415 s -= fa->texturemins[0];
1416 t -= fa->texturemins[1];
1419 // LordHavoc: calc lightmap data offset
1420 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;
1421 poly->verts[i][7] = j;
1422 s += fa->light_s*16;
1423 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1425 t += fa->light_t*16;
1426 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1428 poly->verts[i][5] = s;
1429 poly->verts[i][6] = t;
1433 // remove co-linear points - Ed
1436 if (!gl_keeptjunctions.value)
1438 for (i = 0 ; i < lnumverts ; ++i)
1441 float *prev, *this, *next;
1443 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1444 this = poly->verts[i];
1445 next = poly->verts[(i + 1) % lnumverts];
1447 VectorSubtract( this, prev, v1 );
1448 VectorNormalize( v1 );
1449 VectorSubtract( next, prev, v2 );
1450 VectorNormalize( v2 );
1452 // skip co-linear points
1453 #define COLINEAR_EPSILON 0.001
1454 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1455 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1456 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1459 for (j = i + 1; j < lnumverts; ++j)
1462 for (k = 0; k < VERTEXSIZE; ++k)
1463 poly->verts[j - 1][k] = poly->verts[j][k];
1467 // retry next vertex next time, which is now current vertex
1473 poly->numverts = lnumverts;
1477 ========================
1478 GL_CreateSurfaceLightmap
1479 ========================
1481 void GL_CreateSurfaceLightmap (msurface_t *surf)
1485 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1488 smax = (surf->extents[0]>>4)+1;
1489 tmax = (surf->extents[1]>>4)+1;
1491 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1492 if (nosubimage || nosubimagefragments)
1494 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1495 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1498 R_BuildLightMap (surf, templight, smax * 4);
1500 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1504 R_BuildLightMap (surf, templight, smax * 3);
1506 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1515 Builds the lightmap texture
1516 with all the surfaces from all brush models
1519 void GL_BuildLightmaps (void)
1524 memset (allocated, 0, sizeof(allocated));
1526 r_framecount = 1; // no dlightcache
1528 if (gl_nosubimagefragments.value)
1529 nosubimagefragments = 1;
1531 nosubimagefragments = 0;
1533 if (gl_nosubimage.value)
1538 if (gl_lightmaprgba.value)
1540 lightmaprgba = true;
1545 lightmaprgba = false;
1549 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1550 // it needs to be aligned on 4 pixel boundaries...
1551 // so I implemented an adjustable lightmap alignment
1552 if (gl_lightmapalign.value < 1)
1553 gl_lightmapalign.value = 1;
1554 if (gl_lightmapalign.value > 16)
1555 gl_lightmapalign.value = 16;
1557 while (lightmapalign < gl_lightmapalign.value)
1558 lightmapalign <<= 1;
1559 gl_lightmapalign.value = lightmapalign;
1560 lightmapalignmask = ~(lightmapalign - 1);
1561 if (nosubimagefragments || nosubimage)
1564 lightmapalignmask = ~0;
1567 if (!lightmap_textures)
1568 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
1570 for (j=1 ; j<MAX_MODELS ; j++)
1572 m = cl.model_precache[j];
1575 if (m->name[0] == '*')
1577 r_pcurrentvertbase = m->vertexes;
1579 for (i=0 ; i<m->numsurfaces ; i++)
1581 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1583 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1585 GL_CreateSurfaceLightmap (m->surfaces + i);
1586 BuildSurfaceDisplayList (m->surfaces + i);
1590 if (nosubimage || nosubimagefragments)
1594 qglSelectTexture(gl_mtex_enum+1);
1595 for (i = 0;i < MAX_LIGHTMAPS;i++)
1597 if (!allocated[i][0])
1599 lightmapupdate[i][0] = BLOCK_HEIGHT;
1600 lightmapupdate[i][1] = 0;
1603 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1604 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1605 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1607 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1609 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1614 qglSelectTexture(gl_mtex_enum+0);