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", "1"};
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)
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;
245 smax = (surf->extents[0]>>4)+1;
246 tmax = (surf->extents[1]>>4)+1;
249 lightmap = surf->samples;
251 // set to full bright if no light data
253 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
255 for (i = 0;i < size3;i++)
261 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
264 for (i = 0;i < size3;i++)
268 memset(bl, 0, size*3*sizeof(unsigned int));
270 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
272 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
273 if (surf->cached_dlight)
277 // add all the lightmaps
281 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
282 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
283 bl[i] += lightmap[i] * scale;
287 stain = surf->stainsamples;
290 // deal with lightmap brightness scale
291 shift = 7 + r_lightmapscalebit + 8;
292 if (ent->model->lightmaprgba)
294 stride = (surf->lightmaptexturestride - smax) * 4;
295 for (i = 0;i < tmax;i++, out += stride)
297 for (j = 0;j < smax;j++)
299 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
300 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
301 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
308 stride = (surf->lightmaptexturestride - smax) * 3;
309 for (i = 0;i < tmax;i++, out += stride)
311 for (j = 0;j < smax;j++)
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
315 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 R_UpdateTexture(surf->lightmaptexture, templight);
324 int smax, tmax, i, j, size, size3, maps, stride, l;
326 qbyte *lightmap, *out, *stain;
328 // update cached lighting info
329 surf->cached_dlight = 0;
331 smax = (surf->extents[0]>>4)+1;
332 tmax = (surf->extents[1]>>4)+1;
335 lightmap = surf->samples;
337 // set to full bright if no light data
338 bl = floatblocklights;
339 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
342 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
347 for (i = 0;i < size3;i++)
351 memset(bl, 0, size*3*sizeof(float));
353 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
355 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
356 if (surf->cached_dlight)
360 // add all the lightmaps
363 bl = floatblocklights;
364 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
365 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
366 bl[i] += lightmap[i] * scale;
369 stain = surf->stainsamples;
370 bl = floatblocklights;
372 // deal with lightmap brightness scale
373 scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
374 if (ent->model->lightmaprgba)
376 stride = (surf->lightmaptexturestride - smax) * 4;
377 for (i = 0;i < tmax;i++, out += stride)
379 for (j = 0;j < smax;j++)
381 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
382 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
383 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
390 stride = (surf->lightmaptexturestride - smax) * 3;
391 for (i = 0;i < tmax;i++, out += stride)
393 for (j = 0;j < smax;j++)
395 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
396 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 R_UpdateTexture(surf->lightmaptexture, templight);
406 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
408 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
409 msurface_t *surf, *endsurf;
410 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
414 maxdist = radius * radius;
415 invradius = 1.0f / radius;
418 if (node->contents < 0)
420 ndist = PlaneDiff(origin, node->plane);
423 node = node->children[0];
428 node = node->children[1];
432 dist2 = ndist * ndist;
433 maxdist3 = maxdist - dist2;
435 if (node->plane->type < 3)
437 VectorCopy(origin, impact);
438 impact[node->plane->type] -= ndist;
442 impact[0] = origin[0] - node->plane->normal[0] * ndist;
443 impact[1] = origin[1] - node->plane->normal[1] * ndist;
444 impact[2] = origin[2] - node->plane->normal[2] * ndist;
447 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
449 if (surf->stainsamples)
451 smax = (surf->extents[0] >> 4) + 1;
452 tmax = (surf->extents[1] >> 4) + 1;
454 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
455 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
457 s = bound(0, impacts, smax * 16) - impacts;
458 t = bound(0, impactt, tmax * 16) - impactt;
459 i = s * s + t * t + dist2;
463 // reduce calculations
464 for (s = 0, i = impacts; s < smax; s++, i -= 16)
465 sdtable[s] = i * i + dist2;
467 bl = surf->stainsamples;
472 for (t = 0;t < tmax;t++, i -= 16)
475 // make sure some part of it is visible on this line
478 maxdist2 = maxdist - td;
479 for (s = 0;s < smax;s++)
481 if (sdtable[s] < maxdist2)
483 ratio = lhrandom(0.0f, 1.0f);
484 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
485 if (a >= (1.0f / 64.0f))
489 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
490 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
491 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
501 // force lightmap upload
503 surf->cached_dlight = true;
507 if (node->children[0]->contents >= 0)
509 if (node->children[1]->contents >= 0)
511 R_StainNode(node->children[0], model, origin, radius, fcolor);
512 node = node->children[1];
517 node = node->children[0];
521 else if (node->children[1]->contents >= 0)
523 node = node->children[1];
528 void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
532 entity_render_t *ent;
535 if (cl.worldmodel == NULL)
540 fcolor[3] = ca1 * (1.0f / 64.0f);
541 fcolor[4] = cr2 - cr1;
542 fcolor[5] = cg2 - cg1;
543 fcolor[6] = cb2 - cb1;
544 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
546 R_StainNode(cl.worldmodel->nodes + cl.worldmodel->hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
548 // look for embedded bmodels
549 for (n = 0;n < cl_num_brushmodel_entities;n++)
551 ent = cl_brushmodel_entities[n];
553 if (model && model->name[0] == '*')
555 Mod_CheckLoaded(model);
556 if (model->type == mod_brush)
558 Matrix4x4_Transform(&ent->inversematrix, origin, org);
559 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, fcolor);
567 =============================================================
571 =============================================================
574 static void RSurf_AddLightmapToVertexColors(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
579 if (styles[0] != 255)
581 for (i = 0;i < numverts;i++, c += 4)
583 lm = samples + lightmapoffsets[i];
584 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
585 VectorMA(c, scale, lm, c);
586 if (styles[1] != 255)
589 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
590 VectorMA(c, scale, lm, c);
591 if (styles[2] != 255)
594 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
595 VectorMA(c, scale, lm, c);
596 if (styles[3] != 255)
599 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
600 VectorMA(c, scale, lm, c);
608 static void RSurf_FogColors(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
614 for (i = 0;i < numverts;i++, v += 4, c += 4)
616 VectorSubtract(v, modelorg, diff);
617 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
618 VectorScale(c, f, c);
621 else if (colorscale != 1)
622 for (i = 0;i < numverts;i++, c += 4)
623 VectorScale(c, colorscale, c);
626 static void RSurf_FoggedColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
635 for (i = 0;i < numverts;i++, v += 4, c += 4)
637 VectorSubtract(v, modelorg, diff);
638 f = 1 - exp(fogdensity/DotProduct(diff, diff));
647 for (i = 0;i < numverts;i++, c += 4)
657 static void RSurf_FogPassColors(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
664 for (i = 0;i < numverts;i++, v += 4, c += 4)
666 VectorSubtract(v, modelorg, diff);
667 f = exp(fogdensity/DotProduct(diff, diff));
675 static void RSurf_ScaleColors(float *c, float scale, int numverts)
679 for (i = 0;i < numverts;i++, c += 4)
680 VectorScale(c, scale, c);
683 static int RSurf_LightSeparate(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color)
688 int i, l, lit = false;
691 for (l = 0;l < r_numdlights;l++)
693 if (dlightbits[l >> 5] & (1 << (l & 31)))
696 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
697 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
699 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
700 if (f < rd->cullradius2)
702 f = (1.0f / f) - rd->subtract;
703 VectorMA(c, f, rd->light, c);
712 // note: this untransforms lights to do the checking,
713 // and takes surf->mesh->verts data
714 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
720 for (l = 0;l < r_numdlights;l++)
722 if (dlightbits[l >> 5] & (1 << (l & 31)))
725 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
726 for (i = 0, v = mesh->verts;i < mesh->numverts;i++, v += 4)
727 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
734 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
736 const msurface_t *surf;
737 const surfmesh_t *mesh;
740 // LordHavoc: HalfLife maps have freaky skypolys...
741 if (ent->model->ishlbsp)
746 skyrendernow = false;
751 R_Mesh_Matrix(&ent->matrix);
753 // draw depth-only polys
754 memset(&m, 0, sizeof(m));
757 qglColorMask(0,0,0,0);
758 // just to make sure that braindead drivers don't draw anything
759 // despite that colormask...
760 m.blendfunc1 = GL_ZERO;
761 m.blendfunc2 = GL_ONE;
766 m.blendfunc1 = GL_ONE;
767 m.blendfunc2 = GL_ZERO;
771 while((surf = *surfchain++) != NULL)
773 if (surf->visframe == r_framecount)
775 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
777 R_Mesh_ResizeCheck(mesh->numverts);
778 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
779 GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, 1);
780 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
784 qglColorMask(1,1,1,1);
787 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
790 const entity_render_t *ent = calldata1;
791 const msurface_t *surf = ent->model->surfaces + calldata2;
792 float f, colorscale, scroll[2], *v;
793 const surfmesh_t *mesh;
798 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
800 R_Mesh_Matrix(&ent->matrix);
802 memset(&m, 0, sizeof(m));
803 texture = surf->texinfo->texture->currentframe;
804 alpha = texture->currentalpha;
805 if (texture->rendertype == SURFRENDER_ADD)
807 m.blendfunc1 = GL_SRC_ALPHA;
808 m.blendfunc2 = GL_ONE;
810 else if (texture->rendertype == SURFRENDER_ALPHA)
812 m.blendfunc1 = GL_SRC_ALPHA;
813 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
817 m.blendfunc1 = GL_ONE;
818 m.blendfunc2 = GL_ZERO;
820 m.tex[0] = R_GetTexture(texture->skin.base);
821 colorscale = r_colorscale;
822 if (gl_combine.integer)
824 m.texrgbscale[0] = 4;
829 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
831 R_Mesh_ResizeCheck(mesh->numverts);
832 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
833 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
834 scroll[0] = sin(cl.time) * 0.125f;
835 scroll[1] = sin(cl.time * 0.8f) * 0.125f;
836 for (i = 0, v = varray_texcoord[0];i < mesh->numverts;i++, v += 4)
841 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
842 R_FillColors(varray_color, mesh->numverts, f, f, f, alpha);
843 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
845 if (surf->dlightframe == r_framecount)
846 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
847 if (surf->flags & SURF_LIGHTMAP)
848 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
850 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
851 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
856 memset(&m, 0, sizeof(m));
857 m.blendfunc1 = GL_SRC_ALPHA;
858 m.blendfunc2 = GL_ONE;
859 m.tex[0] = R_GetTexture(texture->skin.fog);
861 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
863 R_Mesh_ResizeCheck(mesh->numverts);
864 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
866 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
867 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], alpha, r_colorscale, mesh->numverts, modelorg);
868 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
873 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
875 const msurface_t *surf;
878 if (texture->rendertype != SURFRENDER_OPAQUE)
880 for (chain = surfchain;(surf = *chain) != NULL;chain++)
882 if (surf->visframe == r_framecount)
884 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
885 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->surfaces);
890 for (chain = surfchain;(surf = *chain) != NULL;chain++)
891 if (surf->visframe == r_framecount)
892 RSurfShader_Water_Callback(ent, surf - ent->model->surfaces);
895 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
897 float base, colorscale;
898 const surfmesh_t *mesh;
901 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
902 memset(&m, 0, sizeof(m));
903 if (rendertype == SURFRENDER_ADD)
905 m.blendfunc1 = GL_SRC_ALPHA;
906 m.blendfunc2 = GL_ONE;
908 else if (rendertype == SURFRENDER_ALPHA)
910 m.blendfunc1 = GL_SRC_ALPHA;
911 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
915 m.blendfunc1 = GL_ONE;
916 m.blendfunc2 = GL_ZERO;
918 m.tex[0] = R_GetTexture(texture->skin.base);
919 colorscale = r_colorscale;
920 if (gl_combine.integer)
922 m.texrgbscale[0] = 4;
925 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
928 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
930 R_Mesh_ResizeCheck(mesh->numverts);
931 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
932 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
933 R_FillColors(varray_color, mesh->numverts, base, base, base, currentalpha);
934 if (!(ent->effects & EF_FULLBRIGHT))
936 if (surf->dlightframe == r_framecount)
937 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
938 if (surf->flags & SURF_LIGHTMAP)
939 RSurf_AddLightmapToVertexColors(mesh->lightmapoffsets, varray_color, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
941 RSurf_FogColors(varray_vertex, varray_color, colorscale, mesh->numverts, modelorg);
942 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
946 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
948 const surfmesh_t *mesh;
951 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
952 memset(&m, 0, sizeof(m));
953 m.blendfunc1 = GL_SRC_ALPHA;
954 m.blendfunc2 = GL_ONE;
955 m.tex[0] = R_GetTexture(texture->skin.glow);
958 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
960 R_Mesh_ResizeCheck(mesh->numverts);
961 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
962 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
963 RSurf_FoggedColors(varray_vertex, varray_color, 1, 1, 1, currentalpha, r_colorscale, mesh->numverts, modelorg);
964 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
968 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
970 const surfmesh_t *mesh;
973 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
974 memset(&m, 0, sizeof(m));
975 m.blendfunc1 = GL_SRC_ALPHA;
976 m.blendfunc2 = GL_ONE;
977 m.tex[0] = R_GetTexture(texture->skin.fog);
980 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
982 R_Mesh_ResizeCheck(mesh->numverts);
983 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
985 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
986 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, r_colorscale, mesh->numverts, modelorg);
987 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
991 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
993 const msurface_t *surf;
994 const surfmesh_t *mesh;
996 int lightmaptexturenum;
998 memset(&m, 0, sizeof(m));
999 m.blendfunc1 = GL_ONE;
1000 m.blendfunc2 = GL_ZERO;
1001 m.tex[0] = R_GetTexture(texture->skin.base);
1002 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1003 m.tex[2] = R_GetTexture(texture->skin.detail);
1004 m.texrgbscale[0] = 1;
1005 m.texrgbscale[1] = 4;
1006 m.texrgbscale[2] = 2;
1008 cl = (float) (1 << r_lightmapscalebit) * r_colorscale;
1009 GL_Color(cl, cl, cl, 1);
1010 while((surf = *surfchain++) != NULL)
1012 if (surf->visframe == r_framecount)
1014 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1015 if (m.tex[1] != lightmaptexturenum)
1017 m.tex[1] = lightmaptexturenum;
1020 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1022 R_Mesh_ResizeCheck(mesh->numverts);
1023 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1024 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1025 memcpy(varray_texcoord[1], mesh->uvw, mesh->numverts * sizeof(float[4]));
1026 memcpy(varray_texcoord[2], mesh->abc, mesh->numverts * sizeof(float[4]));
1027 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1033 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1035 const msurface_t *surf;
1036 const surfmesh_t *mesh;
1038 int lightmaptexturenum;
1039 memset(&m, 0, sizeof(m));
1040 m.blendfunc1 = GL_ONE;
1041 m.blendfunc2 = GL_ZERO;
1042 m.tex[0] = R_GetTexture(texture->skin.base);
1043 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1044 if (gl_combine.integer)
1045 m.texrgbscale[1] = 4;
1047 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1048 while((surf = *surfchain++) != NULL)
1050 if (surf->visframe == r_framecount)
1052 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1053 if (m.tex[1] != lightmaptexturenum)
1055 m.tex[1] = lightmaptexturenum;
1058 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1060 R_Mesh_ResizeCheck(mesh->numverts);
1061 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1062 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1063 memcpy(varray_texcoord[1], mesh->uvw, mesh->numverts * sizeof(float[4]));
1064 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1070 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1072 const msurface_t *surf;
1073 const surfmesh_t *mesh;
1075 memset(&m, 0, sizeof(m));
1076 m.blendfunc1 = GL_ONE;
1077 m.blendfunc2 = GL_ZERO;
1078 m.tex[0] = R_GetTexture(texture->skin.base);
1080 GL_Color(1, 1, 1, 1);
1081 while((surf = *surfchain++) != NULL)
1083 if (surf->visframe == r_framecount)
1085 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1087 R_Mesh_ResizeCheck(mesh->numverts);
1088 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1089 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1090 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1096 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1098 const msurface_t *surf;
1099 const surfmesh_t *mesh;
1101 int lightmaptexturenum;
1102 memset(&m, 0, sizeof(m));
1103 m.blendfunc1 = GL_ZERO;
1104 m.blendfunc2 = GL_SRC_COLOR;
1105 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1106 if (gl_combine.integer)
1107 m.texrgbscale[0] = 4;
1109 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1110 while((surf = *surfchain++) != NULL)
1112 if (surf->visframe == r_framecount)
1114 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1115 if (m.tex[0] != lightmaptexturenum)
1117 m.tex[0] = lightmaptexturenum;
1120 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1122 R_Mesh_ResizeCheck(mesh->numverts);
1123 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1124 memcpy(varray_texcoord[0], mesh->uvw, mesh->numverts * sizeof(float[4]));
1125 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1131 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1133 const msurface_t *surf;
1134 const surfmesh_t *mesh;
1138 memset(&m, 0, sizeof(m));
1139 m.blendfunc1 = GL_SRC_ALPHA;
1140 m.blendfunc2 = GL_ONE;
1141 m.tex[0] = R_GetTexture(texture->skin.base);
1142 colorscale = r_colorscale;
1143 if (gl_combine.integer)
1145 m.texrgbscale[0] = 4;
1146 colorscale *= 0.25f;
1150 while((surf = *surfchain++) != NULL)
1152 if (surf->visframe == r_framecount && surf->dlightframe == r_framecount)
1154 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1156 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1158 R_Mesh_ResizeCheck(mesh->numverts);
1159 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1160 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1161 R_FillColors(varray_color, mesh->numverts, 0, 0, 0, 1);
1162 RSurf_LightSeparate(&ent->inversematrix, surf->dlightbits, mesh->numverts, varray_vertex, varray_color);
1163 RSurf_ScaleColors(varray_color, colorscale, mesh->numverts);
1164 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1171 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1173 const msurface_t *surf;
1174 const surfmesh_t *mesh;
1177 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1178 memset(&m, 0, sizeof(m));
1179 m.blendfunc1 = GL_SRC_ALPHA;
1180 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1183 while((surf = *surfchain++) != NULL)
1185 if (surf->visframe == r_framecount)
1187 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1189 R_Mesh_ResizeCheck(mesh->numverts);
1190 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1192 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1193 RSurf_FogPassColors(varray_vertex, varray_color, fogcolor[0], fogcolor[1], fogcolor[2], 1, r_colorscale, mesh->numverts, modelorg);
1194 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1200 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1202 const msurface_t *surf;
1203 const surfmesh_t *mesh;
1205 memset(&m, 0, sizeof(m));
1206 m.blendfunc1 = GL_DST_COLOR;
1207 m.blendfunc2 = GL_SRC_COLOR;
1208 m.tex[0] = R_GetTexture(texture->skin.detail);
1210 GL_Color(1, 1, 1, 1);
1211 while((surf = *surfchain++) != NULL)
1213 if (surf->visframe == r_framecount)
1215 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1217 R_Mesh_ResizeCheck(mesh->numverts);
1218 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1219 memcpy(varray_texcoord[0], mesh->abc, mesh->numverts * sizeof(float[4]));
1220 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1226 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1228 const msurface_t *surf;
1229 const surfmesh_t *mesh;
1231 memset(&m, 0, sizeof(m));
1232 m.blendfunc1 = GL_SRC_ALPHA;
1233 m.blendfunc2 = GL_ONE;
1234 m.tex[0] = R_GetTexture(texture->skin.glow);
1236 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1237 while((surf = *surfchain++) != NULL)
1239 if (surf->visframe == r_framecount)
1241 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1243 R_Mesh_ResizeCheck(mesh->numverts);
1244 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1245 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1246 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1252 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1254 const msurface_t *surf;
1255 const surfmesh_t *mesh;
1257 memset(&m, 0, sizeof(m));
1258 m.blendfunc1 = GL_SRC_ALPHA;
1259 m.blendfunc2 = GL_ZERO;
1260 m.tex[0] = R_GetTexture(texture->skin.glow);
1263 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1265 GL_Color(0, 0, 0, 1);
1266 while((surf = *surfchain++) != NULL)
1268 if (surf->visframe == r_framecount)
1270 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1272 R_Mesh_ResizeCheck(mesh->numverts);
1273 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1274 memcpy(varray_texcoord[0], mesh->str, mesh->numverts * sizeof(float[4]));
1275 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->index);
1281 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1283 const entity_render_t *ent = calldata1;
1284 const msurface_t *surf = ent->model->surfaces + calldata2;
1288 R_Mesh_Matrix(&ent->matrix);
1290 texture = surf->texinfo->texture;
1291 if (texture->animated)
1292 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];
1294 currentalpha = ent->alpha;
1295 if (texture->flags & SURF_WATERALPHA)
1296 currentalpha *= r_wateralpha.value;
1297 if (ent->effects & EF_ADDITIVE)
1298 rendertype = SURFRENDER_ADD;
1299 else if (currentalpha < 1 || texture->skin.fog != NULL)
1300 rendertype = SURFRENDER_ALPHA;
1302 rendertype = SURFRENDER_OPAQUE;
1304 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1305 if (texture->skin.glow)
1306 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1308 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1311 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1313 const msurface_t *surf;
1316 if (texture->rendertype != SURFRENDER_OPAQUE)
1318 // transparent vertex shaded from lightmap
1319 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1321 if (surf->visframe == r_framecount)
1323 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1324 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->surfaces);
1328 else if (r_shadow_lightingmode >= 2)
1330 // opaque base lighting
1331 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1333 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1335 else if (r_vertexsurfaces.integer)
1337 // opaque vertex shaded from lightmap
1338 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1339 if (surf->visframe == r_framecount)
1340 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, texture->rendertype, texture->currentalpha);
1341 if (texture->skin.glow)
1342 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1343 if (surf->visframe == r_framecount)
1344 RSurfShader_Wall_Pass_Glow(ent, surf, texture, texture->rendertype, texture->currentalpha);
1346 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1347 if (surf->visframe == r_framecount)
1348 RSurfShader_Wall_Pass_Fog(ent, surf, texture, texture->rendertype, texture->currentalpha);
1352 // opaque lightmapped
1353 if (r_textureunits.integer >= 2)
1355 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1356 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1359 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture, surfchain);
1360 if (r_detailtextures.integer)
1361 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1366 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1367 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1368 if (r_detailtextures.integer)
1369 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1371 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1372 RSurfShader_OpaqueWall_Pass_Light(ent, texture, surfchain);
1373 if (texture->skin.glow)
1374 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1376 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1380 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1381 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1382 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1384 int Cshader_count = 3;
1385 Cshader_t *Cshaders[3] =
1387 &Cshader_wall_lightmap,
1392 void R_UpdateTextureInfo(entity_render_t *ent)
1394 int i, texframe, alttextures;
1400 alttextures = ent->frame != 0;
1401 texframe = (int)(cl.time * 5.0f);
1402 for (i = 0;i < ent->model->numtextures;i++)
1404 t = ent->model->textures + i;
1405 t->currentalpha = ent->alpha;
1406 if (t->flags & SURF_WATERALPHA)
1407 t->currentalpha *= r_wateralpha.value;
1408 if (ent->effects & EF_ADDITIVE)
1409 t->rendertype = SURFRENDER_ADD;
1410 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1411 t->rendertype = SURFRENDER_ALPHA;
1413 t->rendertype = SURFRENDER_OPAQUE;
1414 // we don't need to set currentframe if t->animated is false because
1415 // it was already set up by the texture loader for non-animating
1417 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1421 void R_PrepareSurfaces(entity_render_t *ent)
1423 int i, numsurfaces, *surfacevisframes;
1425 msurface_t *surf, *surfaces, **surfchain;
1432 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1433 numsurfaces = model->nummodelsurfaces;
1434 surfaces = model->surfaces + model->firstmodelsurface;
1435 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1437 R_UpdateTextureInfo(ent);
1439 if (r_dynamic.integer && r_shadow_lightingmode < 1)
1442 if (model->light_ambient != r_ambient.value || model->light_scalebit != r_lightmapscalebit)
1444 model->light_ambient = r_ambient.value;
1445 model->light_scalebit = r_lightmapscalebit;
1446 for (i = 0;i < model->nummodelsurfaces;i++)
1447 model->surfaces[i + model->firstmodelsurface].cached_dlight = true;
1451 for (i = 0;i < model->light_styles;i++)
1453 if (model->light_stylevalue[i] != d_lightstylevalue[model->light_style[i]])
1455 model->light_stylevalue[i] = d_lightstylevalue[model->light_style[i]];
1456 for (surfchain = model->light_styleupdatechains[i];*surfchain;surfchain++)
1457 (**surfchain).cached_dlight = true;
1462 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1464 if (surfacevisframes[i] == r_framecount)
1466 #if !WORLDNODECULLBACKFACES
1467 // mark any backface surfaces as not visible
1468 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1470 if (!(surf->flags & SURF_PLANEBACK))
1471 surfacevisframes[i] = -1;
1475 if ((surf->flags & SURF_PLANEBACK))
1476 surfacevisframes[i] = -1;
1478 if (surfacevisframes[i] == r_framecount)
1482 surf->visframe = r_framecount;
1483 if (surf->cached_dlight && surf->lightmaptexture != NULL && !r_vertexsurfaces.integer)
1484 R_BuildLightMap(ent, surf);
1490 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1494 if (ent->model == NULL)
1496 R_Mesh_Matrix(&ent->matrix);
1497 for (i = 0, t = ent->model->textures;i < ent->model->numtextures;i++, t++)
1498 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1499 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1502 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1507 const entity_render_t *ent = calldata1;
1508 const mportal_t *portal = ent->model->portals + calldata2;
1509 memset(&m, 0, sizeof(m));
1510 m.blendfunc1 = GL_SRC_ALPHA;
1511 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1512 R_Mesh_Matrix(&ent->matrix);
1514 R_Mesh_ResizeCheck(portal->numpoints);
1515 i = portal - ent->model->portals;
1516 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_colorscale,
1517 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_colorscale,
1518 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_colorscale,
1520 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1522 for (i = portal->numpoints - 1, v = varray_vertex;i >= 0;i--, v += 4)
1523 VectorCopy(portal->points[i].position, v);
1526 for (i = 0, v = varray_vertex;i < portal->numpoints;i++, v += 4)
1527 VectorCopy(portal->points[i].position, v);
1528 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1531 static void R_DrawPortals(entity_render_t *ent)
1534 mportal_t *portal, *endportal;
1535 float temp[3], center[3], f;
1536 if (ent->model == NULL)
1538 for (portal = ent->model->portals, endportal = portal + ent->model->numportals;portal < endportal;portal++)
1540 if ((portal->here->pvsframe == ent->model->pvsframecount || portal->past->pvsframe == ent->model->pvsframecount) && portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1543 for (i = 0;i < portal->numpoints;i++)
1544 VectorAdd(temp, portal->points[i].position, temp);
1545 f = ixtable[portal->numpoints];
1546 VectorScale(temp, f, temp);
1547 Matrix4x4_Transform(&ent->matrix, temp, center);
1548 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->portals);
1553 void R_PrepareBrushModel(entity_render_t *ent)
1555 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1558 #if WORLDNODECULLBACKFACES
1562 // because bmodels can be reused, we have to decide which things to render
1563 // from scratch every time
1567 #if WORLDNODECULLBACKFACES
1568 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1570 numsurfaces = model->nummodelsurfaces;
1571 surf = model->surfaces + model->firstmodelsurface;
1572 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1573 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1574 for (i = 0;i < numsurfaces;i++, surf++)
1576 #if WORLDNODECULLBACKFACES
1577 // mark any backface surfaces as not visible
1578 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1580 if ((surf->flags & SURF_PLANEBACK))
1581 surfacevisframes[i] = r_framecount;
1583 else if (!(surf->flags & SURF_PLANEBACK))
1584 surfacevisframes[i] = r_framecount;
1586 surfacevisframes[i] = r_framecount;
1588 surf->dlightframe = -1;
1590 R_PrepareSurfaces(ent);
1593 void R_SurfaceWorldNode (entity_render_t *ent)
1595 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1601 // equivilant to quake's RecursiveWorldNode but faster and more effective
1605 surfacevisframes = model->surfacevisframes + model->firstmodelsurface;
1606 surfacepvsframes = model->surfacepvsframes + model->firstmodelsurface;
1607 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1609 for (leaf = model->pvsleafchain;leaf;leaf = leaf->pvschain)
1611 if (!R_CullBox (leaf->mins, leaf->maxs))
1614 leaf->visframe = r_framecount;
1618 for (i = 0;i < model->pvssurflistlength;i++)
1620 surfnum = model->pvssurflist[i];
1621 surf = model->surfaces + surfnum;
1622 #if WORLDNODECULLBACKFACES
1623 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1625 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1626 surfacevisframes[surfnum] = r_framecount;
1630 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1631 surfacevisframes[surfnum] = r_framecount;
1634 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1635 surfacevisframes[surfnum] = r_framecount;
1640 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1642 int c, leafstackpos, *mark, *surfacevisframes;
1643 #if WORLDNODECULLBACKFACES
1647 mleaf_t *leaf, *leafstack[8192];
1650 msurface_t *surfaces;
1651 if (ent->model == NULL)
1653 // LordHavoc: portal-passage worldnode with PVS;
1654 // follows portals leading outward from viewleaf, does not venture
1655 // offscreen or into leafs that are not visible, faster than Quake's
1656 // RecursiveWorldNode
1657 surfaces = ent->model->surfaces;
1658 surfacevisframes = ent->model->surfacevisframes;
1659 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1660 viewleaf->worldnodeframe = r_framecount;
1661 leafstack[0] = viewleaf;
1663 while (leafstackpos)
1666 leaf = leafstack[--leafstackpos];
1667 leaf->visframe = r_framecount;
1668 // draw any surfaces bounding this leaf
1669 if (leaf->nummarksurfaces)
1671 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1673 #if WORLDNODECULLBACKFACES
1675 if (surfacevisframes[n] != r_framecount)
1677 surf = surfaces + n;
1678 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1680 if ((surf->flags & SURF_PLANEBACK))
1681 surfacevisframes[n] = r_framecount;
1685 if (!(surf->flags & SURF_PLANEBACK))
1686 surfacevisframes[n] = r_framecount;
1690 surfacevisframes[*mark++] = r_framecount;
1694 // follow portals into other leafs
1695 for (p = leaf->portals;p;p = p->next)
1697 // LordHavoc: this DotProduct hurts less than a cache miss
1698 // (which is more likely to happen if backflowing through leafs)
1699 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1702 if (leaf->worldnodeframe != r_framecount)
1704 leaf->worldnodeframe = r_framecount;
1705 // FIXME: R_CullBox is absolute, should be done relative
1706 if (leaf->pvsframe == ent->model->pvsframecount && !R_CullBox(leaf->mins, leaf->maxs))
1707 leafstack[leafstackpos++] = leaf;
1714 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1716 int i, j, l, c, bits, *surfacepvsframes, *mark;
1722 if (model && (model->pvsviewleaf != viewleaf || model->pvsviewleafnovis != r_novis.integer))
1724 model->pvsframecount++;
1725 model->pvsviewleaf = viewleaf;
1726 model->pvsviewleafnovis = r_novis.integer;
1727 model->pvsleafchain = NULL;
1728 model->pvssurflistlength = 0;
1731 surfacepvsframes = model->surfacepvsframes;
1732 vis = Mod_LeafPVS (viewleaf, model);
1733 for (j = 0;j < model->numleafs;j += 8)
1738 l = model->numleafs - j;
1741 for (i = 0;i < l;i++)
1743 if (bits & (1 << i))
1745 leaf = &model->leafs[j + i + 1];
1746 leaf->pvschain = model->pvsleafchain;
1747 model->pvsleafchain = leaf;
1748 leaf->pvsframe = model->pvsframecount;
1749 // mark surfaces bounding this leaf as visible
1750 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1751 surfacepvsframes[*mark] = model->pvsframecount;
1756 Mod_BuildPVSTextureChains(model);
1761 void R_WorldVisibility (entity_render_t *ent)
1766 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1767 viewleaf = Mod_PointInLeaf (modelorg, ent->model);
1768 R_PVSUpdate(ent, viewleaf);
1773 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1774 R_SurfaceWorldNode (ent);
1776 R_PortalWorldNode (ent, viewleaf);
1779 void R_DrawWorld (entity_render_t *ent)
1781 if (ent->model == NULL)
1783 R_PrepareSurfaces(ent);
1784 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->pvstexturechains);
1785 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->pvstexturechains);
1786 if (r_drawportals.integer)
1790 void R_Model_Brush_DrawSky (entity_render_t *ent)
1792 if (ent->model == NULL)
1794 if (ent != &cl_entities[0].render)
1795 R_PrepareBrushModel(ent);
1796 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->pvstexturechains);
1799 void R_Model_Brush_Draw (entity_render_t *ent)
1801 if (ent->model == NULL)
1804 if (ent != &cl_entities[0].render)
1805 R_PrepareBrushModel(ent);
1806 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->pvstexturechains);
1809 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1813 float projectdistance, f, temp[3], lightradius2;
1815 if (ent->model == NULL)
1817 R_Mesh_Matrix(&ent->matrix);
1818 lightradius2 = lightradius * lightradius;
1819 R_UpdateTextureInfo(ent);
1820 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1821 for (i = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;i < ent->model->nummodelsurfaces;i++, surf++)
1823 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1825 f = PlaneDiff(relativelightorigin, surf->plane);
1826 if (surf->flags & SURF_PLANEBACK)
1828 // draw shadows only for frontfaces and only if they are close
1829 if (f >= 0.1 && f < lightradius)
1831 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1832 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1833 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1834 if (DotProduct(temp, temp) < lightradius2)
1836 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1838 R_Mesh_ResizeCheck(mesh->numverts * 2);
1839 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1840 R_Shadow_Volume(mesh->numverts, mesh->numtriangles, mesh->index, mesh->triangleneighbors, relativelightorigin, lightradius, projectdistance);
1848 void R_Model_Brush_DrawLightForSurfaceList(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, msurface_t **surflist, int numsurfaces)
1854 if (ent->model == NULL)
1856 R_Mesh_Matrix(&ent->matrix);
1857 R_UpdateTextureInfo(ent);
1858 for (surfnum = 0;surfnum < numsurfaces;surfnum++)
1860 surf = surflist[surfnum];
1861 if (surf->visframe == r_framecount)
1863 t = surf->texinfo->texture->currentframe;
1864 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1866 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1868 R_Mesh_ResizeCheck(mesh->numverts);
1869 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1870 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->skin.base, t->skin.nmap, NULL);
1871 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->skin.gloss, t->skin.nmap, NULL);
1878 void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor)
1883 float f, lightradius2, temp[3];
1885 if (ent->model == NULL)
1887 R_Mesh_Matrix(&ent->matrix);
1888 lightradius2 = lightradius * lightradius;
1889 R_UpdateTextureInfo(ent);
1890 if (ent != &cl_entities[0].render)
1892 // bmodel, cull crudely to view and light
1893 for (surfnum = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;surfnum < ent->model->nummodelsurfaces;surfnum++, surf++)
1895 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1896 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1897 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1898 if (DotProduct(temp, temp) < lightradius2)
1900 f = PlaneDiff(relativelightorigin, surf->plane);
1901 if (surf->flags & SURF_PLANEBACK)
1903 if (f >= -0.1 && f < lightradius)
1905 f = PlaneDiff(relativeeyeorigin, surf->plane);
1906 if (surf->flags & SURF_PLANEBACK)
1910 t = surf->texinfo->texture->currentframe;
1911 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1913 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1915 R_Mesh_ResizeCheck(mesh->numverts);
1916 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1917 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->skin.base, t->skin.nmap, NULL);
1918 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->skin.gloss, t->skin.nmap, NULL);
1928 // world, already culled to view, just cull to light
1929 for (surfnum = 0, surf = ent->model->surfaces + ent->model->firstmodelsurface;surfnum < ent->model->nummodelsurfaces;surfnum++, surf++)
1931 if (surf->visframe == r_framecount)
1933 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1934 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1935 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1936 if (DotProduct(temp, temp) < lightradius2)
1938 f = PlaneDiff(relativelightorigin, surf->plane);
1939 if (surf->flags & SURF_PLANEBACK)
1941 if (f >= -0.1 && f < lightradius)
1943 t = surf->texinfo->texture->currentframe;
1944 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1946 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1948 R_Mesh_ResizeCheck(mesh->numverts);
1949 memcpy(varray_vertex, mesh->verts, mesh->numverts * sizeof(float[4]));
1950 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, lightradius, lightcolor, t->skin.base, t->skin.nmap, NULL);
1951 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->index, mesh->svectors, mesh->tvectors, mesh->normals, mesh->str, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, t->skin.gloss, t->skin.nmap, NULL);
1961 static void gl_surf_start(void)
1965 static void gl_surf_shutdown(void)
1969 static void gl_surf_newmap(void)
1973 void GL_Surf_Init(void)
1976 dlightdivtable[0] = 4194304;
1977 for (i = 1;i < 32768;i++)
1978 dlightdivtable[i] = 4194304 / (i << 7);
1980 Cvar_RegisterVariable(&r_ambient);
1981 Cvar_RegisterVariable(&r_vertexsurfaces);
1982 Cvar_RegisterVariable(&r_dlightmap);
1983 Cvar_RegisterVariable(&r_drawportals);
1984 Cvar_RegisterVariable(&r_testvis);
1985 Cvar_RegisterVariable(&r_floatbuildlightmap);
1986 Cvar_RegisterVariable(&r_detailtextures);
1987 Cvar_RegisterVariable(&r_surfaceworldnode);
1989 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);