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_ColorPointer(NULL);
749 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
752 // depth-only (masking)
753 GL_ColorMask(0,0,0,0);
754 // just to make sure that braindead drivers don't draw anything
755 // despite that colormask...
756 GL_BlendFunc(GL_ZERO, GL_ONE);
761 GL_BlendFunc(GL_ONE, GL_ZERO);
767 while((surf = *surfchain++) != NULL)
769 if (surf->visframe == r_framecount)
771 memset(&m, 0, sizeof(m));
772 m.pointer_vertex = surf->mesh.data_vertex3f;
774 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 matrix4x4_t tempmatrix;
789 float args[4] = {0.05f,0,0,0.04f};
791 if (gl_textureshader && r_watershader.value && !fogenabled)
793 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
794 R_Mesh_TextureMatrix(1, &tempmatrix);
795 Matrix4x4_CreateFromQuakeEntity(&tempmatrix, 0, 0, 0, 0, 0, 0, r_watershader.value);
796 R_Mesh_TextureMatrix(0, &tempmatrix);
798 else if (r_waterscroll.value)
800 // scrolling in texture matrix
801 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
802 R_Mesh_TextureMatrix(0, &tempmatrix);
805 R_Mesh_Matrix(&ent->matrix);
806 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
808 memset(&m, 0, sizeof(m));
809 texture = surf->texinfo->texture->currentframe;
810 alpha = texture->currentalpha;
811 if (texture->rendertype == SURFRENDER_ADD)
813 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
816 else if (texture->rendertype == SURFRENDER_ALPHA)
818 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
823 GL_BlendFunc(GL_ONE, GL_ZERO);
826 if (gl_textureshader && r_watershader.value && !fogenabled)
828 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
829 m.tex[1] = R_GetTexture(texture->skin.base);
832 m.tex[0] = R_GetTexture(texture->skin.base);
835 GL_ColorPointer(varray_color4f);
838 GL_ColorPointer(NULL);
839 GL_Color(1, 1, 1, alpha);
841 if (gl_textureshader && r_watershader.value && !fogenabled)
843 GL_ActiveTexture (0);
844 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
845 GL_ActiveTexture (1);
846 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
847 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
848 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
849 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
850 qglEnable (GL_TEXTURE_SHADER_NV);
853 m.pointer_vertex = surf->mesh.data_vertex3f;
854 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
855 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
856 m.texcombinergb[1] = GL_REPLACE;
860 R_FillColors(varray_color4f, surf->mesh.num_vertices, 1, 1, 1, alpha);
861 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, surf->mesh.num_vertices, modelorg);
863 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
865 if (gl_textureshader && r_watershader.value && !fogenabled)
867 qglDisable (GL_TEXTURE_SHADER_NV);
868 GL_ActiveTexture (0);
873 memset(&m, 0, sizeof(m));
874 m.pointer_vertex = surf->mesh.data_vertex3f;
875 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
878 m.tex[0] = R_GetTexture(texture->skin.fog);
879 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
880 GL_ColorPointer(varray_color4f);
882 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, surf->mesh.num_vertices, modelorg);
883 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
886 if ((gl_textureshader && r_watershader.value && !fogenabled) || r_waterscroll.value)
888 Matrix4x4_CreateIdentity(&tempmatrix);
889 R_Mesh_TextureMatrix(0, &tempmatrix);
890 R_Mesh_TextureMatrix(1, &tempmatrix);
894 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
896 const msurface_t *surf;
899 if (texture->rendertype != SURFRENDER_OPAQUE)
901 for (chain = surfchain;(surf = *chain) != NULL;chain++)
903 if (surf->visframe == r_framecount)
905 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
906 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
911 for (chain = surfchain;(surf = *chain) != NULL;chain++)
912 if (surf->visframe == r_framecount)
913 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
916 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
918 float base, colorscale;
921 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
922 memset(&m, 0, sizeof(m));
923 if (rendertype == SURFRENDER_ADD)
925 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
928 else if (rendertype == SURFRENDER_ALPHA)
930 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
935 GL_BlendFunc(GL_ONE, GL_ZERO);
938 m.tex[0] = R_GetTexture(texture->skin.base);
940 if (gl_combine.integer)
942 m.texrgbscale[0] = 4;
945 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
947 GL_ColorPointer(varray_color4f);
949 m.pointer_vertex = surf->mesh.data_vertex3f;
950 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
952 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
953 if (!(ent->effects & EF_FULLBRIGHT))
955 if (surf->dlightframe == r_framecount)
956 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
957 if (surf->flags & SURF_LIGHTMAP)
958 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);
960 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
961 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
964 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
968 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
969 memset(&m, 0, sizeof(m));
970 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
973 m.tex[0] = R_GetTexture(texture->skin.glow);
974 GL_ColorPointer(varray_color4f);
976 m.pointer_vertex = surf->mesh.data_vertex3f;
978 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
980 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
981 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
984 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
988 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
989 memset(&m, 0, sizeof(m));
990 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
993 m.tex[0] = R_GetTexture(texture->skin.fog);
994 GL_ColorPointer(varray_color4f);
996 m.pointer_vertex = surf->mesh.data_vertex3f;
998 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1000 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
1001 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1004 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1006 const msurface_t *surf;
1008 int lightmaptexturenum;
1009 memset(&m, 0, sizeof(m));
1010 GL_BlendFunc(GL_ONE, GL_ZERO);
1013 m.tex[0] = R_GetTexture(texture->skin.base);
1014 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1015 m.texrgbscale[1] = 2;
1016 m.tex[2] = R_GetTexture(texture->skin.detail);
1017 m.texrgbscale[2] = 2;
1018 if (texture->skin.glow)
1020 m.tex[3] = R_GetTexture(texture->skin.glow);
1021 m.texcombinergb[3] = GL_ADD;
1023 GL_ColorPointer(NULL);
1024 if (r_shadow_realtime_world.integer)
1025 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1027 GL_Color(1, 1, 1, 1);
1029 while((surf = *surfchain++) != NULL)
1031 if (surf->visframe == r_framecount)
1033 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1034 m.tex[1] = lightmaptexturenum;
1035 m.pointer_vertex = surf->mesh.data_vertex3f;
1036 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1037 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1038 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1039 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
1041 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1046 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1048 const msurface_t *surf;
1050 int lightmaptexturenum;
1051 memset(&m, 0, sizeof(m));
1052 GL_BlendFunc(GL_ONE, GL_ZERO);
1055 m.tex[0] = R_GetTexture(texture->skin.base);
1056 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1057 m.texrgbscale[1] = 2;
1058 m.tex[2] = R_GetTexture(texture->skin.detail);
1059 m.texrgbscale[2] = 2;
1060 GL_ColorPointer(NULL);
1061 if (r_shadow_realtime_world.integer)
1062 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1064 GL_Color(1, 1, 1, 1);
1066 while((surf = *surfchain++) != NULL)
1068 if (surf->visframe == r_framecount)
1070 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1071 m.tex[1] = lightmaptexturenum;
1072 m.pointer_vertex = surf->mesh.data_vertex3f;
1073 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1074 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1075 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1077 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1082 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1084 const msurface_t *surf;
1086 int lightmaptexturenum;
1087 memset(&m, 0, sizeof(m));
1088 GL_BlendFunc(GL_ONE, GL_ZERO);
1091 m.tex[0] = R_GetTexture(texture->skin.base);
1092 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1093 m.texrgbscale[1] = 2;
1094 GL_ColorPointer(NULL);
1095 if (r_shadow_realtime_world.integer)
1096 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1098 GL_Color(1, 1, 1, 1);
1099 while((surf = *surfchain++) != NULL)
1101 if (surf->visframe == r_framecount)
1103 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1104 m.tex[1] = lightmaptexturenum;
1105 m.pointer_vertex = surf->mesh.data_vertex3f;
1106 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1107 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1109 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1114 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1116 const msurface_t *surf;
1118 memset(&m, 0, sizeof(m));
1121 GL_BlendFunc(GL_ONE, GL_ZERO);
1122 m.tex[0] = R_GetTexture(texture->skin.base);
1123 GL_ColorPointer(NULL);
1124 if (r_shadow_realtime_world.integer)
1125 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1127 GL_Color(1, 1, 1, 1);
1128 while((surf = *surfchain++) != NULL)
1130 if (surf->visframe == r_framecount)
1132 m.pointer_vertex = surf->mesh.data_vertex3f;
1133 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1135 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1140 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1142 const msurface_t *surf;
1144 int lightmaptexturenum;
1145 memset(&m, 0, sizeof(m));
1146 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1147 GL_DepthMask(false);
1149 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1150 GL_ColorPointer(NULL);
1151 GL_Color(1, 1, 1, 1);
1152 while((surf = *surfchain++) != NULL)
1154 if (surf->visframe == r_framecount)
1156 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1157 m.tex[0] = lightmaptexturenum;
1158 m.pointer_vertex = surf->mesh.data_vertex3f;
1159 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1161 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1166 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1168 const msurface_t *surf;
1171 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1172 memset(&m, 0, sizeof(m));
1173 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1174 GL_DepthMask(false);
1176 GL_ColorPointer(varray_color4f);
1177 while((surf = *surfchain++) != NULL)
1179 if (surf->visframe == r_framecount)
1181 m.pointer_vertex = surf->mesh.data_vertex3f;
1183 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1185 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1186 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1191 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1193 const msurface_t *surf;
1195 memset(&m, 0, sizeof(m));
1196 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1197 GL_DepthMask(false);
1199 m.tex[0] = R_GetTexture(texture->skin.detail);
1200 GL_ColorPointer(NULL);
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_texcoorddetail2f;
1209 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1214 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1216 const msurface_t *surf;
1218 memset(&m, 0, sizeof(m));
1219 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1220 GL_DepthMask(false);
1222 m.tex[0] = R_GetTexture(texture->skin.glow);
1223 GL_ColorPointer(NULL);
1224 GL_Color(1, 1, 1, 1);
1225 while((surf = *surfchain++) != NULL)
1227 if (surf->visframe == r_framecount)
1229 m.pointer_vertex = surf->mesh.data_vertex3f;
1230 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1232 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1238 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1240 const msurface_t *surf;
1242 memset(&m, 0, sizeof(m));
1243 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1245 m.tex[0] = R_GetTexture(texture->skin.glow);
1246 GL_ColorPointer(NULL);
1248 GL_Color(1, 1, 1, 1);
1250 GL_Color(0, 0, 0, 1);
1251 while((surf = *surfchain++) != NULL)
1253 if (surf->visframe == r_framecount)
1255 m.pointer_vertex = surf->mesh.data_vertex3f;
1256 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1258 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1264 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1266 const msurface_t *surf;
1268 int lightmaptexturenum;
1269 memset(&m, 0, sizeof(m));
1270 GL_BlendFunc(GL_ONE, GL_ZERO);
1273 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1274 GL_ColorPointer(NULL);
1275 if (r_shadow_realtime_world.integer)
1276 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1278 GL_Color(1, 1, 1, 1);
1279 while((surf = *surfchain++) != NULL)
1281 if (surf->visframe == r_framecount)
1283 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1284 m.tex[0] = lightmaptexturenum;
1285 m.pointer_vertex = surf->mesh.data_vertex3f;
1286 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1288 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1293 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1295 const entity_render_t *ent = calldata1;
1296 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1300 R_Mesh_Matrix(&ent->matrix);
1302 texture = surf->texinfo->texture;
1303 if (texture->animated)
1304 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];
1306 currentalpha = ent->alpha;
1307 if (ent->effects & EF_ADDITIVE)
1308 rendertype = SURFRENDER_ADD;
1309 else if (currentalpha < 1 || texture->skin.fog != NULL)
1310 rendertype = SURFRENDER_ALPHA;
1312 rendertype = SURFRENDER_OPAQUE;
1314 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1315 if (texture->skin.glow)
1316 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1318 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1321 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1323 const msurface_t *surf;
1326 if (texture->rendertype != SURFRENDER_OPAQUE)
1328 // transparent vertex shaded from lightmap
1329 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1331 if (surf->visframe == r_framecount)
1333 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1334 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1338 else if (ent->effects & EF_FULLBRIGHT)
1340 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1341 if (r_detailtextures.integer)
1342 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1343 if (texture->skin.glow)
1344 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1346 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1349 // opaque base lighting
1350 else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
1352 if (r_ambient.value > 0)
1356 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1358 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1361 // opaque lightmapped
1362 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1364 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1366 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1368 else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1370 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1371 if (texture->skin.glow)
1372 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1374 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1376 else if (r_textureunits.integer >= 2 && gl_combine.integer && !gl_lightmaps.integer)
1378 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1379 if (r_detailtextures.integer)
1380 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1381 if (texture->skin.glow)
1382 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1384 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1386 else if (!gl_lightmaps.integer)
1388 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1389 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1390 if (r_detailtextures.integer)
1391 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1392 if (texture->skin.glow)
1393 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1395 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1399 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1401 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1405 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1406 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1407 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1409 int Cshader_count = 3;
1410 Cshader_t *Cshaders[3] =
1412 &Cshader_wall_lightmap,
1417 void R_UpdateTextureInfo(entity_render_t *ent)
1419 int i, texframe, alttextures;
1425 alttextures = ent->frame != 0;
1426 texframe = (int)(cl.time * 5.0f);
1427 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1429 t = ent->model->brushq1.textures + i;
1430 t->currentalpha = ent->alpha;
1431 if (t->flags & SURF_WATERALPHA)
1432 t->currentalpha *= r_wateralpha.value;
1433 if (ent->effects & EF_ADDITIVE)
1434 t->rendertype = SURFRENDER_ADD;
1435 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1436 t->rendertype = SURFRENDER_ALPHA;
1438 t->rendertype = SURFRENDER_OPAQUE;
1439 // we don't need to set currentframe if t->animated is false because
1440 // it was already set up by the texture loader for non-animating
1442 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1446 void R_PrepareSurfaces(entity_render_t *ent)
1448 int i, numsurfaces, *surfacevisframes;
1450 msurface_t *surf, *surfaces, **surfchain;
1457 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1458 numsurfaces = model->brushq1.nummodelsurfaces;
1459 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1460 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1462 R_UpdateTextureInfo(ent);
1464 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1467 if (model->brushq1.light_ambient != r_ambient.value)
1469 model->brushq1.light_ambient = r_ambient.value;
1470 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1471 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1475 for (i = 0;i < model->brushq1.light_styles;i++)
1477 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1479 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1480 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1481 (**surfchain).cached_dlight = true;
1486 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1488 if (surfacevisframes[i] == r_framecount)
1490 #if !WORLDNODECULLBACKFACES
1491 // mark any backface surfaces as not visible
1492 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1494 if (!(surf->flags & SURF_PLANEBACK))
1495 surfacevisframes[i] = -1;
1499 if ((surf->flags & SURF_PLANEBACK))
1500 surfacevisframes[i] = -1;
1502 if (surfacevisframes[i] == r_framecount)
1506 surf->visframe = r_framecount;
1507 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1508 R_BuildLightMap(ent, surf);
1514 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1518 if (ent->model == NULL)
1520 R_Mesh_Matrix(&ent->matrix);
1521 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1522 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1523 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1526 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1531 const entity_render_t *ent = calldata1;
1532 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1533 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1534 GL_DepthMask(false);
1536 R_Mesh_Matrix(&ent->matrix);
1538 memset(&m, 0, sizeof(m));
1539 m.pointer_vertex = varray_vertex3f;
1542 i = portal - ent->model->brushq1.portals;
1543 GL_ColorPointer(NULL);
1544 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1545 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1546 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1548 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1550 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1551 VectorCopy(portal->points[i].position, v);
1554 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1555 VectorCopy(portal->points[i].position, v);
1556 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1559 // LordHavoc: this is just a nice debugging tool, very slow
1560 static void R_DrawPortals(entity_render_t *ent)
1563 mportal_t *portal, *endportal;
1564 float temp[3], center[3], f;
1565 if (ent->model == NULL)
1567 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1569 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1572 for (i = 0;i < portal->numpoints;i++)
1573 VectorAdd(temp, portal->points[i].position, temp);
1574 f = ixtable[portal->numpoints];
1575 VectorScale(temp, f, temp);
1576 Matrix4x4_Transform(&ent->matrix, temp, center);
1577 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1582 void R_PrepareBrushModel(entity_render_t *ent)
1584 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1587 #if WORLDNODECULLBACKFACES
1591 // because bmodels can be reused, we have to decide which things to render
1592 // from scratch every time
1596 #if WORLDNODECULLBACKFACES
1597 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1599 numsurfaces = model->brushq1.nummodelsurfaces;
1600 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1601 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1602 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1603 for (i = 0;i < numsurfaces;i++, surf++)
1605 #if WORLDNODECULLBACKFACES
1606 // mark any backface surfaces as not visible
1607 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1609 if ((surf->flags & SURF_PLANEBACK))
1610 surfacevisframes[i] = r_framecount;
1612 else if (!(surf->flags & SURF_PLANEBACK))
1613 surfacevisframes[i] = r_framecount;
1615 surfacevisframes[i] = r_framecount;
1617 surf->dlightframe = -1;
1619 R_PrepareSurfaces(ent);
1622 void R_SurfaceWorldNode (entity_render_t *ent)
1624 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1630 // equivilant to quake's RecursiveWorldNode but faster and more effective
1634 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1635 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1636 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1638 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1640 if (!R_CullBox (leaf->mins, leaf->maxs))
1643 leaf->visframe = r_framecount;
1647 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1649 surfnum = model->brushq1.pvssurflist[i];
1650 surf = model->brushq1.surfaces + surfnum;
1651 #if WORLDNODECULLBACKFACES
1652 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1654 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1655 surfacevisframes[surfnum] = r_framecount;
1659 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1660 surfacevisframes[surfnum] = r_framecount;
1663 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1664 surfacevisframes[surfnum] = r_framecount;
1669 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1671 int c, leafstackpos, *mark, *surfacevisframes;
1672 #if WORLDNODECULLBACKFACES
1676 mleaf_t *leaf, *leafstack[8192];
1679 msurface_t *surfaces;
1680 if (ent->model == NULL)
1682 // LordHavoc: portal-passage worldnode with PVS;
1683 // follows portals leading outward from viewleaf, does not venture
1684 // offscreen or into leafs that are not visible, faster than Quake's
1685 // RecursiveWorldNode
1686 surfaces = ent->model->brushq1.surfaces;
1687 surfacevisframes = ent->model->brushq1.surfacevisframes;
1688 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1689 viewleaf->worldnodeframe = r_framecount;
1690 leafstack[0] = viewleaf;
1692 while (leafstackpos)
1695 leaf = leafstack[--leafstackpos];
1696 leaf->visframe = r_framecount;
1697 // draw any surfaces bounding this leaf
1698 if (leaf->nummarksurfaces)
1700 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1702 #if WORLDNODECULLBACKFACES
1704 if (surfacevisframes[n] != r_framecount)
1706 surf = surfaces + n;
1707 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1709 if ((surf->flags & SURF_PLANEBACK))
1710 surfacevisframes[n] = r_framecount;
1714 if (!(surf->flags & SURF_PLANEBACK))
1715 surfacevisframes[n] = r_framecount;
1719 surfacevisframes[*mark++] = r_framecount;
1723 // follow portals into other leafs
1724 for (p = leaf->portals;p;p = p->next)
1726 // LordHavoc: this DotProduct hurts less than a cache miss
1727 // (which is more likely to happen if backflowing through leafs)
1728 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1731 if (leaf->worldnodeframe != r_framecount)
1733 leaf->worldnodeframe = r_framecount;
1734 // FIXME: R_CullBox is absolute, should be done relative
1735 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1736 leafstack[leafstackpos++] = leaf;
1743 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1745 int j, c, *surfacepvsframes, *mark;
1750 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1752 model->brushq1.pvsframecount++;
1753 model->brushq1.pvsviewleaf = viewleaf;
1754 model->brushq1.pvsviewleafnovis = r_novis.integer;
1755 model->brushq1.pvsleafchain = NULL;
1756 model->brushq1.pvssurflistlength = 0;
1759 surfacepvsframes = model->brushq1.surfacepvsframes;
1760 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1762 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1764 leaf->pvsframe = model->brushq1.pvsframecount;
1765 leaf->pvschain = model->brushq1.pvsleafchain;
1766 model->brushq1.pvsleafchain = leaf;
1767 // mark surfaces bounding this leaf as visible
1768 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1769 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1772 model->brushq1.BuildPVSTextureChains(model);
1777 void R_WorldVisibility(entity_render_t *ent)
1782 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1783 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1784 R_PVSUpdate(ent, viewleaf);
1789 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1790 R_SurfaceWorldNode (ent);
1792 R_PortalWorldNode (ent, viewleaf);
1795 void R_DrawWorld(entity_render_t *ent)
1797 if (ent->model == NULL)
1799 if (!ent->model->brushq1.num_leafs)
1801 if (ent->model->DrawSky)
1802 ent->model->DrawSky(ent);
1803 if (ent->model->Draw)
1804 ent->model->Draw(ent);
1808 R_PrepareSurfaces(ent);
1809 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1810 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1811 if (r_drawportals.integer)
1816 void R_Model_Brush_DrawSky(entity_render_t *ent)
1818 if (ent->model == NULL)
1820 if (ent != &cl_entities[0].render)
1821 R_PrepareBrushModel(ent);
1822 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1825 void R_Model_Brush_Draw(entity_render_t *ent)
1827 if (ent->model == NULL)
1830 if (ent != &cl_entities[0].render)
1831 R_PrepareBrushModel(ent);
1832 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1835 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1840 float projectdistance, f, temp[3], lightradius2;
1841 if (ent->model == NULL)
1843 R_Mesh_Matrix(&ent->matrix);
1844 lightradius2 = lightradius * lightradius;
1845 R_UpdateTextureInfo(ent);
1846 projectdistance = lightradius + ent->model->radius;//projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1847 //projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1848 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1850 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1852 f = PlaneDiff(relativelightorigin, surf->plane);
1853 if (surf->flags & SURF_PLANEBACK)
1855 // draw shadows only for frontfaces and only if they are close
1856 if (f >= 0.1 && f < lightradius)
1858 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1859 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1860 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1861 if (DotProduct(temp, temp) < lightradius2)
1862 R_Shadow_VolumeFromSphere(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_vertex3f, surf->mesh.data_element3i, surf->mesh.data_neighbor3i, relativelightorigin, projectdistance, lightradius);
1867 int t, leafnum, marksurfnum, trianglenum;
1872 float projectdistance;
1874 vec3_t lightmins, lightmaxs;
1875 if (ent->model == NULL)
1877 R_Mesh_Matrix(&ent->matrix);
1878 R_UpdateTextureInfo(ent);
1879 projectdistance = lightradius + ent->model->radius;//projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1880 lightmins[0] = relativelightorigin[0] - lightradius;
1881 lightmins[1] = relativelightorigin[1] - lightradius;
1882 lightmins[2] = relativelightorigin[2] - lightradius;
1883 lightmaxs[0] = relativelightorigin[0] + lightradius;
1884 lightmaxs[1] = relativelightorigin[1] + lightradius;
1885 lightmaxs[2] = relativelightorigin[2] + lightradius;
1887 R_Shadow_PrepareShadowMark(ent->model->brush.shadowmesh->numtriangles);
1888 maxmarksurfaces = sizeof(surfacelist) / sizeof(surfacelist[0]);
1889 ent->model->brushq1.GetVisible(ent->model, relativelightorigin, lightmins, lightmaxs, 0, NULL, NULL, maxmarkleafs, markleaf, &nummarkleafs);
1890 for (marksurfacenum = 0;marksurfacenum < nummarksurfaces;marksurfacenum++)
1892 surf = marksurface[marksurfacenum];
1893 if (surf->shadowmark != shadowmarkcount)
1895 surf->shadowmark = shadowmarkcount;
1896 if (BoxesOverlap(lightmins, lightmaxs, surf->poly_mins, surf->poly_maxs) && surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && (surf->flags & SURF_SHADOWCAST))
1898 for (trianglenum = 0, t = surf->num_firstshadowmeshtriangle, e = ent->model->brush.shadowmesh->element3i + t * 3;trianglenum < surf->mesh.num_triangles;trianglenum++, t++, e += 3)
1900 v[0] = ent->model->brush.shadowmesh->vertex3f + e[0] * 3;
1901 v[1] = ent->model->brush.shadowmesh->vertex3f + e[1] * 3;
1902 v[2] = ent->model->brush.shadowmesh->vertex3f + e[2] * 3;
1903 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])))
1904 shadowmarklist[numshadowmark++] = t;
1910 R_Shadow_PrepareShadowMark(ent->model->brush.shadowmesh->numtriangles);
1911 if (ent->model->brush.GetPVS && (pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin)))
1913 pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin);
1914 // FIXME: use BSP recursion in q1bsp as dlights are often small
1915 for (leafnum = 0, leaf = ent->model->brushq1.data_leafs;leafnum < ent->model->brushq1.num_leafs;leafnum++, leaf++)
1917 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && CHECKPVSBIT(pvs, leaf->clusterindex))
1919 for (marksurfnum = 0;marksurfnum < leaf->nummarksurfaces;marksurfnum++)
1921 surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[marksurfnum];
1922 if (surf->shadowmark != shadowmarkcount)
1924 surf->shadowmark = shadowmarkcount;
1925 if (BoxesOverlap(lightmins, lightmaxs, surf->poly_mins, surf->poly_maxs) && surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && (surf->flags & SURF_SHADOWCAST))
1927 for (trianglenum = 0, t = surf->num_firstshadowmeshtriangle, e = ent->model->brush.shadowmesh->element3i + t * 3;trianglenum < surf->mesh.num_triangles;trianglenum++, t++, e += 3)
1929 v[0] = ent->model->brush.shadowmesh->vertex3f + e[0] * 3;
1930 v[1] = ent->model->brush.shadowmesh->vertex3f + e[1] * 3;
1931 v[2] = ent->model->brush.shadowmesh->vertex3f + e[2] * 3;
1932 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])))
1933 shadowmarklist[numshadowmark++] = t;
1943 for (marksurfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;marksurfnum < ent->model->brushq1.nummodelsurfaces;marksurfnum++, surf++)
1945 if (BoxesOverlap(lightmins, lightmaxs, surf->poly_mins, surf->poly_maxs) && surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && (surf->flags & SURF_SHADOWCAST))
1947 for (trianglenum = 0, t = surf->num_firstshadowmeshtriangle, e = ent->model->brush.shadowmesh->element3i + t * 3;trianglenum < surf->mesh.num_triangles;trianglenum++, t++, e += 3)
1949 v[0] = ent->model->brush.shadowmesh->vertex3f + e[0] * 3;
1950 v[1] = ent->model->brush.shadowmesh->vertex3f + e[1] * 3;
1951 v[2] = ent->model->brush.shadowmesh->vertex3f + e[2] * 3;
1952 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])))
1953 shadowmarklist[numshadowmark++] = t;
1958 R_Shadow_VolumeFromList(ent->model->brush.shadowmesh->numverts, ent->model->brush.shadowmesh->numtriangles, ent->model->brush.shadowmesh->vertex3f, ent->model->brush.shadowmesh->element3i, ent->model->brush.shadowmesh->neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
1962 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)
1964 int leafnum, marksurfnum;
1969 float lightmins[3], lightmaxs[3];
1970 if (ent->model == NULL)
1972 R_Mesh_Matrix(&ent->matrix);
1973 lightmins[0] = relativelightorigin[0] - lightradius;
1974 lightmins[1] = relativelightorigin[1] - lightradius;
1975 lightmins[2] = relativelightorigin[2] - lightradius;
1976 lightmaxs[0] = relativelightorigin[0] + lightradius;
1977 lightmaxs[1] = relativelightorigin[1] + lightradius;
1978 lightmaxs[2] = relativelightorigin[2] + lightradius;
1979 R_UpdateTextureInfo(ent);
1981 if (ent->model->brush.GetPVS && (pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin)))
1983 pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin);
1984 for (leafnum = 0, leaf = ent->model->brushq1.data_leafs;leafnum < ent->model->brushq1.num_leafs;leafnum++, leaf++)
1986 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && CHECKPVSBIT(pvs, leaf->clusterindex))
1988 for (marksurfnum = 0;marksurfnum < leaf->nummarksurfaces;marksurfnum++)
1990 surf = ent->model->brushq1.surfaces + leaf->firstmarksurface[marksurfnum];
1991 if (surf->shadowmark != shadowmarkcount)
1993 surf->shadowmark = shadowmarkcount;
1994 if (BoxesOverlap(lightmins, lightmaxs, surf->poly_mins, surf->poly_maxs) && (ent != &cl_entities[0].render || surf->visframe == r_framecount))
1996 t = surf->texinfo->texture->currentframe;
1997 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1999 R_Shadow_DiffuseLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, lightcubemap);
2000 R_Shadow_SpecularLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, lightcubemap);
2010 for (marksurfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;marksurfnum < ent->model->brushq1.nummodelsurfaces;marksurfnum++, surf++)
2012 if (BoxesOverlap(lightmins, lightmaxs, surf->poly_mins, surf->poly_maxs) && (ent != &cl_entities[0].render || surf->visframe == r_framecount))
2014 t = surf->texinfo->texture->currentframe;
2015 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
2017 R_Shadow_DiffuseLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, lightcubemap);
2018 R_Shadow_SpecularLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, lightcubemap);
2025 void R_DrawCollisionBrush(colbrushf_t *brush)
2029 memset(&m, 0, sizeof(m));
2030 m.pointer_vertex = brush->points->v;
2032 i = ((int)brush) / sizeof(colbrushf_t);
2033 GL_ColorPointer(NULL);
2034 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
2035 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
2038 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3mface_t *face)
2042 if (!face->num_collisiontriangles)
2044 memset(&m, 0, sizeof(m));
2045 m.pointer_vertex = face->data_collisionvertex3f;
2047 i = ((int)face) / sizeof(q3mface_t);
2048 GL_ColorPointer(NULL);
2049 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
2050 R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
2053 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
2056 if (!face->num_triangles)
2061 skyrendernow = false;
2062 if (skyrendermasked)
2066 R_Mesh_Matrix(&ent->matrix);
2068 GL_ColorPointer(NULL);
2069 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
2070 if (skyrendermasked)
2072 // depth-only (masking)
2073 GL_ColorMask(0,0,0,0);
2074 // just to make sure that braindead drivers don't draw anything
2075 // despite that colormask...
2076 GL_BlendFunc(GL_ZERO, GL_ONE);
2081 GL_BlendFunc(GL_ONE, GL_ZERO);
2086 memset(&m, 0, sizeof(m));
2087 m.pointer_vertex = face->data_vertex3f;
2090 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2091 GL_ColorMask(1,1,1,1);
2094 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
2097 memset(&m, 0, sizeof(m));
2098 GL_BlendFunc(GL_ONE, GL_ZERO);
2101 GL_ColorPointer(NULL);
2102 if (face->texture->skin.glow)
2104 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2105 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2106 GL_Color(1, 1, 1, 1);
2109 GL_Color(0, 0, 0, 1);
2110 m.pointer_vertex = face->data_vertex3f;
2112 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2115 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
2118 memset(&m, 0, sizeof(m));
2119 GL_BlendFunc(GL_ONE, GL_ZERO);
2122 m.tex[0] = R_GetTexture(face->texture->skin.base);
2123 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2124 m.tex[1] = R_GetTexture(face->lightmaptexture);
2125 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2126 m.texrgbscale[1] = 2;
2127 GL_ColorPointer(NULL);
2128 GL_Color(1, 1, 1, 1);
2129 m.pointer_vertex = face->data_vertex3f;
2131 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2134 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
2137 memset(&m, 0, sizeof(m));
2138 GL_BlendFunc(GL_ONE, GL_ZERO);
2141 m.tex[0] = R_GetTexture(face->texture->skin.base);
2142 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2143 GL_ColorPointer(NULL);
2144 GL_Color(1, 1, 1, 1);
2145 m.pointer_vertex = face->data_vertex3f;
2147 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2150 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
2153 memset(&m, 0, sizeof(m));
2154 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2155 GL_DepthMask(false);
2157 m.tex[0] = R_GetTexture(face->lightmaptexture);
2158 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2159 GL_ColorPointer(NULL);
2160 GL_Color(1, 1, 1, 1);
2161 m.pointer_vertex = face->data_vertex3f;
2163 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2166 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3mface_t *face)
2169 memset(&m, 0, sizeof(m));
2170 GL_BlendFunc(GL_ONE, GL_ZERO);
2173 m.tex[0] = R_GetTexture(face->lightmaptexture);
2174 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2175 GL_ColorPointer(NULL);
2176 if (r_shadow_realtime_world.integer)
2177 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2179 GL_Color(1, 1, 1, 1);
2180 m.pointer_vertex = face->data_vertex3f;
2182 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2185 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
2188 memset(&m, 0, sizeof(m));
2189 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2190 GL_DepthMask(false);
2192 GL_ColorPointer(NULL);
2193 if (face->texture->skin.glow)
2195 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2196 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2197 GL_Color(1, 1, 1, 1);
2200 GL_Color(0, 0, 0, 1);
2201 m.pointer_vertex = face->data_vertex3f;
2203 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2206 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
2211 memset(&m, 0, sizeof(m));
2212 GL_BlendFunc(GL_ONE, GL_ZERO);
2215 m.tex[0] = R_GetTexture(face->texture->skin.base);
2216 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2218 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2219 mul *= r_shadow_realtime_world_lightmaps.value;
2220 if (mul == 2 && gl_combine.integer)
2222 m.texrgbscale[0] = 2;
2223 GL_ColorPointer(face->data_color4f);
2226 GL_ColorPointer(face->data_color4f);
2229 for (i = 0;i < face->num_vertices;i++)
2231 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2232 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2233 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2234 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2236 GL_ColorPointer(varray_color4f);
2238 m.pointer_vertex = face->data_vertex3f;
2240 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2243 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3mface_t *face)
2248 memset(&m, 0, sizeof(m));
2249 GL_BlendFunc(GL_ONE, GL_ZERO);
2253 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2254 mul *= r_shadow_realtime_world_lightmaps.value;
2256 GL_ColorPointer(face->data_color4f);
2259 for (i = 0;i < face->num_vertices;i++)
2261 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2262 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2263 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2264 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2266 GL_ColorPointer(varray_color4f);
2268 m.pointer_vertex = face->data_vertex3f;
2270 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2273 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
2276 memset(&m, 0, sizeof(m));
2277 GL_BlendFunc(GL_ONE, GL_ONE);
2280 m.tex[0] = R_GetTexture(face->texture->skin.base);
2281 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2282 GL_ColorPointer(NULL);
2283 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2284 m.pointer_vertex = face->data_vertex3f;
2286 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2289 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2291 const entity_render_t *ent = voident;
2292 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
2294 R_Mesh_Matrix(&ent->matrix);
2295 memset(&m, 0, sizeof(m));
2296 if (ent->effects & EF_ADDITIVE)
2297 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2299 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2300 GL_DepthMask(false);
2302 m.tex[0] = R_GetTexture(face->texture->skin.base);
2303 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2304 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2305 if (gl_combine.integer)
2307 m.texrgbscale[0] = 2;
2308 if (r_textureunits.integer >= 2)
2310 m.tex[1] = R_GetTexture(face->lightmaptexture);
2311 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2312 GL_ColorPointer(NULL);
2313 GL_Color(1, 1, 1, ent->alpha);
2317 if (ent->alpha == 1)
2318 GL_ColorPointer(face->data_color4f);
2322 for (i = 0;i < face->num_vertices;i++)
2324 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2325 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2326 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2327 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2329 GL_ColorPointer(varray_color4f);
2336 for (i = 0;i < face->num_vertices;i++)
2338 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2339 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2340 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2341 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2343 GL_ColorPointer(varray_color4f);
2345 m.pointer_vertex = face->data_vertex3f;
2347 qglDisable(GL_CULL_FACE);
2348 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2349 qglEnable(GL_CULL_FACE);
2352 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2354 if (!face->num_triangles)
2356 if (face->texture->surfaceparms)
2358 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2362 face->visframe = r_framecount;
2363 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2365 vec3_t facecenter, center;
2366 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2367 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2368 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2369 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2370 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2373 R_Mesh_Matrix(&ent->matrix);
2374 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2375 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2376 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2378 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2379 if (face->texture->skin.glow)
2380 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2382 else if (face->lightmaptexture)
2384 if (gl_lightmaps.integer)
2385 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2388 if (r_textureunits.integer >= 2 && gl_combine.integer)
2389 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2392 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2393 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2395 if (face->texture->skin.glow)
2396 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2401 if (gl_lightmaps.integer)
2402 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2405 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2406 if (face->texture->skin.glow)
2407 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2410 if (r_ambient.value)
2411 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2414 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2420 if (R_CullBox(node->mins, node->maxs))
2425 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2426 node = node->children[1];
2428 leaf = (q3mleaf_t *)node;
2429 if (CHECKPVSBIT(pvs, leaf->clusterindex))
2432 for (i = 0;i < leaf->numleaffaces;i++)
2433 leaf->firstleafface[i]->markframe = markframe;
2437 // FIXME: num_leafs needs to be recalculated at load time to include only
2438 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2439 // the submodels (which would render the submodels occasionally, as part of
2440 // the world - not good)
2441 void R_Q3BSP_MarkLeafPVS(entity_render_t *ent, qbyte *pvs, int markframe)
2445 for (j = 0, leaf = ent->model->brushq3.data_leafs;j < ent->model->brushq3.num_leafs;j++, leaf++)
2447 if (CHECKPVSBIT(pvs, leaf->clusterindex))
2450 for (i = 0;i < leaf->numleaffaces;i++)
2451 leaf->firstleafface[i]->markframe = markframe;
2456 static int r_q3bsp_framecount = -1;
2458 void R_Q3BSP_DrawSky(entity_render_t *ent)
2465 R_Mesh_Matrix(&ent->matrix);
2467 if (r_drawcollisionbrushes.integer < 2)
2469 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2470 if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2472 if (r_q3bsp_framecount != r_framecount)
2474 r_q3bsp_framecount = r_framecount;
2475 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2476 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2478 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2479 if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2480 R_Q3BSP_DrawSkyFace(ent, face);
2483 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2484 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2485 R_Q3BSP_DrawSkyFace(ent, face);
2489 void R_Q3BSP_Draw(entity_render_t *ent)
2496 R_Mesh_Matrix(&ent->matrix);
2498 if (r_drawcollisionbrushes.integer < 2)
2500 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2501 if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2503 if (r_q3bsp_framecount != r_framecount)
2505 r_q3bsp_framecount = r_framecount;
2506 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2507 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2509 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2510 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2511 R_Q3BSP_DrawFace(ent, face);
2514 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2515 R_Q3BSP_DrawFace(ent, face);
2517 if (r_drawcollisionbrushes.integer >= 1)
2519 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2520 GL_DepthMask(false);
2522 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2523 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2524 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2525 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2526 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2527 if (face->num_collisiontriangles)
2528 R_Q3BSP_DrawCollisionFace(ent, face);
2529 qglPolygonOffset(0, 0);
2533 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2538 vec3_t modelorg, lightmins, lightmaxs;
2540 float projectdistance;
2541 projectdistance = lightradius + ent->model->radius;//projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2542 if (r_drawcollisionbrushes.integer < 2)
2545 R_Mesh_Matrix(&ent->matrix);
2546 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2547 lightmins[0] = relativelightorigin[0] - lightradius;
2548 lightmins[1] = relativelightorigin[1] - lightradius;
2549 lightmins[2] = relativelightorigin[2] - lightradius;
2550 lightmaxs[0] = relativelightorigin[0] + lightradius;
2551 lightmaxs[1] = relativelightorigin[1] + lightradius;
2552 lightmaxs[2] = relativelightorigin[2] + lightradius;
2553 //if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2554 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2556 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2557 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2558 R_Shadow_VolumeFromSphere(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, projectdistance, lightradius);
2561 int j, t, leafnum, marksurfnum;
2567 vec3_t modelorg, lightmins, lightmaxs;
2569 float projectdistance;
2570 projectdistance = lightradius + ent->model->radius;//projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2571 if (r_drawcollisionbrushes.integer < 2)
2574 R_Mesh_Matrix(&ent->matrix);
2575 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2576 lightmins[0] = relativelightorigin[0] - lightradius;
2577 lightmins[1] = relativelightorigin[1] - lightradius;
2578 lightmins[2] = relativelightorigin[2] - lightradius;
2579 lightmaxs[0] = relativelightorigin[0] + lightradius;
2580 lightmaxs[1] = relativelightorigin[1] + lightradius;
2581 lightmaxs[2] = relativelightorigin[2] + lightradius;
2582 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2583 if (ent->model->brush.GetPVS && (pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin)))
2585 for (leafnum = 0, leaf = ent->model->brushq3.data_leafs;leafnum < ent->model->brushq3.num_leafs;leafnum++, leaf++)
2587 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && CHECKPVSBIT(pvs, leaf->clusterindex))
2589 for (marksurfnum = 0;marksurfnum < leaf->numleaffaces;marksurfnum++)
2591 face = leaf->firstleafface[marksurfnum];
2592 if (face->shadowmark != shadowmarkcount)
2594 face->shadowmark = shadowmarkcount;
2595 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2597 for (j = 0, t = face->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < face->num_triangles;j++, t++, e += 3)
2599 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2600 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2601 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2602 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])))
2603 shadowmarklist[numshadowmark++] = t;
2613 for (marksurfnum = 0, face = model->brushq3.data_thismodel->firstface;marksurfnum < model->brushq3.data_thismodel->numfaces;marksurfnum++, face++)
2615 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2617 for (j = 0, t = face->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < face->num_triangles;j++, t++, e += 3)
2619 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2620 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2621 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2622 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])))
2623 shadowmarklist[numshadowmark++] = t;
2628 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, projectdistance, numshadowmark, shadowmarklist);
2633 void R_Q3BSP_DrawFaceLight(entity_render_t *ent, q3mface_t *face, 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)
2635 if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
2637 R_Shadow_DiffuseLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, lightcubemap);
2638 R_Shadow_SpecularLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, lightcubemap);
2641 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)
2643 int leafnum, marksurfnum;
2647 vec3_t modelorg, lightmins, lightmaxs;
2650 //static int markframe = 0;
2651 if (r_drawcollisionbrushes.integer < 2)
2654 R_Mesh_Matrix(&ent->matrix);
2655 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2656 lightmins[0] = relativelightorigin[0] - lightradius;
2657 lightmins[1] = relativelightorigin[1] - lightradius;
2658 lightmins[2] = relativelightorigin[2] - lightradius;
2659 lightmaxs[0] = relativelightorigin[0] + lightradius;
2660 lightmaxs[1] = relativelightorigin[1] + lightradius;
2661 lightmaxs[2] = relativelightorigin[2] + lightradius;
2663 if (ent->model->brush.GetPVS && (pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin)))
2665 pvs = ent->model->brush.GetPVS(ent->model, relativelightorigin);
2666 for (leafnum = 0, leaf = ent->model->brushq3.data_leafs;leafnum < ent->model->brushq3.num_leafs;leafnum++, leaf++)
2668 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && CHECKPVSBIT(pvs, leaf->clusterindex))
2670 for (marksurfnum = 0;marksurfnum < leaf->numleaffaces;marksurfnum++)
2672 face = leaf->firstleafface[marksurfnum];
2673 if (face->shadowmark != shadowmarkcount)
2675 face->shadowmark = shadowmarkcount;
2676 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs) && (ent != &cl_entities[0].render || face->visframe == r_framecount))
2677 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, lightcubemap);
2685 for (marksurfnum = 0, face = model->brushq3.data_thismodel->firstface;marksurfnum < model->brushq3.data_thismodel->numfaces;marksurfnum++, face++)
2686 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs) && (ent != &cl_entities[0].render || face->visframe == r_framecount))
2687 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, lightcubemap);
2692 static void gl_surf_start(void)
2696 static void gl_surf_shutdown(void)
2700 static void gl_surf_newmap(void)
2704 void GL_Surf_Init(void)
2707 dlightdivtable[0] = 4194304;
2708 for (i = 1;i < 32768;i++)
2709 dlightdivtable[i] = 4194304 / (i << 7);
2711 Cvar_RegisterVariable(&r_ambient);
2712 Cvar_RegisterVariable(&r_drawportals);
2713 Cvar_RegisterVariable(&r_testvis);
2714 Cvar_RegisterVariable(&r_floatbuildlightmap);
2715 Cvar_RegisterVariable(&r_detailtextures);
2716 Cvar_RegisterVariable(&r_surfaceworldnode);
2717 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2718 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2719 Cvar_RegisterVariable(&gl_lightmaps);
2721 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);