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 q3mface_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 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
285 for (i = 0;i < size3;i++)
289 memset(bl, 0, size*3*sizeof(unsigned int));
291 if (surf->dlightframe == r_framecount)
293 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
294 if (surf->cached_dlight)
298 // add all the lightmaps
302 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
303 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
304 bl[i] += lightmap[i] * scale;
308 stain = surf->stainsamples;
311 // the >> 16 shift adjusts down 8 bits to account for the stainmap
312 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
313 // be doubled during rendering to achieve 2x overbright
314 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
315 if (ent->model->brushq1.lightmaprgba)
317 stride = (surf->lightmaptexturestride - smax) * 4;
318 for (i = 0;i < tmax;i++, out += stride)
320 for (j = 0;j < smax;j++)
322 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
323 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
324 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
331 stride = (surf->lightmaptexturestride - smax) * 3;
332 for (i = 0;i < tmax;i++, out += stride)
334 for (j = 0;j < smax;j++)
336 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
337 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
338 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
343 R_UpdateTexture(surf->lightmaptexture, templight);
347 int smax, tmax, i, j, size, size3, maps, stride, l;
349 qbyte *lightmap, *out, *stain;
351 // update cached lighting info
352 surf->cached_dlight = 0;
354 smax = (surf->extents[0]>>4)+1;
355 tmax = (surf->extents[1]>>4)+1;
358 lightmap = surf->samples;
360 // set to full bright if no light data
361 bl = floatblocklights;
362 if (!ent->model->brushq1.lightdata)
365 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
370 for (i = 0;i < size3;i++)
374 memset(bl, 0, size*3*sizeof(float));
376 if (surf->dlightframe == r_framecount)
378 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
379 if (surf->cached_dlight)
383 // add all the lightmaps
386 bl = floatblocklights;
387 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
388 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
389 bl[i] += lightmap[i] * scale;
392 stain = surf->stainsamples;
393 bl = floatblocklights;
395 // this scaling adjusts down 8 bits to account for the stainmap
396 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
397 // be doubled during rendering to achieve 2x overbright
398 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
399 scale = 1.0f / (1 << 16);
400 if (ent->model->brushq1.lightmaprgba)
402 stride = (surf->lightmaptexturestride - smax) * 4;
403 for (i = 0;i < tmax;i++, out += stride)
405 for (j = 0;j < smax;j++)
407 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
408 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
409 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416 stride = (surf->lightmaptexturestride - smax) * 3;
417 for (i = 0;i < tmax;i++, out += stride)
419 for (j = 0;j < smax;j++)
421 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
422 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
423 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
428 R_UpdateTexture(surf->lightmaptexture, templight);
432 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
434 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
435 msurface_t *surf, *endsurf;
436 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
440 maxdist = radius * radius;
441 invradius = 1.0f / radius;
444 if (node->contents < 0)
446 ndist = PlaneDiff(origin, node->plane);
449 node = node->children[0];
454 node = node->children[1];
458 dist2 = ndist * ndist;
459 maxdist3 = maxdist - dist2;
461 if (node->plane->type < 3)
463 VectorCopy(origin, impact);
464 impact[node->plane->type] -= ndist;
468 impact[0] = origin[0] - node->plane->normal[0] * ndist;
469 impact[1] = origin[1] - node->plane->normal[1] * ndist;
470 impact[2] = origin[2] - node->plane->normal[2] * ndist;
473 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
475 if (surf->stainsamples)
477 smax = (surf->extents[0] >> 4) + 1;
478 tmax = (surf->extents[1] >> 4) + 1;
480 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
481 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
483 s = bound(0, impacts, smax * 16) - impacts;
484 t = bound(0, impactt, tmax * 16) - impactt;
485 i = s * s + t * t + dist2;
489 // reduce calculations
490 for (s = 0, i = impacts; s < smax; s++, i -= 16)
491 sdtable[s] = i * i + dist2;
493 bl = surf->stainsamples;
498 for (t = 0;t < tmax;t++, i -= 16)
501 // make sure some part of it is visible on this line
504 maxdist2 = maxdist - td;
505 for (s = 0;s < smax;s++)
507 if (sdtable[s] < maxdist2)
509 ratio = lhrandom(0.0f, 1.0f);
510 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
511 if (a >= (1.0f / 64.0f))
515 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
516 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
517 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
527 // force lightmap upload
529 surf->cached_dlight = true;
533 if (node->children[0]->contents >= 0)
535 if (node->children[1]->contents >= 0)
537 R_StainNode(node->children[0], model, origin, radius, fcolor);
538 node = node->children[1];
543 node = node->children[0];
547 else if (node->children[1]->contents >= 0)
549 node = node->children[1];
554 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)
558 entity_render_t *ent;
561 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
566 fcolor[3] = ca1 * (1.0f / 64.0f);
567 fcolor[4] = cr2 - cr1;
568 fcolor[5] = cg2 - cg1;
569 fcolor[6] = cb2 - cb1;
570 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
572 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
574 // look for embedded bmodels
575 for (n = 0;n < cl_num_brushmodel_entities;n++)
577 ent = cl_brushmodel_entities[n];
579 if (model && model->name[0] == '*')
581 Mod_CheckLoaded(model);
582 if (model->brushq1.nodes)
584 Matrix4x4_Transform(&ent->inversematrix, origin, org);
585 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
593 =============================================================
597 =============================================================
600 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
605 if (styles[0] != 255)
607 for (i = 0;i < numverts;i++, c += 4)
609 lm = samples + lightmapoffsets[i];
610 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
611 VectorMA(c, scale, lm, c);
612 if (styles[1] != 255)
615 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
616 VectorMA(c, scale, lm, c);
617 if (styles[2] != 255)
620 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
621 VectorMA(c, scale, lm, c);
622 if (styles[3] != 255)
625 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
626 VectorMA(c, scale, lm, c);
634 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
640 for (i = 0;i < numverts;i++, v += 3, c += 4)
642 VectorSubtract(v, modelorg, diff);
643 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
644 VectorScale(c, f, c);
647 else if (colorscale != 1)
648 for (i = 0;i < numverts;i++, c += 4)
649 VectorScale(c, colorscale, c);
652 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)
661 for (i = 0;i < numverts;i++, v += 3, c += 4)
663 VectorSubtract(v, modelorg, diff);
664 f = 1 - exp(fogdensity/DotProduct(diff, diff));
673 for (i = 0;i < numverts;i++, c += 4)
683 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)
690 for (i = 0;i < numverts;i++, v += 3, c += 4)
692 VectorSubtract(v, modelorg, diff);
693 f = exp(fogdensity/DotProduct(diff, diff));
701 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
706 int i, l, lit = false;
707 const dlight_t *light;
709 for (l = 0;l < r_numdlights;l++)
711 if (dlightbits[l >> 5] & (1 << (l & 31)))
713 light = &r_dlight[l];
714 Matrix4x4_Transform(matrix, light->origin, lightorigin);
715 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
717 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
718 if (f < light->rtlight.lightmap_cullradius2)
720 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
721 VectorMA(c, f, light->rtlight.lightmap_light, c);
730 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
732 const msurface_t *surf;
735 // LordHavoc: HalfLife maps have freaky skypolys...
736 if (ent->model->brush.ishlbsp)
741 skyrendernow = false;
746 R_Mesh_Matrix(&ent->matrix);
748 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
751 // depth-only (masking)
752 GL_ColorMask(0,0,0,0);
753 // just to make sure that braindead drivers don't draw anything
754 // despite that colormask...
755 GL_BlendFunc(GL_ZERO, GL_ONE);
760 GL_BlendFunc(GL_ONE, GL_ZERO);
765 memset(&m, 0, sizeof(m));
766 while((surf = *surfchain++) != NULL)
768 if (surf->visframe == r_framecount)
770 m.pointer_vertex = surf->mesh.data_vertex3f;
772 GL_LockArrays(0, surf->mesh.num_vertices);
773 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
777 GL_ColorMask(1,1,1,1);
780 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
782 const entity_render_t *ent = calldata1;
783 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
788 float args[4] = {0.05f,0,0,0.04f};
791 R_Mesh_Matrix(&ent->matrix);
792 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
794 texture = surf->texinfo->texture->currentframe;
795 currentalpha = texture->currentalpha;
796 if (ent->effects & EF_ADDITIVE)
798 rendertype = SURFRENDER_ADD;
799 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
802 else if (currentalpha < 1 || texture->skin.fog != NULL)
804 rendertype = SURFRENDER_ALPHA;
805 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
810 rendertype = SURFRENDER_OPAQUE;
811 GL_BlendFunc(GL_ONE, GL_ZERO);
815 GL_Color(1, 1, 1, currentalpha);
816 memset(&m, 0, sizeof(m));
817 m.pointer_vertex = surf->mesh.data_vertex3f;
818 if (gl_textureshader && r_watershader.value && !fogenabled)
820 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
821 m.tex[1] = R_GetTexture(texture->skin.base);
822 m.texcombinergb[0] = GL_REPLACE;
823 m.texcombinergb[1] = GL_REPLACE;
824 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
825 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
826 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
827 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
831 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
833 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
834 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
835 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
836 qglEnable(GL_TEXTURE_SHADER_NV);
838 GL_LockArrays(0, surf->mesh.num_vertices);
839 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
842 qglDisable(GL_TEXTURE_SHADER_NV);
843 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
848 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
849 m.pointer_color = varray_color4f;
850 m.tex[0] = R_GetTexture(texture->skin.base);
851 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
852 if (r_waterscroll.value)
854 // scrolling in texture matrix
855 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
858 GL_LockArrays(0, surf->mesh.num_vertices);
859 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
861 if (fogenabled && rendertype != SURFRENDER_ADD)
863 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
865 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
866 m.tex[0] = R_GetTexture(texture->skin.fog);
868 GL_LockArrays(0, surf->mesh.num_vertices);
869 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
875 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
877 const entity_render_t *ent = calldata1;
878 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
882 float base, colorscale;
885 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
886 R_Mesh_Matrix(&ent->matrix);
888 texture = surf->texinfo->texture;
889 if (texture->animated)
890 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];
892 currentalpha = ent->alpha;
893 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
895 if (ent->effects & EF_ADDITIVE)
897 rendertype = SURFRENDER_ADD;
898 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
901 else if (currentalpha < 1 || texture->skin.fog != NULL)
903 rendertype = SURFRENDER_ALPHA;
904 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
909 rendertype = SURFRENDER_OPAQUE;
910 GL_BlendFunc(GL_ONE, GL_ZERO);
915 memset(&m, 0, sizeof(m));
916 m.tex[0] = R_GetTexture(texture->skin.base);
918 if (gl_combine.integer)
920 m.texrgbscale[0] = 4;
923 m.pointer_color = varray_color4f;
924 m.pointer_vertex = surf->mesh.data_vertex3f;
925 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
927 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
928 if (!(ent->effects & EF_FULLBRIGHT))
930 if (surf->dlightframe == r_framecount)
931 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
932 if (surf->flags & SURF_LIGHTMAP)
933 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);
935 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
936 GL_LockArrays(0, surf->mesh.num_vertices);
937 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
940 if (texture->skin.glow)
942 memset(&m, 0, sizeof(m));
943 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
945 m.pointer_color = varray_color4f;
946 m.tex[0] = R_GetTexture(texture->skin.glow);
947 m.pointer_vertex = surf->mesh.data_vertex3f;
949 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
951 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
952 GL_LockArrays(0, surf->mesh.num_vertices);
953 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
956 if (fogenabled && rendertype != SURFRENDER_ADD)
958 memset(&m, 0, sizeof(m));
959 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
961 m.pointer_color = varray_color4f;
962 m.tex[0] = R_GetTexture(texture->skin.fog);
963 m.pointer_vertex = surf->mesh.data_vertex3f;
965 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
967 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
968 GL_LockArrays(0, surf->mesh.num_vertices);
969 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
974 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
976 const msurface_t *surf;
978 int lightmaptexturenum;
979 memset(&m, 0, sizeof(m));
980 GL_BlendFunc(GL_ONE, GL_ZERO);
983 m.tex[0] = R_GetTexture(texture->skin.base);
984 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
985 m.texrgbscale[1] = 2;
986 m.tex[2] = R_GetTexture(texture->skin.detail);
987 m.texrgbscale[2] = 2;
988 if (texture->skin.glow)
990 m.tex[3] = R_GetTexture(texture->skin.glow);
991 m.texcombinergb[3] = GL_ADD;
993 if (r_shadow_realtime_world.integer)
994 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
996 GL_Color(1, 1, 1, 1);
998 while((surf = *surfchain++) != NULL)
1000 if (surf->visframe == r_framecount)
1002 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1003 m.tex[1] = lightmaptexturenum;
1004 m.pointer_vertex = surf->mesh.data_vertex3f;
1005 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1006 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1007 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1008 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
1010 GL_LockArrays(0, surf->mesh.num_vertices);
1011 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1012 GL_LockArrays(0, 0);
1017 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1019 const msurface_t *surf;
1021 int lightmaptexturenum;
1022 memset(&m, 0, sizeof(m));
1023 GL_BlendFunc(GL_ONE, GL_ZERO);
1026 m.tex[0] = R_GetTexture(texture->skin.base);
1027 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1028 m.texrgbscale[1] = 2;
1029 m.tex[2] = R_GetTexture(texture->skin.detail);
1030 m.texrgbscale[2] = 2;
1031 if (r_shadow_realtime_world.integer)
1032 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1034 GL_Color(1, 1, 1, 1);
1036 while((surf = *surfchain++) != NULL)
1038 if (surf->visframe == r_framecount)
1040 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1041 m.tex[1] = lightmaptexturenum;
1042 m.pointer_vertex = surf->mesh.data_vertex3f;
1043 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1044 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1045 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1047 GL_LockArrays(0, surf->mesh.num_vertices);
1048 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1049 GL_LockArrays(0, 0);
1054 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1056 const msurface_t *surf;
1058 int lightmaptexturenum;
1059 memset(&m, 0, sizeof(m));
1060 GL_BlendFunc(GL_ONE, GL_ZERO);
1063 m.tex[0] = R_GetTexture(texture->skin.base);
1064 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1065 m.texrgbscale[1] = 2;
1066 if (r_shadow_realtime_world.integer)
1067 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1069 GL_Color(1, 1, 1, 1);
1070 while((surf = *surfchain++) != NULL)
1072 if (surf->visframe == r_framecount)
1074 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1075 m.tex[1] = lightmaptexturenum;
1076 m.pointer_vertex = surf->mesh.data_vertex3f;
1077 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1078 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1080 GL_LockArrays(0, surf->mesh.num_vertices);
1081 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1082 GL_LockArrays(0, 0);
1087 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1089 const msurface_t *surf;
1091 memset(&m, 0, sizeof(m));
1094 GL_BlendFunc(GL_ONE, GL_ZERO);
1095 m.tex[0] = R_GetTexture(texture->skin.base);
1096 if (r_shadow_realtime_world.integer)
1097 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1099 GL_Color(1, 1, 1, 1);
1100 while((surf = *surfchain++) != NULL)
1102 if (surf->visframe == r_framecount)
1104 m.pointer_vertex = surf->mesh.data_vertex3f;
1105 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1107 GL_LockArrays(0, surf->mesh.num_vertices);
1108 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1109 GL_LockArrays(0, 0);
1114 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1116 const msurface_t *surf;
1118 int lightmaptexturenum;
1119 memset(&m, 0, sizeof(m));
1120 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1121 GL_DepthMask(false);
1123 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1124 GL_Color(1, 1, 1, 1);
1125 while((surf = *surfchain++) != NULL)
1127 if (surf->visframe == r_framecount)
1129 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1130 m.tex[0] = lightmaptexturenum;
1131 m.pointer_vertex = surf->mesh.data_vertex3f;
1132 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1134 GL_LockArrays(0, surf->mesh.num_vertices);
1135 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1136 GL_LockArrays(0, 0);
1141 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1143 const msurface_t *surf;
1146 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1147 memset(&m, 0, sizeof(m));
1148 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1149 GL_DepthMask(false);
1151 m.pointer_color = varray_color4f;
1152 while((surf = *surfchain++) != NULL)
1154 if (surf->visframe == r_framecount)
1156 m.pointer_vertex = surf->mesh.data_vertex3f;
1158 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1160 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1161 GL_LockArrays(0, surf->mesh.num_vertices);
1162 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1163 GL_LockArrays(0, 0);
1168 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1170 const msurface_t *surf;
1172 memset(&m, 0, sizeof(m));
1173 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1174 GL_DepthMask(false);
1176 m.tex[0] = R_GetTexture(texture->skin.detail);
1177 GL_Color(1, 1, 1, 1);
1178 while((surf = *surfchain++) != NULL)
1180 if (surf->visframe == r_framecount)
1182 m.pointer_vertex = surf->mesh.data_vertex3f;
1183 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1185 GL_LockArrays(0, surf->mesh.num_vertices);
1186 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1187 GL_LockArrays(0, 0);
1192 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1194 const msurface_t *surf;
1196 memset(&m, 0, sizeof(m));
1197 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1198 GL_DepthMask(false);
1200 m.tex[0] = R_GetTexture(texture->skin.glow);
1201 GL_Color(1, 1, 1, 1);
1202 while((surf = *surfchain++) != NULL)
1204 if (surf->visframe == r_framecount)
1206 m.pointer_vertex = surf->mesh.data_vertex3f;
1207 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1209 GL_LockArrays(0, surf->mesh.num_vertices);
1210 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1211 GL_LockArrays(0, 0);
1217 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1219 const msurface_t *surf;
1221 memset(&m, 0, sizeof(m));
1222 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1224 m.tex[0] = R_GetTexture(texture->skin.glow);
1226 GL_Color(1, 1, 1, 1);
1228 GL_Color(0, 0, 0, 1);
1229 while((surf = *surfchain++) != NULL)
1231 if (surf->visframe == r_framecount)
1233 m.pointer_vertex = surf->mesh.data_vertex3f;
1234 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1236 GL_LockArrays(0, surf->mesh.num_vertices);
1237 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1238 GL_LockArrays(0, 0);
1244 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1246 const msurface_t *surf;
1248 int lightmaptexturenum;
1249 memset(&m, 0, sizeof(m));
1250 GL_BlendFunc(GL_ONE, GL_ZERO);
1253 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1254 if (r_shadow_realtime_world.integer)
1255 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1257 GL_Color(1, 1, 1, 1);
1258 while((surf = *surfchain++) != NULL)
1260 if (surf->visframe == r_framecount)
1262 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1263 m.tex[0] = lightmaptexturenum;
1264 m.pointer_vertex = surf->mesh.data_vertex3f;
1265 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1267 GL_LockArrays(0, surf->mesh.num_vertices);
1268 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1269 GL_LockArrays(0, 0);
1274 static void R_DrawSurfaceChain(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1276 const msurface_t *surf;
1279 if (texture->flags & SURF_LIGHTMAP)
1281 if (gl_lightmaps.integer)
1283 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1285 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1287 else if (texture->rendertype != SURFRENDER_OPAQUE)
1289 // transparent vertex shaded from lightmap
1290 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1292 if (surf->visframe == r_framecount)
1294 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1295 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1299 else if (ent->effects & EF_FULLBRIGHT || r_fullbright.integer)
1301 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1302 if (r_detailtextures.integer)
1303 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1304 if (texture->skin.glow)
1305 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1307 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1309 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer)
1311 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1313 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1315 else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1317 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1318 if (texture->skin.glow)
1319 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1321 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1323 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1325 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1326 if (r_detailtextures.integer)
1327 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1328 if (texture->skin.glow)
1329 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1331 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1335 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1336 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1337 if (r_detailtextures.integer)
1338 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1339 if (texture->skin.glow)
1340 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1342 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1345 else if (texture->flags & SURF_DRAWTURB)
1347 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1349 if (surf->visframe == r_framecount)
1351 if (texture->rendertype == SURFRENDER_OPAQUE)
1352 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
1355 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1356 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
1361 else if (texture->flags & SURF_DRAWSKY)
1362 RSurfShader_Sky(ent, texture, surfchain);
1365 void R_UpdateTextureInfo(entity_render_t *ent)
1367 int i, texframe, alttextures;
1373 alttextures = ent->frame != 0;
1374 texframe = (int)(cl.time * 5.0f);
1375 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1377 t = ent->model->brushq1.textures + i;
1378 t->currentalpha = ent->alpha;
1379 if (t->flags & SURF_WATERALPHA)
1380 t->currentalpha *= r_wateralpha.value;
1381 if (ent->effects & EF_ADDITIVE)
1382 t->rendertype = SURFRENDER_ADD;
1383 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1384 t->rendertype = SURFRENDER_ALPHA;
1386 t->rendertype = SURFRENDER_OPAQUE;
1387 // we don't need to set currentframe if t->animated is false because
1388 // it was already set up by the texture loader for non-animating
1390 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1394 void R_PrepareSurfaces(entity_render_t *ent)
1396 int i, numsurfaces, *surfacevisframes;
1398 msurface_t *surf, *surfaces, **surfchain;
1405 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1406 numsurfaces = model->brushq1.nummodelsurfaces;
1407 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1408 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1410 R_UpdateTextureInfo(ent);
1412 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1415 if (model->brushq1.light_ambient != r_ambient.value)
1417 model->brushq1.light_ambient = r_ambient.value;
1418 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1419 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1423 for (i = 0;i < model->brushq1.light_styles;i++)
1425 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1427 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1428 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1429 (**surfchain).cached_dlight = true;
1434 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1436 if (surfacevisframes[i] == r_framecount)
1438 #if !WORLDNODECULLBACKFACES
1439 // mark any backface surfaces as not visible
1440 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1442 if (!(surf->flags & SURF_PLANEBACK))
1443 surfacevisframes[i] = -1;
1447 if ((surf->flags & SURF_PLANEBACK))
1448 surfacevisframes[i] = -1;
1450 if (surfacevisframes[i] == r_framecount)
1454 surf->visframe = r_framecount;
1455 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1456 R_BuildLightMap(ent, surf);
1462 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1466 model_t *model = ent->model;
1469 R_Mesh_Matrix(&ent->matrix);
1470 for (i = 0, t = model->brushq1.textures;i < model->brushq1.numtextures;i++, t++)
1472 f = t->flags & flagsmask;
1473 if (f && t->currentframe && model->brushq1.pvstexturechains[i] != NULL)
1474 R_DrawSurfaceChain(ent, t->currentframe, model->brushq1.pvstexturechains[i]);
1478 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1483 const entity_render_t *ent = calldata1;
1484 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1485 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1486 GL_DepthMask(false);
1488 R_Mesh_Matrix(&ent->matrix);
1490 memset(&m, 0, sizeof(m));
1491 m.pointer_vertex = varray_vertex3f;
1494 i = portal - ent->model->brushq1.portals;
1495 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1496 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1497 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1499 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1501 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1502 VectorCopy(portal->points[i].position, v);
1505 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1506 VectorCopy(portal->points[i].position, v);
1507 GL_LockArrays(0, portal->numpoints);
1508 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1509 GL_LockArrays(0, 0);
1512 // LordHavoc: this is just a nice debugging tool, very slow
1513 static void R_DrawPortals(entity_render_t *ent)
1516 mportal_t *portal, *endportal;
1517 float temp[3], center[3], f;
1518 if (ent->model == NULL)
1520 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1522 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1525 for (i = 0;i < portal->numpoints;i++)
1526 VectorAdd(temp, portal->points[i].position, temp);
1527 f = ixtable[portal->numpoints];
1528 VectorScale(temp, f, temp);
1529 Matrix4x4_Transform(&ent->matrix, temp, center);
1530 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1535 void R_PrepareBrushModel(entity_render_t *ent)
1537 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1540 #if WORLDNODECULLBACKFACES
1544 // because bmodels can be reused, we have to decide which things to render
1545 // from scratch every time
1549 #if WORLDNODECULLBACKFACES
1550 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1552 numsurfaces = model->brushq1.nummodelsurfaces;
1553 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1554 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1555 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1556 for (i = 0;i < numsurfaces;i++, surf++)
1558 #if WORLDNODECULLBACKFACES
1559 // mark any backface surfaces as not visible
1560 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1562 if ((surf->flags & SURF_PLANEBACK))
1563 surfacevisframes[i] = r_framecount;
1565 else if (!(surf->flags & SURF_PLANEBACK))
1566 surfacevisframes[i] = r_framecount;
1568 surfacevisframes[i] = r_framecount;
1570 surf->dlightframe = -1;
1572 R_PrepareSurfaces(ent);
1575 void R_SurfaceWorldNode (entity_render_t *ent)
1577 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1583 // equivilant to quake's RecursiveWorldNode but faster and more effective
1587 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1588 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1589 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1591 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1593 if (!R_CullBox (leaf->mins, leaf->maxs))
1596 leaf->visframe = r_framecount;
1600 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1602 surfnum = model->brushq1.pvssurflist[i];
1603 surf = model->brushq1.surfaces + surfnum;
1604 #if WORLDNODECULLBACKFACES
1605 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1607 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1608 surfacevisframes[surfnum] = r_framecount;
1612 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1613 surfacevisframes[surfnum] = r_framecount;
1616 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1617 surfacevisframes[surfnum] = r_framecount;
1622 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1624 int c, leafstackpos, *mark, *surfacevisframes;
1625 #if WORLDNODECULLBACKFACES
1629 mleaf_t *leaf, *leafstack[8192];
1632 msurface_t *surfaces;
1633 if (ent->model == NULL)
1635 // LordHavoc: portal-passage worldnode with PVS;
1636 // follows portals leading outward from viewleaf, does not venture
1637 // offscreen or into leafs that are not visible, faster than Quake's
1638 // RecursiveWorldNode
1639 surfaces = ent->model->brushq1.surfaces;
1640 surfacevisframes = ent->model->brushq1.surfacevisframes;
1641 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1642 viewleaf->worldnodeframe = r_framecount;
1643 leafstack[0] = viewleaf;
1645 while (leafstackpos)
1648 leaf = leafstack[--leafstackpos];
1649 leaf->visframe = r_framecount;
1650 // draw any surfaces bounding this leaf
1651 if (leaf->nummarksurfaces)
1653 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1655 #if WORLDNODECULLBACKFACES
1657 if (surfacevisframes[n] != r_framecount)
1659 surf = surfaces + n;
1660 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1662 if ((surf->flags & SURF_PLANEBACK))
1663 surfacevisframes[n] = r_framecount;
1667 if (!(surf->flags & SURF_PLANEBACK))
1668 surfacevisframes[n] = r_framecount;
1672 surfacevisframes[*mark++] = r_framecount;
1676 // follow portals into other leafs
1677 for (p = leaf->portals;p;p = p->next)
1679 // LordHavoc: this DotProduct hurts less than a cache miss
1680 // (which is more likely to happen if backflowing through leafs)
1681 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1684 if (leaf->worldnodeframe != r_framecount)
1686 leaf->worldnodeframe = r_framecount;
1687 // FIXME: R_CullBox is absolute, should be done relative
1688 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1689 leafstack[leafstackpos++] = leaf;
1696 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1698 int j, c, *surfacepvsframes, *mark;
1703 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1705 model->brushq1.pvsframecount++;
1706 model->brushq1.pvsviewleaf = viewleaf;
1707 model->brushq1.pvsviewleafnovis = r_novis.integer;
1708 model->brushq1.pvsleafchain = NULL;
1709 model->brushq1.pvssurflistlength = 0;
1712 surfacepvsframes = model->brushq1.surfacepvsframes;
1713 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1715 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1717 leaf->pvsframe = model->brushq1.pvsframecount;
1718 leaf->pvschain = model->brushq1.pvsleafchain;
1719 model->brushq1.pvsleafchain = leaf;
1720 // mark surfaces bounding this leaf as visible
1721 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1722 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1725 model->brushq1.BuildPVSTextureChains(model);
1730 void R_WorldVisibility(entity_render_t *ent)
1735 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1736 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1737 R_PVSUpdate(ent, viewleaf);
1742 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1743 R_SurfaceWorldNode (ent);
1745 R_PortalWorldNode (ent, viewleaf);
1748 void R_DrawWorld(entity_render_t *ent)
1750 if (ent->model == NULL)
1752 if (!ent->model->brushq1.num_leafs)
1754 if (ent->model->DrawSky)
1755 ent->model->DrawSky(ent);
1756 if (ent->model->Draw)
1757 ent->model->Draw(ent);
1761 R_PrepareSurfaces(ent);
1762 R_DrawSurfaces(ent, SURF_DRAWSKY);
1763 R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1764 if (r_drawportals.integer)
1769 void R_Model_Brush_DrawSky(entity_render_t *ent)
1771 if (ent->model == NULL)
1773 if (ent != &cl_entities[0].render)
1774 R_PrepareBrushModel(ent);
1775 R_DrawSurfaces(ent, SURF_DRAWSKY);
1778 void R_Model_Brush_Draw(entity_render_t *ent)
1780 if (ent->model == NULL)
1783 if (ent != &cl_entities[0].render)
1784 R_PrepareBrushModel(ent);
1785 R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1788 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)
1790 model_t *model = ent->model;
1791 vec3_t lightmins, lightmaxs;
1792 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1795 msurface_t *surface;
1798 lightmins[0] = relativelightorigin[0] - lightradius;
1799 lightmins[1] = relativelightorigin[1] - lightradius;
1800 lightmins[2] = relativelightorigin[2] - lightradius;
1801 lightmaxs[0] = relativelightorigin[0] + lightradius;
1802 lightmaxs[1] = relativelightorigin[1] + lightradius;
1803 lightmaxs[2] = relativelightorigin[2] + lightradius;
1804 *outnumclusterspointer = 0;
1805 *outnumsurfacespointer = 0;
1806 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1807 memset(outsurfacepvs, 0, (model->numsurfaces + 7) >> 3);
1810 VectorCopy(lightmins, outmins);
1811 VectorCopy(lightmaxs, outmaxs);
1814 VectorCopy(relativelightorigin, outmins);
1815 VectorCopy(relativelightorigin, outmaxs);
1816 if (model->brush.GetPVS)
1817 pvs = model->brush.GetPVS(model, relativelightorigin);
1820 // FIXME: use BSP recursion as lights are often small
1821 for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1823 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1825 outmins[0] = min(outmins[0], leaf->mins[0]);
1826 outmins[1] = min(outmins[1], leaf->mins[1]);
1827 outmins[2] = min(outmins[2], leaf->mins[2]);
1828 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1829 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1830 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1831 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1833 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1834 outclusterlist[outnumclusters++] = leaf->clusterindex;
1836 for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1838 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1839 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1841 surface = model->brushq1.surfaces + surfaceindex;
1842 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1844 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1846 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1847 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1848 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1849 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])))
1851 SETPVSBIT(outsurfacepvs, surfaceindex);
1852 outsurfacelist[outnumsurfaces++] = surfaceindex;
1862 // limit combined leaf box to light boundaries
1863 outmins[0] = max(outmins[0], lightmins[0]);
1864 outmins[1] = max(outmins[1], lightmins[1]);
1865 outmins[2] = max(outmins[2], lightmins[2]);
1866 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1867 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1868 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1870 *outnumclusterspointer = outnumclusters;
1871 *outnumsurfacespointer = outnumsurfaces;
1874 void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1876 model_t *model = ent->model;
1877 vec3_t lightmins, lightmaxs;
1878 msurface_t *surface;
1879 int surfacelistindex, j, t;
1882 if (r_drawcollisionbrushes.integer < 2)
1884 lightmins[0] = relativelightorigin[0] - lightradius;
1885 lightmins[1] = relativelightorigin[1] - lightradius;
1886 lightmins[2] = relativelightorigin[2] - lightradius;
1887 lightmaxs[0] = relativelightorigin[0] + lightradius;
1888 lightmaxs[1] = relativelightorigin[1] + lightradius;
1889 lightmaxs[2] = relativelightorigin[2] + lightradius;
1890 R_Mesh_Matrix(&ent->matrix);
1891 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1892 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1894 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1895 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1897 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1898 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1899 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1900 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])))
1901 shadowmarklist[numshadowmark++] = t;
1904 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);
1908 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)
1910 model_t *model = ent->model;
1911 vec3_t lightmins, lightmaxs, modelorg;
1912 msurface_t *surface;
1914 int surfacelistindex;
1915 if (r_drawcollisionbrushes.integer < 2)
1917 lightmins[0] = relativelightorigin[0] - lightradius;
1918 lightmins[1] = relativelightorigin[1] - lightradius;
1919 lightmins[2] = relativelightorigin[2] - lightradius;
1920 lightmaxs[0] = relativelightorigin[0] + lightradius;
1921 lightmaxs[1] = relativelightorigin[1] + lightradius;
1922 lightmaxs[2] = relativelightorigin[2] + lightradius;
1923 R_Mesh_Matrix(&ent->matrix);
1924 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1925 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1927 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1928 if (r_shadow_compilingrtlight)
1930 // if compiling an rtlight, capture the mesh
1931 t = surface->texinfo->texture;
1932 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1933 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);
1935 else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1937 t = surface->texinfo->texture->currentframe;
1938 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1939 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);
1945 void R_DrawCollisionBrush(colbrushf_t *brush)
1949 memset(&m, 0, sizeof(m));
1950 m.pointer_vertex = brush->points->v;
1952 i = ((int)brush) / sizeof(colbrushf_t);
1953 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1954 GL_LockArrays(0, brush->numpoints);
1955 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1956 GL_LockArrays(0, 0);
1959 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3mface_t *face)
1963 if (!face->num_collisiontriangles)
1965 memset(&m, 0, sizeof(m));
1966 m.pointer_vertex = face->data_collisionvertex3f;
1968 i = ((int)face) / sizeof(q3mface_t);
1969 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1970 GL_LockArrays(0, face->num_collisionvertices);
1971 R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1972 GL_LockArrays(0, 0);
1975 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1978 if (!face->num_triangles)
1983 skyrendernow = false;
1984 if (skyrendermasked)
1988 R_Mesh_Matrix(&ent->matrix);
1990 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1991 if (skyrendermasked)
1993 // depth-only (masking)
1994 GL_ColorMask(0,0,0,0);
1995 // just to make sure that braindead drivers don't draw anything
1996 // despite that colormask...
1997 GL_BlendFunc(GL_ZERO, GL_ONE);
2002 GL_BlendFunc(GL_ONE, GL_ZERO);
2007 memset(&m, 0, sizeof(m));
2008 m.pointer_vertex = face->data_vertex3f;
2011 GL_LockArrays(0, face->num_vertices);
2012 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2013 GL_LockArrays(0, 0);
2014 GL_ColorMask(1,1,1,1);
2017 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
2020 memset(&m, 0, sizeof(m));
2021 GL_BlendFunc(GL_ONE, GL_ZERO);
2024 if (face->texture->skin.glow)
2026 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2027 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2028 GL_Color(1, 1, 1, 1);
2031 GL_Color(0, 0, 0, 1);
2032 m.pointer_vertex = face->data_vertex3f;
2034 GL_LockArrays(0, face->num_vertices);
2035 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2036 GL_LockArrays(0, 0);
2039 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
2042 memset(&m, 0, sizeof(m));
2043 GL_BlendFunc(GL_ONE, GL_ZERO);
2046 m.tex[0] = R_GetTexture(face->texture->skin.base);
2047 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2048 m.tex[1] = R_GetTexture(face->lightmaptexture);
2049 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2050 m.texrgbscale[1] = 2;
2051 GL_Color(1, 1, 1, 1);
2052 m.pointer_vertex = face->data_vertex3f;
2054 GL_LockArrays(0, face->num_vertices);
2055 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2056 GL_LockArrays(0, 0);
2059 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
2062 memset(&m, 0, sizeof(m));
2063 GL_BlendFunc(GL_ONE, GL_ZERO);
2066 m.tex[0] = R_GetTexture(face->texture->skin.base);
2067 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2068 GL_Color(1, 1, 1, 1);
2069 m.pointer_vertex = face->data_vertex3f;
2071 GL_LockArrays(0, face->num_vertices);
2072 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2073 GL_LockArrays(0, 0);
2076 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
2079 memset(&m, 0, sizeof(m));
2080 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2081 GL_DepthMask(false);
2083 m.tex[0] = R_GetTexture(face->lightmaptexture);
2084 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2085 GL_Color(1, 1, 1, 1);
2086 m.pointer_vertex = face->data_vertex3f;
2088 GL_LockArrays(0, face->num_vertices);
2089 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2090 GL_LockArrays(0, 0);
2093 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3mface_t *face)
2096 memset(&m, 0, sizeof(m));
2097 GL_BlendFunc(GL_ONE, GL_ZERO);
2100 m.tex[0] = R_GetTexture(face->lightmaptexture);
2101 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2102 if (r_shadow_realtime_world.integer)
2103 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2105 GL_Color(1, 1, 1, 1);
2106 m.pointer_vertex = face->data_vertex3f;
2108 GL_LockArrays(0, face->num_vertices);
2109 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2110 GL_LockArrays(0, 0);
2113 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
2116 memset(&m, 0, sizeof(m));
2117 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2118 GL_DepthMask(false);
2120 if (face->texture->skin.glow)
2122 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2123 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2124 GL_Color(1, 1, 1, 1);
2127 GL_Color(0, 0, 0, 1);
2128 m.pointer_vertex = face->data_vertex3f;
2130 GL_LockArrays(0, face->num_vertices);
2131 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2132 GL_LockArrays(0, 0);
2135 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
2140 memset(&m, 0, sizeof(m));
2141 GL_BlendFunc(GL_ONE, GL_ZERO);
2144 m.tex[0] = R_GetTexture(face->texture->skin.base);
2145 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2147 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2148 mul *= r_shadow_realtime_world_lightmaps.value;
2149 if (mul == 2 && gl_combine.integer)
2151 m.texrgbscale[0] = 2;
2152 m.pointer_color = face->data_color4f;
2155 m.pointer_color = face->data_color4f;
2158 for (i = 0;i < face->num_vertices;i++)
2160 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2161 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2162 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2163 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2165 m.pointer_color = varray_color4f;
2167 m.pointer_vertex = face->data_vertex3f;
2169 GL_LockArrays(0, face->num_vertices);
2170 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2171 GL_LockArrays(0, 0);
2174 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3mface_t *face)
2179 memset(&m, 0, sizeof(m));
2180 GL_BlendFunc(GL_ONE, GL_ZERO);
2184 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2185 mul *= r_shadow_realtime_world_lightmaps.value;
2187 m.pointer_color = face->data_color4f;
2190 for (i = 0;i < face->num_vertices;i++)
2192 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2193 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2194 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2195 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2197 m.pointer_color = varray_color4f;
2199 m.pointer_vertex = face->data_vertex3f;
2201 GL_LockArrays(0, face->num_vertices);
2202 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2203 GL_LockArrays(0, 0);
2206 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
2209 memset(&m, 0, sizeof(m));
2210 GL_BlendFunc(GL_ONE, GL_ONE);
2213 m.tex[0] = R_GetTexture(face->texture->skin.base);
2214 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2215 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2216 m.pointer_vertex = face->data_vertex3f;
2218 GL_LockArrays(0, face->num_vertices);
2219 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2220 GL_LockArrays(0, 0);
2223 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2225 const entity_render_t *ent = voident;
2226 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
2228 R_Mesh_Matrix(&ent->matrix);
2229 memset(&m, 0, sizeof(m));
2230 if (ent->effects & EF_ADDITIVE)
2231 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2233 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2234 GL_DepthMask(false);
2236 m.tex[0] = R_GetTexture(face->texture->skin.base);
2237 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2238 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2239 if (gl_combine.integer)
2241 m.texrgbscale[0] = 2;
2242 if (r_textureunits.integer >= 2)
2244 m.tex[1] = R_GetTexture(face->lightmaptexture);
2245 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2246 GL_Color(1, 1, 1, ent->alpha);
2250 if (ent->alpha == 1)
2251 m.pointer_color = face->data_color4f;
2255 for (i = 0;i < face->num_vertices;i++)
2257 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2258 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2259 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2260 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2262 m.pointer_color = varray_color4f;
2269 for (i = 0;i < face->num_vertices;i++)
2271 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2272 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2273 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2274 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2276 m.pointer_color = varray_color4f;
2278 m.pointer_vertex = face->data_vertex3f;
2280 qglDisable(GL_CULL_FACE);
2281 GL_LockArrays(0, face->num_vertices);
2282 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2283 GL_LockArrays(0, 0);
2284 qglEnable(GL_CULL_FACE);
2287 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2289 if (!face->num_triangles)
2291 if (face->texture->surfaceparms)
2293 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2297 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2299 vec3_t facecenter, center;
2300 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2301 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2302 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2303 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2304 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2307 R_Mesh_Matrix(&ent->matrix);
2308 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2309 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2310 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2312 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2313 if (face->texture->skin.glow)
2314 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2316 else if (face->lightmaptexture)
2318 if (gl_lightmaps.integer)
2319 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2322 if (r_textureunits.integer >= 2 && gl_combine.integer)
2323 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2326 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2327 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2329 if (face->texture->skin.glow)
2330 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2335 if (gl_lightmaps.integer)
2336 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2339 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2340 if (face->texture->skin.glow)
2341 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2344 if (r_ambient.value)
2345 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2348 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2354 if (R_CullBox(node->mins, node->maxs))
2359 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2360 node = node->children[1];
2362 leaf = (q3mleaf_t *)node;
2363 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2366 for (i = 0;i < leaf->numleaffaces;i++)
2367 leaf->firstleafface[i]->visframe = r_framecount;
2371 // FIXME: num_leafs needs to be recalculated at load time to include only
2372 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2373 // the submodels (which would render the submodels occasionally, as part of
2374 // the world - not good)
2375 void R_Q3BSP_MarkLeafPVS(void)
2379 for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2381 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2384 for (i = 0;i < leaf->numleaffaces;i++)
2385 leaf->firstleafface[i]->visframe = r_framecount;
2390 static int r_q3bsp_framecount = -1;
2392 void R_Q3BSP_DrawSky(entity_render_t *ent)
2398 R_Mesh_Matrix(&ent->matrix);
2400 if (r_drawcollisionbrushes.integer < 2)
2402 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2403 if (ent == &cl_entities[0].render)
2405 if (r_q3bsp_framecount != r_framecount)
2407 r_q3bsp_framecount = r_framecount;
2408 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2409 //R_Q3BSP_MarkLeafPVS();
2411 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2412 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2413 R_Q3BSP_DrawSkyFace(ent, face);
2416 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2417 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2418 R_Q3BSP_DrawSkyFace(ent, face);
2422 void R_Q3BSP_Draw(entity_render_t *ent)
2429 R_Mesh_Matrix(&ent->matrix);
2431 if (r_drawcollisionbrushes.integer < 2)
2433 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2434 if (ent == &cl_entities[0].render)
2436 if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2437 if (r_q3bsp_framecount != r_framecount)
2439 r_q3bsp_framecount = r_framecount;
2440 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2441 //R_Q3BSP_MarkLeafPVS();
2443 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2444 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2445 R_Q3BSP_DrawFace(ent, face);
2448 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2449 R_Q3BSP_DrawFace(ent, face);
2451 if (r_drawcollisionbrushes.integer >= 1)
2453 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2454 GL_DepthMask(false);
2456 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2457 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2458 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2459 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2460 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2461 if (face->num_collisiontriangles)
2462 R_Q3BSP_DrawCollisionFace(ent, face);
2463 qglPolygonOffset(0, 0);
2467 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)
2469 model_t *model = ent->model;
2470 vec3_t lightmins, lightmaxs;
2471 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2477 lightmins[0] = relativelightorigin[0] - lightradius;
2478 lightmins[1] = relativelightorigin[1] - lightradius;
2479 lightmins[2] = relativelightorigin[2] - lightradius;
2480 lightmaxs[0] = relativelightorigin[0] + lightradius;
2481 lightmaxs[1] = relativelightorigin[1] + lightradius;
2482 lightmaxs[2] = relativelightorigin[2] + lightradius;
2483 *outnumclusterspointer = 0;
2484 *outnumsurfacespointer = 0;
2485 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2486 memset(outsurfacepvs, 0, (model->numsurfaces + 7) >> 3);
2489 VectorCopy(lightmins, outmins);
2490 VectorCopy(lightmaxs, outmaxs);
2493 VectorCopy(relativelightorigin, outmins);
2494 VectorCopy(relativelightorigin, outmaxs);
2495 if (model->brush.GetPVS)
2496 pvs = model->brush.GetPVS(model, relativelightorigin);
2499 // FIXME: use BSP recursion as lights are often small
2500 for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2502 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2504 outmins[0] = min(outmins[0], leaf->mins[0]);
2505 outmins[1] = min(outmins[1], leaf->mins[1]);
2506 outmins[2] = min(outmins[2], leaf->mins[2]);
2507 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2508 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2509 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2510 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2512 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2513 outclusterlist[outnumclusters++] = leaf->clusterindex;
2515 for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2517 surface = leaf->firstleafface[marksurfaceindex];
2518 surfaceindex = surface - model->brushq3.data_faces;
2519 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2521 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2523 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2525 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2526 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2527 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2528 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])))
2530 SETPVSBIT(outsurfacepvs, surfaceindex);
2531 outsurfacelist[outnumsurfaces++] = surfaceindex;
2541 // limit combined leaf box to light boundaries
2542 outmins[0] = max(outmins[0], lightmins[0]);
2543 outmins[1] = max(outmins[1], lightmins[1]);
2544 outmins[2] = max(outmins[2], lightmins[2]);
2545 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2546 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2547 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2549 *outnumclusterspointer = outnumclusters;
2550 *outnumsurfacespointer = outnumsurfaces;
2553 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2555 model_t *model = ent->model;
2556 vec3_t lightmins, lightmaxs;
2558 int surfacelistindex, j, t;
2561 if (r_drawcollisionbrushes.integer < 2)
2563 lightmins[0] = relativelightorigin[0] - lightradius;
2564 lightmins[1] = relativelightorigin[1] - lightradius;
2565 lightmins[2] = relativelightorigin[2] - lightradius;
2566 lightmaxs[0] = relativelightorigin[0] + lightradius;
2567 lightmaxs[1] = relativelightorigin[1] + lightradius;
2568 lightmaxs[2] = relativelightorigin[2] + lightradius;
2569 R_Mesh_Matrix(&ent->matrix);
2570 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2571 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2573 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2574 // FIXME: check some manner of face->rendermode here?
2575 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2577 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2579 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2580 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2581 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2582 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])))
2583 shadowmarklist[numshadowmark++] = t;
2587 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);
2591 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)
2593 model_t *model = ent->model;
2594 vec3_t lightmins, lightmaxs, modelorg;
2596 int surfacelistindex;
2597 if (r_drawcollisionbrushes.integer < 2)
2599 lightmins[0] = relativelightorigin[0] - lightradius;
2600 lightmins[1] = relativelightorigin[1] - lightradius;
2601 lightmins[2] = relativelightorigin[2] - lightradius;
2602 lightmaxs[0] = relativelightorigin[0] + lightradius;
2603 lightmaxs[1] = relativelightorigin[1] + lightradius;
2604 lightmaxs[2] = relativelightorigin[2] + lightradius;
2605 R_Mesh_Matrix(&ent->matrix);
2606 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2607 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2609 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2610 if (r_shadow_compilingrtlight)
2612 // if compiling an rtlight, capture the mesh
2613 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);
2615 else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2616 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);
2621 static void gl_surf_start(void)
2625 static void gl_surf_shutdown(void)
2629 static void gl_surf_newmap(void)
2633 void GL_Surf_Init(void)
2636 dlightdivtable[0] = 4194304;
2637 for (i = 1;i < 32768;i++)
2638 dlightdivtable[i] = 4194304 / (i << 7);
2640 Cvar_RegisterVariable(&r_ambient);
2641 Cvar_RegisterVariable(&r_drawportals);
2642 Cvar_RegisterVariable(&r_testvis);
2643 Cvar_RegisterVariable(&r_floatbuildlightmap);
2644 Cvar_RegisterVariable(&r_detailtextures);
2645 Cvar_RegisterVariable(&r_surfaceworldnode);
2646 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2647 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2648 Cvar_RegisterVariable(&gl_lightmaps);
2650 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);