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_Water_Callback(const void *calldata1, int calldata2)
771 const entity_render_t *ent = calldata1;
772 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
777 float args[4] = {0.05f,0,0,0.04f};
780 R_Mesh_Matrix(&ent->matrix);
781 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
783 texture = surf->texinfo->texture->currentframe;
784 currentalpha = texture->currentalpha;
785 if (ent->effects & EF_ADDITIVE)
787 rendertype = SURFRENDER_ADD;
788 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
791 else if (currentalpha < 1 || texture->skin.fog != NULL)
793 rendertype = SURFRENDER_ALPHA;
794 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
799 rendertype = SURFRENDER_OPAQUE;
800 GL_BlendFunc(GL_ONE, GL_ZERO);
804 GL_Color(1, 1, 1, currentalpha);
805 memset(&m, 0, sizeof(m));
806 m.pointer_vertex = surf->mesh.data_vertex3f;
807 if (gl_textureshader && r_watershader.value && !fogenabled)
809 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
810 m.tex[1] = R_GetTexture(texture->skin.base);
811 m.texcombinergb[0] = GL_REPLACE;
812 m.texcombinergb[1] = GL_REPLACE;
813 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
814 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
815 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
816 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
820 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
822 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
823 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
824 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
825 qglEnable(GL_TEXTURE_SHADER_NV);
827 GL_LockArrays(0, surf->mesh.num_vertices);
828 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
831 qglDisable(GL_TEXTURE_SHADER_NV);
832 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
837 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
838 m.pointer_color = varray_color4f;
839 m.tex[0] = R_GetTexture(texture->skin.base);
840 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
841 if (r_waterscroll.value)
843 // scrolling in texture matrix
844 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
847 GL_LockArrays(0, surf->mesh.num_vertices);
848 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
850 if (fogenabled && rendertype != SURFRENDER_ADD)
852 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
854 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
855 m.tex[0] = R_GetTexture(texture->skin.fog);
857 GL_LockArrays(0, surf->mesh.num_vertices);
858 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
864 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
866 const entity_render_t *ent = calldata1;
867 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
871 float base, colorscale;
874 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
875 R_Mesh_Matrix(&ent->matrix);
877 texture = surf->texinfo->texture;
878 if (texture->animated)
879 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];
881 currentalpha = ent->alpha;
882 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
884 if (ent->effects & EF_ADDITIVE)
886 rendertype = SURFRENDER_ADD;
887 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
890 else if (currentalpha < 1 || texture->skin.fog != NULL)
892 rendertype = SURFRENDER_ALPHA;
893 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
898 rendertype = SURFRENDER_OPAQUE;
899 GL_BlendFunc(GL_ONE, GL_ZERO);
904 memset(&m, 0, sizeof(m));
905 m.tex[0] = R_GetTexture(texture->skin.base);
907 if (gl_combine.integer)
909 m.texrgbscale[0] = 4;
912 m.pointer_color = varray_color4f;
913 m.pointer_vertex = surf->mesh.data_vertex3f;
914 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
916 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
917 if (!(ent->effects & EF_FULLBRIGHT))
919 if (surf->dlightframe == r_framecount)
920 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
921 if (surf->flags & SURF_LIGHTMAP)
922 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);
924 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
925 GL_LockArrays(0, surf->mesh.num_vertices);
926 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
929 if (texture->skin.glow)
931 memset(&m, 0, sizeof(m));
932 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
934 m.pointer_color = varray_color4f;
935 m.tex[0] = R_GetTexture(texture->skin.glow);
936 m.pointer_vertex = surf->mesh.data_vertex3f;
938 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
940 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
941 GL_LockArrays(0, surf->mesh.num_vertices);
942 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
945 if (fogenabled && rendertype != SURFRENDER_ADD)
947 memset(&m, 0, sizeof(m));
948 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
950 m.pointer_color = varray_color4f;
951 m.tex[0] = R_GetTexture(texture->skin.fog);
952 m.pointer_vertex = surf->mesh.data_vertex3f;
954 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
956 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
957 GL_LockArrays(0, surf->mesh.num_vertices);
958 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
963 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
965 const msurface_t *surf;
967 int lightmaptexturenum;
968 memset(&m, 0, sizeof(m));
969 GL_BlendFunc(GL_ONE, GL_ZERO);
972 m.tex[0] = R_GetTexture(texture->skin.base);
973 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
974 m.texrgbscale[1] = 2;
975 m.tex[2] = R_GetTexture(texture->skin.detail);
976 m.texrgbscale[2] = 2;
977 if (texture->skin.glow)
979 m.tex[3] = R_GetTexture(texture->skin.glow);
980 m.texcombinergb[3] = GL_ADD;
982 if (r_shadow_realtime_world.integer)
983 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
985 GL_Color(1, 1, 1, 1);
987 while((surf = *surfchain++) != NULL)
989 if (surf->visframe == r_framecount)
991 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
992 m.tex[1] = lightmaptexturenum;
993 m.pointer_vertex = surf->mesh.data_vertex3f;
994 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
995 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
996 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
997 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
999 GL_LockArrays(0, surf->mesh.num_vertices);
1000 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1001 GL_LockArrays(0, 0);
1006 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1008 const msurface_t *surf;
1010 int lightmaptexturenum;
1011 memset(&m, 0, sizeof(m));
1012 GL_BlendFunc(GL_ONE, GL_ZERO);
1015 m.tex[0] = R_GetTexture(texture->skin.base);
1016 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1017 m.texrgbscale[1] = 2;
1018 m.tex[2] = R_GetTexture(texture->skin.detail);
1019 m.texrgbscale[2] = 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);
1025 while((surf = *surfchain++) != NULL)
1027 if (surf->visframe == r_framecount)
1029 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1030 m.tex[1] = lightmaptexturenum;
1031 m.pointer_vertex = surf->mesh.data_vertex3f;
1032 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1033 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1034 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1036 GL_LockArrays(0, surf->mesh.num_vertices);
1037 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1038 GL_LockArrays(0, 0);
1043 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1045 const msurface_t *surf;
1047 int lightmaptexturenum;
1048 memset(&m, 0, sizeof(m));
1049 GL_BlendFunc(GL_ONE, GL_ZERO);
1052 m.tex[0] = R_GetTexture(texture->skin.base);
1053 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1054 m.texrgbscale[1] = 2;
1055 if (r_shadow_realtime_world.integer)
1056 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1058 GL_Color(1, 1, 1, 1);
1059 while((surf = *surfchain++) != NULL)
1061 if (surf->visframe == r_framecount)
1063 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1064 m.tex[1] = lightmaptexturenum;
1065 m.pointer_vertex = surf->mesh.data_vertex3f;
1066 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1067 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1069 GL_LockArrays(0, surf->mesh.num_vertices);
1070 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1071 GL_LockArrays(0, 0);
1076 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1078 const msurface_t *surf;
1080 memset(&m, 0, sizeof(m));
1083 GL_BlendFunc(GL_ONE, GL_ZERO);
1084 m.tex[0] = R_GetTexture(texture->skin.base);
1085 if (r_shadow_realtime_world.integer)
1086 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1088 GL_Color(1, 1, 1, 1);
1089 while((surf = *surfchain++) != NULL)
1091 if (surf->visframe == r_framecount)
1093 m.pointer_vertex = surf->mesh.data_vertex3f;
1094 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1096 GL_LockArrays(0, surf->mesh.num_vertices);
1097 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1098 GL_LockArrays(0, 0);
1103 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1105 const msurface_t *surf;
1107 int lightmaptexturenum;
1108 memset(&m, 0, sizeof(m));
1109 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1110 GL_DepthMask(false);
1112 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1113 GL_Color(1, 1, 1, 1);
1114 while((surf = *surfchain++) != NULL)
1116 if (surf->visframe == r_framecount)
1118 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1119 m.tex[0] = lightmaptexturenum;
1120 m.pointer_vertex = surf->mesh.data_vertex3f;
1121 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1123 GL_LockArrays(0, surf->mesh.num_vertices);
1124 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1125 GL_LockArrays(0, 0);
1130 static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1132 const msurface_t *surf;
1134 memset(&m, 0, sizeof(m));
1135 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1136 GL_DepthMask(false);
1138 m.tex[0] = R_GetTexture(texture->skin.base);
1139 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1140 while((surf = *surfchain++) != NULL)
1142 if (surf->visframe == r_framecount)
1144 m.pointer_vertex = surf->mesh.data_vertex3f;
1145 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1147 GL_LockArrays(0, surf->mesh.num_vertices);
1148 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1149 GL_LockArrays(0, 0);
1154 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1156 const msurface_t *surf;
1159 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1160 memset(&m, 0, sizeof(m));
1161 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1162 GL_DepthMask(false);
1164 m.pointer_color = varray_color4f;
1165 while((surf = *surfchain++) != NULL)
1167 if (surf->visframe == r_framecount)
1169 m.pointer_vertex = surf->mesh.data_vertex3f;
1171 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1173 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1174 GL_LockArrays(0, surf->mesh.num_vertices);
1175 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1176 GL_LockArrays(0, 0);
1181 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1183 const msurface_t *surf;
1185 memset(&m, 0, sizeof(m));
1186 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1187 GL_DepthMask(false);
1189 m.tex[0] = R_GetTexture(texture->skin.detail);
1190 GL_Color(1, 1, 1, 1);
1191 while((surf = *surfchain++) != NULL)
1193 if (surf->visframe == r_framecount)
1195 m.pointer_vertex = surf->mesh.data_vertex3f;
1196 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1198 GL_LockArrays(0, surf->mesh.num_vertices);
1199 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1200 GL_LockArrays(0, 0);
1205 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1207 const msurface_t *surf;
1209 memset(&m, 0, sizeof(m));
1210 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1211 GL_DepthMask(false);
1213 m.tex[0] = R_GetTexture(texture->skin.glow);
1214 GL_Color(1, 1, 1, 1);
1215 while((surf = *surfchain++) != NULL)
1217 if (surf->visframe == r_framecount)
1219 m.pointer_vertex = surf->mesh.data_vertex3f;
1220 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1222 GL_LockArrays(0, surf->mesh.num_vertices);
1223 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1224 GL_LockArrays(0, 0);
1230 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1232 const msurface_t *surf;
1234 memset(&m, 0, sizeof(m));
1235 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1237 m.tex[0] = R_GetTexture(texture->skin.glow);
1239 GL_Color(1, 1, 1, 1);
1241 GL_Color(0, 0, 0, 1);
1242 while((surf = *surfchain++) != NULL)
1244 if (surf->visframe == r_framecount)
1246 m.pointer_vertex = surf->mesh.data_vertex3f;
1247 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1249 GL_LockArrays(0, surf->mesh.num_vertices);
1250 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1251 GL_LockArrays(0, 0);
1257 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1259 const msurface_t *surf;
1261 int lightmaptexturenum;
1262 memset(&m, 0, sizeof(m));
1263 GL_BlendFunc(GL_ONE, GL_ZERO);
1266 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1267 if (r_shadow_realtime_world.integer)
1268 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1270 GL_Color(1, 1, 1, 1);
1271 while((surf = *surfchain++) != NULL)
1273 if (surf->visframe == r_framecount)
1275 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1276 m.tex[0] = lightmaptexturenum;
1277 m.pointer_vertex = surf->mesh.data_vertex3f;
1278 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1280 GL_LockArrays(0, surf->mesh.num_vertices);
1281 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1282 GL_LockArrays(0, 0);
1287 void R_UpdateTextureInfo(entity_render_t *ent)
1289 int i, texframe, alttextures;
1295 alttextures = ent->frame != 0;
1296 texframe = (int)(cl.time * 5.0f);
1297 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1299 t = ent->model->brushq1.textures + i;
1300 t->currentalpha = ent->alpha;
1301 if (t->flags & SURF_WATERALPHA)
1302 t->currentalpha *= r_wateralpha.value;
1303 if (ent->effects & EF_ADDITIVE)
1304 t->rendertype = SURFRENDER_ADD;
1305 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1306 t->rendertype = SURFRENDER_ALPHA;
1308 t->rendertype = SURFRENDER_OPAQUE;
1309 // we don't need to set currentframe if t->animated is false because
1310 // it was already set up by the texture loader for non-animating
1312 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1316 void R_UpdateLightmapInfo(entity_render_t *ent)
1319 msurface_t *surface, **surfacechain;
1320 model_t *model = ent->model;
1323 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1325 for (i = 0;i < model->brushq1.light_styles;i++)
1327 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1329 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1330 for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1331 surface->cached_dlight = true;
1336 void R_PrepareSurfaces(entity_render_t *ent)
1338 int i, numsurfaces, *surfacevisframes;
1340 msurface_t *surf, *surfaces;
1347 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1348 numsurfaces = model->nummodelsurfaces;
1349 surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1350 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1351 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1353 if (surfacevisframes[i] == r_framecount)
1355 #if !WORLDNODECULLBACKFACES
1356 // mark any backface surfaces as not visible
1357 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1359 if (!(surf->flags & SURF_PLANEBACK))
1360 surfacevisframes[i] = -1;
1364 if ((surf->flags & SURF_PLANEBACK))
1365 surfacevisframes[i] = -1;
1367 if (surfacevisframes[i] == r_framecount)
1371 surf->visframe = r_framecount;
1372 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1373 R_BuildLightMap(ent, surf);
1379 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1381 int texturenumber, f;
1383 msurface_t *surf, **chain, **surfchain;
1384 texture_t *t, *texture;
1385 model_t *model = ent->model;
1388 R_Mesh_Matrix(&ent->matrix);
1389 for (texturenumber = 0, t = model->brushq1.textures;texturenumber < model->brushq1.numtextures;texturenumber++, t++)
1391 if ((f = t->flags & flagsmask) && (texture = t->currentframe) && (surfchain = model->brushq1.pvstexturechains[texturenumber]) != NULL)
1393 if (texture->flags & SURF_LIGHTMAP)
1395 if (gl_lightmaps.integer)
1397 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1399 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1401 else if (texture->rendertype != SURFRENDER_OPAQUE)
1403 // transparent vertex shaded from lightmap
1404 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1406 if (surf->visframe == r_framecount)
1408 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1409 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1415 if (ent->effects & EF_FULLBRIGHT || r_fullbright.integer)
1417 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1418 if (r_detailtextures.integer)
1419 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1420 if (texture->skin.glow)
1421 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1423 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1424 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1427 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1428 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1431 if (r_textureunits.integer >= 2 && gl_combine.integer)
1432 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1435 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1436 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1438 if (r_ambient.value > 0)
1439 RSurfShader_OpaqueWall_Pass_BaseAmbient(ent, texture, surfchain);
1440 if (r_detailtextures.integer)
1441 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1443 if (texture->skin.glow)
1444 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1447 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1450 else if (texture->flags & SURF_DRAWTURB)
1452 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1454 if (surf->visframe == r_framecount)
1456 if (texture->rendertype == SURFRENDER_OPAQUE)
1457 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
1460 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1461 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
1466 else if (texture->flags & SURF_DRAWSKY)
1467 RSurfShader_Sky(ent, texture, surfchain);
1472 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1477 const entity_render_t *ent = calldata1;
1478 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1479 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1480 GL_DepthMask(false);
1482 R_Mesh_Matrix(&ent->matrix);
1484 memset(&m, 0, sizeof(m));
1485 m.pointer_vertex = varray_vertex3f;
1488 i = portal - ent->model->brushq1.portals;
1489 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1490 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1491 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1493 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1495 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1496 VectorCopy(portal->points[i].position, v);
1499 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1500 VectorCopy(portal->points[i].position, v);
1501 GL_LockArrays(0, portal->numpoints);
1502 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1503 GL_LockArrays(0, 0);
1506 // LordHavoc: this is just a nice debugging tool, very slow
1507 static void R_DrawPortals(entity_render_t *ent)
1510 mportal_t *portal, *endportal;
1511 float temp[3], center[3], f;
1512 if (ent->model == NULL)
1514 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1516 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1519 for (i = 0;i < portal->numpoints;i++)
1520 VectorAdd(temp, portal->points[i].position, temp);
1521 f = ixtable[portal->numpoints];
1522 VectorScale(temp, f, temp);
1523 Matrix4x4_Transform(&ent->matrix, temp, center);
1524 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1529 void R_PrepareBrushModel(entity_render_t *ent)
1531 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1534 #if WORLDNODECULLBACKFACES
1538 // because bmodels can be reused, we have to decide which things to render
1539 // from scratch every time
1543 #if WORLDNODECULLBACKFACES
1544 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1546 numsurfaces = model->nummodelsurfaces;
1547 surf = model->brushq1.surfaces + model->firstmodelsurface;
1548 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1549 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1550 for (i = 0;i < numsurfaces;i++, surf++)
1552 #if WORLDNODECULLBACKFACES
1553 // mark any backface surfaces as not visible
1554 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1556 if ((surf->flags & SURF_PLANEBACK))
1557 surfacevisframes[i] = r_framecount;
1559 else if (!(surf->flags & SURF_PLANEBACK))
1560 surfacevisframes[i] = r_framecount;
1562 surfacevisframes[i] = r_framecount;
1564 surf->dlightframe = -1;
1566 R_UpdateTextureInfo(ent);
1567 R_UpdateLightmapInfo(ent);
1570 void R_SurfaceWorldNode (entity_render_t *ent)
1572 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1578 // equivilant to quake's RecursiveWorldNode but faster and more effective
1582 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1583 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1584 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1586 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1588 if (!R_CullBox (leaf->mins, leaf->maxs))
1591 leaf->visframe = r_framecount;
1595 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1597 surfnum = model->brushq1.pvssurflist[i];
1598 surf = model->brushq1.surfaces + surfnum;
1599 #if WORLDNODECULLBACKFACES
1600 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1602 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1603 surfacevisframes[surfnum] = r_framecount;
1607 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1608 surfacevisframes[surfnum] = r_framecount;
1611 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1612 surfacevisframes[surfnum] = r_framecount;
1617 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1619 int c, leafstackpos, *mark, *surfacevisframes;
1620 #if WORLDNODECULLBACKFACES
1624 mleaf_t *leaf, *leafstack[8192];
1627 msurface_t *surfaces;
1628 if (ent->model == NULL)
1630 // LordHavoc: portal-passage worldnode with PVS;
1631 // follows portals leading outward from viewleaf, does not venture
1632 // offscreen or into leafs that are not visible, faster than Quake's
1633 // RecursiveWorldNode
1634 surfaces = ent->model->brushq1.surfaces;
1635 surfacevisframes = ent->model->brushq1.surfacevisframes;
1636 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1637 viewleaf->worldnodeframe = r_framecount;
1638 leafstack[0] = viewleaf;
1640 while (leafstackpos)
1643 leaf = leafstack[--leafstackpos];
1644 leaf->visframe = r_framecount;
1645 // draw any surfaces bounding this leaf
1646 if (leaf->nummarksurfaces)
1648 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1650 #if WORLDNODECULLBACKFACES
1652 if (surfacevisframes[n] != r_framecount)
1654 surf = surfaces + n;
1655 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1657 if ((surf->flags & SURF_PLANEBACK))
1658 surfacevisframes[n] = r_framecount;
1662 if (!(surf->flags & SURF_PLANEBACK))
1663 surfacevisframes[n] = r_framecount;
1667 surfacevisframes[*mark++] = r_framecount;
1671 // follow portals into other leafs
1672 for (p = leaf->portals;p;p = p->next)
1674 // LordHavoc: this DotProduct hurts less than a cache miss
1675 // (which is more likely to happen if backflowing through leafs)
1676 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1679 if (leaf->worldnodeframe != r_framecount)
1681 leaf->worldnodeframe = r_framecount;
1682 // FIXME: R_CullBox is absolute, should be done relative
1683 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1684 leafstack[leafstackpos++] = leaf;
1691 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1693 int j, c, *surfacepvsframes, *mark;
1698 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1700 model->brushq1.pvsframecount++;
1701 model->brushq1.pvsviewleaf = viewleaf;
1702 model->brushq1.pvsviewleafnovis = r_novis.integer;
1703 model->brushq1.pvsleafchain = NULL;
1704 model->brushq1.pvssurflistlength = 0;
1707 surfacepvsframes = model->brushq1.surfacepvsframes;
1708 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1710 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1712 leaf->pvsframe = model->brushq1.pvsframecount;
1713 leaf->pvschain = model->brushq1.pvsleafchain;
1714 model->brushq1.pvsleafchain = leaf;
1715 // mark surfaces bounding this leaf as visible
1716 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1717 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1720 model->brushq1.BuildPVSTextureChains(model);
1725 void R_WorldVisibility(entity_render_t *ent)
1730 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1731 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1732 R_PVSUpdate(ent, viewleaf);
1737 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1738 R_SurfaceWorldNode (ent);
1740 R_PortalWorldNode (ent, viewleaf);
1742 if (r_drawportals.integer)
1746 void R_Model_Brush_DrawSky(entity_render_t *ent)
1748 if (ent->model == NULL)
1750 if (ent != &cl_entities[0].render)
1751 R_PrepareBrushModel(ent);
1752 R_PrepareSurfaces(ent);
1753 R_DrawSurfaces(ent, SURF_DRAWSKY);
1756 void R_Model_Brush_Draw(entity_render_t *ent)
1758 if (ent->model == NULL)
1761 if (ent != &cl_entities[0].render)
1762 R_PrepareBrushModel(ent);
1763 R_PrepareSurfaces(ent);
1764 R_UpdateTextureInfo(ent);
1765 R_UpdateLightmapInfo(ent);
1766 R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1769 void R_Model_Brush_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)
1771 model_t *model = ent->model;
1772 vec3_t lightmins, lightmaxs;
1773 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1776 msurface_t *surface;
1779 lightmins[0] = relativelightorigin[0] - lightradius;
1780 lightmins[1] = relativelightorigin[1] - lightradius;
1781 lightmins[2] = relativelightorigin[2] - lightradius;
1782 lightmaxs[0] = relativelightorigin[0] + lightradius;
1783 lightmaxs[1] = relativelightorigin[1] + lightradius;
1784 lightmaxs[2] = relativelightorigin[2] + lightradius;
1785 *outnumclusterspointer = 0;
1786 *outnumsurfacespointer = 0;
1787 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1788 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1791 VectorCopy(lightmins, outmins);
1792 VectorCopy(lightmaxs, outmaxs);
1795 VectorCopy(relativelightorigin, outmins);
1796 VectorCopy(relativelightorigin, outmaxs);
1797 if (model->brush.GetPVS)
1798 pvs = model->brush.GetPVS(model, relativelightorigin);
1801 // FIXME: use BSP recursion as lights are often small
1802 for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1804 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1806 outmins[0] = min(outmins[0], leaf->mins[0]);
1807 outmins[1] = min(outmins[1], leaf->mins[1]);
1808 outmins[2] = min(outmins[2], leaf->mins[2]);
1809 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1810 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1811 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1812 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1814 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1815 outclusterlist[outnumclusters++] = leaf->clusterindex;
1817 for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1819 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1820 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1822 surface = model->brushq1.surfaces + surfaceindex;
1823 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1825 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1827 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1828 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1829 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1830 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])))
1832 SETPVSBIT(outsurfacepvs, surfaceindex);
1833 outsurfacelist[outnumsurfaces++] = surfaceindex;
1843 // limit combined leaf box to light boundaries
1844 outmins[0] = max(outmins[0], lightmins[0]);
1845 outmins[1] = max(outmins[1], lightmins[1]);
1846 outmins[2] = max(outmins[2], lightmins[2]);
1847 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1848 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1849 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1851 *outnumclusterspointer = outnumclusters;
1852 *outnumsurfacespointer = outnumsurfaces;
1855 void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1857 model_t *model = ent->model;
1858 vec3_t lightmins, lightmaxs;
1859 msurface_t *surface;
1860 int surfacelistindex, j, t;
1863 if (r_drawcollisionbrushes.integer < 2)
1865 lightmins[0] = relativelightorigin[0] - lightradius;
1866 lightmins[1] = relativelightorigin[1] - lightradius;
1867 lightmins[2] = relativelightorigin[2] - lightradius;
1868 lightmaxs[0] = relativelightorigin[0] + lightradius;
1869 lightmaxs[1] = relativelightorigin[1] + lightradius;
1870 lightmaxs[2] = relativelightorigin[2] + lightradius;
1871 R_Mesh_Matrix(&ent->matrix);
1872 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1873 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1875 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1876 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1878 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1879 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1880 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1881 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])))
1882 shadowmarklist[numshadowmark++] = t;
1885 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);
1889 void R_Model_Brush_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)
1891 model_t *model = ent->model;
1892 vec3_t lightmins, lightmaxs, modelorg;
1893 msurface_t *surface;
1895 int surfacelistindex;
1896 if (r_drawcollisionbrushes.integer < 2)
1898 lightmins[0] = relativelightorigin[0] - lightradius;
1899 lightmins[1] = relativelightorigin[1] - lightradius;
1900 lightmins[2] = relativelightorigin[2] - lightradius;
1901 lightmaxs[0] = relativelightorigin[0] + lightradius;
1902 lightmaxs[1] = relativelightorigin[1] + lightradius;
1903 lightmaxs[2] = relativelightorigin[2] + lightradius;
1904 R_Mesh_Matrix(&ent->matrix);
1905 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1906 R_UpdateTextureInfo(ent);
1907 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1909 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1910 if (r_shadow_compilingrtlight)
1912 // if compiling an rtlight, capture the mesh
1913 t = surface->texinfo->texture;
1914 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1915 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);
1917 else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1919 t = surface->texinfo->texture->currentframe;
1920 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1921 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);
1927 void R_DrawCollisionBrush(colbrushf_t *brush)
1931 memset(&m, 0, sizeof(m));
1932 m.pointer_vertex = brush->points->v;
1934 i = ((int)brush) / sizeof(colbrushf_t);
1935 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1936 GL_LockArrays(0, brush->numpoints);
1937 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1938 GL_LockArrays(0, 0);
1941 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1945 if (!face->num_collisiontriangles)
1947 memset(&m, 0, sizeof(m));
1948 m.pointer_vertex = face->data_collisionvertex3f;
1950 i = ((int)face) / sizeof(q3msurface_t);
1951 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1952 GL_LockArrays(0, face->num_collisionvertices);
1953 R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1954 GL_LockArrays(0, 0);
1957 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1960 if (!face->num_triangles)
1965 skyrendernow = false;
1966 if (skyrendermasked)
1970 R_Mesh_Matrix(&ent->matrix);
1972 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1973 if (skyrendermasked)
1975 // depth-only (masking)
1976 GL_ColorMask(0,0,0,0);
1977 // just to make sure that braindead drivers don't draw anything
1978 // despite that colormask...
1979 GL_BlendFunc(GL_ZERO, GL_ONE);
1984 GL_BlendFunc(GL_ONE, GL_ZERO);
1989 memset(&m, 0, sizeof(m));
1990 m.pointer_vertex = face->data_vertex3f;
1993 GL_LockArrays(0, face->num_vertices);
1994 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1995 GL_LockArrays(0, 0);
1996 GL_ColorMask(1,1,1,1);
1999 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
2002 memset(&m, 0, sizeof(m));
2003 GL_BlendFunc(GL_ONE, GL_ZERO);
2006 if (face->texture->skin.glow)
2008 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2009 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2010 GL_Color(1, 1, 1, 1);
2013 GL_Color(0, 0, 0, 1);
2014 m.pointer_vertex = face->data_vertex3f;
2016 GL_LockArrays(0, face->num_vertices);
2017 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2018 GL_LockArrays(0, 0);
2021 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
2024 memset(&m, 0, sizeof(m));
2025 GL_BlendFunc(GL_ONE, GL_ZERO);
2028 m.tex[0] = R_GetTexture(face->texture->skin.base);
2029 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2030 m.tex[1] = R_GetTexture(face->lightmaptexture);
2031 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2032 m.texrgbscale[1] = 2;
2033 GL_Color(1, 1, 1, 1);
2034 m.pointer_vertex = face->data_vertex3f;
2036 GL_LockArrays(0, face->num_vertices);
2037 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2038 GL_LockArrays(0, 0);
2041 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2044 memset(&m, 0, sizeof(m));
2045 GL_BlendFunc(GL_ONE, GL_ZERO);
2048 m.tex[0] = R_GetTexture(face->texture->skin.base);
2049 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2050 GL_Color(1, 1, 1, 1);
2051 m.pointer_vertex = face->data_vertex3f;
2053 GL_LockArrays(0, face->num_vertices);
2054 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2055 GL_LockArrays(0, 0);
2058 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2061 memset(&m, 0, sizeof(m));
2062 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2063 GL_DepthMask(false);
2065 m.tex[0] = R_GetTexture(face->lightmaptexture);
2066 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2067 GL_Color(1, 1, 1, 1);
2068 m.pointer_vertex = face->data_vertex3f;
2070 GL_LockArrays(0, face->num_vertices);
2071 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2072 GL_LockArrays(0, 0);
2075 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2078 memset(&m, 0, sizeof(m));
2079 GL_BlendFunc(GL_ONE, GL_ZERO);
2082 m.tex[0] = R_GetTexture(face->lightmaptexture);
2083 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2084 if (r_shadow_realtime_world.integer)
2085 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2087 GL_Color(1, 1, 1, 1);
2088 m.pointer_vertex = face->data_vertex3f;
2090 GL_LockArrays(0, face->num_vertices);
2091 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2092 GL_LockArrays(0, 0);
2095 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
2098 memset(&m, 0, sizeof(m));
2099 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2100 GL_DepthMask(false);
2102 if (face->texture->skin.glow)
2104 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2105 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2106 GL_Color(1, 1, 1, 1);
2109 GL_Color(0, 0, 0, 1);
2110 m.pointer_vertex = face->data_vertex3f;
2112 GL_LockArrays(0, face->num_vertices);
2113 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2114 GL_LockArrays(0, 0);
2117 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
2122 memset(&m, 0, sizeof(m));
2123 GL_BlendFunc(GL_ONE, GL_ZERO);
2126 m.tex[0] = R_GetTexture(face->texture->skin.base);
2127 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2129 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2130 mul *= r_shadow_realtime_world_lightmaps.value;
2131 if (mul == 2 && gl_combine.integer)
2133 m.texrgbscale[0] = 2;
2134 m.pointer_color = face->data_color4f;
2137 m.pointer_color = face->data_color4f;
2140 for (i = 0;i < face->num_vertices;i++)
2142 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2143 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2144 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2145 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2147 m.pointer_color = varray_color4f;
2149 m.pointer_vertex = face->data_vertex3f;
2151 GL_LockArrays(0, face->num_vertices);
2152 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2153 GL_LockArrays(0, 0);
2156 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2161 memset(&m, 0, sizeof(m));
2162 GL_BlendFunc(GL_ONE, GL_ZERO);
2166 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2167 mul *= r_shadow_realtime_world_lightmaps.value;
2169 m.pointer_color = face->data_color4f;
2172 for (i = 0;i < face->num_vertices;i++)
2174 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2175 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2176 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2177 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2179 m.pointer_color = varray_color4f;
2181 m.pointer_vertex = face->data_vertex3f;
2183 GL_LockArrays(0, face->num_vertices);
2184 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2185 GL_LockArrays(0, 0);
2188 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2191 memset(&m, 0, sizeof(m));
2192 GL_BlendFunc(GL_ONE, GL_ONE);
2195 m.tex[0] = R_GetTexture(face->texture->skin.base);
2196 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2197 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2198 m.pointer_vertex = face->data_vertex3f;
2200 GL_LockArrays(0, face->num_vertices);
2201 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2202 GL_LockArrays(0, 0);
2205 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2207 const entity_render_t *ent = voident;
2208 q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2210 R_Mesh_Matrix(&ent->matrix);
2211 memset(&m, 0, sizeof(m));
2212 if (ent->effects & EF_ADDITIVE)
2213 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2215 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2216 GL_DepthMask(false);
2218 m.tex[0] = R_GetTexture(face->texture->skin.base);
2219 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2220 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2221 if (gl_combine.integer)
2223 m.texrgbscale[0] = 2;
2224 if (r_textureunits.integer >= 2)
2226 m.tex[1] = R_GetTexture(face->lightmaptexture);
2227 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2228 GL_Color(1, 1, 1, ent->alpha);
2232 if (ent->alpha == 1)
2233 m.pointer_color = face->data_color4f;
2237 for (i = 0;i < face->num_vertices;i++)
2239 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2240 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2241 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2242 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2244 m.pointer_color = varray_color4f;
2251 for (i = 0;i < face->num_vertices;i++)
2253 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2254 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2255 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2256 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2258 m.pointer_color = varray_color4f;
2260 m.pointer_vertex = face->data_vertex3f;
2262 qglDisable(GL_CULL_FACE);
2263 GL_LockArrays(0, face->num_vertices);
2264 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2265 GL_LockArrays(0, 0);
2266 qglEnable(GL_CULL_FACE);
2269 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2271 if (!face->num_triangles)
2273 if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2276 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2278 vec3_t facecenter, center;
2279 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2280 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2281 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2282 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2283 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2286 R_Mesh_Matrix(&ent->matrix);
2287 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2288 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2289 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2291 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2292 if (face->texture->skin.glow)
2293 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2295 else if (face->lightmaptexture)
2297 if (gl_lightmaps.integer)
2298 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2301 if (r_textureunits.integer >= 2 && gl_combine.integer)
2302 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2305 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2306 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2308 if (face->texture->skin.glow)
2309 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2314 if (gl_lightmaps.integer)
2315 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2318 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2319 if (face->texture->skin.glow)
2320 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2323 if (r_ambient.value)
2324 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2327 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2333 if (R_CullBox(node->mins, node->maxs))
2338 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2339 node = node->children[1];
2341 leaf = (q3mleaf_t *)node;
2342 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2345 for (i = 0;i < leaf->numleaffaces;i++)
2346 leaf->firstleafface[i]->visframe = r_framecount;
2350 // FIXME: num_leafs needs to be recalculated at load time to include only
2351 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2352 // the submodels (which would render the submodels occasionally, as part of
2353 // the world - not good)
2354 void R_Q3BSP_MarkLeafPVS(void)
2358 for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2360 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2363 for (i = 0;i < leaf->numleaffaces;i++)
2364 leaf->firstleafface[i]->visframe = r_framecount;
2369 static int r_q3bsp_framecount = -1;
2371 void R_Q3BSP_DrawSky(entity_render_t *ent)
2377 R_Mesh_Matrix(&ent->matrix);
2379 if (r_drawcollisionbrushes.integer < 2)
2381 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2382 if (ent == &cl_entities[0].render)
2384 if (r_q3bsp_framecount != r_framecount)
2386 r_q3bsp_framecount = r_framecount;
2387 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2388 //R_Q3BSP_MarkLeafPVS();
2390 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2391 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2392 R_Q3BSP_DrawSkyFace(ent, face);
2395 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2396 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2397 R_Q3BSP_DrawSkyFace(ent, face);
2401 void R_Q3BSP_Draw(entity_render_t *ent)
2408 R_Mesh_Matrix(&ent->matrix);
2410 if (r_drawcollisionbrushes.integer < 2)
2412 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2413 if (ent == &cl_entities[0].render)
2415 if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2416 if (r_q3bsp_framecount != r_framecount)
2418 r_q3bsp_framecount = r_framecount;
2419 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2420 //R_Q3BSP_MarkLeafPVS();
2422 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2423 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2424 R_Q3BSP_DrawFace(ent, face);
2427 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2428 R_Q3BSP_DrawFace(ent, face);
2430 if (r_drawcollisionbrushes.integer >= 1)
2432 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2433 GL_DepthMask(false);
2435 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2436 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2437 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2438 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2439 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2440 if (face->num_collisiontriangles)
2441 R_Q3BSP_DrawCollisionFace(ent, face);
2442 qglPolygonOffset(0, 0);
2446 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)
2448 model_t *model = ent->model;
2449 vec3_t lightmins, lightmaxs;
2450 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2453 q3msurface_t *surface;
2456 lightmins[0] = relativelightorigin[0] - lightradius;
2457 lightmins[1] = relativelightorigin[1] - lightradius;
2458 lightmins[2] = relativelightorigin[2] - lightradius;
2459 lightmaxs[0] = relativelightorigin[0] + lightradius;
2460 lightmaxs[1] = relativelightorigin[1] + lightradius;
2461 lightmaxs[2] = relativelightorigin[2] + lightradius;
2462 *outnumclusterspointer = 0;
2463 *outnumsurfacespointer = 0;
2464 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2465 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2468 VectorCopy(lightmins, outmins);
2469 VectorCopy(lightmaxs, outmaxs);
2472 VectorCopy(relativelightorigin, outmins);
2473 VectorCopy(relativelightorigin, outmaxs);
2474 if (model->brush.GetPVS)
2475 pvs = model->brush.GetPVS(model, relativelightorigin);
2478 // FIXME: use BSP recursion as lights are often small
2479 for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2481 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2483 outmins[0] = min(outmins[0], leaf->mins[0]);
2484 outmins[1] = min(outmins[1], leaf->mins[1]);
2485 outmins[2] = min(outmins[2], leaf->mins[2]);
2486 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2487 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2488 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2489 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2491 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2492 outclusterlist[outnumclusters++] = leaf->clusterindex;
2494 for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2496 surface = leaf->firstleafface[marksurfaceindex];
2497 surfaceindex = surface - model->brushq3.data_faces;
2498 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2500 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2502 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2504 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2505 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2506 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2507 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])))
2509 SETPVSBIT(outsurfacepvs, surfaceindex);
2510 outsurfacelist[outnumsurfaces++] = surfaceindex;
2520 // limit combined leaf box to light boundaries
2521 outmins[0] = max(outmins[0], lightmins[0]);
2522 outmins[1] = max(outmins[1], lightmins[1]);
2523 outmins[2] = max(outmins[2], lightmins[2]);
2524 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2525 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2526 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2528 *outnumclusterspointer = outnumclusters;
2529 *outnumsurfacespointer = outnumsurfaces;
2532 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2534 model_t *model = ent->model;
2535 vec3_t lightmins, lightmaxs;
2536 q3msurface_t *surface;
2537 int surfacelistindex, j, t;
2540 if (r_drawcollisionbrushes.integer < 2)
2542 lightmins[0] = relativelightorigin[0] - lightradius;
2543 lightmins[1] = relativelightorigin[1] - lightradius;
2544 lightmins[2] = relativelightorigin[2] - lightradius;
2545 lightmaxs[0] = relativelightorigin[0] + lightradius;
2546 lightmaxs[1] = relativelightorigin[1] + lightradius;
2547 lightmaxs[2] = relativelightorigin[2] + lightradius;
2548 R_Mesh_Matrix(&ent->matrix);
2549 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2550 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2552 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2553 // FIXME: check some manner of face->rendermode here?
2554 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2556 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2558 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2559 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2560 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2561 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])))
2562 shadowmarklist[numshadowmark++] = t;
2566 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);
2570 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)
2572 model_t *model = ent->model;
2573 vec3_t lightmins, lightmaxs, modelorg;
2574 q3msurface_t *surface;
2575 int surfacelistindex;
2576 if (r_drawcollisionbrushes.integer < 2)
2578 lightmins[0] = relativelightorigin[0] - lightradius;
2579 lightmins[1] = relativelightorigin[1] - lightradius;
2580 lightmins[2] = relativelightorigin[2] - lightradius;
2581 lightmaxs[0] = relativelightorigin[0] + lightradius;
2582 lightmaxs[1] = relativelightorigin[1] + lightradius;
2583 lightmaxs[2] = relativelightorigin[2] + lightradius;
2584 R_Mesh_Matrix(&ent->matrix);
2585 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2586 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2588 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2589 if (r_shadow_compilingrtlight)
2591 // if compiling an rtlight, capture the mesh
2592 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);
2594 else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2595 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);
2600 static void gl_surf_start(void)
2604 static void gl_surf_shutdown(void)
2608 static void gl_surf_newmap(void)
2612 void GL_Surf_Init(void)
2615 dlightdivtable[0] = 4194304;
2616 for (i = 1;i < 32768;i++)
2617 dlightdivtable[i] = 4194304 / (i << 7);
2619 Cvar_RegisterVariable(&r_ambient);
2620 Cvar_RegisterVariable(&r_drawportals);
2621 Cvar_RegisterVariable(&r_testvis);
2622 Cvar_RegisterVariable(&r_floatbuildlightmap);
2623 Cvar_RegisterVariable(&r_detailtextures);
2624 Cvar_RegisterVariable(&r_surfaceworldnode);
2625 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2626 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2627 Cvar_RegisterVariable(&gl_lightmaps);
2629 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);