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 #define MAX_LIGHTMAP_SIZE 256
26 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31 cvar_t r_ambient = {0, "r_ambient", "0"};
32 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
33 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
37 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
39 static int dlightdivtable[32768];
41 static int R_IntAddDynamicLights (msurface_t *surf)
43 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
45 float dist, impact[3], local[3];
47 // LordHavoc: use 64bit integer... shame it's not very standardized...
48 #if _MSC_VER || __BORLANDC__
56 smax = (surf->extents[0] >> 4) + 1;
57 tmax = (surf->extents[1] >> 4) + 1;
60 for (lnum = 0; lnum < r_numdlights; lnum++)
62 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
63 continue; // not lit by this light
65 softwareuntransform(r_dlight[lnum].origin, local);
66 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
68 // for comparisons to minimum acceptable light
69 // compensate for LIGHTOFFSET
70 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
77 if (surf->plane->type < 3)
79 VectorCopy(local, impact);
80 impact[surf->plane->type] -= dist;
84 impact[0] = local[0] - surf->plane->normal[0] * dist;
85 impact[1] = local[1] - surf->plane->normal[1] * dist;
86 impact[2] = local[2] - surf->plane->normal[2] * dist;
89 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
90 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
92 s = bound(0, impacts, smax * 16) - impacts;
93 t = bound(0, impactt, tmax * 16) - impactt;
94 i = s * s + t * t + dist2;
98 // reduce calculations
99 for (s = 0, i = impacts; s < smax; s++, i -= 16)
100 sdtable[s] = i * i + dist2;
102 maxdist3 = maxdist - dist2;
104 // convert to 8.8 blocklights format
105 red = r_dlight[lnum].light[0];
106 green = r_dlight[lnum].light[1];
107 blue = r_dlight[lnum].light[2];
108 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
112 for (t = 0;t < tmax;t++, i -= 16)
115 // make sure some part of it is visible on this line
118 maxdist2 = maxdist - td;
119 for (s = 0;s < smax;s++)
121 if (sdtable[s] < maxdist2)
123 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
126 bl[0] += (red * k) >> 7;
127 bl[1] += (green * k) >> 7;
128 bl[2] += (blue * k) >> 7;
142 static int R_FloatAddDynamicLights (msurface_t *surf)
144 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
145 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
149 smax = (surf->extents[0] >> 4) + 1;
150 tmax = (surf->extents[1] >> 4) + 1;
153 for (lnum = 0; lnum < r_numdlights; lnum++)
155 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
156 continue; // not lit by this light
158 softwareuntransform(r_dlight[lnum].origin, local);
159 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
161 // for comparisons to minimum acceptable light
162 // compensate for LIGHTOFFSET
163 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
166 dist2 += LIGHTOFFSET;
167 if (dist2 >= maxdist)
170 if (surf->plane->type < 3)
172 VectorCopy(local, impact);
173 impact[surf->plane->type] -= dist;
177 impact[0] = local[0] - surf->plane->normal[0] * dist;
178 impact[1] = local[1] - surf->plane->normal[1] * dist;
179 impact[2] = local[2] - surf->plane->normal[2] * dist;
182 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
183 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
185 td = bound(0, impacts, smax * 16) - impacts;
186 td1 = bound(0, impactt, tmax * 16) - impactt;
187 td = td * td + td1 * td1 + dist2;
191 // reduce calculations
192 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
193 sdtable[s] = td1 * td1 + dist2;
195 maxdist3 = maxdist - dist2;
197 // convert to 8.8 blocklights format
198 red = r_dlight[lnum].light[0];
199 green = r_dlight[lnum].light[1];
200 blue = r_dlight[lnum].light[2];
201 subtract = r_dlight[lnum].subtract * 32768.0f;
202 bl = floatblocklights;
205 for (t = 0;t < tmax;t++, td1 -= 16.0f)
208 // make sure some part of it is visible on this line
211 maxdist2 = maxdist - td;
212 for (s = 0;s < smax;s++)
214 if (sdtable[s] < maxdist2)
216 k = (32768.0f / (sdtable[s] + td)) - subtract;
236 Combine and scale multiple lightmaps into the 8.8 format in blocklights
239 static void R_BuildLightMap (entity_render_t *ent, msurface_t *surf, int dlightchanged)
241 if (!r_floatbuildlightmap.integer)
243 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
244 unsigned int *bl, scale;
245 qbyte *lightmap, *out, *stain;
247 // update cached lighting info
248 surf->cached_dlight = 0;
249 surf->cached_lightscalebit = lightscalebit;
250 surf->cached_ambient = r_ambient.value;
251 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
252 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
253 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
254 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
256 smax = (surf->extents[0]>>4)+1;
257 tmax = (surf->extents[1]>>4)+1;
260 lightmap = surf->samples;
262 // set to full bright if no light data
264 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
266 for (i = 0;i < size3;i++)
272 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
275 for (i = 0;i < size3;i++)
279 memset(bl, 0, size*3*sizeof(unsigned int));
281 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
283 surf->cached_dlight = R_IntAddDynamicLights(surf);
284 if (surf->cached_dlight)
286 else if (dlightchanged)
287 return; // don't upload if only updating dlights and none mattered
290 // add all the lightmaps
294 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
295 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
296 bl[i] += lightmap[i] * scale;
300 stain = surf->stainsamples;
303 // deal with lightmap brightness scale
304 shift = 7 + lightscalebit + 8;
305 if (ent->model->lightmaprgba)
307 stride = (surf->lightmaptexturestride - smax) * 4;
308 for (i = 0;i < tmax;i++, out += stride)
310 for (j = 0;j < smax;j++)
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
321 stride = (surf->lightmaptexturestride - smax) * 3;
322 for (i = 0;i < tmax;i++, out += stride)
324 for (j = 0;j < smax;j++)
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
328 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
333 R_UpdateTexture(surf->lightmaptexture, templight);
337 int smax, tmax, i, j, size, size3, maps, stride, l;
339 qbyte *lightmap, *out, *stain;
341 // update cached lighting info
342 surf->cached_dlight = 0;
343 surf->cached_lightscalebit = lightscalebit;
344 surf->cached_ambient = r_ambient.value;
345 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
346 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
347 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
348 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
350 smax = (surf->extents[0]>>4)+1;
351 tmax = (surf->extents[1]>>4)+1;
354 lightmap = surf->samples;
356 // set to full bright if no light data
357 bl = floatblocklights;
358 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
361 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
366 for (i = 0;i < size3;i++)
370 memset(bl, 0, size*3*sizeof(float));
372 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
374 surf->cached_dlight = R_FloatAddDynamicLights(surf);
375 if (surf->cached_dlight)
377 else if (dlightchanged)
378 return; // don't upload if only updating dlights and none mattered
381 // add all the lightmaps
384 bl = floatblocklights;
385 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
386 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
387 bl[i] += lightmap[i] * scale;
390 stain = surf->stainsamples;
391 bl = floatblocklights;
393 // deal with lightmap brightness scale
394 scale = 1.0f / (1 << (7 + lightscalebit + 8));
395 if (ent->model->lightmaprgba)
397 stride = (surf->lightmaptexturestride - smax) * 4;
398 for (i = 0;i < tmax;i++, out += stride)
400 for (j = 0;j < smax;j++)
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
404 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411 stride = (surf->lightmaptexturestride - smax) * 3;
412 for (i = 0;i < tmax;i++, out += stride)
414 for (j = 0;j < smax;j++)
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
423 R_UpdateTexture(surf->lightmaptexture, templight);
427 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
430 msurface_t *surf, *endsurf;
431 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
434 // LordHavoc: use 64bit integer... shame it's not very standardized...
435 #if _MSC_VER || __BORLANDC__
442 // for comparisons to minimum acceptable light
443 // compensate for 256 offset
444 maxdist = radius * radius + 256.0f;
446 // clamp radius to avoid exceeding 32768 entry division table
447 if (maxdist > 4194304)
450 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
453 if (node->contents < 0)
455 ndist = PlaneDiff(origin, node->plane);
458 node = node->children[0];
463 node = node->children[1];
467 dist2 = ndist * ndist + 256.0f;
470 maxdist3 = maxdist - dist2;
472 if (node->plane->type < 3)
474 VectorCopy(origin, impact);
475 impact[node->plane->type] -= ndist;
479 impact[0] = origin[0] - node->plane->normal[0] * ndist;
480 impact[1] = origin[1] - node->plane->normal[1] * ndist;
481 impact[2] = origin[2] - node->plane->normal[2] * ndist;
484 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
486 if (surf->stainsamples)
488 smax = (surf->extents[0] >> 4) + 1;
489 tmax = (surf->extents[1] >> 4) + 1;
491 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
492 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
494 s = bound(0, impacts, smax * 16) - impacts;
495 t = bound(0, impactt, tmax * 16) - impactt;
496 i = s * s + t * t + dist2;
500 // reduce calculations
501 for (s = 0, i = impacts; s < smax; s++, i -= 16)
502 sdtable[s] = i * i + dist2;
504 // convert to 8.8 blocklights format
505 bl = surf->stainsamples;
510 for (t = 0;t < tmax;t++, i -= 16)
513 // make sure some part of it is visible on this line
516 maxdist2 = maxdist - td;
517 for (s = 0;s < smax;s++)
519 if (sdtable[s] < maxdist2)
521 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
524 ratio = rand() & 255;
525 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
529 a = bound(0, a, 256);
530 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
531 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
532 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
533 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
534 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
535 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
546 // force lightmap upload
548 surf->cached_dlight = true;
553 if (node->children[0]->contents >= 0)
555 if (node->children[1]->contents >= 0)
557 R_StainNode(node->children[0], model, origin, radius, icolor);
558 node = node->children[1];
563 node = node->children[0];
567 else if (node->children[1]->contents >= 0)
569 node = node->children[1];
574 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
577 entity_render_t *ent;
589 model = cl.worldmodel;
590 softwaretransformidentity();
591 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
593 // look for embedded bmodels
594 for (n = 0;n < cl_num_brushmodel_entities;n++)
596 ent = cl_brushmodel_entities[n];
598 if (model && model->name[0] == '*')
600 Mod_CheckLoaded(model);
601 if (model->type == mod_brush)
603 softwaretransformforentity(ent);
604 softwareuntransform(origin, org);
605 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
613 =============================================================
617 =============================================================
620 static void RSurfShader_Sky(entity_render_t *ent, msurface_t *firstsurf)
627 float cr, cg, cb, ca;
630 // LordHavoc: HalfLife maps have freaky skypolys...
631 if (ent->model->ishlbsp)
636 skyrendernow = false;
640 for (surf = firstsurf;surf;surf = surf->chain)
642 // draw depth-only polys
643 memset(&m, 0, sizeof(m));
646 m.blendfunc1 = GL_ZERO;
647 m.blendfunc2 = GL_ONE;
652 m.blendfunc1 = GL_ONE;
653 m.blendfunc2 = GL_ZERO;
655 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
657 m.numtriangles = mesh->numtriangles;
658 m.numverts = mesh->numverts;
659 if (R_Mesh_Draw_GetBuffer(&m, false))
661 cr = fogcolor[0] * m.colorscale;
662 cg = fogcolor[1] * m.colorscale;
663 cb = fogcolor[2] * m.colorscale;
665 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
666 for (i = 0, v = mesh->vertex, outv = m.vertex;i < m.numverts;i++, v++, outv += 4, outc += 4)
668 softwaretransform(v->v, outv);
670 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
683 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
686 int i, l, lit = false;
689 for (l = 0;l < r_numdlights;l++)
691 if (dlightbits[l >> 5] & (1 << (l & 31)))
694 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
695 VectorCopy(rd->origin, lightorigin);
696 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
698 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
699 if (f < rd->cullradius2)
701 f = (1.0f / f) - rd->subtract;
702 VectorMA(c, f, rd->light, c);
711 // note: this untransforms lights to do the checking,
712 // and takes surf->mesh->vertex data
713 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
719 for (l = 0;l < r_numdlights;l++)
721 if (dlightbits[l >> 5] & (1 << (l & 31)))
724 softwareuntransform(rd->origin, lightorigin);
725 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
726 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
733 static void RSurfShader_Water_Pass_Base(entity_render_t *ent, msurface_t *surf)
737 float *outv, *outc, *outst, cl, diff[3];
738 float base[3], scale, f;
742 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
743 memset(&m, 0, sizeof(m));
744 if (ent->effects & EF_ADDITIVE)
746 m.transparent = true;
747 m.blendfunc1 = GL_SRC_ALPHA;
748 m.blendfunc2 = GL_ONE;
750 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
752 m.transparent = true;
753 m.blendfunc1 = GL_SRC_ALPHA;
754 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
758 m.blendfunc1 = GL_ONE;
759 m.blendfunc2 = GL_ZERO;
761 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
762 if (surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT)
764 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
766 m.numtriangles = mesh->numtriangles;
767 m.numverts = mesh->numverts;
768 if (R_Mesh_Draw_GetBuffer(&m, true))
770 base[0] = base[1] = base[2] = 1.0f * m.colorscale;
771 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
772 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
774 softwaretransform(v->v, outv);
776 VectorCopy(base, outc);
782 VectorSubtract(outv, r_origin, diff);
783 f = 1 - exp(fogdensity/DotProduct(diff, diff));
784 VectorScale(outc, f, outc);
793 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
794 base[0] = base[1] = base[2] = (r_ambient.value * (1.0f / 64.0f) + ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f));
795 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
797 m.numtriangles = mesh->numtriangles;
798 m.numverts = mesh->numverts;
799 if (R_Mesh_Draw_GetBuffer(&m, true))
802 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
803 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
805 softwaretransform(v->v, outv);
807 VectorCopy(base, outc);
812 if (surf->dlightframe == r_framecount)
813 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
814 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
816 if (surf->flags & SURF_LIGHTMAP)
817 if (surf->styles[0] != 255)
819 lm = surf->samples + v->lightmapoffset;
820 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
821 VectorMA(outc, scale, lm, outc);
822 if (surf->styles[1] != 255)
825 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
826 VectorMA(outc, scale, lm, outc);
827 if (surf->styles[2] != 255)
830 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
831 VectorMA(outc, scale, lm, outc);
832 if (surf->styles[3] != 255)
835 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
836 VectorMA(outc, scale, lm, outc);
843 VectorSubtract(outv, r_origin, diff);
844 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
845 VectorScale(outc, f, outc);
848 VectorScale(outc, cl, outc);
856 static void RSurfShader_Water_Pass_Fog(entity_render_t *ent, msurface_t *surf)
860 float *outv, *outc, *outst, diff[3];
864 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
865 memset(&m, 0, sizeof(m));
866 m.transparent = ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || alpha < 1;
867 m.blendfunc1 = GL_SRC_ALPHA;
868 m.blendfunc2 = GL_ONE;
869 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
870 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
872 m.numtriangles = mesh->numtriangles;
873 m.numverts = mesh->numverts;
874 if (R_Mesh_Draw_GetBuffer(&m, false))
876 VectorScale(fogcolor, m.colorscale, base);
877 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
878 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
880 softwaretransform(v->v, outv);
882 VectorSubtract(outv, r_origin, diff);
883 f = exp(fogdensity/DotProduct(diff, diff));
884 VectorScale(base, f, outc);
889 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
900 static void RSurfShader_Water(entity_render_t *ent, msurface_t *firstsurf)
903 for (surf = firstsurf;surf;surf = surf->chain)
904 RSurfShader_Water_Pass_Base(ent, surf);
906 for (surf = firstsurf;surf;surf = surf->chain)
907 RSurfShader_Water_Pass_Fog(ent, surf);
910 static void RSurfShader_Wall_Pass_BaseVertex(entity_render_t *ent, msurface_t *surf)
914 float *outv, *outc, *outst, cl, ca, diff[3];
915 float base[3], scale, f;
919 memset(&m, 0, sizeof(m));
920 if (ent->effects & EF_ADDITIVE)
922 m.transparent = true;
923 m.blendfunc1 = GL_SRC_ALPHA;
924 m.blendfunc2 = GL_ONE;
926 else if (surf->currenttexture->fogtexture != NULL || ent->alpha != 1)
928 m.transparent = true;
929 m.blendfunc1 = GL_SRC_ALPHA;
930 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
934 m.blendfunc1 = GL_ONE;
935 m.blendfunc2 = GL_ZERO;
937 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
939 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
941 base[0] = base[1] = base[2] = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
944 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
946 m.numtriangles = mesh->numtriangles;
947 m.numverts = mesh->numverts;
949 if (R_Mesh_Draw_GetBuffer(&m, true))
952 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
954 if (ent->effects & EF_FULLBRIGHT)
956 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
958 softwaretransform(v->v, outv);
960 VectorSubtract(outv, r_origin, diff);
961 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
969 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
971 softwaretransform(v->v, outv);
973 VectorCopy(base, outc);
979 if (surf->dlightframe == r_framecount)
980 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
982 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
984 if (surf->styles[0] != 255)
986 lm = surf->samples + v->lightmapoffset;
987 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
988 VectorMA(outc, scale, lm, outc);
989 if (surf->styles[1] != 255)
992 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
993 VectorMA(outc, scale, lm, outc);
994 if (surf->styles[2] != 255)
997 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
998 VectorMA(outc, scale, lm, outc);
999 if (surf->styles[3] != 255)
1002 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1003 VectorMA(outc, scale, lm, outc);
1010 VectorSubtract(outv, r_origin, diff);
1011 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1012 VectorScale(outc, f, outc);
1015 VectorScale(outc, cl, outc);
1023 static void RSurfShader_Wall_Pass_BaseFullbright(entity_render_t *ent, msurface_t *surf)
1027 float *outv, *outc, *outst, cl, ca, diff[3];
1029 rmeshbufferinfo_t m;
1030 memset(&m, 0, sizeof(m));
1031 if (ent->effects & EF_ADDITIVE)
1033 m.transparent = true;
1034 m.blendfunc1 = GL_SRC_ALPHA;
1035 m.blendfunc2 = GL_ONE;
1037 else if (surf->currenttexture->fogtexture != NULL || ent->alpha != 1)
1039 m.transparent = true;
1040 m.blendfunc1 = GL_SRC_ALPHA;
1041 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1045 m.blendfunc1 = GL_ONE;
1046 m.blendfunc2 = GL_ZERO;
1048 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1050 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1052 m.numtriangles = mesh->numtriangles;
1053 m.numverts = mesh->numverts;
1055 if (R_Mesh_Draw_GetBuffer(&m, false))
1058 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1059 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1061 softwaretransform(v->v, outv);
1065 VectorSubtract(outv, r_origin, diff);
1066 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1069 outc[0] = outc[1] = outc[2] = cl;
1071 outst[0] = v->st[0];
1072 outst[1] = v->st[1];
1079 static void RSurfShader_Wall_Pass_Glow(entity_render_t *ent, msurface_t *surf)
1083 float *outv, *outc, *outst, cl, ca, diff[3];
1085 rmeshbufferinfo_t m;
1086 memset(&m, 0, sizeof(m));
1087 m.transparent = ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || ent->alpha != 1;
1088 m.blendfunc1 = GL_SRC_ALPHA;
1089 m.blendfunc2 = GL_ONE;
1090 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1092 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1094 m.numtriangles = mesh->numtriangles;
1095 m.numverts = mesh->numverts;
1097 if (R_Mesh_Draw_GetBuffer(&m, false))
1100 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1101 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1103 softwaretransform(v->v, outv);
1107 VectorSubtract(outv, r_origin, diff);
1108 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1111 outc[0] = outc[1] = outc[2] = cl;
1113 outst[0] = v->st[0];
1114 outst[1] = v->st[1];
1121 static void RSurfShader_Wall_Pass_Fog(entity_render_t *ent, msurface_t *surf)
1125 float *outv, *outc, *outst, cl, ca, diff[3], f;
1127 rmeshbufferinfo_t m;
1128 memset(&m, 0, sizeof(m));
1129 m.transparent = ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || ent->alpha != 1;
1130 m.blendfunc1 = GL_SRC_ALPHA;
1131 m.blendfunc2 = GL_ONE;
1133 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1135 m.numtriangles = mesh->numtriangles;
1136 m.numverts = mesh->numverts;
1138 if (R_Mesh_Draw_GetBuffer(&m, false))
1141 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1142 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1144 softwaretransform(v->v, outv);
1146 VectorSubtract(outv, r_origin, diff);
1147 f = cl * exp(fogdensity/DotProduct(diff, diff));
1148 VectorScale(fogcolor, f, outc);
1150 outst[0] = v->st[0];
1151 outst[1] = v->st[1];
1158 static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(entity_render_t *ent, msurface_t *surf)
1162 float *outv, *outc, *outst, *outuv, *outab, cl;
1164 rmeshbufferinfo_t m;
1165 memset(&m, 0, sizeof(m));
1166 m.blendfunc1 = GL_ONE;
1167 m.blendfunc2 = GL_ZERO;
1168 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1169 m.texrgbscale[0] = 1.0f;
1170 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1171 m.texrgbscale[1] = 4.0f;
1172 m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
1173 m.texrgbscale[2] = 2.0f;
1174 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1176 m.numtriangles = mesh->numtriangles;
1177 m.numverts = mesh->numverts;
1179 if (R_Mesh_Draw_GetBuffer(&m, false))
1181 cl = (float) (1 << lightscalebit) * m.colorscale;
1182 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1183 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1], outab = m.texcoords[2];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2, outab += 2)
1185 softwaretransform(v->v, outv);
1187 outc[0] = outc[1] = outc[2] = cl;
1189 outst[0] = v->st[0];
1190 outst[1] = v->st[1];
1191 outuv[0] = v->uv[0];
1192 outuv[1] = v->uv[1];
1193 outab[0] = v->ab[0];
1194 outab[1] = v->ab[1];
1201 static void RSurfShader_OpaqueWall_Pass_BaseMTex(entity_render_t *ent, msurface_t *surf)
1205 float *outv, *outc, *outst, *outuv, cl;
1207 rmeshbufferinfo_t m;
1208 memset(&m, 0, sizeof(m));
1209 m.blendfunc1 = GL_ONE;
1210 m.blendfunc2 = GL_ZERO;
1211 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1212 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1213 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1215 m.numtriangles = mesh->numtriangles;
1216 m.numverts = mesh->numverts;
1218 if (R_Mesh_Draw_GetBuffer(&m, true))
1220 cl = (float) (1 << lightscalebit) * m.colorscale;
1221 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1222 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1224 softwaretransform(v->v, outv);
1226 outc[0] = outc[1] = outc[2] = cl;
1228 outst[0] = v->st[0];
1229 outst[1] = v->st[1];
1230 outuv[0] = v->uv[0];
1231 outuv[1] = v->uv[1];
1238 static void RSurfShader_OpaqueWall_Pass_BaseTexture(entity_render_t *ent, msurface_t *surf)
1242 float *outv, *outc, *outst, cl;
1244 rmeshbufferinfo_t m;
1245 memset(&m, 0, sizeof(m));
1246 m.blendfunc1 = GL_ONE;
1247 m.blendfunc2 = GL_ZERO;
1248 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1249 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1251 m.numtriangles = mesh->numtriangles;
1252 m.numverts = mesh->numverts;
1254 if (R_Mesh_Draw_GetBuffer(&m, false))
1256 cl = (float) (1 << lightscalebit) * m.colorscale;
1257 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1258 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1260 softwaretransform(v->v, outv);
1262 outc[0] = outc[1] = outc[2] = cl;
1264 outst[0] = v->st[0];
1265 outst[1] = v->st[1];
1272 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(entity_render_t *ent, msurface_t *surf)
1276 float *outv, *outc, *outuv, cl;
1278 rmeshbufferinfo_t m;
1279 memset(&m, 0, sizeof(m));
1280 m.blendfunc1 = GL_ZERO;
1281 m.blendfunc2 = GL_SRC_COLOR;
1282 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1283 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1285 m.numtriangles = mesh->numtriangles;
1286 m.numverts = mesh->numverts;
1288 if (R_Mesh_Draw_GetBuffer(&m, true))
1290 cl = (float) (1 << lightscalebit) * m.colorscale;
1291 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1292 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1294 softwaretransform(v->v, outv);
1296 outc[0] = outc[1] = outc[2] = cl;
1298 outuv[0] = v->uv[0];
1299 outuv[1] = v->uv[1];
1306 static void RSurfShader_OpaqueWall_Pass_Light(entity_render_t *ent, msurface_t *surf)
1310 float *outv, *outc, *outst, cl;
1312 rmeshbufferinfo_t m;
1314 if (surf->dlightframe != r_framecount)
1316 if (ent->effects & EF_FULLBRIGHT)
1319 memset(&m, 0, sizeof(m));
1320 m.blendfunc1 = GL_SRC_ALPHA;
1321 m.blendfunc2 = GL_ONE;
1322 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1323 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1325 if (RSurf_LightCheck(surf->dlightbits, mesh))
1327 m.numtriangles = mesh->numtriangles;
1328 m.numverts = mesh->numverts;
1330 if (R_Mesh_Draw_GetBuffer(&m, true))
1333 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1334 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1336 softwaretransform(v->v, outv);
1340 outst[0] = v->st[0];
1341 outst[1] = v->st[1];
1343 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1345 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1346 VectorScale(outc, cl, outc);
1353 static void RSurfShader_OpaqueWall_Pass_Fog(entity_render_t *ent, msurface_t *surf)
1357 float *outv, *outc, cl, diff[3], fcolor[3];
1359 rmeshbufferinfo_t m;
1360 memset(&m, 0, sizeof(m));
1361 m.blendfunc1 = GL_SRC_ALPHA;
1362 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1363 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1365 m.numtriangles = mesh->numtriangles;
1366 m.numverts = mesh->numverts;
1368 if (R_Mesh_Draw_GetBuffer(&m, false))
1371 VectorScale(fogcolor, cl, fcolor);
1372 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1373 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1375 softwaretransform(v->v, outv);
1377 VectorCopy(fcolor, outc);
1378 VectorSubtract(outv, r_origin, diff);
1379 outc[3] = exp(fogdensity/DotProduct(diff, diff));
1386 static void RSurfShader_OpaqueWall_Pass_BaseDetail(entity_render_t *ent, msurface_t *surf)
1390 float *outv, *outc, *outst;
1392 rmeshbufferinfo_t m;
1393 memset(&m, 0, sizeof(m));
1394 m.blendfunc1 = GL_DST_COLOR;
1395 m.blendfunc2 = GL_SRC_COLOR;
1396 m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
1397 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1399 m.numtriangles = mesh->numtriangles;
1400 m.numverts = mesh->numverts;
1402 if (R_Mesh_Draw_GetBuffer(&m, false))
1404 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1405 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1407 softwaretransform(v->v, outv);
1409 outc[0] = outc[1] = outc[2] = outc[3] = 1;
1410 outst[0] = v->ab[0];
1411 outst[1] = v->ab[1];
1418 static void RSurfShader_OpaqueWall_Pass_Glow(entity_render_t *ent, msurface_t *surf)
1422 float *outv, *outc, *outst, cl;
1424 rmeshbufferinfo_t m;
1425 memset(&m, 0, sizeof(m));
1426 m.blendfunc1 = GL_SRC_ALPHA;
1427 m.blendfunc2 = GL_ONE;
1428 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1429 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1431 m.numtriangles = mesh->numtriangles;
1432 m.numverts = mesh->numverts;
1434 if (R_Mesh_Draw_GetBuffer(&m, false))
1437 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1438 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1440 softwaretransform(v->v, outv);
1442 outc[0] = outc[1] = outc[2] = cl;
1444 outst[0] = v->st[0];
1445 outst[1] = v->st[1];
1452 static void RSurfShader_Wall_Fullbright(entity_render_t *ent, msurface_t *firstsurf)
1455 for (surf = firstsurf;surf;surf = surf->chain)
1458 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1460 for (surf = firstsurf;surf;surf = surf->chain)
1461 if (surf->currenttexture->glowtexture)
1462 RSurfShader_Wall_Pass_Glow(ent, surf);
1464 for (surf = firstsurf;surf;surf = surf->chain)
1465 RSurfShader_Wall_Pass_Fog(ent, surf);
1468 static void RSurfShader_Wall_Vertex(entity_render_t *ent, msurface_t *firstsurf)
1471 for (surf = firstsurf;surf;surf = surf->chain)
1474 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1476 for (surf = firstsurf;surf;surf = surf->chain)
1477 if (surf->currenttexture->glowtexture)
1478 RSurfShader_Wall_Pass_Glow(ent, surf);
1480 for (surf = firstsurf;surf;surf = surf->chain)
1481 RSurfShader_Wall_Pass_Fog(ent, surf);
1484 static void RSurfShader_Wall_Lightmap(entity_render_t *ent, msurface_t *firstsurf)
1487 if (r_vertexsurfaces.integer || firstsurf->currenttexture->fogtexture != NULL || ent->alpha != 1 || ent->effects & EF_ADDITIVE)
1489 for (surf = firstsurf;surf;surf = surf->chain)
1492 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1494 for (surf = firstsurf;surf;surf = surf->chain)
1495 if (surf->currenttexture->glowtexture)
1496 RSurfShader_Wall_Pass_Glow(ent, surf);
1498 for (surf = firstsurf;surf;surf = surf->chain)
1499 RSurfShader_Wall_Pass_Fog(ent, surf);
1503 if (r_textureunits.integer >= 2)
1505 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1507 for (surf = firstsurf;surf;surf = surf->chain)
1510 RSurfShader_OpaqueWall_Pass_TripleTexCombine(ent, surf);
1515 for (surf = firstsurf;surf;surf = surf->chain)
1518 RSurfShader_OpaqueWall_Pass_BaseMTex(ent, surf);
1520 if (r_detailtextures.integer)
1521 for (surf = firstsurf;surf;surf = surf->chain)
1522 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1527 for (surf = firstsurf;surf;surf = surf->chain)
1530 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, surf);
1532 for (surf = firstsurf;surf;surf = surf->chain)
1533 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, surf);
1534 if (r_detailtextures.integer)
1535 for (surf = firstsurf;surf;surf = surf->chain)
1536 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1538 if (!r_dlightmap.integer)
1539 for (surf = firstsurf;surf;surf = surf->chain)
1540 if (surf->dlightframe == r_framecount)
1541 RSurfShader_OpaqueWall_Pass_Light(ent, surf);
1542 for (surf = firstsurf;surf;surf = surf->chain)
1543 if (surf->currenttexture->glowtexture)
1544 RSurfShader_OpaqueWall_Pass_Glow(ent, surf);
1546 for (surf = firstsurf;surf;surf = surf->chain)
1547 RSurfShader_OpaqueWall_Pass_Fog(ent, surf);
1552 =============================================================
1556 =============================================================
1559 static void R_SolidWorldNode (entity_render_t *ent)
1561 if (r_viewleaf->contents != CONTENTS_SOLID)
1564 mportal_t *p, *pstack[8192];
1565 msurface_t *surf, **mark, **endmark;
1567 // LordHavoc: portal-passage worldnode; follows portals leading
1568 // outward from viewleaf, if a portal leads offscreen it is not
1569 // followed, in indoor maps this can often cull a great deal of
1570 // geometry away when pvs data is not present (useful with pvs as well)
1573 leaf->worldnodeframe = r_framecount;
1578 leaf->visframe = r_framecount;
1580 if (leaf->nummarksurfaces)
1582 mark = leaf->firstmarksurface;
1583 endmark = mark + leaf->nummarksurfaces;
1587 // make sure surfaces are only processed once
1588 if (surf->worldnodeframe == r_framecount)
1590 surf->worldnodeframe = r_framecount;
1591 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1593 if (surf->flags & SURF_PLANEBACK)
1594 surf->visframe = r_framecount;
1598 if (!(surf->flags & SURF_PLANEBACK))
1599 surf->visframe = r_framecount;
1602 while (mark < endmark);
1605 // follow portals into other leafs
1607 for (;p;p = p->next)
1609 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1612 if (leaf->worldnodeframe != r_framecount)
1614 leaf->worldnodeframe = r_framecount;
1615 if (leaf->contents != CONTENTS_SOLID)
1617 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1619 p->visframe = r_framecount;
1620 pstack[portalstack++] = p;
1624 p = pstack[--portalstack];
1636 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1637 int nodestackpos = 0;
1638 // LordHavoc: recursive descending worldnode; if portals are not
1639 // available, this is a good last resort, can cull large amounts of
1640 // geometry, but is more time consuming than portal-passage and renders
1641 // things behind walls
1644 if (R_NotCulledBox(node->mins, node->maxs))
1646 if (node->numsurfaces)
1648 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1649 if (PlaneDiff (r_origin, node->plane) < 0)
1651 for (;surf < surfend;surf++)
1653 if (surf->flags & SURF_PLANEBACK)
1654 surf->visframe = r_framecount;
1659 for (;surf < surfend;surf++)
1661 if (!(surf->flags & SURF_PLANEBACK))
1662 surf->visframe = r_framecount;
1667 // recurse down the children
1668 if (node->children[0]->contents >= 0)
1670 if (node->children[1]->contents >= 0)
1672 if (nodestackpos < 8192)
1673 nodestack[nodestackpos++] = node->children[1];
1674 node = node->children[0];
1678 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1679 node = node->children[0];
1684 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1685 if (node->children[1]->contents >= 0)
1687 node = node->children[1];
1690 else if (nodestackpos > 0)
1692 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1693 node = nodestack[--nodestackpos];
1698 else if (nodestackpos > 0)
1700 node = nodestack[--nodestackpos];
1706 static int r_portalframecount = 0;
1708 static void R_PVSWorldNode()
1711 mportal_t *p, *pstack[8192];
1712 msurface_t *surf, **mark, **endmark;
1716 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1719 leaf->worldnodeframe = r_framecount;
1724 leaf->visframe = r_framecount;
1726 if (leaf->nummarksurfaces)
1728 mark = leaf->firstmarksurface;
1729 endmark = mark + leaf->nummarksurfaces;
1733 // make sure surfaces are only processed once
1734 if (surf->worldnodeframe == r_framecount)
1736 surf->worldnodeframe = r_framecount;
1737 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1739 if (surf->flags & SURF_PLANEBACK)
1740 surf->visframe = r_framecount;
1744 if (!(surf->flags & SURF_PLANEBACK))
1745 surf->visframe = r_framecount;
1748 while (mark < endmark);
1751 // follow portals into other leafs
1752 for (p = leaf->portals;p;p = p->next)
1754 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1757 if (leaf->worldnodeframe != r_framecount)
1759 leaf->worldnodeframe = r_framecount;
1760 if (leaf->contents != CONTENTS_SOLID)
1762 i = (leaf - cl.worldmodel->leafs) - 1;
1763 if (worldvis[i>>3] & (1<<(i&7)))
1765 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1767 pstack[portalstack++] = p;
1771 p = pstack[--portalstack];
1783 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1784 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1785 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1786 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1787 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1789 int Cshader_count = 5;
1790 Cshader_t *Cshaders[5] =
1792 &Cshader_wall_vertex,
1793 &Cshader_wall_lightmap,
1794 &Cshader_wall_fullbright,
1799 void R_PrepareSurfaces(entity_render_t *ent)
1801 int i, alttextures, texframe, framecount;
1806 for (i = 0;i < Cshader_count;i++)
1807 Cshaders[i]->chain = NULL;
1810 alttextures = ent->frame != 0;
1811 texframe = (int)(cl.time * 5.0f);
1813 for (i = 0;i < model->nummodelsurfaces;i++)
1815 surf = model->modelsortedsurfaces[i];
1816 if (surf->visframe == r_framecount)
1818 if (surf->insertframe != r_framecount)
1820 surf->insertframe = r_framecount;
1822 t = surf->texinfo->texture;
1825 framecount = t->anim_total[alttextures];
1826 if (framecount >= 2)
1827 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1829 surf->currenttexture = t->anim_frames[alttextures][0];
1832 surf->currenttexture = t;
1835 surf->chain = surf->shader->chain;
1836 surf->shader->chain = surf;
1841 void R_DrawSurfaces (entity_render_t *ent, int type)
1846 for (i = 0;i < Cshader_count;i++)
1848 shader = Cshaders[i];
1849 if (shader->chain && shader->shaderfunc[type])
1850 shader->shaderfunc[type](ent, shader->chain);
1854 void R_DrawPortals(entity_render_t *ent)
1858 mportal_t *portal, *endportal;
1859 rmeshbufferinfo_t m;
1860 drawportals = r_drawportals.integer;
1862 if (drawportals < 1)
1865 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1867 if (portal->visframe == r_portalframecount)
1869 memset(&m, 0, sizeof(m));
1870 m.transparent = true;
1871 m.blendfunc1 = GL_SRC_ALPHA;
1872 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1873 m.numverts = portal->numpoints;
1874 m.numtriangles = portal->numpoints - 2;
1875 if (R_Mesh_Draw_GetBuffer(&m, false))
1877 for (i = 0;i < m.numtriangles;i++)
1879 m.index[i * 3 + 0] = 0;
1880 m.index[i * 3 + 1] = i + 1;
1881 m.index[i * 3 + 2] = i + 2;
1883 i = portal - cl.worldmodel->portals;
1884 R_FillColors(m.color, m.numverts,
1885 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * m.colorscale,
1886 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * m.colorscale,
1887 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * m.colorscale,
1889 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1891 for (i = portal->numpoints - 1, v = m.vertex;i >= 0;i--, v += 4)
1892 VectorCopy(portal->points[i].position, v);
1895 for (i = 0, v = m.vertex;i < portal->numpoints;i++, v += 4)
1896 VectorCopy(portal->points[i].position, v);
1903 void R_SetupForBModelRendering(entity_render_t *ent)
1910 // because bmodels can be reused, we have to decide which things to render
1911 // from scratch every time
1915 softwaretransformforentity (ent);
1916 softwareuntransform(r_origin, modelorg);
1918 for (i = 0;i < model->nummodelsurfaces;i++)
1920 surf = model->modelsortedsurfaces[i];
1921 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
1922 surf->visframe = r_framecount;
1924 surf->visframe = -1;
1925 surf->worldnodeframe = -1;
1926 surf->lightframe = -1;
1927 surf->dlightframe = -1;
1928 surf->insertframe = -1;
1932 void R_SetupForWorldRendering(entity_render_t *ent)
1934 // there is only one instance of the world, but it can be rendered in
1936 softwaretransformidentity();
1939 static void R_SurfMarkLights (entity_render_t *ent)
1944 if (r_dynamic.integer)
1947 if (!r_vertexsurfaces.integer)
1949 for (i = 0;i < ent->model->nummodelsurfaces;i++)
1951 surf = ent->model->modelsortedsurfaces[i];
1952 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
1954 if (surf->cached_dlight
1955 || surf->cached_ambient != r_ambient.value
1956 || surf->cached_lightscalebit != lightscalebit)
1957 R_BuildLightMap(ent, surf, false); // base lighting changed
1958 else if (r_dynamic.integer)
1960 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1961 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1962 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1963 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1964 R_BuildLightMap(ent, surf, false); // base lighting changed
1965 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1966 R_BuildLightMap(ent, surf, true); // only dlights
1973 void R_MarkWorldLights(entity_render_t *ent)
1975 R_SetupForWorldRendering(ent);
1976 R_SurfMarkLights(ent);
1984 void R_DrawWorld (entity_render_t *ent)
1986 R_SetupForWorldRendering(ent);
1988 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
1989 R_SolidWorldNode (ent);
1991 R_PVSWorldNode (ent);
1999 void R_DrawBrushModelSky (entity_render_t *ent)
2001 R_SetupForBModelRendering(ent);
2003 R_PrepareSurfaces(ent);
2004 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
2007 void R_DrawBrushModelNormal (entity_render_t *ent)
2011 // have to flush queue because of possible lightmap reuse
2014 R_SetupForBModelRendering(ent);
2016 R_SurfMarkLights(ent);
2018 R_PrepareSurfaces(ent);
2020 if (!skyrendermasked)
2021 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
2022 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL);
2025 static void gl_surf_start(void)
2029 static void gl_surf_shutdown(void)
2033 static void gl_surf_newmap(void)
2037 void GL_Surf_Init(void)
2040 dlightdivtable[0] = 4194304;
2041 for (i = 1;i < 32768;i++)
2042 dlightdivtable[i] = 4194304 / (i << 7);
2044 Cvar_RegisterVariable(&r_ambient);
2045 Cvar_RegisterVariable(&r_vertexsurfaces);
2046 Cvar_RegisterVariable(&r_dlightmap);
2047 Cvar_RegisterVariable(&r_drawportals);
2048 Cvar_RegisterVariable(&r_testvis);
2049 Cvar_RegisterVariable(&r_floatbuildlightmap);
2050 Cvar_RegisterVariable(&r_detailtextures);
2052 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);