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 int i/*, l*/, k, c, row, numbits, bit, leafnum, numleafs;
1775 msurface_t *surf, **mark, **endmark;
1776 model_t *model = cl.worldmodel;
1778 // mportal_t *portal;
1783 // r_viewleaf->visframe = r_framecount;
1784 if (!r_testvis.value)
1785 r_portalframecount++;
1787 numleafs = model->numleafs;
1790 in = r_viewleaf->compressed_vis;
1791 row = (numbits + 7) >> 3;
1797 for (i = 0, bit = 1;c;i++, bit <<= 1)
1801 leafnum = (k << 3)+i+1;
1802 if (leafnum > numleafs)
1805 leaf = &model->leafs[leafnum];
1806 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1808 //for (portal = leaf->portals;portal;portal = portal->next)
1809 // if (DotProduct(r_origin, portal->plane.normal) > portal->plane.dist)
1810 // R_Clip_AddPolygon((float *)portal->points, portal->numpoints, sizeof(mvertex_t), false, R_Portal_Callback, leaf, portal, portal->plane);
1811 //leaf->visframe = r_framecount;
1813 if (leaf->nummarksurfaces)
1815 mark = leaf->firstmarksurface;
1816 endmark = mark + leaf->nummarksurfaces;
1822 // make sure surfaces are only processed once
1823 if (surf->worldnodeframe == r_framecount)
1825 surf->worldnodeframe = r_framecount;
1826 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1828 if (surf->flags & SURF_PLANEBACK)
1830 VectorNegate(surf->plane->normal, plane.normal);
1831 plane.dist = -surf->plane->dist;
1832 for (poly = surf->polys;poly;poly = poly->next)
1833 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1838 if (!(surf->flags & SURF_PLANEBACK))
1839 for (poly = surf->polys;poly;poly = poly->next)
1840 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1843 while (mark < endmark);
1850 // make sure surfaces are only processed once
1851 if (surf->worldnodeframe == r_framecount)
1853 surf->worldnodeframe = r_framecount;
1854 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1856 if (surf->flags & SURF_PLANEBACK)
1857 surf->visframe = r_framecount;
1861 if (!(surf->flags & SURF_PLANEBACK))
1862 surf->visframe = r_framecount;
1865 while (mark < endmark);
1879 entity_t clworldent;
1881 void R_DrawSurfaces (void)
1883 msurface_t *surf, *endsurf;
1884 texture_t *t, *currentt;
1885 int vertex = gl_vertex.value;
1887 currententity = &clworldent;
1889 softwaretransformidentity();
1890 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1891 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1892 t = currentt = NULL;
1893 for (;surf < endsurf;surf++)
1895 if (surf->visframe == r_framecount)
1898 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1900 if (surf->flags & SURF_DRAWSKY)
1901 RSurf_DrawSky(surf, false);
1904 if (currentt != surf->texinfo->texture)
1906 currentt = surf->texinfo->texture;
1907 t = R_TextureAnimation(surf->texinfo->texture);
1909 RSurf_DrawWater(surf, t, false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1914 if (currentt != surf->texinfo->texture)
1916 currentt = surf->texinfo->texture;
1917 t = R_TextureAnimation(surf->texinfo->texture);
1920 RSurf_DrawWallVertex(surf, t, false, false);
1922 RSurf_DrawWall(surf, t, false);
1928 void R_DrawPortals(void)
1930 int drawportals, i, r, g, b;
1931 // mleaf_t *leaf, *endleaf;
1932 mportal_t *portal, *endportal;
1933 mvertex_t *point/*, *endpoint*/;
1934 drawportals = (int)r_drawportals.value;
1935 if (drawportals < 1)
1938 leaf = cl.worldmodel->leafs;
1939 endleaf = leaf + cl.worldmodel->numleafs;
1940 for (;leaf < endleaf;leaf++)
1942 if (leaf->visframe == r_framecount && leaf->portals)
1944 i = leaf - cl.worldmodel->leafs;
1945 r = (i & 0x0007) << 5;
1946 g = (i & 0x0038) << 2;
1947 b = (i & 0x01C0) >> 1;
1948 portal = leaf->portals;
1951 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1952 point = portal->points + portal->numpoints - 1;
1953 endpoint = portal->points;
1954 for (;point >= endpoint;point--)
1955 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1957 portal = portal->next;
1962 portal = cl.worldmodel->portals;
1963 endportal = portal + cl.worldmodel->numportals;
1964 for (;portal < endportal;portal++)
1966 if (portal->visframe == r_portalframecount)
1968 i = portal - cl.worldmodel->portals;
1969 r = (i & 0x0007) << 5;
1970 g = (i & 0x0038) << 2;
1971 b = (i & 0x01C0) >> 1;
1972 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1973 point = portal->points;
1974 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1976 for (i = portal->numpoints - 1;i >= 0;i--)
1977 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1981 for (i = 0;i < portal->numpoints;i++)
1982 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1994 void R_DrawWorld (void)
1996 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1997 vertexworld = gl_vertex.value;
1999 memset (&clworldent, 0, sizeof(clworldent));
2000 clworldent.render.model = cl.worldmodel;
2001 clworldent.render.colormod[0] = clworldent.render.colormod[1] = clworldent.render.colormod[2] = 1;
2002 modelalpha = clworldent.render.alpha = 1;
2003 clworldent.render.scale = 1;
2005 VectorCopy (r_origin, modelorg);
2007 currententity = &clworldent;
2009 softwaretransformidentity(); // LordHavoc: clear transform
2013 if (r_novis.value || r_viewleaf->compressed_vis == NULL)
2014 R_SolidWorldNode ();
2024 =============================================================================
2028 =============================================================================
2031 // returns a texture number and the position inside it
2032 int AllocBlock (int w, int h, short *x, short *y)
2038 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
2040 best = BLOCK_HEIGHT;
2042 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
2046 for (j=0 ; j<w ; j++)
2048 if (allocated[texnum][i+j] >= best)
2050 if (allocated[texnum][i+j] > best2)
2051 best2 = allocated[texnum][i+j];
2054 { // this is a valid spot
2060 if (best + h > BLOCK_HEIGHT)
2063 if (nosubimagefragments || nosubimage)
2065 if (!lightmaps[texnum])
2067 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
2068 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
2071 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
2072 else if (!allocated[texnum][0])
2074 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
2075 memset(blank, 0, sizeof(blank));
2078 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
2079 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2080 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2082 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
2084 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
2088 for (i = 0;i < w;i++)
2089 allocated[texnum][*x + i] = best + h;
2094 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
2103 BuildSurfaceDisplayList
2106 void BuildSurfaceDisplayList (model_t *model, mvertex_t *vertices, msurface_t *fa)
2108 int i, j, lindex, lnumverts;
2114 // reconstruct the polygon
2115 pedges = model->edges;
2116 lnumverts = fa->numedges;
2121 poly = Hunk_AllocName (sizeof(glpolysizeof_t) + lnumverts * sizeof(float[VERTEXSIZE]), "surfaces");
2122 poly->next = fa->polys;
2124 // poly->flags = fa->flags;
2125 poly->numverts = lnumverts;
2127 for (i=0 ; i<lnumverts ; i++)
2129 lindex = model->surfedges[fa->firstedge + i];
2132 vec = vertices[pedges[lindex].v[0]].position;
2134 vec = vertices[pedges[-lindex].v[1]].position;
2136 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
2137 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
2139 VectorCopy (vec, poly->verts[i]);
2140 poly->verts[i][3] = s / fa->texinfo->texture->width;
2141 poly->verts[i][4] = t / fa->texinfo->texture->height;
2144 // lightmap texture coordinates
2146 s -= fa->texturemins[0];
2147 t -= fa->texturemins[1];
2150 // LordHavoc: calc lightmap data offset
2151 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;
2152 poly->verts[i][7] = j;
2153 s += fa->light_s*16;
2154 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
2156 t += fa->light_t*16;
2157 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
2159 poly->verts[i][5] = s;
2160 poly->verts[i][6] = t;
2164 // remove co-linear points - Ed
2167 if (!gl_keeptjunctions.value)
2169 for (i = 0 ; i < lnumverts ; ++i)
2172 float *prev, *this, *next;
2174 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
2175 this = poly->verts[i];
2176 next = poly->verts[(i + 1) % lnumverts];
2178 VectorSubtract( this, prev, v1 );
2179 VectorNormalize( v1 );
2180 VectorSubtract( next, prev, v2 );
2181 VectorNormalize( v2 );
2183 // skip co-linear points
2184 #define COLINEAR_EPSILON 0.001
2185 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
2186 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
2187 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
2190 for (j = i + 1; j < lnumverts; ++j)
2193 for (k = 0; k < VERTEXSIZE; ++k)
2194 poly->verts[j - 1][k] = poly->verts[j][k];
2198 // retry next vertex next time, which is now current vertex
2202 poly->numverts = lnumverts;
2208 ========================
2209 GL_CreateSurfaceLightmap
2210 ========================
2212 void GL_CreateSurfaceLightmap (msurface_t *surf)
2216 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
2219 smax = (surf->extents[0]>>4)+1;
2220 tmax = (surf->extents[1]>>4)+1;
2222 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
2223 if (nosubimage || nosubimagefragments)
2225 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
2226 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
2229 R_BuildLightMap (surf, templight, smax * 4);
2231 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
2235 R_BuildLightMap (surf, templight, smax * 3);
2237 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
2246 Builds the lightmap texture
2247 with all the surfaces from all brush models
2250 void GL_BuildLightmaps (void)
2255 memset (allocated, 0, sizeof(allocated));
2257 r_framecount = 1; // no dlightcache
2259 if (gl_nosubimagefragments.value)
2260 nosubimagefragments = 1;
2262 nosubimagefragments = 0;
2264 if (gl_nosubimage.value)
2269 if (gl_lightmaprgba.value)
2271 lightmaprgba = true;
2276 lightmaprgba = false;
2280 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
2281 // it needs to be aligned on 4 pixel boundaries...
2282 // so I implemented an adjustable lightmap alignment
2283 if (gl_lightmapalign.value < 1)
2284 gl_lightmapalign.value = 1;
2285 if (gl_lightmapalign.value > 16)
2286 gl_lightmapalign.value = 16;
2288 while (lightmapalign < gl_lightmapalign.value)
2289 lightmapalign <<= 1;
2290 gl_lightmapalign.value = lightmapalign;
2291 lightmapalignmask = ~(lightmapalign - 1);
2292 if (nosubimagefragments || nosubimage)
2295 lightmapalignmask = ~0;
2298 if (!lightmap_textures)
2299 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
2301 for (j=1 ; j<MAX_MODELS ; j++)
2303 m = cl.model_precache[j];
2306 if (m->name[0] == '*')
2308 for (i=0 ; i<m->numsurfaces ; i++)
2310 if ( m->surfaces[i].flags & SURF_DRAWTURB )
2312 if ( m->surfaces[i].flags & SURF_DRAWSKY )
2314 GL_CreateSurfaceLightmap (m->surfaces + i);
2315 BuildSurfaceDisplayList (m, m->vertexes, m->surfaces + i);
2319 if (nosubimage || nosubimagefragments)
2323 qglSelectTexture(gl_mtex_enum+1);
2324 for (i = 0;i < MAX_LIGHTMAPS;i++)
2326 if (!allocated[i][0])
2328 lightmapupdate[i][0] = BLOCK_HEIGHT;
2329 lightmapupdate[i][1] = 0;
2332 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
2333 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2334 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2336 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
2338 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
2343 qglSelectTexture(gl_mtex_enum+0);