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_Callback(void *calldata1, int calldata2)
735 entity_render_t *ent = calldata1;
736 msurface_t *surf = ent->model->surfaces + calldata2;
739 float *outv, *outc, *outst, cl, diff[3];
740 float base[3], scale, f;
744 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
746 softwaretransformforentity(ent);
747 memset(&m, 0, sizeof(m));
748 if (ent->effects & EF_ADDITIVE)
750 m.blendfunc1 = GL_SRC_ALPHA;
751 m.blendfunc2 = GL_ONE;
753 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
755 m.blendfunc1 = GL_SRC_ALPHA;
756 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
760 m.blendfunc1 = GL_ONE;
761 m.blendfunc2 = GL_ZERO;
763 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
764 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
765 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
767 m.numtriangles = mesh->numtriangles;
768 m.numverts = mesh->numverts;
769 if (R_Mesh_Draw_GetBuffer(&m, true))
772 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
773 for (i = 0, v = mesh->vertex, outv = m.vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
775 softwaretransform(v->v, outv);
780 base[0] = base[1] = base[2] = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
781 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
783 VectorCopy(base, outc);
786 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
788 if (surf->dlightframe == r_framecount)
789 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
790 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
792 if (surf->flags & SURF_LIGHTMAP)
793 if (surf->styles[0] != 255)
795 lm = surf->samples + v->lightmapoffset;
796 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
797 VectorMA(outc, scale, lm, outc);
798 if (surf->styles[1] != 255)
801 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
802 VectorMA(outc, scale, lm, outc);
803 if (surf->styles[2] != 255)
806 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
807 VectorMA(outc, scale, lm, outc);
808 if (surf->styles[3] != 255)
811 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
812 VectorMA(outc, scale, lm, outc);
821 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
823 VectorSubtract(outv, r_origin, diff);
824 f = m.colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
825 VectorScale(outc, f, outc);
828 else if (m.colorscale != 1)
830 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
831 VectorScale(outc, m.colorscale, outc);
839 memset(&m, 0, sizeof(m));
840 m.blendfunc1 = GL_SRC_ALPHA;
841 m.blendfunc2 = GL_ONE;
842 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
843 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
845 m.numtriangles = mesh->numtriangles;
846 m.numverts = mesh->numverts;
847 if (R_Mesh_Draw_GetBuffer(&m, false))
849 VectorScale(fogcolor, m.colorscale, base);
850 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
851 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
853 softwaretransform(v->v, outv);
855 VectorSubtract(outv, r_origin, diff);
856 f = exp(fogdensity/DotProduct(diff, diff));
857 VectorScale(base, f, outc);
862 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
874 static void RSurfShader_Water(entity_render_t *ent, msurface_t *firstsurf)
877 for (surf = firstsurf;surf;surf = surf->chain)
879 if ((r_wateralpha.value < 1 && !(surf->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture)
880 R_MeshQueue_AddTransparent(surf->poly_center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
882 R_MeshQueue_Add(RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
886 static void RSurfShader_Wall_Pass_BaseVertex(entity_render_t *ent, msurface_t *surf)
890 float *outv, *outc, *outst, cl, ca, diff[3];
891 float base[3], scale, f;
895 memset(&m, 0, sizeof(m));
896 if (ent->effects & EF_ADDITIVE)
898 m.transparent = true;
899 m.blendfunc1 = GL_SRC_ALPHA;
900 m.blendfunc2 = GL_ONE;
902 else if (surf->currenttexture->fogtexture != NULL || ent->alpha != 1)
904 m.transparent = true;
905 m.blendfunc1 = GL_SRC_ALPHA;
906 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
910 m.blendfunc1 = GL_ONE;
911 m.blendfunc2 = GL_ZERO;
913 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
915 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
917 base[0] = base[1] = base[2] = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
920 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
922 m.numtriangles = mesh->numtriangles;
923 m.numverts = mesh->numverts;
925 if (R_Mesh_Draw_GetBuffer(&m, true))
928 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
930 if (ent->effects & EF_FULLBRIGHT)
932 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)
934 softwaretransform(v->v, outv);
936 VectorSubtract(outv, r_origin, diff);
937 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
945 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)
947 softwaretransform(v->v, outv);
949 VectorCopy(base, outc);
955 if (surf->dlightframe == r_framecount)
956 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
958 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
960 if (surf->styles[0] != 255)
962 lm = surf->samples + v->lightmapoffset;
963 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
964 VectorMA(outc, scale, lm, outc);
965 if (surf->styles[1] != 255)
968 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
969 VectorMA(outc, scale, lm, outc);
970 if (surf->styles[2] != 255)
973 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
974 VectorMA(outc, scale, lm, outc);
975 if (surf->styles[3] != 255)
978 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
979 VectorMA(outc, scale, lm, outc);
986 VectorSubtract(outv, r_origin, diff);
987 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
988 VectorScale(outc, f, outc);
991 VectorScale(outc, cl, outc);
999 static void RSurfShader_Wall_Pass_BaseFullbright(entity_render_t *ent, msurface_t *surf)
1003 float *outv, *outc, *outst, cl, ca, diff[3];
1005 rmeshbufferinfo_t m;
1006 memset(&m, 0, sizeof(m));
1007 if (ent->effects & EF_ADDITIVE)
1009 m.transparent = true;
1010 m.blendfunc1 = GL_SRC_ALPHA;
1011 m.blendfunc2 = GL_ONE;
1013 else if (surf->currenttexture->fogtexture != NULL || ent->alpha != 1)
1015 m.transparent = true;
1016 m.blendfunc1 = GL_SRC_ALPHA;
1017 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1021 m.blendfunc1 = GL_ONE;
1022 m.blendfunc2 = GL_ZERO;
1024 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1026 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1028 m.numtriangles = mesh->numtriangles;
1029 m.numverts = mesh->numverts;
1031 if (R_Mesh_Draw_GetBuffer(&m, false))
1034 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1035 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)
1037 softwaretransform(v->v, outv);
1041 VectorSubtract(outv, r_origin, diff);
1042 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1045 outc[0] = outc[1] = outc[2] = cl;
1047 outst[0] = v->st[0];
1048 outst[1] = v->st[1];
1055 static void RSurfShader_Wall_Pass_Glow(entity_render_t *ent, msurface_t *surf)
1059 float *outv, *outc, *outst, cl, ca, diff[3];
1061 rmeshbufferinfo_t m;
1062 memset(&m, 0, sizeof(m));
1063 m.transparent = ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || ent->alpha != 1;
1064 m.blendfunc1 = GL_SRC_ALPHA;
1065 m.blendfunc2 = GL_ONE;
1066 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1068 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1070 m.numtriangles = mesh->numtriangles;
1071 m.numverts = mesh->numverts;
1073 if (R_Mesh_Draw_GetBuffer(&m, false))
1076 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1077 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)
1079 softwaretransform(v->v, outv);
1083 VectorSubtract(outv, r_origin, diff);
1084 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1087 outc[0] = outc[1] = outc[2] = cl;
1089 outst[0] = v->st[0];
1090 outst[1] = v->st[1];
1097 static void RSurfShader_Wall_Pass_Fog(entity_render_t *ent, msurface_t *surf)
1101 float *outv, *outc, *outst, cl, ca, diff[3], f;
1103 rmeshbufferinfo_t m;
1104 memset(&m, 0, sizeof(m));
1105 m.transparent = ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || ent->alpha != 1;
1106 m.blendfunc1 = GL_SRC_ALPHA;
1107 m.blendfunc2 = GL_ONE;
1109 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1111 m.numtriangles = mesh->numtriangles;
1112 m.numverts = mesh->numverts;
1114 if (R_Mesh_Draw_GetBuffer(&m, false))
1117 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1118 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)
1120 softwaretransform(v->v, outv);
1122 VectorSubtract(outv, r_origin, diff);
1123 f = cl * exp(fogdensity/DotProduct(diff, diff));
1124 VectorScale(fogcolor, f, outc);
1126 outst[0] = v->st[0];
1127 outst[1] = v->st[1];
1134 static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(entity_render_t *ent, msurface_t *surf)
1138 float *outv, *outc, *outst, *outuv, *outab, cl;
1140 rmeshbufferinfo_t m;
1141 memset(&m, 0, sizeof(m));
1142 m.blendfunc1 = GL_ONE;
1143 m.blendfunc2 = GL_ZERO;
1144 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1145 m.texrgbscale[0] = 1.0f;
1146 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1147 m.texrgbscale[1] = 4.0f;
1148 m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
1149 m.texrgbscale[2] = 2.0f;
1150 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1152 m.numtriangles = mesh->numtriangles;
1153 m.numverts = mesh->numverts;
1155 if (R_Mesh_Draw_GetBuffer(&m, false))
1157 cl = (float) (1 << lightscalebit) * m.colorscale;
1158 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1159 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)
1161 softwaretransform(v->v, outv);
1163 outc[0] = outc[1] = outc[2] = cl;
1165 outst[0] = v->st[0];
1166 outst[1] = v->st[1];
1167 outuv[0] = v->uv[0];
1168 outuv[1] = v->uv[1];
1169 outab[0] = v->ab[0];
1170 outab[1] = v->ab[1];
1177 static void RSurfShader_OpaqueWall_Pass_BaseMTex(entity_render_t *ent, msurface_t *surf)
1181 float *outv, *outc, *outst, *outuv, cl;
1183 rmeshbufferinfo_t m;
1184 memset(&m, 0, sizeof(m));
1185 m.blendfunc1 = GL_ONE;
1186 m.blendfunc2 = GL_ZERO;
1187 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1188 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1189 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1191 m.numtriangles = mesh->numtriangles;
1192 m.numverts = mesh->numverts;
1194 if (R_Mesh_Draw_GetBuffer(&m, true))
1196 cl = (float) (1 << lightscalebit) * m.colorscale;
1197 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1198 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)
1200 softwaretransform(v->v, outv);
1202 outc[0] = outc[1] = outc[2] = cl;
1204 outst[0] = v->st[0];
1205 outst[1] = v->st[1];
1206 outuv[0] = v->uv[0];
1207 outuv[1] = v->uv[1];
1214 static void RSurfShader_OpaqueWall_Pass_BaseTexture(entity_render_t *ent, msurface_t *surf)
1218 float *outv, *outc, *outst, cl;
1220 rmeshbufferinfo_t m;
1221 memset(&m, 0, sizeof(m));
1222 m.blendfunc1 = GL_ONE;
1223 m.blendfunc2 = GL_ZERO;
1224 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1225 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1227 m.numtriangles = mesh->numtriangles;
1228 m.numverts = mesh->numverts;
1230 if (R_Mesh_Draw_GetBuffer(&m, false))
1232 cl = (float) (1 << lightscalebit) * m.colorscale;
1233 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1234 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)
1236 softwaretransform(v->v, outv);
1238 outc[0] = outc[1] = outc[2] = cl;
1240 outst[0] = v->st[0];
1241 outst[1] = v->st[1];
1248 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(entity_render_t *ent, msurface_t *surf)
1252 float *outv, *outc, *outuv, cl;
1254 rmeshbufferinfo_t m;
1255 memset(&m, 0, sizeof(m));
1256 m.blendfunc1 = GL_ZERO;
1257 m.blendfunc2 = GL_SRC_COLOR;
1258 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1259 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1261 m.numtriangles = mesh->numtriangles;
1262 m.numverts = mesh->numverts;
1264 if (R_Mesh_Draw_GetBuffer(&m, true))
1266 cl = (float) (1 << lightscalebit) * m.colorscale;
1267 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1268 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)
1270 softwaretransform(v->v, outv);
1272 outc[0] = outc[1] = outc[2] = cl;
1274 outuv[0] = v->uv[0];
1275 outuv[1] = v->uv[1];
1282 static void RSurfShader_OpaqueWall_Pass_Light(entity_render_t *ent, msurface_t *surf)
1286 float *outv, *outc, *outst, cl;
1288 rmeshbufferinfo_t m;
1290 if (surf->dlightframe != r_framecount)
1292 if (ent->effects & EF_FULLBRIGHT)
1295 memset(&m, 0, sizeof(m));
1296 m.blendfunc1 = GL_SRC_ALPHA;
1297 m.blendfunc2 = GL_ONE;
1298 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1299 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1301 if (RSurf_LightCheck(surf->dlightbits, mesh))
1303 m.numtriangles = mesh->numtriangles;
1304 m.numverts = mesh->numverts;
1306 if (R_Mesh_Draw_GetBuffer(&m, true))
1309 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1310 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)
1312 softwaretransform(v->v, outv);
1316 outst[0] = v->st[0];
1317 outst[1] = v->st[1];
1319 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1321 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1322 VectorScale(outc, cl, outc);
1329 static void RSurfShader_OpaqueWall_Pass_Fog(entity_render_t *ent, msurface_t *surf)
1333 float *outv, *outc, cl, diff[3], fcolor[3];
1335 rmeshbufferinfo_t m;
1336 memset(&m, 0, sizeof(m));
1337 m.blendfunc1 = GL_SRC_ALPHA;
1338 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1339 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1341 m.numtriangles = mesh->numtriangles;
1342 m.numverts = mesh->numverts;
1344 if (R_Mesh_Draw_GetBuffer(&m, false))
1347 VectorScale(fogcolor, cl, fcolor);
1348 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1349 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1351 softwaretransform(v->v, outv);
1353 VectorCopy(fcolor, outc);
1354 VectorSubtract(outv, r_origin, diff);
1355 outc[3] = exp(fogdensity/DotProduct(diff, diff));
1362 static void RSurfShader_OpaqueWall_Pass_BaseDetail(entity_render_t *ent, msurface_t *surf)
1366 float *outv, *outc, *outst;
1368 rmeshbufferinfo_t m;
1369 memset(&m, 0, sizeof(m));
1370 m.blendfunc1 = GL_DST_COLOR;
1371 m.blendfunc2 = GL_SRC_COLOR;
1372 m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
1373 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1375 m.numtriangles = mesh->numtriangles;
1376 m.numverts = mesh->numverts;
1378 if (R_Mesh_Draw_GetBuffer(&m, false))
1380 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1381 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)
1383 softwaretransform(v->v, outv);
1385 outc[0] = outc[1] = outc[2] = outc[3] = 1;
1386 outst[0] = v->ab[0];
1387 outst[1] = v->ab[1];
1394 static void RSurfShader_OpaqueWall_Pass_Glow(entity_render_t *ent, msurface_t *surf)
1398 float *outv, *outc, *outst, cl;
1400 rmeshbufferinfo_t m;
1401 memset(&m, 0, sizeof(m));
1402 m.blendfunc1 = GL_SRC_ALPHA;
1403 m.blendfunc2 = GL_ONE;
1404 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1405 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1407 m.numtriangles = mesh->numtriangles;
1408 m.numverts = mesh->numverts;
1410 if (R_Mesh_Draw_GetBuffer(&m, false))
1413 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1414 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)
1416 softwaretransform(v->v, outv);
1418 outc[0] = outc[1] = outc[2] = cl;
1420 outst[0] = v->st[0];
1421 outst[1] = v->st[1];
1428 static void RSurfShader_Wall_Fullbright(entity_render_t *ent, msurface_t *firstsurf)
1431 for (surf = firstsurf;surf;surf = surf->chain)
1434 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1436 for (surf = firstsurf;surf;surf = surf->chain)
1437 if (surf->currenttexture->glowtexture)
1438 RSurfShader_Wall_Pass_Glow(ent, surf);
1440 for (surf = firstsurf;surf;surf = surf->chain)
1441 RSurfShader_Wall_Pass_Fog(ent, surf);
1444 static void RSurfShader_Wall_Vertex(entity_render_t *ent, msurface_t *firstsurf)
1447 for (surf = firstsurf;surf;surf = surf->chain)
1450 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1452 for (surf = firstsurf;surf;surf = surf->chain)
1453 if (surf->currenttexture->glowtexture)
1454 RSurfShader_Wall_Pass_Glow(ent, surf);
1456 for (surf = firstsurf;surf;surf = surf->chain)
1457 RSurfShader_Wall_Pass_Fog(ent, surf);
1460 static void RSurfShader_Wall_Lightmap(entity_render_t *ent, msurface_t *firstsurf)
1463 if (r_vertexsurfaces.integer || firstsurf->currenttexture->fogtexture != NULL || ent->alpha != 1 || ent->effects & EF_ADDITIVE)
1465 for (surf = firstsurf;surf;surf = surf->chain)
1468 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1470 for (surf = firstsurf;surf;surf = surf->chain)
1471 if (surf->currenttexture->glowtexture)
1472 RSurfShader_Wall_Pass_Glow(ent, surf);
1474 for (surf = firstsurf;surf;surf = surf->chain)
1475 RSurfShader_Wall_Pass_Fog(ent, surf);
1479 if (r_textureunits.integer >= 2)
1481 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1483 for (surf = firstsurf;surf;surf = surf->chain)
1486 RSurfShader_OpaqueWall_Pass_TripleTexCombine(ent, surf);
1491 for (surf = firstsurf;surf;surf = surf->chain)
1494 RSurfShader_OpaqueWall_Pass_BaseMTex(ent, surf);
1496 if (r_detailtextures.integer)
1497 for (surf = firstsurf;surf;surf = surf->chain)
1498 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1503 for (surf = firstsurf;surf;surf = surf->chain)
1506 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, surf);
1508 for (surf = firstsurf;surf;surf = surf->chain)
1509 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, surf);
1510 if (r_detailtextures.integer)
1511 for (surf = firstsurf;surf;surf = surf->chain)
1512 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1514 if (!r_dlightmap.integer)
1515 for (surf = firstsurf;surf;surf = surf->chain)
1516 if (surf->dlightframe == r_framecount)
1517 RSurfShader_OpaqueWall_Pass_Light(ent, surf);
1518 for (surf = firstsurf;surf;surf = surf->chain)
1519 if (surf->currenttexture->glowtexture)
1520 RSurfShader_OpaqueWall_Pass_Glow(ent, surf);
1522 for (surf = firstsurf;surf;surf = surf->chain)
1523 RSurfShader_OpaqueWall_Pass_Fog(ent, surf);
1528 =============================================================
1532 =============================================================
1535 static void R_SolidWorldNode (entity_render_t *ent)
1537 if (r_viewleaf->contents != CONTENTS_SOLID)
1540 mportal_t *p, *pstack[8192];
1541 msurface_t *surf, **mark, **endmark;
1543 // LordHavoc: portal-passage worldnode; follows portals leading
1544 // outward from viewleaf, if a portal leads offscreen it is not
1545 // followed, in indoor maps this can often cull a great deal of
1546 // geometry away when pvs data is not present (useful with pvs as well)
1549 leaf->worldnodeframe = r_framecount;
1554 leaf->visframe = r_framecount;
1556 if (leaf->nummarksurfaces)
1558 mark = leaf->firstmarksurface;
1559 endmark = mark + leaf->nummarksurfaces;
1563 // make sure surfaces are only processed once
1564 if (surf->worldnodeframe == r_framecount)
1566 surf->worldnodeframe = r_framecount;
1567 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1569 if (surf->flags & SURF_PLANEBACK)
1570 surf->visframe = r_framecount;
1574 if (!(surf->flags & SURF_PLANEBACK))
1575 surf->visframe = r_framecount;
1578 while (mark < endmark);
1581 // follow portals into other leafs
1583 for (;p;p = p->next)
1585 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1588 if (leaf->worldnodeframe != r_framecount)
1590 leaf->worldnodeframe = r_framecount;
1591 if (leaf->contents != CONTENTS_SOLID)
1593 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1595 p->visframe = r_framecount;
1596 pstack[portalstack++] = p;
1600 p = pstack[--portalstack];
1612 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1613 int nodestackpos = 0;
1614 // LordHavoc: recursive descending worldnode; if portals are not
1615 // available, this is a good last resort, can cull large amounts of
1616 // geometry, but is more time consuming than portal-passage and renders
1617 // things behind walls
1620 if (R_NotCulledBox(node->mins, node->maxs))
1622 if (node->numsurfaces)
1624 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1625 if (PlaneDiff (r_origin, node->plane) < 0)
1627 for (;surf < surfend;surf++)
1629 if (surf->flags & SURF_PLANEBACK)
1630 surf->visframe = r_framecount;
1635 for (;surf < surfend;surf++)
1637 if (!(surf->flags & SURF_PLANEBACK))
1638 surf->visframe = r_framecount;
1643 // recurse down the children
1644 if (node->children[0]->contents >= 0)
1646 if (node->children[1]->contents >= 0)
1648 if (nodestackpos < 8192)
1649 nodestack[nodestackpos++] = node->children[1];
1650 node = node->children[0];
1654 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1655 node = node->children[0];
1660 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1661 if (node->children[1]->contents >= 0)
1663 node = node->children[1];
1666 else if (nodestackpos > 0)
1668 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1669 node = nodestack[--nodestackpos];
1674 else if (nodestackpos > 0)
1676 node = nodestack[--nodestackpos];
1682 static int r_portalframecount = 0;
1684 static void R_PVSWorldNode()
1687 mportal_t *p, *pstack[8192];
1688 msurface_t *surf, **mark, **endmark;
1692 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1695 leaf->worldnodeframe = r_framecount;
1700 leaf->visframe = r_framecount;
1702 if (leaf->nummarksurfaces)
1704 mark = leaf->firstmarksurface;
1705 endmark = mark + leaf->nummarksurfaces;
1709 // make sure surfaces are only processed once
1710 if (surf->worldnodeframe == r_framecount)
1712 surf->worldnodeframe = r_framecount;
1713 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1715 if (surf->flags & SURF_PLANEBACK)
1716 surf->visframe = r_framecount;
1720 if (!(surf->flags & SURF_PLANEBACK))
1721 surf->visframe = r_framecount;
1724 while (mark < endmark);
1727 // follow portals into other leafs
1728 for (p = leaf->portals;p;p = p->next)
1730 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1733 if (leaf->worldnodeframe != r_framecount)
1735 leaf->worldnodeframe = r_framecount;
1736 if (leaf->contents != CONTENTS_SOLID)
1738 i = (leaf - cl.worldmodel->leafs) - 1;
1739 if (worldvis[i>>3] & (1<<(i&7)))
1741 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1743 pstack[portalstack++] = p;
1747 p = pstack[--portalstack];
1759 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1760 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1761 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1762 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1763 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1765 int Cshader_count = 5;
1766 Cshader_t *Cshaders[5] =
1768 &Cshader_wall_vertex,
1769 &Cshader_wall_lightmap,
1770 &Cshader_wall_fullbright,
1775 void R_PrepareSurfaces(entity_render_t *ent)
1777 int i, alttextures, texframe, framecount;
1782 for (i = 0;i < Cshader_count;i++)
1783 Cshaders[i]->chain = NULL;
1786 alttextures = ent->frame != 0;
1787 texframe = (int)(cl.time * 5.0f);
1789 for (i = 0;i < model->nummodelsurfaces;i++)
1791 surf = model->modelsortedsurfaces[i];
1792 if (surf->visframe == r_framecount)
1794 if (surf->insertframe != r_framecount)
1796 surf->insertframe = r_framecount;
1798 t = surf->texinfo->texture;
1801 framecount = t->anim_total[alttextures];
1802 if (framecount >= 2)
1803 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1805 surf->currenttexture = t->anim_frames[alttextures][0];
1808 surf->currenttexture = t;
1811 surf->chain = surf->shader->chain;
1812 surf->shader->chain = surf;
1817 void R_DrawSurfaces (entity_render_t *ent, int type)
1822 for (i = 0;i < Cshader_count;i++)
1824 shader = Cshaders[i];
1825 if (shader->chain && shader->shaderfunc[type])
1826 shader->shaderfunc[type](ent, shader->chain);
1830 void R_DrawPortals(entity_render_t *ent)
1834 mportal_t *portal, *endportal;
1835 rmeshbufferinfo_t m;
1836 drawportals = r_drawportals.integer;
1838 if (drawportals < 1)
1841 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1843 if (portal->visframe == r_portalframecount)
1845 memset(&m, 0, sizeof(m));
1846 m.transparent = true;
1847 m.blendfunc1 = GL_SRC_ALPHA;
1848 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1849 m.numverts = portal->numpoints;
1850 m.numtriangles = portal->numpoints - 2;
1851 if (R_Mesh_Draw_GetBuffer(&m, false))
1853 for (i = 0;i < m.numtriangles;i++)
1855 m.index[i * 3 + 0] = 0;
1856 m.index[i * 3 + 1] = i + 1;
1857 m.index[i * 3 + 2] = i + 2;
1859 i = portal - cl.worldmodel->portals;
1860 R_FillColors(m.color, m.numverts,
1861 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * m.colorscale,
1862 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * m.colorscale,
1863 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * m.colorscale,
1865 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1867 for (i = portal->numpoints - 1, v = m.vertex;i >= 0;i--, v += 4)
1868 VectorCopy(portal->points[i].position, v);
1871 for (i = 0, v = m.vertex;i < portal->numpoints;i++, v += 4)
1872 VectorCopy(portal->points[i].position, v);
1879 void R_SetupForBModelRendering(entity_render_t *ent)
1886 // because bmodels can be reused, we have to decide which things to render
1887 // from scratch every time
1891 softwaretransformforentity (ent);
1892 softwareuntransform(r_origin, modelorg);
1894 for (i = 0;i < model->nummodelsurfaces;i++)
1896 surf = model->modelsortedsurfaces[i];
1897 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
1898 surf->visframe = r_framecount;
1900 surf->visframe = -1;
1901 surf->worldnodeframe = -1;
1902 surf->lightframe = -1;
1903 surf->dlightframe = -1;
1904 surf->insertframe = -1;
1908 void R_SetupForWorldRendering(entity_render_t *ent)
1910 // there is only one instance of the world, but it can be rendered in
1912 softwaretransformidentity();
1915 static void R_SurfMarkLights (entity_render_t *ent)
1920 if (r_dynamic.integer)
1923 if (!r_vertexsurfaces.integer)
1925 for (i = 0;i < ent->model->nummodelsurfaces;i++)
1927 surf = ent->model->modelsortedsurfaces[i];
1928 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
1930 if (surf->cached_dlight
1931 || surf->cached_ambient != r_ambient.value
1932 || surf->cached_lightscalebit != lightscalebit)
1933 R_BuildLightMap(ent, surf, false); // base lighting changed
1934 else if (r_dynamic.integer)
1936 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1937 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1938 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1939 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1940 R_BuildLightMap(ent, surf, false); // base lighting changed
1941 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1942 R_BuildLightMap(ent, surf, true); // only dlights
1949 void R_MarkWorldLights(entity_render_t *ent)
1951 R_SetupForWorldRendering(ent);
1952 R_SurfMarkLights(ent);
1960 void R_DrawWorld (entity_render_t *ent)
1962 R_SetupForWorldRendering(ent);
1964 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
1965 R_SolidWorldNode (ent);
1967 R_PVSWorldNode (ent);
1975 void R_DrawBrushModelSky (entity_render_t *ent)
1977 R_SetupForBModelRendering(ent);
1979 R_PrepareSurfaces(ent);
1980 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
1983 void R_DrawBrushModelNormal (entity_render_t *ent)
1987 // have to flush queue because of possible lightmap reuse
1990 R_SetupForBModelRendering(ent);
1992 R_SurfMarkLights(ent);
1994 R_PrepareSurfaces(ent);
1996 if (!skyrendermasked)
1997 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
1998 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL);
2001 static void gl_surf_start(void)
2005 static void gl_surf_shutdown(void)
2009 static void gl_surf_newmap(void)
2013 void GL_Surf_Init(void)
2016 dlightdivtable[0] = 4194304;
2017 for (i = 1;i < 32768;i++)
2018 dlightdivtable[i] = 4194304 / (i << 7);
2020 Cvar_RegisterVariable(&r_ambient);
2021 Cvar_RegisterVariable(&r_vertexsurfaces);
2022 Cvar_RegisterVariable(&r_dlightmap);
2023 Cvar_RegisterVariable(&r_drawportals);
2024 Cvar_RegisterVariable(&r_testvis);
2025 Cvar_RegisterVariable(&r_floatbuildlightmap);
2026 Cvar_RegisterVariable(&r_detailtextures);
2028 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);