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;
51 float dist, impact[3], local[3];
53 // LordHavoc: use 64bit integer... shame it's not very standardized...
54 #if _MSC_VER || __BORLANDC__
62 smax = (surf->extents[0] >> 4) + 1;
63 tmax = (surf->extents[1] >> 4) + 1;
66 for (lnum = 0; lnum < r_numdlights; lnum++)
68 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
69 continue; // not lit by this light
71 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
72 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
74 // for comparisons to minimum acceptable light
75 // compensate for LIGHTOFFSET
76 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
83 if (surf->plane->type < 3)
85 VectorCopy(local, impact);
86 impact[surf->plane->type] -= dist;
90 impact[0] = local[0] - surf->plane->normal[0] * dist;
91 impact[1] = local[1] - surf->plane->normal[1] * dist;
92 impact[2] = local[2] - surf->plane->normal[2] * dist;
95 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
96 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
98 s = bound(0, impacts, smax * 16) - impacts;
99 t = bound(0, impactt, tmax * 16) - impactt;
100 i = s * s + t * t + dist2;
104 // reduce calculations
105 for (s = 0, i = impacts; s < smax; s++, i -= 16)
106 sdtable[s] = i * i + dist2;
108 maxdist3 = maxdist - dist2;
110 // convert to 8.8 blocklights format
111 red = r_dlight[lnum].light[0];
112 green = r_dlight[lnum].light[1];
113 blue = r_dlight[lnum].light[2];
114 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
118 for (t = 0;t < tmax;t++, i -= 16)
121 // make sure some part of it is visible on this line
124 maxdist2 = maxdist - td;
125 for (s = 0;s < smax;s++)
127 if (sdtable[s] < maxdist2)
129 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
132 bl[0] += (red * k) >> 7;
133 bl[1] += (green * k) >> 7;
134 bl[2] += (blue * k) >> 7;
148 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
150 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
151 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
155 smax = (surf->extents[0] >> 4) + 1;
156 tmax = (surf->extents[1] >> 4) + 1;
159 for (lnum = 0; lnum < r_numdlights; lnum++)
161 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
162 continue; // not lit by this light
164 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
165 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
167 // for comparisons to minimum acceptable light
168 // compensate for LIGHTOFFSET
169 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
172 dist2 += LIGHTOFFSET;
173 if (dist2 >= maxdist)
176 if (surf->plane->type < 3)
178 VectorCopy(local, impact);
179 impact[surf->plane->type] -= dist;
183 impact[0] = local[0] - surf->plane->normal[0] * dist;
184 impact[1] = local[1] - surf->plane->normal[1] * dist;
185 impact[2] = local[2] - surf->plane->normal[2] * dist;
188 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
189 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
191 td = bound(0, impacts, smax * 16) - impacts;
192 td1 = bound(0, impactt, tmax * 16) - impactt;
193 td = td * td + td1 * td1 + dist2;
197 // reduce calculations
198 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
199 sdtable[s] = td1 * td1 + dist2;
201 maxdist3 = maxdist - dist2;
203 // convert to 8.8 blocklights format
204 red = r_dlight[lnum].light[0];
205 green = r_dlight[lnum].light[1];
206 blue = r_dlight[lnum].light[2];
207 subtract = r_dlight[lnum].subtract * 32768.0f;
208 bl = floatblocklights;
211 for (t = 0;t < tmax;t++, td1 -= 16.0f)
214 // make sure some part of it is visible on this line
217 maxdist2 = maxdist - td;
218 for (s = 0;s < smax;s++)
220 if (sdtable[s] < maxdist2)
222 k = (32768.0f / (sdtable[s] + td)) - subtract;
242 Combine and scale multiple lightmaps into the 8.8 format in blocklights
245 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int dlightchanged)
247 if (!r_floatbuildlightmap.integer)
249 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
250 unsigned int *bl, scale;
251 qbyte *lightmap, *out, *stain;
253 // update cached lighting info
254 surf->cached_dlight = 0;
255 surf->cached_lightscalebit = lightscalebit;
256 surf->cached_ambient = r_ambient.value;
257 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
258 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
259 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
260 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
262 smax = (surf->extents[0]>>4)+1;
263 tmax = (surf->extents[1]>>4)+1;
266 lightmap = surf->samples;
268 // set to full bright if no light data
270 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
272 for (i = 0;i < size3;i++)
278 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
281 for (i = 0;i < size3;i++)
285 memset(bl, 0, size*3*sizeof(unsigned int));
287 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
289 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
290 if (surf->cached_dlight)
292 else if (dlightchanged)
293 return; // don't upload if only updating dlights and none mattered
296 // add all the lightmaps
300 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
301 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
302 bl[i] += lightmap[i] * scale;
306 stain = surf->stainsamples;
309 // deal with lightmap brightness scale
310 shift = 7 + lightscalebit + 8;
311 if (ent->model->lightmaprgba)
313 stride = (surf->lightmaptexturestride - smax) * 4;
314 for (i = 0;i < tmax;i++, out += stride)
316 for (j = 0;j < smax;j++)
318 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
319 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 stride = (surf->lightmaptexturestride - smax) * 3;
328 for (i = 0;i < tmax;i++, out += stride)
330 for (j = 0;j < smax;j++)
332 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
333 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
334 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
339 R_UpdateTexture(surf->lightmaptexture, templight);
343 int smax, tmax, i, j, size, size3, maps, stride, l;
345 qbyte *lightmap, *out, *stain;
347 // update cached lighting info
348 surf->cached_dlight = 0;
349 surf->cached_lightscalebit = lightscalebit;
350 surf->cached_ambient = r_ambient.value;
351 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
352 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
353 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
354 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
356 smax = (surf->extents[0]>>4)+1;
357 tmax = (surf->extents[1]>>4)+1;
360 lightmap = surf->samples;
362 // set to full bright if no light data
363 bl = floatblocklights;
364 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
367 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
372 for (i = 0;i < size3;i++)
376 memset(bl, 0, size*3*sizeof(float));
378 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
380 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
381 if (surf->cached_dlight)
383 else if (dlightchanged)
384 return; // don't upload if only updating dlights and none mattered
387 // add all the lightmaps
390 bl = floatblocklights;
391 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
392 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
393 bl[i] += lightmap[i] * scale;
396 stain = surf->stainsamples;
397 bl = floatblocklights;
399 // deal with lightmap brightness scale
400 scale = 1.0f / (1 << (7 + lightscalebit + 8));
401 if (ent->model->lightmaprgba)
403 stride = (surf->lightmaptexturestride - smax) * 4;
404 for (i = 0;i < tmax;i++, out += stride)
406 for (j = 0;j < smax;j++)
408 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
409 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 stride = (surf->lightmaptexturestride - smax) * 3;
418 for (i = 0;i < tmax;i++, out += stride)
420 for (j = 0;j < smax;j++)
422 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
423 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
424 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
429 R_UpdateTexture(surf->lightmaptexture, templight);
433 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
436 msurface_t *surf, *endsurf;
437 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
440 // LordHavoc: use 64bit integer... shame it's not very standardized...
441 #if _MSC_VER || __BORLANDC__
448 // for comparisons to minimum acceptable light
449 // compensate for 256 offset
450 maxdist = radius * radius + 256.0f;
452 // clamp radius to avoid exceeding 32768 entry division table
453 if (maxdist > 4194304)
456 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
459 if (node->contents < 0)
461 ndist = PlaneDiff(origin, node->plane);
464 node = node->children[0];
469 node = node->children[1];
473 dist2 = ndist * ndist + 256.0f;
476 maxdist3 = maxdist - dist2;
478 if (node->plane->type < 3)
480 VectorCopy(origin, impact);
481 impact[node->plane->type] -= ndist;
485 impact[0] = origin[0] - node->plane->normal[0] * ndist;
486 impact[1] = origin[1] - node->plane->normal[1] * ndist;
487 impact[2] = origin[2] - node->plane->normal[2] * ndist;
490 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
492 if (surf->stainsamples)
494 smax = (surf->extents[0] >> 4) + 1;
495 tmax = (surf->extents[1] >> 4) + 1;
497 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
498 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
500 s = bound(0, impacts, smax * 16) - impacts;
501 t = bound(0, impactt, tmax * 16) - impactt;
502 i = s * s + t * t + dist2;
506 // reduce calculations
507 for (s = 0, i = impacts; s < smax; s++, i -= 16)
508 sdtable[s] = i * i + dist2;
510 // convert to 8.8 blocklights format
511 bl = surf->stainsamples;
516 for (t = 0;t < tmax;t++, i -= 16)
519 // make sure some part of it is visible on this line
522 maxdist2 = maxdist - td;
523 for (s = 0;s < smax;s++)
525 if (sdtable[s] < maxdist2)
527 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
530 ratio = rand() & 255;
531 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
535 a = bound(0, a, 256);
536 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
537 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
538 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
539 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
540 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
541 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
552 // force lightmap upload
554 surf->cached_dlight = true;
559 if (node->children[0]->contents >= 0)
561 if (node->children[1]->contents >= 0)
563 R_StainNode(node->children[0], model, origin, radius, icolor);
564 node = node->children[1];
569 node = node->children[0];
573 else if (node->children[1]->contents >= 0)
575 node = node->children[1];
580 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
583 entity_render_t *ent;
595 model = cl.worldmodel;
597 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
599 // look for embedded bmodels
600 for (n = 0;n < cl_num_brushmodel_entities;n++)
602 ent = cl_brushmodel_entities[n];
604 if (model && model->name[0] == '*')
606 Mod_CheckLoaded(model);
607 if (model->type == mod_brush)
609 Matrix4x4_Transform(&ent->inversematrix, origin, org);
610 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
618 =============================================================
622 =============================================================
625 static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
630 if (styles[0] != 255)
632 for (i = 0;i < numverts;i++, c += 4)
634 lm = samples + lightmapoffsets[i];
635 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
636 VectorMA(c, scale, lm, c);
637 if (styles[1] != 255)
640 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
641 VectorMA(c, scale, lm, c);
642 if (styles[2] != 255)
645 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
646 VectorMA(c, scale, lm, c);
647 if (styles[3] != 255)
650 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
651 VectorMA(c, scale, lm, c);
659 static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
665 for (i = 0;i < numverts;i++, v += 4, c += 4)
667 VectorSubtract(v, modelorg, diff);
668 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
669 VectorScale(c, f, c);
672 else if (colorscale != 1)
673 for (i = 0;i < numverts;i++, c += 4)
674 VectorScale(c, colorscale, c);
677 static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
686 for (i = 0;i < numverts;i++, v += 4, c += 4)
688 VectorSubtract(v, modelorg, diff);
689 f = 1 - exp(fogdensity/DotProduct(diff, diff));
698 for (i = 0;i < numverts;i++, c += 4)
708 static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
715 for (i = 0;i < numverts;i++, v += 4, c += 4)
717 VectorSubtract(v, modelorg, diff);
718 f = exp(fogdensity/DotProduct(diff, diff));
726 static void RSurf_ScaleColors(float *c, float scale, int numverts)
730 for (i = 0;i < numverts;i++, c += 4)
731 VectorScale(c, scale, c);
734 static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
739 int i, l, lit = false;
742 for (l = 0;l < r_numdlights;l++)
744 if (dlightbits[l >> 5] & (1 << (l & 31)))
747 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
748 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
750 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
751 if (f < rd->cullradius2)
753 f = (1.0f / f) - rd->subtract;
754 VectorMA(c, f, rd->light, c);
763 // note: this untransforms lights to do the checking,
764 // and takes surf->mesh->verts data
765 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
771 for (l = 0;l < r_numdlights;l++)
773 if (dlightbits[l >> 5] & (1 << (l & 31)))
776 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
777 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
778 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
785 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture)
787 const msurface_t *surf;
788 const surfmesh_t *mesh;
791 // LordHavoc: HalfLife maps have freaky skypolys...
792 if (ent->model->ishlbsp)
797 skyrendernow = false;
802 R_Mesh_Matrix(&ent->matrix);
804 // draw depth-only polys
805 memset(&m, 0, sizeof(m));
808 m.blendfunc1 = GL_ZERO;
809 m.blendfunc2 = GL_ONE;
814 m.blendfunc1 = GL_ONE;
815 m.blendfunc2 = GL_ZERO;
817 m.wantoverbright = false;
820 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
822 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
824 R_Mesh_ResizeCheck(mesh->numverts);
825 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
827 memset(varray_color, 0, mesh->numverts * sizeof(float[4]));
829 R_FillColors(varray_color, mesh->numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, 1);
830 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
835 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
837 const entity_render_t *ent = calldata1;
838 const msurface_t *surf = ent->model->surfaces + calldata2;
840 const surfmesh_t *mesh;
842 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
844 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
846 R_Mesh_Matrix(&ent->matrix);
848 memset(&m, 0, sizeof(m));
849 if (ent->effects & EF_ADDITIVE)
851 m.blendfunc1 = GL_SRC_ALPHA;
852 m.blendfunc2 = GL_ONE;
854 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
856 m.blendfunc1 = GL_SRC_ALPHA;
857 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
861 m.blendfunc1 = GL_ONE;
862 m.blendfunc2 = GL_ZERO;
864 m.wantoverbright = true;
865 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
867 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
869 R_Mesh_ResizeCheck(mesh->numverts);
870 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
871 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
872 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
873 R_FillColors(varray_color, mesh->numverts, f, f, f, alpha);
874 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
876 if (surf->dlightframe == r_framecount)
877 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
878 if (surf->flags & SURF_LIGHTMAP)
879 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
881 RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
882 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
887 memset(&m, 0, sizeof(m));
888 m.blendfunc1 = GL_SRC_ALPHA;
889 m.blendfunc2 = GL_ONE;
890 m.wantoverbright = false;
891 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
893 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
895 R_Mesh_ResizeCheck(mesh->numverts);
896 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
898 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
899 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, mesh_colorscale, mesh->numverts, modelorg);
900 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
905 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture)
907 const msurface_t *surf;
909 if ((r_wateralpha.value < 1 && !(texture->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || texture->fogtexture)
911 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
913 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
914 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
918 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
919 RSurfShader_Water_Callback(ent, surf - ent->model->surfaces);
922 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
925 const surfmesh_t *mesh;
928 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
929 memset(&m, 0, sizeof(m));
930 if (ent->effects & EF_ADDITIVE)
932 m.blendfunc1 = GL_SRC_ALPHA;
933 m.blendfunc2 = GL_ONE;
935 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
937 m.blendfunc1 = GL_SRC_ALPHA;
938 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
942 m.blendfunc1 = GL_ONE;
943 m.blendfunc2 = GL_ZERO;
945 m.wantoverbright = true;
946 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
947 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
949 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
951 R_Mesh_ResizeCheck(mesh->numverts);
952 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
953 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
954 R_FillColors(varray_color, mesh->numverts, base, base, base, ent->alpha);
955 if (!(ent->effects & EF_FULLBRIGHT))
957 if (surf->dlightframe == r_framecount)
958 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
959 if (surf->flags & SURF_LIGHTMAP)
960 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
962 RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
963 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
967 static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, const msurface_t *surf)
969 const surfmesh_t *mesh;
972 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
973 memset(&m, 0, sizeof(m));
974 if (ent->effects & EF_ADDITIVE)
976 m.blendfunc1 = GL_SRC_ALPHA;
977 m.blendfunc2 = GL_ONE;
979 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
981 m.blendfunc1 = GL_SRC_ALPHA;
982 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
986 m.blendfunc1 = GL_ONE;
987 m.blendfunc2 = GL_ZERO;
989 m.wantoverbright = false;
990 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
992 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
994 R_Mesh_ResizeCheck(mesh->numverts);
995 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
996 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
997 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
998 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1002 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
1004 const surfmesh_t *mesh;
1007 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1008 memset(&m, 0, sizeof(m));
1009 m.blendfunc1 = GL_SRC_ALPHA;
1010 m.blendfunc2 = GL_ONE;
1011 m.wantoverbright = false;
1012 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1014 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1016 R_Mesh_ResizeCheck(mesh->numverts);
1017 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1018 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1019 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
1020 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1024 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
1026 const surfmesh_t *mesh;
1029 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1030 memset(&m, 0, sizeof(m));
1031 m.blendfunc1 = GL_SRC_ALPHA;
1032 m.blendfunc2 = GL_ONE;
1033 m.wantoverbright = false;
1034 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1036 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1038 R_Mesh_ResizeCheck(mesh->numverts);
1039 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1041 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1042 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
1043 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1047 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture)
1049 const msurface_t *surf;
1050 const surfmesh_t *mesh;
1052 int lightmaptexturenum;
1054 memset(&m, 0, sizeof(m));
1055 m.blendfunc1 = GL_ONE;
1056 m.blendfunc2 = GL_ZERO;
1057 //m.wantoverbright = false;
1058 m.tex[0] = R_GetTexture(texture->texture);
1059 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1060 m.tex[2] = R_GetTexture(texture->detailtexture);
1061 m.texrgbscale[0] = 1;
1062 m.texrgbscale[1] = 4;
1063 m.texrgbscale[2] = 2;
1065 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1067 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1068 if (m.tex[1] != lightmaptexturenum)
1070 m.tex[1] = lightmaptexturenum;
1073 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1075 R_Mesh_ResizeCheck(mesh->numverts);
1076 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1077 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1078 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1079 memcpy(varray_texcoord[2], mesh->ab, mesh->numverts * sizeof(float[2]));
1080 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1081 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1082 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1087 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture)
1089 const msurface_t *surf;
1090 const surfmesh_t *mesh;
1092 int lightmaptexturenum;
1094 memset(&m, 0, sizeof(m));
1095 m.blendfunc1 = GL_ONE;
1096 m.blendfunc2 = GL_ZERO;
1097 m.wantoverbright = true;
1098 m.tex[0] = R_GetTexture(texture->texture);
1099 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1101 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1103 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1104 if (m.tex[1] != lightmaptexturenum)
1106 m.tex[1] = lightmaptexturenum;
1109 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1111 R_Mesh_ResizeCheck(mesh->numverts);
1112 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1113 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1114 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1115 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1116 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1117 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1122 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture)
1124 const msurface_t *surf;
1125 const surfmesh_t *mesh;
1127 memset(&m, 0, sizeof(m));
1128 m.blendfunc1 = GL_ONE;
1129 m.blendfunc2 = GL_ZERO;
1130 //m.wantoverbright = false;
1131 m.tex[0] = R_GetTexture(texture->texture);
1133 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1135 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1137 R_Mesh_ResizeCheck(mesh->numverts);
1138 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1139 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1140 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1141 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1146 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture)
1148 const msurface_t *surf;
1149 const surfmesh_t *mesh;
1151 int lightmaptexturenum;
1153 memset(&m, 0, sizeof(m));
1154 m.blendfunc1 = GL_ZERO;
1155 m.blendfunc2 = GL_SRC_COLOR;
1156 m.wantoverbright = true;
1157 m.tex[0] = R_GetTexture(texture->surfacechain->lightmaptexture);
1159 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1161 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1162 if (m.tex[0] != lightmaptexturenum)
1164 m.tex[0] = lightmaptexturenum;
1167 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1169 R_Mesh_ResizeCheck(mesh->numverts);
1170 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1171 memcpy(varray_texcoord[0], mesh->uv, mesh->numverts * sizeof(float[2]));
1172 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1173 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1174 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1179 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture)
1181 const msurface_t *surf;
1182 const surfmesh_t *mesh;
1185 memset(&m, 0, sizeof(m));
1186 m.blendfunc1 = GL_SRC_ALPHA;
1187 m.blendfunc2 = GL_ONE;
1188 m.wantoverbright = true;
1189 m.tex[0] = R_GetTexture(texture->texture);
1191 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1193 if (surf->dlightframe == r_framecount)
1195 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1197 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1199 R_Mesh_ResizeCheck(mesh->numverts);
1200 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1201 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1202 R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
1203 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
1204 RSurf_ScaleColors(varray_color, mesh_colorscale, mesh->numverts);
1205 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1212 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture)
1214 const msurface_t *surf;
1215 const surfmesh_t *mesh;
1218 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1219 memset(&m, 0, sizeof(m));
1220 m.blendfunc1 = GL_SRC_ALPHA;
1221 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1222 //m.wantoverbright = false;
1225 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1227 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1229 R_Mesh_ResizeCheck(mesh->numverts);
1230 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1232 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1233 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, mesh_colorscale, mesh->numverts, modelorg);
1234 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1239 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture)
1241 const msurface_t *surf;
1242 const surfmesh_t *mesh;
1244 memset(&m, 0, sizeof(m));
1245 m.blendfunc1 = GL_DST_COLOR;
1246 m.blendfunc2 = GL_SRC_COLOR;
1247 //m.wantoverbright = false;
1248 m.tex[0] = R_GetTexture(texture->detailtexture);
1250 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1252 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1254 R_Mesh_ResizeCheck(mesh->numverts);
1255 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1256 memcpy(varray_texcoord[0], mesh->ab, mesh->numverts * sizeof(float[2]));
1257 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1258 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1263 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture)
1265 const msurface_t *surf;
1266 const surfmesh_t *mesh;
1268 memset(&m, 0, sizeof(m));
1269 m.blendfunc1 = GL_SRC_ALPHA;
1270 m.blendfunc2 = GL_ONE;
1271 //m.wantoverbright = false;
1272 m.tex[0] = R_GetTexture(texture->glowtexture);
1274 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1276 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1278 R_Mesh_ResizeCheck(mesh->numverts);
1279 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1280 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1281 R_FillColors(varray_color, mesh->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale, 1);
1282 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1287 static void RSurfShader_Wall_Fullbright_Callback(const void *calldata1, int calldata2)
1289 const entity_render_t *ent = calldata1;
1290 const msurface_t *surf = ent->model->surfaces + calldata2;
1291 R_Mesh_Matrix(&ent->matrix);
1292 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1293 if (surf->currenttexture->glowtexture)
1294 RSurfShader_Wall_Pass_Glow(ent, surf);
1296 RSurfShader_Wall_Pass_Fog(ent, surf);
1299 static void RSurfShader_Wall_Fullbright(const entity_render_t *ent, const texture_t *texture)
1301 const msurface_t *surf;
1303 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1305 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1307 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1308 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
1313 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1314 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1315 if (texture->glowtexture)
1316 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1317 RSurfShader_Wall_Pass_Glow(ent, surf);
1319 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1320 RSurfShader_Wall_Pass_Fog(ent, surf);
1324 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1326 const entity_render_t *ent = calldata1;
1327 const msurface_t *surf = ent->model->surfaces + calldata2;
1328 R_Mesh_Matrix(&ent->matrix);
1329 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1330 if (surf->currenttexture->glowtexture)
1331 RSurfShader_Wall_Pass_Glow(ent, surf);
1333 RSurfShader_Wall_Pass_Fog(ent, surf);
1336 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture)
1338 const msurface_t *surf;
1340 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1342 // transparent vertex shaded from lightmap
1343 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1345 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1346 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1349 else if (r_vertexsurfaces.integer)
1351 // opaque vertex shaded from lightmap
1352 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1353 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1354 if (texture->glowtexture)
1355 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1356 RSurfShader_Wall_Pass_Glow(ent, surf);
1358 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1359 RSurfShader_Wall_Pass_Fog(ent, surf);
1363 // opaque lightmapped
1364 if (r_textureunits.integer >= 2)
1366 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1367 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture);
1370 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture);
1371 if (r_detailtextures.integer)
1372 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1377 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture);
1378 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture);
1379 if (r_detailtextures.integer)
1380 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1382 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1383 RSurfShader_OpaqueWall_Pass_Light(ent, texture);
1384 if (texture->glowtexture)
1385 RSurfShader_OpaqueWall_Pass_Glow(ent, texture);
1387 RSurfShader_OpaqueWall_Pass_Fog(ent, texture);
1391 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1392 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, 0};
1393 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1394 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1396 int Cshader_count = 4;
1397 Cshader_t *Cshaders[4] =
1399 &Cshader_wall_lightmap,
1400 &Cshader_wall_fullbright,
1405 void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
1407 int i, alttextures, texframe, framecount, numtextures, numsurfaces, *surfacevisframes;
1408 texture_t *t, *textures;
1410 msurface_t *surf, *surfaces;
1416 // mark the surfaces touched by dynamic lights
1417 if (normal && r_dynamic.integer)
1420 R_Mesh_Matrix(&ent->matrix);
1423 alttextures = ent->frame != 0;
1424 texframe = (int)(cl.time * 5.0f);
1426 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1428 numtextures = model->numtextures;
1429 textures = model->textures;
1430 numsurfaces = model->nummodelsurfaces;
1431 surfaces = model->surfaces + model->firstmodelsurface;
1432 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1434 for (i = 0;i < numtextures;i++)
1435 textures[i].surfacechain = NULL;
1437 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1439 if (surfacevisframes[i] == r_framecount)
1441 #if !WORLDNODECULLBACKFACES
1442 // mark any backface surfaces as not visible
1443 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1445 if (!(surf->flags & SURF_PLANEBACK))
1446 surfacevisframes[i] = -1;
1450 if ((surf->flags & SURF_PLANEBACK))
1451 surfacevisframes[i] = -1;
1453 if (surfacevisframes[i] == r_framecount)
1457 t = surf->texinfo->texture;
1460 framecount = t->anim_total[alttextures];
1461 if (framecount >= 2)
1462 t = t->anim_frames[alttextures][texframe % framecount];
1464 t = t->anim_frames[alttextures][0];
1466 surf->currenttexture = t;
1467 surf->texturechain = t->surfacechain;
1468 t->surfacechain = surf;
1469 if (!r_vertexsurfaces.integer && surf->lightmaptexture != NULL)
1471 if (surf->cached_dlight
1472 || surf->cached_ambient != r_ambient.value
1473 || surf->cached_lightscalebit != lightscalebit)
1474 R_BuildLightMap(ent, surf, false); // base lighting changed
1475 else if (r_dynamic.integer)
1477 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1478 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1479 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1480 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1481 R_BuildLightMap(ent, surf, false); // base lighting changed
1482 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1483 R_BuildLightMap(ent, surf, true); // only dlights
1491 for (i = 0, t = textures;i < numtextures;i++, t++)
1492 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_SKY])
1493 t->shader->shaderfunc[SHADERSTAGE_SKY](ent, t);
1496 for (i = 0, t = textures;i < numtextures;i++, t++)
1497 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_NORMAL])
1498 t->shader->shaderfunc[SHADERSTAGE_NORMAL](ent, t);
1501 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1506 const entity_render_t *ent = calldata1;
1507 const mportal_t *portal = ent->model->portals + calldata2;
1508 memset(&m, 0, sizeof(m));
1509 m.blendfunc1 = GL_SRC_ALPHA;
1510 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1511 //m.wantoverbright = false;
1512 R_Mesh_Matrix(&ent->matrix);
1514 R_Mesh_ResizeCheck(portal->numpoints);
1515 i = portal - ent->model->portals;
1516 R_FillColors(varray_color, portal->numpoints,
1517 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * mesh_colorscale,
1518 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * mesh_colorscale,
1519 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * mesh_colorscale,
1521 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1523 for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
1524 VectorCopy(portal->points[i].position, v);
1527 for (i = 0, v = varray_vertex;i < portal->numpoints;i++, v += 4)
1528 VectorCopy(portal->points[i].position, v);
1529 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1532 static void R_DrawPortals(entity_render_t *ent)
1535 mportal_t *portal, *endportal;
1536 float temp[3], center[3], f;
1538 if (r_drawportals.integer < 1)
1541 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1543 if (portal->here->pvsframe == r_pvsframecount || portal->past->pvsframe == r_pvsframecount)
1545 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1548 for (i = 0;i < portal->numpoints;i++)
1549 VectorAdd(temp, portal->points[i].position, temp);
1550 f = ixtable[portal->numpoints];
1551 VectorScale(temp, f, temp);
1552 Matrix4x4_Transform(&ent->matrix, temp, center);
1553 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1559 void R_DrawBrushModel(entity_render_t *ent, int sky, int normal)
1561 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1564 #if WORLDNODECULLBACKFACES
1568 // because bmodels can be reused, we have to decide which things to render
1569 // from scratch every time
1571 #if WORLDNODECULLBACKFACES
1572 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1574 numsurfaces = model->nummodelsurfaces;
1575 surf = model->surfaces + model->firstmodelsurface;
1576 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1577 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1578 for (i = 0;i < numsurfaces;i++, surf++)
1580 #if WORLDNODECULLBACKFACES
1581 // mark any backface surfaces as not visible
1582 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1584 if ((surf->flags & SURF_PLANEBACK))
1586 surfacevisframes[i] = r_framecount;
1587 surfacepvsframes[i] = r_pvsframecount;
1592 if (!(surf->flags & SURF_PLANEBACK))
1594 surfacevisframes[i] = r_framecount;
1595 surfacepvsframes[i] = r_pvsframecount;
1599 surfacevisframes[i] = r_framecount;
1600 surfacepvsframes[i] = r_pvsframecount;
1602 surf->dlightframe = -1;
1604 R_DrawSurfaces(ent, sky, normal);
1607 void R_SurfaceWorldNode (entity_render_t *ent)
1609 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1610 msurface_t *surfaces, *surf;
1615 numsurfaces = model->nummodelsurfaces;
1616 surfaces = model->surfaces + model->firstmodelsurface;
1617 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1618 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1619 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1621 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1623 if (surfacepvsframes[i] == r_pvsframecount)
1625 #if WORLDNODECULLBACKFACES
1626 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1628 if ((surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1629 surfacevisframes[i] = r_framecount;
1633 if (!(surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1634 surfacevisframes[i] = r_framecount;
1637 if (R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1638 surfacevisframes[i] = r_framecount;
1645 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1648 mportal_t *p, *pstack[8192];
1649 msurface_t *surf, **mark, **endmark;
1651 // LordHavoc: portal-passage worldnode with PVS;
1652 // follows portals leading outward from viewleaf, does not venture
1653 // offscreen or into leafs that are not visible, faster than Quake's
1654 // RecursiveWorldNode
1656 leaf->worldnodeframe = r_framecount;
1660 if (leaf->nummarksurfaces)
1662 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1665 // make sure surfaces are only processed once
1666 if (surf->worldnodeframe != r_framecount)
1668 surf->worldnodeframe = r_framecount;
1669 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1671 if (surf->flags & SURF_PLANEBACK)
1672 surf->visframe = r_framecount;
1676 if (!(surf->flags & SURF_PLANEBACK))
1677 surf->visframe = r_framecount;
1682 // follow portals into other leafs
1683 for (p = leaf->portals;p;p = p->next)
1686 if (leaf->worldnodeframe != r_framecount)
1688 leaf->worldnodeframe = r_framecount;
1689 // FIXME: R_NotCulledBox is absolute, should be done relative
1690 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1692 p->visframe = r_framecount;
1693 pstack[portalstack++] = p;
1696 p = pstack[--portalstack];
1705 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1707 int c, leafstackpos, *mark, *surfacevisframes;
1708 #if WORLDNODECULLBACKFACES
1712 mleaf_t *leaf, *leafstack[8192];
1715 msurface_t *surfaces;
1716 // LordHavoc: portal-passage worldnode with PVS;
1717 // follows portals leading outward from viewleaf, does not venture
1718 // offscreen or into leafs that are not visible, faster than Quake's
1719 // RecursiveWorldNode
1720 surfaces = ent->model->surfaces;
1721 surfacevisframes = ent->model->surfacevisframes;
1722 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1723 viewleaf->worldnodeframe = r_framecount;
1724 leafstack[0] = viewleaf;
1726 while (leafstackpos)
1729 leaf = leafstack[--leafstackpos];
1730 // draw any surfaces bounding this leaf
1731 if (leaf->nummarksurfaces)
1733 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1735 #if WORLDNODECULLBACKFACES
1737 if (surfacevisframes[n] != r_framecount)
1739 surf = surfaces + n;
1740 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1742 if ((surf->flags & SURF_PLANEBACK))
1743 surfacevisframes[n] = r_framecount;
1747 if (!(surf->flags & SURF_PLANEBACK))
1748 surfacevisframes[n] = r_framecount;
1752 surfacevisframes[*mark++] = r_framecount;
1756 // follow portals into other leafs
1757 for (p = leaf->portals;p;p = p->next)
1759 // LordHavoc: this DotProduct hurts less than a cache miss
1760 // (which is more likely to happen if backflowing through leafs)
1761 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1764 if (leaf->worldnodeframe != r_framecount)
1766 leaf->worldnodeframe = r_framecount;
1767 // FIXME: R_NotCulledBox is absolute, should be done relative
1768 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1769 leafstack[leafstackpos++] = leaf;
1774 if (r_drawportals.integer)
1778 void R_PVSUpdate (mleaf_t *viewleaf)
1780 int i, j, l, c, bits, *surfacepvsframes, *mark;
1784 if (r_pvsviewleaf == viewleaf && r_pvsviewleafnovis == r_novis.integer)
1788 r_pvsviewleaf = viewleaf;
1789 r_pvsviewleafnovis = r_novis.integer;
1793 surfacepvsframes = cl.worldmodel->surfacepvsframes;
1794 vis = Mod_LeafPVS (viewleaf, cl.worldmodel);
1795 for (j = 0;j < cl.worldmodel->numleafs;j += 8)
1800 l = cl.worldmodel->numleafs - j;
1803 for (i = 0;i < l;i++)
1805 if (bits & (1 << i))
1807 leaf = &cl.worldmodel->leafs[j + i + 1];
1808 leaf->pvsframe = r_pvsframecount;
1809 // mark surfaces bounding this leaf as visible
1810 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1811 surfacepvsframes[*mark++] = r_pvsframecount;
1824 void R_DrawWorld (entity_render_t *ent)
1827 viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
1828 R_PVSUpdate(viewleaf);
1831 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1832 R_SurfaceWorldNode (ent);
1834 R_PortalWorldNode (ent, viewleaf);
1835 R_DrawSurfaces(ent, true, true);
1843 void R_DrawBrushModelSky (entity_render_t *ent)
1845 R_DrawBrushModel(ent, true, false);
1848 void R_DrawBrushModelNormal (entity_render_t *ent)
1851 R_DrawBrushModel(ent, false, true);
1854 static void gl_surf_start(void)
1858 static void gl_surf_shutdown(void)
1862 static void gl_surf_newmap(void)
1864 // reset pvs visibility variables so it will update on first frame
1865 r_pvsframecount = 1;
1866 r_pvsviewleaf = NULL;
1867 r_pvsviewleafnovis = false;
1870 void GL_Surf_Init(void)
1873 dlightdivtable[0] = 4194304;
1874 for (i = 1;i < 32768;i++)
1875 dlightdivtable[i] = 4194304 / (i << 7);
1877 Cvar_RegisterVariable(&r_ambient);
1878 Cvar_RegisterVariable(&r_vertexsurfaces);
1879 Cvar_RegisterVariable(&r_dlightmap);
1880 Cvar_RegisterVariable(&r_drawportals);
1881 Cvar_RegisterVariable(&r_testvis);
1882 Cvar_RegisterVariable(&r_floatbuildlightmap);
1883 Cvar_RegisterVariable(&r_detailtextures);
1884 Cvar_RegisterVariable(&r_surfaceworldnode);
1886 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);