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 = {0, "gl_lightmapalign", "4"};
43 cvar_t gl_lightmaprgba = {0, "gl_lightmaprgba", "1"};
44 cvar_t gl_nosubimagefragments = {0, "gl_nosubimagefragments", "0"};
45 cvar_t gl_nosubimage = {0, "gl_nosubimage", "0"};
46 cvar_t r_ambient = {0, "r_ambient", "0"};
47 cvar_t gl_vertex = {0, "gl_vertex", "0"};
48 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
49 cvar_t r_drawportals = {0, "r_drawportals", "0"};
50 cvar_t r_testvis = {0, "r_testvis", "0"};
51 cvar_t r_solidworldnode = {0, "r_solidworldnode", "3"};
52 cvar_t r_pvsworldnode = {0, "r_pvsworldnode", "1"};
54 qboolean lightmaprgba, nosubimagefragments, nosubimage;
59 void gl_surf_start(void)
63 void gl_surf_shutdown(void)
67 void gl_surf_newmap(void)
71 void GL_Surf_Init(void)
74 for (i = 0;i < MAX_LIGHTMAPS;i++)
76 Cvar_RegisterVariable(&gl_lightmapalign);
77 Cvar_RegisterVariable(&gl_lightmaprgba);
78 Cvar_RegisterVariable(&gl_nosubimagefragments);
79 Cvar_RegisterVariable(&gl_nosubimage);
80 Cvar_RegisterVariable(&r_ambient);
81 Cvar_RegisterVariable(&gl_vertex);
82 Cvar_RegisterVariable(&r_dlightmap);
83 Cvar_RegisterVariable(&r_drawportals);
84 Cvar_RegisterVariable(&r_testvis);
85 Cvar_RegisterVariable(&r_solidworldnode);
86 Cvar_RegisterVariable(&r_pvsworldnode);
88 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
91 int dlightdivtable[32768];
96 int R_AddDynamicLights (msurface_t *surf)
98 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
101 vec3_t impact, local;
103 // LordHavoc: use 64bit integer... shame it's not very standardized...
104 #if _MSC_VER || __BORLANDC__
112 if (!dlightdivtable[1])
114 dlightdivtable[0] = 4194304;
115 for (s = 1; s < 32768; s++)
116 dlightdivtable[s] = 4194304 / (s << 7);
119 smax = (surf->extents[0] >> 4) + 1;
120 tmax = (surf->extents[1] >> 4) + 1;
122 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
124 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
125 continue; // not lit by this light
127 VectorSubtract (cl_dlights[lnum].origin, currententity->render.origin, local);
128 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
130 // for comparisons to minimum acceptable light
131 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
133 // clamp radius to avoid exceeding 32768 entry division table
134 if (maxdist > 4194304)
138 if (dist2 >= maxdist)
141 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
142 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
143 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
145 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
146 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
148 s = bound(0, impacts, smax * 16) - impacts;
149 t = bound(0, impactt, tmax * 16) - impactt;
150 i = s * s + t * t + dist2;
154 // reduce calculations
155 for (s = 0, i = impacts; s < smax; s++, i -= 16)
156 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
158 maxdist3 = maxdist - (int) (dist * dist);
160 // convert to 8.8 blocklights format and scale up by radius
161 red = cl_dlights[lnum].color[0] * maxdist;
162 green = cl_dlights[lnum].color[1] * maxdist;
163 blue = cl_dlights[lnum].color[2] * maxdist;
167 for (t = 0; t < tmax; t++, i -= 16)
170 // make sure some part of it is visible on this line
173 maxdist2 = maxdist - td;
174 for (s = 0; s < smax; s++)
176 if (sdtable[s] < maxdist2)
178 k = dlightdivtable[(sdtable[s] + td) >> 7];
179 bl[0] += (red * k) >> 9;
180 bl[1] += (green * k) >> 9;
181 bl[2] += (blue * k) >> 9;
195 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
198 stride -= (width*lightmapbytes);
199 // deal with lightmap brightness scale
200 shift = 7 + lightscalebit;
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] >> shift, 255);
208 out[1] = min(in[1] >> shift, 255);
209 out[2] = min(in[2] >> shift, 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] >> shift, 255);
221 out[1] = min(in[1] >> shift, 255);
222 out[2] = min(in[2] >> shift, 255);
232 Combine and scale multiple lightmaps into the 8.8 format in blocklights
235 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
238 int i, j, size, size3;
244 surf->cached_dlight = 0;
245 surf->cached_lightscalebit = lightscalebit;
246 surf->cached_ambient = r_ambient.value;
248 smax = (surf->extents[0]>>4)+1;
249 tmax = (surf->extents[1]>>4)+1;
252 lightmap = surf->samples;
254 // set to full bright if no light data
255 if ((currententity && (currententity->render.effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
258 for (i=0 ; i<size ; i++)
268 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
272 for (i = 0;i < size3;i++)
276 memset(&blocklights[0], 0, size*3*sizeof(int));
278 // add all the lightmaps
281 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
283 scale = d_lightstylevalue[surf->styles[maps]];
284 surf->cached_light[maps] = scale; // 8.8 fraction
286 for (i = 0;i < size3;i++)
287 *bl++ += *lightmap++ * scale;
290 if (r_dlightmap.value && surf->dlightframe == r_framecount)
291 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
294 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
297 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
299 void R_UpdateLightmap(msurface_t *s, int lnum)
302 // upload the new lightmap texture fragment
304 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
305 if (nosubimage || nosubimagefragments)
307 if (lightmapupdate[lnum][0] > s->light_t)
308 lightmapupdate[lnum][0] = s->light_t;
309 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
310 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
312 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
314 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
318 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
319 tmax = (s->extents[1]>>4)+1;
322 R_BuildLightMap (s, templight, smax * 4);
324 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
328 R_BuildLightMap (s, templight, smax * 3);
330 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
340 Returns the proper texture for a given time and base texture
343 texture_t *R_TextureAnimation (texture_t *base)
345 // texture_t *original;
349 if (currententity->render.frame)
351 if (base->alternate_anims)
352 base = base->alternate_anims;
355 if (!base->anim_total)
358 return base->anim_frames[(int)(cl.time*5) % base->anim_total];
363 relative = (int)(cl.time*5) % base->anim_total;
366 while (base->anim_min > relative || base->anim_max <= relative)
368 base = base->anim_next;
371 Con_Printf("R_TextureAnimation: broken cycle");
376 Con_Printf("R_TextureAnimation: infinite cycle");
387 =============================================================
391 =============================================================
397 #include "gl_warp_sin.h"
399 #define TURBSCALE (256.0 / (2 * M_PI))
402 void UploadLightmaps(void)
405 if (nosubimage || nosubimagefragments)
407 for (i = 0;i < MAX_LIGHTMAPS;i++)
409 if (lightmapupdate[i][0] < lightmapupdate[i][1])
413 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
417 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
419 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
424 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]));
426 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]));
430 lightmapupdate[i][0] = BLOCK_HEIGHT;
431 lightmapupdate[i][1] = 0;
436 float wvert[1024*6]; // used by the following functions
438 void RSurf_DrawSky(msurface_t *s, int transform)
444 // LordHavoc: HalfLife maps have freaky skypolys...
448 for (p=s->polys ; p ; p=p->next)
450 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
452 skypoly[currentskypoly].firstvert = currentskyvert;
453 skypoly[currentskypoly++].verts = p->numverts;
456 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
458 softwaretransform(v, skyvert[currentskyvert].v);
464 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
466 VectorCopy(v, skyvert[currentskyvert].v);
474 int RSurf_Light(int *dlightbits, glpoly_t *polys)
476 float cr, cg, cb, radius, radius2, f, *v, *wv;
477 int i, a, b, lit = false;
482 for (a = 0;a < 8;a++)
484 if ((c = dlightbits[a]))
486 for (b = 0, d = 1;c;b++, d <<= 1)
491 light = &cl_dlights[a * 32 + b];
492 lightorigin = light->origin;
493 cr = light->color[0];
494 cg = light->color[1];
495 cb = light->color[2];
496 radius = light->radius*light->radius;
497 radius2 = radius * 256.0f;
499 for (p = polys;p;p = p->next)
501 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
503 f = VectorDistance2(wv, lightorigin);
506 f = radius2 / (f + LIGHTOFFSET);
522 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
525 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
528 // FIXME: make fog texture if water texture is transparent?
530 if (s->dlightframe != r_framecount)
533 // LordHavoc: fast path for no vertex lighting cases
536 if (r_waterripple.value)
538 for (p=s->polys ; p ; p=p->next)
540 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
541 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
543 softwaretransform(v, temp);
544 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);
551 for (p=s->polys ; p ; p=p->next)
553 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
554 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
556 softwaretransform(v, temp);
557 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);
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)
571 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);
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)
581 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);
591 for (p = s->polys;p;p = p->next)
593 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
596 softwaretransform(v, wv);
599 if (r_waterripple.value)
600 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);
601 wv[3] = wv[4] = wv[5] = 128.0f;
605 if (s->dlightframe == r_framecount)
606 RSurf_Light(s->dlightbits, s->polys);
608 for (p=s->polys ; p ; p=p->next)
610 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
611 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
612 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);
618 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
620 int i, lit = false, polys = 0, verts = 0;
625 wallvertcolor_t *outcolor;
626 // check for lightmap modification
628 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
629 || r_ambient.value != s->cached_ambient
630 || lightscalebit != s->cached_lightscalebit
632 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
633 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
634 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
635 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
636 R_UpdateLightmap(s, s->lightmaptexturenum);
637 if (s->dlightframe != r_framecount || r_dlightmap.value)
639 // LordHavoc: fast path version for no vertex lighting cases
640 wp = &wallpoly[currentwallpoly];
641 out = &wallvert[currentwallvert];
642 for (p = s->polys;p;p = p->next)
644 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
646 wp->texnum = (unsigned short) R_GetTexture(t->texture);
647 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
648 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
649 wp->firstvert = currentwallvert;
650 wp->numverts = p->numverts;
654 currentwallvert += p->numverts;
658 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
660 softwaretransform(v, out->vert);
670 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
672 VectorCopy(v, out->vert);
679 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
688 for (p = s->polys;p;p = p->next)
690 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
693 softwaretransform(v, wv);
696 wv[3] = wv[4] = wv[5] = 0.0f;
699 verts += p->numverts;
702 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
704 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
705 lit = RSurf_Light(s->dlightbits, s->polys);
707 wp = &wallpoly[currentwallpoly];
708 out = &wallvert[currentwallvert];
709 outcolor = &wallvertcolor[currentwallvert];
710 currentwallpoly += polys;
711 for (p = s->polys;p;p = p->next)
714 wp->texnum = (unsigned short) R_GetTexture(t->texture);
715 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
716 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
717 wp->firstvert = currentwallvert;
718 wp->numverts = p->numverts;
721 currentwallvert += p->numverts;
722 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
728 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
729 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
730 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
735 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
736 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
737 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
741 out->vert[0] = wv[0];
742 out->vert[1] = wv[1];
743 out->vert[2] = wv[2];
753 // LordHavoc: transparent brush models
754 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
757 float *v, *wv, scale;
760 alpha = (int) (modelalpha * 255.0f);
761 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
763 for (p = s->polys;p;p = p->next)
765 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
768 softwaretransform(v, wv);
771 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
772 if (s->styles[0] != 255)
774 lm = (byte *)((long) s->samples + (int) v[7]);
775 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;
776 if (s->styles[1] != 255)
778 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;
779 if (s->styles[2] != 255)
781 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;
782 if (s->styles[3] != 255)
784 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;
792 if (s->dlightframe == r_framecount)
793 RSurf_Light(s->dlightbits, s->polys);
795 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
797 for (p = s->polys;p;p = p->next)
800 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
801 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
802 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);
808 for (p = s->polys;p;p = p->next)
811 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
812 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
813 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
819 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
821 float bmverts[256*3];
826 void RBrushModelSurf_DoVisible(msurface_t *surf)
828 // float *v, *bmv, *endbmv;
830 // for (p = surf->polys;p;p = p->next)
832 // for (v = p->verts[0], bmv = bmpoints, endbmv = bmv + p->numverts * 3;bmv < endbmv;v += VERTEXSIZE, bmv += 3)
833 // softwaretransform(v, bmv);
834 // if (R_Clip_Polygon(bmpoints, p->numverts, sizeof(float) * 3, surf->flags & SURF_CLIPSOLID))
835 surf->visframe = r_framecount;
840 void RBrushModelSurf_Callback(void *data, void *data2)
842 entity_t *ent = data2;
843 msurface_t *surf = data;
847 // FIXME: implement better dupe prevention in AddPolygon callback code
848 if (ent->render.model->firstmodelsurface != 0)
850 // 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)
851 if (surf->visframe == r_framecount)
855 surf->visframe = r_framecount;
860 modelalpha = ent->render.alpha;
862 softwaretransformforbrushentity (ent);
864 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
866 // sky and liquid don't need sorting (skypoly/transpoly)
867 if (surf->flags & SURF_DRAWSKY)
868 RSurf_DrawSky(surf, true);
870 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), true, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
874 t = R_TextureAnimation(surf->texinfo->texture);
875 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)
876 RSurf_DrawWallVertex(surf, t, true, true);
878 RSurf_DrawWall(surf, t, true);
887 void R_DrawBrushModel (entity_t *e)
899 clmodel = e->render.model;
901 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
904 for (i=0 ; i<3 ; i++)
906 mins[i] = e->render.origin[i] - clmodel->radius;
907 maxs[i] = e->render.origin[i] + clmodel->radius;
913 VectorAdd (e->render.origin, clmodel->mins, mins);
914 VectorAdd (e->render.origin, clmodel->maxs, maxs);
917 if (R_CullBox (mins, maxs))
922 VectorSubtract (r_origin, e->render.origin, modelorg);
926 vec3_t forward, right, up;
928 VectorCopy (modelorg, temp);
929 AngleVectors (e->render.angles, forward, right, up);
930 modelorg[0] = DotProduct (temp, forward);
931 modelorg[1] = -DotProduct (temp, right);
932 modelorg[2] = DotProduct (temp, up);
935 softwaretransformforbrushentity (e);
937 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
940 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
941 s->visframe = r_framecount;
944 // calculate dynamic lighting for bmodel if it's not an instanced model
945 for (i = 0;i < MAX_DLIGHTS;i++)
947 if (!cl_dlights[i].radius)
950 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
951 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
953 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;
956 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
958 if (s->visframe == r_framecount)
960 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
963 for (p = s->polys;p;p = p->next)
965 for (j = 0;j < p->numverts;j++)
966 softwaretransform(&p->verts[j][0], bmverts + j * 3);
967 R_Clip_AddPolygon(bmverts, p->numverts, 3 * sizeof(float), (s->flags & SURF_CLIPSOLID) != 0 && modelalpha == 1, RBrushModelSurf_Callback, s, e, NULL);
972 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
974 // sky and liquid don't need sorting (skypoly/transpoly)
975 if (s->flags & SURF_DRAWSKY)
976 RSurf_DrawSky(s, true);
978 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
982 texture_t *t = R_TextureAnimation(s->texinfo->texture);
983 if (vertexlit || s->texinfo->texture->transparent)
984 RSurf_DrawWallVertex(s, t, true, true);
986 RSurf_DrawWall(s, t, true);
995 =============================================================
999 =============================================================
1003 static byte *worldvis;
1005 void R_MarkLeaves (void)
1007 static float noviscache;
1008 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1011 r_oldviewleaf = r_viewleaf;
1012 noviscache = r_novis.value;
1014 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1018 void RSurf_Callback(void *data, void *junk)
1020 ((msurface_t *)data)->visframe = r_framecount;
1024 void RSurf_Callback(void *data, void *junk)
1026 msurface_t *surf = data;
1029 // if (surf->visframe == r_framecount)
1032 surf->visframe = r_framecount;
1036 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1038 // sky and liquid don't need sorting (skypoly/transpoly)
1039 if (surf->flags & SURF_DRAWSKY)
1040 RSurf_DrawSky(surf, false);
1042 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1046 t = R_TextureAnimation(surf->texinfo->texture);
1048 RSurf_DrawWallVertex(surf, t, false, false);
1050 RSurf_DrawWall(surf, t, false);
1056 mleaf_t *r_oldviewleaf;
1057 int r_markvisframecount = 0;
1059 void R_MarkLeaves (void)
1061 static float noviscache;
1064 msurface_t *surf, **mark, **endmark;
1065 model_t *model = cl.worldmodel;
1066 // mportal_t *portal;
1071 // ignore testvis if the map just changed
1072 if (r_testvis.value && model->nodes->markvisframe == r_markvisframecount)
1075 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1078 r_oldviewleaf = r_viewleaf;
1079 noviscache = r_novis.value;
1081 if ((in = r_viewleaf->compressed_vis))
1083 row = (model->numleafs+7)>>3;
1085 if (!r_testvis.value)
1086 r_markvisframecount++;
1088 // LordHavoc: mark the root node as visible, it will terminate all other ascensions
1089 model->nodes->markvisframe = r_markvisframecount;
1097 l = model->numleafs - (k << 3);
1100 for (i=0 ; i<l ; i++)
1104 leaf = &model->leafs[(k << 3)+i+1];
1105 node = (mnode_t *)leaf;
1108 node->markvisframe = r_markvisframecount;
1109 node = node->parent;
1111 while (node->markvisframecount != r_markvisframecount);
1122 // LordHavoc: no vis data, mark everything as visible
1123 model->nodes->markvisframe = r_markvisframecount;
1125 for (i = 1;i < model->numleafs;i++)
1127 node = (mnode_t *)&model->leafs[i];
1130 node->markvisframe = r_markvisframecount;
1131 node = node->parent;
1133 while (node->markvisframecount != r_markvisframecount);
1139 void R_SolidWorldNode (void)
1141 if ((int) r_solidworldnode.value == 3)
1144 mportal_t *p, *pstack[8192];
1145 msurface_t *surf, **mark, **endmark;
1151 leaf->worldnodeframe = r_framecount;
1156 leaf->visframe = r_framecount;
1158 if (leaf->nummarksurfaces)
1160 mark = leaf->firstmarksurface;
1161 endmark = mark + leaf->nummarksurfaces;
1167 // make sure surfaces are only processed once
1168 if (surf->worldnodeframe == r_framecount)
1170 surf->worldnodeframe = r_framecount;
1171 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1173 if (surf->flags & SURF_PLANEBACK)
1175 VectorNegate(surf->plane->normal, plane.normal);
1176 plane.dist = -surf->plane->dist;
1177 for (poly = surf->polys;poly;poly = poly->next)
1178 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1183 if (!(surf->flags & SURF_PLANEBACK))
1184 for (poly = surf->polys;poly;poly = poly->next)
1185 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1188 while (mark < endmark);
1195 // make sure surfaces are only processed once
1196 if (surf->worldnodeframe == r_framecount)
1198 surf->worldnodeframe = r_framecount;
1199 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1201 if (surf->flags & SURF_PLANEBACK)
1202 surf->visframe = r_framecount;
1206 if (!(surf->flags & SURF_PLANEBACK))
1207 surf->visframe = r_framecount;
1210 while (mark < endmark);
1214 // follow portals into other leafs
1216 for (;p;p = p->next)
1218 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1221 if (leaf->worldnodeframe != r_framecount)
1223 leaf->worldnodeframe = r_framecount;
1224 if (leaf->contents != CONTENTS_SOLID)
1226 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1228 pstack[portalstack++] = p;
1232 p = pstack[--portalstack];
1242 else if ((int) r_solidworldnode.value == 2)
1244 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1245 int nodestackpos = 0;
1249 if (R_NotCulledBox(node->mins, node->maxs))
1253 if (node->numsurfaces)
1255 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1257 if (PlaneDiff (r_origin, node->plane) < 0)
1259 for (;surf < surfend;surf++)
1261 if (surf->flags & SURF_PLANEBACK)
1263 VectorNegate(surf->plane->normal, plane.normal);
1264 plane.dist = -surf->plane->dist;
1265 for (poly = surf->polys;poly;poly = poly->next)
1266 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1272 for (;surf < surfend;surf++)
1274 if (!(surf->flags & SURF_PLANEBACK))
1275 for (poly = surf->polys;poly;poly = poly->next)
1276 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1283 if (node->numsurfaces)
1285 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1286 if (PlaneDiff (r_origin, node->plane) < 0)
1288 for (;surf < surfend;surf++)
1290 if (surf->flags & SURF_PLANEBACK)
1291 surf->visframe = r_framecount;
1296 for (;surf < surfend;surf++)
1298 if (!(surf->flags & SURF_PLANEBACK))
1299 surf->visframe = r_framecount;
1305 // recurse down the children
1306 if (node->children[0]->contents >= 0)
1308 if (node->children[1]->contents >= 0)
1310 if (nodestackpos < 8192)
1311 nodestack[nodestackpos++] = node->children[1];
1312 node = node->children[0];
1316 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1317 node = node->children[0];
1322 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1323 if (node->children[1]->contents >= 0)
1325 node = node->children[1];
1328 else if (nodestackpos > 0)
1330 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1331 node = nodestack[--nodestackpos];
1336 else if (nodestackpos > 0)
1338 node = nodestack[--nodestackpos];
1342 else if ((int) r_solidworldnode.value == 1 && r_ser.value)
1345 msurface_t *surf, *endsurf;
1348 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1349 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1350 for (;surf < endsurf;surf++)
1352 if (PlaneDiff(r_origin, surf->plane) < 0)
1354 if (surf->flags & SURF_PLANEBACK)
1356 VectorNegate(surf->plane->normal, plane.normal);
1357 plane.dist = -surf->plane->dist;
1358 for (poly = surf->polys;poly;poly = poly->next)
1359 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1364 if (!(surf->flags & SURF_PLANEBACK))
1365 for (poly = surf->polys;poly;poly = poly->next)
1366 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)&surf->plane);
1374 msurface_t *surf, **mark, **endmark;
1378 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
1380 if (R_CullBox(leaf->mins, leaf->maxs))
1382 leaf->visframe = r_framecount;
1384 if (leaf->nummarksurfaces)
1386 // if (R_CullBox(leaf->mins, leaf->maxs))
1389 if (leaf->nummarksurfaces)
1391 mark = leaf->firstmarksurface;
1392 endmark = mark + leaf->nummarksurfaces;
1398 // make sure surfaces are only processed once
1399 if (surf->worldnodeframe == r_framecount)
1401 surf->worldnodeframe = r_framecount;
1402 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1404 if (surf->flags & SURF_PLANEBACK)
1406 VectorNegate(surf->plane->normal, plane.normal);
1407 plane.dist = -surf->plane->dist;
1408 for (poly = surf->polys;poly;poly = poly->next)
1409 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1414 if (!(surf->flags & SURF_PLANEBACK))
1415 for (poly = surf->polys;poly;poly = poly->next)
1416 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1419 while (mark < endmark);
1426 // make sure surfaces are only processed once
1427 if (surf->worldnodeframe == r_framecount)
1429 surf->worldnodeframe = r_framecount;
1430 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1432 if (surf->flags & SURF_PLANEBACK)
1433 surf->visframe = r_framecount;
1437 if (!(surf->flags & SURF_PLANEBACK))
1438 surf->visframe = r_framecount;
1441 while (mark < endmark);
1450 void RSurf_Callback(void *data, void *junk)
1452 ((msurface_t *)data)->visframe = r_framecount;
1455 int R_FrustumTestPolygon(float *points, int numpoints, int stride);
1457 void RSurf_DoVisible(msurface_t *surf)
1460 for (p = surf->polys;p;p = p->next)
1461 if (R_FrustumTestPolygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float)) >= 3)
1462 // R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), true, RSurf_Callback, surf, 1);
1463 // if (R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID))
1464 surf->visframe = r_framecount;
1468 //mleaf_t *llistbuffer[32768], *l, **llist;
1471 void RSurfLeaf_Callback(void *data)
1473 int portalstackpos = 0;
1475 mportal_t *p, *portalstack[32768];
1476 msurface_t *surf, **mark, **endmark;
1481 if (leaf->visframe == r_framecount)
1483 leaf->visframe = r_framecount;
1487 if (leaf->nummarksurfaces)
1489 mark = leaf->firstmarksurface;
1490 endmark = mark + leaf->nummarksurfaces;
1494 // make sure surfaces are only processed once
1495 if (surf->worldnodeframe == r_framecount)
1497 surf->worldnodeframe = r_framecount;
1498 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1500 if (surf->flags & SURF_PLANEBACK)
1501 RSurf_DoVisible(surf);
1505 if (!(surf->flags & SURF_PLANEBACK))
1506 RSurf_DoVisible(surf);
1509 while (mark < endmark);
1512 // follow portals into other leafs
1513 for (p = leaf->portals;p;p = p->next)
1515 if (p->past->visframe != r_framecount && DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1517 // R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3, RSurfLeaf_Callback, p->past, 1);
1518 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1519 portalstack[portalstackpos++] = p;
1523 while(portalstackpos);
1524 RSurfLeaf_Callback(p->past);
1525 // 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
1530 // experimental and inferior to the other in recursion depth allowances
1531 void R_PortalWorldNode (void)
1535 msurface_t *surf, **mark, **endmark;
1536 mleaf_t *leaf, *llistbuffer[32768], **l, **llist;
1539 leaf->visframe = r_framecount;
1540 l = llist = &llistbuffer[0];
1541 *llist++ = r_viewleaf;
1548 if (leaf->nummarksurfaces)
1550 mark = leaf->firstmarksurface;
1551 endmark = mark + leaf->nummarksurfaces;
1555 // make sure surfaces are only processed once
1556 if (surf->worldnodeframe == r_framecount)
1558 surf->worldnodeframe = r_framecount;
1559 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1561 if (surf->flags & SURF_PLANEBACK)
1562 RSurf_DoVisible(surf);
1566 if (!(surf->flags & SURF_PLANEBACK))
1567 RSurf_DoVisible(surf);
1570 while (mark < endmark);
1573 // follow portals into other leafs
1574 for (p = leaf->portals;p;p = p->next)
1576 if (p->past->visframe != r_framecount)
1578 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1580 p->past->visframe = r_framecount;
1586 // for (p = leaf->portals;p;p = p->next)
1589 // if (leaf->worldnodeframe != r_framecount)
1591 // leaf->worldnodeframe = r_framecount;
1592 // i = (leaf - cl.worldmodel->leafs) - 1;
1593 // if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1601 // p = r_viewleaf->portals;
1602 // for (;p;p = p->next)
1605 // if (p->past->worldnodeframe != r_framecount)
1609 // Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1614 int r_portalframecount = 0;
1617 void R_Portal_Callback(void *data, void *data2)
1619 mleaf_t *leaf = data;
1620 if (!r_testvis.value)
1621 ((mportal_t *)data2)->visframe = r_portalframecount;
1622 if (leaf->visframe != r_framecount)
1625 leaf->visframe = r_framecount;
1630 void R_PVSWorldNode()
1632 if (r_pvsworldnode.value == 1)
1635 mportal_t *p, *pstack[8192];
1636 msurface_t *surf, **mark, **endmark;
1642 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1645 leaf->worldnodeframe = r_framecount;
1650 leaf->visframe = r_framecount;
1652 if (leaf->nummarksurfaces)
1654 mark = leaf->firstmarksurface;
1655 endmark = mark + leaf->nummarksurfaces;
1661 // make sure surfaces are only processed once
1662 if (surf->worldnodeframe == r_framecount)
1664 surf->worldnodeframe = r_framecount;
1665 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1667 if (surf->flags & SURF_PLANEBACK)
1669 VectorNegate(surf->plane->normal, plane.normal);
1670 plane.dist = -surf->plane->dist;
1671 for (poly = surf->polys;poly;poly = poly->next)
1672 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1677 if (!(surf->flags & SURF_PLANEBACK))
1678 for (poly = surf->polys;poly;poly = poly->next)
1679 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1682 while (mark < endmark);
1689 // make sure surfaces are only processed once
1690 if (surf->worldnodeframe == r_framecount)
1692 surf->worldnodeframe = r_framecount;
1693 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1695 if (surf->flags & SURF_PLANEBACK)
1696 surf->visframe = r_framecount;
1700 if (!(surf->flags & SURF_PLANEBACK))
1701 surf->visframe = r_framecount;
1704 while (mark < endmark);
1708 // follow portals into other leafs
1710 for (;p;p = p->next)
1712 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1715 if (leaf->worldnodeframe != r_framecount)
1717 leaf->worldnodeframe = r_framecount;
1718 if (leaf->contents != CONTENTS_SOLID)
1720 i = (leaf - cl.worldmodel->leafs) - 1;
1721 if (worldvis[i>>3] & (1<<(i&7)))
1723 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1725 pstack[portalstack++] = p;
1729 p = pstack[--portalstack];
1742 int i/*, l*/, k, c, row, numbits, bit, leafnum, numleafs;
1744 msurface_t *surf, **mark, **endmark;
1745 model_t *model = cl.worldmodel;
1747 // mportal_t *portal;
1752 // r_viewleaf->visframe = r_framecount;
1753 if (!r_testvis.value)
1754 r_portalframecount++;
1756 numleafs = model->numleafs;
1759 in = r_viewleaf->compressed_vis;
1760 row = (numbits + 7) >> 3;
1766 for (i = 0, bit = 1;c;i++, bit <<= 1)
1770 leafnum = (k << 3)+i+1;
1771 if (leafnum > numleafs)
1774 leaf = &model->leafs[leafnum];
1775 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1777 //for (portal = leaf->portals;portal;portal = portal->next)
1778 // if (DotProduct(r_origin, portal->plane.normal) > portal->plane.dist)
1779 // R_Clip_AddPolygon((float *)portal->points, portal->numpoints, sizeof(mvertex_t), false, R_Portal_Callback, leaf, portal, portal->plane);
1780 //leaf->visframe = r_framecount;
1782 if (leaf->nummarksurfaces)
1784 mark = leaf->firstmarksurface;
1785 endmark = mark + leaf->nummarksurfaces;
1791 // make sure surfaces are only processed once
1792 if (surf->worldnodeframe == r_framecount)
1794 surf->worldnodeframe = r_framecount;
1795 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1797 if (surf->flags & SURF_PLANEBACK)
1799 VectorNegate(surf->plane->normal, plane.normal);
1800 plane.dist = -surf->plane->dist;
1801 for (poly = surf->polys;poly;poly = poly->next)
1802 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1807 if (!(surf->flags & SURF_PLANEBACK))
1808 for (poly = surf->polys;poly;poly = poly->next)
1809 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1812 while (mark < endmark);
1819 // make sure surfaces are only processed once
1820 if (surf->worldnodeframe == r_framecount)
1822 surf->worldnodeframe = r_framecount;
1823 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1825 if (surf->flags & SURF_PLANEBACK)
1826 surf->visframe = r_framecount;
1830 if (!(surf->flags & SURF_PLANEBACK))
1831 surf->visframe = r_framecount;
1834 while (mark < endmark);
1848 entity_t clworldent;
1850 void R_DrawSurfaces (void)
1852 msurface_t *surf, *endsurf;
1853 texture_t *t, *currentt;
1854 int vertex = gl_vertex.value;
1856 currententity = &clworldent;
1858 softwaretransformidentity();
1859 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1860 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1861 t = currentt = NULL;
1862 for (;surf < endsurf;surf++)
1864 if (surf->visframe == r_framecount)
1867 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1869 if (surf->flags & SURF_DRAWSKY)
1870 RSurf_DrawSky(surf, false);
1873 if (currentt != surf->texinfo->texture)
1875 currentt = surf->texinfo->texture;
1876 t = R_TextureAnimation(surf->texinfo->texture);
1878 RSurf_DrawWater(surf, t, false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1883 if (currentt != surf->texinfo->texture)
1885 currentt = surf->texinfo->texture;
1886 t = R_TextureAnimation(surf->texinfo->texture);
1889 RSurf_DrawWallVertex(surf, t, false, false);
1891 RSurf_DrawWall(surf, t, false);
1897 void R_DrawPortals(void)
1899 int drawportals, i, r, g, b;
1900 // mleaf_t *leaf, *endleaf;
1901 mportal_t *portal, *endportal;
1902 mvertex_t *point/*, *endpoint*/;
1903 drawportals = (int)r_drawportals.value;
1904 if (drawportals < 1)
1907 leaf = cl.worldmodel->leafs;
1908 endleaf = leaf + cl.worldmodel->numleafs;
1909 for (;leaf < endleaf;leaf++)
1911 if (leaf->visframe == r_framecount && leaf->portals)
1913 i = leaf - cl.worldmodel->leafs;
1914 r = (i & 0x0007) << 5;
1915 g = (i & 0x0038) << 2;
1916 b = (i & 0x01C0) >> 1;
1917 portal = leaf->portals;
1920 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1921 point = portal->points + portal->numpoints - 1;
1922 endpoint = portal->points;
1923 for (;point >= endpoint;point--)
1924 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1926 portal = portal->next;
1931 portal = cl.worldmodel->portals;
1932 endportal = portal + cl.worldmodel->numportals;
1933 for (;portal < endportal;portal++)
1935 if (portal->visframe == r_portalframecount)
1937 i = portal - cl.worldmodel->portals;
1938 r = (i & 0x0007) << 5;
1939 g = (i & 0x0038) << 2;
1940 b = (i & 0x01C0) >> 1;
1941 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1942 point = portal->points;
1943 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1945 for (i = portal->numpoints - 1;i >= 0;i--)
1946 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1950 for (i = 0;i < portal->numpoints;i++)
1951 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1963 void R_DrawWorld (void)
1965 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1966 vertexworld = gl_vertex.value;
1968 memset (&clworldent, 0, sizeof(clworldent));
1969 clworldent.render.model = cl.worldmodel;
1970 clworldent.render.colormod[0] = clworldent.render.colormod[1] = clworldent.render.colormod[2] = 1;
1971 modelalpha = clworldent.render.alpha = 1;
1972 clworldent.render.scale = 1;
1974 VectorCopy (r_origin, modelorg);
1976 currententity = &clworldent;
1978 softwaretransformidentity(); // LordHavoc: clear transform
1982 if (r_novis.value || r_viewleaf->compressed_vis == NULL)
1983 R_SolidWorldNode ();
1993 =============================================================================
1997 =============================================================================
2000 // returns a texture number and the position inside it
2001 int AllocBlock (int w, int h, short *x, short *y)
2007 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
2009 best = BLOCK_HEIGHT;
2011 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
2015 for (j=0 ; j<w ; j++)
2017 if (allocated[texnum][i+j] >= best)
2019 if (allocated[texnum][i+j] > best2)
2020 best2 = allocated[texnum][i+j];
2023 { // this is a valid spot
2029 if (best + h > BLOCK_HEIGHT)
2032 if (nosubimagefragments || nosubimage)
2034 if (!lightmaps[texnum])
2036 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
2037 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
2040 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
2041 else if (!allocated[texnum][0])
2043 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
2044 memset(blank, 0, sizeof(blank));
2047 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
2048 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2049 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2051 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
2053 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
2057 for (i = 0;i < w;i++)
2058 allocated[texnum][*x + i] = best + h;
2063 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
2072 BuildSurfaceDisplayList
2075 void BuildSurfaceDisplayList (model_t *model, mvertex_t *vertices, msurface_t *fa)
2077 int i, j, lindex, lnumverts;
2083 // reconstruct the polygon
2084 pedges = model->edges;
2085 lnumverts = fa->numedges;
2090 poly = Hunk_AllocName (sizeof(glpolysizeof_t) + lnumverts * sizeof(float[VERTEXSIZE]), "surfaces");
2091 poly->next = fa->polys;
2093 // poly->flags = fa->flags;
2094 poly->numverts = lnumverts;
2096 for (i=0 ; i<lnumverts ; i++)
2098 lindex = model->surfedges[fa->firstedge + i];
2101 vec = vertices[pedges[lindex].v[0]].position;
2103 vec = vertices[pedges[-lindex].v[1]].position;
2105 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
2106 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
2108 VectorCopy (vec, poly->verts[i]);
2109 poly->verts[i][3] = s / fa->texinfo->texture->width;
2110 poly->verts[i][4] = t / fa->texinfo->texture->height;
2113 // lightmap texture coordinates
2115 s -= fa->texturemins[0];
2116 t -= fa->texturemins[1];
2119 // LordHavoc: calc lightmap data offset
2120 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;
2121 poly->verts[i][7] = j;
2122 s += fa->light_s*16;
2123 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
2125 t += fa->light_t*16;
2126 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
2128 poly->verts[i][5] = s;
2129 poly->verts[i][6] = t;
2133 // remove co-linear points - Ed
2136 if (!gl_keeptjunctions.value)
2138 for (i = 0 ; i < lnumverts ; ++i)
2141 float *prev, *this, *next;
2143 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
2144 this = poly->verts[i];
2145 next = poly->verts[(i + 1) % lnumverts];
2147 VectorSubtract( this, prev, v1 );
2148 VectorNormalize( v1 );
2149 VectorSubtract( next, prev, v2 );
2150 VectorNormalize( v2 );
2152 // skip co-linear points
2153 #define COLINEAR_EPSILON 0.001
2154 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
2155 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
2156 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
2159 for (j = i + 1; j < lnumverts; ++j)
2162 for (k = 0; k < VERTEXSIZE; ++k)
2163 poly->verts[j - 1][k] = poly->verts[j][k];
2167 // retry next vertex next time, which is now current vertex
2171 poly->numverts = lnumverts;
2177 ========================
2178 GL_CreateSurfaceLightmap
2179 ========================
2181 void GL_CreateSurfaceLightmap (msurface_t *surf)
2185 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
2188 smax = (surf->extents[0]>>4)+1;
2189 tmax = (surf->extents[1]>>4)+1;
2191 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
2192 if (nosubimage || nosubimagefragments)
2194 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
2195 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
2198 R_BuildLightMap (surf, templight, smax * 4);
2200 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
2204 R_BuildLightMap (surf, templight, smax * 3);
2206 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
2215 Builds the lightmap texture
2216 with all the surfaces from all brush models
2219 void GL_BuildLightmaps (void)
2224 memset (allocated, 0, sizeof(allocated));
2226 r_framecount = 1; // no dlightcache
2228 if (gl_nosubimagefragments.value)
2229 nosubimagefragments = 1;
2231 nosubimagefragments = 0;
2233 if (gl_nosubimage.value)
2238 if (gl_lightmaprgba.value)
2240 lightmaprgba = true;
2245 lightmaprgba = false;
2249 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
2250 // it needs to be aligned on 4 pixel boundaries...
2251 // so I implemented an adjustable lightmap alignment
2252 if (gl_lightmapalign.value < 1)
2253 gl_lightmapalign.value = 1;
2254 if (gl_lightmapalign.value > 16)
2255 gl_lightmapalign.value = 16;
2257 while (lightmapalign < gl_lightmapalign.value)
2258 lightmapalign <<= 1;
2259 gl_lightmapalign.value = lightmapalign;
2260 lightmapalignmask = ~(lightmapalign - 1);
2261 if (nosubimagefragments || nosubimage)
2264 lightmapalignmask = ~0;
2267 if (!lightmap_textures)
2268 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
2270 for (j=1 ; j<MAX_MODELS ; j++)
2272 m = cl.model_precache[j];
2275 if (m->name[0] == '*')
2277 for (i=0 ; i<m->numsurfaces ; i++)
2279 if ( m->surfaces[i].flags & SURF_DRAWTURB )
2281 if ( m->surfaces[i].flags & SURF_DRAWSKY )
2283 GL_CreateSurfaceLightmap (m->surfaces + i);
2284 BuildSurfaceDisplayList (m, m->vertexes, m->surfaces + i);
2288 if (nosubimage || nosubimagefragments)
2292 qglSelectTexture(gl_mtex_enum+1);
2293 for (i = 0;i < MAX_LIGHTMAPS;i++)
2295 if (!allocated[i][0])
2297 lightmapupdate[i][0] = BLOCK_HEIGHT;
2298 lightmapupdate[i][1] = 0;
2301 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
2302 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2303 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2305 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
2307 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
2312 qglSelectTexture(gl_mtex_enum+0);