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_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
37 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
38 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "-4"};
41 static int dlightdivtable[32768];
43 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
45 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
47 float dist, impact[3], local[3];
51 smax = (surf->extents[0] >> 4) + 1;
52 tmax = (surf->extents[1] >> 4) + 1;
55 for (lnum = 0; lnum < r_numdlights; lnum++)
57 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
58 continue; // not lit by this light
60 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
61 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
63 // for comparisons to minimum acceptable light
64 // compensate for LIGHTOFFSET
65 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
72 if (surf->plane->type < 3)
74 VectorCopy(local, impact);
75 impact[surf->plane->type] -= dist;
79 impact[0] = local[0] - surf->plane->normal[0] * dist;
80 impact[1] = local[1] - surf->plane->normal[1] * dist;
81 impact[2] = local[2] - surf->plane->normal[2] * dist;
84 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
85 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
87 s = bound(0, impacts, smax * 16) - impacts;
88 t = bound(0, impactt, tmax * 16) - impactt;
89 i = s * s + t * t + dist2;
93 // reduce calculations
94 for (s = 0, i = impacts; s < smax; s++, i -= 16)
95 sdtable[s] = i * i + dist2;
97 maxdist3 = maxdist - dist2;
99 // convert to 8.8 blocklights format
100 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
101 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
102 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
103 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
107 for (t = 0;t < tmax;t++, i -= 16)
110 // make sure some part of it is visible on this line
113 maxdist2 = maxdist - td;
114 for (s = 0;s < smax;s++)
116 if (sdtable[s] < maxdist2)
118 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
122 bl[1] += (green * k);
137 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
139 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
140 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
144 smax = (surf->extents[0] >> 4) + 1;
145 tmax = (surf->extents[1] >> 4) + 1;
148 for (lnum = 0; lnum < r_numdlights; lnum++)
150 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
151 continue; // not lit by this light
153 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
154 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
156 // for comparisons to minimum acceptable light
157 // compensate for LIGHTOFFSET
158 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
161 dist2 += LIGHTOFFSET;
162 if (dist2 >= maxdist)
165 if (surf->plane->type < 3)
167 VectorCopy(local, impact);
168 impact[surf->plane->type] -= dist;
172 impact[0] = local[0] - surf->plane->normal[0] * dist;
173 impact[1] = local[1] - surf->plane->normal[1] * dist;
174 impact[2] = local[2] - surf->plane->normal[2] * dist;
177 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
178 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
180 td = bound(0, impacts, smax * 16) - impacts;
181 td1 = bound(0, impactt, tmax * 16) - impactt;
182 td = td * td + td1 * td1 + dist2;
186 // reduce calculations
187 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
188 sdtable[s] = td1 * td1 + dist2;
190 maxdist3 = maxdist - dist2;
192 // convert to 8.8 blocklights format
193 red = r_dlight[lnum].light[0];
194 green = r_dlight[lnum].light[1];
195 blue = r_dlight[lnum].light[2];
196 subtract = r_dlight[lnum].subtract * 32768.0f;
197 bl = floatblocklights;
200 for (t = 0;t < tmax;t++, td1 -= 16.0f)
203 // make sure some part of it is visible on this line
206 maxdist2 = maxdist - td;
207 for (s = 0;s < smax;s++)
209 if (sdtable[s] < maxdist2)
211 k = (32768.0f / (sdtable[s] + td)) - subtract;
231 Combine and scale multiple lightmaps into the 8.8 format in blocklights
234 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
236 if (!r_floatbuildlightmap.integer)
238 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
239 unsigned int *bl, scale;
240 qbyte *lightmap, *out, *stain;
242 // update cached lighting info
243 surf->cached_dlight = 0;
245 smax = (surf->extents[0]>>4)+1;
246 tmax = (surf->extents[1]>>4)+1;
249 lightmap = surf->samples;
251 // set to full bright if no light data
253 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
255 for (i = 0;i < size3;i++)
261 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
264 for (i = 0;i < size3;i++)
268 memset(bl, 0, size*3*sizeof(unsigned int));
270 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
272 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
273 if (surf->cached_dlight)
277 // add all the lightmaps
281 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
282 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
283 bl[i] += lightmap[i] * scale;
287 stain = surf->stainsamples;
290 // deal with lightmap brightness scale
291 shift = 7 + r_lightmapscalebit + 8;
292 if (ent->model->brushq1.lightmaprgba)
294 stride = (surf->lightmaptexturestride - smax) * 4;
295 for (i = 0;i < tmax;i++, out += stride)
297 for (j = 0;j < smax;j++)
299 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
300 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
301 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
308 stride = (surf->lightmaptexturestride - smax) * 3;
309 for (i = 0;i < tmax;i++, out += stride)
311 for (j = 0;j < smax;j++)
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
315 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 R_UpdateTexture(surf->lightmaptexture, templight);
324 int smax, tmax, i, j, size, size3, maps, stride, l;
326 qbyte *lightmap, *out, *stain;
328 // update cached lighting info
329 surf->cached_dlight = 0;
331 smax = (surf->extents[0]>>4)+1;
332 tmax = (surf->extents[1]>>4)+1;
335 lightmap = surf->samples;
337 // set to full bright if no light data
338 bl = floatblocklights;
339 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
342 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
347 for (i = 0;i < size3;i++)
351 memset(bl, 0, size*3*sizeof(float));
353 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
355 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
356 if (surf->cached_dlight)
360 // add all the lightmaps
363 bl = floatblocklights;
364 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
365 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
366 bl[i] += lightmap[i] * scale;
369 stain = surf->stainsamples;
370 bl = floatblocklights;
372 // deal with lightmap brightness scale
373 scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
374 if (ent->model->brushq1.lightmaprgba)
376 stride = (surf->lightmaptexturestride - smax) * 4;
377 for (i = 0;i < tmax;i++, out += stride)
379 for (j = 0;j < smax;j++)
381 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
382 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
383 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
390 stride = (surf->lightmaptexturestride - smax) * 3;
391 for (i = 0;i < tmax;i++, out += stride)
393 for (j = 0;j < smax;j++)
395 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
396 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 R_UpdateTexture(surf->lightmaptexture, templight);
406 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
408 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
409 msurface_t *surf, *endsurf;
410 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
414 maxdist = radius * radius;
415 invradius = 1.0f / radius;
418 if (node->contents < 0)
420 ndist = PlaneDiff(origin, node->plane);
423 node = node->children[0];
428 node = node->children[1];
432 dist2 = ndist * ndist;
433 maxdist3 = maxdist - dist2;
435 if (node->plane->type < 3)
437 VectorCopy(origin, impact);
438 impact[node->plane->type] -= ndist;
442 impact[0] = origin[0] - node->plane->normal[0] * ndist;
443 impact[1] = origin[1] - node->plane->normal[1] * ndist;
444 impact[2] = origin[2] - node->plane->normal[2] * ndist;
447 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
449 if (surf->stainsamples)
451 smax = (surf->extents[0] >> 4) + 1;
452 tmax = (surf->extents[1] >> 4) + 1;
454 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
455 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
457 s = bound(0, impacts, smax * 16) - impacts;
458 t = bound(0, impactt, tmax * 16) - impactt;
459 i = s * s + t * t + dist2;
463 // reduce calculations
464 for (s = 0, i = impacts; s < smax; s++, i -= 16)
465 sdtable[s] = i * i + dist2;
467 bl = surf->stainsamples;
472 for (t = 0;t < tmax;t++, i -= 16)
475 // make sure some part of it is visible on this line
478 maxdist2 = maxdist - td;
479 for (s = 0;s < smax;s++)
481 if (sdtable[s] < maxdist2)
483 ratio = lhrandom(0.0f, 1.0f);
484 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
485 if (a >= (1.0f / 64.0f))
489 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
490 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
491 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
501 // force lightmap upload
503 surf->cached_dlight = true;
507 if (node->children[0]->contents >= 0)
509 if (node->children[1]->contents >= 0)
511 R_StainNode(node->children[0], model, origin, radius, fcolor);
512 node = node->children[1];
517 node = node->children[0];
521 else if (node->children[1]->contents >= 0)
523 node = node->children[1];
528 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)
532 entity_render_t *ent;
535 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
540 fcolor[3] = ca1 * (1.0f / 64.0f);
541 fcolor[4] = cr2 - cr1;
542 fcolor[5] = cg2 - cg1;
543 fcolor[6] = cb2 - cb1;
544 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
546 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
548 // look for embedded bmodels
549 for (n = 0;n < cl_num_brushmodel_entities;n++)
551 ent = cl_brushmodel_entities[n];
553 if (model && model->name[0] == '*')
555 Mod_CheckLoaded(model);
556 if (model->brushq1.nodes)
558 Matrix4x4_Transform(&ent->inversematrix, origin, org);
559 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
567 =============================================================
571 =============================================================
574 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
579 if (styles[0] != 255)
581 for (i = 0;i < numverts;i++, c += 4)
583 lm = samples + lightmapoffsets[i];
584 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
585 VectorMA(c, scale, lm, c);
586 if (styles[1] != 255)
589 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
590 VectorMA(c, scale, lm, c);
591 if (styles[2] != 255)
594 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
595 VectorMA(c, scale, lm, c);
596 if (styles[3] != 255)
599 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
600 VectorMA(c, scale, lm, c);
608 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
614 for (i = 0;i < numverts;i++, v += 3, c += 4)
616 VectorSubtract(v, modelorg, diff);
617 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
618 VectorScale(c, f, c);
621 else if (colorscale != 1)
622 for (i = 0;i < numverts;i++, c += 4)
623 VectorScale(c, colorscale, c);
626 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)
635 for (i = 0;i < numverts;i++, v += 3, c += 4)
637 VectorSubtract(v, modelorg, diff);
638 f = 1 - exp(fogdensity/DotProduct(diff, diff));
647 for (i = 0;i < numverts;i++, c += 4)
657 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)
664 for (i = 0;i < numverts;i++, v += 3, c += 4)
666 VectorSubtract(v, modelorg, diff);
667 f = exp(fogdensity/DotProduct(diff, diff));
675 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
680 int i, l, lit = false;
683 for (l = 0;l < r_numdlights;l++)
685 if (dlightbits[l >> 5] & (1 << (l & 31)))
688 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
689 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
691 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
692 if (f < rd->cullradius2)
694 f = ((1.0f / f) - rd->subtract) * scale;
695 VectorMA(c, f, rd->light, c);
704 // note: this untransforms lights to do the checking
705 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
711 for (l = 0;l < r_numdlights;l++)
713 if (dlightbits[l >> 5] & (1 << (l & 31)))
716 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
717 for (i = 0, v = mesh->vertex3f;i < mesh->numverts;i++, v += 3)
718 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
725 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
727 const msurface_t *surf;
728 const surfmesh_t *mesh;
731 // LordHavoc: HalfLife maps have freaky skypolys...
732 if (ent->model->brush.ishlbsp)
737 skyrendernow = false;
742 R_Mesh_Matrix(&ent->matrix);
744 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
747 // depth-only (masking)
748 qglColorMask(0,0,0,0);
749 // just to make sure that braindead drivers don't draw anything
750 // despite that colormask...
751 GL_BlendFunc(GL_ZERO, GL_ONE);
756 GL_BlendFunc(GL_ONE, GL_ZERO);
761 memset(&m, 0, sizeof(m));
762 R_Mesh_State_Texture(&m);
764 while((surf = *surfchain++) != NULL)
766 if (surf->visframe == r_framecount)
768 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
770 GL_VertexPointer(mesh->vertex3f);
771 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
775 qglColorMask(1,1,1,1);
778 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
780 const entity_render_t *ent = calldata1;
781 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
783 const surfmesh_t *mesh;
788 matrix4x4_t tempmatrix;
789 float args[4] = {0.05f,0,0,0.04f};
791 if (r_waterscroll.value)
793 // scrolling in texture matrix
794 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
795 if (gl_textureshader && r_watershader.integer)
797 R_Mesh_TextureMatrix(1, &tempmatrix);
798 Matrix4x4_CreateTranslate(&tempmatrix, -sin(cl.time) * 0.025 * r_waterscroll.value, -sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
800 R_Mesh_TextureMatrix(0, &tempmatrix);
803 R_Mesh_Matrix(&ent->matrix);
804 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
806 memset(&m, 0, sizeof(m));
807 texture = surf->texinfo->texture->currentframe;
808 alpha = texture->currentalpha;
809 if (texture->rendertype == SURFRENDER_ADD)
811 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
814 else if (texture->rendertype == SURFRENDER_ALPHA)
816 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
821 GL_BlendFunc(GL_ONE, GL_ZERO);
824 if (gl_textureshader && r_watershader.integer)
826 m.tex[0] = R_GetTexture(mod_shared_distorttexture);
827 m.tex[1] = R_GetTexture(texture->skin.base);
830 m.tex[0] = R_GetTexture(texture->skin.base);
832 if (gl_combine.integer)
834 m.texrgbscale[0] = 4;
839 GL_ColorPointer(varray_color4f);
841 GL_Color(1, 1, 1, alpha);
842 if (gl_textureshader && r_watershader.integer)
844 GL_ActiveTexture (0);
845 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
846 GL_ActiveTexture (1);
847 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
848 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
849 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
850 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
851 qglEnable (GL_TEXTURE_SHADER_NV);
853 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
855 GL_VertexPointer(mesh->vertex3f);
856 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
857 m.pointer_texcoord[1] = mesh->texcoordtexture2f;
858 m.texcombinergb[1] = GL_REPLACE;
859 R_Mesh_State_Texture(&m);
862 R_FillColors(varray_color4f, mesh->numverts, 1, 1, 1, alpha);
863 RSurf_FogColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, colorscale, mesh->numverts, modelorg);
865 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
867 if (gl_textureshader && r_watershader.integer)
869 qglDisable (GL_TEXTURE_SHADER_NV);
870 GL_ActiveTexture (0);
875 memset(&m, 0, sizeof(m));
876 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
879 m.tex[0] = R_GetTexture(texture->skin.fog);
880 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
882 GL_VertexPointer(mesh->vertex3f);
883 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
884 GL_ColorPointer(varray_color4f);
885 R_Mesh_State_Texture(&m);
886 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, mesh->numverts, modelorg);
887 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
891 if (r_waterscroll.value)
893 Matrix4x4_CreateIdentity(&tempmatrix);
894 R_Mesh_TextureMatrix(0, &tempmatrix);
895 R_Mesh_TextureMatrix(1, &tempmatrix);
899 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
901 const msurface_t *surf;
904 if (texture->rendertype != SURFRENDER_OPAQUE)
906 for (chain = surfchain;(surf = *chain) != NULL;chain++)
908 if (surf->visframe == r_framecount)
910 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
911 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
916 for (chain = surfchain;(surf = *chain) != NULL;chain++)
917 if (surf->visframe == r_framecount)
918 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
921 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
923 float base, colorscale;
924 const surfmesh_t *mesh;
927 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
928 memset(&m, 0, sizeof(m));
929 if (rendertype == SURFRENDER_ADD)
931 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
934 else if (rendertype == SURFRENDER_ALPHA)
936 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
941 GL_BlendFunc(GL_ONE, GL_ZERO);
944 m.tex[0] = R_GetTexture(texture->skin.base);
946 if (gl_combine.integer)
948 m.texrgbscale[0] = 4;
951 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
953 GL_ColorPointer(varray_color4f);
954 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
956 GL_VertexPointer(mesh->vertex3f);
957 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
958 R_Mesh_State_Texture(&m);
959 R_FillColors(varray_color4f, mesh->numverts, base, base, base, currentalpha);
960 if (!(ent->effects & EF_FULLBRIGHT))
962 if (surf->dlightframe == r_framecount)
963 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, mesh->numverts, mesh->vertex3f, varray_color4f, 1);
964 if (surf->flags & SURF_LIGHTMAP)
965 RSurf_AddLightmapToVertexColors_Color4f(mesh->lightmapoffsets, varray_color4f, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
967 RSurf_FogColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, colorscale, mesh->numverts, modelorg);
968 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
972 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
974 const surfmesh_t *mesh;
977 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
978 memset(&m, 0, sizeof(m));
979 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
982 m.tex[0] = R_GetTexture(texture->skin.glow);
983 GL_ColorPointer(varray_color4f);
984 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
986 GL_VertexPointer(mesh->vertex3f);
988 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
989 R_Mesh_State_Texture(&m);
990 RSurf_FoggedColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, mesh->numverts, modelorg);
991 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
995 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
997 const surfmesh_t *mesh;
1000 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1001 memset(&m, 0, sizeof(m));
1002 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1003 GL_DepthMask(false);
1005 m.tex[0] = R_GetTexture(texture->skin.fog);
1006 GL_ColorPointer(varray_color4f);
1007 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1009 GL_VertexPointer(mesh->vertex3f);
1011 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1012 R_Mesh_State_Texture(&m);
1013 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, mesh->numverts, modelorg);
1014 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1018 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1020 const msurface_t *surf;
1021 const surfmesh_t *mesh;
1023 int lightmaptexturenum;
1025 memset(&m, 0, sizeof(m));
1026 GL_BlendFunc(GL_ONE, GL_ZERO);
1029 m.tex[0] = R_GetTexture(texture->skin.base);
1030 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1031 m.tex[2] = R_GetTexture(texture->skin.detail);
1032 m.texrgbscale[0] = 1;
1033 m.texrgbscale[1] = 4;
1034 m.texrgbscale[2] = 2;
1035 cl = (float) (1 << r_lightmapscalebit);
1036 GL_Color(cl, cl, cl, 1);
1038 while((surf = *surfchain++) != NULL)
1040 if (surf->visframe == r_framecount)
1042 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1043 //if (m.tex[1] != lightmaptexturenum)
1045 m.tex[1] = lightmaptexturenum;
1046 // R_Mesh_State_Texture(&m);
1048 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1050 GL_VertexPointer(mesh->vertex3f);
1051 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1052 m.pointer_texcoord[1] = mesh->texcoordlightmap2f;
1053 m.pointer_texcoord[2] = mesh->texcoorddetail2f;
1054 R_Mesh_State_Texture(&m);
1055 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1061 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1063 const msurface_t *surf;
1064 const surfmesh_t *mesh;
1066 int lightmaptexturenum;
1067 memset(&m, 0, sizeof(m));
1068 GL_BlendFunc(GL_ONE, GL_ZERO);
1071 m.tex[0] = R_GetTexture(texture->skin.base);
1072 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1073 if (gl_combine.integer)
1074 m.texrgbscale[1] = 4;
1075 GL_Color(1, 1, 1, 1);
1076 while((surf = *surfchain++) != NULL)
1078 if (surf->visframe == r_framecount)
1080 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1081 //if (m.tex[1] != lightmaptexturenum)
1083 m.tex[1] = lightmaptexturenum;
1084 // R_Mesh_State_Texture(&m);
1086 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1088 GL_VertexPointer(mesh->vertex3f);
1089 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1090 m.pointer_texcoord[1] = mesh->texcoordlightmap2f;
1091 R_Mesh_State_Texture(&m);
1092 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1098 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1100 const msurface_t *surf;
1101 const surfmesh_t *mesh;
1103 memset(&m, 0, sizeof(m));
1106 GL_BlendFunc(GL_ONE, GL_ZERO);
1107 m.tex[0] = R_GetTexture(texture->skin.base);
1108 GL_Color(1, 1, 1, 1);
1109 while((surf = *surfchain++) != NULL)
1111 if (surf->visframe == r_framecount)
1113 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1115 GL_VertexPointer(mesh->vertex3f);
1116 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1117 R_Mesh_State_Texture(&m);
1118 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1124 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1126 const msurface_t *surf;
1127 const surfmesh_t *mesh;
1129 int lightmaptexturenum;
1130 memset(&m, 0, sizeof(m));
1131 GL_BlendFunc(GL_ZERO, GL_SRC_COLOR);
1132 GL_DepthMask(false);
1134 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1135 if (gl_combine.integer)
1136 m.texrgbscale[0] = 4;
1137 GL_Color(1, 1, 1, 1);
1138 while((surf = *surfchain++) != NULL)
1140 if (surf->visframe == r_framecount)
1142 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1143 //if (m.tex[0] != lightmaptexturenum)
1145 m.tex[0] = lightmaptexturenum;
1146 // R_Mesh_State_Texture(&m);
1148 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1150 GL_VertexPointer(mesh->vertex3f);
1151 m.pointer_texcoord[0] = mesh->texcoordlightmap2f;
1152 R_Mesh_State_Texture(&m);
1153 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1159 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1161 const msurface_t *surf;
1162 const surfmesh_t *mesh;
1166 memset(&m, 0, sizeof(m));
1167 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1168 GL_DepthMask(false);
1170 m.tex[0] = R_GetTexture(texture->skin.base);
1172 if (gl_combine.integer)
1174 m.texrgbscale[0] = 4;
1175 colorscale *= 0.25f;
1177 GL_ColorPointer(varray_color4f);
1178 while((surf = *surfchain++) != NULL)
1180 if (surf->visframe == r_framecount && surf->dlightframe == r_framecount)
1182 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1184 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1186 GL_VertexPointer(mesh->vertex3f);
1187 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1188 R_FillColors(varray_color4f, mesh->numverts, 0, 0, 0, 1);
1189 R_Mesh_State_Texture(&m);
1190 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, mesh->numverts, mesh->vertex3f, varray_color4f, colorscale);
1191 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1198 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1200 const msurface_t *surf;
1201 const surfmesh_t *mesh;
1204 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1205 memset(&m, 0, sizeof(m));
1206 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1207 GL_DepthMask(false);
1209 GL_ColorPointer(varray_color4f);
1210 while((surf = *surfchain++) != NULL)
1212 if (surf->visframe == r_framecount)
1214 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1216 GL_VertexPointer(mesh->vertex3f);
1218 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1219 R_Mesh_State_Texture(&m);
1220 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, mesh->numverts, modelorg);
1221 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1227 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1229 const msurface_t *surf;
1230 const surfmesh_t *mesh;
1232 memset(&m, 0, sizeof(m));
1233 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1234 GL_DepthMask(false);
1236 m.tex[0] = R_GetTexture(texture->skin.detail);
1237 GL_Color(1, 1, 1, 1);
1238 while((surf = *surfchain++) != NULL)
1240 if (surf->visframe == r_framecount)
1242 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1244 GL_VertexPointer(mesh->vertex3f);
1245 m.pointer_texcoord[0] = mesh->texcoorddetail2f;
1246 R_Mesh_State_Texture(&m);
1247 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1253 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1255 const msurface_t *surf;
1256 const surfmesh_t *mesh;
1258 memset(&m, 0, sizeof(m));
1259 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1260 GL_DepthMask(false);
1262 m.tex[0] = R_GetTexture(texture->skin.glow);
1263 GL_Color(1, 1, 1, 1);
1264 while((surf = *surfchain++) != NULL)
1266 if (surf->visframe == r_framecount)
1268 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1270 GL_VertexPointer(mesh->vertex3f);
1271 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1272 R_Mesh_State_Texture(&m);
1273 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1279 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1281 const msurface_t *surf;
1282 const surfmesh_t *mesh;
1284 memset(&m, 0, sizeof(m));
1285 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1287 m.tex[0] = R_GetTexture(texture->skin.glow);
1289 GL_Color(1, 1, 1, 1);
1291 GL_Color(0, 0, 0, 1);
1292 while((surf = *surfchain++) != NULL)
1294 if (surf->visframe == r_framecount)
1296 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1298 GL_VertexPointer(mesh->vertex3f);
1299 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1300 R_Mesh_State_Texture(&m);
1301 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1307 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1309 const entity_render_t *ent = calldata1;
1310 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1314 R_Mesh_Matrix(&ent->matrix);
1316 texture = surf->texinfo->texture;
1317 if (texture->animated)
1318 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];
1320 currentalpha = ent->alpha;
1321 if (ent->effects & EF_ADDITIVE)
1322 rendertype = SURFRENDER_ADD;
1323 else if (currentalpha < 1 || texture->skin.fog != NULL)
1324 rendertype = SURFRENDER_ALPHA;
1326 rendertype = SURFRENDER_OPAQUE;
1328 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1329 if (texture->skin.glow)
1330 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1332 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1335 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1337 const msurface_t *surf;
1340 if (texture->rendertype != SURFRENDER_OPAQUE)
1342 // transparent vertex shaded from lightmap
1343 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1345 if (surf->visframe == r_framecount)
1347 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1348 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1352 else if (r_shadow_realtime_world.integer)
1354 // opaque base lighting
1355 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1357 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1361 // opaque lightmapped
1362 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1363 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1364 else if (r_textureunits.integer >= 2)
1366 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture, surfchain);
1367 if (r_detailtextures.integer)
1368 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1372 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1373 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1374 if (r_detailtextures.integer)
1375 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1377 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1378 RSurfShader_OpaqueWall_Pass_Light(ent, texture, surfchain);
1379 if (texture->skin.glow)
1380 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1382 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1386 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1387 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1388 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1390 int Cshader_count = 3;
1391 Cshader_t *Cshaders[3] =
1393 &Cshader_wall_lightmap,
1398 void R_UpdateTextureInfo(entity_render_t *ent)
1400 int i, texframe, alttextures;
1406 alttextures = ent->frame != 0;
1407 texframe = (int)(cl.time * 5.0f);
1408 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1410 t = ent->model->brushq1.textures + i;
1411 t->currentalpha = ent->alpha;
1412 if (t->flags & SURF_WATERALPHA)
1413 t->currentalpha *= r_wateralpha.value;
1414 if (ent->effects & EF_ADDITIVE)
1415 t->rendertype = SURFRENDER_ADD;
1416 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1417 t->rendertype = SURFRENDER_ALPHA;
1419 t->rendertype = SURFRENDER_OPAQUE;
1420 // we don't need to set currentframe if t->animated is false because
1421 // it was already set up by the texture loader for non-animating
1423 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1427 void R_PrepareSurfaces(entity_render_t *ent)
1429 int i, numsurfaces, *surfacevisframes;
1431 msurface_t *surf, *surfaces, **surfchain;
1438 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1439 numsurfaces = model->brushq1.nummodelsurfaces;
1440 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1441 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1443 R_UpdateTextureInfo(ent);
1445 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1448 if (model->brushq1.light_ambient != r_ambient.value || model->brushq1.light_scalebit != r_lightmapscalebit)
1450 model->brushq1.light_ambient = r_ambient.value;
1451 model->brushq1.light_scalebit = r_lightmapscalebit;
1452 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1453 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1457 for (i = 0;i < model->brushq1.light_styles;i++)
1459 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1461 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1462 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1463 (**surfchain).cached_dlight = true;
1468 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1470 if (surfacevisframes[i] == r_framecount)
1472 #if !WORLDNODECULLBACKFACES
1473 // mark any backface surfaces as not visible
1474 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1476 if (!(surf->flags & SURF_PLANEBACK))
1477 surfacevisframes[i] = -1;
1481 if ((surf->flags & SURF_PLANEBACK))
1482 surfacevisframes[i] = -1;
1484 if (surfacevisframes[i] == r_framecount)
1488 surf->visframe = r_framecount;
1489 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1490 R_BuildLightMap(ent, surf);
1496 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1500 if (ent->model == NULL)
1502 R_Mesh_Matrix(&ent->matrix);
1503 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1504 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1505 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1508 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1513 const entity_render_t *ent = calldata1;
1514 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1515 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1516 GL_DepthMask(false);
1518 R_Mesh_Matrix(&ent->matrix);
1519 GL_VertexPointer(varray_vertex3f);
1521 memset(&m, 0, sizeof(m));
1522 R_Mesh_State_Texture(&m);
1524 i = portal - ent->model->brushq1.portals;
1525 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1526 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1527 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1529 if (PlaneDiff(r_origin, (&portal->plane)) < 0)
1531 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1532 VectorCopy(portal->points[i].position, v);
1535 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1536 VectorCopy(portal->points[i].position, v);
1537 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1540 // LordHavoc: this is just a nice debugging tool, very slow
1541 static void R_DrawPortals(entity_render_t *ent)
1544 mportal_t *portal, *endportal;
1545 float temp[3], center[3], f;
1546 if (ent->model == NULL)
1548 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1550 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1553 for (i = 0;i < portal->numpoints;i++)
1554 VectorAdd(temp, portal->points[i].position, temp);
1555 f = ixtable[portal->numpoints];
1556 VectorScale(temp, f, temp);
1557 Matrix4x4_Transform(&ent->matrix, temp, center);
1558 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1563 void R_PrepareBrushModel(entity_render_t *ent)
1565 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1568 #if WORLDNODECULLBACKFACES
1572 // because bmodels can be reused, we have to decide which things to render
1573 // from scratch every time
1577 #if WORLDNODECULLBACKFACES
1578 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1580 numsurfaces = model->brushq1.nummodelsurfaces;
1581 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1582 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1583 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1584 for (i = 0;i < numsurfaces;i++, surf++)
1586 #if WORLDNODECULLBACKFACES
1587 // mark any backface surfaces as not visible
1588 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1590 if ((surf->flags & SURF_PLANEBACK))
1591 surfacevisframes[i] = r_framecount;
1593 else if (!(surf->flags & SURF_PLANEBACK))
1594 surfacevisframes[i] = r_framecount;
1596 surfacevisframes[i] = r_framecount;
1598 surf->dlightframe = -1;
1600 R_PrepareSurfaces(ent);
1603 void R_SurfaceWorldNode (entity_render_t *ent)
1605 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1611 // equivilant to quake's RecursiveWorldNode but faster and more effective
1615 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1616 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1617 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1619 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1621 if (!R_CullBox (leaf->mins, leaf->maxs))
1624 leaf->visframe = r_framecount;
1628 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1630 surfnum = model->brushq1.pvssurflist[i];
1631 surf = model->brushq1.surfaces + surfnum;
1632 #if WORLDNODECULLBACKFACES
1633 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1635 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1636 surfacevisframes[surfnum] = r_framecount;
1640 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1641 surfacevisframes[surfnum] = r_framecount;
1644 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1645 surfacevisframes[surfnum] = r_framecount;
1650 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1652 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1653 #if WORLDNODECULLBACKFACES
1657 mleaf_t *leaf, *leafstack[8192];
1660 msurface_t *surfaces;
1661 if (ent->model == NULL)
1663 // LordHavoc: portal-passage worldnode with PVS;
1664 // follows portals leading outward from viewleaf, does not venture
1665 // offscreen or into leafs that are not visible, faster than Quake's
1666 // RecursiveWorldNode
1667 surfaces = ent->model->brushq1.surfaces;
1668 surfacevisframes = ent->model->brushq1.surfacevisframes;
1669 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1670 viewleaf->worldnodeframe = r_framecount;
1671 leafstack[0] = viewleaf;
1673 while (leafstackpos)
1676 leaf = leafstack[--leafstackpos];
1677 leaf->visframe = r_framecount;
1678 // draw any surfaces bounding this leaf
1679 if (leaf->nummarksurfaces)
1681 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1683 #if WORLDNODECULLBACKFACES
1685 if (surfacevisframes[n] != r_framecount)
1687 surf = surfaces + n;
1688 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1690 if ((surf->flags & SURF_PLANEBACK))
1691 surfacevisframes[n] = r_framecount;
1695 if (!(surf->flags & SURF_PLANEBACK))
1696 surfacevisframes[n] = r_framecount;
1700 surfacevisframes[*mark++] = r_framecount;
1704 // follow portals into other leafs
1705 for (p = leaf->portals;p;p = p->next)
1707 // LordHavoc: this DotProduct hurts less than a cache miss
1708 // (which is more likely to happen if backflowing through leafs)
1709 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1712 if (leaf->worldnodeframe != r_framecount)
1714 leaf->worldnodeframe = r_framecount;
1715 // FIXME: R_CullBox is absolute, should be done relative
1716 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1717 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1718 leafstack[leafstackpos++] = leaf;
1725 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1727 int j, c, *surfacepvsframes, *mark;
1732 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1734 model->brushq1.pvsframecount++;
1735 model->brushq1.pvsviewleaf = viewleaf;
1736 model->brushq1.pvsviewleafnovis = r_novis.integer;
1737 model->brushq1.pvsleafchain = NULL;
1738 model->brushq1.pvssurflistlength = 0;
1741 surfacepvsframes = model->brushq1.surfacepvsframes;
1742 for (j = 0;j < model->brushq1.visleafs;j++)
1744 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1746 leaf = model->brushq1.leafs + j + 1;
1747 leaf->pvsframe = model->brushq1.pvsframecount;
1748 leaf->pvschain = model->brushq1.pvsleafchain;
1749 model->brushq1.pvsleafchain = leaf;
1750 // mark surfaces bounding this leaf as visible
1751 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1752 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1755 model->brushq1.BuildPVSTextureChains(model);
1760 void R_WorldVisibility(entity_render_t *ent)
1765 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1766 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1767 R_PVSUpdate(ent, viewleaf);
1772 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1773 R_SurfaceWorldNode (ent);
1775 R_PortalWorldNode (ent, viewleaf);
1778 void R_DrawWorld(entity_render_t *ent)
1780 if (ent->model == NULL)
1782 if (!ent->model->brushq1.numleafs && ent->model->Draw)
1783 ent->model->Draw(ent);
1786 R_PrepareSurfaces(ent);
1787 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1788 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1789 if (r_drawportals.integer)
1794 void R_Model_Brush_DrawSky(entity_render_t *ent)
1796 if (ent->model == NULL)
1798 if (ent != &cl_entities[0].render)
1799 R_PrepareBrushModel(ent);
1800 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1803 void R_Model_Brush_Draw(entity_render_t *ent)
1805 if (ent->model == NULL)
1808 if (ent != &cl_entities[0].render)
1809 R_PrepareBrushModel(ent);
1810 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1813 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1817 float projectdistance, f, temp[3], lightradius2;
1819 if (ent->model == NULL)
1821 R_Mesh_Matrix(&ent->matrix);
1822 lightradius2 = lightradius * lightradius;
1823 R_UpdateTextureInfo(ent);
1824 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1825 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1827 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1829 f = PlaneDiff(relativelightorigin, surf->plane);
1830 if (surf->flags & SURF_PLANEBACK)
1832 // draw shadows only for frontfaces and only if they are close
1833 if (f >= 0.1 && f < lightradius)
1835 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1836 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1837 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1838 if (DotProduct(temp, temp) < lightradius2)
1839 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1840 R_Shadow_Volume(mesh->numverts, mesh->numtriangles, mesh->vertex3f, mesh->element3i, mesh->neighbor3i, relativelightorigin, lightradius, projectdistance);
1846 void R_Model_Brush_DrawLightForSurfaceList(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, msurface_t **surflist, int numsurfaces, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
1852 if (ent->model == NULL)
1854 R_Mesh_Matrix(&ent->matrix);
1855 R_UpdateTextureInfo(ent);
1856 for (surfnum = 0;surfnum < numsurfaces;surfnum++)
1858 surf = surflist[surfnum];
1859 if (surf->visframe == r_framecount)
1861 t = surf->texinfo->texture->currentframe;
1862 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1864 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1866 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, NULL);
1867 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, NULL);
1874 void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
1879 float f, lightmins[3], lightmaxs[3];
1881 if (ent->model == NULL)
1883 R_Mesh_Matrix(&ent->matrix);
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_UpdateTextureInfo(ent);
1891 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1893 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1895 f = PlaneDiff(relativelightorigin, surf->plane);
1896 if (surf->flags & SURF_PLANEBACK)
1898 if (f >= -0.1 && f < lightradius)
1900 t = surf->texinfo->texture->currentframe;
1901 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1903 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1905 R_Shadow_DiffuseLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, NULL);
1906 R_Shadow_SpecularLighting(mesh->numverts, mesh->numtriangles, mesh->element3i, mesh->vertex3f, mesh->svector3f, mesh->tvector3f, mesh->normal3f, mesh->texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, NULL);
1914 void R_DrawCollisionBrush(colbrushf_t *brush)
1917 i = ((int)brush) / sizeof(colbrushf_t);
1918 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1919 GL_VertexPointer(brush->points->v);
1920 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1923 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
1926 if (!face->numtriangles)
1928 if (face->texture->renderflags)
1930 if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY)
1934 skyrendernow = false;
1935 if (skyrendermasked)
1939 R_Mesh_Matrix(&ent->matrix);
1941 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1942 if (skyrendermasked)
1944 // depth-only (masking)
1945 qglColorMask(0,0,0,0);
1946 // just to make sure that braindead drivers don't draw anything
1947 // despite that colormask...
1948 GL_BlendFunc(GL_ZERO, GL_ONE);
1953 GL_BlendFunc(GL_ONE, GL_ZERO);
1958 memset(&m, 0, sizeof(m));
1959 R_Mesh_State_Texture(&m);
1961 GL_VertexPointer(face->data_vertex3f);
1962 R_Mesh_Draw(face->numvertices, face->numtriangles, face->data_element3i);
1963 qglColorMask(1,1,1,1);
1966 if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW)
1969 R_Mesh_Matrix(&ent->matrix);
1970 face->visframe = r_framecount;
1971 memset(&m, 0, sizeof(m));
1972 GL_BlendFunc(GL_ONE, GL_ZERO);
1975 m.tex[0] = R_GetTexture(face->texture->skin.base);
1976 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1977 if (face->lightmaptexture)
1979 m.tex[1] = R_GetTexture(face->lightmaptexture);
1980 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1981 m.texrgbscale[1] = 2;
1982 GL_Color(1, 1, 1, 1);
1986 m.texrgbscale[0] = 2;
1987 GL_ColorPointer(face->data_color4f);
1989 R_Mesh_State_Texture(&m);
1990 GL_VertexPointer(face->data_vertex3f);
1991 R_Mesh_Draw(face->numvertices, face->numtriangles, face->data_element3i);
1995 void R_Q3BSP_DrawSky(entity_render_t *ent)
2000 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2005 while (node->isnode)
2007 if (R_CullBox(node->mins, node->maxs))
2009 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2010 node = node->children[1];
2012 if (R_CullBox(node->mins, node->maxs))
2014 leaf = (q3mleaf_t *)node;
2015 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2017 for (i = 0;i < leaf->numleaffaces;i++)
2019 face = leaf->firstleafface[i];
2020 if (face->markframe != markframe)
2022 face->markframe = markframe;
2023 if (!R_CullBox(face->mins, face->maxs))
2024 R_Q3BSP_DrawFace(ent, face);
2032 void R_Q3BSP_Draw(entity_render_t *ent)
2039 static int markframe = 0;
2040 R_Mesh_Matrix(&ent->matrix);
2042 if (r_drawcollisionbrushes.integer < 2)
2044 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2045 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2046 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2048 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2049 R_Q3BSP_DrawFace(ent, face);
2051 if (r_drawcollisionbrushes.integer >= 1)
2054 memset(&m, 0, sizeof(m));
2055 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2056 GL_DepthMask(false);
2058 R_Mesh_State_Texture(&m);
2059 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2060 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2061 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2065 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2069 vec3_t modelorg, lightmins, lightmaxs;
2071 float projectdistance;
2072 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2073 if (r_drawcollisionbrushes.integer < 2)
2076 R_Mesh_Matrix(&ent->matrix);
2077 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2078 lightmins[0] = relativelightorigin[0] - lightradius;
2079 lightmins[1] = relativelightorigin[1] - lightradius;
2080 lightmins[2] = relativelightorigin[2] - lightradius;
2081 lightmaxs[0] = relativelightorigin[0] + lightradius;
2082 lightmaxs[1] = relativelightorigin[1] + lightradius;
2083 lightmaxs[2] = relativelightorigin[2] + lightradius;
2084 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2085 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2087 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2088 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2089 R_Shadow_Volume(face->numvertices, face->numtriangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2093 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_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
2095 if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW) || !face->numtriangles)
2097 R_Shadow_DiffuseLighting(face->numvertices, face->numtriangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, NULL);
2098 R_Shadow_SpecularLighting(face->numvertices, face->numtriangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, NULL);
2101 void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltofilter, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz)
2105 vec3_t modelorg, lightmins, lightmaxs;
2108 //static int markframe = 0;
2109 if (r_drawcollisionbrushes.integer < 2)
2112 R_Mesh_Matrix(&ent->matrix);
2113 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2114 lightmins[0] = relativelightorigin[0] - lightradius;
2115 lightmins[1] = relativelightorigin[1] - lightradius;
2116 lightmins[2] = relativelightorigin[2] - lightradius;
2117 lightmaxs[0] = relativelightorigin[0] + lightradius;
2118 lightmaxs[1] = relativelightorigin[1] + lightradius;
2119 lightmaxs[2] = relativelightorigin[2] + lightradius;
2120 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2121 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2123 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2124 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2125 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2129 static void gl_surf_start(void)
2133 static void gl_surf_shutdown(void)
2137 static void gl_surf_newmap(void)
2141 void GL_Surf_Init(void)
2144 dlightdivtable[0] = 4194304;
2145 for (i = 1;i < 32768;i++)
2146 dlightdivtable[i] = 4194304 / (i << 7);
2148 Cvar_RegisterVariable(&r_ambient);
2149 Cvar_RegisterVariable(&r_dlightmap);
2150 Cvar_RegisterVariable(&r_drawportals);
2151 Cvar_RegisterVariable(&r_testvis);
2152 Cvar_RegisterVariable(&r_floatbuildlightmap);
2153 Cvar_RegisterVariable(&r_detailtextures);
2154 Cvar_RegisterVariable(&r_surfaceworldnode);
2155 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2157 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);