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
25 #define MAX_LIGHTMAP_SIZE 256
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
34 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
35 cvar_t r_drawportals = {0, "r_drawportals", "0"};
36 cvar_t r_testvis = {0, "r_testvis", "0"};
37 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
38 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
39 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "0"};
41 static int dlightdivtable[32768];
43 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
45 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
47 float dist, impact[3], local[3];
51 smax = (surf->extents[0] >> 4) + 1;
52 tmax = (surf->extents[1] >> 4) + 1;
55 for (lnum = 0; lnum < r_numdlights; lnum++)
57 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
58 continue; // not lit by this light
60 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
61 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
63 // for comparisons to minimum acceptable light
64 // compensate for LIGHTOFFSET
65 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
72 if (surf->plane->type < 3)
74 VectorCopy(local, impact);
75 impact[surf->plane->type] -= dist;
79 impact[0] = local[0] - surf->plane->normal[0] * dist;
80 impact[1] = local[1] - surf->plane->normal[1] * dist;
81 impact[2] = local[2] - surf->plane->normal[2] * dist;
84 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
85 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
87 s = bound(0, impacts, smax * 16) - impacts;
88 t = bound(0, impactt, tmax * 16) - impactt;
89 i = s * s + t * t + dist2;
93 // reduce calculations
94 for (s = 0, i = impacts; s < smax; s++, i -= 16)
95 sdtable[s] = i * i + dist2;
97 maxdist3 = maxdist - dist2;
99 // convert to 8.8 blocklights format
100 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
101 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
102 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
103 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
107 for (t = 0;t < tmax;t++, i -= 16)
110 // make sure some part of it is visible on this line
113 maxdist2 = maxdist - td;
114 for (s = 0;s < smax;s++)
116 if (sdtable[s] < maxdist2)
118 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
122 bl[1] += (green * k);
137 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
139 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
140 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
144 smax = (surf->extents[0] >> 4) + 1;
145 tmax = (surf->extents[1] >> 4) + 1;
148 for (lnum = 0; lnum < r_numdlights; lnum++)
150 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
151 continue; // not lit by this light
153 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
154 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
156 // for comparisons to minimum acceptable light
157 // compensate for LIGHTOFFSET
158 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
161 dist2 += LIGHTOFFSET;
162 if (dist2 >= maxdist)
165 if (surf->plane->type < 3)
167 VectorCopy(local, impact);
168 impact[surf->plane->type] -= dist;
172 impact[0] = local[0] - surf->plane->normal[0] * dist;
173 impact[1] = local[1] - surf->plane->normal[1] * dist;
174 impact[2] = local[2] - surf->plane->normal[2] * dist;
177 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
178 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
180 td = bound(0, impacts, smax * 16) - impacts;
181 td1 = bound(0, impactt, tmax * 16) - impactt;
182 td = td * td + td1 * td1 + dist2;
186 // reduce calculations
187 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
188 sdtable[s] = td1 * td1 + dist2;
190 maxdist3 = maxdist - dist2;
192 // convert to 8.8 blocklights format
193 red = r_dlight[lnum].light[0];
194 green = r_dlight[lnum].light[1];
195 blue = r_dlight[lnum].light[2];
196 subtract = r_dlight[lnum].subtract * 32768.0f;
197 bl = floatblocklights;
200 for (t = 0;t < tmax;t++, td1 -= 16.0f)
203 // make sure some part of it is visible on this line
206 maxdist2 = maxdist - td;
207 for (s = 0;s < smax;s++)
209 if (sdtable[s] < maxdist2)
211 k = (32768.0f / (sdtable[s] + td)) - subtract;
231 Combine and scale multiple lightmaps into the 8.8 format in blocklights
234 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf, int dlightchanged)
236 if (!r_floatbuildlightmap.integer)
238 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
239 unsigned int *bl, scale;
240 qbyte *lightmap, *out, *stain;
242 // update cached lighting info
243 surf->cached_dlight = 0;
244 surf->cached_lightmapscalebit = r_lightmapscalebit;
245 surf->cached_ambient = r_ambient.value;
246 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
247 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
248 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
249 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
251 smax = (surf->extents[0]>>4)+1;
252 tmax = (surf->extents[1]>>4)+1;
255 lightmap = surf->samples;
257 // set to full bright if no light data
259 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
261 for (i = 0;i < size3;i++)
267 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
270 for (i = 0;i < size3;i++)
274 memset(bl, 0, size*3*sizeof(unsigned int));
276 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
278 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
279 if (surf->cached_dlight)
281 else if (dlightchanged)
282 return; // don't upload if only updating dlights and none mattered
285 // add all the lightmaps
289 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
290 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
291 bl[i] += lightmap[i] * scale;
295 stain = surf->stainsamples;
298 // deal with lightmap brightness scale
299 shift = 7 + r_lightmapscalebit + 8;
300 if (ent->model->lightmaprgba)
302 stride = (surf->lightmaptexturestride - smax) * 4;
303 for (i = 0;i < tmax;i++, out += stride)
305 for (j = 0;j < smax;j++)
307 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
308 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
309 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
316 stride = (surf->lightmaptexturestride - smax) * 3;
317 for (i = 0;i < tmax;i++, out += stride)
319 for (j = 0;j < smax;j++)
321 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
322 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
323 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
328 R_UpdateTexture(surf->lightmaptexture, templight);
332 int smax, tmax, i, j, size, size3, maps, stride, l;
334 qbyte *lightmap, *out, *stain;
336 // update cached lighting info
337 surf->cached_dlight = 0;
338 surf->cached_lightmapscalebit = r_lightmapscalebit;
339 surf->cached_ambient = r_ambient.value;
340 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
341 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
342 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
343 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
345 smax = (surf->extents[0]>>4)+1;
346 tmax = (surf->extents[1]>>4)+1;
349 lightmap = surf->samples;
351 // set to full bright if no light data
352 bl = floatblocklights;
353 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
356 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
361 for (i = 0;i < size3;i++)
365 memset(bl, 0, size*3*sizeof(float));
367 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
369 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
370 if (surf->cached_dlight)
372 else if (dlightchanged)
373 return; // don't upload if only updating dlights and none mattered
376 // add all the lightmaps
379 bl = floatblocklights;
380 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
381 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
382 bl[i] += lightmap[i] * scale;
385 stain = surf->stainsamples;
386 bl = floatblocklights;
388 // deal with lightmap brightness scale
389 scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
390 if (ent->model->lightmaprgba)
392 stride = (surf->lightmaptexturestride - smax) * 4;
393 for (i = 0;i < tmax;i++, out += stride)
395 for (j = 0;j < smax;j++)
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
399 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
406 stride = (surf->lightmaptexturestride - smax) * 3;
407 for (i = 0;i < tmax;i++, out += stride)
409 for (j = 0;j < smax;j++)
411 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
412 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
413 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418 R_UpdateTexture(surf->lightmaptexture, templight);
422 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, float fcolor[8])
424 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
425 msurface_t *surf, *endsurf;
426 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
430 maxdist = radius * radius;
431 invradius = 1.0f / radius;
434 if (node->contents < 0)
436 ndist = PlaneDiff(origin, node->plane);
439 node = node->children[0];
444 node = node->children[1];
448 dist2 = ndist * ndist;
449 maxdist3 = maxdist - dist2;
451 if (node->plane->type < 3)
453 VectorCopy(origin, impact);
454 impact[node->plane->type] -= ndist;
458 impact[0] = origin[0] - node->plane->normal[0] * ndist;
459 impact[1] = origin[1] - node->plane->normal[1] * ndist;
460 impact[2] = origin[2] - node->plane->normal[2] * ndist;
463 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
465 if (surf->stainsamples)
467 smax = (surf->extents[0] >> 4) + 1;
468 tmax = (surf->extents[1] >> 4) + 1;
470 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
471 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
473 s = bound(0, impacts, smax * 16) - impacts;
474 t = bound(0, impactt, tmax * 16) - impactt;
475 i = s * s + t * t + dist2;
479 // reduce calculations
480 for (s = 0, i = impacts; s < smax; s++, i -= 16)
481 sdtable[s] = i * i + dist2;
483 bl = surf->stainsamples;
488 for (t = 0;t < tmax;t++, i -= 16)
491 // make sure some part of it is visible on this line
494 maxdist2 = maxdist - td;
495 for (s = 0;s < smax;s++)
497 if (sdtable[s] < maxdist2)
499 ratio = lhrandom(0.0f, 1.0f);
500 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
501 if (a >= (1.0f / 64.0f))
505 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
506 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
507 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
517 // force lightmap upload
519 surf->cached_dlight = true;
523 if (node->children[0]->contents >= 0)
525 if (node->children[1]->contents >= 0)
527 R_StainNode(node->children[0], model, origin, radius, fcolor);
528 node = node->children[1];
533 node = node->children[0];
537 else if (node->children[1]->contents >= 0)
539 node = node->children[1];
544 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
548 entity_render_t *ent;
554 fcolor[3] = ca1 * (1.0f / 64.0f);
555 fcolor[4] = cr2 - cr1;
556 fcolor[5] = cg2 - cg1;
557 fcolor[6] = cb2 - cb1;
558 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
560 model = cl.worldmodel;
562 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, fcolor);
564 // look for embedded bmodels
565 for (n = 0;n < cl_num_brushmodel_entities;n++)
567 ent = cl_brushmodel_entities[n];
569 if (model && model->name[0] == '*')
571 Mod_CheckLoaded(model);
572 if (model->type == mod_brush)
574 Matrix4x4_Transform(&ent->inversematrix, origin, org);
575 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, fcolor);
583 =============================================================
587 =============================================================
590 static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
595 if (styles[0] != 255)
597 for (i = 0;i < numverts;i++, c += 4)
599 lm = samples + lightmapoffsets[i];
600 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
601 VectorMA(c, scale, lm, c);
602 if (styles[1] != 255)
605 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
606 VectorMA(c, scale, lm, c);
607 if (styles[2] != 255)
610 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
611 VectorMA(c, scale, lm, c);
612 if (styles[3] != 255)
615 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
616 VectorMA(c, scale, lm, c);
624 static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
630 for (i = 0;i < numverts;i++, v += 4, c += 4)
632 VectorSubtract(v, modelorg, diff);
633 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
634 VectorScale(c, f, c);
637 else if (colorscale != 1)
638 for (i = 0;i < numverts;i++, c += 4)
639 VectorScale(c, colorscale, c);
642 static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
651 for (i = 0;i < numverts;i++, v += 4, c += 4)
653 VectorSubtract(v, modelorg, diff);
654 f = 1 - exp(fogdensity/DotProduct(diff, diff));
663 for (i = 0;i < numverts;i++, c += 4)
673 static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
680 for (i = 0;i < numverts;i++, v += 4, c += 4)
682 VectorSubtract(v, modelorg, diff);
683 f = exp(fogdensity/DotProduct(diff, diff));
691 static void RSurf_ScaleColors(float *c, float scale, int numverts)
695 for (i = 0;i < numverts;i++, c += 4)
696 VectorScale(c, scale, c);
699 static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
704 int i, l, lit = false;
707 for (l = 0;l < r_numdlights;l++)
709 if (dlightbits[l >> 5] & (1 << (l & 31)))
712 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
713 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
715 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
716 if (f < rd->cullradius2)
718 f = (1.0f / f) - rd->subtract;
719 VectorMA(c, f, rd->light, c);
728 // note: this untransforms lights to do the checking,
729 // and takes surf->mesh->verts data
730 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
736 for (l = 0;l < r_numdlights;l++)
738 if (dlightbits[l >> 5] & (1 << (l & 31)))
741 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
742 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
743 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
750 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
752 const msurface_t *surf;
753 const surfmesh_t *mesh;
756 // LordHavoc: HalfLife maps have freaky skypolys...
757 if (ent->model->ishlbsp)
762 skyrendernow = false;
767 R_Mesh_Matrix(&ent->matrix);
769 // draw depth-only polys
770 memset(&m, 0, sizeof(m));
773 qglColorMask(0,0,0,0);
774 // just to make sure that braindead drivers don't draw anything
775 // despite that colormask...
776 m.blendfunc1 = GL_ZERO;
777 m.blendfunc2 = GL_ONE;
782 m.blendfunc1 = GL_ONE;
783 m.blendfunc2 = GL_ZERO;
787 for (surf = firstsurf;surf;surf = surf->texturechain)
789 if (surf->visframe == r_framecount)
791 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
793 R_Mesh_ResizeCheck(mesh->numverts);
794 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
795 GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, 1);
796 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
800 qglColorMask(1,1,1,1);
803 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
805 const entity_render_t *ent = calldata1;
806 const msurface_t *surf = ent->model->surfaces + calldata2;
808 const surfmesh_t *mesh;
813 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
815 R_Mesh_Matrix(&ent->matrix);
817 memset(&m, 0, sizeof(m));
818 texture = surf->texinfo->texture->currentframe;
819 alpha = texture->currentalpha;
820 if (texture->rendertype == SURFRENDER_ADD)
822 m.blendfunc1 = GL_SRC_ALPHA;
823 m.blendfunc2 = GL_ONE;
825 else if (texture->rendertype == SURFRENDER_ALPHA)
827 m.blendfunc1 = GL_SRC_ALPHA;
828 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
832 m.blendfunc1 = GL_ONE;
833 m.blendfunc2 = GL_ZERO;
835 m.tex[0] = R_GetTexture(texture->texture);
836 colorscale = r_colorscale;
837 if (gl_combine.integer)
839 m.texrgbscale[0] = 4;
844 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
846 R_Mesh_ResizeCheck(mesh->numverts);
847 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
848 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
849 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
850 R_FillColors(varray_color, mesh->numverts, f, f, f, alpha);
851 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
853 if (surf->dlightframe == r_framecount)
854 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
855 if (surf->flags & SURF_LIGHTMAP)
856 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
858 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
859 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
864 memset(&m, 0, sizeof(m));
865 m.blendfunc1 = GL_SRC_ALPHA;
866 m.blendfunc2 = GL_ONE;
867 m.tex[0] = R_GetTexture(texture->fogtexture);
869 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
871 R_Mesh_ResizeCheck(mesh->numverts);
872 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
874 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
875 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, r_colorscale, mesh->numverts, modelorg);
876 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
881 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
883 const msurface_t *surf;
885 if (texture->rendertype != SURFRENDER_OPAQUE)
887 for (surf = firstsurf;surf;surf = surf->texturechain)
889 if (surf->visframe == r_framecount)
891 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
892 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
897 for (surf = firstsurf;surf;surf = surf->texturechain)
898 if (surf->visframe == r_framecount)
899 RSurfShader_Water_Callback(ent, surf - ent->model->surfaces);
902 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
904 float base, colorscale;
905 const surfmesh_t *mesh;
908 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
909 memset(&m, 0, sizeof(m));
910 if (rendertype == SURFRENDER_ADD)
912 m.blendfunc1 = GL_SRC_ALPHA;
913 m.blendfunc2 = GL_ONE;
915 else if (rendertype == SURFRENDER_ALPHA)
917 m.blendfunc1 = GL_SRC_ALPHA;
918 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
922 m.blendfunc1 = GL_ONE;
923 m.blendfunc2 = GL_ZERO;
925 m.tex[0] = R_GetTexture(texture->texture);
926 colorscale = r_colorscale;
927 if (gl_combine.integer)
929 m.texrgbscale[0] = 4;
932 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
935 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
937 R_Mesh_ResizeCheck(mesh->numverts);
938 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
939 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
940 R_FillColors(varray_color, mesh->numverts, base, base, base, currentalpha);
941 if (!(ent->effects & EF_FULLBRIGHT))
943 if (surf->dlightframe == r_framecount)
944 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
945 if (surf->flags & SURF_LIGHTMAP)
946 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
948 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
949 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
953 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
955 const surfmesh_t *mesh;
958 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
959 memset(&m, 0, sizeof(m));
960 m.blendfunc1 = GL_SRC_ALPHA;
961 m.blendfunc2 = GL_ONE;
962 m.tex[0] = R_GetTexture(texture->glowtexture);
965 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
967 R_Mesh_ResizeCheck(mesh->numverts);
968 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
969 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
970 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, currentalpha, r_colorscale, mesh->numverts, modelorg);
971 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
975 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
977 const surfmesh_t *mesh;
980 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
981 memset(&m, 0, sizeof(m));
982 m.blendfunc1 = GL_SRC_ALPHA;
983 m.blendfunc2 = GL_ONE;
984 m.tex[0] = R_GetTexture(texture->fogtexture);
987 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
989 R_Mesh_ResizeCheck(mesh->numverts);
990 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
992 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
993 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, r_colorscale, mesh->numverts, modelorg);
994 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
998 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1000 const msurface_t *surf;
1001 const surfmesh_t *mesh;
1003 int lightmaptexturenum;
1005 memset(&m, 0, sizeof(m));
1006 m.blendfunc1 = GL_ONE;
1007 m.blendfunc2 = GL_ZERO;
1008 m.tex[0] = R_GetTexture(texture->texture);
1009 m.tex[1] = R_GetTexture(firstsurf->lightmaptexture);
1010 m.tex[2] = R_GetTexture(texture->detailtexture);
1011 m.texrgbscale[0] = 1;
1012 m.texrgbscale[1] = 4;
1013 m.texrgbscale[2] = 2;
1015 cl = (float) (1 << r_lightmapscalebit) * r_colorscale;
1016 GL_Color(cl, cl, cl, 1);
1017 for (surf = firstsurf;surf;surf = surf->texturechain)
1019 if (surf->visframe == r_framecount)
1021 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1022 if (m.tex[1] != lightmaptexturenum)
1024 m.tex[1] = lightmaptexturenum;
1027 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1029 R_Mesh_ResizeCheck(mesh->numverts);
1030 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1031 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1032 memcpy(varray_texcoord[1], mesh->uvw, mesh->numverts * sizeof(float[4]));
1033 memcpy(varray_texcoord[2], mesh->abc, mesh->numverts * sizeof(float[4]));
1034 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1040 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1042 const msurface_t *surf;
1043 const surfmesh_t *mesh;
1045 int lightmaptexturenum;
1046 memset(&m, 0, sizeof(m));
1047 m.blendfunc1 = GL_ONE;
1048 m.blendfunc2 = GL_ZERO;
1049 m.tex[0] = R_GetTexture(texture->texture);
1050 m.tex[1] = R_GetTexture(firstsurf->lightmaptexture);
1051 if (gl_combine.integer)
1052 m.texrgbscale[1] = 4;
1054 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1055 for (surf = firstsurf;surf;surf = surf->texturechain)
1057 if (surf->visframe == r_framecount)
1059 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1060 if (m.tex[1] != lightmaptexturenum)
1062 m.tex[1] = lightmaptexturenum;
1065 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1067 R_Mesh_ResizeCheck(mesh->numverts);
1068 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1069 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1070 memcpy(varray_texcoord[1], mesh->uvw, mesh->numverts * sizeof(float[4]));
1071 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1077 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1079 const msurface_t *surf;
1080 const surfmesh_t *mesh;
1082 memset(&m, 0, sizeof(m));
1083 m.blendfunc1 = GL_ONE;
1084 m.blendfunc2 = GL_ZERO;
1085 m.tex[0] = R_GetTexture(texture->texture);
1087 GL_Color(1, 1, 1, 1);
1088 for (surf = firstsurf;surf;surf = surf->texturechain)
1090 if (surf->visframe == r_framecount)
1092 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1094 R_Mesh_ResizeCheck(mesh->numverts);
1095 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1096 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1097 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1103 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1105 const msurface_t *surf;
1106 const surfmesh_t *mesh;
1108 int lightmaptexturenum;
1109 memset(&m, 0, sizeof(m));
1110 m.blendfunc1 = GL_ZERO;
1111 m.blendfunc2 = GL_SRC_COLOR;
1112 m.tex[0] = R_GetTexture(firstsurf->lightmaptexture);
1113 if (gl_combine.integer)
1114 m.texrgbscale[0] = 4;
1116 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1117 for (surf = firstsurf;surf;surf = surf->texturechain)
1119 if (surf->visframe == r_framecount)
1121 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1122 if (m.tex[0] != lightmaptexturenum)
1124 m.tex[0] = lightmaptexturenum;
1127 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1129 R_Mesh_ResizeCheck(mesh->numverts);
1130 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1131 memcpy(varray_texcoord[0], mesh->uvw, mesh->numverts * sizeof(float[4]));
1132 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1138 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1140 const msurface_t *surf;
1141 const surfmesh_t *mesh;
1145 memset(&m, 0, sizeof(m));
1146 m.blendfunc1 = GL_SRC_ALPHA;
1147 m.blendfunc2 = GL_ONE;
1148 m.tex[0] = R_GetTexture(texture->texture);
1149 colorscale = r_colorscale;
1150 if (gl_combine.integer)
1152 m.texrgbscale[0] = 4;
1153 colorscale *= 0.25f;
1157 for (surf = firstsurf;surf;surf = surf->texturechain)
1159 if (surf->visframe == r_framecount && surf->dlightframe == r_framecount)
1161 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1163 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1165 R_Mesh_ResizeCheck(mesh->numverts);
1166 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1167 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1168 R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
1169 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
1170 RSurf_ScaleColors(varray_color, colorscale, mesh->numverts);
1171 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1178 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1180 const msurface_t *surf;
1181 const surfmesh_t *mesh;
1184 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1185 memset(&m, 0, sizeof(m));
1186 m.blendfunc1 = GL_SRC_ALPHA;
1187 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1190 for (surf = firstsurf;surf;surf = surf->texturechain)
1192 if (surf->visframe == r_framecount)
1194 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1196 R_Mesh_ResizeCheck(mesh->numverts);
1197 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1199 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1200 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, r_colorscale, mesh->numverts, modelorg);
1201 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1207 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1209 const msurface_t *surf;
1210 const surfmesh_t *mesh;
1212 memset(&m, 0, sizeof(m));
1213 m.blendfunc1 = GL_DST_COLOR;
1214 m.blendfunc2 = GL_SRC_COLOR;
1215 m.tex[0] = R_GetTexture(texture->detailtexture);
1217 GL_Color(1, 1, 1, 1);
1218 for (surf = firstsurf;surf;surf = surf->texturechain)
1220 if (surf->visframe == r_framecount)
1222 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1224 R_Mesh_ResizeCheck(mesh->numverts);
1225 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1226 memcpy(varray_texcoord[0], mesh->abc, mesh->numverts * sizeof(float[4]));
1227 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1233 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1235 const msurface_t *surf;
1236 const surfmesh_t *mesh;
1238 memset(&m, 0, sizeof(m));
1239 m.blendfunc1 = GL_SRC_ALPHA;
1240 m.blendfunc2 = GL_ONE;
1241 m.tex[0] = R_GetTexture(texture->glowtexture);
1243 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1244 for (surf = firstsurf;surf;surf = surf->texturechain)
1246 if (surf->visframe == r_framecount)
1248 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1250 R_Mesh_ResizeCheck(mesh->numverts);
1251 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1252 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1253 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1259 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1261 const msurface_t *surf;
1262 const surfmesh_t *mesh;
1264 memset(&m, 0, sizeof(m));
1265 m.blendfunc1 = GL_SRC_ALPHA;
1266 m.blendfunc2 = GL_ZERO;
1267 m.tex[0] = R_GetTexture(texture->glowtexture);
1270 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1272 GL_Color(0, 0, 0, 1);
1273 for (surf = firstsurf;surf;surf = surf->texturechain)
1275 if (surf->visframe == r_framecount)
1277 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1279 R_Mesh_ResizeCheck(mesh->numverts);
1280 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1281 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1282 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1288 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1290 const entity_render_t *ent = calldata1;
1291 const msurface_t *surf = ent->model->surfaces + calldata2;
1295 R_Mesh_Matrix(&ent->matrix);
1297 texture = surf->texinfo->texture;
1298 if (texture->animated)
1299 texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (cl.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
1301 currentalpha = ent->alpha;
1302 if (texture->flags & SURF_WATERALPHA)
1303 currentalpha *= r_wateralpha.value;
1304 if (ent->effects & EF_ADDITIVE)
1305 rendertype = SURFRENDER_ADD;
1306 else if (currentalpha < 1 || texture->fogtexture != NULL)
1307 rendertype = SURFRENDER_ALPHA;
1309 rendertype = SURFRENDER_OPAQUE;
1311 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1312 if (texture->glowtexture)
1313 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1315 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1318 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, const msurface_t *firstsurf)
1320 const msurface_t *surf;
1322 if (texture->rendertype != SURFRENDER_OPAQUE)
1324 // transparent vertex shaded from lightmap
1325 for (surf = firstsurf;surf;surf = surf->texturechain)
1327 if (surf->visframe == r_framecount)
1329 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1330 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1334 else if (r_shadow_lightingmode >= 2)
1336 // opaque base lighting
1337 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, firstsurf);
1339 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, firstsurf);
1341 else if (r_vertexsurfaces.integer)
1343 // opaque vertex shaded from lightmap
1344 for (surf = firstsurf;surf;surf = surf->texturechain)
1345 if (surf->visframe == r_framecount)
1346 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, texture->rendertype, texture->currentalpha);
1347 if (texture->glowtexture)
1348 for (surf = firstsurf;surf;surf = surf->texturechain)
1349 if (surf->visframe == r_framecount)
1350 RSurfShader_Wall_Pass_Glow(ent, surf, texture, texture->rendertype, texture->currentalpha);
1352 for (surf = firstsurf;surf;surf = surf->texturechain)
1353 if (surf->visframe == r_framecount)
1354 RSurfShader_Wall_Pass_Fog(ent, surf, texture, texture->rendertype, texture->currentalpha);
1358 // opaque lightmapped
1359 if (r_textureunits.integer >= 2)
1361 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1362 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, firstsurf);
1365 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture, firstsurf);
1366 if (r_detailtextures.integer)
1367 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, firstsurf);
1372 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, firstsurf);
1373 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, firstsurf);
1374 if (r_detailtextures.integer)
1375 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, firstsurf);
1377 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1378 RSurfShader_OpaqueWall_Pass_Light(ent, texture, firstsurf);
1379 if (texture->glowtexture)
1380 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, firstsurf);
1382 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, firstsurf);
1386 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1387 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1388 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1390 int Cshader_count = 3;
1391 Cshader_t *Cshaders[3] =
1393 &Cshader_wall_lightmap,
1398 void R_UpdateTextureInfo(entity_render_t *ent)
1400 int i, texframe, alttextures;
1406 alttextures = ent->frame != 0;
1407 texframe = (int)(cl.time * 5.0f);
1408 for (i = 0;i < ent->model->numtextures;i++)
1410 t = ent->model->textures + i;
1411 t->currentalpha = ent->alpha;
1412 if (t->flags & SURF_WATERALPHA)
1413 t->currentalpha *= r_wateralpha.value;
1414 if (ent->effects & EF_ADDITIVE)
1415 t->rendertype = SURFRENDER_ADD;
1416 else if (t->currentalpha < 1 || t->fogtexture != NULL)
1417 t->rendertype = SURFRENDER_ALPHA;
1419 t->rendertype = SURFRENDER_OPAQUE;
1420 // we don't need to set currentframe if t->animated is false because
1421 // it was already set up by the texture loader for non-animating
1423 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1427 void R_PrepareSurfaces(entity_render_t *ent)
1429 int i, numsurfaces, *surfacevisframes;
1431 msurface_t *surf, *surfaces;
1438 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1439 numsurfaces = model->nummodelsurfaces;
1440 surfaces = model->surfaces + model->firstmodelsurface;
1441 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1443 R_UpdateTextureInfo(ent);
1445 if (r_dynamic.integer && r_shadow_lightingmode < 1)
1448 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1450 if (surfacevisframes[i] == r_framecount)
1452 #if !WORLDNODECULLBACKFACES
1453 // mark any backface surfaces as not visible
1454 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1456 if (!(surf->flags & SURF_PLANEBACK))
1457 surfacevisframes[i] = -1;
1461 if ((surf->flags & SURF_PLANEBACK))
1462 surfacevisframes[i] = -1;
1464 if (surfacevisframes[i] == r_framecount)
1468 surf->visframe = r_framecount;
1469 if (!r_vertexsurfaces.integer && surf->lightmaptexture != NULL)
1471 if (surf->cached_dlight
1472 || surf->cached_ambient != r_ambient.value
1473 || surf->cached_lightmapscalebit != r_lightmapscalebit)
1474 R_BuildLightMap(ent, surf, false); // base lighting changed
1475 else if (r_dynamic.integer)
1477 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1478 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1479 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1480 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1481 R_BuildLightMap(ent, surf, false); // base lighting changed
1482 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1483 R_BuildLightMap(ent, surf, true); // only dlights
1491 void R_DrawSurfaces(entity_render_t *ent, int type)
1495 R_Mesh_Matrix(&ent->matrix);
1496 for (i = 0, t = ent->model->textures;i < ent->model->numtextures;i++, t++)
1497 if (t->shader->shaderfunc[type] && ent->model->texturesurfacechains[i])
1498 t->shader->shaderfunc[type](ent, t->currentframe, ent->model->texturesurfacechains[i]);
1501 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1506 const entity_render_t *ent = calldata1;
1507 const mportal_t *portal = ent->model->portals + calldata2;
1508 memset(&m, 0, sizeof(m));
1509 m.blendfunc1 = GL_SRC_ALPHA;
1510 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1511 R_Mesh_Matrix(&ent->matrix);
1513 R_Mesh_ResizeCheck(portal->numpoints);
1514 i = portal - ent->model->portals;
1515 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_colorscale,
1516 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_colorscale,
1517 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_colorscale,
1519 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1521 for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
1522 VectorCopy(portal->points[i].position, v);
1525 for (i = 0, v = varray_vertex;i < portal->numpoints;i++, v += 4)
1526 VectorCopy(portal->points[i].position, v);
1527 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1530 static void R_DrawPortals(entity_render_t *ent)
1533 mportal_t *portal, *endportal;
1534 float temp[3], center[3], f;
1536 if (r_drawportals.integer < 1)
1539 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1541 if (portal->here->pvsframe == ent->model->pvsframecount || portal->past->pvsframe == ent->model->pvsframecount)
1543 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1546 for (i = 0;i < portal->numpoints;i++)
1547 VectorAdd(temp, portal->points[i].position, temp);
1548 f = ixtable[portal->numpoints];
1549 VectorScale(temp, f, temp);
1550 Matrix4x4_Transform(&ent->matrix, temp, center);
1551 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1557 void R_PrepareBrushModel(entity_render_t *ent)
1559 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1562 #if WORLDNODECULLBACKFACES
1566 // because bmodels can be reused, we have to decide which things to render
1567 // from scratch every time
1569 #if WORLDNODECULLBACKFACES
1570 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1572 numsurfaces = model->nummodelsurfaces;
1573 surf = model->surfaces + model->firstmodelsurface;
1574 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1575 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1576 for (i = 0;i < numsurfaces;i++, surf++)
1578 #if WORLDNODECULLBACKFACES
1579 // mark any backface surfaces as not visible
1580 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1582 if ((surf->flags & SURF_PLANEBACK))
1584 surfacevisframes[i] = r_framecount;
1585 surfacepvsframes[i] = model->pvsframecount;
1590 if (!(surf->flags & SURF_PLANEBACK))
1592 surfacevisframes[i] = r_framecount;
1593 surfacepvsframes[i] = model->pvsframecount;
1597 surfacevisframes[i] = r_framecount;
1598 surfacepvsframes[i] = model->pvsframecount;
1600 surf->dlightframe = -1;
1602 R_PrepareSurfaces(ent);
1605 void R_SurfaceWorldNode (entity_render_t *ent)
1607 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1614 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1615 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1616 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1618 for (leaf = model->pvsleafchain;leaf;leaf = leaf->pvschain)
1620 if (!R_CullBox (leaf->mins, leaf->maxs))
1623 leaf->visframe = r_framecount;
1627 for (i = 0;i < model->pvssurflistlength;i++)
1629 surfnum = model->pvssurflist[i];
1630 surf = model->surfaces + surfnum;
1631 #if WORLDNODECULLBACKFACES
1632 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1634 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1635 surfacevisframes[surfnum] = r_framecount;
1639 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1640 surfacevisframes[surfnum] = r_framecount;
1643 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1644 surfacevisframes[surfnum] = r_framecount;
1649 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1651 int c, leafstackpos, *mark, *surfacevisframes;
1652 #if WORLDNODECULLBACKFACES
1656 mleaf_t *leaf, *leafstack[8192];
1659 msurface_t *surfaces;
1660 // LordHavoc: portal-passage worldnode with PVS;
1661 // follows portals leading outward from viewleaf, does not venture
1662 // offscreen or into leafs that are not visible, faster than Quake's
1663 // RecursiveWorldNode
1664 surfaces = ent->model->surfaces;
1665 surfacevisframes = ent->model->surfacevisframes;
1666 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1667 viewleaf->worldnodeframe = r_framecount;
1668 leafstack[0] = viewleaf;
1670 while (leafstackpos)
1673 leaf = leafstack[--leafstackpos];
1674 leaf->visframe = r_framecount;
1675 // draw any surfaces bounding this leaf
1676 if (leaf->nummarksurfaces)
1678 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1680 #if WORLDNODECULLBACKFACES
1682 if (surfacevisframes[n] != r_framecount)
1684 surf = surfaces + n;
1685 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1687 if ((surf->flags & SURF_PLANEBACK))
1688 surfacevisframes[n] = r_framecount;
1692 if (!(surf->flags & SURF_PLANEBACK))
1693 surfacevisframes[n] = r_framecount;
1697 surfacevisframes[*mark++] = r_framecount;
1701 // follow portals into other leafs
1702 for (p = leaf->portals;p;p = p->next)
1704 // LordHavoc: this DotProduct hurts less than a cache miss
1705 // (which is more likely to happen if backflowing through leafs)
1706 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1709 if (leaf->worldnodeframe != r_framecount)
1711 leaf->worldnodeframe = r_framecount;
1712 // FIXME: R_CullBox is absolute, should be done relative
1713 if (leaf->pvsframe == ent->model->pvsframecount && !R_CullBox(leaf->mins, leaf->maxs))
1714 leafstack[leafstackpos++] = leaf;
1719 if (r_drawportals.integer)
1723 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1725 int i, j, l, c, bits, *surfacepvsframes, *mark;
1731 if (model && (model->pvsviewleaf != viewleaf || model->pvsviewleafnovis != r_novis.integer))
1733 model->pvsframecount++;
1734 model->pvsviewleaf = viewleaf;
1735 model->pvsviewleafnovis = r_novis.integer;
1736 model->pvsleafchain = NULL;
1737 model->pvssurflistlength = 0;
1740 surfacepvsframes = model->surfacepvsframes;
1741 vis = Mod_LeafPVS (viewleaf, model);
1742 for (j = 0;j < model->numleafs;j += 8)
1747 l = model->numleafs - j;
1750 for (i = 0;i < l;i++)
1752 if (bits & (1 << i))
1754 leaf = &model->leafs[j + i + 1];
1755 leaf->pvschain = model->pvsleafchain;
1756 model->pvsleafchain = leaf;
1757 leaf->pvsframe = model->pvsframecount;
1758 // mark surfaces bounding this leaf as visible
1759 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1761 //if (surfacepvsframes[*mark] != model->pvsframecount)
1763 surfacepvsframes[*mark] = model->pvsframecount;
1764 // model->pvssurflist[model->pvssurflistlength++] = *mark;
1771 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1772 if (model->surfacepvsframes[j] == model->pvsframecount)
1773 model->pvssurflist[model->pvssurflistlength++] = j;
1778 void R_WorldVisibility (entity_render_t *ent)
1783 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1784 viewleaf = Mod_PointInLeaf (modelorg, ent->model);
1785 R_PVSUpdate(ent, viewleaf);
1790 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1791 R_SurfaceWorldNode (ent);
1793 R_PortalWorldNode (ent, viewleaf);
1796 void R_DrawWorld (entity_render_t *ent)
1798 R_PrepareSurfaces(ent);
1799 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
1800 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL);
1803 void R_Model_Brush_DrawSky (entity_render_t *ent)
1805 if (ent != &cl_entities[0].render)
1806 R_PrepareBrushModel(ent);
1807 R_DrawSurfaces(ent, SHADERSTAGE_SKY);
1810 void R_Model_Brush_Draw (entity_render_t *ent)
1813 if (ent != &cl_entities[0].render)
1814 R_PrepareBrushModel(ent);
1815 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL);
1818 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1822 float projectdistance, f, temp[3], lightradius2;
1824 R_Mesh_Matrix(&ent->matrix);
1825 lightradius2 = lightradius * lightradius;
1826 R_UpdateTextureInfo(ent);
1827 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1828 for (i = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;i < ent->model->nummodelsurfaces;i++, surf++)
1830 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1832 f = PlaneDiff(relativelightorigin, surf->plane);
1833 if (surf->flags & SURF_PLANEBACK)
1835 // draw shadows only for frontfaces and only if they are close
1836 if (f >= 0.1 && f < lightradius)
1838 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1839 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1840 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1841 if (DotProduct(temp, temp) < lightradius2)
1843 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1845 R_Mesh_ResizeCheck(mesh->numverts * 2);
1846 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1847 R_Shadow_Volume(mesh->numverts, mesh->numtriangles, mesh->index, mesh->triangleneighbors, relativelightorigin, lightradius, projectdistance);
1855 void R_Model_Brush_DrawLightForSurfaceList(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, msurface_t **surflist, int numsurfaces)
1861 R_Mesh_Matrix(&ent->matrix);
1862 R_UpdateTextureInfo(ent);
1863 for (surfnum = 0;surfnum < numsurfaces;surfnum++)
1865 surf = surflist[surfnum];
1866 if (surf->visframe == r_framecount)
1868 t = surf->texinfo->texture->currentframe;
1869 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1871 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1873 R_Mesh_ResizeCheck(mesh->numverts);
1874 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1875 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->texture, t->nmaptexture, NULL);
1876 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->glosstexture, t->nmaptexture, NULL);
1883 void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor)
1888 float f, lightradius2, temp[3];
1890 R_Mesh_Matrix(&ent->matrix);
1891 lightradius2 = lightradius * lightradius;
1892 R_UpdateTextureInfo(ent);
1893 if (ent != &cl_entities[0].render)
1895 // bmodel, cull crudely to view and light
1896 for (surfnum = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;surfnum < ent->model->nummodelsurfaces;surfnum++, surf++)
1898 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1899 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1900 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1901 if (DotProduct(temp, temp) < lightradius2)
1903 f = PlaneDiff(relativelightorigin, surf->plane);
1904 if (surf->flags & SURF_PLANEBACK)
1906 if (f >= -0.1 && f < lightradius)
1908 f = PlaneDiff(relativeeyeorigin, surf->plane);
1909 if (surf->flags & SURF_PLANEBACK)
1913 t = surf->texinfo->texture->currentframe;
1914 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1916 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1918 R_Mesh_ResizeCheck(mesh->numverts);
1919 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1920 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->texture, t->nmaptexture, NULL);
1921 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->glosstexture, t->nmaptexture, NULL);
1931 // world, already culled to view, just cull to light
1932 for (surfnum = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;surfnum < ent->model->nummodelsurfaces;surfnum++, surf++)
1934 if (surf->visframe == r_framecount)
1936 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1937 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1938 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1939 if (DotProduct(temp, temp) < lightradius2)
1941 f = PlaneDiff(relativelightorigin, surf->plane);
1942 if (surf->flags & SURF_PLANEBACK)
1944 if (f >= -0.1 && f < lightradius)
1946 t = surf->texinfo->texture->currentframe;
1947 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1949 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1951 R_Mesh_ResizeCheck(mesh->numverts);
1952 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1953 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->texture, t->nmaptexture, NULL);
1954 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->glosstexture, t->nmaptexture, NULL);
1964 static void gl_surf_start(void)
1968 static void gl_surf_shutdown(void)
1972 static void gl_surf_newmap(void)
1976 void GL_Surf_Init(void)
1979 dlightdivtable[0] = 4194304;
1980 for (i = 1;i < 32768;i++)
1981 dlightdivtable[i] = 4194304 / (i << 7);
1983 Cvar_RegisterVariable(&r_ambient);
1984 Cvar_RegisterVariable(&r_vertexsurfaces);
1985 Cvar_RegisterVariable(&r_dlightmap);
1986 Cvar_RegisterVariable(&r_drawportals);
1987 Cvar_RegisterVariable(&r_testvis);
1988 Cvar_RegisterVariable(&r_floatbuildlightmap);
1989 Cvar_RegisterVariable(&r_detailtextures);
1990 Cvar_RegisterVariable(&r_surfaceworldnode);
1992 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);