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 #define BLOCK_WIDTH 256
27 #define BLOCK_HEIGHT 256
28 // LordHavoc: increased lightmap limit from 64 to 1024
29 #define MAX_LIGHTMAPS 1024
30 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
34 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
36 byte *lightmaps[MAX_LIGHTMAPS];
37 short lightmapupdate[MAX_LIGHTMAPS][2];
39 signed int blocklights[BLOCK_WIDTH*BLOCK_HEIGHT*3]; // LordHavoc: *3 for colored lighting
41 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
42 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
43 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "1"};
44 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
45 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
46 cvar_t r_ambient = {"r_ambient", "0"};
47 cvar_t gl_vertex = {"gl_vertex", "0"};
48 cvar_t r_dlightmap = {"r_dlightmap", "1"};
49 cvar_t r_drawportals = {"r_drawportals", "0"};
50 cvar_t r_testvis = {"r_testvis", "0"};
51 cvar_t r_solidworldnode = {"r_solidworldnode", "1"};
53 qboolean lightmaprgba, nosubimagefragments, nosubimage;
58 void gl_surf_start(void)
62 void gl_surf_shutdown(void)
66 void gl_surf_newmap(void)
70 void GL_Surf_Init(void)
73 for (i = 0;i < MAX_LIGHTMAPS;i++)
75 Cvar_RegisterVariable(&gl_lightmapalign);
76 Cvar_RegisterVariable(&gl_lightmaprgba);
77 Cvar_RegisterVariable(&gl_nosubimagefragments);
78 Cvar_RegisterVariable(&gl_nosubimage);
79 Cvar_RegisterVariable(&r_ambient);
80 Cvar_RegisterVariable(&gl_vertex);
81 Cvar_RegisterVariable(&r_dlightmap);
82 Cvar_RegisterVariable(&r_drawportals);
83 Cvar_RegisterVariable(&r_testvis);
84 Cvar_RegisterVariable(&r_solidworldnode);
86 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
89 int dlightdivtable[32768];
94 int R_AddDynamicLights (msurface_t *surf)
96 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
101 // LordHavoc: use 64bit integer... shame it's not very standardized...
102 #if _MSC_VER || __BORLANDC__
110 if (!dlightdivtable[1])
112 dlightdivtable[0] = 4194304;
113 for (s = 1; s < 32768; s++)
114 dlightdivtable[s] = 4194304 / (s << 7);
117 smax = (surf->extents[0] >> 4) + 1;
118 tmax = (surf->extents[1] >> 4) + 1;
120 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
122 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
123 continue; // not lit by this light
125 VectorSubtract (cl_dlights[lnum].origin, currententity->render.origin, local);
126 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
128 // for comparisons to minimum acceptable light
129 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
131 // clamp radius to avoid exceeding 32768 entry division table
132 if (maxdist > 4194304)
136 if (dist2 >= maxdist)
139 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
140 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
141 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
143 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
144 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
146 s = bound(0, impacts, smax * 16) - impacts;
147 t = bound(0, impactt, tmax * 16) - impactt;
148 i = s * s + t * t + dist2;
152 // reduce calculations
153 for (s = 0, i = impacts; s < smax; s++, i -= 16)
154 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
156 maxdist3 = maxdist - (int) (dist * dist);
158 // convert to 8.8 blocklights format and scale up by radius
159 red = cl_dlights[lnum].color[0] * maxdist;
160 green = cl_dlights[lnum].color[1] * maxdist;
161 blue = cl_dlights[lnum].color[2] * maxdist;
165 for (t = 0; t < tmax; t++, i -= 16)
168 // make sure some part of it is visible on this line
171 maxdist2 = maxdist - td;
172 for (s = 0; s < smax; s++)
174 if (sdtable[s] < maxdist2)
176 k = dlightdivtable[(sdtable[s] + td) >> 7];
177 bl[0] += (red * k) >> 9;
178 bl[1] += (green * k) >> 9;
179 bl[2] += (blue * k) >> 9;
193 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
196 stride -= (width*lightmapbytes);
199 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
200 // the image is brightened as a processing pass
203 for (i = 0;i < height;i++, out += stride)
205 for (j = 0;j < width;j++, in += 3, out += 4)
207 out[0] = min(in[0] >> 8, 255);
208 out[1] = min(in[1] >> 8, 255);
209 out[2] = min(in[2] >> 8, 255);
216 for (i = 0;i < height;i++, out += stride)
218 for (j = 0;j < width;j++, in += 3, out += 3)
220 out[0] = min(in[0] >> 8, 255);
221 out[1] = min(in[1] >> 8, 255);
222 out[2] = min(in[2] >> 8, 255);
231 for (i = 0;i < height;i++, out += stride)
233 for (j = 0;j < width;j++, in += 3, out += 4)
235 out[0] = min(in[0] >> 7, 255);
236 out[1] = min(in[1] >> 7, 255);
237 out[2] = min(in[2] >> 7, 255);
244 for (i = 0;i < height;i++, out += stride)
246 for (j = 0;j < width;j++, in += 3, out += 3)
248 out[0] = min(in[0] >> 7, 255);
249 out[1] = min(in[1] >> 7, 255);
250 out[2] = min(in[2] >> 7, 255);
261 Combine and scale multiple lightmaps into the 8.8 format in blocklights
264 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
267 int i, j, size, size3;
273 surf->cached_dlight = 0;
274 surf->cached_lighthalf = lighthalf;
275 surf->cached_ambient = r_ambient.value;
277 smax = (surf->extents[0]>>4)+1;
278 tmax = (surf->extents[1]>>4)+1;
281 lightmap = surf->samples;
283 // set to full bright if no light data
284 if ((currententity && (currententity->render.effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
287 for (i=0 ; i<size ; i++)
297 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
301 for (i = 0;i < size3;i++)
305 memset(&blocklights[0], 0, size*3*sizeof(int));
307 // add all the lightmaps
310 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
312 scale = d_lightstylevalue[surf->styles[maps]];
313 surf->cached_light[maps] = scale; // 8.8 fraction
315 for (i = 0;i < size3;i++)
316 *bl++ += *lightmap++ * scale;
319 if (r_dlightmap.value && surf->dlightframe == r_framecount)
320 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
323 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
326 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
328 void R_UpdateLightmap(msurface_t *s, int lnum)
331 // upload the new lightmap texture fragment
333 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
334 if (nosubimage || nosubimagefragments)
336 if (lightmapupdate[lnum][0] > s->light_t)
337 lightmapupdate[lnum][0] = s->light_t;
338 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
339 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
341 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
343 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
347 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
348 tmax = (s->extents[1]>>4)+1;
351 R_BuildLightMap (s, templight, smax * 4);
353 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
357 R_BuildLightMap (s, templight, smax * 3);
359 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
369 Returns the proper texture for a given time and base texture
372 texture_t *R_TextureAnimation (texture_t *base)
374 // texture_t *original;
378 if (currententity->render.frame)
380 if (base->alternate_anims)
381 base = base->alternate_anims;
384 if (!base->anim_total)
387 return base->anim_frames[(int)(cl.time*5) % base->anim_total];
392 relative = (int)(cl.time*5) % base->anim_total;
395 while (base->anim_min > relative || base->anim_max <= relative)
397 base = base->anim_next;
400 Con_Printf("R_TextureAnimation: broken cycle");
405 Con_Printf("R_TextureAnimation: infinite cycle");
416 =============================================================
420 =============================================================
426 #include "gl_warp_sin.h"
428 #define TURBSCALE (256.0 / (2 * M_PI))
431 void UploadLightmaps(void)
434 if (nosubimage || nosubimagefragments)
436 for (i = 0;i < MAX_LIGHTMAPS;i++)
438 if (lightmapupdate[i][0] < lightmapupdate[i][1])
442 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
446 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
448 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
453 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]));
455 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]));
459 lightmapupdate[i][0] = BLOCK_HEIGHT;
460 lightmapupdate[i][1] = 0;
465 float wvert[1024*6]; // used by the following functions
467 void RSurf_DrawSky(msurface_t *s, int transform)
473 // LordHavoc: HalfLife maps have freaky skypolys...
477 for (p=s->polys ; p ; p=p->next)
479 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
481 skypoly[currentskypoly].firstvert = currentskyvert;
482 skypoly[currentskypoly++].verts = p->numverts;
485 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
487 softwaretransform(v, skyvert[currentskyvert].v);
493 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
495 VectorCopy(v, skyvert[currentskyvert].v);
503 int RSurf_Light(int *dlightbits, glpoly_t *polys)
505 float cr, cg, cb, radius, radius2, f, *v, *wv;
506 int i, a, b, lit = false;
511 for (a = 0;a < 8;a++)
513 if ((c = dlightbits[a]))
515 for (b = 0, d = 1;c;b++, d <<= 1)
520 light = &cl_dlights[a * 32 + b];
521 lightorigin = light->origin;
522 cr = light->color[0];
523 cg = light->color[1];
524 cb = light->color[2];
525 radius = light->radius*light->radius;
526 radius2 = radius * 256.0f;
528 for (p = polys;p;p = p->next)
530 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
532 f = VectorDistance2(wv, lightorigin);
535 f = radius2 / (f + LIGHTOFFSET);
551 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
554 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
557 // FIXME: make fog texture if water texture is transparent?
559 if (s->dlightframe != r_framecount)
562 // LordHavoc: fast path for no vertex lighting cases
565 if (r_waterripple.value)
567 for (p=s->polys ; p ; p=p->next)
569 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
570 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
572 softwaretransform(v, temp);
573 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);
580 for (p=s->polys ; p ; p=p->next)
582 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
583 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
585 softwaretransform(v, temp);
586 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);
594 if (r_waterripple.value)
596 for (p=s->polys ; p ; p=p->next)
598 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
599 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
600 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);
606 for (p=s->polys ; p ; p=p->next)
608 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
609 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
610 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);
620 for (p = s->polys;p;p = p->next)
622 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
625 softwaretransform(v, wv);
628 if (r_waterripple.value)
629 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);
630 wv[3] = wv[4] = wv[5] = 128.0f;
634 if (s->dlightframe == r_framecount)
635 RSurf_Light(s->dlightbits, s->polys);
637 for (p=s->polys ; p ; p=p->next)
639 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
640 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
641 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);
647 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
649 int i, lit = false, polys = 0, verts = 0;
654 wallvertcolor_t *outcolor;
655 // check for lightmap modification
657 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
658 || r_ambient.value != s->cached_ambient
659 || lighthalf != s->cached_lighthalf
661 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
662 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
663 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
664 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
665 R_UpdateLightmap(s, s->lightmaptexturenum);
666 if (s->dlightframe != r_framecount || r_dlightmap.value)
668 // LordHavoc: fast path version for no vertex lighting cases
669 wp = &wallpoly[currentwallpoly];
670 out = &wallvert[currentwallvert];
671 for (p = s->polys;p;p = p->next)
673 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
675 wp->texnum = (unsigned short) R_GetTexture(t->texture);
676 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
677 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
678 wp->firstvert = currentwallvert;
679 wp->numverts = p->numverts;
683 currentwallvert += p->numverts;
687 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
689 softwaretransform(v, out->vert);
699 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
701 VectorCopy(v, out->vert);
708 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
717 for (p = s->polys;p;p = p->next)
719 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
722 softwaretransform(v, wv);
725 wv[3] = wv[4] = wv[5] = 0.0f;
728 verts += p->numverts;
731 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
733 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
734 lit = RSurf_Light(s->dlightbits, s->polys);
736 wp = &wallpoly[currentwallpoly];
737 out = &wallvert[currentwallvert];
738 outcolor = &wallvertcolor[currentwallvert];
739 currentwallpoly += polys;
740 for (p = s->polys;p;p = p->next)
743 wp->texnum = (unsigned short) R_GetTexture(t->texture);
744 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
745 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
746 wp->firstvert = currentwallvert;
747 wp->numverts = p->numverts;
750 currentwallvert += p->numverts;
751 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
757 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
758 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
759 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
764 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
765 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
766 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
770 out->vert[0] = wv[0];
771 out->vert[1] = wv[1];
772 out->vert[2] = wv[2];
782 // LordHavoc: transparent brush models
783 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
786 float *v, *wv, scale;
789 alpha = (int) (modelalpha * 255.0f);
790 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
792 for (p = s->polys;p;p = p->next)
794 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
797 softwaretransform(v, wv);
800 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
801 if (s->styles[0] != 255)
803 lm = (byte *)((long) s->samples + (int) v[7]);
804 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;
805 if (s->styles[1] != 255)
807 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;
808 if (s->styles[2] != 255)
810 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;
811 if (s->styles[3] != 255)
813 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;
821 if (s->dlightframe == r_framecount)
822 RSurf_Light(s->dlightbits, s->polys);
824 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
826 for (p = s->polys;p;p = p->next)
829 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
830 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
831 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);
837 for (p = s->polys;p;p = p->next)
840 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
841 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
842 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
848 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
850 float bmverts[256*3];
855 void RBrushModelSurf_DoVisible(msurface_t *surf)
857 // float *v, *bmv, *endbmv;
859 // for (p = surf->polys;p;p = p->next)
861 // for (v = p->verts[0], bmv = bmpoints, endbmv = bmv + p->numverts * 3;bmv < endbmv;v += VERTEXSIZE, bmv += 3)
862 // softwaretransform(v, bmv);
863 // if (R_Clip_Polygon(bmpoints, p->numverts, sizeof(float) * 3, surf->flags & SURF_CLIPSOLID))
864 surf->visframe = r_framecount;
869 void RBrushModelSurf_Callback(void *data, void *data2)
871 entity_t *ent = data2;
872 msurface_t *surf = data;
876 // FIXME: implement better dupe prevention in AddPolygon callback code
877 if (ent->render.model->firstmodelsurface != 0)
879 // it's not an instanced model, so we already rely on there being only one of it (usually a valid assumption, but QC can break this)
880 if (surf->visframe == r_framecount)
884 surf->visframe = r_framecount;
889 modelalpha = ent->render.alpha;
891 softwaretransformforbrushentity (ent);
893 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
895 // sky and liquid don't need sorting (skypoly/transpoly)
896 if (surf->flags & SURF_DRAWSKY)
897 RSurf_DrawSky(surf, true);
899 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), true, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
903 t = R_TextureAnimation(surf->texinfo->texture);
904 if (surf->texinfo->texture->transparent || vertexworld || ent->render.alpha != 1 || ent->render.model->firstmodelsurface == 0 || (ent->render.effects & EF_FULLBRIGHT) || ent->render.colormod[0] != 1 || ent->render.colormod[2] != 1 || ent->render.colormod[2] != 1)
905 RSurf_DrawWallVertex(surf, t, true, true);
907 RSurf_DrawWall(surf, t, true);
916 void R_DrawBrushModel (entity_t *e)
928 clmodel = e->render.model;
930 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
933 for (i=0 ; i<3 ; i++)
935 mins[i] = e->render.origin[i] - clmodel->radius;
936 maxs[i] = e->render.origin[i] + clmodel->radius;
942 VectorAdd (e->render.origin, clmodel->mins, mins);
943 VectorAdd (e->render.origin, clmodel->maxs, maxs);
946 if (R_CullBox (mins, maxs))
951 VectorSubtract (r_origin, e->render.origin, modelorg);
955 vec3_t forward, right, up;
957 VectorCopy (modelorg, temp);
958 AngleVectors (e->render.angles, forward, right, up);
959 modelorg[0] = DotProduct (temp, forward);
960 modelorg[1] = -DotProduct (temp, right);
961 modelorg[2] = DotProduct (temp, up);
964 softwaretransformforbrushentity (e);
966 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
969 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
970 s->visframe = r_framecount;
973 // calculate dynamic lighting for bmodel if it's not an instanced model
974 for (i = 0;i < MAX_DLIGHTS;i++)
976 if (!cl_dlights[i].radius)
979 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
980 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
982 // 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;
985 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
987 if (s->visframe == r_framecount)
989 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
990 for (p = s->polys;p;p = p->next)
992 for (j = 0;j < p->numverts;j++)
993 softwaretransform(&p->verts[j][0], bmverts + j * 3);
994 R_Clip_AddPolygon(bmverts, p->numverts, 3 * sizeof(float), (s->flags & SURF_CLIPSOLID) != 0 && modelalpha == 1, RBrushModelSurf_Callback, s, e, NULL);
997 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
999 // sky and liquid don't need sorting (skypoly/transpoly)
1000 if (s->flags & SURF_DRAWSKY)
1001 RSurf_DrawSky(s, true);
1003 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1007 texture_t *t = R_TextureAnimation(s->texinfo->texture);
1008 if (vertexlit || s->texinfo->texture->transparent)
1009 RSurf_DrawWallVertex(s, t, true, true);
1011 RSurf_DrawWall(s, t, true);
1020 =============================================================
1024 =============================================================
1028 static byte *worldvis;
1030 void R_MarkLeaves (void)
1032 static float noviscache;
1033 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1036 r_oldviewleaf = r_viewleaf;
1037 noviscache = r_novis.value;
1039 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1043 void RSurf_Callback(void *data, void *junk)
1045 ((msurface_t *)data)->visframe = r_framecount;
1049 void RSurf_Callback(void *data, void *junk)
1051 msurface_t *surf = data;
1054 // if (surf->visframe == r_framecount)
1057 surf->visframe = r_framecount;
1061 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1063 // sky and liquid don't need sorting (skypoly/transpoly)
1064 if (surf->flags & SURF_DRAWSKY)
1065 RSurf_DrawSky(surf, false);
1067 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1071 t = R_TextureAnimation(surf->texinfo->texture);
1073 RSurf_DrawWallVertex(surf, t, false, false);
1075 RSurf_DrawWall(surf, t, false);
1081 mleaf_t *r_oldviewleaf;
1082 int r_markvisframecount = 0;
1084 void R_MarkLeaves (void)
1086 static float noviscache;
1089 msurface_t *surf, **mark, **endmark;
1090 model_t *model = cl.worldmodel;
1091 // mportal_t *portal;
1096 // ignore testvis if the map just changed
1097 if (r_testvis.value && model->nodes->markvisframe == r_markvisframecount)
1100 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1103 r_oldviewleaf = r_viewleaf;
1104 noviscache = r_novis.value;
1106 if ((in = r_viewleaf->compressed_vis))
1108 row = (model->numleafs+7)>>3;
1110 if (!r_testvis.value)
1111 r_markvisframecount++;
1113 // LordHavoc: mark the root node as visible, it will terminate all other ascensions
1114 model->nodes->markvisframe = r_markvisframecount;
1122 l = model->numleafs - (k << 3);
1125 for (i=0 ; i<l ; i++)
1129 leaf = &model->leafs[(k << 3)+i+1];
1130 node = (mnode_t *)leaf;
1133 node->markvisframe = r_markvisframecount;
1134 node = node->parent;
1136 while (node->markvisframecount != r_markvisframecount);
1147 // LordHavoc: no vis data, mark everything as visible
1148 model->nodes->markvisframe = r_markvisframecount;
1150 for (i = 1;i < model->numleafs;i++)
1152 node = (mnode_t *)&model->leafs[i];
1155 node->markvisframe = r_markvisframecount;
1156 node = node->parent;
1158 while (node->markvisframecount != r_markvisframecount);
1164 void R_SolidWorldNode (void)
1166 if ((int) r_solidworldnode.value == 2)
1168 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1169 int nodestackpos = 0;
1173 if (node->numsurfaces)
1175 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1177 if (PlaneDiff (r_origin, node->plane) < 0)
1179 for (;surf < surfend;surf++)
1181 if (surf->flags & SURF_PLANEBACK)
1183 VectorNegate(surf->plane->normal, plane.normal);
1184 plane.dist = -surf->plane->dist;
1185 for (p = surf->polys;p;p = p->next)
1186 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1192 for (;surf < surfend;surf++)
1194 if (!(surf->flags & SURF_PLANEBACK))
1195 for (p = surf->polys;p;p = p->next)
1196 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1201 // recurse down the children
1202 if (node->children[0]->contents >= 0)
1204 if (node->children[1]->contents >= 0)
1206 if (nodestackpos < 8192)
1207 nodestack[nodestackpos++] = node->children[1];
1208 node = node->children[0];
1211 node = node->children[0];
1214 else if (node->children[1]->contents >= 0)
1216 node = node->children[1];
1219 else if (nodestackpos > 0)
1221 node = nodestack[--nodestackpos];
1225 else if ((int) r_solidworldnode.value == 1)
1228 msurface_t *surf, *endsurf;
1232 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1233 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1234 for (;surf < endsurf;surf++)
1236 if (PlaneDiff(r_origin, surf->plane) < 0)
1238 if (surf->flags & SURF_PLANEBACK)
1240 VectorNegate(surf->plane->normal, plane.normal);
1241 plane.dist = -surf->plane->dist;
1242 for (p = surf->polys;p;p = p->next)
1243 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1248 if (!(surf->flags & SURF_PLANEBACK))
1249 for (p = surf->polys;p;p = p->next)
1250 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)&surf->plane);
1258 msurface_t *surf, **mark, **endmark;
1262 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
1264 if (R_CullBox(leaf->mins, leaf->maxs))
1266 // leaf->visframe = r_framecount;
1268 if (leaf->nummarksurfaces)
1270 // if (R_CullBox(leaf->mins, leaf->maxs))
1273 if (leaf->nummarksurfaces)
1275 mark = leaf->firstmarksurface;
1276 endmark = mark + leaf->nummarksurfaces;
1280 // make sure surfaces are only processed once
1281 if (surf->worldnodeframe == r_framecount)
1283 surf->worldnodeframe = r_framecount;
1284 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1286 if (surf->flags & SURF_PLANEBACK)
1288 VectorNegate(surf->plane->normal, plane.normal);
1289 plane.dist = -surf->plane->dist;
1290 for (p = surf->polys;p;p = p->next)
1291 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1296 if (!(surf->flags & SURF_PLANEBACK))
1297 for (p = surf->polys;p;p = p->next)
1298 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1301 while (mark < endmark);
1309 void RSurf_Callback(void *data, void *junk)
1311 ((msurface_t *)data)->visframe = r_framecount;
1314 int R_FrustumTestPolygon(float *points, int numpoints, int stride);
1316 void RSurf_DoVisible(msurface_t *surf)
1319 for (p = surf->polys;p;p = p->next)
1320 if (R_FrustumTestPolygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float)) >= 3)
1321 // R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), true, RSurf_Callback, surf, 1);
1322 // if (R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID))
1323 surf->visframe = r_framecount;
1327 //mleaf_t *llistbuffer[32768], *l, **llist;
1330 void RSurfLeaf_Callback(void *data)
1332 int portalstackpos = 0;
1334 mportal_t *p, *portalstack[32768];
1335 msurface_t *surf, **mark, **endmark;
1340 if (leaf->visframe == r_framecount)
1342 leaf->visframe = r_framecount;
1346 if (leaf->nummarksurfaces)
1348 mark = leaf->firstmarksurface;
1349 endmark = mark + leaf->nummarksurfaces;
1353 // make sure surfaces are only processed once
1354 if (surf->worldnodeframe == r_framecount)
1356 surf->worldnodeframe = r_framecount;
1357 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1359 if (surf->flags & SURF_PLANEBACK)
1360 RSurf_DoVisible(surf);
1364 if (!(surf->flags & SURF_PLANEBACK))
1365 RSurf_DoVisible(surf);
1368 while (mark < endmark);
1371 // follow portals into other leafs
1372 for (p = leaf->portals;p;p = p->next)
1374 if (p->past->visframe != r_framecount && DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1376 // R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3, RSurfLeaf_Callback, p->past, 1);
1377 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1378 portalstack[portalstackpos++] = p;
1382 while(portalstackpos);
1383 RSurfLeaf_Callback(p->past);
1384 // upon returning, R_ProcessSpans will notice that the spans have changed and restart the line, this is ok because we're not adding any polygons that aren't already behind the portal
1389 // experimental and inferior to the other in recursion depth allowances
1390 void R_PortalWorldNode (void)
1394 msurface_t *surf, **mark, **endmark;
1395 mleaf_t *leaf, *llistbuffer[32768], **l, **llist;
1398 leaf->visframe = r_framecount;
1399 l = llist = &llistbuffer[0];
1400 *llist++ = r_viewleaf;
1407 if (leaf->nummarksurfaces)
1409 mark = leaf->firstmarksurface;
1410 endmark = mark + leaf->nummarksurfaces;
1414 // make sure surfaces are only processed once
1415 if (surf->worldnodeframe == r_framecount)
1417 surf->worldnodeframe = r_framecount;
1418 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1420 if (surf->flags & SURF_PLANEBACK)
1421 RSurf_DoVisible(surf);
1425 if (!(surf->flags & SURF_PLANEBACK))
1426 RSurf_DoVisible(surf);
1429 while (mark < endmark);
1432 // follow portals into other leafs
1433 for (p = leaf->portals;p;p = p->next)
1435 if (p->past->visframe != r_framecount)
1437 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1439 p->past->visframe = r_framecount;
1445 // for (p = leaf->portals;p;p = p->next)
1448 // if (leaf->worldnodeframe != r_framecount)
1450 // leaf->worldnodeframe = r_framecount;
1451 // i = (leaf - cl.worldmodel->leafs) - 1;
1452 // if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1460 // p = r_viewleaf->portals;
1461 // for (;p;p = p->next)
1464 // if (p->past->worldnodeframe != r_framecount)
1468 // Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1473 #define MAXRECURSIVEPORTALPLANES 1024
1474 #define MAXRECURSIVEPORTALS 256
1476 tinyplane_t portalplanes[MAXRECURSIVEPORTALPLANES];
1477 int portalplanestack[MAXRECURSIVEPORTALS];
1478 int portalplanecount;
1479 int ranoutofportalplanes;
1480 int ranoutofportals;
1482 int portalcantseeself;
1483 int portalrecursion;
1485 int R_ClipPolygonToPlane(float *in, float *out, int inpoints, int maxoutpoints, tinyplane_t *p)
1487 int i, outpoints, prevside, side;
1488 float *prevpoint, prevdist, dist, dot;
1492 // begin with the last point, then enter the loop with the first point as current
1493 prevpoint = in + 3 * (inpoints - 1);
1494 prevdist = DotProduct(prevpoint, p->normal) - p->dist;
1495 prevside = prevdist >= 0 ? SIDE_FRONT : SIDE_BACK;
1499 for (;i < inpoints;i++)
1507 dist = DotProduct(in, p->normal) - p->dist;
1508 side = dist >= 0 ? SIDE_FRONT : SIDE_BACK;
1510 if (prevside == SIDE_FRONT)
1512 if (outpoints >= maxoutpoints)
1514 VectorCopy(prevpoint, out);
1517 if (side == SIDE_FRONT)
1520 else if (side == SIDE_BACK)
1523 // generate a split point
1524 if (outpoints >= maxoutpoints)
1526 dot = prevdist / (prevdist - dist);
1527 out[0] = prevpoint[0] + dot * (in[0] - prevpoint[0]);
1528 out[1] = prevpoint[1] + dot * (in[1] - prevpoint[1]);
1529 out[2] = prevpoint[2] + dot * (in[2] - prevpoint[2]);
1537 float portaltemppoints[2][256][3];
1538 float portaltemppoints2[256][3];
1540 int R_FrustumTestPolygon(float *points, int numpoints, int stride)
1546 out = &portaltemppoints[0][0][0];
1547 for (i = 0;i < numpoints;i++)
1549 VectorCopy(points, portaltemppoints[0][i]);
1550 (byte *)points += stride;
1552 numpoints = R_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, (tinyplane_t *)&frustum[0]);
1555 numpoints = R_ClipPolygonToPlane(&portaltemppoints[1][0][0], &portaltemppoints[0][0][0], numpoints, 256, (tinyplane_t *)&frustum[1]);
1558 numpoints = R_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, (tinyplane_t *)&frustum[2]);
1561 return R_ClipPolygonToPlane(&portaltemppoints[1][0][0], &portaltemppoints[0][0][0], numpoints, 256, (tinyplane_t *)&frustum[3]);
1564 void R_TriangleToPlane(vec3_t point1, vec3_t point2, vec3_t point3, tinyplane_t *p)
1567 VectorSubtract(point1, point2, v1);
1568 VectorSubtract(point3, point2, v2);
1569 CrossProduct(v1, v2, p->normal);
1570 // VectorNormalize(p->normal);
1571 VectorNormalizeFast(p->normal);
1572 p->dist = DotProduct(point1, p->normal);
1575 int R_PortalThroughPortalPlanes(tinyplane_t *clipplanes, int clipnumplanes, float *targpoints, int targnumpoints, float *out, int maxpoints)
1578 if (targnumpoints < 3)
1579 return targnumpoints;
1582 numpoints = targnumpoints;
1583 memcpy(&portaltemppoints[0][0][0], targpoints, numpoints * 3 * sizeof(float));
1584 for (i = 0;i < clipnumplanes;i++)
1586 numpoints = R_ClipPolygonToPlane(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints, 256, clipplanes + i);
1589 memcpy(&portaltemppoints[0][0][0], &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
1591 if (numpoints > maxpoints)
1593 memcpy(out, &portaltemppoints[1][0][0], numpoints * 3 * sizeof(float));
1597 #define MAXRECURSIVEPORTALLEAFS 256
1599 //mleaf_t *leafstack[MAXRECURSIVEPORTALLEAFS];
1601 int r_portalframecount;
1603 void R_RecursivePortalWorldNode (mleaf_t *leaf, int firstclipplane, int numclipplanes)
1607 // if (leafstackpos >= MAXRECURSIVEPORTALLEAFS)
1609 // ranoutofleafs = true;
1613 // leafstack[leafstackpos++] = leaf;
1615 if (leaf->visframe != r_framecount)
1618 leaf->visframe = r_framecount;
1619 if (leaf->nummarksurfaces)
1621 msurface_t *surf, **mark, **endmark;
1622 mark = leaf->firstmarksurface;
1623 endmark = mark + leaf->nummarksurfaces;
1627 // make sure surfaces are only processed once
1628 if (surf->worldnodeframe == r_framecount)
1630 surf->worldnodeframe = r_framecount;
1631 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1633 if (surf->flags & SURF_PLANEBACK)
1634 RSurf_DoVisible(surf);
1638 if (!(surf->flags & SURF_PLANEBACK))
1639 RSurf_DoVisible(surf);
1642 while (mark < endmark);
1646 // follow portals into other leafs
1647 for (p = leaf->portals;p;p = p->next)
1649 int newpoints, i, prev;
1652 tinyplane_t *newplanes;
1653 // only flow through portals facing away from the viewer
1654 if (PlaneDiff(r_origin, (&p->plane)) < 0)
1658 for (i = 0;i < leafstackpos;i++)
1659 if (leafstack[i] == p->past)
1661 if (i < leafstackpos)
1663 portalrecursion = true;
1668 newpoints = R_PortalThroughPortalPlanes(&portalplanes[firstclipplane], numclipplanes, (float *) p->points, p->numpoints, &portaltemppoints2[0][0], 256);
1671 else if (firstclipplane + numclipplanes + newpoints > MAXRECURSIVEPORTALPLANES)
1672 ranoutofportalplanes = true;
1675 // go ahead and mark the leaf early, nothing can abort here
1676 if (!r_testvis.value)
1677 p->visframe = r_portalframecount;
1679 // find the center by averaging
1680 VectorClear(center);
1681 for (i = 0;i < newpoints;i++)
1682 VectorAdd(center, portaltemppoints2[i], center);
1683 // ixtable is a 1.0f / N table
1684 VectorScale(center, ixtable[newpoints], center);
1685 // calculate the planes, and make sure the polygon can see it's own center
1686 newplanes = &portalplanes[firstclipplane + numclipplanes];
1687 for (prev = newpoints - 1, i = 0;i < newpoints;prev = i, i++)
1689 // R_TriangleToPlane(r_origin, portaltemppoints2[i], portaltemppoints2[prev], newplanes + i);
1690 VectorSubtract(r_origin, portaltemppoints2[i], v1);
1691 VectorSubtract(portaltemppoints2[prev], portaltemppoints2[i], v2);
1692 CrossProduct(v1, v2, newplanes[i].normal);
1693 VectorNormalizeFast(newplanes[i].normal);
1694 newplanes[i].dist = DotProduct(r_origin, newplanes[i].normal);
1695 if (DotProduct(newplanes[i].normal, center) <= newplanes[i].dist)
1697 // polygon can't see it's own center, discard and use parent portal
1702 R_RecursivePortalWorldNode(p->past, firstclipplane + numclipplanes, newpoints);
1704 R_RecursivePortalWorldNode(p->past, firstclipplane, numclipplanes);
1711 //float viewportalpoints[16*3];
1715 int r_portalframecount = 0;
1718 void R_Portal_Callback(void *data, void *data2)
1720 mleaf_t *leaf = data;
1721 if (!r_testvis.value)
1722 ((mportal_t *)data2)->visframe = r_portalframecount;
1723 if (leaf->visframe != r_framecount)
1726 leaf->visframe = r_framecount;
1731 void R_PVSWorldNode()
1733 int i/*, l*/, k, c, row, numbits, bit, leafnum, numleafs;
1735 msurface_t *surf, **mark, **endmark;
1736 model_t *model = cl.worldmodel;
1738 // mportal_t *portal;
1743 // r_viewleaf->visframe = r_framecount;
1744 if (!r_testvis.value)
1745 r_portalframecount++;
1747 numleafs = model->numleafs;
1748 numbits = numleafs - 1;
1750 in = r_viewleaf->compressed_vis;
1751 row = (numbits + 7) >> 3;
1757 for (i = 0, bit = 1;c;i++, bit <<= 1)
1761 leafnum = (k << 3)+i+1;
1762 if (leafnum >= numleafs)
1765 leaf = &model->leafs[leafnum];
1766 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1768 //for (portal = leaf->portals;portal;portal = portal->next)
1769 // if (DotProduct(r_origin, portal->plane.normal) > portal->plane.dist)
1770 // R_Clip_AddPolygon((float *)portal->points, portal->numpoints, sizeof(mvertex_t), false, R_Portal_Callback, leaf, portal, portal->plane);
1771 //leaf->visframe = r_framecount;
1773 if (leaf->nummarksurfaces)
1775 mark = leaf->firstmarksurface;
1776 endmark = mark + leaf->nummarksurfaces;
1780 // make sure surfaces are only processed once
1781 if (surf->worldnodeframe == r_framecount)
1783 surf->worldnodeframe = r_framecount;
1784 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1786 if (surf->flags & SURF_PLANEBACK)
1788 VectorNegate(surf->plane->normal, plane.normal);
1789 plane.dist = -surf->plane->dist;
1790 for (p = surf->polys;p;p = p->next)
1791 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1796 if (!(surf->flags & SURF_PLANEBACK))
1797 for (p = surf->polys;p;p = p->next)
1798 R_Clip_AddPolygon((float *)p->verts, p->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1801 while (mark < endmark);
1814 if (!r_testvis.value)
1815 r_portalframecount = r_framecount;
1816 portalplanecount = 0;
1817 // leafstackpos = 0;
1818 ranoutofportalplanes = false;
1819 ranoutofportals = false;
1820 ranoutofleafs = false;
1821 portalcantseeself = 0;
1822 portalrecursion = false;
1823 memcpy(&portalplanes[0], &frustum[0], sizeof(tinyplane_t));
1824 memcpy(&portalplanes[1], &frustum[1], sizeof(tinyplane_t));
1825 memcpy(&portalplanes[2], &frustum[2], sizeof(tinyplane_t));
1826 memcpy(&portalplanes[3], &frustum[3], sizeof(tinyplane_t));
1827 R_RecursivePortalWorldNode(r_viewleaf, 0, 4);
1828 if (ranoutofportalplanes)
1829 Con_Printf("R_RecursivePortalWorldNode: ran out of %d plane stack when recursing through portals\n", MAXRECURSIVEPORTALPLANES);
1830 if (ranoutofportals)
1831 Con_Printf("R_RecursivePortalWorldNode: ran out of %d portal stack when recursing through portals\n", MAXRECURSIVEPORTALS);
1833 Con_Printf("R_RecursivePortalWorldNode: ran out of %d leaf stack when recursing through portals\n", MAXRECURSIVEPORTALLEAFS);
1834 // if (portalcantseeself)
1835 // Con_Printf("R_RecursivePortalWorldNode: %d portals could not see themself during clipping\n", portalcantseeself);
1836 if (portalrecursion)
1837 Con_Printf("R_RecursivePortalWorldNode: portal saw into previously encountered leaf??\n");
1841 void R_OldPortalWorldNode (void)
1844 mportal_t *p, *pstack[8192];
1845 msurface_t *surf, **mark, **endmark;
1849 leaf->worldnodeframe = r_framecount;
1854 leaf->visframe = r_framecount;
1856 if (leaf->nummarksurfaces)
1858 mark = leaf->firstmarksurface;
1859 endmark = mark + leaf->nummarksurfaces;
1863 // make sure surfaces are only processed once
1864 if (surf->worldnodeframe == r_framecount)
1866 surf->worldnodeframe = r_framecount;
1867 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1869 if (surf->flags & SURF_PLANEBACK)
1870 surf->visframe = r_framecount;
1874 if (!(surf->flags & SURF_PLANEBACK))
1875 surf->visframe = r_framecount;
1878 while (mark < endmark);
1881 // follow portals into other leafs
1883 for (;p;p = p->next)
1886 if (leaf->worldnodeframe != r_framecount)
1888 leaf->worldnodeframe = r_framecount;
1889 if (leaf->contents != CONTENTS_SOLID)
1891 i = (leaf - cl.worldmodel->leafs) - 1;
1892 if (worldvis[i>>3] & (1<<(i&7)))
1894 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1896 pstack[portalstack++] = p;
1900 p = pstack[--portalstack];
1912 p = r_viewleaf->portals;
1913 for (;p;p = p->next)
1916 if (p->past->worldnodeframe != r_framecount)
1920 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
1924 entity_t clworldent;
1926 void R_DrawSurfaces (void)
1928 msurface_t *surf, *endsurf;
1929 texture_t *t, *currentt;
1930 int vertex = gl_vertex.value;
1932 currententity = &clworldent;
1934 softwaretransformidentity();
1935 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1936 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1937 t = currentt = NULL;
1938 for (;surf < endsurf;surf++)
1940 if (surf->visframe == r_framecount)
1943 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1945 // sky and liquid don't need sorting (skypoly/transpoly)
1946 if (surf->flags & SURF_DRAWSKY)
1947 RSurf_DrawSky(surf, false);
1950 if (currentt != surf->texinfo->texture)
1952 currentt = surf->texinfo->texture;
1953 t = R_TextureAnimation(surf->texinfo->texture);
1955 RSurf_DrawWater(surf, t, false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1960 if (currentt != surf->texinfo->texture)
1962 currentt = surf->texinfo->texture;
1963 t = R_TextureAnimation(surf->texinfo->texture);
1966 RSurf_DrawWallVertex(surf, t, false, false);
1968 RSurf_DrawWall(surf, t, false);
1974 void R_DrawPortals(void)
1976 int drawportals, i, r, g, b;
1977 // mleaf_t *leaf, *endleaf;
1978 mportal_t *portal, *endportal;
1979 mvertex_t *point/*, *endpoint*/;
1980 drawportals = (int)r_drawportals.value;
1981 if (drawportals < 1)
1984 leaf = cl.worldmodel->leafs;
1985 endleaf = leaf + cl.worldmodel->numleafs;
1986 for (;leaf < endleaf;leaf++)
1988 if (leaf->visframe == r_framecount && leaf->portals)
1990 i = leaf - cl.worldmodel->leafs;
1991 r = (i & 0x0007) << 5;
1992 g = (i & 0x0038) << 2;
1993 b = (i & 0x01C0) >> 1;
1994 portal = leaf->portals;
1997 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1998 point = portal->points + portal->numpoints - 1;
1999 endpoint = portal->points;
2000 for (;point >= endpoint;point--)
2001 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2003 portal = portal->next;
2008 portal = cl.worldmodel->portals;
2009 endportal = portal + cl.worldmodel->numportals;
2010 for (;portal < endportal;portal++)
2012 if (portal->visframe == r_portalframecount)
2014 i = portal - cl.worldmodel->portals;
2015 r = (i & 0x0007) << 5;
2016 g = (i & 0x0038) << 2;
2017 b = (i & 0x01C0) >> 1;
2018 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2019 point = portal->points;
2020 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2022 for (i = portal->numpoints - 1;i >= 0;i--)
2023 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
2027 for (i = 0;i < portal->numpoints;i++)
2028 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
2040 void R_DrawWorld (void)
2042 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
2043 vertexworld = gl_vertex.value;
2045 memset (&clworldent, 0, sizeof(clworldent));
2046 clworldent.render.model = cl.worldmodel;
2047 clworldent.render.colormod[0] = clworldent.render.colormod[1] = clworldent.render.colormod[2] = 1;
2048 modelalpha = clworldent.render.alpha = 1;
2049 clworldent.render.scale = 1;
2051 VectorCopy (r_origin, modelorg);
2053 currententity = &clworldent;
2055 softwaretransformidentity(); // LordHavoc: clear transform
2059 if (r_novis.value || r_viewleaf->compressed_vis == NULL)
2060 R_SolidWorldNode ();
2070 =============================================================================
2074 =============================================================================
2077 // returns a texture number and the position inside it
2078 int AllocBlock (int w, int h, short *x, short *y)
2084 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
2086 best = BLOCK_HEIGHT;
2088 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
2092 for (j=0 ; j<w ; j++)
2094 if (allocated[texnum][i+j] >= best)
2096 if (allocated[texnum][i+j] > best2)
2097 best2 = allocated[texnum][i+j];
2100 { // this is a valid spot
2106 if (best + h > BLOCK_HEIGHT)
2109 if (nosubimagefragments || nosubimage)
2111 if (!lightmaps[texnum])
2113 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
2114 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
2117 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
2118 else if (!allocated[texnum][0])
2120 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
2121 memset(blank, 0, sizeof(blank));
2124 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
2125 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2126 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2128 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
2130 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
2134 for (i = 0;i < w;i++)
2135 allocated[texnum][*x + i] = best + h;
2140 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
2149 BuildSurfaceDisplayList
2152 void BuildSurfaceDisplayList (model_t *model, mvertex_t *vertices, msurface_t *fa)
2154 int i, j, lindex, lnumverts;
2160 // reconstruct the polygon
2161 pedges = model->edges;
2162 lnumverts = fa->numedges;
2167 poly = Hunk_AllocName (sizeof(glpolysizeof_t) + lnumverts * sizeof(float[VERTEXSIZE]), "surfaces");
2168 poly->next = fa->polys;
2170 // poly->flags = fa->flags;
2171 poly->numverts = lnumverts;
2173 for (i=0 ; i<lnumverts ; i++)
2175 lindex = model->surfedges[fa->firstedge + i];
2178 vec = vertices[pedges[lindex].v[0]].position;
2180 vec = vertices[pedges[-lindex].v[1]].position;
2182 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
2183 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
2185 VectorCopy (vec, poly->verts[i]);
2186 poly->verts[i][3] = s / fa->texinfo->texture->width;
2187 poly->verts[i][4] = t / fa->texinfo->texture->height;
2190 // lightmap texture coordinates
2192 s -= fa->texturemins[0];
2193 t -= fa->texturemins[1];
2196 // LordHavoc: calc lightmap data offset
2197 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;
2198 poly->verts[i][7] = j;
2199 s += fa->light_s*16;
2200 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
2202 t += fa->light_t*16;
2203 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
2205 poly->verts[i][5] = s;
2206 poly->verts[i][6] = t;
2210 // remove co-linear points - Ed
2213 if (!gl_keeptjunctions.value)
2215 for (i = 0 ; i < lnumverts ; ++i)
2218 float *prev, *this, *next;
2220 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
2221 this = poly->verts[i];
2222 next = poly->verts[(i + 1) % lnumverts];
2224 VectorSubtract( this, prev, v1 );
2225 VectorNormalize( v1 );
2226 VectorSubtract( next, prev, v2 );
2227 VectorNormalize( v2 );
2229 // skip co-linear points
2230 #define COLINEAR_EPSILON 0.001
2231 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
2232 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
2233 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
2236 for (j = i + 1; j < lnumverts; ++j)
2239 for (k = 0; k < VERTEXSIZE; ++k)
2240 poly->verts[j - 1][k] = poly->verts[j][k];
2244 // retry next vertex next time, which is now current vertex
2248 poly->numverts = lnumverts;
2254 ========================
2255 GL_CreateSurfaceLightmap
2256 ========================
2258 void GL_CreateSurfaceLightmap (msurface_t *surf)
2262 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
2265 smax = (surf->extents[0]>>4)+1;
2266 tmax = (surf->extents[1]>>4)+1;
2268 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
2269 if (nosubimage || nosubimagefragments)
2271 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
2272 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
2275 R_BuildLightMap (surf, templight, smax * 4);
2277 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
2281 R_BuildLightMap (surf, templight, smax * 3);
2283 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
2292 Builds the lightmap texture
2293 with all the surfaces from all brush models
2296 void GL_BuildLightmaps (void)
2301 memset (allocated, 0, sizeof(allocated));
2303 r_framecount = 1; // no dlightcache
2305 if (gl_nosubimagefragments.value)
2306 nosubimagefragments = 1;
2308 nosubimagefragments = 0;
2310 if (gl_nosubimage.value)
2315 if (gl_lightmaprgba.value)
2317 lightmaprgba = true;
2322 lightmaprgba = false;
2326 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
2327 // it needs to be aligned on 4 pixel boundaries...
2328 // so I implemented an adjustable lightmap alignment
2329 if (gl_lightmapalign.value < 1)
2330 gl_lightmapalign.value = 1;
2331 if (gl_lightmapalign.value > 16)
2332 gl_lightmapalign.value = 16;
2334 while (lightmapalign < gl_lightmapalign.value)
2335 lightmapalign <<= 1;
2336 gl_lightmapalign.value = lightmapalign;
2337 lightmapalignmask = ~(lightmapalign - 1);
2338 if (nosubimagefragments || nosubimage)
2341 lightmapalignmask = ~0;
2344 if (!lightmap_textures)
2345 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
2347 for (j=1 ; j<MAX_MODELS ; j++)
2349 m = cl.model_precache[j];
2352 if (m->name[0] == '*')
2354 for (i=0 ; i<m->numsurfaces ; i++)
2356 if ( m->surfaces[i].flags & SURF_DRAWTURB )
2358 if ( m->surfaces[i].flags & SURF_DRAWSKY )
2360 GL_CreateSurfaceLightmap (m->surfaces + i);
2361 BuildSurfaceDisplayList (m, m->vertexes, m->surfaces + i);
2365 if (nosubimage || nosubimagefragments)
2369 qglSelectTexture(gl_mtex_enum+1);
2370 for (i = 0;i < MAX_LIGHTMAPS;i++)
2372 if (!allocated[i][0])
2374 lightmapupdate[i][0] = BLOCK_HEIGHT;
2375 lightmapupdate[i][1] = 0;
2378 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
2379 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2380 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2382 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
2384 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
2389 qglSelectTexture(gl_mtex_enum+0);