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"};
38 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
40 static int dlightdivtable[32768];
42 // variables used by R_PVSUpdate
43 int r_pvsframecount = 0;
44 mleaf_t *r_pvsviewleaf = NULL;
45 int r_pvsviewleafnovis = 0;
47 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
49 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
51 float dist, impact[3], local[3];
55 smax = (surf->extents[0] >> 4) + 1;
56 tmax = (surf->extents[1] >> 4) + 1;
59 for (lnum = 0; lnum < r_numdlights; lnum++)
61 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
62 continue; // not lit by this light
64 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
65 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
67 // for comparisons to minimum acceptable light
68 // compensate for LIGHTOFFSET
69 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
76 if (surf->plane->type < 3)
78 VectorCopy(local, impact);
79 impact[surf->plane->type] -= dist;
83 impact[0] = local[0] - surf->plane->normal[0] * dist;
84 impact[1] = local[1] - surf->plane->normal[1] * dist;
85 impact[2] = local[2] - surf->plane->normal[2] * dist;
88 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
89 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
91 s = bound(0, impacts, smax * 16) - impacts;
92 t = bound(0, impactt, tmax * 16) - impactt;
93 i = s * s + t * t + dist2;
97 // reduce calculations
98 for (s = 0, i = impacts; s < smax; s++, i -= 16)
99 sdtable[s] = i * i + dist2;
101 maxdist3 = maxdist - dist2;
103 // convert to 8.8 blocklights format
104 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
105 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
106 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
107 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
111 for (t = 0;t < tmax;t++, i -= 16)
114 // make sure some part of it is visible on this line
117 maxdist2 = maxdist - td;
118 for (s = 0;s < smax;s++)
120 if (sdtable[s] < maxdist2)
122 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
126 bl[1] += (green * k);
141 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
143 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
144 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
148 smax = (surf->extents[0] >> 4) + 1;
149 tmax = (surf->extents[1] >> 4) + 1;
152 for (lnum = 0; lnum < r_numdlights; lnum++)
154 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155 continue; // not lit by this light
157 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
158 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
160 // for comparisons to minimum acceptable light
161 // compensate for LIGHTOFFSET
162 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
165 dist2 += LIGHTOFFSET;
166 if (dist2 >= maxdist)
169 if (surf->plane->type < 3)
171 VectorCopy(local, impact);
172 impact[surf->plane->type] -= dist;
176 impact[0] = local[0] - surf->plane->normal[0] * dist;
177 impact[1] = local[1] - surf->plane->normal[1] * dist;
178 impact[2] = local[2] - surf->plane->normal[2] * dist;
181 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
184 td = bound(0, impacts, smax * 16) - impacts;
185 td1 = bound(0, impactt, tmax * 16) - impactt;
186 td = td * td + td1 * td1 + dist2;
190 // reduce calculations
191 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192 sdtable[s] = td1 * td1 + dist2;
194 maxdist3 = maxdist - dist2;
196 // convert to 8.8 blocklights format
197 red = r_dlight[lnum].light[0];
198 green = r_dlight[lnum].light[1];
199 blue = r_dlight[lnum].light[2];
200 subtract = r_dlight[lnum].subtract * 32768.0f;
201 bl = floatblocklights;
204 for (t = 0;t < tmax;t++, td1 -= 16.0f)
207 // make sure some part of it is visible on this line
210 maxdist2 = maxdist - td;
211 for (s = 0;s < smax;s++)
213 if (sdtable[s] < maxdist2)
215 k = (32768.0f / (sdtable[s] + td)) - subtract;
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
238 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int dlightchanged)
240 if (!r_floatbuildlightmap.integer)
242 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
243 unsigned int *bl, scale;
244 qbyte *lightmap, *out, *stain;
246 // update cached lighting info
247 surf->cached_dlight = 0;
248 surf->cached_lightmapscalebit = r_lightmapscalebit;
249 surf->cached_ambient = r_ambient.value;
250 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
251 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
252 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
253 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
255 smax = (surf->extents[0]>>4)+1;
256 tmax = (surf->extents[1]>>4)+1;
259 lightmap = surf->samples;
261 // set to full bright if no light data
263 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
265 for (i = 0;i < size3;i++)
271 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
274 for (i = 0;i < size3;i++)
278 memset(bl, 0, size*3*sizeof(unsigned int));
280 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
282 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
283 if (surf->cached_dlight)
285 else if (dlightchanged)
286 return; // don't upload if only updating dlights and none mattered
289 // add all the lightmaps
293 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
294 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
295 bl[i] += lightmap[i] * scale;
299 stain = surf->stainsamples;
302 // deal with lightmap brightness scale
303 shift = 7 + r_lightmapscalebit + 8;
304 if (ent->model->lightmaprgba)
306 stride = (surf->lightmaptexturestride - smax) * 4;
307 for (i = 0;i < tmax;i++, out += stride)
309 for (j = 0;j < smax;j++)
311 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 stride = (surf->lightmaptexturestride - smax) * 3;
321 for (i = 0;i < tmax;i++, out += stride)
323 for (j = 0;j < smax;j++)
325 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
332 R_UpdateTexture(surf->lightmaptexture, templight);
336 int smax, tmax, i, j, size, size3, maps, stride, l;
338 qbyte *lightmap, *out, *stain;
340 // update cached lighting info
341 surf->cached_dlight = 0;
342 surf->cached_lightmapscalebit = r_lightmapscalebit;
343 surf->cached_ambient = r_ambient.value;
344 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
345 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
346 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
347 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
349 smax = (surf->extents[0]>>4)+1;
350 tmax = (surf->extents[1]>>4)+1;
353 lightmap = surf->samples;
355 // set to full bright if no light data
356 bl = floatblocklights;
357 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
360 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
365 for (i = 0;i < size3;i++)
369 memset(bl, 0, size*3*sizeof(float));
371 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
373 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
374 if (surf->cached_dlight)
376 else if (dlightchanged)
377 return; // don't upload if only updating dlights and none mattered
380 // add all the lightmaps
383 bl = floatblocklights;
384 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
385 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
386 bl[i] += lightmap[i] * scale;
389 stain = surf->stainsamples;
390 bl = floatblocklights;
392 // deal with lightmap brightness scale
393 scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
394 if (ent->model->lightmaprgba)
396 stride = (surf->lightmaptexturestride - smax) * 4;
397 for (i = 0;i < tmax;i++, out += stride)
399 for (j = 0;j < smax;j++)
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 stride = (surf->lightmaptexturestride - smax) * 3;
411 for (i = 0;i < tmax;i++, out += stride)
413 for (j = 0;j < smax;j++)
415 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
422 R_UpdateTexture(surf->lightmaptexture, templight);
426 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, float fcolor[8])
428 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
429 msurface_t *surf, *endsurf;
430 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
434 maxdist = radius * radius;
435 invradius = 1.0f / radius;
438 if (node->contents < 0)
440 ndist = PlaneDiff(origin, node->plane);
443 node = node->children[0];
448 node = node->children[1];
452 dist2 = ndist * ndist;
453 maxdist3 = maxdist - dist2;
455 if (node->plane->type < 3)
457 VectorCopy(origin, impact);
458 impact[node->plane->type] -= ndist;
462 impact[0] = origin[0] - node->plane->normal[0] * ndist;
463 impact[1] = origin[1] - node->plane->normal[1] * ndist;
464 impact[2] = origin[2] - node->plane->normal[2] * ndist;
467 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
469 if (surf->stainsamples)
471 smax = (surf->extents[0] >> 4) + 1;
472 tmax = (surf->extents[1] >> 4) + 1;
474 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
475 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
477 s = bound(0, impacts, smax * 16) - impacts;
478 t = bound(0, impactt, tmax * 16) - impactt;
479 i = s * s + t * t + dist2;
483 // reduce calculations
484 for (s = 0, i = impacts; s < smax; s++, i -= 16)
485 sdtable[s] = i * i + dist2;
487 bl = surf->stainsamples;
492 for (t = 0;t < tmax;t++, i -= 16)
495 // make sure some part of it is visible on this line
498 maxdist2 = maxdist - td;
499 for (s = 0;s < smax;s++)
501 if (sdtable[s] < maxdist2)
503 ratio = lhrandom(0.0f, 1.0f);
504 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
505 if (a >= (1.0f / 64.0f))
509 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
510 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
511 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
521 // force lightmap upload
523 surf->cached_dlight = true;
527 if (node->children[0]->contents >= 0)
529 if (node->children[1]->contents >= 0)
531 R_StainNode(node->children[0], model, origin, radius, fcolor);
532 node = node->children[1];
537 node = node->children[0];
541 else if (node->children[1]->contents >= 0)
543 node = node->children[1];
548 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
552 entity_render_t *ent;
558 fcolor[3] = ca1 * (1.0f / 64.0f);
559 fcolor[4] = cr2 - cr1;
560 fcolor[5] = cg2 - cg1;
561 fcolor[6] = cb2 - cb1;
562 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
564 model = cl.worldmodel;
566 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, fcolor);
568 // look for embedded bmodels
569 for (n = 0;n < cl_num_brushmodel_entities;n++)
571 ent = cl_brushmodel_entities[n];
573 if (model && model->name[0] == '*')
575 Mod_CheckLoaded(model);
576 if (model->type == mod_brush)
578 Matrix4x4_Transform(&ent->inversematrix, origin, org);
579 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, fcolor);
587 =============================================================
591 =============================================================
594 static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
599 if (styles[0] != 255)
601 for (i = 0;i < numverts;i++, c += 4)
603 lm = samples + lightmapoffsets[i];
604 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
605 VectorMA(c, scale, lm, c);
606 if (styles[1] != 255)
609 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
610 VectorMA(c, scale, lm, c);
611 if (styles[2] != 255)
614 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
615 VectorMA(c, scale, lm, c);
616 if (styles[3] != 255)
619 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
620 VectorMA(c, scale, lm, c);
628 static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
634 for (i = 0;i < numverts;i++, v += 4, c += 4)
636 VectorSubtract(v, modelorg, diff);
637 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
638 VectorScale(c, f, c);
641 else if (colorscale != 1)
642 for (i = 0;i < numverts;i++, c += 4)
643 VectorScale(c, colorscale, c);
646 static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
655 for (i = 0;i < numverts;i++, v += 4, c += 4)
657 VectorSubtract(v, modelorg, diff);
658 f = 1 - exp(fogdensity/DotProduct(diff, diff));
667 for (i = 0;i < numverts;i++, c += 4)
677 static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
684 for (i = 0;i < numverts;i++, v += 4, c += 4)
686 VectorSubtract(v, modelorg, diff);
687 f = exp(fogdensity/DotProduct(diff, diff));
695 static void RSurf_ScaleColors(float *c, float scale, int numverts)
699 for (i = 0;i < numverts;i++, c += 4)
700 VectorScale(c, scale, c);
703 static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
708 int i, l, lit = false;
711 for (l = 0;l < r_numdlights;l++)
713 if (dlightbits[l >> 5] & (1 << (l & 31)))
716 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
717 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
719 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
720 if (f < rd->cullradius2)
722 f = (1.0f / f) - rd->subtract;
723 VectorMA(c, f, rd->light, c);
732 // note: this untransforms lights to do the checking,
733 // and takes surf->mesh->verts data
734 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
740 for (l = 0;l < r_numdlights;l++)
742 if (dlightbits[l >> 5] & (1 << (l & 31)))
745 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
746 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
747 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
754 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture)
756 const msurface_t *surf;
757 const surfmesh_t *mesh;
760 // LordHavoc: HalfLife maps have freaky skypolys...
761 if (ent->model->ishlbsp)
766 skyrendernow = false;
771 R_Mesh_Matrix(&ent->matrix);
773 // draw depth-only polys
774 memset(&m, 0, sizeof(m));
777 m.blendfunc1 = GL_ZERO;
778 m.blendfunc2 = GL_ONE;
783 m.blendfunc1 = GL_ONE;
784 m.blendfunc2 = GL_ZERO;
788 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
790 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
792 R_Mesh_ResizeCheck(mesh->numverts);
793 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
795 memset(varray_color, 0, mesh->numverts * sizeof(float[4]));
797 R_FillColors(varray_color, mesh->numverts, fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, 1);
798 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
803 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
805 const entity_render_t *ent = calldata1;
806 const msurface_t *surf = ent->model->surfaces + calldata2;
808 const surfmesh_t *mesh;
810 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
812 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
814 R_Mesh_Matrix(&ent->matrix);
816 memset(&m, 0, sizeof(m));
817 if (ent->effects & EF_ADDITIVE)
819 m.blendfunc1 = GL_SRC_ALPHA;
820 m.blendfunc2 = GL_ONE;
822 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
824 m.blendfunc1 = GL_SRC_ALPHA;
825 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
829 m.blendfunc1 = GL_ONE;
830 m.blendfunc2 = GL_ZERO;
832 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
833 colorscale = r_colorscale;
834 if (gl_combine.integer)
836 m.texrgbscale[0] = 4;
840 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
842 R_Mesh_ResizeCheck(mesh->numverts);
843 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
844 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
845 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
846 R_FillColors(varray_color, mesh->numverts, f, f, f, alpha);
847 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
849 if (surf->dlightframe == r_framecount)
850 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
851 if (surf->flags & SURF_LIGHTMAP)
852 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
854 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
855 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
860 memset(&m, 0, sizeof(m));
861 m.blendfunc1 = GL_SRC_ALPHA;
862 m.blendfunc2 = GL_ONE;
863 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
865 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
867 R_Mesh_ResizeCheck(mesh->numverts);
868 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
870 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
871 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, r_colorscale, mesh->numverts, modelorg);
872 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
877 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture)
879 const msurface_t *surf;
881 if ((r_wateralpha.value < 1 && !(texture->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || texture->fogtexture)
883 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
885 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
886 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
890 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
891 RSurfShader_Water_Callback(ent, surf - ent->model->surfaces);
894 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
896 float base, colorscale;
897 const surfmesh_t *mesh;
900 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
901 memset(&m, 0, sizeof(m));
902 if (ent->effects & EF_ADDITIVE)
904 m.blendfunc1 = GL_SRC_ALPHA;
905 m.blendfunc2 = GL_ONE;
907 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
909 m.blendfunc1 = GL_SRC_ALPHA;
910 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
914 m.blendfunc1 = GL_ONE;
915 m.blendfunc2 = GL_ZERO;
917 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
918 colorscale = r_colorscale;
919 if (gl_combine.integer)
921 m.texrgbscale[0] = 4;
924 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
926 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
928 R_Mesh_ResizeCheck(mesh->numverts);
929 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
930 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
931 R_FillColors(varray_color, mesh->numverts, base, base, base, ent->alpha);
932 if (!(ent->effects & EF_FULLBRIGHT))
934 if (surf->dlightframe == r_framecount)
935 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
936 if (surf->flags & SURF_LIGHTMAP)
937 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
939 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
940 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
944 static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, const msurface_t *surf)
946 const surfmesh_t *mesh;
949 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
950 memset(&m, 0, sizeof(m));
951 if (ent->effects & EF_ADDITIVE)
953 m.blendfunc1 = GL_SRC_ALPHA;
954 m.blendfunc2 = GL_ONE;
956 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
958 m.blendfunc1 = GL_SRC_ALPHA;
959 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
963 m.blendfunc1 = GL_ONE;
964 m.blendfunc2 = GL_ZERO;
966 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
968 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
970 R_Mesh_ResizeCheck(mesh->numverts);
971 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
972 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
973 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, r_colorscale, mesh->numverts, modelorg);
974 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
978 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
980 const surfmesh_t *mesh;
983 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
984 memset(&m, 0, sizeof(m));
985 m.blendfunc1 = GL_SRC_ALPHA;
986 m.blendfunc2 = GL_ONE;
987 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
989 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
991 R_Mesh_ResizeCheck(mesh->numverts);
992 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
993 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
994 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, r_colorscale, mesh->numverts, modelorg);
995 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
999 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
1001 const surfmesh_t *mesh;
1004 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1005 memset(&m, 0, sizeof(m));
1006 m.blendfunc1 = GL_SRC_ALPHA;
1007 m.blendfunc2 = GL_ONE;
1008 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1010 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1012 R_Mesh_ResizeCheck(mesh->numverts);
1013 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1015 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1016 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, r_colorscale, mesh->numverts, modelorg);
1017 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1021 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture)
1023 const msurface_t *surf;
1024 const surfmesh_t *mesh;
1026 int lightmaptexturenum;
1028 memset(&m, 0, sizeof(m));
1029 m.blendfunc1 = GL_ONE;
1030 m.blendfunc2 = GL_ZERO;
1031 m.tex[0] = R_GetTexture(texture->texture);
1032 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1033 m.tex[2] = R_GetTexture(texture->detailtexture);
1034 m.texrgbscale[0] = 1;
1035 m.texrgbscale[1] = 4;
1036 m.texrgbscale[2] = 2;
1038 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1040 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1041 if (m.tex[1] != lightmaptexturenum)
1043 m.tex[1] = lightmaptexturenum;
1046 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1048 R_Mesh_ResizeCheck(mesh->numverts);
1049 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1050 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1051 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1052 memcpy(varray_texcoord[2], mesh->ab, mesh->numverts * sizeof(float[2]));
1053 cl = (float) (1 << r_lightmapscalebit) * r_colorscale;
1054 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1055 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1060 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture)
1062 const msurface_t *surf;
1063 const surfmesh_t *mesh;
1065 int lightmaptexturenum;
1066 memset(&m, 0, sizeof(m));
1067 m.blendfunc1 = GL_ONE;
1068 m.blendfunc2 = GL_ZERO;
1069 m.tex[0] = R_GetTexture(texture->texture);
1070 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1071 if (gl_combine.integer)
1072 m.texrgbscale[1] = 4;
1074 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1076 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1077 if (m.tex[1] != lightmaptexturenum)
1079 m.tex[1] = lightmaptexturenum;
1082 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1084 R_Mesh_ResizeCheck(mesh->numverts);
1085 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1086 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1087 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1088 R_FillColors(varray_color, mesh->numverts, r_colorscale, r_colorscale, r_colorscale, 1);
1089 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1094 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture)
1096 const msurface_t *surf;
1097 const surfmesh_t *mesh;
1099 memset(&m, 0, sizeof(m));
1100 m.blendfunc1 = GL_ONE;
1101 m.blendfunc2 = GL_ZERO;
1102 m.tex[0] = R_GetTexture(texture->texture);
1104 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1106 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1108 R_Mesh_ResizeCheck(mesh->numverts);
1109 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1110 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1111 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1112 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1117 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture)
1119 const msurface_t *surf;
1120 const surfmesh_t *mesh;
1122 int lightmaptexturenum;
1123 memset(&m, 0, sizeof(m));
1124 m.blendfunc1 = GL_ZERO;
1125 m.blendfunc2 = GL_SRC_COLOR;
1126 m.tex[0] = R_GetTexture(texture->surfacechain->lightmaptexture);
1127 if (gl_combine.integer)
1128 m.texrgbscale[0] = 4;
1130 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1132 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1133 if (m.tex[0] != lightmaptexturenum)
1135 m.tex[0] = lightmaptexturenum;
1138 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1140 R_Mesh_ResizeCheck(mesh->numverts);
1141 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1142 memcpy(varray_texcoord[0], mesh->uv, mesh->numverts * sizeof(float[2]));
1143 R_FillColors(varray_color, mesh->numverts, r_colorscale, r_colorscale, r_colorscale, 1);
1144 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1149 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture)
1151 const msurface_t *surf;
1152 const surfmesh_t *mesh;
1156 memset(&m, 0, sizeof(m));
1157 m.blendfunc1 = GL_SRC_ALPHA;
1158 m.blendfunc2 = GL_ONE;
1159 m.tex[0] = R_GetTexture(texture->texture);
1160 colorscale = r_colorscale;
1161 if (gl_combine.integer)
1163 m.texrgbscale[0] = 4;
1164 colorscale *= 0.25f;
1167 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1169 if (surf->dlightframe == r_framecount)
1171 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1173 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1175 R_Mesh_ResizeCheck(mesh->numverts);
1176 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1177 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1178 R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
1179 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
1180 RSurf_ScaleColors(varray_color, colorscale, mesh->numverts);
1181 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1188 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture)
1190 const msurface_t *surf;
1191 const surfmesh_t *mesh;
1194 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1195 memset(&m, 0, sizeof(m));
1196 m.blendfunc1 = GL_SRC_ALPHA;
1197 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1199 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1201 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1203 R_Mesh_ResizeCheck(mesh->numverts);
1204 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1206 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1207 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, r_colorscale, mesh->numverts, modelorg);
1208 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1213 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture)
1215 const msurface_t *surf;
1216 const surfmesh_t *mesh;
1218 memset(&m, 0, sizeof(m));
1219 m.blendfunc1 = GL_DST_COLOR;
1220 m.blendfunc2 = GL_SRC_COLOR;
1221 m.tex[0] = R_GetTexture(texture->detailtexture);
1223 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1225 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1227 R_Mesh_ResizeCheck(mesh->numverts);
1228 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1229 memcpy(varray_texcoord[0], mesh->ab, mesh->numverts * sizeof(float[2]));
1230 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1231 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1236 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture)
1238 const msurface_t *surf;
1239 const surfmesh_t *mesh;
1241 memset(&m, 0, sizeof(m));
1242 m.blendfunc1 = GL_SRC_ALPHA;
1243 m.blendfunc2 = GL_ONE;
1244 m.tex[0] = R_GetTexture(texture->glowtexture);
1246 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1248 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1250 R_Mesh_ResizeCheck(mesh->numverts);
1251 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1252 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1253 R_FillColors(varray_color, mesh->numverts, r_colorscale, r_colorscale, r_colorscale, 1);
1254 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1259 static void RSurfShader_Wall_Fullbright_Callback(const void *calldata1, int calldata2)
1261 const entity_render_t *ent = calldata1;
1262 const msurface_t *surf = ent->model->surfaces + calldata2;
1263 R_Mesh_Matrix(&ent->matrix);
1264 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1265 if (surf->currenttexture->glowtexture)
1266 RSurfShader_Wall_Pass_Glow(ent, surf);
1268 RSurfShader_Wall_Pass_Fog(ent, surf);
1271 static void RSurfShader_Wall_Fullbright(const entity_render_t *ent, const texture_t *texture)
1273 const msurface_t *surf;
1275 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1277 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1279 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1280 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
1285 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1286 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1287 if (texture->glowtexture)
1288 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1289 RSurfShader_Wall_Pass_Glow(ent, surf);
1291 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1292 RSurfShader_Wall_Pass_Fog(ent, surf);
1296 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1298 const entity_render_t *ent = calldata1;
1299 const msurface_t *surf = ent->model->surfaces + calldata2;
1300 R_Mesh_Matrix(&ent->matrix);
1301 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1302 if (surf->currenttexture->glowtexture)
1303 RSurfShader_Wall_Pass_Glow(ent, surf);
1305 RSurfShader_Wall_Pass_Fog(ent, surf);
1308 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture)
1310 const msurface_t *surf;
1312 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1314 // transparent vertex shaded from lightmap
1315 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1317 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1318 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1321 else if (r_vertexsurfaces.integer)
1323 // opaque vertex shaded from lightmap
1324 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1325 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1326 if (texture->glowtexture)
1327 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1328 RSurfShader_Wall_Pass_Glow(ent, surf);
1330 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1331 RSurfShader_Wall_Pass_Fog(ent, surf);
1335 // opaque lightmapped
1336 if (r_textureunits.integer >= 2)
1338 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1339 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture);
1342 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture);
1343 if (r_detailtextures.integer)
1344 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1349 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture);
1350 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture);
1351 if (r_detailtextures.integer)
1352 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1354 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1355 RSurfShader_OpaqueWall_Pass_Light(ent, texture);
1356 if (texture->glowtexture)
1357 RSurfShader_OpaqueWall_Pass_Glow(ent, texture);
1359 RSurfShader_OpaqueWall_Pass_Fog(ent, texture);
1363 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1364 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, 0};
1365 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1366 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1368 int Cshader_count = 4;
1369 Cshader_t *Cshaders[4] =
1371 &Cshader_wall_lightmap,
1372 &Cshader_wall_fullbright,
1377 void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
1379 int i, alttextures, texframe, framecount, numtextures, numsurfaces, *surfacevisframes;
1380 texture_t *t, *textures;
1382 msurface_t *surf, *surfaces;
1388 // mark the surfaces touched by dynamic lights
1389 if (normal && r_dynamic.integer)
1392 R_Mesh_Matrix(&ent->matrix);
1395 alttextures = ent->frame != 0;
1396 texframe = (int)(cl.time * 5.0f);
1398 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1400 numtextures = model->numtextures;
1401 textures = model->textures;
1402 numsurfaces = model->nummodelsurfaces;
1403 surfaces = model->surfaces + model->firstmodelsurface;
1404 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1406 for (i = 0;i < numtextures;i++)
1407 textures[i].surfacechain = NULL;
1409 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1411 if (surfacevisframes[i] == r_framecount)
1413 #if !WORLDNODECULLBACKFACES
1414 // mark any backface surfaces as not visible
1415 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1417 if (!(surf->flags & SURF_PLANEBACK))
1418 surfacevisframes[i] = -1;
1422 if ((surf->flags & SURF_PLANEBACK))
1423 surfacevisframes[i] = -1;
1425 if (surfacevisframes[i] == r_framecount)
1429 t = surf->texinfo->texture;
1432 framecount = t->anim_total[alttextures];
1433 if (framecount >= 2)
1434 t = t->anim_frames[alttextures][texframe % framecount];
1436 t = t->anim_frames[alttextures][0];
1438 surf->currenttexture = t;
1439 surf->texturechain = t->surfacechain;
1440 t->surfacechain = surf;
1441 if (!r_vertexsurfaces.integer && surf->lightmaptexture != NULL)
1443 if (surf->cached_dlight
1444 || surf->cached_ambient != r_ambient.value
1445 || surf->cached_lightmapscalebit != r_lightmapscalebit)
1446 R_BuildLightMap(ent, surf, false); // base lighting changed
1447 else if (r_dynamic.integer)
1449 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1450 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1451 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1452 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1453 R_BuildLightMap(ent, surf, false); // base lighting changed
1454 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1455 R_BuildLightMap(ent, surf, true); // only dlights
1463 for (i = 0, t = textures;i < numtextures;i++, t++)
1464 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_SKY])
1465 t->shader->shaderfunc[SHADERSTAGE_SKY](ent, t);
1468 for (i = 0, t = textures;i < numtextures;i++, t++)
1469 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_NORMAL])
1470 t->shader->shaderfunc[SHADERSTAGE_NORMAL](ent, t);
1473 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1478 const entity_render_t *ent = calldata1;
1479 const mportal_t *portal = ent->model->portals + calldata2;
1480 memset(&m, 0, sizeof(m));
1481 m.blendfunc1 = GL_SRC_ALPHA;
1482 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1483 R_Mesh_Matrix(&ent->matrix);
1485 R_Mesh_ResizeCheck(portal->numpoints);
1486 i = portal - ent->model->portals;
1487 R_FillColors(varray_color, portal->numpoints,
1488 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_colorscale,
1489 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_colorscale,
1490 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_colorscale,
1492 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1494 for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
1495 VectorCopy(portal->points[i].position, v);
1498 for (i = 0, v = varray_vertex;i < portal->numpoints;i++, v += 4)
1499 VectorCopy(portal->points[i].position, v);
1500 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1503 static void R_DrawPortals(entity_render_t *ent)
1506 mportal_t *portal, *endportal;
1507 float temp[3], center[3], f;
1509 if (r_drawportals.integer < 1)
1512 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1514 if (portal->here->pvsframe == r_pvsframecount || portal->past->pvsframe == r_pvsframecount)
1516 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1519 for (i = 0;i < portal->numpoints;i++)
1520 VectorAdd(temp, portal->points[i].position, temp);
1521 f = ixtable[portal->numpoints];
1522 VectorScale(temp, f, temp);
1523 Matrix4x4_Transform(&ent->matrix, temp, center);
1524 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1530 void R_DrawBrushModel(entity_render_t *ent, int sky, int normal)
1532 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1535 #if WORLDNODECULLBACKFACES
1539 // because bmodels can be reused, we have to decide which things to render
1540 // from scratch every time
1542 #if WORLDNODECULLBACKFACES
1543 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1545 numsurfaces = model->nummodelsurfaces;
1546 surf = model->surfaces + model->firstmodelsurface;
1547 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1548 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1549 for (i = 0;i < numsurfaces;i++, surf++)
1551 #if WORLDNODECULLBACKFACES
1552 // mark any backface surfaces as not visible
1553 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1555 if ((surf->flags & SURF_PLANEBACK))
1557 surfacevisframes[i] = r_framecount;
1558 surfacepvsframes[i] = r_pvsframecount;
1563 if (!(surf->flags & SURF_PLANEBACK))
1565 surfacevisframes[i] = r_framecount;
1566 surfacepvsframes[i] = r_pvsframecount;
1570 surfacevisframes[i] = r_framecount;
1571 surfacepvsframes[i] = r_pvsframecount;
1573 surf->dlightframe = -1;
1575 R_DrawSurfaces(ent, sky, normal);
1578 void R_SurfaceWorldNode (entity_render_t *ent)
1580 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1581 msurface_t *surfaces, *surf;
1586 numsurfaces = model->nummodelsurfaces;
1587 surfaces = model->surfaces + model->firstmodelsurface;
1588 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1589 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1590 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1592 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1594 if (surfacepvsframes[i] == r_pvsframecount)
1596 #if WORLDNODECULLBACKFACES
1597 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1599 if ((surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1600 surfacevisframes[i] = r_framecount;
1604 if (!(surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1605 surfacevisframes[i] = r_framecount;
1608 if (R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1609 surfacevisframes[i] = r_framecount;
1616 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1619 mportal_t *p, *pstack[8192];
1620 msurface_t *surf, **mark, **endmark;
1622 // LordHavoc: portal-passage worldnode with PVS;
1623 // follows portals leading outward from viewleaf, does not venture
1624 // offscreen or into leafs that are not visible, faster than Quake's
1625 // RecursiveWorldNode
1627 leaf->worldnodeframe = r_framecount;
1631 if (leaf->nummarksurfaces)
1633 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1636 // make sure surfaces are only processed once
1637 if (surf->worldnodeframe != r_framecount)
1639 surf->worldnodeframe = r_framecount;
1640 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1642 if (surf->flags & SURF_PLANEBACK)
1643 surf->visframe = r_framecount;
1647 if (!(surf->flags & SURF_PLANEBACK))
1648 surf->visframe = r_framecount;
1653 // follow portals into other leafs
1654 for (p = leaf->portals;p;p = p->next)
1657 if (leaf->worldnodeframe != r_framecount)
1659 leaf->worldnodeframe = r_framecount;
1660 // FIXME: R_NotCulledBox is absolute, should be done relative
1661 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1663 p->visframe = r_framecount;
1664 pstack[portalstack++] = p;
1667 p = pstack[--portalstack];
1676 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1678 int c, leafstackpos, *mark, *surfacevisframes;
1679 #if WORLDNODECULLBACKFACES
1683 mleaf_t *leaf, *leafstack[8192];
1686 msurface_t *surfaces;
1687 // LordHavoc: portal-passage worldnode with PVS;
1688 // follows portals leading outward from viewleaf, does not venture
1689 // offscreen or into leafs that are not visible, faster than Quake's
1690 // RecursiveWorldNode
1691 surfaces = ent->model->surfaces;
1692 surfacevisframes = ent->model->surfacevisframes;
1693 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1694 viewleaf->worldnodeframe = r_framecount;
1695 leafstack[0] = viewleaf;
1697 while (leafstackpos)
1700 leaf = leafstack[--leafstackpos];
1701 // draw any surfaces bounding this leaf
1702 if (leaf->nummarksurfaces)
1704 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1706 #if WORLDNODECULLBACKFACES
1708 if (surfacevisframes[n] != r_framecount)
1710 surf = surfaces + n;
1711 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1713 if ((surf->flags & SURF_PLANEBACK))
1714 surfacevisframes[n] = r_framecount;
1718 if (!(surf->flags & SURF_PLANEBACK))
1719 surfacevisframes[n] = r_framecount;
1723 surfacevisframes[*mark++] = r_framecount;
1727 // follow portals into other leafs
1728 for (p = leaf->portals;p;p = p->next)
1730 // LordHavoc: this DotProduct hurts less than a cache miss
1731 // (which is more likely to happen if backflowing through leafs)
1732 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1735 if (leaf->worldnodeframe != r_framecount)
1737 leaf->worldnodeframe = r_framecount;
1738 // FIXME: R_NotCulledBox is absolute, should be done relative
1739 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1740 leafstack[leafstackpos++] = leaf;
1745 if (r_drawportals.integer)
1749 void R_PVSUpdate (mleaf_t *viewleaf)
1751 int i, j, l, c, bits, *surfacepvsframes, *mark;
1755 if (r_pvsviewleaf == viewleaf && r_pvsviewleafnovis == r_novis.integer)
1759 r_pvsviewleaf = viewleaf;
1760 r_pvsviewleafnovis = r_novis.integer;
1764 surfacepvsframes = cl.worldmodel->surfacepvsframes;
1765 vis = Mod_LeafPVS (viewleaf, cl.worldmodel);
1766 for (j = 0;j < cl.worldmodel->numleafs;j += 8)
1771 l = cl.worldmodel->numleafs - j;
1774 for (i = 0;i < l;i++)
1776 if (bits & (1 << i))
1778 leaf = &cl.worldmodel->leafs[j + i + 1];
1779 leaf->pvsframe = r_pvsframecount;
1780 // mark surfaces bounding this leaf as visible
1781 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1782 surfacepvsframes[*mark++] = r_pvsframecount;
1795 void R_DrawWorld (entity_render_t *ent)
1798 viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
1799 R_PVSUpdate(viewleaf);
1802 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1803 R_SurfaceWorldNode (ent);
1805 R_PortalWorldNode (ent, viewleaf);
1806 R_DrawSurfaces(ent, true, true);
1814 void R_DrawBrushModelSky (entity_render_t *ent)
1816 R_DrawBrushModel(ent, true, false);
1819 void R_DrawBrushModelNormal (entity_render_t *ent)
1822 R_DrawBrushModel(ent, false, true);
1825 static void gl_surf_start(void)
1829 static void gl_surf_shutdown(void)
1833 static void gl_surf_newmap(void)
1835 // reset pvs visibility variables so it will update on first frame
1836 r_pvsframecount = 1;
1837 r_pvsviewleaf = NULL;
1838 r_pvsviewleafnovis = false;
1841 void GL_Surf_Init(void)
1844 dlightdivtable[0] = 4194304;
1845 for (i = 1;i < 32768;i++)
1846 dlightdivtable[i] = 4194304 / (i << 7);
1848 Cvar_RegisterVariable(&r_ambient);
1849 Cvar_RegisterVariable(&r_vertexsurfaces);
1850 Cvar_RegisterVariable(&r_dlightmap);
1851 Cvar_RegisterVariable(&r_drawportals);
1852 Cvar_RegisterVariable(&r_testvis);
1853 Cvar_RegisterVariable(&r_floatbuildlightmap);
1854 Cvar_RegisterVariable(&r_detailtextures);
1855 Cvar_RegisterVariable(&r_surfaceworldnode);
1857 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);