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_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
40 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
43 // FIXME: these arrays are huge!
44 int r_q1bsp_maxmarkleafs;
45 int r_q1bsp_nummarkleafs;
46 mleaf_t *r_q1bsp_maxleaflist[65536];
47 int r_q1bsp_maxmarksurfaces;
48 int r_q1bsp_nummarksurfaces;
49 msurface_t *r_q1bsp_maxsurfacelist[65536];
51 // FIXME: these arrays are huge!
52 int r_q3bsp_maxmarkleafs;
53 int r_q3bsp_nummarkleafs;
54 q3mleaf_t *r_q3bsp_maxleaflist[65536];
55 int r_q3bsp_maxmarksurfaces;
56 int r_q3bsp_nummarksurfaces;
57 q3msurface_t *r_q3bsp_maxsurfacelist[65536];
60 static int dlightdivtable[32768];
62 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
64 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
66 float dist, impact[3], local[3];
71 smax = (surf->extents[0] >> 4) + 1;
72 tmax = (surf->extents[1] >> 4) + 1;
75 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
77 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
78 continue; // not lit by this light
80 Matrix4x4_Transform(matrix, light->origin, local);
81 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
83 // for comparisons to minimum acceptable light
84 // compensate for LIGHTOFFSET
85 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
92 if (surf->plane->type < 3)
94 VectorCopy(local, impact);
95 impact[surf->plane->type] -= dist;
99 impact[0] = local[0] - surf->plane->normal[0] * dist;
100 impact[1] = local[1] - surf->plane->normal[1] * dist;
101 impact[2] = local[2] - surf->plane->normal[2] * dist;
104 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
105 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
107 s = bound(0, impacts, smax * 16) - impacts;
108 t = bound(0, impactt, tmax * 16) - impactt;
109 i = s * s + t * t + dist2;
113 // reduce calculations
114 for (s = 0, i = impacts; s < smax; s++, i -= 16)
115 sdtable[s] = i * i + dist2;
117 maxdist3 = maxdist - dist2;
119 // convert to 8.8 blocklights format
120 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
121 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
122 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
123 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
127 for (t = 0;t < tmax;t++, i -= 16)
130 // make sure some part of it is visible on this line
133 maxdist2 = maxdist - td;
134 for (s = 0;s < smax;s++)
136 if (sdtable[s] < maxdist2)
138 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
142 bl[1] += (green * k);
157 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
159 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
160 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
165 smax = (surf->extents[0] >> 4) + 1;
166 tmax = (surf->extents[1] >> 4) + 1;
169 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
171 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
172 continue; // not lit by this light
174 Matrix4x4_Transform(matrix, light->origin, local);
175 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
177 // for comparisons to minimum acceptable light
178 // compensate for LIGHTOFFSET
179 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
182 dist2 += LIGHTOFFSET;
183 if (dist2 >= maxdist)
186 if (surf->plane->type < 3)
188 VectorCopy(local, impact);
189 impact[surf->plane->type] -= dist;
193 impact[0] = local[0] - surf->plane->normal[0] * dist;
194 impact[1] = local[1] - surf->plane->normal[1] * dist;
195 impact[2] = local[2] - surf->plane->normal[2] * dist;
198 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
199 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
201 td = bound(0, impacts, smax * 16) - impacts;
202 td1 = bound(0, impactt, tmax * 16) - impactt;
203 td = td * td + td1 * td1 + dist2;
207 // reduce calculations
208 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
209 sdtable[s] = td1 * td1 + dist2;
211 maxdist3 = maxdist - dist2;
213 // convert to 8.8 blocklights format
214 red = light->rtlight.lightmap_light[0];
215 green = light->rtlight.lightmap_light[1];
216 blue = light->rtlight.lightmap_light[2];
217 subtract = light->rtlight.lightmap_subtract * 32768.0f;
218 bl = floatblocklights;
221 for (t = 0;t < tmax;t++, td1 -= 16.0f)
224 // make sure some part of it is visible on this line
227 maxdist2 = maxdist - td;
228 for (s = 0;s < smax;s++)
230 if (sdtable[s] < maxdist2)
232 k = (32768.0f / (sdtable[s] + td)) - subtract;
252 Combine and scale multiple lightmaps into the 8.8 format in blocklights
255 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
257 if (!r_floatbuildlightmap.integer)
259 int smax, tmax, i, j, size, size3, maps, stride, l;
260 unsigned int *bl, scale;
261 qbyte *lightmap, *out, *stain;
263 // update cached lighting info
264 surf->cached_dlight = 0;
266 smax = (surf->extents[0]>>4)+1;
267 tmax = (surf->extents[1]>>4)+1;
270 lightmap = surf->samples;
272 // set to full bright if no light data
274 if (!ent->model->brushq1.lightdata)
276 for (i = 0;i < size3;i++)
282 memset(bl, 0, size*3*sizeof(unsigned int));
284 if (surf->dlightframe == r_framecount)
286 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
287 if (surf->cached_dlight)
291 // add all the lightmaps
295 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
296 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
297 bl[i] += lightmap[i] * scale;
301 stain = surf->stainsamples;
304 // the >> 16 shift adjusts down 8 bits to account for the stainmap
305 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
306 // be doubled during rendering to achieve 2x overbright
307 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
308 if (ent->model->brushq1.lightmaprgba)
310 stride = (surf->lightmaptexturestride - smax) * 4;
311 for (i = 0;i < tmax;i++, out += stride)
313 for (j = 0;j < smax;j++)
315 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
316 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
317 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
324 stride = (surf->lightmaptexturestride - smax) * 3;
325 for (i = 0;i < tmax;i++, out += stride)
327 for (j = 0;j < smax;j++)
329 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
330 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
331 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
336 R_UpdateTexture(surf->lightmaptexture, templight);
340 int smax, tmax, i, j, size, size3, maps, stride, l;
342 qbyte *lightmap, *out, *stain;
344 // update cached lighting info
345 surf->cached_dlight = 0;
347 smax = (surf->extents[0]>>4)+1;
348 tmax = (surf->extents[1]>>4)+1;
351 lightmap = surf->samples;
353 // set to full bright if no light data
354 bl = floatblocklights;
355 if (!ent->model->brushq1.lightdata)
357 for (i = 0;i < size3;i++)
362 memset(bl, 0, size*3*sizeof(float));
364 if (surf->dlightframe == r_framecount)
366 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
367 if (surf->cached_dlight)
371 // add all the lightmaps
374 bl = floatblocklights;
375 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
376 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
377 bl[i] += lightmap[i] * scale;
381 stain = surf->stainsamples;
382 bl = floatblocklights;
384 // this scaling adjusts down 8 bits to account for the stainmap
385 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
386 // be doubled during rendering to achieve 2x overbright
387 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
388 scale = 1.0f / (1 << 16);
389 if (ent->model->brushq1.lightmaprgba)
391 stride = (surf->lightmaptexturestride - smax) * 4;
392 for (i = 0;i < tmax;i++, out += stride)
394 for (j = 0;j < smax;j++)
396 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
405 stride = (surf->lightmaptexturestride - smax) * 3;
406 for (i = 0;i < tmax;i++, out += stride)
408 for (j = 0;j < smax;j++)
410 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
412 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 R_UpdateTexture(surf->lightmaptexture, templight);
421 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
423 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
424 msurface_t *surf, *endsurf;
425 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
429 maxdist = radius * radius;
430 invradius = 1.0f / radius;
433 if (node->contents < 0)
435 ndist = PlaneDiff(origin, node->plane);
438 node = node->children[0];
443 node = node->children[1];
447 dist2 = ndist * ndist;
448 maxdist3 = maxdist - dist2;
450 if (node->plane->type < 3)
452 VectorCopy(origin, impact);
453 impact[node->plane->type] -= ndist;
457 impact[0] = origin[0] - node->plane->normal[0] * ndist;
458 impact[1] = origin[1] - node->plane->normal[1] * ndist;
459 impact[2] = origin[2] - node->plane->normal[2] * ndist;
462 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
464 if (surf->stainsamples)
466 smax = (surf->extents[0] >> 4) + 1;
467 tmax = (surf->extents[1] >> 4) + 1;
469 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
470 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
472 s = bound(0, impacts, smax * 16) - impacts;
473 t = bound(0, impactt, tmax * 16) - impactt;
474 i = s * s + t * t + dist2;
478 // reduce calculations
479 for (s = 0, i = impacts; s < smax; s++, i -= 16)
480 sdtable[s] = i * i + dist2;
482 bl = surf->stainsamples;
487 for (t = 0;t < tmax;t++, i -= 16)
490 // make sure some part of it is visible on this line
493 maxdist2 = maxdist - td;
494 for (s = 0;s < smax;s++)
496 if (sdtable[s] < maxdist2)
498 ratio = lhrandom(0.0f, 1.0f);
499 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
500 if (a >= (1.0f / 64.0f))
504 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
505 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
506 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
516 // force lightmap upload
518 surf->cached_dlight = true;
522 if (node->children[0]->contents >= 0)
524 if (node->children[1]->contents >= 0)
526 R_StainNode(node->children[0], model, origin, radius, fcolor);
527 node = node->children[1];
532 node = node->children[0];
536 else if (node->children[1]->contents >= 0)
538 node = node->children[1];
543 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)
547 entity_render_t *ent;
550 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
555 fcolor[3] = ca1 * (1.0f / 64.0f);
556 fcolor[4] = cr2 - cr1;
557 fcolor[5] = cg2 - cg1;
558 fcolor[6] = cb2 - cb1;
559 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
561 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
563 // look for embedded bmodels
564 for (n = 0;n < cl_num_brushmodel_entities;n++)
566 ent = cl_brushmodel_entities[n];
568 if (model && model->name[0] == '*')
570 Mod_CheckLoaded(model);
571 if (model->brushq1.nodes)
573 Matrix4x4_Transform(&ent->inversematrix, origin, org);
574 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
582 =============================================================
586 =============================================================
589 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
594 if (styles[0] != 255)
596 for (i = 0;i < numverts;i++, c += 4)
598 lm = samples + lightmapoffsets[i];
599 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
600 VectorMA(c, scale, lm, c);
601 if (styles[1] != 255)
604 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
605 VectorMA(c, scale, lm, c);
606 if (styles[2] != 255)
609 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
610 VectorMA(c, scale, lm, c);
611 if (styles[3] != 255)
614 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
615 VectorMA(c, scale, lm, c);
623 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
629 for (i = 0;i < numverts;i++, v += 3, c += 4)
631 VectorSubtract(v, modelorg, diff);
632 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
633 VectorScale(c, f, c);
636 else if (colorscale != 1)
637 for (i = 0;i < numverts;i++, c += 4)
638 VectorScale(c, colorscale, c);
641 static void RSurf_FoggedColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
650 for (i = 0;i < numverts;i++, v += 3, c += 4)
652 VectorSubtract(v, modelorg, diff);
653 f = 1 - exp(fogdensity/DotProduct(diff, diff));
662 for (i = 0;i < numverts;i++, c += 4)
672 static void RSurf_FogPassColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
679 for (i = 0;i < numverts;i++, v += 3, c += 4)
681 VectorSubtract(v, modelorg, diff);
682 f = exp(fogdensity/DotProduct(diff, diff));
690 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
695 int i, l, lit = false;
696 const dlight_t *light;
698 for (l = 0;l < r_numdlights;l++)
700 if (dlightbits[l >> 5] & (1 << (l & 31)))
702 light = &r_dlight[l];
703 Matrix4x4_Transform(matrix, light->origin, lightorigin);
704 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
706 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
707 if (f < light->rtlight.lightmap_cullradius2)
709 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
710 VectorMA(c, f, light->rtlight.lightmap_light, c);
719 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
721 const msurface_t *surf;
724 // LordHavoc: HalfLife maps have freaky skypolys...
725 if (ent->model->brush.ishlbsp)
730 skyrendernow = false;
735 R_Mesh_Matrix(&ent->matrix);
737 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
740 // depth-only (masking)
741 GL_ColorMask(0,0,0,0);
742 // just to make sure that braindead drivers don't draw anything
743 // despite that colormask...
744 GL_BlendFunc(GL_ZERO, GL_ONE);
749 GL_BlendFunc(GL_ONE, GL_ZERO);
754 memset(&m, 0, sizeof(m));
755 while((surf = *surfchain++) != NULL)
757 if (surf->visframe == r_framecount)
759 m.pointer_vertex = surf->mesh.data_vertex3f;
761 GL_LockArrays(0, surf->mesh.num_vertices);
762 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
766 GL_ColorMask(1,1,1,1);
769 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
771 const entity_render_t *ent = calldata1;
772 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
775 float base, colorscale;
778 float args[4] = {0.05f,0,0,0.04f};
779 int rendertype, turb, fullbright;
781 R_Mesh_Matrix(&ent->matrix);
782 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
784 texture = surf->texinfo->texture;
785 if (texture->animated)
786 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];
787 currentalpha = ent->alpha;
788 if (surf->flags & SURF_WATERALPHA)
789 currentalpha *= r_wateralpha.value;
792 if (ent->effects & EF_ADDITIVE)
794 rendertype = SURFRENDER_ADD;
795 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
798 else if (currentalpha < 1 || texture->skin.fog != NULL)
800 rendertype = SURFRENDER_ALPHA;
801 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
806 rendertype = SURFRENDER_OPAQUE;
807 GL_BlendFunc(GL_ONE, GL_ZERO);
811 turb = (surf->flags & SURF_DRAWTURB) && r_waterscroll.value;
812 fullbright = (ent->effects & EF_FULLBRIGHT) || (surf->flags & SURF_DRAWFULLBRIGHT) || !surf->samples;
813 base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
814 if (surf->flags & SURF_DRAWTURB)
816 if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled)
818 // NVIDIA Geforce3 distortion texture shader on water
819 GL_Color(1, 1, 1, currentalpha);
820 memset(&m, 0, sizeof(m));
821 m.pointer_vertex = surf->mesh.data_vertex3f;
822 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
823 m.tex[1] = R_GetTexture(texture->skin.base);
824 m.texcombinergb[0] = GL_REPLACE;
825 m.texcombinergb[1] = GL_REPLACE;
826 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
827 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
828 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
829 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
833 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
835 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
836 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
837 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
838 qglEnable(GL_TEXTURE_SHADER_NV);
840 GL_LockArrays(0, surf->mesh.num_vertices);
841 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
844 qglDisable(GL_TEXTURE_SHADER_NV);
845 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
850 memset(&m, 0, sizeof(m));
851 m.pointer_vertex = surf->mesh.data_vertex3f;
852 m.pointer_color = varray_color4f;
853 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
854 m.tex[0] = R_GetTexture(texture->skin.base);
857 // scrolling in texture matrix
858 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
861 if (gl_combine.integer)
863 m.texrgbscale[0] = 4;
866 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
869 if (surf->dlightframe == r_framecount)
870 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
872 RSurf_AddLightmapToVertexColors_Color4f(surf->mesh.data_lightmapoffsets, varray_color4f,surf->mesh.num_vertices, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
874 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
876 GL_LockArrays(0, surf->mesh.num_vertices);
877 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
879 if (texture->skin.glow)
881 memset(&m, 0, sizeof(m));
882 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
884 m.pointer_color = varray_color4f;
885 m.tex[0] = R_GetTexture(texture->skin.glow);
886 m.pointer_vertex = surf->mesh.data_vertex3f;
889 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
892 // scrolling in texture matrix
893 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
897 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
898 GL_LockArrays(0, surf->mesh.num_vertices);
899 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
902 if (fogenabled && rendertype != SURFRENDER_ADD)
904 memset(&m, 0, sizeof(m));
905 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
907 m.pointer_color = varray_color4f;
908 m.tex[0] = R_GetTexture(texture->skin.fog);
909 m.pointer_vertex = surf->mesh.data_vertex3f;
912 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
915 // scrolling in texture matrix
916 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
920 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
921 GL_LockArrays(0, surf->mesh.num_vertices);
922 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
928 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
930 const msurface_t *surf;
932 int lightmaptexturenum;
933 memset(&m, 0, sizeof(m));
934 GL_BlendFunc(GL_ONE, GL_ZERO);
937 m.tex[0] = R_GetTexture(texture->skin.base);
938 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
939 m.texrgbscale[1] = 2;
940 m.tex[2] = R_GetTexture(texture->skin.detail);
941 m.texrgbscale[2] = 2;
942 if (texture->skin.glow)
944 m.tex[3] = R_GetTexture(texture->skin.glow);
945 m.texcombinergb[3] = GL_ADD;
947 if (r_shadow_realtime_world.integer)
948 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
950 GL_Color(1, 1, 1, 1);
952 while((surf = *surfchain++) != NULL)
954 if (surf->visframe == r_framecount)
956 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
957 m.tex[1] = lightmaptexturenum;
958 m.pointer_vertex = surf->mesh.data_vertex3f;
959 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
960 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
961 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
962 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
964 GL_LockArrays(0, surf->mesh.num_vertices);
965 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
971 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
973 const msurface_t *surf;
975 int lightmaptexturenum;
976 memset(&m, 0, sizeof(m));
977 GL_BlendFunc(GL_ONE, GL_ZERO);
980 m.tex[0] = R_GetTexture(texture->skin.base);
981 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
982 m.texrgbscale[1] = 2;
983 m.tex[2] = R_GetTexture(texture->skin.detail);
984 m.texrgbscale[2] = 2;
985 if (r_shadow_realtime_world.integer)
986 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
988 GL_Color(1, 1, 1, 1);
990 while((surf = *surfchain++) != NULL)
992 if (surf->visframe == r_framecount)
994 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
995 m.tex[1] = lightmaptexturenum;
996 m.pointer_vertex = surf->mesh.data_vertex3f;
997 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
998 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
999 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1001 GL_LockArrays(0, surf->mesh.num_vertices);
1002 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1003 GL_LockArrays(0, 0);
1008 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1010 const msurface_t *surf;
1012 int lightmaptexturenum;
1013 memset(&m, 0, sizeof(m));
1014 GL_BlendFunc(GL_ONE, GL_ZERO);
1017 m.tex[0] = R_GetTexture(texture->skin.base);
1018 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1019 m.texrgbscale[1] = 2;
1020 if (r_shadow_realtime_world.integer)
1021 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1023 GL_Color(1, 1, 1, 1);
1024 while((surf = *surfchain++) != NULL)
1026 if (surf->visframe == r_framecount)
1028 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1029 m.tex[1] = lightmaptexturenum;
1030 m.pointer_vertex = surf->mesh.data_vertex3f;
1031 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1032 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1034 GL_LockArrays(0, surf->mesh.num_vertices);
1035 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1036 GL_LockArrays(0, 0);
1041 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1043 const msurface_t *surf;
1045 memset(&m, 0, sizeof(m));
1048 GL_BlendFunc(GL_ONE, GL_ZERO);
1049 m.tex[0] = R_GetTexture(texture->skin.base);
1050 if (r_shadow_realtime_world.integer)
1051 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1053 GL_Color(1, 1, 1, 1);
1054 while((surf = *surfchain++) != NULL)
1056 if (surf->visframe == r_framecount)
1058 m.pointer_vertex = surf->mesh.data_vertex3f;
1059 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1061 GL_LockArrays(0, surf->mesh.num_vertices);
1062 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1063 GL_LockArrays(0, 0);
1068 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1070 const msurface_t *surf;
1072 int lightmaptexturenum;
1073 memset(&m, 0, sizeof(m));
1074 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1075 GL_DepthMask(false);
1077 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1078 GL_Color(1, 1, 1, 1);
1079 while((surf = *surfchain++) != NULL)
1081 if (surf->visframe == r_framecount)
1083 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1084 m.tex[0] = lightmaptexturenum;
1085 m.pointer_vertex = surf->mesh.data_vertex3f;
1086 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1088 GL_LockArrays(0, surf->mesh.num_vertices);
1089 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1090 GL_LockArrays(0, 0);
1095 static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1097 const msurface_t *surf;
1099 memset(&m, 0, sizeof(m));
1100 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1101 GL_DepthMask(false);
1103 m.tex[0] = R_GetTexture(texture->skin.base);
1104 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1105 while((surf = *surfchain++) != NULL)
1107 if (surf->visframe == r_framecount)
1109 m.pointer_vertex = surf->mesh.data_vertex3f;
1110 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1112 GL_LockArrays(0, surf->mesh.num_vertices);
1113 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1114 GL_LockArrays(0, 0);
1119 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1121 const msurface_t *surf;
1124 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1125 memset(&m, 0, sizeof(m));
1126 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1127 GL_DepthMask(false);
1129 m.pointer_color = varray_color4f;
1130 while((surf = *surfchain++) != NULL)
1132 if (surf->visframe == r_framecount)
1134 m.pointer_vertex = surf->mesh.data_vertex3f;
1136 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1138 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1139 GL_LockArrays(0, surf->mesh.num_vertices);
1140 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1141 GL_LockArrays(0, 0);
1146 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1148 const msurface_t *surf;
1150 memset(&m, 0, sizeof(m));
1151 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1152 GL_DepthMask(false);
1154 m.tex[0] = R_GetTexture(texture->skin.detail);
1155 GL_Color(1, 1, 1, 1);
1156 while((surf = *surfchain++) != NULL)
1158 if (surf->visframe == r_framecount)
1160 m.pointer_vertex = surf->mesh.data_vertex3f;
1161 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1163 GL_LockArrays(0, surf->mesh.num_vertices);
1164 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1165 GL_LockArrays(0, 0);
1170 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1172 const msurface_t *surf;
1174 memset(&m, 0, sizeof(m));
1175 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1176 GL_DepthMask(false);
1178 m.tex[0] = R_GetTexture(texture->skin.glow);
1179 GL_Color(1, 1, 1, 1);
1180 while((surf = *surfchain++) != NULL)
1182 if (surf->visframe == r_framecount)
1184 m.pointer_vertex = surf->mesh.data_vertex3f;
1185 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1187 GL_LockArrays(0, surf->mesh.num_vertices);
1188 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1189 GL_LockArrays(0, 0);
1195 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1197 const msurface_t *surf;
1199 memset(&m, 0, sizeof(m));
1200 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1202 m.tex[0] = R_GetTexture(texture->skin.glow);
1204 GL_Color(1, 1, 1, 1);
1206 GL_Color(0, 0, 0, 1);
1207 while((surf = *surfchain++) != NULL)
1209 if (surf->visframe == r_framecount)
1211 m.pointer_vertex = surf->mesh.data_vertex3f;
1212 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1214 GL_LockArrays(0, surf->mesh.num_vertices);
1215 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1216 GL_LockArrays(0, 0);
1222 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1224 const msurface_t *surf;
1226 int lightmaptexturenum;
1227 memset(&m, 0, sizeof(m));
1228 GL_BlendFunc(GL_ONE, GL_ZERO);
1231 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1232 if (r_shadow_realtime_world.integer)
1233 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1235 GL_Color(1, 1, 1, 1);
1236 while((surf = *surfchain++) != NULL)
1238 if (surf->visframe == r_framecount)
1240 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1241 m.tex[0] = lightmaptexturenum;
1242 m.pointer_vertex = surf->mesh.data_vertex3f;
1243 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1245 GL_LockArrays(0, surf->mesh.num_vertices);
1246 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1247 GL_LockArrays(0, 0);
1252 void R_UpdateTextureInfo(entity_render_t *ent)
1254 int i, texframe, alttextures;
1260 alttextures = ent->frame != 0;
1261 texframe = (int)(cl.time * 5.0f);
1262 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1264 t = ent->model->brushq1.textures + i;
1265 t->currentalpha = ent->alpha;
1266 if (t->flags & SURF_WATERALPHA)
1267 t->currentalpha *= r_wateralpha.value;
1268 if (ent->effects & EF_ADDITIVE)
1269 t->rendertype = SURFRENDER_ADD;
1270 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1271 t->rendertype = SURFRENDER_ALPHA;
1273 t->rendertype = SURFRENDER_OPAQUE;
1274 // we don't need to set currentframe if t->animated is false because
1275 // it was already set up by the texture loader for non-animating
1277 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1281 void R_UpdateLightmapInfo(entity_render_t *ent)
1284 msurface_t *surface, **surfacechain;
1285 model_t *model = ent->model;
1288 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1290 for (i = 0;i < model->brushq1.light_styles;i++)
1292 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1294 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1295 for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1296 surface->cached_dlight = true;
1301 void R_PrepareSurfaces(entity_render_t *ent)
1303 int i, numsurfaces, *surfacevisframes;
1305 msurface_t *surf, *surfaces;
1312 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1313 numsurfaces = model->nummodelsurfaces;
1314 surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1315 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1316 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1318 if (surfacevisframes[i] == r_framecount)
1320 #if !WORLDNODECULLBACKFACES
1321 // mark any backface surfaces as not visible
1322 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1324 if (!(surf->flags & SURF_PLANEBACK))
1325 surfacevisframes[i] = -1;
1329 if ((surf->flags & SURF_PLANEBACK))
1330 surfacevisframes[i] = -1;
1332 if (surfacevisframes[i] == r_framecount)
1336 surf->visframe = r_framecount;
1337 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1338 R_BuildLightMap(ent, surf);
1344 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1346 int texturenumber, f;
1348 msurface_t *surf, **chain, **surfchain;
1349 texture_t *t, *texture;
1350 model_t *model = ent->model;
1353 R_Mesh_Matrix(&ent->matrix);
1354 for (texturenumber = 0, t = model->brushq1.textures;texturenumber < model->brushq1.numtextures;texturenumber++, t++)
1356 if ((f = t->flags & flagsmask) && (texture = t->currentframe) && (surfchain = model->brushq1.pvstexturechains[texturenumber]) != NULL)
1358 if (texture->flags & SURF_LIGHTMAP)
1360 if (gl_lightmaps.integer)
1362 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1364 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1366 else if (texture->rendertype != SURFRENDER_OPAQUE)
1368 // transparent vertex shaded from lightmap
1369 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1371 if (surf->visframe == r_framecount)
1373 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1374 R_MeshQueue_AddTransparent(center, RSurfShader_Transparent_Callback, ent, surf - ent->model->brushq1.surfaces);
1380 if (ent->effects & EF_FULLBRIGHT || r_fullbright.integer)
1382 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1383 if (r_detailtextures.integer)
1384 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1385 if (texture->skin.glow)
1386 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1388 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1389 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1392 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1393 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1396 if (r_textureunits.integer >= 2 && gl_combine.integer)
1397 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1400 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1401 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1403 if (r_ambient.value > 0)
1404 RSurfShader_OpaqueWall_Pass_BaseAmbient(ent, texture, surfchain);
1405 if (r_detailtextures.integer)
1406 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1408 if (texture->skin.glow)
1409 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1412 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1415 else if (texture->flags & SURF_DRAWTURB)
1417 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1419 if (surf->visframe == r_framecount)
1421 if (texture->rendertype == SURFRENDER_OPAQUE)
1422 RSurfShader_Transparent_Callback(ent, surf - ent->model->brushq1.surfaces);
1425 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1426 R_MeshQueue_AddTransparent(center, RSurfShader_Transparent_Callback, ent, surf - ent->model->brushq1.surfaces);
1431 else if (texture->flags & SURF_DRAWSKY)
1432 RSurfShader_Sky(ent, texture, surfchain);
1437 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1442 const entity_render_t *ent = calldata1;
1443 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1444 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1445 GL_DepthMask(false);
1447 R_Mesh_Matrix(&ent->matrix);
1449 memset(&m, 0, sizeof(m));
1450 m.pointer_vertex = varray_vertex3f;
1453 i = portal - ent->model->brushq1.portals;
1454 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1455 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1456 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1458 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1460 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1461 VectorCopy(portal->points[i].position, v);
1464 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1465 VectorCopy(portal->points[i].position, v);
1466 GL_LockArrays(0, portal->numpoints);
1467 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1468 GL_LockArrays(0, 0);
1471 // LordHavoc: this is just a nice debugging tool, very slow
1472 static void R_DrawPortals(entity_render_t *ent)
1475 mportal_t *portal, *endportal;
1476 float temp[3], center[3], f;
1477 if (ent->model == NULL)
1479 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1481 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1484 for (i = 0;i < portal->numpoints;i++)
1485 VectorAdd(temp, portal->points[i].position, temp);
1486 f = ixtable[portal->numpoints];
1487 VectorScale(temp, f, temp);
1488 Matrix4x4_Transform(&ent->matrix, temp, center);
1489 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1494 void R_PrepareBrushModel(entity_render_t *ent)
1496 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1499 #if WORLDNODECULLBACKFACES
1503 // because bmodels can be reused, we have to decide which things to render
1504 // from scratch every time
1508 #if WORLDNODECULLBACKFACES
1509 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1511 numsurfaces = model->nummodelsurfaces;
1512 surf = model->brushq1.surfaces + model->firstmodelsurface;
1513 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1514 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1515 for (i = 0;i < numsurfaces;i++, surf++)
1517 #if WORLDNODECULLBACKFACES
1518 // mark any backface surfaces as not visible
1519 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1521 if ((surf->flags & SURF_PLANEBACK))
1522 surfacevisframes[i] = r_framecount;
1524 else if (!(surf->flags & SURF_PLANEBACK))
1525 surfacevisframes[i] = r_framecount;
1527 surfacevisframes[i] = r_framecount;
1529 surf->dlightframe = -1;
1531 R_UpdateTextureInfo(ent);
1532 R_UpdateLightmapInfo(ent);
1535 void R_SurfaceWorldNode (entity_render_t *ent)
1537 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1543 // equivilant to quake's RecursiveWorldNode but faster and more effective
1547 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1548 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1549 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1551 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1553 if (!R_CullBox (leaf->mins, leaf->maxs))
1556 leaf->visframe = r_framecount;
1560 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1562 surfnum = model->brushq1.pvssurflist[i];
1563 surf = model->brushq1.surfaces + surfnum;
1564 #if WORLDNODECULLBACKFACES
1565 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1567 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1568 surfacevisframes[surfnum] = r_framecount;
1572 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1573 surfacevisframes[surfnum] = r_framecount;
1576 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1577 surfacevisframes[surfnum] = r_framecount;
1582 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1584 int c, leafstackpos, *mark, *surfacevisframes;
1585 #if WORLDNODECULLBACKFACES
1589 mleaf_t *leaf, *leafstack[8192];
1592 msurface_t *surfaces;
1593 if (ent->model == NULL)
1595 // LordHavoc: portal-passage worldnode with PVS;
1596 // follows portals leading outward from viewleaf, does not venture
1597 // offscreen or into leafs that are not visible, faster than Quake's
1598 // RecursiveWorldNode
1599 surfaces = ent->model->brushq1.surfaces;
1600 surfacevisframes = ent->model->brushq1.surfacevisframes;
1601 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1602 viewleaf->worldnodeframe = r_framecount;
1603 leafstack[0] = viewleaf;
1605 while (leafstackpos)
1608 leaf = leafstack[--leafstackpos];
1609 leaf->visframe = r_framecount;
1610 // draw any surfaces bounding this leaf
1611 if (leaf->nummarksurfaces)
1613 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1615 #if WORLDNODECULLBACKFACES
1617 if (surfacevisframes[n] != r_framecount)
1619 surf = surfaces + n;
1620 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1622 if ((surf->flags & SURF_PLANEBACK))
1623 surfacevisframes[n] = r_framecount;
1627 if (!(surf->flags & SURF_PLANEBACK))
1628 surfacevisframes[n] = r_framecount;
1632 surfacevisframes[*mark++] = r_framecount;
1636 // follow portals into other leafs
1637 for (p = leaf->portals;p;p = p->next)
1639 // LordHavoc: this DotProduct hurts less than a cache miss
1640 // (which is more likely to happen if backflowing through leafs)
1641 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1644 if (leaf->worldnodeframe != r_framecount)
1646 leaf->worldnodeframe = r_framecount;
1647 // FIXME: R_CullBox is absolute, should be done relative
1648 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1649 leafstack[leafstackpos++] = leaf;
1656 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1658 int j, c, *surfacepvsframes, *mark;
1663 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1665 model->brushq1.pvsframecount++;
1666 model->brushq1.pvsviewleaf = viewleaf;
1667 model->brushq1.pvsviewleafnovis = r_novis.integer;
1668 model->brushq1.pvsleafchain = NULL;
1669 model->brushq1.pvssurflistlength = 0;
1672 surfacepvsframes = model->brushq1.surfacepvsframes;
1673 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1675 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1677 leaf->pvsframe = model->brushq1.pvsframecount;
1678 leaf->pvschain = model->brushq1.pvsleafchain;
1679 model->brushq1.pvsleafchain = leaf;
1680 // mark surfaces bounding this leaf as visible
1681 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1682 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1685 model->brushq1.BuildPVSTextureChains(model);
1690 void R_WorldVisibility(entity_render_t *ent)
1695 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1696 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1697 R_PVSUpdate(ent, viewleaf);
1702 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1703 R_SurfaceWorldNode (ent);
1705 R_PortalWorldNode (ent, viewleaf);
1707 if (r_drawportals.integer)
1711 void R_Q1BSP_DrawSky(entity_render_t *ent)
1713 if (ent->model == NULL)
1715 if (ent != &cl_entities[0].render)
1716 R_PrepareBrushModel(ent);
1717 R_PrepareSurfaces(ent);
1718 R_DrawSurfaces(ent, SURF_DRAWSKY);
1721 void R_Q1BSP_Draw(entity_render_t *ent)
1723 if (ent->model == NULL)
1726 if (ent != &cl_entities[0].render)
1727 R_PrepareBrushModel(ent);
1728 R_PrepareSurfaces(ent);
1729 R_UpdateTextureInfo(ent);
1730 R_UpdateLightmapInfo(ent);
1731 R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1734 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
1736 model_t *model = ent->model;
1737 vec3_t lightmins, lightmaxs;
1738 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1741 msurface_t *surface;
1744 lightmins[0] = relativelightorigin[0] - lightradius;
1745 lightmins[1] = relativelightorigin[1] - lightradius;
1746 lightmins[2] = relativelightorigin[2] - lightradius;
1747 lightmaxs[0] = relativelightorigin[0] + lightradius;
1748 lightmaxs[1] = relativelightorigin[1] + lightradius;
1749 lightmaxs[2] = relativelightorigin[2] + lightradius;
1750 *outnumclusterspointer = 0;
1751 *outnumsurfacespointer = 0;
1752 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1753 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1756 VectorCopy(lightmins, outmins);
1757 VectorCopy(lightmaxs, outmaxs);
1760 VectorCopy(relativelightorigin, outmins);
1761 VectorCopy(relativelightorigin, outmaxs);
1762 if (model->brush.GetPVS)
1763 pvs = model->brush.GetPVS(model, relativelightorigin);
1766 // FIXME: use BSP recursion as lights are often small
1767 for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1769 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1771 outmins[0] = min(outmins[0], leaf->mins[0]);
1772 outmins[1] = min(outmins[1], leaf->mins[1]);
1773 outmins[2] = min(outmins[2], leaf->mins[2]);
1774 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1775 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1776 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1777 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1779 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1780 outclusterlist[outnumclusters++] = leaf->clusterindex;
1782 for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1784 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1785 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1787 surface = model->brushq1.surfaces + surfaceindex;
1788 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1790 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1792 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1793 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1794 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1795 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1797 SETPVSBIT(outsurfacepvs, surfaceindex);
1798 outsurfacelist[outnumsurfaces++] = surfaceindex;
1808 // limit combined leaf box to light boundaries
1809 outmins[0] = max(outmins[0], lightmins[0]);
1810 outmins[1] = max(outmins[1], lightmins[1]);
1811 outmins[2] = max(outmins[2], lightmins[2]);
1812 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1813 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1814 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1816 *outnumclusterspointer = outnumclusters;
1817 *outnumsurfacespointer = outnumsurfaces;
1820 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1822 model_t *model = ent->model;
1823 vec3_t lightmins, lightmaxs;
1824 msurface_t *surface;
1825 int surfacelistindex, j, t;
1828 if (r_drawcollisionbrushes.integer < 2)
1830 lightmins[0] = relativelightorigin[0] - lightradius;
1831 lightmins[1] = relativelightorigin[1] - lightradius;
1832 lightmins[2] = relativelightorigin[2] - lightradius;
1833 lightmaxs[0] = relativelightorigin[0] + lightradius;
1834 lightmaxs[1] = relativelightorigin[1] + lightradius;
1835 lightmaxs[2] = relativelightorigin[2] + lightradius;
1836 R_Mesh_Matrix(&ent->matrix);
1837 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1838 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1840 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1841 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1843 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1844 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1845 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1846 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1847 shadowmarklist[numshadowmark++] = t;
1850 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
1854 void R_Q1BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist)
1856 model_t *model = ent->model;
1857 vec3_t lightmins, lightmaxs, modelorg;
1858 msurface_t *surface;
1860 int surfacelistindex;
1861 if (r_drawcollisionbrushes.integer < 2)
1863 lightmins[0] = relativelightorigin[0] - lightradius;
1864 lightmins[1] = relativelightorigin[1] - lightradius;
1865 lightmins[2] = relativelightorigin[2] - lightradius;
1866 lightmaxs[0] = relativelightorigin[0] + lightradius;
1867 lightmaxs[1] = relativelightorigin[1] + lightradius;
1868 lightmaxs[2] = relativelightorigin[2] + lightradius;
1869 R_Mesh_Matrix(&ent->matrix);
1870 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1871 R_UpdateTextureInfo(ent);
1872 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1874 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1875 if (r_shadow_compilingrtlight)
1877 // if compiling an rtlight, capture the mesh
1878 t = surface->texinfo->texture;
1879 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1880 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texinfo->texture->skin.base, surface->texinfo->texture->skin.gloss, surface->texinfo->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
1882 else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1884 t = surface->texinfo->texture->currentframe;
1885 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1886 R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
1892 void R_DrawCollisionBrush(colbrushf_t *brush)
1896 memset(&m, 0, sizeof(m));
1897 m.pointer_vertex = brush->points->v;
1899 i = ((int)brush) / sizeof(colbrushf_t);
1900 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1901 GL_LockArrays(0, brush->numpoints);
1902 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1903 GL_LockArrays(0, 0);
1906 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1910 if (!face->num_collisiontriangles)
1912 memset(&m, 0, sizeof(m));
1913 m.pointer_vertex = face->data_collisionvertex3f;
1915 i = ((int)face) / sizeof(q3msurface_t);
1916 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1917 GL_LockArrays(0, face->num_collisionvertices);
1918 R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1919 GL_LockArrays(0, 0);
1922 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1925 if (!face->num_triangles)
1930 skyrendernow = false;
1931 if (skyrendermasked)
1935 R_Mesh_Matrix(&ent->matrix);
1937 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1938 if (skyrendermasked)
1940 // depth-only (masking)
1941 GL_ColorMask(0,0,0,0);
1942 // just to make sure that braindead drivers don't draw anything
1943 // despite that colormask...
1944 GL_BlendFunc(GL_ZERO, GL_ONE);
1949 GL_BlendFunc(GL_ONE, GL_ZERO);
1954 memset(&m, 0, sizeof(m));
1955 m.pointer_vertex = face->data_vertex3f;
1958 GL_LockArrays(0, face->num_vertices);
1959 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1960 GL_LockArrays(0, 0);
1961 GL_ColorMask(1,1,1,1);
1964 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
1967 memset(&m, 0, sizeof(m));
1968 GL_BlendFunc(GL_ONE, GL_ZERO);
1971 if (face->texture->skin.glow)
1973 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1974 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1975 GL_Color(1, 1, 1, 1);
1978 GL_Color(0, 0, 0, 1);
1979 m.pointer_vertex = face->data_vertex3f;
1981 GL_LockArrays(0, face->num_vertices);
1982 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1983 GL_LockArrays(0, 0);
1986 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
1989 memset(&m, 0, sizeof(m));
1990 GL_BlendFunc(GL_ONE, GL_ZERO);
1993 m.tex[0] = R_GetTexture(face->texture->skin.base);
1994 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1995 m.tex[1] = R_GetTexture(face->lightmaptexture);
1996 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1997 m.texrgbscale[1] = 2;
1998 GL_Color(1, 1, 1, 1);
1999 m.pointer_vertex = face->data_vertex3f;
2001 GL_LockArrays(0, face->num_vertices);
2002 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2003 GL_LockArrays(0, 0);
2006 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2009 memset(&m, 0, sizeof(m));
2010 GL_BlendFunc(GL_ONE, GL_ZERO);
2013 m.tex[0] = R_GetTexture(face->texture->skin.base);
2014 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2015 GL_Color(1, 1, 1, 1);
2016 m.pointer_vertex = face->data_vertex3f;
2018 GL_LockArrays(0, face->num_vertices);
2019 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2020 GL_LockArrays(0, 0);
2023 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2026 memset(&m, 0, sizeof(m));
2027 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2028 GL_DepthMask(false);
2030 m.tex[0] = R_GetTexture(face->lightmaptexture);
2031 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2032 GL_Color(1, 1, 1, 1);
2033 m.pointer_vertex = face->data_vertex3f;
2035 GL_LockArrays(0, face->num_vertices);
2036 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2037 GL_LockArrays(0, 0);
2040 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2043 memset(&m, 0, sizeof(m));
2044 GL_BlendFunc(GL_ONE, GL_ZERO);
2047 m.tex[0] = R_GetTexture(face->lightmaptexture);
2048 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2049 if (r_shadow_realtime_world.integer)
2050 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2052 GL_Color(1, 1, 1, 1);
2053 m.pointer_vertex = face->data_vertex3f;
2055 GL_LockArrays(0, face->num_vertices);
2056 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2057 GL_LockArrays(0, 0);
2060 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
2063 memset(&m, 0, sizeof(m));
2064 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2065 GL_DepthMask(false);
2067 if (face->texture->skin.glow)
2069 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2070 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2071 GL_Color(1, 1, 1, 1);
2074 GL_Color(0, 0, 0, 1);
2075 m.pointer_vertex = face->data_vertex3f;
2077 GL_LockArrays(0, face->num_vertices);
2078 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2079 GL_LockArrays(0, 0);
2082 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
2087 memset(&m, 0, sizeof(m));
2088 GL_BlendFunc(GL_ONE, GL_ZERO);
2091 m.tex[0] = R_GetTexture(face->texture->skin.base);
2092 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2094 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2095 mul *= r_shadow_realtime_world_lightmaps.value;
2096 if (mul == 2 && gl_combine.integer)
2098 m.texrgbscale[0] = 2;
2099 m.pointer_color = face->data_color4f;
2102 m.pointer_color = face->data_color4f;
2105 for (i = 0;i < face->num_vertices;i++)
2107 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2108 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2109 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2110 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2112 m.pointer_color = varray_color4f;
2114 m.pointer_vertex = face->data_vertex3f;
2116 GL_LockArrays(0, face->num_vertices);
2117 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2118 GL_LockArrays(0, 0);
2121 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2126 memset(&m, 0, sizeof(m));
2127 GL_BlendFunc(GL_ONE, GL_ZERO);
2131 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2132 mul *= r_shadow_realtime_world_lightmaps.value;
2134 m.pointer_color = face->data_color4f;
2137 for (i = 0;i < face->num_vertices;i++)
2139 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2140 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2141 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2142 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2144 m.pointer_color = varray_color4f;
2146 m.pointer_vertex = face->data_vertex3f;
2148 GL_LockArrays(0, face->num_vertices);
2149 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2150 GL_LockArrays(0, 0);
2153 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2156 memset(&m, 0, sizeof(m));
2157 GL_BlendFunc(GL_ONE, GL_ONE);
2160 m.tex[0] = R_GetTexture(face->texture->skin.base);
2161 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2162 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2163 m.pointer_vertex = face->data_vertex3f;
2165 GL_LockArrays(0, face->num_vertices);
2166 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2167 GL_LockArrays(0, 0);
2170 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2172 const entity_render_t *ent = voident;
2173 q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2175 R_Mesh_Matrix(&ent->matrix);
2176 memset(&m, 0, sizeof(m));
2177 if (ent->effects & EF_ADDITIVE)
2178 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2180 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2181 GL_DepthMask(false);
2183 m.tex[0] = R_GetTexture(face->texture->skin.base);
2184 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2185 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2186 if (gl_combine.integer)
2188 m.texrgbscale[0] = 2;
2189 if (r_textureunits.integer >= 2)
2191 m.tex[1] = R_GetTexture(face->lightmaptexture);
2192 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2193 GL_Color(1, 1, 1, ent->alpha);
2197 if (ent->alpha == 1)
2198 m.pointer_color = face->data_color4f;
2202 for (i = 0;i < face->num_vertices;i++)
2204 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2205 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2206 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2207 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2209 m.pointer_color = varray_color4f;
2216 for (i = 0;i < face->num_vertices;i++)
2218 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2219 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2220 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2221 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2223 m.pointer_color = varray_color4f;
2225 m.pointer_vertex = face->data_vertex3f;
2227 qglDisable(GL_CULL_FACE);
2228 GL_LockArrays(0, face->num_vertices);
2229 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2230 GL_LockArrays(0, 0);
2231 qglEnable(GL_CULL_FACE);
2234 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2236 if (!face->num_triangles)
2238 if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2241 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2243 vec3_t facecenter, center;
2244 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2245 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2246 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2247 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2248 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2251 R_Mesh_Matrix(&ent->matrix);
2252 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2253 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2254 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2256 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2257 if (face->texture->skin.glow)
2258 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2260 else if (face->lightmaptexture)
2262 if (gl_lightmaps.integer)
2263 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2266 if (r_textureunits.integer >= 2 && gl_combine.integer)
2267 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2270 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2271 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2273 if (face->texture->skin.glow)
2274 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2279 if (gl_lightmaps.integer)
2280 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2283 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2284 if (face->texture->skin.glow)
2285 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2288 if (r_ambient.value)
2289 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2292 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2298 if (R_CullBox(node->mins, node->maxs))
2303 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2304 node = node->children[1];
2306 leaf = (q3mleaf_t *)node;
2307 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2310 for (i = 0;i < leaf->numleaffaces;i++)
2311 leaf->firstleafface[i]->visframe = r_framecount;
2315 // FIXME: num_leafs needs to be recalculated at load time to include only
2316 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2317 // the submodels (which would render the submodels occasionally, as part of
2318 // the world - not good)
2319 void R_Q3BSP_MarkLeafPVS(void)
2323 for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2325 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2328 for (i = 0;i < leaf->numleaffaces;i++)
2329 leaf->firstleafface[i]->visframe = r_framecount;
2334 static int r_q3bsp_framecount = -1;
2336 void R_Q3BSP_DrawSky(entity_render_t *ent)
2342 R_Mesh_Matrix(&ent->matrix);
2344 if (r_drawcollisionbrushes.integer < 2)
2346 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2347 if (ent == &cl_entities[0].render)
2349 if (r_q3bsp_framecount != r_framecount)
2351 r_q3bsp_framecount = r_framecount;
2352 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2353 //R_Q3BSP_MarkLeafPVS();
2355 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2356 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2357 R_Q3BSP_DrawSkyFace(ent, face);
2360 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2361 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2362 R_Q3BSP_DrawSkyFace(ent, face);
2366 void R_Q3BSP_Draw(entity_render_t *ent)
2373 R_Mesh_Matrix(&ent->matrix);
2375 if (r_drawcollisionbrushes.integer < 2)
2377 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2378 if (ent == &cl_entities[0].render)
2380 if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2381 if (r_q3bsp_framecount != r_framecount)
2383 r_q3bsp_framecount = r_framecount;
2384 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2385 //R_Q3BSP_MarkLeafPVS();
2387 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2388 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2389 R_Q3BSP_DrawFace(ent, face);
2392 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2393 R_Q3BSP_DrawFace(ent, face);
2395 if (r_drawcollisionbrushes.integer >= 1)
2397 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2398 GL_DepthMask(false);
2400 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2401 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2402 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2403 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2404 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2405 if (face->num_collisiontriangles)
2406 R_Q3BSP_DrawCollisionFace(ent, face);
2407 qglPolygonOffset(0, 0);
2411 void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
2413 model_t *model = ent->model;
2414 vec3_t lightmins, lightmaxs;
2415 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2418 q3msurface_t *surface;
2421 lightmins[0] = relativelightorigin[0] - lightradius;
2422 lightmins[1] = relativelightorigin[1] - lightradius;
2423 lightmins[2] = relativelightorigin[2] - lightradius;
2424 lightmaxs[0] = relativelightorigin[0] + lightradius;
2425 lightmaxs[1] = relativelightorigin[1] + lightradius;
2426 lightmaxs[2] = relativelightorigin[2] + lightradius;
2427 *outnumclusterspointer = 0;
2428 *outnumsurfacespointer = 0;
2429 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2430 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2433 VectorCopy(lightmins, outmins);
2434 VectorCopy(lightmaxs, outmaxs);
2437 VectorCopy(relativelightorigin, outmins);
2438 VectorCopy(relativelightorigin, outmaxs);
2439 if (model->brush.GetPVS)
2440 pvs = model->brush.GetPVS(model, relativelightorigin);
2443 // FIXME: use BSP recursion as lights are often small
2444 for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2446 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2448 outmins[0] = min(outmins[0], leaf->mins[0]);
2449 outmins[1] = min(outmins[1], leaf->mins[1]);
2450 outmins[2] = min(outmins[2], leaf->mins[2]);
2451 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2452 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2453 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2454 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2456 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2457 outclusterlist[outnumclusters++] = leaf->clusterindex;
2459 for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2461 surface = leaf->firstleafface[marksurfaceindex];
2462 surfaceindex = surface - model->brushq3.data_faces;
2463 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2465 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2467 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2469 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2470 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2471 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2472 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2474 SETPVSBIT(outsurfacepvs, surfaceindex);
2475 outsurfacelist[outnumsurfaces++] = surfaceindex;
2485 // limit combined leaf box to light boundaries
2486 outmins[0] = max(outmins[0], lightmins[0]);
2487 outmins[1] = max(outmins[1], lightmins[1]);
2488 outmins[2] = max(outmins[2], lightmins[2]);
2489 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2490 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2491 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2493 *outnumclusterspointer = outnumclusters;
2494 *outnumsurfacespointer = outnumsurfaces;
2497 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2499 model_t *model = ent->model;
2500 vec3_t lightmins, lightmaxs;
2501 q3msurface_t *surface;
2502 int surfacelistindex, j, t;
2505 if (r_drawcollisionbrushes.integer < 2)
2507 lightmins[0] = relativelightorigin[0] - lightradius;
2508 lightmins[1] = relativelightorigin[1] - lightradius;
2509 lightmins[2] = relativelightorigin[2] - lightradius;
2510 lightmaxs[0] = relativelightorigin[0] + lightradius;
2511 lightmaxs[1] = relativelightorigin[1] + lightradius;
2512 lightmaxs[2] = relativelightorigin[2] + lightradius;
2513 R_Mesh_Matrix(&ent->matrix);
2514 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2515 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2517 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2518 // FIXME: check some manner of face->rendermode here?
2519 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2521 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2523 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2524 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2525 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2526 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2527 shadowmarklist[numshadowmark++] = t;
2531 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
2535 void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist)
2537 model_t *model = ent->model;
2538 vec3_t lightmins, lightmaxs, modelorg;
2539 q3msurface_t *surface;
2540 int surfacelistindex;
2541 if (r_drawcollisionbrushes.integer < 2)
2543 lightmins[0] = relativelightorigin[0] - lightradius;
2544 lightmins[1] = relativelightorigin[1] - lightradius;
2545 lightmins[2] = relativelightorigin[2] - lightradius;
2546 lightmaxs[0] = relativelightorigin[0] + lightradius;
2547 lightmaxs[1] = relativelightorigin[1] + lightradius;
2548 lightmaxs[2] = relativelightorigin[2] + lightradius;
2549 R_Mesh_Matrix(&ent->matrix);
2550 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2551 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2553 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2554 if (r_shadow_compilingrtlight)
2556 // if compiling an rtlight, capture the mesh
2557 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texture->skin.base, surface->texture->skin.gloss, surface->texture->skin.nmap, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, surface->num_triangles, surface->data_element3i);
2559 else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2560 R_Shadow_RenderLighting(surface->num_vertices, surface->num_triangles, surface->data_element3i, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
2565 static void gl_surf_start(void)
2569 static void gl_surf_shutdown(void)
2573 static void gl_surf_newmap(void)
2577 void GL_Surf_Init(void)
2580 dlightdivtable[0] = 4194304;
2581 for (i = 1;i < 32768;i++)
2582 dlightdivtable[i] = 4194304 / (i << 7);
2584 Cvar_RegisterVariable(&r_ambient);
2585 Cvar_RegisterVariable(&r_drawportals);
2586 Cvar_RegisterVariable(&r_testvis);
2587 Cvar_RegisterVariable(&r_floatbuildlightmap);
2588 Cvar_RegisterVariable(&r_detailtextures);
2589 Cvar_RegisterVariable(&r_surfaceworldnode);
2590 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2591 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2592 Cvar_RegisterVariable(&gl_lightmaps);
2594 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);