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 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
43 int lightmapalign, lightmapalignmask; // LordHavoc: align texsubimage updates on 4 byte boundaries
44 cvar_t gl_lightmapalign = {0, "gl_lightmapalign", "4"}; // align texsubimage updates on 4 byte boundaries
45 cvar_t gl_lightmaprgba = {0, "gl_lightmaprgba", "1"};
46 cvar_t gl_nosubimagefragments = {0, "gl_nosubimagefragments", "0"};
47 cvar_t gl_nosubimage = {0, "gl_nosubimage", "0"};
48 cvar_t r_ambient = {0, "r_ambient", "0"};
49 cvar_t gl_vertex = {0, "gl_vertex", "0"};
50 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
51 cvar_t r_drawportals = {0, "r_drawportals", "0"};
52 cvar_t r_testvis = {0, "r_testvis", "0"};
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);
86 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
89 int dlightdivtable[32768];
94 int R_AddDynamicLights (msurface_t *surf)
96 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
101 // LordHavoc: use 64bit integer... shame it's not very standardized...
102 #if _MSC_VER || __BORLANDC__
110 if (!dlightdivtable[1])
112 dlightdivtable[0] = 4194304;
113 for (s = 1; s < 32768; s++)
114 dlightdivtable[s] = 4194304 / (s << 7);
117 smax = (surf->extents[0] >> 4) + 1;
118 tmax = (surf->extents[1] >> 4) + 1;
120 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
122 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
123 continue; // not lit by this light
125 VectorSubtract (cl_dlights[lnum].origin, currentrenderentity->origin, local);
126 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
128 // for comparisons to minimum acceptable light
129 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
131 // clamp radius to avoid exceeding 32768 entry division table
132 if (maxdist > 4194304)
136 if (dist2 >= maxdist)
139 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
140 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
141 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
143 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
144 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
146 s = bound(0, impacts, smax * 16) - impacts;
147 t = bound(0, impactt, tmax * 16) - impactt;
148 i = s * s + t * t + dist2;
152 // reduce calculations
153 for (s = 0, i = impacts; s < smax; s++, i -= 16)
154 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
156 maxdist3 = maxdist - (int) (dist * dist);
158 // convert to 8.8 blocklights format and scale up by radius
159 red = cl_dlights[lnum].color[0] * maxdist;
160 green = cl_dlights[lnum].color[1] * maxdist;
161 blue = cl_dlights[lnum].color[2] * maxdist;
165 for (t = 0; t < tmax; t++, i -= 16)
168 // make sure some part of it is visible on this line
171 maxdist2 = maxdist - td;
172 for (s = 0; s < smax; s++)
174 if (sdtable[s] < maxdist2)
176 k = dlightdivtable[(sdtable[s] + td) >> 7];
177 bl[0] += (red * k) >> 9;
178 bl[1] += (green * k) >> 9;
179 bl[2] += (blue * k) >> 9;
193 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
196 stride -= (width*lightmapbytes);
197 // deal with lightmap brightness scale
198 shift = 7 + lightscalebit;
201 for (i = 0;i < height;i++, out += stride)
203 for (j = 0;j < width;j++, in += 3, out += 4)
205 out[0] = min(in[0] >> shift, 255);
206 out[1] = min(in[1] >> shift, 255);
207 out[2] = min(in[2] >> shift, 255);
214 for (i = 0;i < height;i++, out += stride)
216 for (j = 0;j < width;j++, in += 3, out += 3)
218 out[0] = min(in[0] >> shift, 255);
219 out[1] = min(in[1] >> shift, 255);
220 out[2] = min(in[2] >> shift, 255);
230 Combine and scale multiple lightmaps into the 8.8 format in blocklights
233 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride, int dlightchanged)
236 int i, j, size, size3;
242 // update cached lighting info
243 surf->cached_dlight = 0;
244 surf->cached_lightscalebit = lightscalebit;
245 surf->cached_ambient = r_ambient.value;
246 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
247 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
248 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
249 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
251 smax = (surf->extents[0]>>4)+1;
252 tmax = (surf->extents[1]>>4)+1;
255 lightmap = surf->samples;
257 // set to full bright if no light data
258 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
261 for (i = 0;i < size;i++)
271 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
275 for (i = 0;i < size3;i++)
279 memset(&blocklights[0], 0, size*3*sizeof(int));
281 if (r_dlightmap.value && surf->dlightframe == r_framecount)
283 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
285 else if (dlightchanged)
286 return; // don't upload if only updating dlights and none mattered
289 // add all the lightmaps
291 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
292 for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
293 *bl++ += *lightmap++ * scale;
296 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
299 void R_UpdateLightmap(msurface_t *s, int lnum, int dlightschanged)
302 // upload the new lightmap texture fragment
304 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
305 if (nosubimage || nosubimagefragments)
307 if (lightmapupdate[lnum][0] > s->light_t)
308 lightmapupdate[lnum][0] = s->light_t;
309 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
310 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
312 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4, false);
314 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3, false);
318 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
319 tmax = (s->extents[1]>>4)+1;
322 R_BuildLightMap (s, templight, smax * 4, false);
324 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
328 R_BuildLightMap (s, templight, smax * 3, false);
330 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
340 Returns the proper texture for a given time and base texture
343 texture_t *R_TextureAnimation (texture_t *base)
345 if (currentrenderentity->frame && base->alternate_anims != NULL)
346 base = base->alternate_anims;
348 if (base->anim_total < 2)
351 return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
356 =============================================================
360 =============================================================
366 #include "gl_warp_sin.h"
368 #define TURBSCALE (256.0 / (2 * M_PI))
371 void UploadLightmaps(void)
374 if (nosubimage || nosubimagefragments)
376 for (i = 0;i < MAX_LIGHTMAPS;i++)
378 if (lightmapupdate[i][0] < lightmapupdate[i][1])
382 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
386 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
388 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
393 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]));
395 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]));
399 lightmapupdate[i][0] = BLOCK_HEIGHT;
400 lightmapupdate[i][1] = 0;
405 float wvert[1024*6]; // used by the following functions
407 void RSurf_DrawSky(msurface_t *s, int transform)
413 // LordHavoc: HalfLife maps have freaky skypolys...
417 for (p=s->polys ; p ; p=p->next)
419 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
421 skypoly[currentskypoly].firstvert = currentskyvert;
422 skypoly[currentskypoly++].verts = p->numverts;
425 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
427 softwaretransform(v, skyvert[currentskyvert].v);
433 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
435 VectorCopy(v, skyvert[currentskyvert].v);
443 int RSurf_Light(int *dlightbits, glpoly_t *polys)
445 float cr, cg, cb, radius, radius2, f, *v, *wv;
446 int i, a, b, lit = false;
451 for (a = 0;a < 8;a++)
453 if ((c = dlightbits[a]))
455 for (b = 0, d = 1;c;b++, d <<= 1)
460 light = &cl_dlights[a * 32 + b];
461 lightorigin = light->origin;
462 cr = light->color[0];
463 cg = light->color[1];
464 cb = light->color[2];
465 radius = light->radius*light->radius;
466 radius2 = radius * 256.0f;
468 for (p = polys;p;p = p->next)
470 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
472 f = VectorDistance2(wv, lightorigin);
475 f = radius2 / (f + LIGHTOFFSET);
491 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
494 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
497 // FIXME: make fog texture if water texture is transparent?
499 if (s->dlightframe != r_framecount)
502 // LordHavoc: fast path for no vertex lighting cases
505 if (r_waterripple.value)
507 for (p=s->polys ; p ; p=p->next)
509 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
510 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
512 softwaretransform(v, temp);
513 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);
520 for (p=s->polys ; p ; p=p->next)
522 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
523 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
525 softwaretransform(v, temp);
526 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);
534 if (r_waterripple.value)
536 for (p=s->polys ; p ; p=p->next)
538 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
539 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
540 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);
546 for (p=s->polys ; p ; p=p->next)
548 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
549 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
550 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);
560 for (p = s->polys;p;p = p->next)
562 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
565 softwaretransform(v, wv);
568 if (r_waterripple.value)
569 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);
570 wv[3] = wv[4] = wv[5] = 128.0f;
574 if (s->dlightframe == r_framecount)
575 RSurf_Light(s->dlightbits, s->polys);
577 for (p=s->polys ; p ; p=p->next)
579 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
580 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
581 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);
587 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
589 int i, lit = false, polys = 0, verts = 0;
594 wallvertcolor_t *outcolor;
595 // check for lightmap modification
597 || r_ambient.value != s->cached_ambient
598 || lightscalebit != s->cached_lightscalebit
600 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
601 || d_lightstylevalue[s->styles[1]] != s->cached_light[1]
602 || d_lightstylevalue[s->styles[2]] != s->cached_light[2]
603 || d_lightstylevalue[s->styles[3]] != s->cached_light[3])))
604 R_UpdateLightmap(s, s->lightmaptexturenum, false); // base lighting changed
605 else if (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
606 R_UpdateLightmap(s, s->lightmaptexturenum, true); // only dlights
608 if (s->dlightframe != r_framecount || r_dlightmap.value)
610 // LordHavoc: fast path version for no vertex lighting cases
611 out = &wallvert[currentwallvert];
612 for (p = s->polys;p;p = p->next)
614 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
616 wp = &wallpoly[currentwallpoly++];
617 wp->texnum = (unsigned short) R_GetTexture(t->texture);
618 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
619 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
620 wp->firstvert = currentwallvert;
621 wp->numverts = p->numverts;
624 currentwallvert += p->numverts;
628 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
630 softwaretransform(v, out->vert);
640 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
642 VectorCopy(v, out->vert);
649 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
658 for (p = s->polys;p;p = p->next)
660 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
663 softwaretransform(v, wv);
666 wv[3] = wv[4] = wv[5] = 0.0f;
669 verts += p->numverts;
672 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
674 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
675 lit = RSurf_Light(s->dlightbits, s->polys);
677 wp = &wallpoly[currentwallpoly];
678 out = &wallvert[currentwallvert];
679 outcolor = &wallvertcolor[currentwallvert];
680 currentwallpoly += polys;
681 for (p = s->polys;p;p = p->next)
684 wp->texnum = (unsigned short) R_GetTexture(t->texture);
685 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
686 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
687 wp->firstvert = currentwallvert;
688 wp->numverts = p->numverts;
691 currentwallvert += p->numverts;
692 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
698 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
699 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
700 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
705 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
706 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
707 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
711 out->vert[0] = wv[0];
712 out->vert[1] = wv[1];
713 out->vert[2] = wv[2];
723 // LordHavoc: transparent brush models
724 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
727 float *v, *wv, scale;
730 alpha = (int) (currentrenderentity->alpha * 255.0f);
731 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
733 for (p = s->polys;p;p = p->next)
735 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
738 softwaretransform(v, wv);
741 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
742 if (s->styles[0] != 255)
744 lm = (byte *)((long) s->samples + (int) v[7]);
745 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;
746 if (s->styles[1] != 255)
748 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;
749 if (s->styles[2] != 255)
751 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;
752 if (s->styles[3] != 255)
754 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;
762 if (s->dlightframe == r_framecount)
763 RSurf_Light(s->dlightbits, s->polys);
765 if (alpha != 255 || currentrenderentity->colormod[0] != 1 || currentrenderentity->colormod[1] != 1 || currentrenderentity->colormod[2] != 1)
767 for (p = s->polys;p;p = p->next)
770 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currentrenderentity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
771 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
772 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currentrenderentity->colormod[0], wv[4] * currentrenderentity->colormod[1], wv[5] * currentrenderentity->colormod[2], alpha);
778 for (p = s->polys;p;p = p->next)
781 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currentrenderentity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
782 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
783 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
789 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
791 float bmverts[256*3];
795 // LordHavoc: disabled clipping on bmodels because they tend to intersect things sometimes
797 void RBrushModelSurf_DoVisible(msurface_t *surf)
799 // float *v, *bmv, *endbmv;
801 // for (p = surf->polys;p;p = p->next)
803 // for (v = p->verts[0], bmv = bmpoints, endbmv = bmv + p->numverts * 3;bmv < endbmv;v += VERTEXSIZE, bmv += 3)
804 // softwaretransform(v, bmv);
805 // if (R_Clip_Polygon(bmpoints, p->numverts, sizeof(float) * 3, surf->flags & SURF_CLIPSOLID))
806 surf->visframe = r_framecount;
812 void RBrushModelSurf_Callback(void *data, void *data2)
814 msurface_t *surf = data;
817 currentrenderentity = data2;
820 // FIXME: implement better dupe prevention in AddPolygon callback code
821 if (ent->render.model->firstmodelsurface != 0)
823 // 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)
824 if (surf->visframe == r_framecount)
829 surf->visframe = r_framecount;
833 softwaretransformforbrushentity (currentrenderentity);
835 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
837 // sky and liquid don't need sorting (skypoly/transpoly)
838 if (surf->flags & SURF_DRAWSKY)
839 RSurf_DrawSky(surf, true);
841 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), true, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
845 t = R_TextureAnimation(surf->texinfo->texture);
846 if (t->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)
847 RSurf_DrawWallVertex(surf, t, true, true);
849 RSurf_DrawWall(surf, t, true);
859 void R_DrawBrushModel (void)
861 int i/*, j*/, vertexlit, rotated, transform;
864 vec3_t org, temp, forward, right, up;
868 model = currentrenderentity->model;
872 VectorSubtract (r_origin, currentrenderentity->origin, modelorg);
875 if (currentrenderentity->angles[0] || currentrenderentity->angles[1] || currentrenderentity->angles[2])
879 VectorCopy (modelorg, temp);
880 AngleVectors (currentrenderentity->angles, forward, right, up);
881 modelorg[0] = DotProduct (temp, forward);
882 modelorg[1] = -DotProduct (temp, right);
883 modelorg[2] = DotProduct (temp, up);
885 else if (currentrenderentity->origin[0] || currentrenderentity->origin[1] || currentrenderentity->origin[2] || currentrenderentity->scale)
889 softwaretransformforbrushentity (currentrenderentity);
891 for (i = 0, s = &model->surfaces[model->firstmodelsurface];i < model->nummodelsurfaces;i++, s++)
894 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
895 s->visframe = r_framecount;
898 // calculate dynamic lighting for bmodel if it's not an instanced model
899 for (i = 0;i < MAX_DLIGHTS;i++)
901 if (!cl_dlights[i].radius)
906 VectorSubtract(cl_dlights[i].origin, currentrenderentity->origin, temp);
907 org[0] = DotProduct (temp, forward);
908 org[1] = -DotProduct (temp, right);
909 org[2] = DotProduct (temp, up);
912 VectorSubtract(cl_dlights[i].origin, currentrenderentity->origin, org);
913 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, model);
915 vertexlit = vertexworld || currentrenderentity->alpha != 1 || model->firstmodelsurface == 0 || (currentrenderentity->effects & EF_FULLBRIGHT) || currentrenderentity->colormod[0] != 1 || currentrenderentity->colormod[2] != 1 || currentrenderentity->colormod[2] != 1;
918 for (i = 0, s = &model->surfaces[model->firstmodelsurface];i < model->nummodelsurfaces;i++, s++)
920 if (s->visframe == r_framecount)
922 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
926 for (p = s->polys;p;p = p->next)
928 for (j = 0;j < p->numverts;j++)
929 softwaretransform(&p->verts[j][0], bmverts + j * 3);
930 R_Clip_AddPolygon(bmverts, p->numverts, 3 * sizeof(float), (s->flags & SURF_CLIPSOLID) != 0 && currentrenderentity->alpha == 1, RBrushModelSurf_Callback, s, e, NULL);
937 t = R_TextureAnimation(s->texinfo->texture);
938 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
940 // sky and liquid don't need sorting (skypoly/transpoly)
941 if (s->flags & SURF_DRAWSKY)
942 RSurf_DrawSky(s, transform);
944 RSurf_DrawWater(s, t, transform, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
948 if (t->transparent || vertexlit)
949 RSurf_DrawWallVertex(s, t, transform, true);
951 RSurf_DrawWall(s, t, transform);
960 =============================================================
964 =============================================================
968 static byte *worldvis;
970 void R_MarkLeaves (void)
972 static float noviscache;
973 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
976 r_oldviewleaf = r_viewleaf;
977 noviscache = r_novis.value;
979 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
983 void RSurf_Callback(void *data, void *junk)
985 ((msurface_t *)data)->visframe = r_framecount;
989 void RSurf_Callback(void *data, void *junk)
991 msurface_t *surf = data;
994 // if (surf->visframe == r_framecount)
997 surf->visframe = r_framecount;
1001 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1003 // sky and liquid don't need sorting (skypoly/transpoly)
1004 if (surf->flags & SURF_DRAWSKY)
1005 RSurf_DrawSky(surf, false);
1007 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1011 t = R_TextureAnimation(surf->texinfo->texture);
1013 RSurf_DrawWallVertex(surf, t, false, false);
1015 RSurf_DrawWall(surf, t, false);
1021 mleaf_t *r_oldviewleaf;
1022 int r_markvisframecount = 0;
1024 void R_MarkLeaves (void)
1026 static float noviscache;
1029 msurface_t *surf, **mark, **endmark;
1030 model_t *model = cl.worldmodel;
1031 // mportal_t *portal;
1036 // ignore testvis if the map just changed
1037 if (r_testvis.value && model->nodes->markvisframe == r_markvisframecount)
1040 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
1043 r_oldviewleaf = r_viewleaf;
1044 noviscache = r_novis.value;
1046 if ((in = r_viewleaf->compressed_vis))
1048 row = (model->numleafs+7)>>3;
1050 if (!r_testvis.value)
1051 r_markvisframecount++;
1053 // LordHavoc: mark the root node as visible, it will terminate all other ascensions
1054 model->nodes->markvisframe = r_markvisframecount;
1062 l = model->numleafs - (k << 3);
1065 for (i=0 ; i<l ; i++)
1069 leaf = &model->leafs[(k << 3)+i+1];
1070 node = (mnode_t *)leaf;
1073 node->markvisframe = r_markvisframecount;
1074 node = node->parent;
1076 while (node->markvisframecount != r_markvisframecount);
1087 // LordHavoc: no vis data, mark everything as visible
1088 model->nodes->markvisframe = r_markvisframecount;
1090 for (i = 1;i < model->numleafs;i++)
1092 node = (mnode_t *)&model->leafs[i];
1095 node->markvisframe = r_markvisframecount;
1096 node = node->parent;
1098 while (node->markvisframecount != r_markvisframecount);
1104 void R_SolidWorldNode (void)
1106 if (r_viewleaf->contents != CONTENTS_SOLID)
1109 mportal_t *p, *pstack[8192];
1110 msurface_t *surf, **mark, **endmark;
1114 // LordHavoc: portal-passage worldnode; follows portals leading
1115 // outward from viewleaf, if a portal leads offscreen it is not
1116 // followed, in indoor maps this can often cull a great deal of
1117 // geometry away when pvs data is not present (useful with pvs as well)
1120 leaf->worldnodeframe = r_framecount;
1125 leaf->visframe = r_framecount;
1127 if (leaf->nummarksurfaces)
1129 mark = leaf->firstmarksurface;
1130 endmark = mark + leaf->nummarksurfaces;
1136 // make sure surfaces are only processed once
1137 if (surf->worldnodeframe == r_framecount)
1139 surf->worldnodeframe = r_framecount;
1140 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1142 if (surf->flags & SURF_PLANEBACK)
1144 VectorNegate(surf->plane->normal, plane.normal);
1145 plane.dist = -surf->plane->dist;
1146 for (poly = surf->polys;poly;poly = poly->next)
1147 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1152 if (!(surf->flags & SURF_PLANEBACK))
1153 for (poly = surf->polys;poly;poly = poly->next)
1154 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1157 while (mark < endmark);
1164 // make sure surfaces are only processed once
1165 if (surf->worldnodeframe == r_framecount)
1167 surf->worldnodeframe = r_framecount;
1168 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1170 if (surf->flags & SURF_PLANEBACK)
1171 surf->visframe = r_framecount;
1175 if (!(surf->flags & SURF_PLANEBACK))
1176 surf->visframe = r_framecount;
1179 while (mark < endmark);
1183 // follow portals into other leafs
1185 for (;p;p = p->next)
1187 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1190 if (leaf->worldnodeframe != r_framecount)
1192 leaf->worldnodeframe = r_framecount;
1193 if (leaf->contents != CONTENTS_SOLID)
1195 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1197 pstack[portalstack++] = p;
1201 p = pstack[--portalstack];
1213 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1214 int nodestackpos = 0;
1216 // LordHavoc: recursive descending worldnode; if portals are not
1217 // available, this is a good last resort, can cull large amounts of
1218 // geometry, but is more time consuming than portal-passage and renders
1219 // things behind walls
1222 if (R_NotCulledBox(node->mins, node->maxs))
1224 if (node->numsurfaces)
1228 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1230 if (PlaneDiff (r_origin, node->plane) < 0)
1232 for (;surf < surfend;surf++)
1234 if (surf->flags & SURF_PLANEBACK)
1236 VectorNegate(surf->plane->normal, plane.normal);
1237 plane.dist = -surf->plane->dist;
1238 for (poly = surf->polys;poly;poly = poly->next)
1239 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1245 for (;surf < surfend;surf++)
1247 if (!(surf->flags & SURF_PLANEBACK))
1248 for (poly = surf->polys;poly;poly = poly->next)
1249 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1255 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1256 if (PlaneDiff (r_origin, node->plane) < 0)
1258 for (;surf < surfend;surf++)
1260 if (surf->flags & SURF_PLANEBACK)
1261 surf->visframe = r_framecount;
1266 for (;surf < surfend;surf++)
1268 if (!(surf->flags & SURF_PLANEBACK))
1269 surf->visframe = r_framecount;
1275 // recurse down the children
1276 if (node->children[0]->contents >= 0)
1278 if (node->children[1]->contents >= 0)
1280 if (nodestackpos < 8192)
1281 nodestack[nodestackpos++] = node->children[1];
1282 node = node->children[0];
1286 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1287 node = node->children[0];
1292 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1293 if (node->children[1]->contents >= 0)
1295 node = node->children[1];
1298 else if (nodestackpos > 0)
1300 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1301 node = nodestack[--nodestackpos];
1306 else if (nodestackpos > 0)
1308 node = nodestack[--nodestackpos];
1315 void RSurf_Callback(void *data, void *junk)
1317 ((msurface_t *)data)->visframe = r_framecount;
1320 int R_FrustumTestPolygon(float *points, int numpoints, int stride);
1322 void RSurf_DoVisible(msurface_t *surf)
1325 for (p = surf->polys;p;p = p->next)
1326 if (R_FrustumTestPolygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float)) >= 3)
1327 // R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), true, RSurf_Callback, surf, 1);
1328 // if (R_Clip_Polygon((float *) p->verts, p->numverts, VERTEXSIZE * sizeof(float), surf->flags & SURF_CLIPSOLID))
1329 surf->visframe = r_framecount;
1333 //mleaf_t *llistbuffer[32768], *l, **llist;
1336 void RSurfLeaf_Callback(void *data)
1338 int portalstackpos = 0;
1340 mportal_t *p, *portalstack[32768];
1341 msurface_t *surf, **mark, **endmark;
1346 if (leaf->visframe == r_framecount)
1348 leaf->visframe = r_framecount;
1352 if (leaf->nummarksurfaces)
1354 mark = leaf->firstmarksurface;
1355 endmark = mark + leaf->nummarksurfaces;
1359 // make sure surfaces are only processed once
1360 if (surf->worldnodeframe == r_framecount)
1362 surf->worldnodeframe = r_framecount;
1363 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1365 if (surf->flags & SURF_PLANEBACK)
1366 RSurf_DoVisible(surf);
1370 if (!(surf->flags & SURF_PLANEBACK))
1371 RSurf_DoVisible(surf);
1374 while (mark < endmark);
1377 // follow portals into other leafs
1378 for (p = leaf->portals;p;p = p->next)
1380 if (p->past->visframe != r_framecount && DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1382 // R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3, RSurfLeaf_Callback, p->past, 1);
1383 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1384 portalstack[portalstackpos++] = p;
1388 while(portalstackpos);
1389 RSurfLeaf_Callback(p->past);
1390 // 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
1395 // experimental and inferior to the other in recursion depth allowances
1396 void R_PortalWorldNode (void)
1400 msurface_t *surf, **mark, **endmark;
1401 mleaf_t *leaf, *llistbuffer[32768], **l, **llist;
1404 leaf->visframe = r_framecount;
1405 l = llist = &llistbuffer[0];
1406 *llist++ = r_viewleaf;
1413 if (leaf->nummarksurfaces)
1415 mark = leaf->firstmarksurface;
1416 endmark = mark + leaf->nummarksurfaces;
1420 // make sure surfaces are only processed once
1421 if (surf->worldnodeframe == r_framecount)
1423 surf->worldnodeframe = r_framecount;
1424 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1426 if (surf->flags & SURF_PLANEBACK)
1427 RSurf_DoVisible(surf);
1431 if (!(surf->flags & SURF_PLANEBACK))
1432 RSurf_DoVisible(surf);
1435 while (mark < endmark);
1438 // follow portals into other leafs
1439 for (p = leaf->portals;p;p = p->next)
1441 if (p->past->visframe != r_framecount)
1443 if (R_Clip_Portal((float *) p->points, p->numpoints, sizeof(float) * 3))
1445 p->past->visframe = r_framecount;
1451 // for (p = leaf->portals;p;p = p->next)
1454 // if (leaf->worldnodeframe != r_framecount)
1456 // leaf->worldnodeframe = r_framecount;
1457 // i = (leaf - cl.worldmodel->leafs) - 1;
1458 // if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1466 // p = r_viewleaf->portals;
1467 // for (;p;p = p->next)
1470 // if (p->past->worldnodeframe != r_framecount)
1474 // Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1479 int r_portalframecount = 0;
1482 void R_Portal_Callback(void *data, void *data2)
1484 mleaf_t *leaf = data;
1485 if (!r_testvis.value)
1486 ((mportal_t *)data2)->visframe = r_portalframecount;
1487 if (leaf->visframe != r_framecount)
1490 leaf->visframe = r_framecount;
1495 void R_PVSWorldNode()
1498 mportal_t *p, *pstack[8192];
1499 msurface_t *surf, **mark, **endmark;
1505 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1508 leaf->worldnodeframe = r_framecount;
1513 leaf->visframe = r_framecount;
1515 if (leaf->nummarksurfaces)
1517 mark = leaf->firstmarksurface;
1518 endmark = mark + leaf->nummarksurfaces;
1524 // make sure surfaces are only processed once
1525 if (surf->worldnodeframe == r_framecount)
1527 surf->worldnodeframe = r_framecount;
1528 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1530 if (surf->flags & SURF_PLANEBACK)
1532 VectorNegate(surf->plane->normal, plane.normal);
1533 plane.dist = -surf->plane->dist;
1534 for (poly = surf->polys;poly;poly = poly->next)
1535 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1540 if (!(surf->flags & SURF_PLANEBACK))
1541 for (poly = surf->polys;poly;poly = poly->next)
1542 R_Clip_AddPolygon((float *)poly->verts, poly->numverts, VERTEXSIZE * sizeof(float), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1545 while (mark < endmark);
1552 // make sure surfaces are only processed once
1553 if (surf->worldnodeframe == r_framecount)
1555 surf->worldnodeframe = r_framecount;
1556 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1558 if (surf->flags & SURF_PLANEBACK)
1559 surf->visframe = r_framecount;
1563 if (!(surf->flags & SURF_PLANEBACK))
1564 surf->visframe = r_framecount;
1567 while (mark < endmark);
1571 // follow portals into other leafs
1573 for (;p;p = p->next)
1575 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1578 if (leaf->worldnodeframe != r_framecount)
1580 leaf->worldnodeframe = r_framecount;
1581 if (leaf->contents != CONTENTS_SOLID)
1583 i = (leaf - cl.worldmodel->leafs) - 1;
1584 if (worldvis[i>>3] & (1<<(i&7)))
1586 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1588 pstack[portalstack++] = p;
1592 p = pstack[--portalstack];
1604 entity_t clworldent;
1606 void R_DrawSurfaces (void)
1608 msurface_t *surf, *endsurf;
1609 texture_t *t, *currentt;
1610 int vertex = gl_vertex.value;
1612 currentrenderentity = &clworldent.render;
1613 softwaretransformidentity();
1614 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1615 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1616 t = currentt = NULL;
1617 for (;surf < endsurf;surf++)
1619 if (surf->visframe == r_framecount)
1622 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1624 if (surf->flags & SURF_DRAWSKY)
1625 RSurf_DrawSky(surf, false);
1628 if (currentt != surf->texinfo->texture)
1630 currentt = surf->texinfo->texture;
1631 t = R_TextureAnimation(surf->texinfo->texture);
1633 RSurf_DrawWater(surf, t, false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1638 if (currentt != surf->texinfo->texture)
1640 currentt = surf->texinfo->texture;
1641 t = R_TextureAnimation(surf->texinfo->texture);
1644 RSurf_DrawWallVertex(surf, t, false, false);
1646 RSurf_DrawWall(surf, t, false);
1652 void R_DrawPortals(void)
1654 int drawportals, i, r, g, b;
1655 // mleaf_t *leaf, *endleaf;
1656 mportal_t *portal, *endportal;
1657 mvertex_t *point/*, *endpoint*/;
1658 drawportals = (int)r_drawportals.value;
1659 if (drawportals < 1)
1662 leaf = cl.worldmodel->leafs;
1663 endleaf = leaf + cl.worldmodel->numleafs;
1664 for (;leaf < endleaf;leaf++)
1666 if (leaf->visframe == r_framecount && leaf->portals)
1668 i = leaf - cl.worldmodel->leafs;
1669 r = (i & 0x0007) << 5;
1670 g = (i & 0x0038) << 2;
1671 b = (i & 0x01C0) >> 1;
1672 portal = leaf->portals;
1675 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1676 point = portal->points + portal->numpoints - 1;
1677 endpoint = portal->points;
1678 for (;point >= endpoint;point--)
1679 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1681 portal = portal->next;
1686 portal = cl.worldmodel->portals;
1687 endportal = portal + cl.worldmodel->numportals;
1688 for (;portal < endportal;portal++)
1690 if (portal->visframe == r_portalframecount)
1692 i = portal - cl.worldmodel->portals;
1693 r = (i & 0x0007) << 5;
1694 g = (i & 0x0038) << 2;
1695 b = (i & 0x01C0) >> 1;
1696 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1697 point = portal->points;
1698 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1700 for (i = portal->numpoints - 1;i >= 0;i--)
1701 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1705 for (i = 0;i < portal->numpoints;i++)
1706 transpolyvertub(point[i].position[0], point[i].position[1], point[i].position[2], 0, 0, r, g, b, 32);
1713 void R_SetupWorldEnt(void)
1715 memset (&clworldent, 0, sizeof(clworldent));
1716 clworldent.render.model = cl.worldmodel;
1717 clworldent.render.colormod[0] = clworldent.render.colormod[1] = clworldent.render.colormod[2] = 1;
1718 clworldent.render.alpha = 1;
1719 clworldent.render.scale = 1;
1721 VectorCopy (r_origin, modelorg);
1723 currentrenderentity = &clworldent.render;
1731 void R_DrawWorld (void)
1733 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1734 vertexworld = gl_vertex.value;
1738 softwaretransformidentity(); // LordHavoc: clear transform
1740 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.value || r_viewleaf->compressed_vis == NULL)
1741 R_SolidWorldNode ();
1747 =============================================================================
1751 =============================================================================
1754 // returns a texture number and the position inside it
1755 int AllocBlock (int w, int h, short *x, short *y)
1761 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
1763 best = BLOCK_HEIGHT;
1765 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: align updates on 4 byte boundaries
1769 for (j=0 ; j<w ; j++)
1771 if (allocated[texnum][i+j] >= best)
1773 if (allocated[texnum][i+j] > best2)
1774 best2 = allocated[texnum][i+j];
1777 { // this is a valid spot
1783 if (best + h > BLOCK_HEIGHT)
1786 if (nosubimagefragments || nosubimage)
1788 if (!lightmaps[texnum])
1790 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1791 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1794 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1795 else if (!allocated[texnum][0])
1797 memset(templight, 0, sizeof(templight));
1800 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1801 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1802 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1804 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1806 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, templight);
1810 for (i = 0;i < w;i++)
1811 allocated[texnum][*x + i] = best + h;
1816 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1825 BuildSurfaceDisplayList
1828 void BuildSurfaceDisplayList (model_t *model, mvertex_t *vertices, msurface_t *fa)
1830 int i, j, lindex, lnumverts;
1836 // reconstruct the polygon
1837 pedges = model->edges;
1838 lnumverts = fa->numedges;
1843 poly = Hunk_AllocName (sizeof(glpolysizeof_t) + lnumverts * sizeof(float[VERTEXSIZE]), "surfaces");
1844 poly->next = fa->polys;
1846 // poly->flags = fa->flags;
1847 poly->numverts = lnumverts;
1849 for (i=0 ; i<lnumverts ; i++)
1851 lindex = model->surfedges[fa->firstedge + i];
1854 vec = vertices[pedges[lindex].v[0]].position;
1856 vec = vertices[pedges[-lindex].v[1]].position;
1858 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1859 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1861 VectorCopy (vec, poly->verts[i]);
1862 poly->verts[i][3] = s / fa->texinfo->texture->width;
1863 poly->verts[i][4] = t / fa->texinfo->texture->height;
1866 // lightmap texture coordinates
1868 s -= fa->texturemins[0];
1869 t -= fa->texturemins[1];
1872 // LordHavoc: calc lightmap data offset
1873 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;
1874 poly->verts[i][7] = j;
1875 s += fa->light_s*16;
1876 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1878 t += fa->light_t*16;
1879 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1881 poly->verts[i][5] = s;
1882 poly->verts[i][6] = t;
1886 // remove co-linear points - Ed
1889 if (!gl_keeptjunctions.value)
1891 for (i = 0 ; i < lnumverts ; ++i)
1894 float *prev, *this, *next;
1896 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1897 this = poly->verts[i];
1898 next = poly->verts[(i + 1) % lnumverts];
1900 VectorSubtract( this, prev, v1 );
1901 VectorNormalize( v1 );
1902 VectorSubtract( next, prev, v2 );
1903 VectorNormalize( v2 );
1905 // skip co-linear points
1906 #define COLINEAR_EPSILON 0.001
1907 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1908 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1909 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1912 for (j = i + 1; j < lnumverts; ++j)
1915 for (k = 0; k < VERTEXSIZE; ++k)
1916 poly->verts[j - 1][k] = poly->verts[j][k];
1920 // retry next vertex next time, which is now current vertex
1924 poly->numverts = lnumverts;
1930 ========================
1931 GL_CreateSurfaceLightmap
1932 ========================
1934 void GL_CreateSurfaceLightmap (msurface_t *surf)
1938 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1941 smax = (surf->extents[0]>>4)+1;
1942 tmax = (surf->extents[1]>>4)+1;
1944 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1945 if (nosubimage || nosubimagefragments)
1947 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1948 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1951 R_BuildLightMap (surf, templight, smax * 4, false);
1953 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1957 R_BuildLightMap (surf, templight, smax * 3, false);
1959 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1968 Builds the lightmap texture
1969 with all the surfaces from all brush models
1972 void GL_BuildLightmaps (void)
1977 memset (allocated, 0, sizeof(allocated));
1979 r_framecount = 1; // no dlightcache
1981 if (gl_nosubimagefragments.value)
1982 nosubimagefragments = 1;
1984 nosubimagefragments = 0;
1986 if (gl_nosubimage.value)
1991 if (gl_lightmaprgba.value)
1993 lightmaprgba = true;
1998 lightmaprgba = false;
2002 // LordHavoc: TexSubImage2D needs data aligned on 4 byte boundaries unless
2003 // I specify glPixelStorei(GL_UNPACK_ALIGNMENT, 1), I suspect 4 byte may be
2004 // faster anyway, so I implemented an adjustable lightmap alignment...
2006 // validate the lightmap alignment
2008 while (i < 16 && i < gl_lightmapalign.value)
2010 Cvar_SetValue("gl_lightmapalign", i);
2012 // find the lowest pixel count which satisfies the byte alignment
2014 j = lightmaprgba ? 4 : 3; // bytes per pixel
2015 while ((lightmapalign * j) & (i - 1))
2016 lightmapalign <<= 1;
2017 lightmapalignmask = ~(lightmapalign - 1);
2019 // alignment is irrelevant if using fallback modes
2020 if (nosubimagefragments || nosubimage)
2023 lightmapalignmask = ~0;
2026 if (!lightmap_textures)
2027 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
2029 // need a world entity for lightmap code
2032 for (j=1 ; j<MAX_MODELS ; j++)
2034 m = cl.model_precache[j];
2037 if (m->name[0] == '*')
2039 for (i=0 ; i<m->numsurfaces ; i++)
2041 if ( m->surfaces[i].flags & SURF_DRAWTURB )
2043 if ( m->surfaces[i].flags & SURF_DRAWSKY )
2045 GL_CreateSurfaceLightmap (m->surfaces + i);
2046 BuildSurfaceDisplayList (m, m->vertexes, m->surfaces + i);
2050 if (nosubimage || nosubimagefragments)
2052 // LordHavoc: switch to second TMU as an upload hint for voodoo2
2053 // (don't know if it really pays attention or not, but original
2054 // glquake did this...)
2057 qglActiveTexture(GL_TEXTURE1_ARB);
2058 for (i = 0;i < MAX_LIGHTMAPS;i++)
2060 if (!allocated[i][0])
2062 lightmapupdate[i][0] = BLOCK_HEIGHT;
2063 lightmapupdate[i][1] = 0;
2066 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
2067 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
2068 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
2070 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
2072 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
2077 qglActiveTexture(GL_TEXTURE0_ARB);