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, mesh->numtriangles);
825 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
826 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
828 memset(varray_color, 0, mesh->numverts * sizeof(float[4]));
830 R_FillColors(varray_color, mesh->numverts, fogcolor[0] * mesh_colorscale, fogcolor[1] * mesh_colorscale, fogcolor[2] * mesh_colorscale, 1);
831 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
836 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
838 const entity_render_t *ent = calldata1;
839 const msurface_t *surf = ent->model->surfaces + calldata2;
841 const surfmesh_t *mesh;
843 float alpha = ent->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
845 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
847 R_Mesh_Matrix(&ent->matrix);
849 memset(&m, 0, sizeof(m));
850 if (ent->effects & EF_ADDITIVE)
852 m.blendfunc1 = GL_SRC_ALPHA;
853 m.blendfunc2 = GL_ONE;
855 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
857 m.blendfunc1 = GL_SRC_ALPHA;
858 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
862 m.blendfunc1 = GL_ONE;
863 m.blendfunc2 = GL_ZERO;
865 m.wantoverbright = true;
866 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
868 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
870 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
871 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
872 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
873 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
874 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
875 R_FillColors(varray_color, mesh->numverts, f, f, f, alpha);
876 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
878 if (surf->dlightframe == r_framecount)
879 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
880 if (surf->flags & SURF_LIGHTMAP)
881 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
883 RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
884 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
889 memset(&m, 0, sizeof(m));
890 m.blendfunc1 = GL_SRC_ALPHA;
891 m.blendfunc2 = GL_ONE;
892 m.wantoverbright = false;
893 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
895 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
897 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
898 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
899 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
901 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
902 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, mesh_colorscale, mesh->numverts, modelorg);
903 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
908 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture)
910 const msurface_t *surf;
912 if ((r_wateralpha.value < 1 && !(texture->flags & SURF_DRAWNOALPHA)) || ent->effects & EF_ADDITIVE || texture->fogtexture)
914 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
916 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
917 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
921 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
922 RSurfShader_Water_Callback(ent, surf - ent->model->surfaces);
925 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf)
928 const surfmesh_t *mesh;
931 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
932 memset(&m, 0, sizeof(m));
933 if (ent->effects & EF_ADDITIVE)
935 m.blendfunc1 = GL_SRC_ALPHA;
936 m.blendfunc2 = GL_ONE;
938 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
940 m.blendfunc1 = GL_SRC_ALPHA;
941 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
945 m.blendfunc1 = GL_ONE;
946 m.blendfunc2 = GL_ZERO;
948 m.wantoverbright = true;
949 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
950 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
952 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
954 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
955 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
956 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
957 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
958 R_FillColors(varray_color, mesh->numverts, base, base, base, ent->alpha);
959 if (!(ent->effects & EF_FULLBRIGHT))
961 if (surf->dlightframe == r_framecount)
962 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
963 if (surf->flags & SURF_LIGHTMAP)
964 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
966 RSurf_FogColors(varray_vertex, varray_color, mesh_colorscale, mesh->numverts, modelorg);
967 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
971 static void RSurfShader_Wall_Pass_BaseFullbright(const entity_render_t *ent, const msurface_t *surf)
973 const surfmesh_t *mesh;
976 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
977 memset(&m, 0, sizeof(m));
978 if (ent->effects & EF_ADDITIVE)
980 m.blendfunc1 = GL_SRC_ALPHA;
981 m.blendfunc2 = GL_ONE;
983 else if (surf->currenttexture->fogtexture != NULL || ent->alpha < 1)
985 m.blendfunc1 = GL_SRC_ALPHA;
986 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
990 m.blendfunc1 = GL_ONE;
991 m.blendfunc2 = GL_ZERO;
993 m.wantoverbright = false;
994 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
996 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
998 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
999 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1000 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1001 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1002 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
1003 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1007 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf)
1009 const surfmesh_t *mesh;
1012 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1013 memset(&m, 0, sizeof(m));
1014 m.blendfunc1 = GL_SRC_ALPHA;
1015 m.blendfunc2 = GL_ONE;
1016 m.wantoverbright = false;
1017 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1019 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1021 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1022 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1023 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1024 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1025 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
1026 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1030 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf)
1032 const surfmesh_t *mesh;
1035 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1036 memset(&m, 0, sizeof(m));
1037 m.blendfunc1 = GL_SRC_ALPHA;
1038 m.blendfunc2 = GL_ONE;
1039 m.wantoverbright = false;
1040 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1042 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1044 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1045 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1046 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1048 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1049 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], ent->alpha, mesh_colorscale, mesh->numverts, modelorg);
1050 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1054 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture)
1056 const msurface_t *surf;
1057 const surfmesh_t *mesh;
1059 int lightmaptexturenum;
1061 memset(&m, 0, sizeof(m));
1062 m.blendfunc1 = GL_ONE;
1063 m.blendfunc2 = GL_ZERO;
1064 //m.wantoverbright = false;
1065 m.tex[0] = R_GetTexture(texture->texture);
1066 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1067 m.tex[2] = R_GetTexture(texture->detailtexture);
1068 m.texrgbscale[0] = 1;
1069 m.texrgbscale[1] = 4;
1070 m.texrgbscale[2] = 2;
1072 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1074 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1075 if (m.tex[1] != lightmaptexturenum)
1077 m.tex[1] = lightmaptexturenum;
1080 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1082 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1083 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1084 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1085 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1086 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1087 memcpy(varray_texcoord[2], mesh->ab, mesh->numverts * sizeof(float[2]));
1088 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1089 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1090 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1095 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture)
1097 const msurface_t *surf;
1098 const surfmesh_t *mesh;
1100 int lightmaptexturenum;
1102 memset(&m, 0, sizeof(m));
1103 m.blendfunc1 = GL_ONE;
1104 m.blendfunc2 = GL_ZERO;
1105 m.wantoverbright = true;
1106 m.tex[0] = R_GetTexture(texture->texture);
1107 m.tex[1] = R_GetTexture(texture->surfacechain->lightmaptexture);
1109 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1111 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1112 if (m.tex[1] != lightmaptexturenum)
1114 m.tex[1] = lightmaptexturenum;
1117 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1119 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1120 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1121 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1122 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1123 memcpy(varray_texcoord[1], mesh->uv, mesh->numverts * sizeof(float[2]));
1124 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1125 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1126 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1131 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture)
1133 const msurface_t *surf;
1134 const surfmesh_t *mesh;
1136 memset(&m, 0, sizeof(m));
1137 m.blendfunc1 = GL_ONE;
1138 m.blendfunc2 = GL_ZERO;
1139 //m.wantoverbright = false;
1140 m.tex[0] = R_GetTexture(texture->texture);
1142 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1144 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1146 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1147 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1148 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1149 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1150 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1151 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1156 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture)
1158 const msurface_t *surf;
1159 const surfmesh_t *mesh;
1161 int lightmaptexturenum;
1163 memset(&m, 0, sizeof(m));
1164 m.blendfunc1 = GL_ZERO;
1165 m.blendfunc2 = GL_SRC_COLOR;
1166 m.wantoverbright = true;
1167 m.tex[0] = R_GetTexture(texture->surfacechain->lightmaptexture);
1169 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1171 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1172 if (m.tex[0] != lightmaptexturenum)
1174 m.tex[0] = lightmaptexturenum;
1177 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1179 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1180 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1181 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1182 memcpy(varray_texcoord[0], mesh->uv, mesh->numverts * sizeof(float[2]));
1183 cl = (float) (1 << lightscalebit) * mesh_colorscale;
1184 R_FillColors(varray_color, mesh->numverts, cl, cl, cl, 1);
1185 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1190 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture)
1192 const msurface_t *surf;
1193 const surfmesh_t *mesh;
1196 memset(&m, 0, sizeof(m));
1197 m.blendfunc1 = GL_SRC_ALPHA;
1198 m.blendfunc2 = GL_ONE;
1199 m.wantoverbright = true;
1200 m.tex[0] = R_GetTexture(texture->texture);
1202 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1204 if (surf->dlightframe == r_framecount)
1206 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1208 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1210 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1211 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1212 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1213 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1214 R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
1215 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
1216 RSurf_ScaleColors(varray_color, mesh_colorscale, mesh->numverts);
1217 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1224 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture)
1226 const msurface_t *surf;
1227 const surfmesh_t *mesh;
1230 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1231 memset(&m, 0, sizeof(m));
1232 m.blendfunc1 = GL_SRC_ALPHA;
1233 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1234 //m.wantoverbright = false;
1237 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1239 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1241 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1242 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1243 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1245 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1246 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, mesh_colorscale, mesh->numverts, modelorg);
1247 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1252 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture)
1254 const msurface_t *surf;
1255 const surfmesh_t *mesh;
1257 memset(&m, 0, sizeof(m));
1258 m.blendfunc1 = GL_DST_COLOR;
1259 m.blendfunc2 = GL_SRC_COLOR;
1260 //m.wantoverbright = false;
1261 m.tex[0] = R_GetTexture(texture->detailtexture);
1263 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1265 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1267 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1268 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1269 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1270 memcpy(varray_texcoord[0], mesh->ab, mesh->numverts * sizeof(float[2]));
1271 R_FillColors(varray_color, mesh->numverts, 1, 1, 1, 1);
1272 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1277 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture)
1279 const msurface_t *surf;
1280 const surfmesh_t *mesh;
1282 memset(&m, 0, sizeof(m));
1283 m.blendfunc1 = GL_SRC_ALPHA;
1284 m.blendfunc2 = GL_ONE;
1285 //m.wantoverbright = false;
1286 m.tex[0] = R_GetTexture(texture->glowtexture);
1288 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1290 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1292 R_Mesh_ResizeCheck(mesh->numverts, mesh->numtriangles);
1293 memcpy(varray_element, mesh->index, mesh->numtriangles * sizeof(int[3]));
1294 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1295 memcpy(varray_texcoord[0], mesh->st, mesh->numverts * sizeof(float[2]));
1296 R_FillColors(varray_color, mesh->numverts, mesh_colorscale, mesh_colorscale, mesh_colorscale, 1);
1297 R_Mesh_Draw(mesh->numverts, mesh->numtriangles);
1302 static void RSurfShader_Wall_Fullbright_Callback(const void *calldata1, int calldata2)
1304 const entity_render_t *ent = calldata1;
1305 const msurface_t *surf = ent->model->surfaces + calldata2;
1306 R_Mesh_Matrix(&ent->matrix);
1307 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1308 if (surf->currenttexture->glowtexture)
1309 RSurfShader_Wall_Pass_Glow(ent, surf);
1311 RSurfShader_Wall_Pass_Fog(ent, surf);
1314 static void RSurfShader_Wall_Fullbright(const entity_render_t *ent, const texture_t *texture)
1316 const msurface_t *surf;
1318 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1320 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1322 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1323 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Fullbright_Callback, ent, surf - ent->model->surfaces);
1328 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1329 RSurfShader_Wall_Pass_BaseFullbright(ent, surf);
1330 if (texture->glowtexture)
1331 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1332 RSurfShader_Wall_Pass_Glow(ent, surf);
1334 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1335 RSurfShader_Wall_Pass_Fog(ent, surf);
1339 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1341 const entity_render_t *ent = calldata1;
1342 const msurface_t *surf = ent->model->surfaces + calldata2;
1343 R_Mesh_Matrix(&ent->matrix);
1344 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1345 if (surf->currenttexture->glowtexture)
1346 RSurfShader_Wall_Pass_Glow(ent, surf);
1348 RSurfShader_Wall_Pass_Fog(ent, surf);
1351 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture)
1353 const msurface_t *surf;
1355 if (ent->effects & EF_ADDITIVE || ent->alpha < 1 || texture->fogtexture != NULL)
1357 // transparent vertex shaded from lightmap
1358 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1360 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1361 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1364 else if (r_vertexsurfaces.integer)
1366 // opaque vertex shaded from lightmap
1367 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1368 RSurfShader_Wall_Pass_BaseVertex(ent, surf);
1369 if (texture->glowtexture)
1370 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1371 RSurfShader_Wall_Pass_Glow(ent, surf);
1373 for (surf = texture->surfacechain;surf;surf = surf->texturechain)
1374 RSurfShader_Wall_Pass_Fog(ent, surf);
1378 // opaque lightmapped
1379 if (r_textureunits.integer >= 2)
1381 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1382 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture);
1385 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture);
1386 if (r_detailtextures.integer)
1387 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1392 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture);
1393 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture);
1394 if (r_detailtextures.integer)
1395 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture);
1397 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1398 RSurfShader_OpaqueWall_Pass_Light(ent, texture);
1399 if (texture->glowtexture)
1400 RSurfShader_OpaqueWall_Pass_Glow(ent, texture);
1402 RSurfShader_OpaqueWall_Pass_Fog(ent, texture);
1406 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1407 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, 0};
1408 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1409 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1411 int Cshader_count = 4;
1412 Cshader_t *Cshaders[4] =
1414 &Cshader_wall_lightmap,
1415 &Cshader_wall_fullbright,
1420 void R_DrawSurfaces(entity_render_t *ent, int sky, int normal)
1422 int i, alttextures, texframe, framecount, numtextures, numsurfaces, *surfacevisframes;
1423 texture_t *t, *textures;
1425 msurface_t *surf, *surfaces;
1431 // mark the surfaces touched by dynamic lights
1432 if (normal && r_dynamic.integer)
1435 R_Mesh_Matrix(&ent->matrix);
1438 alttextures = ent->frame != 0;
1439 texframe = (int)(cl.time * 5.0f);
1441 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1443 numtextures = model->numtextures;
1444 textures = model->textures;
1445 numsurfaces = model->nummodelsurfaces;
1446 surfaces = model->surfaces + model->firstmodelsurface;
1447 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1449 for (i = 0;i < numtextures;i++)
1450 textures[i].surfacechain = NULL;
1452 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1454 if (surfacevisframes[i] == r_framecount)
1456 #if !WORLDNODECULLBACKFACES
1457 // mark any backface surfaces as not visible
1458 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1460 if (!(surf->flags & SURF_PLANEBACK))
1461 surfacevisframes[i] = -1;
1465 if ((surf->flags & SURF_PLANEBACK))
1466 surfacevisframes[i] = -1;
1468 if (surfacevisframes[i] == r_framecount)
1472 t = surf->texinfo->texture;
1475 framecount = t->anim_total[alttextures];
1476 if (framecount >= 2)
1477 t = t->anim_frames[alttextures][texframe % framecount];
1479 t = t->anim_frames[alttextures][0];
1481 surf->currenttexture = t;
1482 surf->texturechain = t->surfacechain;
1483 t->surfacechain = surf;
1484 if (!r_vertexsurfaces.integer && surf->lightmaptexture != NULL)
1486 if (surf->cached_dlight
1487 || surf->cached_ambient != r_ambient.value
1488 || surf->cached_lightscalebit != lightscalebit)
1489 R_BuildLightMap(ent, surf, false); // base lighting changed
1490 else if (r_dynamic.integer)
1492 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1493 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1494 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1495 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1496 R_BuildLightMap(ent, surf, false); // base lighting changed
1497 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1498 R_BuildLightMap(ent, surf, true); // only dlights
1506 for (i = 0, t = textures;i < numtextures;i++, t++)
1507 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_SKY])
1508 t->shader->shaderfunc[SHADERSTAGE_SKY](ent, t);
1511 for (i = 0, t = textures;i < numtextures;i++, t++)
1512 if (t->surfacechain && t->shader->shaderfunc[SHADERSTAGE_NORMAL])
1513 t->shader->shaderfunc[SHADERSTAGE_NORMAL](ent, t);
1516 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1521 const entity_render_t *ent = calldata1;
1522 const mportal_t *portal = ent->model->portals + calldata2;
1523 memset(&m, 0, sizeof(m));
1524 m.blendfunc1 = GL_SRC_ALPHA;
1525 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1526 m.wantoverbright = false;
1527 R_Mesh_Matrix(&ent->matrix);
1529 R_Mesh_ResizeCheck(portal->numpoints, portal->numpoints - 2);
1530 for (i = 0;i < portal->numpoints - 2;i++)
1532 varray_element[i * 3 + 0] = 0;
1533 varray_element[i * 3 + 1] = i + 1;
1534 varray_element[i * 3 + 2] = i + 2;
1536 i = portal - ent->model->portals;
1537 R_FillColors(varray_color, portal->numpoints,
1538 ((i & 0x0007) >> 0) * (1.0f / 7.0f) * mesh_colorscale,
1539 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * mesh_colorscale,
1540 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * mesh_colorscale,
1542 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1544 for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
1545 VectorCopy(portal->points[i].position, v);
1548 for (i = 0, v = varray_vertex;i < portal->numpoints;i++, v += 4)
1549 VectorCopy(portal->points[i].position, v);
1550 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2);
1553 static void R_DrawPortals(entity_render_t *ent)
1556 mportal_t *portal, *endportal;
1557 float temp[3], center[3], f;
1559 if (r_drawportals.integer < 1)
1562 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1564 if (portal->here->pvsframe == r_pvsframecount || portal->past->pvsframe == r_pvsframecount)
1567 for (i = 0;i < portal->numpoints;i++)
1568 VectorAdd(temp, portal->points[i].position, temp);
1569 f = ixtable[portal->numpoints];
1570 VectorScale(temp, f, temp);
1571 Matrix4x4_Transform(&ent->matrix, temp, center);
1572 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1577 void R_DrawBrushModel(entity_render_t *ent, int sky, int normal)
1579 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1582 #if WORLDNODECULLBACKFACES
1586 // because bmodels can be reused, we have to decide which things to render
1587 // from scratch every time
1589 #if WORLDNODECULLBACKFACES
1590 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1592 numsurfaces = model->nummodelsurfaces;
1593 surf = model->surfaces + model->firstmodelsurface;
1594 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1595 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1596 for (i = 0;i < numsurfaces;i++, surf++)
1598 #if WORLDNODECULLBACKFACES
1599 // mark any backface surfaces as not visible
1600 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1602 if ((surf->flags & SURF_PLANEBACK))
1604 surfacevisframes[i] = r_framecount;
1605 surfacepvsframes[i] = r_pvsframecount;
1610 if (!(surf->flags & SURF_PLANEBACK))
1612 surfacevisframes[i] = r_framecount;
1613 surfacepvsframes[i] = r_pvsframecount;
1617 surfacevisframes[i] = r_framecount;
1618 surfacepvsframes[i] = r_pvsframecount;
1620 surf->dlightframe = -1;
1622 R_DrawSurfaces(ent, sky, normal);
1625 void R_SurfaceWorldNode (entity_render_t *ent)
1627 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1628 msurface_t *surfaces, *surf;
1633 numsurfaces = model->nummodelsurfaces;
1634 surfaces = model->surfaces + model->firstmodelsurface;
1635 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1636 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1637 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1639 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1641 if (surfacepvsframes[i] == r_pvsframecount)
1643 #if WORLDNODECULLBACKFACES
1644 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1646 if ((surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1647 surfacevisframes[i] = r_framecount;
1651 if (!(surf->flags & SURF_PLANEBACK) && R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1652 surfacevisframes[i] = r_framecount;
1655 if (R_NotCulledBox (surf->poly_mins, surf->poly_maxs))
1656 surfacevisframes[i] = r_framecount;
1663 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1666 mportal_t *p, *pstack[8192];
1667 msurface_t *surf, **mark, **endmark;
1669 // LordHavoc: portal-passage worldnode with PVS;
1670 // follows portals leading outward from viewleaf, does not venture
1671 // offscreen or into leafs that are not visible, faster than Quake's
1672 // RecursiveWorldNode
1674 leaf->worldnodeframe = r_framecount;
1678 if (leaf->nummarksurfaces)
1680 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1683 // make sure surfaces are only processed once
1684 if (surf->worldnodeframe != r_framecount)
1686 surf->worldnodeframe = r_framecount;
1687 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1689 if (surf->flags & SURF_PLANEBACK)
1690 surf->visframe = r_framecount;
1694 if (!(surf->flags & SURF_PLANEBACK))
1695 surf->visframe = r_framecount;
1700 // follow portals into other leafs
1701 for (p = leaf->portals;p;p = p->next)
1704 if (leaf->worldnodeframe != r_framecount)
1706 leaf->worldnodeframe = r_framecount;
1707 // FIXME: R_NotCulledBox is absolute, should be done relative
1708 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1710 p->visframe = r_framecount;
1711 pstack[portalstack++] = p;
1714 p = pstack[--portalstack];
1723 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1725 int c, leafstackpos, *mark, *surfacevisframes;
1726 #if WORLDNODECULLBACKFACES
1730 mleaf_t *leaf, *leafstack[8192];
1733 msurface_t *surfaces;
1734 // LordHavoc: portal-passage worldnode with PVS;
1735 // follows portals leading outward from viewleaf, does not venture
1736 // offscreen or into leafs that are not visible, faster than Quake's
1737 // RecursiveWorldNode
1738 surfaces = ent->model->surfaces;
1739 surfacevisframes = ent->model->surfacevisframes;
1740 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1741 viewleaf->worldnodeframe = r_framecount;
1742 leafstack[0] = viewleaf;
1744 while (leafstackpos)
1747 leaf = leafstack[--leafstackpos];
1748 // draw any surfaces bounding this leaf
1749 if (leaf->nummarksurfaces)
1751 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1753 #if WORLDNODECULLBACKFACES
1755 if (surfacevisframes[n] != r_framecount)
1757 surf = surfaces + n;
1758 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1760 if ((surf->flags & SURF_PLANEBACK))
1761 surfacevisframes[n] = r_framecount;
1765 if (!(surf->flags & SURF_PLANEBACK))
1766 surfacevisframes[n] = r_framecount;
1770 surfacevisframes[*mark++] = r_framecount;
1774 // follow portals into other leafs
1775 for (p = leaf->portals;p;p = p->next)
1778 if (leaf->worldnodeframe != r_framecount)
1780 leaf->worldnodeframe = r_framecount;
1781 // FIXME: R_NotCulledBox is absolute, should be done relative
1782 if (leaf->pvsframe == r_pvsframecount && R_NotCulledBox(leaf->mins, leaf->maxs))
1783 leafstack[leafstackpos++] = leaf;
1787 if (r_drawportals.integer)
1792 void R_PVSUpdate (mleaf_t *viewleaf)
1794 int i, j, l, c, bits, *surfacepvsframes, *mark;
1798 if (r_pvsviewleaf == viewleaf && r_pvsviewleafnovis == r_novis.integer)
1802 r_pvsviewleaf = viewleaf;
1803 r_pvsviewleafnovis = r_novis.integer;
1807 surfacepvsframes = cl.worldmodel->surfacepvsframes;
1808 vis = Mod_LeafPVS (viewleaf, cl.worldmodel);
1809 for (j = 0;j < cl.worldmodel->numleafs;j += 8)
1814 l = cl.worldmodel->numleafs - j;
1817 for (i = 0;i < l;i++)
1819 if (bits & (1 << i))
1821 leaf = &cl.worldmodel->leafs[j + i + 1];
1822 leaf->pvsframe = r_pvsframecount;
1823 // mark surfaces bounding this leaf as visible
1824 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1825 surfacepvsframes[*mark++] = r_pvsframecount;
1838 void R_DrawWorld (entity_render_t *ent)
1841 viewleaf = Mod_PointInLeaf (r_origin, cl.worldmodel);
1842 R_PVSUpdate(viewleaf);
1845 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1846 R_SurfaceWorldNode (ent);
1848 R_PortalWorldNode (ent, viewleaf);
1849 R_DrawSurfaces(ent, true, true);
1857 void R_DrawBrushModelSky (entity_render_t *ent)
1859 R_DrawBrushModel(ent, true, false);
1862 void R_DrawBrushModelNormal (entity_render_t *ent)
1865 R_DrawBrushModel(ent, false, true);
1868 static void gl_surf_start(void)
1872 static void gl_surf_shutdown(void)
1876 static void gl_surf_newmap(void)
1878 // reset pvs visibility variables so it will update on first frame
1879 r_pvsframecount = 1;
1880 r_pvsviewleaf = NULL;
1881 r_pvsviewleafnovis = false;
1884 void GL_Surf_Init(void)
1887 dlightdivtable[0] = 4194304;
1888 for (i = 1;i < 32768;i++)
1889 dlightdivtable[i] = 4194304 / (i << 7);
1891 Cvar_RegisterVariable(&r_ambient);
1892 Cvar_RegisterVariable(&r_vertexsurfaces);
1893 Cvar_RegisterVariable(&r_dlightmap);
1894 Cvar_RegisterVariable(&r_drawportals);
1895 Cvar_RegisterVariable(&r_testvis);
1896 Cvar_RegisterVariable(&r_floatbuildlightmap);
1897 Cvar_RegisterVariable(&r_detailtextures);
1898 Cvar_RegisterVariable(&r_surfaceworldnode);
1900 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);