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 r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
41 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
44 // FIXME: these arrays are huge!
45 int r_q1bsp_maxmarkleafs;
46 int r_q1bsp_nummarkleafs;
47 mleaf_t *r_q1bsp_maxleaflist[65536];
48 int r_q1bsp_maxmarksurfaces;
49 int r_q1bsp_nummarksurfaces;
50 msurface_t *r_q1bsp_maxsurfacelist[65536];
52 // FIXME: these arrays are huge!
53 int r_q3bsp_maxmarkleafs;
54 int r_q3bsp_nummarkleafs;
55 q3mleaf_t *r_q3bsp_maxleaflist[65536];
56 int r_q3bsp_maxmarksurfaces;
57 int r_q3bsp_nummarksurfaces;
58 q3msurface_t *r_q3bsp_maxsurfacelist[65536];
61 static int dlightdivtable[32768];
63 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
65 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
67 float dist, impact[3], local[3];
72 smax = (surf->extents[0] >> 4) + 1;
73 tmax = (surf->extents[1] >> 4) + 1;
76 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
78 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
79 continue; // not lit by this light
81 Matrix4x4_Transform(matrix, light->origin, local);
82 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
84 // for comparisons to minimum acceptable light
85 // compensate for LIGHTOFFSET
86 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
93 if (surf->plane->type < 3)
95 VectorCopy(local, impact);
96 impact[surf->plane->type] -= dist;
100 impact[0] = local[0] - surf->plane->normal[0] * dist;
101 impact[1] = local[1] - surf->plane->normal[1] * dist;
102 impact[2] = local[2] - surf->plane->normal[2] * dist;
105 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
106 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
108 s = bound(0, impacts, smax * 16) - impacts;
109 t = bound(0, impactt, tmax * 16) - impactt;
110 i = s * s + t * t + dist2;
114 // reduce calculations
115 for (s = 0, i = impacts; s < smax; s++, i -= 16)
116 sdtable[s] = i * i + dist2;
118 maxdist3 = maxdist - dist2;
120 // convert to 8.8 blocklights format
121 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
122 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
123 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
124 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
128 for (t = 0;t < tmax;t++, i -= 16)
131 // make sure some part of it is visible on this line
134 maxdist2 = maxdist - td;
135 for (s = 0;s < smax;s++)
137 if (sdtable[s] < maxdist2)
139 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
143 bl[1] += (green * k);
158 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
160 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
161 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
166 smax = (surf->extents[0] >> 4) + 1;
167 tmax = (surf->extents[1] >> 4) + 1;
170 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
172 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
173 continue; // not lit by this light
175 Matrix4x4_Transform(matrix, light->origin, local);
176 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
178 // for comparisons to minimum acceptable light
179 // compensate for LIGHTOFFSET
180 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
183 dist2 += LIGHTOFFSET;
184 if (dist2 >= maxdist)
187 if (surf->plane->type < 3)
189 VectorCopy(local, impact);
190 impact[surf->plane->type] -= dist;
194 impact[0] = local[0] - surf->plane->normal[0] * dist;
195 impact[1] = local[1] - surf->plane->normal[1] * dist;
196 impact[2] = local[2] - surf->plane->normal[2] * dist;
199 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
200 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
202 td = bound(0, impacts, smax * 16) - impacts;
203 td1 = bound(0, impactt, tmax * 16) - impactt;
204 td = td * td + td1 * td1 + dist2;
208 // reduce calculations
209 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
210 sdtable[s] = td1 * td1 + dist2;
212 maxdist3 = maxdist - dist2;
214 // convert to 8.8 blocklights format
215 red = light->rtlight.lightmap_light[0];
216 green = light->rtlight.lightmap_light[1];
217 blue = light->rtlight.lightmap_light[2];
218 subtract = light->rtlight.lightmap_subtract * 32768.0f;
219 bl = floatblocklights;
222 for (t = 0;t < tmax;t++, td1 -= 16.0f)
225 // make sure some part of it is visible on this line
228 maxdist2 = maxdist - td;
229 for (s = 0;s < smax;s++)
231 if (sdtable[s] < maxdist2)
233 k = (32768.0f / (sdtable[s] + td)) - subtract;
253 Combine and scale multiple lightmaps into the 8.8 format in blocklights
256 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
258 if (!r_floatbuildlightmap.integer)
260 int smax, tmax, i, j, size, size3, maps, stride, l;
261 unsigned int *bl, scale;
262 qbyte *lightmap, *out, *stain;
264 // update cached lighting info
265 surf->cached_dlight = 0;
267 smax = (surf->extents[0]>>4)+1;
268 tmax = (surf->extents[1]>>4)+1;
271 lightmap = surf->samples;
273 // set to full bright if no light data
275 if (!ent->model->brushq1.lightdata)
277 for (i = 0;i < size3;i++)
283 memset(bl, 0, size*3*sizeof(unsigned int));
285 if (surf->dlightframe == r_framecount)
287 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
288 if (surf->cached_dlight)
292 // add all the lightmaps
296 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
297 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
298 bl[i] += lightmap[i] * scale;
302 stain = surf->stainsamples;
305 // the >> 16 shift adjusts down 8 bits to account for the stainmap
306 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
307 // be doubled during rendering to achieve 2x overbright
308 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
309 if (ent->model->brushq1.lightmaprgba)
311 stride = (surf->lightmaptexturestride - smax) * 4;
312 for (i = 0;i < tmax;i++, out += stride)
314 for (j = 0;j < smax;j++)
316 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
317 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
318 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
325 stride = (surf->lightmaptexturestride - smax) * 3;
326 for (i = 0;i < tmax;i++, out += stride)
328 for (j = 0;j < smax;j++)
330 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
331 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
332 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
337 R_UpdateTexture(surf->lightmaptexture, templight);
341 int smax, tmax, i, j, size, size3, maps, stride, l;
343 qbyte *lightmap, *out, *stain;
345 // update cached lighting info
346 surf->cached_dlight = 0;
348 smax = (surf->extents[0]>>4)+1;
349 tmax = (surf->extents[1]>>4)+1;
352 lightmap = surf->samples;
354 // set to full bright if no light data
355 bl = floatblocklights;
356 if (!ent->model->brushq1.lightdata)
358 for (i = 0;i < size3;i++)
363 memset(bl, 0, size*3*sizeof(float));
365 if (surf->dlightframe == r_framecount)
367 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
368 if (surf->cached_dlight)
372 // add all the lightmaps
375 bl = floatblocklights;
376 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
377 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
378 bl[i] += lightmap[i] * scale;
382 stain = surf->stainsamples;
383 bl = floatblocklights;
385 // this scaling adjusts down 8 bits to account for the stainmap
386 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
387 // be doubled during rendering to achieve 2x overbright
388 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
389 scale = 1.0f / (1 << 16);
390 if (ent->model->brushq1.lightmaprgba)
392 stride = (surf->lightmaptexturestride - smax) * 4;
393 for (i = 0;i < tmax;i++, out += stride)
395 for (j = 0;j < smax;j++)
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
399 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
406 stride = (surf->lightmaptexturestride - smax) * 3;
407 for (i = 0;i < tmax;i++, out += stride)
409 for (j = 0;j < smax;j++)
411 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
412 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
413 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418 R_UpdateTexture(surf->lightmaptexture, templight);
422 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
424 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
425 msurface_t *surf, *endsurf;
426 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
430 maxdist = radius * radius;
431 invradius = 1.0f / radius;
434 if (node->contents < 0)
436 ndist = PlaneDiff(origin, node->plane);
439 node = node->children[0];
444 node = node->children[1];
448 dist2 = ndist * ndist;
449 maxdist3 = maxdist - dist2;
451 if (node->plane->type < 3)
453 VectorCopy(origin, impact);
454 impact[node->plane->type] -= ndist;
458 impact[0] = origin[0] - node->plane->normal[0] * ndist;
459 impact[1] = origin[1] - node->plane->normal[1] * ndist;
460 impact[2] = origin[2] - node->plane->normal[2] * ndist;
463 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
465 if (surf->stainsamples)
467 smax = (surf->extents[0] >> 4) + 1;
468 tmax = (surf->extents[1] >> 4) + 1;
470 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
471 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
473 s = bound(0, impacts, smax * 16) - impacts;
474 t = bound(0, impactt, tmax * 16) - impactt;
475 i = s * s + t * t + dist2;
479 // reduce calculations
480 for (s = 0, i = impacts; s < smax; s++, i -= 16)
481 sdtable[s] = i * i + dist2;
483 bl = surf->stainsamples;
488 for (t = 0;t < tmax;t++, i -= 16)
491 // make sure some part of it is visible on this line
494 maxdist2 = maxdist - td;
495 for (s = 0;s < smax;s++)
497 if (sdtable[s] < maxdist2)
499 ratio = lhrandom(0.0f, 1.0f);
500 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
501 if (a >= (1.0f / 64.0f))
505 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
506 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
507 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
517 // force lightmap upload
519 surf->cached_dlight = true;
523 if (node->children[0]->contents >= 0)
525 if (node->children[1]->contents >= 0)
527 R_StainNode(node->children[0], model, origin, radius, fcolor);
528 node = node->children[1];
533 node = node->children[0];
537 else if (node->children[1]->contents >= 0)
539 node = node->children[1];
544 void R_Stain (const vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
548 entity_render_t *ent;
551 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
556 fcolor[3] = ca1 * (1.0f / 64.0f);
557 fcolor[4] = cr2 - cr1;
558 fcolor[5] = cg2 - cg1;
559 fcolor[6] = cb2 - cb1;
560 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
562 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
564 // look for embedded bmodels
565 for (n = 0;n < cl_num_brushmodel_entities;n++)
567 ent = cl_brushmodel_entities[n];
569 if (model && model->name[0] == '*')
571 Mod_CheckLoaded(model);
572 if (model->brushq1.nodes)
574 Matrix4x4_Transform(&ent->inversematrix, origin, org);
575 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
583 =============================================================
587 =============================================================
590 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
595 if (styles[0] != 255)
597 for (i = 0;i < numverts;i++, c += 4)
599 lm = samples + lightmapoffsets[i];
600 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
601 VectorMA(c, scale, lm, c);
602 if (styles[1] != 255)
605 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
606 VectorMA(c, scale, lm, c);
607 if (styles[2] != 255)
610 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
611 VectorMA(c, scale, lm, c);
612 if (styles[3] != 255)
615 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
616 VectorMA(c, scale, lm, c);
624 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
630 for (i = 0;i < numverts;i++, v += 3, c += 4)
632 VectorSubtract(v, modelorg, diff);
633 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
634 VectorScale(c, f, c);
637 else if (colorscale != 1)
638 for (i = 0;i < numverts;i++, c += 4)
639 VectorScale(c, colorscale, c);
642 static void RSurf_FoggedColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
651 for (i = 0;i < numverts;i++, v += 3, c += 4)
653 VectorSubtract(v, modelorg, diff);
654 f = 1 - exp(fogdensity/DotProduct(diff, diff));
663 for (i = 0;i < numverts;i++, c += 4)
673 static void RSurf_FogPassColors_Vertex3f_Color4f(const float *v, float *c, float r, float g, float b, float a, float colorscale, int numverts, const float *modelorg)
680 for (i = 0;i < numverts;i++, v += 3, c += 4)
682 VectorSubtract(v, modelorg, diff);
683 f = exp(fogdensity/DotProduct(diff, diff));
691 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
696 int i, l, lit = false;
697 const dlight_t *light;
699 for (l = 0;l < r_numdlights;l++)
701 if (dlightbits[l >> 5] & (1 << (l & 31)))
703 light = &r_dlight[l];
704 Matrix4x4_Transform(matrix, light->origin, lightorigin);
705 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
707 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
708 if (f < light->rtlight.lightmap_cullradius2)
710 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
711 VectorMA(c, f, light->rtlight.lightmap_light, c);
720 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
722 const msurface_t *surf;
725 // LordHavoc: HalfLife maps have freaky skypolys...
726 if (ent->model->brush.ishlbsp)
731 skyrendernow = false;
736 R_Mesh_Matrix(&ent->matrix);
738 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
741 // depth-only (masking)
742 GL_ColorMask(0,0,0,0);
743 // just to make sure that braindead drivers don't draw anything
744 // despite that colormask...
745 GL_BlendFunc(GL_ZERO, GL_ONE);
750 GL_BlendFunc(GL_ONE, GL_ZERO);
755 memset(&m, 0, sizeof(m));
756 while((surf = *surfchain++) != NULL)
758 if (surf->visframe == r_framecount)
760 m.pointer_vertex = surf->mesh.data_vertex3f;
762 GL_LockArrays(0, surf->mesh.num_vertices);
763 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
767 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
770 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
772 const entity_render_t *ent = calldata1;
773 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
776 float base, colorscale;
779 float args[4] = {0.05f,0,0,0.04f};
780 int rendertype, turb, fullbright;
782 R_Mesh_Matrix(&ent->matrix);
783 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
785 texture = surf->texinfo->texture;
786 if (texture->animated)
787 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];
788 currentalpha = ent->alpha;
789 if (surf->flags & SURF_WATERALPHA)
790 currentalpha *= r_wateralpha.value;
793 if (ent->effects & EF_ADDITIVE)
795 rendertype = SURFRENDER_ADD;
796 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
799 else if (currentalpha < 1 || texture->skin.fog != NULL)
801 rendertype = SURFRENDER_ALPHA;
802 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
807 rendertype = SURFRENDER_OPAQUE;
808 GL_BlendFunc(GL_ONE, GL_ZERO);
812 turb = (surf->flags & SURF_DRAWTURB) && r_waterscroll.value;
813 fullbright = !(ent->flags & RENDER_LIGHT) || (surf->flags & SURF_DRAWFULLBRIGHT) || !surf->samples;
814 base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
815 if (surf->flags & SURF_DRAWTURB)
817 if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled)
819 // NVIDIA Geforce3 distortion texture shader on water
820 GL_Color(1, 1, 1, currentalpha);
821 memset(&m, 0, sizeof(m));
822 m.pointer_vertex = surf->mesh.data_vertex3f;
823 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
824 m.tex[1] = R_GetTexture(texture->skin.base);
825 m.texcombinergb[0] = GL_REPLACE;
826 m.texcombinergb[1] = GL_REPLACE;
827 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
828 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
829 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
830 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
834 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
836 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
837 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
838 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
839 qglEnable(GL_TEXTURE_SHADER_NV);
841 GL_LockArrays(0, surf->mesh.num_vertices);
842 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
845 qglDisable(GL_TEXTURE_SHADER_NV);
846 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
851 memset(&m, 0, sizeof(m));
852 m.pointer_vertex = surf->mesh.data_vertex3f;
853 m.pointer_color = varray_color4f;
854 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
855 m.tex[0] = R_GetTexture(texture->skin.base);
858 // scrolling in texture matrix
859 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
862 if (gl_combine.integer)
864 m.texrgbscale[0] = 4;
867 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
870 if (surf->dlightframe == r_framecount)
871 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
873 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);
875 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
877 GL_LockArrays(0, surf->mesh.num_vertices);
878 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
880 if (texture->skin.glow)
882 memset(&m, 0, sizeof(m));
883 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
885 m.pointer_color = varray_color4f;
886 m.tex[0] = R_GetTexture(texture->skin.glow);
887 m.pointer_vertex = surf->mesh.data_vertex3f;
890 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
893 // scrolling in texture matrix
894 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
898 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
899 GL_LockArrays(0, surf->mesh.num_vertices);
900 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
903 if (fogenabled && rendertype != SURFRENDER_ADD)
905 memset(&m, 0, sizeof(m));
906 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
908 m.pointer_color = varray_color4f;
909 m.tex[0] = R_GetTexture(texture->skin.fog);
910 m.pointer_vertex = surf->mesh.data_vertex3f;
913 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
916 // scrolling in texture matrix
917 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
921 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
922 GL_LockArrays(0, surf->mesh.num_vertices);
923 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
929 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
931 const msurface_t *surf;
933 int lightmaptexturenum;
934 memset(&m, 0, sizeof(m));
935 GL_BlendFunc(GL_ONE, GL_ZERO);
938 m.tex[0] = R_GetTexture(texture->skin.base);
939 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
940 m.texrgbscale[1] = 2;
941 m.tex[2] = R_GetTexture(texture->skin.detail);
942 m.texrgbscale[2] = 2;
943 if (texture->skin.glow)
945 m.tex[3] = R_GetTexture(texture->skin.glow);
946 m.texcombinergb[3] = GL_ADD;
948 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
950 while((surf = *surfchain++) != NULL)
952 if (surf->visframe == r_framecount)
954 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
955 m.tex[1] = lightmaptexturenum;
956 m.pointer_vertex = surf->mesh.data_vertex3f;
957 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
958 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
959 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
960 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
962 GL_LockArrays(0, surf->mesh.num_vertices);
963 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
969 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
971 const msurface_t *surf;
973 int lightmaptexturenum;
974 memset(&m, 0, sizeof(m));
975 GL_BlendFunc(GL_ONE, GL_ZERO);
978 m.tex[0] = R_GetTexture(texture->skin.base);
979 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
980 m.texrgbscale[1] = 2;
981 m.tex[2] = R_GetTexture(texture->skin.detail);
982 m.texrgbscale[2] = 2;
983 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
985 while((surf = *surfchain++) != NULL)
987 if (surf->visframe == r_framecount)
989 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
990 m.tex[1] = lightmaptexturenum;
991 m.pointer_vertex = surf->mesh.data_vertex3f;
992 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
993 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
994 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
996 GL_LockArrays(0, surf->mesh.num_vertices);
997 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1003 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1005 const msurface_t *surf;
1007 int lightmaptexturenum;
1008 memset(&m, 0, sizeof(m));
1009 GL_BlendFunc(GL_ONE, GL_ZERO);
1012 m.tex[0] = R_GetTexture(texture->skin.base);
1013 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1014 m.texrgbscale[1] = 2;
1015 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
1016 while((surf = *surfchain++) != NULL)
1018 if (surf->visframe == r_framecount)
1020 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1021 m.tex[1] = lightmaptexturenum;
1022 m.pointer_vertex = surf->mesh.data_vertex3f;
1023 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1024 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1026 GL_LockArrays(0, surf->mesh.num_vertices);
1027 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1028 GL_LockArrays(0, 0);
1033 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1035 const msurface_t *surf;
1037 memset(&m, 0, sizeof(m));
1040 GL_BlendFunc(GL_ONE, GL_ZERO);
1041 m.tex[0] = R_GetTexture(texture->skin.base);
1042 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
1043 while((surf = *surfchain++) != NULL)
1045 if (surf->visframe == r_framecount)
1047 m.pointer_vertex = surf->mesh.data_vertex3f;
1048 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1050 GL_LockArrays(0, surf->mesh.num_vertices);
1051 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1052 GL_LockArrays(0, 0);
1057 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1059 const msurface_t *surf;
1061 int lightmaptexturenum;
1062 memset(&m, 0, sizeof(m));
1063 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1064 GL_DepthMask(false);
1066 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1067 GL_Color(1, 1, 1, 1);
1068 while((surf = *surfchain++) != NULL)
1070 if (surf->visframe == r_framecount)
1072 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1073 m.tex[0] = lightmaptexturenum;
1074 m.pointer_vertex = surf->mesh.data_vertex3f;
1075 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1077 GL_LockArrays(0, surf->mesh.num_vertices);
1078 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1079 GL_LockArrays(0, 0);
1084 static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1086 const msurface_t *surf;
1088 memset(&m, 0, sizeof(m));
1089 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1090 GL_DepthMask(false);
1092 m.tex[0] = R_GetTexture(texture->skin.base);
1093 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1094 while((surf = *surfchain++) != NULL)
1096 if (surf->visframe == r_framecount)
1098 m.pointer_vertex = surf->mesh.data_vertex3f;
1099 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1101 GL_LockArrays(0, surf->mesh.num_vertices);
1102 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1103 GL_LockArrays(0, 0);
1108 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1110 const msurface_t *surf;
1113 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1114 memset(&m, 0, sizeof(m));
1115 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1116 GL_DepthMask(false);
1118 m.pointer_color = varray_color4f;
1119 while((surf = *surfchain++) != NULL)
1121 if (surf->visframe == r_framecount)
1123 m.pointer_vertex = surf->mesh.data_vertex3f;
1125 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1127 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1128 GL_LockArrays(0, surf->mesh.num_vertices);
1129 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1130 GL_LockArrays(0, 0);
1135 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1137 const msurface_t *surf;
1139 memset(&m, 0, sizeof(m));
1140 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1141 GL_DepthMask(false);
1143 m.tex[0] = R_GetTexture(texture->skin.detail);
1144 GL_Color(1, 1, 1, 1);
1145 while((surf = *surfchain++) != NULL)
1147 if (surf->visframe == r_framecount)
1149 m.pointer_vertex = surf->mesh.data_vertex3f;
1150 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1152 GL_LockArrays(0, surf->mesh.num_vertices);
1153 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1154 GL_LockArrays(0, 0);
1159 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1161 const msurface_t *surf;
1163 memset(&m, 0, sizeof(m));
1164 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1165 GL_DepthMask(false);
1167 m.tex[0] = R_GetTexture(texture->skin.glow);
1168 GL_Color(1, 1, 1, 1);
1169 while((surf = *surfchain++) != NULL)
1171 if (surf->visframe == r_framecount)
1173 m.pointer_vertex = surf->mesh.data_vertex3f;
1174 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1176 GL_LockArrays(0, surf->mesh.num_vertices);
1177 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1178 GL_LockArrays(0, 0);
1184 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1186 const msurface_t *surf;
1188 memset(&m, 0, sizeof(m));
1189 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1191 m.tex[0] = R_GetTexture(texture->skin.glow);
1193 GL_Color(1, 1, 1, 1);
1195 GL_Color(0, 0, 0, 1);
1196 while((surf = *surfchain++) != NULL)
1198 if (surf->visframe == r_framecount)
1200 m.pointer_vertex = surf->mesh.data_vertex3f;
1201 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1203 GL_LockArrays(0, surf->mesh.num_vertices);
1204 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1205 GL_LockArrays(0, 0);
1211 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1213 const msurface_t *surf;
1215 int lightmaptexturenum;
1216 memset(&m, 0, sizeof(m));
1217 GL_BlendFunc(GL_ONE, GL_ZERO);
1220 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1221 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
1222 while((surf = *surfchain++) != NULL)
1224 if (surf->visframe == r_framecount)
1226 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1227 m.tex[0] = lightmaptexturenum;
1228 m.pointer_vertex = surf->mesh.data_vertex3f;
1229 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1231 GL_LockArrays(0, surf->mesh.num_vertices);
1232 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1233 GL_LockArrays(0, 0);
1238 void R_UpdateTextureInfo(entity_render_t *ent)
1240 int i, texframe, alttextures;
1246 alttextures = ent->frame != 0;
1247 texframe = (int)(cl.time * 5.0f);
1248 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1250 t = ent->model->brushq1.textures + i;
1251 t->currentalpha = ent->alpha;
1252 if (t->flags & SURF_WATERALPHA)
1253 t->currentalpha *= r_wateralpha.value;
1254 if (ent->effects & EF_ADDITIVE)
1255 t->rendertype = SURFRENDER_ADD;
1256 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1257 t->rendertype = SURFRENDER_ALPHA;
1259 t->rendertype = SURFRENDER_OPAQUE;
1260 // we don't need to set currentframe if t->animated is false because
1261 // it was already set up by the texture loader for non-animating
1263 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1267 void R_UpdateLightmapInfo(entity_render_t *ent)
1270 msurface_t *surface, **surfacechain;
1271 model_t *model = ent->model;
1274 if (r_dynamic.integer && !r_rtdlight)
1276 for (i = 0;i < model->brushq1.light_styles;i++)
1278 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1280 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1281 for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1282 surface->cached_dlight = true;
1287 void R_PrepareSurfaces(entity_render_t *ent)
1289 int i, numsurfaces, *surfacevisframes;
1291 msurface_t *surf, *surfaces;
1298 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1299 numsurfaces = model->nummodelsurfaces;
1300 surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1301 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1302 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1304 if (surfacevisframes[i] == r_framecount)
1306 #if !WORLDNODECULLBACKFACES
1307 // mark any backface surfaces as not visible
1308 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1310 if (!(surf->flags & SURF_PLANEBACK))
1311 surfacevisframes[i] = -1;
1315 if ((surf->flags & SURF_PLANEBACK))
1316 surfacevisframes[i] = -1;
1318 if (surfacevisframes[i] == r_framecount)
1322 surf->visframe = r_framecount;
1323 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1324 R_BuildLightMap(ent, surf);
1330 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1332 int texturenumber, f;
1334 msurface_t *surf, **chain, **surfchain;
1335 texture_t *t, *texture;
1336 model_t *model = ent->model;
1339 R_Mesh_Matrix(&ent->matrix);
1340 for (texturenumber = 0, t = model->brushq1.textures;texturenumber < model->brushq1.numtextures;texturenumber++, t++)
1342 if ((f = t->flags & flagsmask) && (texture = t->currentframe) && (surfchain = model->brushq1.pvstexturechains[texturenumber]) != NULL)
1344 if (texture->flags & SURF_LIGHTMAP)
1346 if (gl_lightmaps.integer)
1348 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1350 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1352 else if (texture->rendertype != SURFRENDER_OPAQUE)
1354 // transparent vertex shaded from lightmap
1355 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1357 if (surf->visframe == r_framecount)
1359 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1360 R_MeshQueue_AddTransparent(center, RSurfShader_Transparent_Callback, ent, surf - ent->model->brushq1.surfaces);
1366 if (!(ent->flags & RENDER_LIGHT))
1368 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1369 if (r_detailtextures.integer)
1370 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1371 if (texture->skin.glow)
1372 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1374 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1375 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1378 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1379 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1382 if (r_textureunits.integer >= 2 && gl_combine.integer)
1383 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1386 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1387 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1389 if (r_ambient.value > 0)
1390 RSurfShader_OpaqueWall_Pass_BaseAmbient(ent, texture, surfchain);
1391 if (r_detailtextures.integer)
1392 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1394 if (texture->skin.glow)
1395 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1398 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1401 else if (texture->flags & SURF_DRAWTURB)
1403 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1405 if (surf->visframe == r_framecount)
1407 if (texture->rendertype == SURFRENDER_OPAQUE)
1408 RSurfShader_Transparent_Callback(ent, surf - ent->model->brushq1.surfaces);
1411 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1412 R_MeshQueue_AddTransparent(center, RSurfShader_Transparent_Callback, ent, surf - ent->model->brushq1.surfaces);
1417 else if (texture->flags & SURF_DRAWSKY)
1418 RSurfShader_Sky(ent, texture, surfchain);
1423 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1428 const entity_render_t *ent = calldata1;
1429 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1430 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1431 GL_DepthMask(false);
1433 R_Mesh_Matrix(&ent->matrix);
1435 memset(&m, 0, sizeof(m));
1436 m.pointer_vertex = varray_vertex3f;
1439 i = portal - ent->model->brushq1.portals;
1440 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1441 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1442 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1444 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1446 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1447 VectorCopy(portal->points[i].position, v);
1450 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1451 VectorCopy(portal->points[i].position, v);
1452 GL_LockArrays(0, portal->numpoints);
1453 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1454 GL_LockArrays(0, 0);
1457 // LordHavoc: this is just a nice debugging tool, very slow
1458 static void R_DrawPortals(entity_render_t *ent)
1461 mportal_t *portal, *endportal;
1462 float temp[3], center[3], f;
1463 if (ent->model == NULL)
1465 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1467 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1470 for (i = 0;i < portal->numpoints;i++)
1471 VectorAdd(temp, portal->points[i].position, temp);
1472 f = ixtable[portal->numpoints];
1473 VectorScale(temp, f, temp);
1474 Matrix4x4_Transform(&ent->matrix, temp, center);
1475 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1480 void R_PrepareBrushModel(entity_render_t *ent)
1482 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1485 #if WORLDNODECULLBACKFACES
1489 // because bmodels can be reused, we have to decide which things to render
1490 // from scratch every time
1494 #if WORLDNODECULLBACKFACES
1495 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1497 numsurfaces = model->nummodelsurfaces;
1498 surf = model->brushq1.surfaces + model->firstmodelsurface;
1499 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1500 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1501 for (i = 0;i < numsurfaces;i++, surf++)
1503 #if WORLDNODECULLBACKFACES
1504 // mark any backface surfaces as not visible
1505 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1507 if ((surf->flags & SURF_PLANEBACK))
1508 surfacevisframes[i] = r_framecount;
1510 else if (!(surf->flags & SURF_PLANEBACK))
1511 surfacevisframes[i] = r_framecount;
1513 surfacevisframes[i] = r_framecount;
1515 surf->dlightframe = -1;
1517 R_UpdateTextureInfo(ent);
1518 R_UpdateLightmapInfo(ent);
1521 void R_SurfaceWorldNode (entity_render_t *ent)
1523 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1529 // equivilant to quake's RecursiveWorldNode but faster and more effective
1533 surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1534 surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1535 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1537 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1539 if (!R_CullBox (leaf->mins, leaf->maxs))
1542 leaf->visframe = r_framecount;
1546 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1548 surfnum = model->brushq1.pvssurflist[i];
1549 surf = model->brushq1.surfaces + surfnum;
1550 #if WORLDNODECULLBACKFACES
1551 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1553 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1554 surfacevisframes[surfnum] = r_framecount;
1558 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1559 surfacevisframes[surfnum] = r_framecount;
1562 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1563 surfacevisframes[surfnum] = r_framecount;
1568 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1570 int c, leafstackpos, *mark, *surfacevisframes;
1571 #if WORLDNODECULLBACKFACES
1575 mleaf_t *leaf, *leafstack[8192];
1578 msurface_t *surfaces;
1579 if (ent->model == NULL)
1581 // LordHavoc: portal-passage worldnode with PVS;
1582 // follows portals leading outward from viewleaf, does not venture
1583 // offscreen or into leafs that are not visible, faster than Quake's
1584 // RecursiveWorldNode
1585 surfaces = ent->model->brushq1.surfaces;
1586 surfacevisframes = ent->model->brushq1.surfacevisframes;
1587 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1588 viewleaf->worldnodeframe = r_framecount;
1589 leafstack[0] = viewleaf;
1591 while (leafstackpos)
1594 leaf = leafstack[--leafstackpos];
1595 leaf->visframe = r_framecount;
1596 // draw any surfaces bounding this leaf
1597 if (leaf->nummarksurfaces)
1599 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1601 #if WORLDNODECULLBACKFACES
1603 if (surfacevisframes[n] != r_framecount)
1605 surf = surfaces + n;
1606 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1608 if ((surf->flags & SURF_PLANEBACK))
1609 surfacevisframes[n] = r_framecount;
1613 if (!(surf->flags & SURF_PLANEBACK))
1614 surfacevisframes[n] = r_framecount;
1618 surfacevisframes[*mark++] = r_framecount;
1622 // follow portals into other leafs
1623 for (p = leaf->portals;p;p = p->next)
1625 // LordHavoc: this DotProduct hurts less than a cache miss
1626 // (which is more likely to happen if backflowing through leafs)
1627 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1630 if (leaf->worldnodeframe != r_framecount)
1632 leaf->worldnodeframe = r_framecount;
1633 // FIXME: R_CullBox is absolute, should be done relative
1634 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1635 leafstack[leafstackpos++] = leaf;
1642 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1644 int j, c, *surfacepvsframes, *mark;
1649 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1651 model->brushq1.pvsframecount++;
1652 model->brushq1.pvsviewleaf = viewleaf;
1653 model->brushq1.pvsviewleafnovis = r_novis.integer;
1654 model->brushq1.pvsleafchain = NULL;
1655 model->brushq1.pvssurflistlength = 0;
1658 surfacepvsframes = model->brushq1.surfacepvsframes;
1659 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1661 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1663 leaf->pvsframe = model->brushq1.pvsframecount;
1664 leaf->pvschain = model->brushq1.pvsleafchain;
1665 model->brushq1.pvsleafchain = leaf;
1666 // mark surfaces bounding this leaf as visible
1667 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1668 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1671 model->brushq1.BuildPVSTextureChains(model);
1676 void R_WorldVisibility(entity_render_t *ent)
1681 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1682 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1683 R_PVSUpdate(ent, viewleaf);
1688 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1689 R_SurfaceWorldNode (ent);
1691 R_PortalWorldNode (ent, viewleaf);
1693 if (r_drawportals.integer)
1697 void R_Q1BSP_DrawSky(entity_render_t *ent)
1699 if (ent->model == NULL)
1701 if (ent != &cl_entities[0].render)
1702 R_PrepareBrushModel(ent);
1703 R_PrepareSurfaces(ent);
1704 R_DrawSurfaces(ent, SURF_DRAWSKY);
1707 void R_Q1BSP_Draw(entity_render_t *ent)
1709 if (ent->model == NULL)
1712 if (ent != &cl_entities[0].render)
1713 R_PrepareBrushModel(ent);
1714 R_PrepareSurfaces(ent);
1715 R_UpdateTextureInfo(ent);
1716 R_UpdateLightmapInfo(ent);
1717 R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1720 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)
1722 model_t *model = ent->model;
1723 vec3_t lightmins, lightmaxs;
1724 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1727 msurface_t *surface;
1730 lightmins[0] = relativelightorigin[0] - lightradius;
1731 lightmins[1] = relativelightorigin[1] - lightradius;
1732 lightmins[2] = relativelightorigin[2] - lightradius;
1733 lightmaxs[0] = relativelightorigin[0] + lightradius;
1734 lightmaxs[1] = relativelightorigin[1] + lightradius;
1735 lightmaxs[2] = relativelightorigin[2] + lightradius;
1736 *outnumclusterspointer = 0;
1737 *outnumsurfacespointer = 0;
1738 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1739 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1742 VectorCopy(lightmins, outmins);
1743 VectorCopy(lightmaxs, outmaxs);
1746 VectorCopy(relativelightorigin, outmins);
1747 VectorCopy(relativelightorigin, outmaxs);
1748 if (model->brush.GetPVS)
1749 pvs = model->brush.GetPVS(model, relativelightorigin);
1752 // FIXME: use BSP recursion as lights are often small
1753 for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1755 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1757 outmins[0] = min(outmins[0], leaf->mins[0]);
1758 outmins[1] = min(outmins[1], leaf->mins[1]);
1759 outmins[2] = min(outmins[2], leaf->mins[2]);
1760 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1761 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1762 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1765 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1767 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1768 outclusterlist[outnumclusters++] = leaf->clusterindex;
1773 for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1775 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1776 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1778 surface = model->brushq1.surfaces + surfaceindex;
1779 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1781 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1783 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1784 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1785 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1786 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])))
1788 SETPVSBIT(outsurfacepvs, surfaceindex);
1789 outsurfacelist[outnumsurfaces++] = surfaceindex;
1800 // limit combined leaf box to light boundaries
1801 outmins[0] = max(outmins[0], lightmins[0]);
1802 outmins[1] = max(outmins[1], lightmins[1]);
1803 outmins[2] = max(outmins[2], lightmins[2]);
1804 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1805 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1806 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1808 *outnumclusterspointer = outnumclusters;
1809 *outnumsurfacespointer = outnumsurfaces;
1812 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1814 model_t *model = ent->model;
1815 vec3_t lightmins, lightmaxs;
1816 msurface_t *surface;
1817 int surfacelistindex, j, t;
1820 if (r_drawcollisionbrushes.integer < 2)
1822 lightmins[0] = relativelightorigin[0] - lightradius;
1823 lightmins[1] = relativelightorigin[1] - lightradius;
1824 lightmins[2] = relativelightorigin[2] - lightradius;
1825 lightmaxs[0] = relativelightorigin[0] + lightradius;
1826 lightmaxs[1] = relativelightorigin[1] + lightradius;
1827 lightmaxs[2] = relativelightorigin[2] + lightradius;
1828 R_Mesh_Matrix(&ent->matrix);
1829 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1830 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1832 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1833 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1835 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1836 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1837 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1838 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])))
1839 shadowmarklist[numshadowmark++] = t;
1842 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);
1846 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)
1848 model_t *model = ent->model;
1849 vec3_t lightmins, lightmaxs, modelorg;
1850 msurface_t *surface;
1852 int surfacelistindex;
1853 if (r_drawcollisionbrushes.integer < 2)
1855 lightmins[0] = relativelightorigin[0] - lightradius;
1856 lightmins[1] = relativelightorigin[1] - lightradius;
1857 lightmins[2] = relativelightorigin[2] - lightradius;
1858 lightmaxs[0] = relativelightorigin[0] + lightradius;
1859 lightmaxs[1] = relativelightorigin[1] + lightradius;
1860 lightmaxs[2] = relativelightorigin[2] + lightradius;
1861 R_Mesh_Matrix(&ent->matrix);
1862 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1863 R_UpdateTextureInfo(ent);
1864 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1866 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1867 if (r_shadow_compilingrtlight)
1869 // if compiling an rtlight, capture the mesh
1870 t = surface->texinfo->texture;
1871 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1872 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);
1874 else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1876 t = surface->texinfo->texture->currentframe;
1877 if (t->flags & SURF_LIGHTMAP)
1878 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);
1884 void R_DrawCollisionBrush(colbrushf_t *brush)
1888 memset(&m, 0, sizeof(m));
1889 m.pointer_vertex = brush->points->v;
1891 i = ((int)brush) / sizeof(colbrushf_t);
1892 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1893 GL_LockArrays(0, brush->numpoints);
1894 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1895 GL_LockArrays(0, 0);
1898 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1902 if (!face->num_collisiontriangles)
1904 memset(&m, 0, sizeof(m));
1905 m.pointer_vertex = face->data_collisionvertex3f;
1907 i = ((int)face) / sizeof(q3msurface_t);
1908 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1909 GL_LockArrays(0, face->num_collisionvertices);
1910 R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1911 GL_LockArrays(0, 0);
1914 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1917 if (!face->num_triangles)
1922 skyrendernow = false;
1923 if (skyrendermasked)
1926 if (!r_q3bsp_renderskydepth.integer)
1929 R_Mesh_Matrix(&ent->matrix);
1931 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1932 if (skyrendermasked)
1934 // depth-only (masking)
1935 GL_ColorMask(0,0,0,0);
1936 // just to make sure that braindead drivers don't draw anything
1937 // despite that colormask...
1938 GL_BlendFunc(GL_ZERO, GL_ONE);
1943 GL_BlendFunc(GL_ONE, GL_ZERO);
1948 memset(&m, 0, sizeof(m));
1949 m.pointer_vertex = face->data_vertex3f;
1952 GL_LockArrays(0, face->num_vertices);
1953 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1954 GL_LockArrays(0, 0);
1955 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1958 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
1961 memset(&m, 0, sizeof(m));
1962 GL_BlendFunc(GL_ONE, GL_ZERO);
1965 if (face->texture->skin.glow)
1967 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1968 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1969 GL_Color(1, 1, 1, 1);
1972 GL_Color(0, 0, 0, 1);
1973 m.pointer_vertex = face->data_vertex3f;
1975 GL_LockArrays(0, face->num_vertices);
1976 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1977 GL_LockArrays(0, 0);
1980 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
1983 memset(&m, 0, sizeof(m));
1984 GL_BlendFunc(GL_ONE, GL_ZERO);
1987 m.tex[0] = R_GetTexture(face->texture->skin.base);
1988 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1989 m.tex[1] = R_GetTexture(face->lightmaptexture);
1990 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1991 m.texrgbscale[1] = 2;
1992 GL_Color(1, 1, 1, 1);
1993 m.pointer_vertex = face->data_vertex3f;
1995 GL_LockArrays(0, face->num_vertices);
1996 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1997 GL_LockArrays(0, 0);
2000 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2003 memset(&m, 0, sizeof(m));
2004 GL_BlendFunc(GL_ONE, GL_ZERO);
2007 m.tex[0] = R_GetTexture(face->texture->skin.base);
2008 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2009 GL_Color(1, 1, 1, 1);
2010 m.pointer_vertex = face->data_vertex3f;
2012 GL_LockArrays(0, face->num_vertices);
2013 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2014 GL_LockArrays(0, 0);
2017 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2020 memset(&m, 0, sizeof(m));
2021 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2022 GL_DepthMask(false);
2024 m.tex[0] = R_GetTexture(face->lightmaptexture);
2025 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2026 GL_Color(1, 1, 1, 1);
2027 m.pointer_vertex = face->data_vertex3f;
2029 GL_LockArrays(0, face->num_vertices);
2030 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2031 GL_LockArrays(0, 0);
2034 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2037 memset(&m, 0, sizeof(m));
2038 GL_BlendFunc(GL_ONE, GL_ZERO);
2041 m.tex[0] = R_GetTexture(face->lightmaptexture);
2042 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2043 GL_Color(r_lightmapintensity, r_lightmapintensity, r_lightmapintensity, 1);
2044 m.pointer_vertex = face->data_vertex3f;
2046 GL_LockArrays(0, face->num_vertices);
2047 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2048 GL_LockArrays(0, 0);
2051 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
2054 memset(&m, 0, sizeof(m));
2055 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2056 GL_DepthMask(false);
2058 if (face->texture->skin.glow)
2060 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2061 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2062 GL_Color(1, 1, 1, 1);
2065 GL_Color(0, 0, 0, 1);
2066 m.pointer_vertex = face->data_vertex3f;
2068 GL_LockArrays(0, face->num_vertices);
2069 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2070 GL_LockArrays(0, 0);
2073 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(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->texture->skin.base);
2083 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2084 mul = 2.0f * r_lightmapintensity;
2085 if (mul == 2 && gl_combine.integer)
2087 m.texrgbscale[0] = 2;
2088 m.pointer_color = face->data_color4f;
2091 m.pointer_color = face->data_color4f;
2094 for (i = 0;i < face->num_vertices;i++)
2096 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2097 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2098 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2099 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2101 m.pointer_color = varray_color4f;
2103 m.pointer_vertex = face->data_vertex3f;
2105 GL_LockArrays(0, face->num_vertices);
2106 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2107 GL_LockArrays(0, 0);
2110 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2115 memset(&m, 0, sizeof(m));
2116 GL_BlendFunc(GL_ONE, GL_ZERO);
2119 mul = 2.0f * r_lightmapintensity;
2121 m.pointer_color = face->data_color4f;
2124 for (i = 0;i < face->num_vertices;i++)
2126 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2127 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2128 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2129 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2131 m.pointer_color = varray_color4f;
2133 m.pointer_vertex = face->data_vertex3f;
2135 GL_LockArrays(0, face->num_vertices);
2136 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2137 GL_LockArrays(0, 0);
2140 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2143 memset(&m, 0, sizeof(m));
2144 GL_BlendFunc(GL_ONE, GL_ONE);
2147 m.tex[0] = R_GetTexture(face->texture->skin.base);
2148 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2149 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2150 m.pointer_vertex = face->data_vertex3f;
2152 GL_LockArrays(0, face->num_vertices);
2153 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2154 GL_LockArrays(0, 0);
2157 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2161 const entity_render_t *ent = voident;
2162 q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2164 R_Mesh_Matrix(&ent->matrix);
2165 memset(&m, 0, sizeof(m));
2166 if (ent->effects & EF_ADDITIVE)
2167 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2169 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2170 GL_DepthMask(false);
2172 m.tex[0] = R_GetTexture(face->texture->skin.base);
2173 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2174 colorscale = r_lightmapintensity;
2175 if (gl_combine.integer)
2176 m.texrgbscale[0] = 2;
2179 for (i = 0;i < face->num_vertices;i++)
2181 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * colorscale;
2182 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * colorscale;
2183 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * colorscale;
2184 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2186 m.pointer_color = varray_color4f;
2187 m.pointer_vertex = face->data_vertex3f;
2189 qglDisable(GL_CULL_FACE);
2190 GL_LockArrays(0, face->num_vertices);
2191 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2192 GL_LockArrays(0, 0);
2193 qglEnable(GL_CULL_FACE);
2196 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2198 if (!face->num_triangles)
2200 if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2203 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2205 vec3_t facecenter, center;
2206 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2207 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2208 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2209 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2210 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2213 R_Mesh_Matrix(&ent->matrix);
2214 if (r_lightmapintensity <= 0)
2215 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2216 else if (!(ent->flags & RENDER_LIGHT))
2218 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2219 if (face->texture->skin.glow)
2220 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2222 else if (face->lightmaptexture)
2224 if (gl_lightmaps.integer)
2225 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2228 if (r_textureunits.integer >= 2 && gl_combine.integer)
2229 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2232 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2233 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2235 if (face->texture->skin.glow)
2236 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2241 if (gl_lightmaps.integer)
2242 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2245 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2246 if (face->texture->skin.glow)
2247 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2250 if (r_ambient.value)
2251 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2254 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2260 if (R_CullBox(node->mins, node->maxs))
2265 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2266 node = node->children[1];
2268 leaf = (q3mleaf_t *)node;
2269 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2272 for (i = 0;i < leaf->numleaffaces;i++)
2273 leaf->firstleafface[i]->visframe = r_framecount;
2277 // FIXME: num_leafs needs to be recalculated at load time to include only
2278 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2279 // the submodels (which would render the submodels occasionally, as part of
2280 // the world - not good)
2281 void R_Q3BSP_MarkLeafPVS(void)
2285 for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2287 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2290 for (i = 0;i < leaf->numleaffaces;i++)
2291 leaf->firstleafface[i]->visframe = r_framecount;
2296 static int r_q3bsp_framecount = -1;
2298 void R_Q3BSP_DrawSky(entity_render_t *ent)
2304 R_Mesh_Matrix(&ent->matrix);
2306 if (r_drawcollisionbrushes.integer < 2)
2308 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2309 if (ent == &cl_entities[0].render)
2311 if (r_q3bsp_framecount != r_framecount)
2313 r_q3bsp_framecount = r_framecount;
2314 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2315 //R_Q3BSP_MarkLeafPVS();
2317 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2318 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2319 R_Q3BSP_DrawSkyFace(ent, face);
2322 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2323 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2324 R_Q3BSP_DrawSkyFace(ent, face);
2328 void R_Q3BSP_Draw(entity_render_t *ent)
2335 R_Mesh_Matrix(&ent->matrix);
2337 if (r_drawcollisionbrushes.integer < 2)
2339 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2340 if (ent == &cl_entities[0].render)
2342 if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2343 if (r_q3bsp_framecount != r_framecount)
2345 r_q3bsp_framecount = r_framecount;
2346 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2347 //R_Q3BSP_MarkLeafPVS();
2349 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2350 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2351 R_Q3BSP_DrawFace(ent, face);
2354 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2355 R_Q3BSP_DrawFace(ent, face);
2357 if (r_drawcollisionbrushes.integer >= 1)
2359 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2360 GL_DepthMask(false);
2362 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2363 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2364 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2365 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2366 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2367 if (face->num_collisiontriangles)
2368 R_Q3BSP_DrawCollisionFace(ent, face);
2369 qglPolygonOffset(0, 0);
2373 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)
2375 model_t *model = ent->model;
2376 vec3_t lightmins, lightmaxs;
2377 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2380 q3msurface_t *surface;
2383 lightmins[0] = relativelightorigin[0] - lightradius;
2384 lightmins[1] = relativelightorigin[1] - lightradius;
2385 lightmins[2] = relativelightorigin[2] - lightradius;
2386 lightmaxs[0] = relativelightorigin[0] + lightradius;
2387 lightmaxs[1] = relativelightorigin[1] + lightradius;
2388 lightmaxs[2] = relativelightorigin[2] + lightradius;
2389 *outnumclusterspointer = 0;
2390 *outnumsurfacespointer = 0;
2391 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2392 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2395 VectorCopy(lightmins, outmins);
2396 VectorCopy(lightmaxs, outmaxs);
2399 VectorCopy(relativelightorigin, outmins);
2400 VectorCopy(relativelightorigin, outmaxs);
2401 if (model->brush.GetPVS)
2402 pvs = model->brush.GetPVS(model, relativelightorigin);
2405 // FIXME: use BSP recursion as lights are often small
2406 for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2408 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2410 outmins[0] = min(outmins[0], leaf->mins[0]);
2411 outmins[1] = min(outmins[1], leaf->mins[1]);
2412 outmins[2] = min(outmins[2], leaf->mins[2]);
2413 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2414 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2415 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2418 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2420 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2421 outclusterlist[outnumclusters++] = leaf->clusterindex;
2426 for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2428 surface = leaf->firstleafface[marksurfaceindex];
2429 surfaceindex = surface - model->brushq3.data_faces;
2430 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2432 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2434 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2436 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2437 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2438 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2439 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])))
2441 SETPVSBIT(outsurfacepvs, surfaceindex);
2442 outsurfacelist[outnumsurfaces++] = surfaceindex;
2453 // limit combined leaf box to light boundaries
2454 outmins[0] = max(outmins[0], lightmins[0]);
2455 outmins[1] = max(outmins[1], lightmins[1]);
2456 outmins[2] = max(outmins[2], lightmins[2]);
2457 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2458 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2459 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2461 *outnumclusterspointer = outnumclusters;
2462 *outnumsurfacespointer = outnumsurfaces;
2465 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2467 model_t *model = ent->model;
2468 vec3_t lightmins, lightmaxs;
2469 q3msurface_t *surface;
2470 int surfacelistindex, j, t;
2473 if (r_drawcollisionbrushes.integer < 2)
2475 lightmins[0] = relativelightorigin[0] - lightradius;
2476 lightmins[1] = relativelightorigin[1] - lightradius;
2477 lightmins[2] = relativelightorigin[2] - lightradius;
2478 lightmaxs[0] = relativelightorigin[0] + lightradius;
2479 lightmaxs[1] = relativelightorigin[1] + lightradius;
2480 lightmaxs[2] = relativelightorigin[2] + lightradius;
2481 R_Mesh_Matrix(&ent->matrix);
2482 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2483 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2485 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2486 // FIXME: check some manner of face->rendermode here?
2487 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2489 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2491 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2492 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2493 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2494 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])))
2495 shadowmarklist[numshadowmark++] = t;
2499 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);
2503 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)
2505 model_t *model = ent->model;
2506 vec3_t lightmins, lightmaxs, modelorg;
2507 q3msurface_t *surface;
2508 int surfacelistindex;
2509 if (r_drawcollisionbrushes.integer < 2)
2511 lightmins[0] = relativelightorigin[0] - lightradius;
2512 lightmins[1] = relativelightorigin[1] - lightradius;
2513 lightmins[2] = relativelightorigin[2] - lightradius;
2514 lightmaxs[0] = relativelightorigin[0] + lightradius;
2515 lightmaxs[1] = relativelightorigin[1] + lightradius;
2516 lightmaxs[2] = relativelightorigin[2] + lightradius;
2517 R_Mesh_Matrix(&ent->matrix);
2518 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2519 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2521 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2522 if (r_shadow_compilingrtlight)
2524 // if compiling an rtlight, capture the mesh
2525 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);
2527 else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2528 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);
2533 static void gl_surf_start(void)
2537 static void gl_surf_shutdown(void)
2541 static void gl_surf_newmap(void)
2545 void GL_Surf_Init(void)
2548 dlightdivtable[0] = 4194304;
2549 for (i = 1;i < 32768;i++)
2550 dlightdivtable[i] = 4194304 / (i << 7);
2552 Cvar_RegisterVariable(&r_ambient);
2553 Cvar_RegisterVariable(&r_drawportals);
2554 Cvar_RegisterVariable(&r_testvis);
2555 Cvar_RegisterVariable(&r_floatbuildlightmap);
2556 Cvar_RegisterVariable(&r_detailtextures);
2557 Cvar_RegisterVariable(&r_surfaceworldnode);
2558 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2559 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2560 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
2561 Cvar_RegisterVariable(&gl_lightmaps);
2563 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);