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);
201 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
202 // the image is brightened as a processing pass
205 for (i = 0;i < height;i++, out += stride)
207 for (j = 0;j < width;j++, in += 3, out += 4)
209 out[0] = min(in[0] >> 8, 255);
210 out[1] = min(in[1] >> 8, 255);
211 out[2] = min(in[2] >> 8, 255);
218 for (i = 0;i < height;i++, out += stride)
220 for (j = 0;j < width;j++, in += 3, out += 3)
222 out[0] = min(in[0] >> 8, 255);
223 out[1] = min(in[1] >> 8, 255);
224 out[2] = min(in[2] >> 8, 255);
233 for (i = 0;i < height;i++, out += stride)
235 for (j = 0;j < width;j++, in += 3, out += 4)
237 out[0] = min(in[0] >> 7, 255);
238 out[1] = min(in[1] >> 7, 255);
239 out[2] = min(in[2] >> 7, 255);
246 for (i = 0;i < height;i++, out += stride)
248 for (j = 0;j < width;j++, in += 3, out += 3)
250 out[0] = min(in[0] >> 7, 255);
251 out[1] = min(in[1] >> 7, 255);
252 out[2] = min(in[2] >> 7, 255);
263 Combine and scale multiple lightmaps into the 8.8 format in blocklights
266 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
269 int i, j, size, size3;
275 surf->cached_dlight = 0;
276 surf->cached_lighthalf = lighthalf;
277 surf->cached_ambient = r_ambient.value;
279 smax = (surf->extents[0]>>4)+1;
280 tmax = (surf->extents[1]>>4)+1;
283 lightmap = surf->samples;
285 // set to full bright if no light data
286 if ((currententity && (currententity->render.effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
289 for (i=0 ; i<size ; i++)
299 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
303 for (i = 0;i < size3;i++)
307 memset(&blocklights[0], 0, size*3*sizeof(int));
309 // add all the lightmaps
312 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
314 scale = d_lightstylevalue[surf->styles[maps]];
315 surf->cached_light[maps] = scale; // 8.8 fraction
317 for (i = 0;i < size3;i++)
318 *bl++ += *lightmap++ * scale;
321 if (r_dlightmap.value && surf->dlightframe == r_framecount)
322 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
325 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
328 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
330 void R_UpdateLightmap(msurface_t *s, int lnum)
333 // upload the new lightmap texture fragment
335 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
336 if (nosubimage || nosubimagefragments)
338 if (lightmapupdate[lnum][0] > s->light_t)
339 lightmapupdate[lnum][0] = s->light_t;
340 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
341 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
343 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
345 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
349 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
350 tmax = (s->extents[1]>>4)+1;
353 R_BuildLightMap (s, templight, smax * 4);
355 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
359 R_BuildLightMap (s, templight, smax * 3);
361 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
371 Returns the proper texture for a given time and base texture
374 texture_t *R_TextureAnimation (texture_t *base)
376 // texture_t *original;
380 if (currententity->render.frame)
382 if (base->alternate_anims)
383 base = base->alternate_anims;
386 if (!base->anim_total)
389 return base->anim_frames[(int)(cl.time*5) % base->anim_total];
394 relative = (int)(cl.time*5) % base->anim_total;
397 while (base->anim_min > relative || base->anim_max <= relative)
399 base = base->anim_next;
402 Con_Printf("R_TextureAnimation: broken cycle");
407 Con_Printf("R_TextureAnimation: infinite cycle");
418 =============================================================
422 =============================================================
428 #include "gl_warp_sin.h"
430 #define TURBSCALE (256.0 / (2 * M_PI))
433 void UploadLightmaps(void)
436 if (nosubimage || nosubimagefragments)
438 for (i = 0;i < MAX_LIGHTMAPS;i++)
440 if (lightmapupdate[i][0] < lightmapupdate[i][1])
444 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
448 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
450 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
455 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]));
457 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]));
461 lightmapupdate[i][0] = BLOCK_HEIGHT;
462 lightmapupdate[i][1] = 0;
467 float wvert[1024*6]; // used by the following functions
469 void RSurf_DrawSky(msurface_t *s, int transform)
475 // LordHavoc: HalfLife maps have freaky skypolys...
479 for (p=s->polys ; p ; p=p->next)
481 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
483 skypoly[currentskypoly].firstvert = currentskyvert;
484 skypoly[currentskypoly++].verts = p->numverts;
487 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
489 softwaretransform(v, skyvert[currentskyvert].v);
495 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
497 VectorCopy(v, skyvert[currentskyvert].v);
505 int RSurf_Light(int *dlightbits, glpoly_t *polys)
507 float cr, cg, cb, radius, radius2, f, *v, *wv;
508 int i, a, b, lit = false;
513 for (a = 0;a < 8;a++)
515 if ((c = dlightbits[a]))
517 for (b = 0, d = 1;c;b++, d <<= 1)
522 light = &cl_dlights[a * 32 + b];
523 lightorigin = light->origin;
524 cr = light->color[0];
525 cg = light->color[1];
526 cb = light->color[2];
527 radius = light->radius*light->radius;
528 radius2 = radius * 256.0f;
530 for (p = polys;p;p = p->next)
532 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
534 f = VectorDistance2(wv, lightorigin);
537 f = radius2 / (f + LIGHTOFFSET);
553 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
556 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
559 // FIXME: make fog texture if water texture is transparent?
561 if (s->dlightframe != r_framecount)
564 // LordHavoc: fast path for no vertex lighting cases
567 if (r_waterripple.value)
569 for (p=s->polys ; p ; p=p->next)
571 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
572 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
574 softwaretransform(v, temp);
575 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);
582 for (p=s->polys ; p ; p=p->next)
584 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
585 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
587 softwaretransform(v, temp);
588 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);
596 if (r_waterripple.value)
598 for (p=s->polys ; p ; p=p->next)
600 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
601 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
602 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);
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)
612 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);
622 for (p = s->polys;p;p = p->next)
624 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
627 softwaretransform(v, wv);
630 if (r_waterripple.value)
631 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);
632 wv[3] = wv[4] = wv[5] = 128.0f;
636 if (s->dlightframe == r_framecount)
637 RSurf_Light(s->dlightbits, s->polys);
639 for (p=s->polys ; p ; p=p->next)
641 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
642 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
643 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);
649 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
651 int i, lit = false, polys = 0, verts = 0;
656 wallvertcolor_t *outcolor;
657 // check for lightmap modification
659 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
660 || r_ambient.value != s->cached_ambient
661 || lighthalf != s->cached_lighthalf
663 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
664 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
665 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
666 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
667 R_UpdateLightmap(s, s->lightmaptexturenum);
668 if (s->dlightframe != r_framecount || r_dlightmap.value)
670 // LordHavoc: fast path version for no vertex lighting cases
671 wp = &wallpoly[currentwallpoly];
672 out = &wallvert[currentwallvert];
673 for (p = s->polys;p;p = p->next)
675 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
677 wp->texnum = (unsigned short) R_GetTexture(t->texture);
678 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
679 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
680 wp->firstvert = currentwallvert;
681 wp->numverts = p->numverts;
685 currentwallvert += p->numverts;
689 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
691 softwaretransform(v, out->vert);
701 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
703 VectorCopy(v, out->vert);
710 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
719 for (p = s->polys;p;p = p->next)
721 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
724 softwaretransform(v, wv);
727 wv[3] = wv[4] = wv[5] = 0.0f;
730 verts += p->numverts;
733 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
735 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
736 lit = RSurf_Light(s->dlightbits, s->polys);
738 wp = &wallpoly[currentwallpoly];
739 out = &wallvert[currentwallvert];
740 outcolor = &wallvertcolor[currentwallvert];
741 currentwallpoly += polys;
742 for (p = s->polys;p;p = p->next)
745 wp->texnum = (unsigned short) R_GetTexture(t->texture);
746 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
747 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
748 wp->firstvert = currentwallvert;
749 wp->numverts = p->numverts;
752 currentwallvert += p->numverts;
753 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
759 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
760 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
761 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
766 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
767 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
768 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
772 out->vert[0] = wv[0];
773 out->vert[1] = wv[1];
774 out->vert[2] = wv[2];
784 // LordHavoc: transparent brush models
785 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
788 float *v, *wv, scale;
791 alpha = (int) (modelalpha * 255.0f);
792 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
794 for (p = s->polys;p;p = p->next)
796 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
799 softwaretransform(v, wv);
802 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
803 if (s->styles[0] != 255)
805 lm = (byte *)((long) s->samples + (int) v[7]);
806 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;
807 if (s->styles[1] != 255)
809 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;
810 if (s->styles[2] != 255)
812 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;
813 if (s->styles[3] != 255)
815 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;
823 if (s->dlightframe == r_framecount)
824 RSurf_Light(s->dlightbits, s->polys);
826 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
828 for (p = s->polys;p;p = p->next)
831 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
832 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
833 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);
839 for (p = s->polys;p;p = p->next)
842 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
843 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
844 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
850 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
852 float bmverts[256*3];
857 void RBrushModelSurf_DoVisible(msurface_t *surf)
859 // float *v, *bmv, *endbmv;
861 // for (p = surf->polys;p;p = p->next)
863 // for (v = p->verts[0], bmv = bmpoints, endbmv = bmv + p->numverts * 3;bmv < endbmv;v += VERTEXSIZE, bmv += 3)
864 // softwaretransform(v, bmv);
865 // if (R_Clip_Polygon(bmpoints, p->numverts, sizeof(float) * 3, surf->flags & SURF_CLIPSOLID))
866 surf->visframe = r_framecount;
871 void RBrushModelSurf_Callback(void *data, void *data2)
873 entity_t *ent = data2;
874 msurface_t *surf = data;
878 // FIXME: implement better dupe prevention in AddPolygon callback code
879 if (ent->render.model->firstmodelsurface != 0)
881 // 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)
882 if (surf->visframe == r_framecount)
886 surf->visframe = r_framecount;
891 modelalpha = ent->render.alpha;
893 softwaretransformforbrushentity (ent);
895 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
897 // sky and liquid don't need sorting (skypoly/transpoly)
898 if (surf->flags & SURF_DRAWSKY)
899 RSurf_DrawSky(surf, true);
901 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), true, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
905 t = R_TextureAnimation(surf->texinfo->texture);
906 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)
907 RSurf_DrawWallVertex(surf, t, true, true);
909 RSurf_DrawWall(surf, t, true);
918 void R_DrawBrushModel (entity_t *e)
930 clmodel = e->render.model;
932 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
935 for (i=0 ; i<3 ; i++)
937 mins[i] = e->render.origin[i] - clmodel->radius;
938 maxs[i] = e->render.origin[i] + clmodel->radius;
944 VectorAdd (e->render.origin, clmodel->mins, mins);
945 VectorAdd (e->render.origin, clmodel->maxs, maxs);
948 if (R_CullBox (mins, maxs))
953 VectorSubtract (r_origin, e->render.origin, modelorg);
957 vec3_t forward, right, up;
959 VectorCopy (modelorg, temp);
960 AngleVectors (e->render.angles, forward, right, up);
961 modelorg[0] = DotProduct (temp, forward);
962 modelorg[1] = -DotProduct (temp, right);
963 modelorg[2] = DotProduct (temp, up);
966 softwaretransformforbrushentity (e);
968 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
971 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
972 s->visframe = r_framecount;
975 // calculate dynamic lighting for bmodel if it's not an instanced model
976 for (i = 0;i < MAX_DLIGHTS;i++)
978 if (!cl_dlights[i].radius)
981 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
982 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
984 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;
987 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
989 if (s->visframe == r_framecount)
991 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
994 for (p = s->polys;p;p = p->next)
996 for (j = 0;j < p->numverts;j++)
997 softwaretransform(&p->verts[j][0], bmverts + j * 3);
998 R_Clip_AddPolygon(bmverts, p->numverts, 3 * sizeof(float), (s->flags & SURF_CLIPSOLID) != 0 && modelalpha == 1, RBrushModelSurf_Callback, s, e, NULL);
1003 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1005 // sky and liquid don't need sorting (skypoly/transpoly)
1006 if (s->flags & SURF_DRAWSKY)
1007 RSurf_DrawSky(s, true);
1009 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1013 texture_t *t = R_TextureAnimation(s->texinfo->texture);
1014 if (vertexlit || s->texinfo->texture->transparent)
1015 RSurf_DrawWallVertex(s, t, true, true);
1017 RSurf_DrawWall(s, t, true);
1026 =============================================================
1030 =============================================================
1034 static byte *worldvis;
1036 void R_MarkLeaves (void)
1038 static float noviscache;
1039 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1042 r_oldviewleaf = r_viewleaf;
1043 noviscache = r_novis.value;
1045 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1049 void RSurf_Callback(void *data, void *junk)
1051 ((msurface_t *)data)->visframe = r_framecount;
1055 void RSurf_Callback(void *data, void *junk)
1057 msurface_t *surf = data;
1060 // if (surf->visframe == r_framecount)
1063 surf->visframe = r_framecount;
1067 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1069 // sky and liquid don't need sorting (skypoly/transpoly)
1070 if (surf->flags & SURF_DRAWSKY)
1071 RSurf_DrawSky(surf, false);
1073 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1077 t = R_TextureAnimation(surf->texinfo->texture);
1079 RSurf_DrawWallVertex(surf, t, false, false);
1081 RSurf_DrawWall(surf, t, false);
1087 mleaf_t *r_oldviewleaf;
1088 int r_markvisframecount = 0;
1090 void R_MarkLeaves (void)
1092 static float noviscache;
1095 msurface_t *surf, **mark, **endmark;
1096 model_t *model = cl.worldmodel;
1097 // mportal_t *portal;
1102 // ignore testvis if the map just changed
1103 if (r_testvis.value && model->nodes->markvisframe == r_markvisframecount)
1106 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1109 r_oldviewleaf = r_viewleaf;
1110 noviscache = r_novis.value;
1112 if ((in = r_viewleaf->compressed_vis))
1114 row = (model->numleafs+7)>>3;
1116 if (!r_testvis.value)
1117 r_markvisframecount++;
1119 // LordHavoc: mark the root node as visible, it will terminate all other ascensions
1120 model->nodes->markvisframe = r_markvisframecount;
1128 l = model->numleafs - (k << 3);
1131 for (i=0 ; i<l ; i++)
1135 leaf = &model->leafs[(k << 3)+i+1];
1136 node = (mnode_t *)leaf;
1139 node->markvisframe = r_markvisframecount;
1140 node = node->parent;
1142 while (node->markvisframecount != r_markvisframecount);
1153 // LordHavoc: no vis data, mark everything as visible
1154 model->nodes->markvisframe = r_markvisframecount;
1156 for (i = 1;i < model->numleafs;i++)
1158 node = (mnode_t *)&model->leafs[i];
1161 node->markvisframe = r_markvisframecount;
1162 node = node->parent;
1164 while (node->markvisframecount != r_markvisframecount);
1170 void R_SolidWorldNode (void)
1172 if ((int) r_solidworldnode.value == 3)
1175 mportal_t *p, *pstack[8192];
1176 msurface_t *surf, **mark, **endmark;
1182 leaf->worldnodeframe = r_framecount;
1187 leaf->visframe = r_framecount;
1189 if (leaf->nummarksurfaces)
1191 mark = leaf->firstmarksurface;
1192 endmark = mark + leaf->nummarksurfaces;
1198 // make sure surfaces are only processed once
1199 if (surf->worldnodeframe == r_framecount)
1201 surf->worldnodeframe = r_framecount;
1202 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1204 if (surf->flags & SURF_PLANEBACK)
1206 VectorNegate(surf->plane->normal, plane.normal);
1207 plane.dist = -surf->plane->dist;
1208 for (poly = surf->polys;poly;poly = poly->next)
1209 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1214 if (!(surf->flags & SURF_PLANEBACK))
1215 for (poly = surf->polys;poly;poly = poly->next)
1216 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1219 while (mark < endmark);
1226 // make sure surfaces are only processed once
1227 if (surf->worldnodeframe == r_framecount)
1229 surf->worldnodeframe = r_framecount;
1230 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1232 if (surf->flags & SURF_PLANEBACK)
1233 surf->visframe = r_framecount;
1237 if (!(surf->flags & SURF_PLANEBACK))
1238 surf->visframe = r_framecount;
1241 while (mark < endmark);
1245 // follow portals into other leafs
1247 for (;p;p = p->next)
1249 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1252 if (leaf->worldnodeframe != r_framecount)
1254 leaf->worldnodeframe = r_framecount;
1255 if (leaf->contents != CONTENTS_SOLID)
1257 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1259 pstack[portalstack++] = p;
1263 p = pstack[--portalstack];
1273 else if ((int) r_solidworldnode.value == 2)
1275 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1276 int nodestackpos = 0;
1280 if (R_NotCulledBox(node->mins, node->maxs))
1284 if (node->numsurfaces)
1286 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1288 if (PlaneDiff (r_origin, node->plane) < 0)
1290 for (;surf < surfend;surf++)
1292 if (surf->flags & SURF_PLANEBACK)
1294 VectorNegate(surf->plane->normal, plane.normal);
1295 plane.dist = -surf->plane->dist;
1296 for (poly = surf->polys;poly;poly = poly->next)
1297 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1303 for (;surf < surfend;surf++)
1305 if (!(surf->flags & SURF_PLANEBACK))
1306 for (poly = surf->polys;poly;poly = poly->next)
1307 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1314 if (node->numsurfaces)
1316 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1317 if (PlaneDiff (r_origin, node->plane) < 0)
1319 for (;surf < surfend;surf++)
1321 if (surf->flags & SURF_PLANEBACK)
1322 surf->visframe = r_framecount;
1327 for (;surf < surfend;surf++)
1329 if (!(surf->flags & SURF_PLANEBACK))
1330 surf->visframe = r_framecount;
1336 // recurse down the children
1337 if (node->children[0]->contents >= 0)
1339 if (node->children[1]->contents >= 0)
1341 if (nodestackpos < 8192)
1342 nodestack[nodestackpos++] = node->children[1];
1343 node = node->children[0];
1347 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1348 node = node->children[0];
1353 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1354 if (node->children[1]->contents >= 0)
1356 node = node->children[1];
1359 else if (nodestackpos > 0)
1361 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1362 node = nodestack[--nodestackpos];
1367 else if (nodestackpos > 0)
1369 node = nodestack[--nodestackpos];
1373 else if ((int) r_solidworldnode.value == 1 && r_ser.value)
1376 msurface_t *surf, *endsurf;
1379 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1380 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1381 for (;surf < endsurf;surf++)
1383 if (PlaneDiff(r_origin, surf->plane) < 0)
1385 if (surf->flags & SURF_PLANEBACK)
1387 VectorNegate(surf->plane->normal, plane.normal);
1388 plane.dist = -surf->plane->dist;
1389 for (poly = surf->polys;poly;poly = poly->next)
1390 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1395 if (!(surf->flags & SURF_PLANEBACK))
1396 for (poly = surf->polys;poly;poly = poly->next)
1397 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)&surf->plane);
1405 msurface_t *surf, **mark, **endmark;
1409 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
1411 if (R_CullBox(leaf->mins, leaf->maxs))
1413 leaf->visframe = r_framecount;
1415 if (leaf->nummarksurfaces)
1417 // if (R_CullBox(leaf->mins, leaf->maxs))
1420 if (leaf->nummarksurfaces)
1422 mark = leaf->firstmarksurface;
1423 endmark = mark + leaf->nummarksurfaces;
1429 // make sure surfaces are only processed once
1430 if (surf->worldnodeframe == r_framecount)
1432 surf->worldnodeframe = r_framecount;
1433 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1435 if (surf->flags & SURF_PLANEBACK)
1437 VectorNegate(surf->plane->normal, plane.normal);
1438 plane.dist = -surf->plane->dist;
1439 for (poly = surf->polys;poly;poly = poly->next)
1440 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1445 if (!(surf->flags & SURF_PLANEBACK))
1446 for (poly = surf->polys;poly;poly = poly->next)
1447 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1450 while (mark < endmark);
1457 // make sure surfaces are only processed once
1458 if (surf->worldnodeframe == r_framecount)
1460 surf->worldnodeframe = r_framecount;
1461 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1463 if (surf->flags & SURF_PLANEBACK)
1464 surf->visframe = r_framecount;
1468 if (!(surf->flags & SURF_PLANEBACK))
1469 surf->visframe = r_framecount;
1472 while (mark < endmark);
1481 void RSurf_Callback(void *data, void *junk)
1483 ((msurface_t *)data)->visframe = r_framecount;
1486 int R_FrustumTestPolygon(float *points, int numpoints, int stride);
1488 void RSurf_DoVisible(msurface_t *surf)
1491 for (p = surf->polys;p;p = p->next)
1492 if (R_FrustumTestPolygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float)) >= 3)
1493 // R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), true, RSurf_Callback, surf, 1);
1494 // if (R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID))
1495 surf->visframe = r_framecount;
1499 //mleaf_t *llistbuffer[32768], *l, **llist;
1502 void RSurfLeaf_Callback(void *data)
1504 int portalstackpos = 0;
1506 mportal_t *p, *portalstack[32768];
1507 msurface_t *surf, **mark, **endmark;
1512 if (leaf->visframe == r_framecount)
1514 leaf->visframe = r_framecount;
1518 if (leaf->nummarksurfaces)
1520 mark = leaf->firstmarksurface;
1521 endmark = mark + leaf->nummarksurfaces;
1525 // make sure surfaces are only processed once
1526 if (surf->worldnodeframe == r_framecount)
1528 surf->worldnodeframe = r_framecount;
1529 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1531 if (surf->flags & SURF_PLANEBACK)
1532 RSurf_DoVisible(surf);
1536 if (!(surf->flags & SURF_PLANEBACK))
1537 RSurf_DoVisible(surf);
1540 while (mark < endmark);
1543 // follow portals into other leafs
1544 for (p = leaf->portals;p;p = p->next)
1546 if (p->past->visframe != r_framecount && DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1548 // R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3, RSurfLeaf_Callback, p->past, 1);
1549 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1550 portalstack[portalstackpos++] = p;
1554 while(portalstackpos);
1555 RSurfLeaf_Callback(p->past);
1556 // 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
1561 // experimental and inferior to the other in recursion depth allowances
1562 void R_PortalWorldNode (void)
1566 msurface_t *surf, **mark, **endmark;
1567 mleaf_t *leaf, *llistbuffer[32768], **l, **llist;
1570 leaf->visframe = r_framecount;
1571 l = llist = &llistbuffer[0];
1572 *llist++ = r_viewleaf;
1579 if (leaf->nummarksurfaces)
1581 mark = leaf->firstmarksurface;
1582 endmark = mark + leaf->nummarksurfaces;
1586 // make sure surfaces are only processed once
1587 if (surf->worldnodeframe == r_framecount)
1589 surf->worldnodeframe = r_framecount;
1590 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1592 if (surf->flags & SURF_PLANEBACK)
1593 RSurf_DoVisible(surf);
1597 if (!(surf->flags & SURF_PLANEBACK))
1598 RSurf_DoVisible(surf);
1601 while (mark < endmark);
1604 // follow portals into other leafs
1605 for (p = leaf->portals;p;p = p->next)
1607 if (p->past->visframe != r_framecount)
1609 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1611 p->past->visframe = r_framecount;
1617 // for (p = leaf->portals;p;p = p->next)
1620 // if (leaf->worldnodeframe != r_framecount)
1622 // leaf->worldnodeframe = r_framecount;
1623 // i = (leaf - cl.worldmodel->leafs) - 1;
1624 // if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1632 // p = r_viewleaf->portals;
1633 // for (;p;p = p->next)
1636 // if (p->past->worldnodeframe != r_framecount)
1640 // Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1645 int r_portalframecount = 0;
1648 void R_Portal_Callback(void *data, void *data2)
1650 mleaf_t *leaf = data;
1651 if (!r_testvis.value)
1652 ((mportal_t *)data2)->visframe = r_portalframecount;
1653 if (leaf->visframe != r_framecount)
1656 leaf->visframe = r_framecount;
1661 void R_PVSWorldNode()
1663 if (r_pvsworldnode.value == 1)
1666 mportal_t *p, *pstack[8192];
1667 msurface_t *surf, **mark, **endmark;
1673 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1676 leaf->worldnodeframe = r_framecount;
1681 leaf->visframe = r_framecount;
1683 if (leaf->nummarksurfaces)
1685 mark = leaf->firstmarksurface;
1686 endmark = mark + leaf->nummarksurfaces;
1692 // make sure surfaces are only processed once
1693 if (surf->worldnodeframe == r_framecount)
1695 surf->worldnodeframe = r_framecount;
1696 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1698 if (surf->flags & SURF_PLANEBACK)
1700 VectorNegate(surf->plane->normal, plane.normal);
1701 plane.dist = -surf->plane->dist;
1702 for (poly = surf->polys;poly;poly = poly->next)
1703 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1708 if (!(surf->flags & SURF_PLANEBACK))
1709 for (poly = surf->polys;poly;poly = poly->next)
1710 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1713 while (mark < endmark);
1720 // make sure surfaces are only processed once
1721 if (surf->worldnodeframe == r_framecount)
1723 surf->worldnodeframe = r_framecount;
1724 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1726 if (surf->flags & SURF_PLANEBACK)
1727 surf->visframe = r_framecount;
1731 if (!(surf->flags & SURF_PLANEBACK))
1732 surf->visframe = r_framecount;
1735 while (mark < endmark);
1739 // follow portals into other leafs
1741 for (;p;p = p->next)
1743 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1746 if (leaf->worldnodeframe != r_framecount)
1748 leaf->worldnodeframe = r_framecount;
1749 if (leaf->contents != CONTENTS_SOLID)
1751 i = (leaf - cl.worldmodel->leafs) - 1;
1752 if (worldvis[i>>3] & (1<<(i&7)))
1754 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1756 pstack[portalstack++] = p;
1760 p = pstack[--portalstack];
1773 p = r_viewleaf->portals;
1774 for (;p;p = p->next)
1777 if (p->past->worldnodeframe != r_framecount)
1781 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
1785 int i/*, l*/, k, c, row, numbits, bit, leafnum, numleafs;
1787 msurface_t *surf, **mark, **endmark;
1788 model_t *model = cl.worldmodel;
1790 // mportal_t *portal;
1795 // r_viewleaf->visframe = r_framecount;
1796 if (!r_testvis.value)
1797 r_portalframecount++;
1799 numleafs = model->numleafs;
1802 in = r_viewleaf->compressed_vis;
1803 row = (numbits + 7) >> 3;
1809 for (i = 0, bit = 1;c;i++, bit <<= 1)
1813 leafnum = (k << 3)+i+1;
1814 if (leafnum > numleafs)
1817 leaf = &model->leafs[leafnum];
1818 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1820 //for (portal = leaf->portals;portal;portal = portal->next)
1821 // if (DotProduct(r_origin, portal->plane.normal) > portal->plane.dist)
1822 // R_Clip_AddPolygon((float *)portal->points, portal->numpoints, sizeof(mvertex_t), false, R_Portal_Callback, leaf, portal, portal->plane);
1823 //leaf->visframe = r_framecount;
1825 if (leaf->nummarksurfaces)
1827 mark = leaf->firstmarksurface;
1828 endmark = mark + leaf->nummarksurfaces;
1834 // make sure surfaces are only processed once
1835 if (surf->worldnodeframe == r_framecount)
1837 surf->worldnodeframe = r_framecount;
1838 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1840 if (surf->flags & SURF_PLANEBACK)
1842 VectorNegate(surf->plane->normal, plane.normal);
1843 plane.dist = -surf->plane->dist;
1844 for (poly = surf->polys;poly;poly = poly->next)
1845 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1850 if (!(surf->flags & SURF_PLANEBACK))
1851 for (poly = surf->polys;poly;poly = poly->next)
1852 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1855 while (mark < endmark);
1862 // make sure surfaces are only processed once
1863 if (surf->worldnodeframe == r_framecount)
1865 surf->worldnodeframe = r_framecount;
1866 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1868 if (surf->flags & SURF_PLANEBACK)
1869 surf->visframe = r_framecount;
1873 if (!(surf->flags & SURF_PLANEBACK))
1874 surf->visframe = r_framecount;
1877 while (mark < endmark);
1891 entity_t clworldent;
1893 void R_DrawSurfaces (void)
1895 msurface_t *surf, *endsurf;
1896 texture_t *t, *currentt;
1897 int vertex = gl_vertex.value;
1899 currententity = &clworldent;
1901 softwaretransformidentity();
1902 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1903 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1904 t = currentt = NULL;
1905 for (;surf < endsurf;surf++)
1907 if (surf->visframe == r_framecount)
1910 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1912 if (surf->flags & SURF_DRAWSKY)
1913 RSurf_DrawSky(surf, false);
1916 if (currentt != surf->texinfo->texture)
1918 currentt = surf->texinfo->texture;
1919 t = R_TextureAnimation(surf->texinfo->texture);
1921 RSurf_DrawWater(surf, t, false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1926 if (currentt != surf->texinfo->texture)
1928 currentt = surf->texinfo->texture;
1929 t = R_TextureAnimation(surf->texinfo->texture);
1932 RSurf_DrawWallVertex(surf, t, false, false);
1934 RSurf_DrawWall(surf, t, false);
1940 void R_DrawPortals(void)
1942 int drawportals, i, r, g, b;
1943 // mleaf_t *leaf, *endleaf;
1944 mportal_t *portal, *endportal;
1945 mvertex_t *point/*, *endpoint*/;
1946 drawportals = (int)r_drawportals.value;
1947 if (drawportals < 1)
1950 leaf = cl.worldmodel->leafs;
1951 endleaf = leaf + cl.worldmodel->numleafs;
1952 for (;leaf < endleaf;leaf++)
1954 if (leaf->visframe == r_framecount && leaf->portals)
1956 i = leaf - cl.worldmodel->leafs;
1957 r = (i & 0x0007) << 5;
1958 g = (i & 0x0038) << 2;
1959 b = (i & 0x01C0) >> 1;
1960 portal = leaf->portals;
1963 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1964 point = portal->points + portal->numpoints - 1;
1965 endpoint = portal->points;
1966 for (;point >= endpoint;point--)
1967 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1969 portal = portal->next;
1974 portal = cl.worldmodel->portals;
1975 endportal = portal + cl.worldmodel->numportals;
1976 for (;portal < endportal;portal++)
1978 if (portal->visframe == r_portalframecount)
1980 i = portal - cl.worldmodel->portals;
1981 r = (i & 0x0007) << 5;
1982 g = (i & 0x0038) << 2;
1983 b = (i & 0x01C0) >> 1;
1984 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1985 point = portal->points;
1986 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1988 for (i = portal->numpoints - 1;i >= 0;i--)
1989 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1993 for (i = 0;i < portal->numpoints;i++)
1994 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
2006 void R_DrawWorld (void)
2008 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
2009 vertexworld = gl_vertex.value;
2011 memset (&clworldent, 0, sizeof(clworldent));
2012 clworldent.render.model = cl.worldmodel;
2013 clworldent.render.colormod[0] = clworldent.render.colormod[1] = clworldent.render.colormod[2] = 1;
2014 modelalpha = clworldent.render.alpha = 1;
2015 clworldent.render.scale = 1;
2017 VectorCopy (r_origin, modelorg);
2019 currententity = &clworldent;
2021 softwaretransformidentity(); // LordHavoc: clear transform
2025 if (r_novis.value || r_viewleaf->compressed_vis == NULL)
2026 R_SolidWorldNode ();
2036 =============================================================================
2040 =============================================================================
2043 // returns a texture number and the position inside it
2044 int AllocBlock (int w, int h, short *x, short *y)
2050 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
2052 best = BLOCK_HEIGHT;
2054 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
2058 for (j=0 ; j<w ; j++)
2060 if (allocated[texnum][i+j] >= best)
2062 if (allocated[texnum][i+j] > best2)
2063 best2 = allocated[texnum][i+j];
2066 { // this is a valid spot
2072 if (best + h > BLOCK_HEIGHT)
2075 if (nosubimagefragments || nosubimage)
2077 if (!lightmaps[texnum])
2079 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
2080 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
2083 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
2084 else if (!allocated[texnum][0])
2086 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
2087 memset(blank, 0, sizeof(blank));
2090 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
2091 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2092 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2094 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
2096 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
2100 for (i = 0;i < w;i++)
2101 allocated[texnum][*x + i] = best + h;
2106 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
2115 BuildSurfaceDisplayList
2118 void BuildSurfaceDisplayList (model_t *model, mvertex_t *vertices, msurface_t *fa)
2120 int i, j, lindex, lnumverts;
2126 // reconstruct the polygon
2127 pedges = model->edges;
2128 lnumverts = fa->numedges;
2133 poly = Hunk_AllocName (sizeof(glpolysizeof_t) + lnumverts * sizeof(float[VERTEXSIZE]), "surfaces");
2134 poly->next = fa->polys;
2136 // poly->flags = fa->flags;
2137 poly->numverts = lnumverts;
2139 for (i=0 ; i<lnumverts ; i++)
2141 lindex = model->surfedges[fa->firstedge + i];
2144 vec = vertices[pedges[lindex].v[0]].position;
2146 vec = vertices[pedges[-lindex].v[1]].position;
2148 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
2149 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
2151 VectorCopy (vec, poly->verts[i]);
2152 poly->verts[i][3] = s / fa->texinfo->texture->width;
2153 poly->verts[i][4] = t / fa->texinfo->texture->height;
2156 // lightmap texture coordinates
2158 s -= fa->texturemins[0];
2159 t -= fa->texturemins[1];
2162 // LordHavoc: calc lightmap data offset
2163 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;
2164 poly->verts[i][7] = j;
2165 s += fa->light_s*16;
2166 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
2168 t += fa->light_t*16;
2169 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
2171 poly->verts[i][5] = s;
2172 poly->verts[i][6] = t;
2176 // remove co-linear points - Ed
2179 if (!gl_keeptjunctions.value)
2181 for (i = 0 ; i < lnumverts ; ++i)
2184 float *prev, *this, *next;
2186 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
2187 this = poly->verts[i];
2188 next = poly->verts[(i + 1) % lnumverts];
2190 VectorSubtract( this, prev, v1 );
2191 VectorNormalize( v1 );
2192 VectorSubtract( next, prev, v2 );
2193 VectorNormalize( v2 );
2195 // skip co-linear points
2196 #define COLINEAR_EPSILON 0.001
2197 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
2198 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
2199 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
2202 for (j = i + 1; j < lnumverts; ++j)
2205 for (k = 0; k < VERTEXSIZE; ++k)
2206 poly->verts[j - 1][k] = poly->verts[j][k];
2210 // retry next vertex next time, which is now current vertex
2214 poly->numverts = lnumverts;
2220 ========================
2221 GL_CreateSurfaceLightmap
2222 ========================
2224 void GL_CreateSurfaceLightmap (msurface_t *surf)
2228 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
2231 smax = (surf->extents[0]>>4)+1;
2232 tmax = (surf->extents[1]>>4)+1;
2234 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
2235 if (nosubimage || nosubimagefragments)
2237 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
2238 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
2241 R_BuildLightMap (surf, templight, smax * 4);
2243 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
2247 R_BuildLightMap (surf, templight, smax * 3);
2249 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
2258 Builds the lightmap texture
2259 with all the surfaces from all brush models
2262 void GL_BuildLightmaps (void)
2267 memset (allocated, 0, sizeof(allocated));
2269 r_framecount = 1; // no dlightcache
2271 if (gl_nosubimagefragments.value)
2272 nosubimagefragments = 1;
2274 nosubimagefragments = 0;
2276 if (gl_nosubimage.value)
2281 if (gl_lightmaprgba.value)
2283 lightmaprgba = true;
2288 lightmaprgba = false;
2292 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
2293 // it needs to be aligned on 4 pixel boundaries...
2294 // so I implemented an adjustable lightmap alignment
2295 if (gl_lightmapalign.value < 1)
2296 gl_lightmapalign.value = 1;
2297 if (gl_lightmapalign.value > 16)
2298 gl_lightmapalign.value = 16;
2300 while (lightmapalign < gl_lightmapalign.value)
2301 lightmapalign <<= 1;
2302 gl_lightmapalign.value = lightmapalign;
2303 lightmapalignmask = ~(lightmapalign - 1);
2304 if (nosubimagefragments || nosubimage)
2307 lightmapalignmask = ~0;
2310 if (!lightmap_textures)
2311 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
2313 for (j=1 ; j<MAX_MODELS ; j++)
2315 m = cl.model_precache[j];
2318 if (m->name[0] == '*')
2320 for (i=0 ; i<m->numsurfaces ; i++)
2322 if ( m->surfaces[i].flags & SURF_DRAWTURB )
2324 if ( m->surfaces[i].flags & SURF_DRAWSKY )
2326 GL_CreateSurfaceLightmap (m->surfaces + i);
2327 BuildSurfaceDisplayList (m, m->vertexes, m->surfaces + i);
2331 if (nosubimage || nosubimagefragments)
2335 qglSelectTexture(gl_mtex_enum+1);
2336 for (i = 0;i < MAX_LIGHTMAPS;i++)
2338 if (!allocated[i][0])
2340 lightmapupdate[i][0] = BLOCK_HEIGHT;
2341 lightmapupdate[i][1] = 0;
2344 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
2345 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2346 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2348 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
2350 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
2355 qglSelectTexture(gl_mtex_enum+0);