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"};
39 cvar_t r_cullsurface = {0, "r_cullsurface", "0"};
41 static int dlightdivtable[32768];
43 // variables used by R_PVSUpdate
44 int r_pvsframecount = 0;
45 mleaf_t *r_pvsviewleaf = NULL;
46 int r_pvsviewleafnovis = 0;
47 msurface_t *r_pvsfirstsurface = NULL;
49 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
51 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
53 float dist, impact[3], local[3];
55 // LordHavoc: use 64bit integer... shame it's not very standardized...
56 #if _MSC_VER || __BORLANDC__
64 smax = (surf->extents[0] >> 4) + 1;
65 tmax = (surf->extents[1] >> 4) + 1;
68 for (lnum = 0; lnum < r_numdlights; lnum++)
70 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
71 continue; // not lit by this light
73 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
74 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
76 // for comparisons to minimum acceptable light
77 // compensate for LIGHTOFFSET
78 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
85 if (surf->plane->type < 3)
87 VectorCopy(local, impact);
88 impact[surf->plane->type] -= dist;
92 impact[0] = local[0] - surf->plane->normal[0] * dist;
93 impact[1] = local[1] - surf->plane->normal[1] * dist;
94 impact[2] = local[2] - surf->plane->normal[2] * dist;
97 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
98 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
100 s = bound(0, impacts, smax * 16) - impacts;
101 t = bound(0, impactt, tmax * 16) - impactt;
102 i = s * s + t * t + dist2;
106 // reduce calculations
107 for (s = 0, i = impacts; s < smax; s++, i -= 16)
108 sdtable[s] = i * i + dist2;
110 maxdist3 = maxdist - dist2;
112 // convert to 8.8 blocklights format
113 red = r_dlight[lnum].light[0];
114 green = r_dlight[lnum].light[1];
115 blue = r_dlight[lnum].light[2];
116 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
120 for (t = 0;t < tmax;t++, i -= 16)
123 // make sure some part of it is visible on this line
126 maxdist2 = maxdist - td;
127 for (s = 0;s < smax;s++)
129 if (sdtable[s] < maxdist2)
131 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
134 bl[0] += (red * k) >> 7;
135 bl[1] += (green * k) >> 7;
136 bl[2] += (blue * k) >> 7;
150 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
152 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
153 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
157 smax = (surf->extents[0] >> 4) + 1;
158 tmax = (surf->extents[1] >> 4) + 1;
161 for (lnum = 0; lnum < r_numdlights; lnum++)
163 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
164 continue; // not lit by this light
166 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
167 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
169 // for comparisons to minimum acceptable light
170 // compensate for LIGHTOFFSET
171 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
174 dist2 += LIGHTOFFSET;
175 if (dist2 >= maxdist)
178 if (surf->plane->type < 3)
180 VectorCopy(local, impact);
181 impact[surf->plane->type] -= dist;
185 impact[0] = local[0] - surf->plane->normal[0] * dist;
186 impact[1] = local[1] - surf->plane->normal[1] * dist;
187 impact[2] = local[2] - surf->plane->normal[2] * dist;
190 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
191 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
193 td = bound(0, impacts, smax * 16) - impacts;
194 td1 = bound(0, impactt, tmax * 16) - impactt;
195 td = td * td + td1 * td1 + dist2;
199 // reduce calculations
200 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
201 sdtable[s] = td1 * td1 + dist2;
203 maxdist3 = maxdist - dist2;
205 // convert to 8.8 blocklights format
206 red = r_dlight[lnum].light[0];
207 green = r_dlight[lnum].light[1];
208 blue = r_dlight[lnum].light[2];
209 subtract = r_dlight[lnum].subtract * 32768.0f;
210 bl = floatblocklights;
213 for (t = 0;t < tmax;t++, td1 -= 16.0f)
216 // make sure some part of it is visible on this line
219 maxdist2 = maxdist - td;
220 for (s = 0;s < smax;s++)
222 if (sdtable[s] < maxdist2)
224 k = (32768.0f / (sdtable[s] + td)) - subtract;
244 Combine and scale multiple lightmaps into the 8.8 format in blocklights
247 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int dlightchanged)
249 if (!r_floatbuildlightmap.integer)
251 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
252 unsigned int *bl, scale;
253 qbyte *lightmap, *out, *stain;
255 // update cached lighting info
256 surf->cached_dlight = 0;
257 surf->cached_lightscalebit = lightscalebit;
258 surf->cached_ambient = r_ambient.value;
259 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
260 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
261 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
262 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
264 smax = (surf->extents[0]>>4)+1;
265 tmax = (surf->extents[1]>>4)+1;
268 lightmap = surf->samples;
270 // set to full bright if no light data
272 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
274 for (i = 0;i < size3;i++)
280 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
283 for (i = 0;i < size3;i++)
287 memset(bl, 0, size*3*sizeof(unsigned int));
289 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
291 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
292 if (surf->cached_dlight)
294 else if (dlightchanged)
295 return; // don't upload if only updating dlights and none mattered
298 // add all the lightmaps
302 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
303 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
304 bl[i] += lightmap[i] * scale;
308 stain = surf->stainsamples;
311 // deal with lightmap brightness scale
312 shift = 7 + lightscalebit + 8;
313 if (ent->model->lightmaprgba)
315 stride = (surf->lightmaptexturestride - smax) * 4;
316 for (i = 0;i < tmax;i++, out += stride)
318 for (j = 0;j < smax;j++)
320 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
321 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
322 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
329 stride = (surf->lightmaptexturestride - smax) * 3;
330 for (i = 0;i < tmax;i++, out += stride)
332 for (j = 0;j < smax;j++)
334 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
335 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
336 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
341 R_UpdateTexture(surf->lightmaptexture, templight);
345 int smax, tmax, i, j, size, size3, maps, stride, l;
347 qbyte *lightmap, *out, *stain;
349 // update cached lighting info
350 surf->cached_dlight = 0;
351 surf->cached_lightscalebit = lightscalebit;
352 surf->cached_ambient = r_ambient.value;
353 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
354 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
355 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
356 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
358 smax = (surf->extents[0]>>4)+1;
359 tmax = (surf->extents[1]>>4)+1;
362 lightmap = surf->samples;
364 // set to full bright if no light data
365 bl = floatblocklights;
366 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
369 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
374 for (i = 0;i < size3;i++)
378 memset(bl, 0, size*3*sizeof(float));
380 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
382 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
383 if (surf->cached_dlight)
385 else if (dlightchanged)
386 return; // don't upload if only updating dlights and none mattered
389 // add all the lightmaps
392 bl = floatblocklights;
393 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
394 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
395 bl[i] += lightmap[i] * scale;
398 stain = surf->stainsamples;
399 bl = floatblocklights;
401 // deal with lightmap brightness scale
402 scale = 1.0f / (1 << (7 + lightscalebit + 8));
403 if (ent->model->lightmaprgba)
405 stride = (surf->lightmaptexturestride - smax) * 4;
406 for (i = 0;i < tmax;i++, out += stride)
408 for (j = 0;j < smax;j++)
410 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
412 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
419 stride = (surf->lightmaptexturestride - smax) * 3;
420 for (i = 0;i < tmax;i++, out += stride)
422 for (j = 0;j < smax;j++)
424 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
425 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
426 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
431 R_UpdateTexture(surf->lightmaptexture, templight);
435 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
438 msurface_t *surf, *endsurf;
439 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
442 // LordHavoc: use 64bit integer... shame it's not very standardized...
443 #if _MSC_VER || __BORLANDC__
450 // for comparisons to minimum acceptable light
451 // compensate for 256 offset
452 maxdist = radius * radius + 256.0f;
454 // clamp radius to avoid exceeding 32768 entry division table
455 if (maxdist > 4194304)
458 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
461 if (node->contents < 0)
463 ndist = PlaneDiff(origin, node->plane);
466 node = node->children[0];
471 node = node->children[1];
475 dist2 = ndist * ndist + 256.0f;
478 maxdist3 = maxdist - dist2;
480 if (node->plane->type < 3)
482 VectorCopy(origin, impact);
483 impact[node->plane->type] -= ndist;
487 impact[0] = origin[0] - node->plane->normal[0] * ndist;
488 impact[1] = origin[1] - node->plane->normal[1] * ndist;
489 impact[2] = origin[2] - node->plane->normal[2] * ndist;
492 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
494 if (surf->stainsamples)
496 smax = (surf->extents[0] >> 4) + 1;
497 tmax = (surf->extents[1] >> 4) + 1;
499 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
500 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
502 s = bound(0, impacts, smax * 16) - impacts;
503 t = bound(0, impactt, tmax * 16) - impactt;
504 i = s * s + t * t + dist2;
508 // reduce calculations
509 for (s = 0, i = impacts; s < smax; s++, i -= 16)
510 sdtable[s] = i * i + dist2;
512 // convert to 8.8 blocklights format
513 bl = surf->stainsamples;
518 for (t = 0;t < tmax;t++, i -= 16)
521 // make sure some part of it is visible on this line
524 maxdist2 = maxdist - td;
525 for (s = 0;s < smax;s++)
527 if (sdtable[s] < maxdist2)
529 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
532 ratio = rand() & 255;
533 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
537 a = bound(0, a, 256);
538 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
539 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
540 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
541 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
542 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
543 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
554 // force lightmap upload
556 surf->cached_dlight = true;
561 if (node->children[0]->contents >= 0)
563 if (node->children[1]->contents >= 0)
565 R_StainNode(node->children[0], model, origin, radius, icolor);
566 node = node->children[1];
571 node = node->children[0];
575 else if (node->children[1]->contents >= 0)
577 node = node->children[1];
582 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
585 entity_render_t *ent;
597 model = cl.worldmodel;
599 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
601 // look for embedded bmodels
602 for (n = 0;n < cl_num_brushmodel_entities;n++)
604 ent = cl_brushmodel_entities[n];
606 if (model && model->name[0] == '*')
608 Mod_CheckLoaded(model);
609 if (model->type == mod_brush)
611 Matrix4x4_Transform(&ent->inversematrix, origin, org);
612 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
620 =============================================================
624 =============================================================
627 static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
632 if (styles[0] != 255)
634 for (i = 0;i < numverts;i++, c += 4)
636 lm = samples + lightmapoffsets[i];
637 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
638 VectorMA(c, scale, lm, c);
639 if (styles[1] != 255)
642 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
643 VectorMA(c, scale, lm, c);
644 if (styles[2] != 255)
647 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
648 VectorMA(c, scale, lm, c);
649 if (styles[3] != 255)
652 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
653 VectorMA(c, scale, lm, c);
661 static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
667 for (i = 0;i < numverts;i++, v += 4, c += 4)
669 VectorSubtract(v, modelorg, diff);
670 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
671 VectorScale(c, f, c);
674 else if (colorscale != 1)
675 for (i = 0;i < numverts;i++, c += 4)
676 VectorScale(c, colorscale, c);
679 static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
688 for (i = 0;i < numverts;i++, v += 4, c += 4)
690 VectorSubtract(v, modelorg, diff);
691 f = 1 - exp(fogdensity/DotProduct(diff, diff));
700 for (i = 0;i < numverts;i++, c += 4)
710 static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
717 for (i = 0;i < numverts;i++, v += 4, c += 4)
719 VectorSubtract(v, modelorg, diff);
720 f = exp(fogdensity/DotProduct(diff, diff));
728 static void RSurf_ScaleColors(float *c, float scale, int numverts)
732 for (i = 0;i < numverts;i++, c += 4)
733 VectorScale(c, scale, c);
736 static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
741 int i, l, lit = false;
744 for (l = 0;l < r_numdlights;l++)
746 if (dlightbits[l >> 5] & (1 << (l & 31)))
749 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
750 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
752 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
753 if (f < rd->cullradius2)
755 f = (1.0f / f) - rd->subtract;
756 VectorMA(c, f, rd->light, c);
765 // note: this untransforms lights to do the checking,
766 // and takes surf->mesh->verts data
767 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
773 for (l = 0;l < r_numdlights;l++)
775 if (dlightbits[l >> 5] & (1 << (l & 31)))
778 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
779 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
780 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
787 static void RSurfShader_Sky(const entity_render_t *ent, const msurface_t *firstsurf)
789 const msurface_t *surf;
790 const surfmesh_t *mesh;
793 // LordHavoc: HalfLife maps have freaky skypolys...
794 if (ent->model->ishlbsp)
799 skyrendernow = false;
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;
818 m.matrix = ent->matrix;
819 for (surf = firstsurf;surf;surf = surf->chain)
821 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
823 m.numtriangles = mesh->numtriangles;
824 m.numverts = mesh->numverts;
825 if (R_Mesh_Draw_GetBuffer(&m, false))
827 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
828 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
830 memset(m.color, 0, m.numverts * sizeof(float[4]));
832 R_FillColors(m.color, m.numverts, fogcolor[0] * m.colorscale, fogcolor[1] * m.colorscale, fogcolor[2] * m.colorscale, 1);
839 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
841 const entity_render_t *ent = calldata1;
842 const msurface_t *surf = ent->model->surfaces + calldata2;
844 const surfmesh_t *mesh;
846 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
848 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
850 memset(&m, 0, sizeof(m));
851 if (ent->effects & EF_ADDITIVE)
853 m.blendfunc1 = GL_SRC_ALPHA;
854 m.blendfunc2 = GL_ONE;
856 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
858 m.blendfunc1 = GL_SRC_ALPHA;
859 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
863 m.blendfunc1 = GL_ONE;
864 m.blendfunc2 = GL_ZERO;
866 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
867 m.matrix = ent->matrix;
868 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
870 m.numtriangles = mesh->numtriangles;
871 m.numverts = mesh->numverts;
872 if (R_Mesh_Draw_GetBuffer(&m, true))
874 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
875 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
876 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
877 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
878 R_FillColors(m.color, m.numverts, f, f, f, alpha);
879 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
881 if (surf->dlightframe == r_framecount)
882 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
883 if (surf->flags & SURF_LIGHTMAP)
884 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, m.color, m.numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
886 RSurf_FogColors(m.vertex, m.color, m.colorscale, m.numverts, modelorg);
893 memset(&m, 0, sizeof(m));
894 m.blendfunc1 = GL_SRC_ALPHA;
895 m.blendfunc2 = GL_ONE;
896 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
897 m.matrix = ent->matrix;
898 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
900 m.numtriangles = mesh->numtriangles;
901 m.numverts = mesh->numverts;
902 if (R_Mesh_Draw_GetBuffer(&m, false))
904 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
905 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
907 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
908 RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, m.colorscale, m.numverts, modelorg);
915 static void RSurfShader_Water(const entity_render_t *ent, const msurface_t *firstsurf)
917 const msurface_t *surf;
919 for (surf = firstsurf;surf;surf = surf->chain)
921 if ((r_wateralpha.value < 1 && !(surf->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || surf->currenttexture->fogtexture)
923 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
924 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
927 R_MeshQueue_Add(RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
931 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
934 const surfmesh_t *mesh;
937 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
938 memset(&m, 0, sizeof(m));
939 if (ent->effects & EF_ADDITIVE)
941 m.blendfunc1 = GL_SRC_ALPHA;
942 m.blendfunc2 = GL_ONE;
944 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
946 m.blendfunc1 = GL_SRC_ALPHA;
947 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
951 m.blendfunc1 = GL_ONE;
952 m.blendfunc2 = GL_ZERO;
954 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
955 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
956 m.matrix = ent->matrix;
957 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
959 m.numtriangles = mesh->numtriangles;
960 m.numverts = mesh->numverts;
961 if (R_Mesh_Draw_GetBuffer(&m, true))
963 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
964 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
965 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
966 R_FillColors(m.color, m.numverts, base, base, base, ent->alpha);
967 if (!(ent->effects & EF_FULLBRIGHT))
969 if (surf->dlightframe == r_framecount)
970 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
971 if (surf->flags & SURF_LIGHTMAP)
972 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, m.color, m.numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
974 RSurf_FogColors(m.vertex, m.color, m.colorscale, m.numverts, modelorg);
980 static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, const msurface_t *surf)
982 const surfmesh_t *mesh;
985 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
986 memset(&m, 0, sizeof(m));
987 if (ent->effects & EF_ADDITIVE)
989 m.blendfunc1 = GL_SRC_ALPHA;
990 m.blendfunc2 = GL_ONE;
992 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
994 m.blendfunc1 = GL_SRC_ALPHA;
995 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
999 m.blendfunc1 = GL_ONE;
1000 m.blendfunc2 = GL_ZERO;
1002 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1003 m.matrix = ent->matrix;
1004 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1006 m.numtriangles = mesh->numtriangles;
1007 m.numverts = mesh->numverts;
1008 if (R_Mesh_Draw_GetBuffer(&m, false))
1010 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1011 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1012 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1013 RSurf_FoggedColors(m.vertex, m.color, 1, 1, 1, ent->alpha, m.colorscale, m.numverts, modelorg);
1019 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
1021 const surfmesh_t *mesh;
1022 rmeshbufferinfo_t m;
1024 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1025 memset(&m, 0, sizeof(m));
1026 m.blendfunc1 = GL_SRC_ALPHA;
1027 m.blendfunc2 = GL_ONE;
1028 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1029 m.matrix = ent->matrix;
1030 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1032 m.numtriangles = mesh->numtriangles;
1033 m.numverts = mesh->numverts;
1034 if (R_Mesh_Draw_GetBuffer(&m, false))
1036 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1037 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1038 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1039 RSurf_FoggedColors(m.vertex, m.color, 1, 1, 1, ent->alpha, m.colorscale, m.numverts, modelorg);
1045 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
1047 const surfmesh_t *mesh;
1048 rmeshbufferinfo_t m;
1050 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1051 memset(&m, 0, sizeof(m));
1052 m.blendfunc1 = GL_SRC_ALPHA;
1053 m.blendfunc2 = GL_ONE;
1054 m.matrix = ent->matrix;
1055 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1056 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1058 m.numtriangles = mesh->numtriangles;
1059 m.numverts = mesh->numverts;
1060 if (R_Mesh_Draw_GetBuffer(&m, false))
1062 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1063 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1065 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1066 RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, m.colorscale, m.numverts, modelorg);
1072 static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(const entity_render_t *ent, const msurface_t *surf)
1074 const surfmesh_t *mesh;
1075 static rmeshbufferinfo_t m;
1077 //memset(&m, 0, sizeof(m));
1078 m.blendfunc1 = GL_ONE;
1079 m.blendfunc2 = GL_ZERO;
1080 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1081 m.texrgbscale[0] = 1.0f;
1082 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1083 m.texrgbscale[1] = 4.0f;
1084 m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
1085 m.texrgbscale[2] = 2.0f;
1086 m.matrix = ent->matrix;
1087 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1089 m.numtriangles = mesh->numtriangles;
1090 m.numverts = mesh->numverts;
1091 if (R_Mesh_Draw_GetBuffer(&m, false))
1093 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1094 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1095 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1096 memcpy(m.texcoords[1], mesh->uv, m.numverts * sizeof(float[2]));
1097 memcpy(m.texcoords[2], mesh->ab, m.numverts * sizeof(float[2]));
1098 cl = (float) (1 << lightscalebit) * m.colorscale;
1099 R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
1105 static void RSurfShader_OpaqueWall_Pass_BaseMTex(const entity_render_t *ent, const msurface_t *surf)
1107 const surfmesh_t *mesh;
1108 rmeshbufferinfo_t m;
1110 memset(&m, 0, sizeof(m));
1111 m.blendfunc1 = GL_ONE;
1112 m.blendfunc2 = GL_ZERO;
1113 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1114 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1115 m.matrix = ent->matrix;
1116 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1118 m.numtriangles = mesh->numtriangles;
1119 m.numverts = mesh->numverts;
1120 if (R_Mesh_Draw_GetBuffer(&m, true))
1122 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1123 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1124 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1125 memcpy(m.texcoords[1], mesh->uv, m.numverts * sizeof(float[2]));
1126 cl = (float) (1 << lightscalebit) * m.colorscale;
1127 R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
1133 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const msurface_t *surf)
1135 const surfmesh_t *mesh;
1136 rmeshbufferinfo_t m;
1138 memset(&m, 0, sizeof(m));
1139 m.blendfunc1 = GL_ONE;
1140 m.blendfunc2 = GL_ZERO;
1141 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1142 m.matrix = ent->matrix;
1143 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1145 m.numtriangles = mesh->numtriangles;
1146 m.numverts = mesh->numverts;
1147 if (R_Mesh_Draw_GetBuffer(&m, false))
1149 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1150 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1151 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1153 R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
1159 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const msurface_t *surf)
1161 const surfmesh_t *mesh;
1162 rmeshbufferinfo_t m;
1164 memset(&m, 0, sizeof(m));
1165 m.blendfunc1 = GL_ZERO;
1166 m.blendfunc2 = GL_SRC_COLOR;
1167 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1168 m.matrix = ent->matrix;
1169 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1171 m.numtriangles = mesh->numtriangles;
1172 m.numverts = mesh->numverts;
1173 if (R_Mesh_Draw_GetBuffer(&m, true))
1175 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1176 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1177 memcpy(m.texcoords[0], mesh->uv, m.numverts * sizeof(float[2]));
1178 cl = (float) (1 << lightscalebit) * m.colorscale;
1179 R_FillColors(m.color, m.numverts, cl, cl, cl, 1);
1185 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const msurface_t *surf)
1187 const surfmesh_t *mesh;
1188 rmeshbufferinfo_t m;
1190 if (surf->dlightframe != r_framecount)
1192 if (ent->effects & EF_FULLBRIGHT)
1195 memset(&m, 0, sizeof(m));
1196 m.blendfunc1 = GL_SRC_ALPHA;
1197 m.blendfunc2 = GL_ONE;
1198 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1199 m.matrix = ent->matrix;
1200 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1202 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1204 m.numtriangles = mesh->numtriangles;
1205 m.numverts = mesh->numverts;
1206 if (R_Mesh_Draw_GetBuffer(&m, true))
1208 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1209 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1210 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1211 R_FillColors(m.color, m.numverts, 0, 0, 0, 1);
1212 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, m.numverts, m.vertex, m.color);
1213 RSurf_ScaleColors(m.color, m.colorscale, m.numverts);
1220 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
1222 const surfmesh_t *mesh;
1223 rmeshbufferinfo_t m;
1225 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1226 memset(&m, 0, sizeof(m));
1227 m.blendfunc1 = GL_SRC_ALPHA;
1228 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1229 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1230 m.matrix = ent->matrix;
1231 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1233 m.numtriangles = mesh->numtriangles;
1234 m.numverts = mesh->numverts;
1235 if (R_Mesh_Draw_GetBuffer(&m, false))
1237 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1238 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1240 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1241 RSurf_FogPassColors(m.vertex, m.color, fogcolor[0], fogcolor[1], fogcolor[2], 1, m.colorscale, m.numverts, modelorg);
1247 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const msurface_t *surf)
1249 const surfmesh_t *mesh;
1250 rmeshbufferinfo_t m;
1251 memset(&m, 0, sizeof(m));
1252 m.blendfunc1 = GL_DST_COLOR;
1253 m.blendfunc2 = GL_SRC_COLOR;
1254 m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
1255 m.matrix = ent->matrix;
1256 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1258 m.numtriangles = mesh->numtriangles;
1259 m.numverts = mesh->numverts;
1260 if (R_Mesh_Draw_GetBuffer(&m, false))
1262 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1263 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1264 memcpy(m.texcoords[0], mesh->ab, m.numverts * sizeof(float[2]));
1265 R_FillColors(m.color, m.numverts, 1, 1, 1, 1);
1271 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
1273 const surfmesh_t *mesh;
1274 rmeshbufferinfo_t m;
1275 memset(&m, 0, sizeof(m));
1276 m.blendfunc1 = GL_SRC_ALPHA;
1277 m.blendfunc2 = GL_ONE;
1278 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1279 m.matrix = ent->matrix;
1280 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1282 m.numtriangles = mesh->numtriangles;
1283 m.numverts = mesh->numverts;
1284 if (R_Mesh_Draw_GetBuffer(&m, false))
1286 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1287 memcpy(m.vertex, mesh->verts, m.numverts * sizeof(float[4]));
1288 memcpy(m.texcoords[0], mesh->st, m.numverts * sizeof(float[2]));
1289 R_FillColors(m.color, m.numverts, m.colorscale, m.colorscale, m.colorscale, 1);
1295 static void RSurfShader_Wall_Fullbright_Callback(const void *calldata1, int calldata2)
1297 const entity_render_t *ent = calldata1;
1298 const msurface_t *surf = ent->model->surfaces + calldata2;
1299 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1300 if (surf->currenttexture->glowtexture)
1301 RSurfShader_Wall_Pass_Glow(ent, surf);
1303 RSurfShader_Wall_Pass_Fog(ent, surf);
1306 static void RSurfShader_Wall_Fullbright(const entity_render_t *ent, const msurface_t *firstsurf)
1308 const msurface_t *surf;
1310 if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
1312 for (surf = firstsurf;surf;surf = surf->chain)
1314 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1315 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
1320 for (surf = firstsurf;surf;surf = surf->chain)
1322 if (surf->currenttexture->fogtexture != NULL)
1324 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1325 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
1328 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1330 for (surf = firstsurf;surf;surf = surf->chain)
1331 if (surf->currenttexture->glowtexture)
1332 if (surf->currenttexture->fogtexture == NULL)
1333 RSurfShader_Wall_Pass_Glow(ent, surf);
1335 for (surf = firstsurf;surf;surf = surf->chain)
1336 if (surf->currenttexture->fogtexture == NULL)
1337 RSurfShader_Wall_Pass_Fog(ent, surf);
1341 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1343 const entity_render_t *ent = calldata1;
1344 const msurface_t *surf = ent->model->surfaces + calldata2;
1345 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1346 if (surf->currenttexture->glowtexture)
1347 RSurfShader_Wall_Pass_Glow(ent, surf);
1349 RSurfShader_Wall_Pass_Fog(ent, surf);
1352 static void RSurfShader_Wall_Vertex(const entity_render_t *ent, const msurface_t *firstsurf)
1354 const msurface_t *surf;
1356 if (ent->effects & EF_ADDITIVE || ent->alpha < 1)
1358 for (surf = firstsurf;surf;surf = surf->chain)
1360 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1361 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1366 for (surf = firstsurf;surf;surf = surf->chain)
1368 if (surf->currenttexture->fogtexture != NULL)
1370 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1371 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1374 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1376 for (surf = firstsurf;surf;surf = surf->chain)
1377 if (surf->currenttexture->glowtexture)
1378 if (surf->currenttexture->fogtexture == NULL)
1379 RSurfShader_Wall_Pass_Glow(ent, surf);
1381 for (surf = firstsurf;surf;surf = surf->chain)
1382 if (surf->currenttexture->fogtexture == NULL)
1383 RSurfShader_Wall_Pass_Fog(ent, surf);
1387 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const msurface_t *firstsurf)
1389 const msurface_t *surf;
1391 if (ent->alpha < 1 || ent->effects & EF_ADDITIVE)
1393 for (surf = firstsurf;surf;surf = surf->chain)
1395 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1396 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1399 else if (r_vertexsurfaces.integer || ent->alpha < 1 || ent->effects & EF_ADDITIVE)
1401 for (surf = firstsurf;surf;surf = surf->chain)
1403 if (surf->currenttexture->fogtexture != NULL)
1405 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1406 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1409 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1411 for (surf = firstsurf;surf;surf = surf->chain)
1412 if (surf->currenttexture->glowtexture)
1413 if (surf->currenttexture->fogtexture == NULL)
1414 RSurfShader_Wall_Pass_Glow(ent, surf);
1416 for (surf = firstsurf;surf;surf = surf->chain)
1417 if (surf->currenttexture->fogtexture == NULL)
1418 RSurfShader_Wall_Pass_Fog(ent, surf);
1422 if (r_textureunits.integer >= 2)
1424 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1426 for (surf = firstsurf;surf;surf = surf->chain)
1428 if (surf->currenttexture->fogtexture != NULL)
1430 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1431 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1434 RSurfShader_OpaqueWall_Pass_TripleTexCombine(ent, surf);
1439 for (surf = firstsurf;surf;surf = surf->chain)
1441 if (surf->currenttexture->fogtexture != NULL)
1443 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1444 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1447 RSurfShader_OpaqueWall_Pass_BaseMTex(ent, surf);
1449 if (r_detailtextures.integer)
1450 for (surf = firstsurf;surf;surf = surf->chain)
1451 if (surf->currenttexture->fogtexture == NULL)
1452 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1457 for (surf = firstsurf;surf;surf = surf->chain)
1459 if (surf->currenttexture->fogtexture != NULL)
1461 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1462 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1465 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, surf);
1467 for (surf = firstsurf;surf;surf = surf->chain)
1468 if (surf->currenttexture->fogtexture == NULL)
1469 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, surf);
1470 if (r_detailtextures.integer)
1471 for (surf = firstsurf;surf;surf = surf->chain)
1472 if (surf->currenttexture->fogtexture == NULL)
1473 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, surf);
1475 if (!r_dlightmap.integer)
1476 for (surf = firstsurf;surf;surf = surf->chain)
1477 if (surf->dlightframe == r_framecount)
1478 if (surf->currenttexture->fogtexture == NULL)
1479 RSurfShader_OpaqueWall_Pass_Light(ent, surf);
1480 for (surf = firstsurf;surf;surf = surf->chain)
1481 if (surf->currenttexture->glowtexture)
1482 if (surf->currenttexture->fogtexture == NULL)
1483 RSurfShader_OpaqueWall_Pass_Glow(ent, surf);
1485 for (surf = firstsurf;surf;surf = surf->chain)
1486 if (surf->currenttexture->fogtexture == NULL)
1487 RSurfShader_OpaqueWall_Pass_Fog(ent, surf);
1491 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1492 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1493 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1494 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1495 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1497 int Cshader_count = 5;
1498 Cshader_t *Cshaders[5] =
1500 &Cshader_wall_vertex,
1501 &Cshader_wall_lightmap,
1502 &Cshader_wall_fullbright,
1507 void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
1509 int i, alttextures, texframe, framecount;
1519 for (i = 0;i < Cshader_count;i++)
1520 Cshaders[i]->chain = NULL;
1523 alttextures = ent->frame != 0;
1524 texframe = (int)(cl.time * 5.0f);
1526 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1527 for (i = 0;i < model->nummodelsurfaces;i++)
1529 surf = model->modelsortedsurfaces[i];
1530 if (surf->visframe == r_framecount)
1532 // mark any backface surfaces as not visible
1533 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1535 if (!(surf->flags & SURF_PLANEBACK))
1536 surf->visframe = -1;
1540 if (surf->flags & SURF_PLANEBACK)
1541 surf->visframe = -1;
1543 if (surf->visframe == r_framecount)
1545 if (r_cullsurface.integer && R_CullBox (surf->poly_mins, surf->poly_maxs))
1546 surf->visframe = -1;
1550 t = surf->texinfo->texture;
1553 framecount = t->anim_total[alttextures];
1554 if (framecount >= 2)
1555 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1557 surf->currenttexture = t->anim_frames[alttextures][0];
1560 surf->currenttexture = t;
1561 surf->chain = surf->shader->chain;
1562 surf->shader->chain = surf;
1570 for (i = 0;i < Cshader_count;i++)
1572 shader = Cshaders[i];
1573 if (shader->chain && shader->shaderfunc[SHADERSTAGE_SKY])
1574 shader->shaderfunc[SHADERSTAGE_SKY](ent, shader->chain);
1580 if (r_dynamic.integer)
1583 if (!r_vertexsurfaces.integer)
1585 for (i = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;i < ent->model->nummodelsurfaces;i++, surf++)
1587 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
1589 if (surf->cached_dlight
1590 || surf->cached_ambient != r_ambient.value
1591 || surf->cached_lightscalebit != lightscalebit)
1592 R_BuildLightMap(ent, surf, false); // base lighting changed
1593 else if (r_dynamic.integer)
1595 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1596 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1597 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1598 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1599 R_BuildLightMap(ent, surf, false); // base lighting changed
1600 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1601 R_BuildLightMap(ent, surf, true); // only dlights
1607 for (i = 0;i < Cshader_count;i++)
1609 shader = Cshaders[i];
1610 if (shader->chain && shader->shaderfunc[SHADERSTAGE_NORMAL])
1611 shader->shaderfunc[SHADERSTAGE_NORMAL](ent, shader->chain);
1617 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1621 rmeshbufferinfo_t m;
1622 const entity_render_t *ent = calldata1;
1623 const mportal_t *portal = ent->model->portals + calldata2;
1624 memset(&m, 0, sizeof(m));
1625 m.blendfunc1 = GL_SRC_ALPHA;
1626 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1627 m.numverts = portal->numpoints;
1628 m.numtriangles = portal->numpoints - 2;
1629 m.matrix = ent->matrix;
1630 if (R_Mesh_Draw_GetBuffer(&m, false))
1632 for (i = 0;i < m.numtriangles;i++)
1634 m.index[i * 3 + 0] = 0;
1635 m.index[i * 3 + 1] = i + 1;
1636 m.index[i * 3 + 2] = i + 2;
1638 i = portal - ent->model->portals;
1639 R_FillColors(m.color, m.numverts,
1640 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * m.colorscale,
1641 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * m.colorscale,
1642 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * m.colorscale,
1644 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1646 for (i = portal->numpoints - 1, v = m.vertex;i >= 0;i--, v += 4)
1647 VectorCopy(portal->points[i].position, v);
1650 for (i = 0, v = m.vertex;i < portal->numpoints;i++, v += 4)
1651 VectorCopy(portal->points[i].position, v);
1656 static void R_DrawPortals(entity_render_t *ent)
1659 mportal_t *portal, *endportal;
1660 float temp[3], center[3], f;
1662 if (r_drawportals.integer < 1)
1665 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1667 if (portal->here->visframe == r_framecount || portal->past->visframe == r_framecount)
1670 for (i = 0;i < portal->numpoints;i++)
1671 VectorAdd(temp, portal->points[i].position, temp);
1672 f = ixtable[portal->numpoints];
1673 VectorScale(temp, f, temp);
1674 Matrix4x4_Transform(&ent->matrix, temp, center);
1675 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1681 void R_DrawBrushModel(entity_render_t *ent, int sky, int normal)
1688 // because bmodels can be reused, we have to decide which things to render
1689 // from scratch every time
1691 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1692 for (i = 0;i < model->nummodelsurfaces;i++)
1694 surf = model->surfaces + model->firstmodelsurface + i;
1695 surf->visframe = r_framecount;
1696 surf->pvsframe = -1;
1697 surf->worldnodeframe = -1;
1698 surf->lightframe = -1;
1699 surf->dlightframe = -1;
1701 R_DrawSurfaces(ent, sky, normal);
1704 void R_SurfaceWorldNode (void)
1707 for (surf = r_pvsfirstsurface;surf;surf = surf->pvschain)
1708 surf->visframe = r_framecount;
1712 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1715 mportal_t *p, *pstack[8192];
1716 msurface_t *surf, **mark, **endmark;
1718 // LordHavoc: portal-passage worldnode with PVS;
1719 // follows portals leading outward from viewleaf, does not venture
1720 // offscreen or into leafs that are not visible, faster than Quake's
1721 // RecursiveWorldNode
1723 leaf->worldnodeframe = r_framecount;
1727 if (leaf->nummarksurfaces)
1729 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1732 // make sure surfaces are only processed once
1733 if (surf->worldnodeframe != r_framecount)
1735 surf->worldnodeframe = r_framecount;
1736 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1738 if (surf->flags & SURF_PLANEBACK)
1739 surf->visframe = r_framecount;
1743 if (!(surf->flags & SURF_PLANEBACK))
1744 surf->visframe = r_framecount;
1749 // follow portals into other leafs
1750 for (p = leaf->portals;p;p = p->next)
1753 if (leaf->worldnodeframe != r_framecount)
1755 leaf->worldnodeframe = r_framecount;
1756 // FIXME: R_NotCulledBox is absolute, should be done relative
1757 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1759 p->visframe = r_framecount;
1760 pstack[portalstack++] = p;
1763 p = pstack[--portalstack];
1772 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1774 int c, leafstackpos;
1775 mleaf_t *leaf, *leafstack[8192];
1779 // LordHavoc: portal-passage worldnode with PVS;
1780 // follows portals leading outward from viewleaf, does not venture
1781 // offscreen or into leafs that are not visible, faster than Quake's
1782 // RecursiveWorldNode
1783 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1784 viewleaf->worldnodeframe = r_framecount;
1785 leafstack[0] = viewleaf;
1787 while (leafstackpos)
1790 leaf = leafstack[--leafstackpos];
1791 // only useful for drawing portals
1792 //leaf->visframe = r_framecount;
1793 // draw any surfaces bounding this leaf
1794 if (leaf->nummarksurfaces)
1795 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1796 (*mark++)->visframe = r_framecount;
1797 // follow portals into other leafs
1798 for (p = leaf->portals;p;p = p->next)
1801 if (leaf->worldnodeframe != r_framecount)
1803 leaf->worldnodeframe = r_framecount;
1804 // FIXME: R_NotCulledBox is absolute, should be done relative
1805 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1806 leafstack[leafstackpos++] = leaf;
1810 //if (r_drawportals.integer)
1811 // R_DrawPortals(ent);
1815 void R_PVSUpdate (mleaf_t *viewleaf)
1817 int i, j, l, c, bits;
1820 msurface_t **mark, *surf;
1822 if (r_pvsviewleaf == viewleaf && r_pvsviewleafnovis == r_novis.integer)
1826 r_pvsviewleaf = viewleaf;
1827 r_pvsviewleafnovis = r_novis.integer;
1831 vis = Mod_LeafPVS (viewleaf, cl.worldmodel);
1832 for (j = 0;j < cl.worldmodel->numleafs;j += 8)
1837 l = cl.worldmodel->numleafs - j;
1840 for (i = 0;i < l;i++)
1842 if (bits & (1 << i))
1844 leaf = &cl.worldmodel->leafs[j + i + 1];
1845 leaf->pvsframe = r_pvsframecount;
1846 // mark surfaces bounding this leaf as visible
1847 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1848 (*mark++)->pvsframe = r_pvsframecount;
1853 // build pvs surfacechain
1854 r_pvsfirstsurface = NULL;
1855 mark = &r_pvsfirstsurface;
1856 for (c = cl.worldmodel->nummodelsurfaces, surf = cl.worldmodel->surfaces + cl.worldmodel->firstmodelsurface;c;c--, surf++)
1858 if (surf->pvsframe == r_pvsframecount)
1861 mark = &surf->pvschain;
1873 void R_DrawWorld (entity_render_t *ent)
1876 viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
1877 R_PVSUpdate(viewleaf);
1880 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1881 R_SurfaceWorldNode ();
1883 R_PortalWorldNode (ent, viewleaf);
1884 R_DrawSurfaces(ent, true, true);
1892 void R_DrawBrushModelSky (entity_render_t *ent)
1894 R_DrawBrushModel(ent, true, false);
1897 void R_DrawBrushModelNormal (entity_render_t *ent)
1900 R_DrawBrushModel(ent, false, true);
1903 static void gl_surf_start(void)
1907 static void gl_surf_shutdown(void)
1911 static void gl_surf_newmap(void)
1913 // reset pvs visibility variables so it will update on first frame
1914 r_pvsframecount = 1;
1915 r_pvsviewleaf = NULL;
1916 r_pvsviewleafnovis = false;
1917 r_pvsfirstsurface = NULL;
1920 void GL_Surf_Init(void)
1923 dlightdivtable[0] = 4194304;
1924 for (i = 1;i < 32768;i++)
1925 dlightdivtable[i] = 4194304 / (i << 7);
1927 Cvar_RegisterVariable(&r_ambient);
1928 Cvar_RegisterVariable(&r_vertexsurfaces);
1929 Cvar_RegisterVariable(&r_dlightmap);
1930 //Cvar_RegisterVariable(&r_drawportals);
1931 Cvar_RegisterVariable(&r_testvis);
1932 Cvar_RegisterVariable(&r_floatbuildlightmap);
1933 Cvar_RegisterVariable(&r_detailtextures);
1934 Cvar_RegisterVariable(&r_surfaceworldnode);
1935 Cvar_RegisterVariable(&r_cullsurface);
1937 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);