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;
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(&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 =============================================================
421 extern int solidskytexture;
422 extern int alphaskytexture;
423 extern float speedscale; // for top sky and bottom sky
425 extern char skyname[];
429 #include "gl_warp_sin.h"
431 #define TURBSCALE (256.0 / (2 * M_PI))
434 void UploadLightmaps()
437 if (nosubimage || nosubimagefragments)
439 for (i = 0;i < MAX_LIGHTMAPS;i++)
441 if (lightmapupdate[i][0] < lightmapupdate[i][1])
445 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
449 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
451 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
456 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]));
458 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]));
462 lightmapupdate[i][0] = BLOCK_HEIGHT;
463 lightmapupdate[i][1] = 0;
468 float wvert[1024*6]; // used by the following functions
470 void RSurf_DrawSky(msurface_t *s, int transform)
476 // LordHavoc: HalfLife maps have freaky skypolys...
480 for (p=s->polys ; p ; p=p->next)
482 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
484 skypoly[currentskypoly].firstvert = currentskyvert;
485 skypoly[currentskypoly++].verts = p->numverts;
488 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
490 softwaretransform(v, skyvert[currentskyvert].v);
496 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
498 VectorCopy(v, skyvert[currentskyvert].v);
506 int RSurf_Light(int *dlightbits, glpoly_t *polys)
508 float cr, cg, cb, radius, radius2, f, *v, *wv;
509 int i, a, b, lit = false;
514 for (a = 0;a < 8;a++)
516 if ((c = dlightbits[a]))
518 for (b = 0, d = 1;c;b++, d <<= 1)
523 light = &cl_dlights[a * 32 + b];
524 lightorigin = light->origin;
525 cr = light->color[0];
526 cg = light->color[1];
527 cb = light->color[2];
528 radius = light->radius*light->radius;
529 radius2 = radius * 256.0f;
531 for (p = polys;p;p = p->next)
533 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
535 f = VectorDistance2(wv, lightorigin);
538 f = radius2 / (f + LIGHTOFFSET);
554 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
557 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
560 // FIXME: make fog texture if water texture is transparent?
562 if (s->dlightframe != r_framecount)
565 // LordHavoc: fast path for no vertex lighting cases
568 if (r_waterripple.value)
570 for (p=s->polys ; p ; p=p->next)
572 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
573 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
575 softwaretransform(v, temp);
576 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);
583 for (p=s->polys ; p ; p=p->next)
585 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
586 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
588 softwaretransform(v, temp);
589 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);
597 if (r_waterripple.value)
599 for (p=s->polys ; p ; p=p->next)
601 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
602 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
603 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);
609 for (p=s->polys ; p ; p=p->next)
611 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
612 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
613 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);
623 for (p = s->polys;p;p = p->next)
625 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
628 softwaretransform(v, wv);
631 if (r_waterripple.value)
632 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);
633 wv[3] = wv[4] = wv[5] = 128.0f;
637 if (s->dlightframe == r_framecount)
638 RSurf_Light(s->dlightbits, s->polys);
640 for (p=s->polys ; p ; p=p->next)
642 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
643 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
644 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);
650 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
652 int i, lit = false, polys = 0, verts = 0;
657 wallvertcolor_t *outcolor;
658 // check for lightmap modification
660 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
661 || r_ambient.value != s->cached_ambient
662 || lighthalf != s->cached_lighthalf
664 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
665 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
666 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
667 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
668 R_UpdateLightmap(s, s->lightmaptexturenum);
669 if (r_dlightmap.value || s->dlightframe != r_framecount)
671 // LordHavoc: fast path version for no vertex lighting cases
672 wp = &wallpoly[currentwallpoly];
673 out = &wallvert[currentwallvert];
674 for (p = s->polys;p;p = p->next)
676 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
678 wp->texnum = (unsigned short) R_GetTexture(t->texture);
679 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
680 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
681 wp->firstvert = currentwallvert;
682 wp->numverts = p->numverts;
686 currentwallvert += p->numverts;
690 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
692 softwaretransform(v, out->vert);
702 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
704 VectorCopy(v, out->vert);
711 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
720 for (p = s->polys;p;p = p->next)
722 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
725 softwaretransform(v, wv);
728 wv[3] = wv[4] = wv[5] = 0.0f;
731 verts += p->numverts;
734 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
736 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
737 lit = RSurf_Light(s->dlightbits, s->polys);
739 wp = &wallpoly[currentwallpoly];
740 out = &wallvert[currentwallvert];
741 outcolor = &wallvertcolor[currentwallvert];
742 currentwallpoly += polys;
743 for (p = s->polys;p;p = p->next)
746 wp->texnum = (unsigned short) R_GetTexture(t->texture);
747 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
748 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
749 wp->firstvert = currentwallvert;
750 wp->numverts = p->numverts;
753 currentwallvert += p->numverts;
754 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
760 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
761 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
762 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
767 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
768 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
769 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
773 out->vert[0] = wv[0];
774 out->vert[1] = wv[1];
775 out->vert[2] = wv[2];
785 // LordHavoc: transparent brush models
786 extern float modelalpha;
788 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
791 float *v, *wv, scale;
794 alpha = (int) (modelalpha * 255.0f);
795 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
797 for (p = s->polys;p;p = p->next)
799 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
802 softwaretransform(v, wv);
805 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
806 if (s->styles[0] != 255)
808 lm = (byte *)((long) s->samples + (int) v[7]);
809 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;
810 if (s->styles[1] != 255)
812 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;
813 if (s->styles[2] != 255)
815 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;
816 if (s->styles[3] != 255)
818 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;
826 if (s->dlightframe == r_framecount)
827 RSurf_Light(s->dlightbits, s->polys);
829 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
831 for (p = s->polys;p;p = p->next)
834 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
835 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
836 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);
842 for (p = s->polys;p;p = p->next)
845 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
846 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
847 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
853 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
860 void R_DrawBrushModel (entity_t *e)
866 int rotated, vertexlit = false;
871 clmodel = e->render.model;
873 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
876 for (i=0 ; i<3 ; i++)
878 mins[i] = e->render.origin[i] - clmodel->radius;
879 maxs[i] = e->render.origin[i] + clmodel->radius;
885 VectorAdd (e->render.origin, clmodel->mins, mins);
886 VectorAdd (e->render.origin, clmodel->maxs, maxs);
889 if (R_VisibleCullBox (mins, maxs))
894 VectorSubtract (r_refdef.vieworg, e->render.origin, modelorg);
898 vec3_t forward, right, up;
900 VectorCopy (modelorg, temp);
901 AngleVectors (e->render.angles, forward, right, up);
902 modelorg[0] = DotProduct (temp, forward);
903 modelorg[1] = -DotProduct (temp, right);
904 modelorg[2] = DotProduct (temp, up);
907 s = &clmodel->surfaces[clmodel->firstmodelsurface];
909 // calculate dynamic lighting for bmodel if it's not an
911 for (i = 0;i < MAX_DLIGHTS;i++)
913 if (!cl_dlights[i].radius)
916 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
917 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
919 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;
921 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
922 softwaretransformforentity (e);
923 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
926 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
928 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
930 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
931 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
933 // sky and liquid don't need sorting (skypoly/transpoly)
934 if (s->flags & SURF_DRAWSKY)
935 RSurf_DrawSky(s, true);
937 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
941 texture_t *t = R_TextureAnimation(s->texinfo->texture);
942 if (vertexlit || s->texinfo->texture->transparent)
943 RSurf_DrawWallVertex(s, t, true, true);
945 RSurf_DrawWall(s, t, true);
953 =============================================================
957 =============================================================
960 static byte *worldvis;
961 extern cvar_t r_novis;
963 void R_MarkLeaves (void)
965 static float noviscache;
966 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
969 r_oldviewleaf = r_viewleaf;
970 noviscache = r_novis.value;
972 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
975 void R_SolidWorldNode ()
979 msurface_t *surf, **mark, **endmark;
981 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
983 if (/*leaf->efrags || */leaf->nummarksurfaces)
985 if (R_CullBox(leaf->mins, leaf->maxs))
990 leaf->visframe = r_framecount;
992 // deal with model fragments in this leaf
994 // R_StoreEfrags (&leaf->efrags);
996 if (leaf->nummarksurfaces)
998 mark = leaf->firstmarksurface;
999 endmark = mark + leaf->nummarksurfaces;
1003 // make sure surfaces are only processed once
1004 if (surf->worldnodeframe == r_framecount)
1006 surf->worldnodeframe = r_framecount;
1007 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1009 if (surf->flags & SURF_PLANEBACK)
1010 surf->visframe = r_framecount;
1014 if (!(surf->flags & SURF_PLANEBACK))
1015 surf->visframe = r_framecount;
1018 while (mark < endmark);
1025 // experimental and inferior to the other in recursion depth allowances
1026 void R_PortalWorldNode ()
1030 msurface_t *surf, **mark, **endmark;
1031 mleaf_t *leaf, *llistbuffer[8192], **l, **llist;
1034 leaf->worldnodeframe = r_framecount;
1035 l = llist = &llistbuffer[0];
1036 *llist++ = r_viewleaf;
1043 leaf->visframe = r_framecount;
1045 // deal with model fragments in this leaf
1046 // if (leaf->efrags)
1047 // R_StoreEfrags (&leaf->efrags);
1049 if (leaf->nummarksurfaces)
1051 mark = leaf->firstmarksurface;
1052 endmark = mark + leaf->nummarksurfaces;
1056 // make sure surfaces are only processed once
1057 if (surf->worldnodeframe == r_framecount)
1059 surf->worldnodeframe = r_framecount;
1060 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1062 if (surf->flags & SURF_PLANEBACK)
1063 surf->visframe = r_framecount;
1067 if (!(surf->flags & SURF_PLANEBACK))
1068 surf->visframe = r_framecount;
1071 while (mark < endmark);
1074 // follow portals into other leafs
1076 for (;p;p = p->next)
1079 if (leaf->worldnodeframe != r_framecount)
1081 leaf->worldnodeframe = r_framecount;
1082 i = (leaf - cl.worldmodel->leafs) - 1;
1083 if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1091 p = r_viewleaf->portals;
1092 for (;p;p = p->next)
1095 if (p->past->worldnodeframe != r_framecount)
1099 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1103 void R_PortalWorldNode ()
1106 mportal_t *p, *pstack[8192];
1107 msurface_t *surf, **mark, **endmark;
1111 leaf->worldnodeframe = r_framecount;
1116 leaf->visframe = r_framecount;
1118 // deal with model fragments in this leaf
1119 // if (leaf->efrags)
1120 // R_StoreEfrags (&leaf->efrags);
1122 if (leaf->nummarksurfaces)
1124 mark = leaf->firstmarksurface;
1125 endmark = mark + leaf->nummarksurfaces;
1129 // make sure surfaces are only processed once
1130 if (surf->worldnodeframe == r_framecount)
1132 surf->worldnodeframe = r_framecount;
1133 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1135 if (surf->flags & SURF_PLANEBACK)
1136 surf->visframe = r_framecount;
1140 if (!(surf->flags & SURF_PLANEBACK))
1141 surf->visframe = r_framecount;
1144 while (mark < endmark);
1147 // follow portals into other leafs
1149 for (;p;p = p->next)
1152 if (leaf->worldnodeframe != r_framecount)
1154 leaf->worldnodeframe = r_framecount;
1155 if (leaf->contents != CONTENTS_SOLID)
1157 i = (leaf - cl.worldmodel->leafs) - 1;
1158 if (worldvis[i>>3] & (1<<(i&7)))
1160 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1162 pstack[portalstack++] = p;
1166 p = pstack[--portalstack];
1178 p = r_viewleaf->portals;
1179 for (;p;p = p->next)
1182 if (p->past->worldnodeframe != r_framecount)
1186 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
1189 void R_DrawSurfaces (void)
1191 msurface_t *surf, *endsurf;
1193 int vertex = gl_vertex.value;
1195 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1196 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1197 for (;surf < endsurf;surf++)
1199 if (surf->visframe == r_framecount)
1202 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1204 // sky and liquid don't need sorting (skypoly/transpoly)
1205 if (surf->flags & SURF_DRAWSKY)
1206 RSurf_DrawSky(surf, false);
1208 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1212 t = R_TextureAnimation(surf->texinfo->texture);
1214 RSurf_DrawWallVertex(surf, t, false, false);
1216 RSurf_DrawWall(surf, t, false);
1222 void R_DrawPortals()
1224 int drawportals, i, r, g, b;
1225 mleaf_t *leaf, *endleaf;
1227 mvertex_t *point, *endpoint;
1228 drawportals = (int)r_drawportals.value;
1229 if (drawportals < 1)
1231 leaf = cl.worldmodel->leafs;
1232 endleaf = leaf + cl.worldmodel->numleafs;
1233 for (;leaf < endleaf;leaf++)
1235 if (leaf->visframe == r_framecount && leaf->portals)
1237 i = leaf - cl.worldmodel->leafs;
1238 r = (i & 0x0007) << 5;
1239 g = (i & 0x0038) << 2;
1240 b = (i & 0x01C0) >> 1;
1241 portal = leaf->portals;
1244 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1245 point = portal->points + portal->numpoints - 1;
1246 endpoint = portal->points;
1247 for (;point >= endpoint;point--)
1248 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1250 portal = portal->next;
1261 void R_DrawWorld (void)
1265 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1267 memset (&ent, 0, sizeof(ent));
1268 ent.render.model = cl.worldmodel;
1269 ent.render.colormod[0] = ent.render.colormod[1] = ent.render.colormod[2] = 1;
1270 modelalpha = ent.render.alpha = 1;
1271 ent.render.scale = 1;
1273 VectorCopy (r_refdef.vieworg, modelorg);
1275 currententity = &ent;
1277 softwaretransformidentity(); // LordHavoc: clear transform
1281 if (r_viewleaf->contents == CONTENTS_SOLID)
1282 R_SolidWorldNode ();
1286 R_PortalWorldNode ();
1290 R_PushDlights (); // now mark the lit surfaces
1298 =============================================================================
1302 =============================================================================
1305 // returns a texture number and the position inside it
1306 int AllocBlock (int w, int h, short *x, short *y)
1312 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
1314 best = BLOCK_HEIGHT;
1316 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1320 for (j=0 ; j<w ; j++)
1322 if (allocated[texnum][i+j] >= best)
1324 if (allocated[texnum][i+j] > best2)
1325 best2 = allocated[texnum][i+j];
1328 { // this is a valid spot
1334 if (best + h > BLOCK_HEIGHT)
1337 if (nosubimagefragments || nosubimage)
1339 if (!lightmaps[texnum])
1341 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1342 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1345 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1346 else if (!allocated[texnum][0])
1348 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
1349 memset(blank, 0, sizeof(blank));
1352 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1353 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1354 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1356 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1358 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1362 for (i = 0;i < w;i++)
1363 allocated[texnum][*x + i] = best + h;
1368 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1373 mvertex_t *r_pcurrentvertbase;
1374 model_t *currentmodel;
1380 BuildSurfaceDisplayList
1383 void BuildSurfaceDisplayList (msurface_t *fa)
1385 int i, j, lindex, lnumverts;
1386 medge_t *pedges, *r_pedge;
1392 // reconstruct the polygon
1393 pedges = currentmodel->edges;
1394 lnumverts = fa->numedges;
1400 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1401 poly->next = fa->polys;
1402 poly->flags = fa->flags;
1404 poly->numverts = lnumverts;
1406 for (i=0 ; i<lnumverts ; i++)
1408 lindex = currentmodel->surfedges[fa->firstedge + i];
1412 r_pedge = &pedges[lindex];
1413 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1417 r_pedge = &pedges[-lindex];
1418 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1420 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1421 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1423 VectorCopy (vec, poly->verts[i]);
1424 poly->verts[i][3] = s / fa->texinfo->texture->width;
1425 poly->verts[i][4] = t / fa->texinfo->texture->height;
1428 // lightmap texture coordinates
1430 s -= fa->texturemins[0];
1431 t -= fa->texturemins[1];
1434 // LordHavoc: calc lightmap data offset
1435 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;
1436 poly->verts[i][7] = j;
1437 s += fa->light_s*16;
1438 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1440 t += fa->light_t*16;
1441 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1443 poly->verts[i][5] = s;
1444 poly->verts[i][6] = t;
1448 // remove co-linear points - Ed
1451 if (!gl_keeptjunctions.value)
1453 for (i = 0 ; i < lnumverts ; ++i)
1456 float *prev, *this, *next;
1458 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1459 this = poly->verts[i];
1460 next = poly->verts[(i + 1) % lnumverts];
1462 VectorSubtract( this, prev, v1 );
1463 VectorNormalize( v1 );
1464 VectorSubtract( next, prev, v2 );
1465 VectorNormalize( v2 );
1467 // skip co-linear points
1468 #define COLINEAR_EPSILON 0.001
1469 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1470 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1471 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1474 for (j = i + 1; j < lnumverts; ++j)
1477 for (k = 0; k < VERTEXSIZE; ++k)
1478 poly->verts[j - 1][k] = poly->verts[j][k];
1482 // retry next vertex next time, which is now current vertex
1488 poly->numverts = lnumverts;
1492 ========================
1493 GL_CreateSurfaceLightmap
1494 ========================
1496 void GL_CreateSurfaceLightmap (msurface_t *surf)
1500 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1503 smax = (surf->extents[0]>>4)+1;
1504 tmax = (surf->extents[1]>>4)+1;
1506 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1507 if (nosubimage || nosubimagefragments)
1509 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1510 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1513 R_BuildLightMap (surf, templight, smax * 4);
1515 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1519 R_BuildLightMap (surf, templight, smax * 3);
1521 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1530 Builds the lightmap texture
1531 with all the surfaces from all brush models
1534 void GL_BuildLightmaps (void)
1539 memset (allocated, 0, sizeof(allocated));
1541 r_framecount = 1; // no dlightcache
1543 if (gl_nosubimagefragments.value)
1544 nosubimagefragments = 1;
1546 nosubimagefragments = 0;
1548 if (gl_nosubimage.value)
1553 if (gl_lightmaprgba.value)
1555 lightmaprgba = true;
1560 lightmaprgba = false;
1564 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1565 // it needs to be aligned on 4 pixel boundaries...
1566 // so I implemented an adjustable lightmap alignment
1567 if (gl_lightmapalign.value < 1)
1568 gl_lightmapalign.value = 1;
1569 if (gl_lightmapalign.value > 16)
1570 gl_lightmapalign.value = 16;
1572 while (lightmapalign < gl_lightmapalign.value)
1573 lightmapalign <<= 1;
1574 gl_lightmapalign.value = lightmapalign;
1575 lightmapalignmask = ~(lightmapalign - 1);
1576 if (nosubimagefragments || nosubimage)
1579 lightmapalignmask = ~0;
1582 if (!lightmap_textures)
1583 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
1585 for (j=1 ; j<MAX_MODELS ; j++)
1587 m = cl.model_precache[j];
1590 if (m->name[0] == '*')
1592 r_pcurrentvertbase = m->vertexes;
1594 for (i=0 ; i<m->numsurfaces ; i++)
1596 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1598 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1600 GL_CreateSurfaceLightmap (m->surfaces + i);
1601 BuildSurfaceDisplayList (m->surfaces + i);
1605 if (nosubimage || nosubimagefragments)
1609 qglSelectTexture(gl_mtex_enum+1);
1610 for (i = 0;i < MAX_LIGHTMAPS;i++)
1612 if (!allocated[i][0])
1614 lightmapupdate[i][0] = BLOCK_HEIGHT;
1615 lightmapupdate[i][1] = 0;
1618 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1619 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1620 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1622 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1624 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1629 qglSelectTexture(gl_mtex_enum+0);