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_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
34 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
35 cvar_t r_drawportals = {0, "r_drawportals", "0"};
36 cvar_t r_testvis = {0, "r_testvis", "0"};
37 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
38 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
39 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
40 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "-4"};
42 static int dlightdivtable[32768];
44 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
46 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
48 float dist, impact[3], local[3];
52 smax = (surf->extents[0] >> 4) + 1;
53 tmax = (surf->extents[1] >> 4) + 1;
56 for (lnum = 0; lnum < r_numdlights; lnum++)
58 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
59 continue; // not lit by this light
61 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
62 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
64 // for comparisons to minimum acceptable light
65 // compensate for LIGHTOFFSET
66 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
73 if (surf->plane->type < 3)
75 VectorCopy(local, impact);
76 impact[surf->plane->type] -= dist;
80 impact[0] = local[0] - surf->plane->normal[0] * dist;
81 impact[1] = local[1] - surf->plane->normal[1] * dist;
82 impact[2] = local[2] - surf->plane->normal[2] * dist;
85 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
86 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
88 s = bound(0, impacts, smax * 16) - impacts;
89 t = bound(0, impactt, tmax * 16) - impactt;
90 i = s * s + t * t + dist2;
94 // reduce calculations
95 for (s = 0, i = impacts; s < smax; s++, i -= 16)
96 sdtable[s] = i * i + dist2;
98 maxdist3 = maxdist - dist2;
100 // convert to 8.8 blocklights format
101 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
102 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
103 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
104 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
108 for (t = 0;t < tmax;t++, i -= 16)
111 // make sure some part of it is visible on this line
114 maxdist2 = maxdist - td;
115 for (s = 0;s < smax;s++)
117 if (sdtable[s] < maxdist2)
119 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
123 bl[1] += (green * k);
138 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
140 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
141 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
145 smax = (surf->extents[0] >> 4) + 1;
146 tmax = (surf->extents[1] >> 4) + 1;
149 for (lnum = 0; lnum < r_numdlights; lnum++)
151 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
152 continue; // not lit by this light
154 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
155 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
157 // for comparisons to minimum acceptable light
158 // compensate for LIGHTOFFSET
159 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
162 dist2 += LIGHTOFFSET;
163 if (dist2 >= maxdist)
166 if (surf->plane->type < 3)
168 VectorCopy(local, impact);
169 impact[surf->plane->type] -= dist;
173 impact[0] = local[0] - surf->plane->normal[0] * dist;
174 impact[1] = local[1] - surf->plane->normal[1] * dist;
175 impact[2] = local[2] - surf->plane->normal[2] * dist;
178 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
179 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
181 td = bound(0, impacts, smax * 16) - impacts;
182 td1 = bound(0, impactt, tmax * 16) - impactt;
183 td = td * td + td1 * td1 + dist2;
187 // reduce calculations
188 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
189 sdtable[s] = td1 * td1 + dist2;
191 maxdist3 = maxdist - dist2;
193 // convert to 8.8 blocklights format
194 red = r_dlight[lnum].light[0];
195 green = r_dlight[lnum].light[1];
196 blue = r_dlight[lnum].light[2];
197 subtract = r_dlight[lnum].subtract * 32768.0f;
198 bl = floatblocklights;
201 for (t = 0;t < tmax;t++, td1 -= 16.0f)
204 // make sure some part of it is visible on this line
207 maxdist2 = maxdist - td;
208 for (s = 0;s < smax;s++)
210 if (sdtable[s] < maxdist2)
212 k = (32768.0f / (sdtable[s] + td)) - subtract;
232 Combine and scale multiple lightmaps into the 8.8 format in blocklights
235 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
237 if (!r_floatbuildlightmap.integer)
239 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
240 unsigned int *bl, scale;
241 qbyte *lightmap, *out, *stain;
243 // update cached lighting info
244 surf->cached_dlight = 0;
246 smax = (surf->extents[0]>>4)+1;
247 tmax = (surf->extents[1]>>4)+1;
250 lightmap = surf->samples;
252 // set to full bright if no light data
254 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
256 for (i = 0;i < size3;i++)
262 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
265 for (i = 0;i < size3;i++)
269 memset(bl, 0, size*3*sizeof(unsigned int));
271 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
273 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
274 if (surf->cached_dlight)
278 // add all the lightmaps
282 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
283 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
284 bl[i] += lightmap[i] * scale;
288 stain = surf->stainsamples;
291 // deal with lightmap brightness scale
292 shift = 7 + r_lightmapscalebit + 8;
293 if (ent->model->brushq1.lightmaprgba)
295 stride = (surf->lightmaptexturestride - smax) * 4;
296 for (i = 0;i < tmax;i++, out += stride)
298 for (j = 0;j < smax;j++)
300 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
301 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
302 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
309 stride = (surf->lightmaptexturestride - smax) * 3;
310 for (i = 0;i < tmax;i++, out += stride)
312 for (j = 0;j < smax;j++)
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
315 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
316 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
321 R_UpdateTexture(surf->lightmaptexture, templight);
325 int smax, tmax, i, j, size, size3, maps, stride, l;
327 qbyte *lightmap, *out, *stain;
329 // update cached lighting info
330 surf->cached_dlight = 0;
332 smax = (surf->extents[0]>>4)+1;
333 tmax = (surf->extents[1]>>4)+1;
336 lightmap = surf->samples;
338 // set to full bright if no light data
339 bl = floatblocklights;
340 if ((ent->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
343 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
348 for (i = 0;i < size3;i++)
352 memset(bl, 0, size*3*sizeof(float));
354 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
356 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
357 if (surf->cached_dlight)
361 // add all the lightmaps
364 bl = floatblocklights;
365 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
366 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
367 bl[i] += lightmap[i] * scale;
370 stain = surf->stainsamples;
371 bl = floatblocklights;
373 // deal with lightmap brightness scale
374 scale = 1.0f / (1 << (7 + r_lightmapscalebit + 8));
375 if (ent->model->brushq1.lightmaprgba)
377 stride = (surf->lightmaptexturestride - smax) * 4;
378 for (i = 0;i < tmax;i++, out += stride)
380 for (j = 0;j < smax;j++)
382 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
383 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
384 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
391 stride = (surf->lightmaptexturestride - smax) * 3;
392 for (i = 0;i < tmax;i++, out += stride)
394 for (j = 0;j < smax;j++)
396 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
397 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 R_UpdateTexture(surf->lightmaptexture, templight);
407 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
409 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
410 msurface_t *surf, *endsurf;
411 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
415 maxdist = radius * radius;
416 invradius = 1.0f / radius;
419 if (node->contents < 0)
421 ndist = PlaneDiff(origin, node->plane);
424 node = node->children[0];
429 node = node->children[1];
433 dist2 = ndist * ndist;
434 maxdist3 = maxdist - dist2;
436 if (node->plane->type < 3)
438 VectorCopy(origin, impact);
439 impact[node->plane->type] -= ndist;
443 impact[0] = origin[0] - node->plane->normal[0] * ndist;
444 impact[1] = origin[1] - node->plane->normal[1] * ndist;
445 impact[2] = origin[2] - node->plane->normal[2] * ndist;
448 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
450 if (surf->stainsamples)
452 smax = (surf->extents[0] >> 4) + 1;
453 tmax = (surf->extents[1] >> 4) + 1;
455 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
456 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
458 s = bound(0, impacts, smax * 16) - impacts;
459 t = bound(0, impactt, tmax * 16) - impactt;
460 i = s * s + t * t + dist2;
464 // reduce calculations
465 for (s = 0, i = impacts; s < smax; s++, i -= 16)
466 sdtable[s] = i * i + dist2;
468 bl = surf->stainsamples;
473 for (t = 0;t < tmax;t++, i -= 16)
476 // make sure some part of it is visible on this line
479 maxdist2 = maxdist - td;
480 for (s = 0;s < smax;s++)
482 if (sdtable[s] < maxdist2)
484 ratio = lhrandom(0.0f, 1.0f);
485 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
486 if (a >= (1.0f / 64.0f))
490 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
491 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
492 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
502 // force lightmap upload
504 surf->cached_dlight = true;
508 if (node->children[0]->contents >= 0)
510 if (node->children[1]->contents >= 0)
512 R_StainNode(node->children[0], model, origin, radius, fcolor);
513 node = node->children[1];
518 node = node->children[0];
522 else if (node->children[1]->contents >= 0)
524 node = node->children[1];
529 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)
533 entity_render_t *ent;
536 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
541 fcolor[3] = ca1 * (1.0f / 64.0f);
542 fcolor[4] = cr2 - cr1;
543 fcolor[5] = cg2 - cg1;
544 fcolor[6] = cb2 - cb1;
545 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
547 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
549 // look for embedded bmodels
550 for (n = 0;n < cl_num_brushmodel_entities;n++)
552 ent = cl_brushmodel_entities[n];
554 if (model && model->name[0] == '*')
556 Mod_CheckLoaded(model);
557 if (model->brushq1.nodes)
559 Matrix4x4_Transform(&ent->inversematrix, origin, org);
560 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
568 =============================================================
572 =============================================================
575 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
580 if (styles[0] != 255)
582 for (i = 0;i < numverts;i++, c += 4)
584 lm = samples + lightmapoffsets[i];
585 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
586 VectorMA(c, scale, lm, c);
587 if (styles[1] != 255)
590 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
591 VectorMA(c, scale, lm, c);
592 if (styles[2] != 255)
595 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
596 VectorMA(c, scale, lm, c);
597 if (styles[3] != 255)
600 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
601 VectorMA(c, scale, lm, c);
609 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
615 for (i = 0;i < numverts;i++, v += 3, c += 4)
617 VectorSubtract(v, modelorg, diff);
618 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
619 VectorScale(c, f, c);
622 else if (colorscale != 1)
623 for (i = 0;i < numverts;i++, c += 4)
624 VectorScale(c, colorscale, c);
627 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)
636 for (i = 0;i < numverts;i++, v += 3, c += 4)
638 VectorSubtract(v, modelorg, diff);
639 f = 1 - exp(fogdensity/DotProduct(diff, diff));
648 for (i = 0;i < numverts;i++, c += 4)
658 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)
665 for (i = 0;i < numverts;i++, v += 3, c += 4)
667 VectorSubtract(v, modelorg, diff);
668 f = exp(fogdensity/DotProduct(diff, diff));
676 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
681 int i, l, lit = false;
684 for (l = 0;l < r_numdlights;l++)
686 if (dlightbits[l >> 5] & (1 << (l & 31)))
689 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
690 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
692 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
693 if (f < rd->cullradius2)
695 f = ((1.0f / f) - rd->subtract) * scale;
696 VectorMA(c, f, rd->light, c);
705 // note: this untransforms lights to do the checking
706 static int RSurf_LightCheck(const matrix4x4_t *matrix, const int *dlightbits, const surfmesh_t *mesh)
712 for (l = 0;l < r_numdlights;l++)
714 if (dlightbits[l >> 5] & (1 << (l & 31)))
717 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
718 for (i = 0, v = mesh->vertex3f;i < mesh->numverts;i++, v += 3)
719 if (VectorDistance2(v, lightorigin) < rd->cullradius2)
726 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
728 const msurface_t *surf;
729 const surfmesh_t *mesh;
732 // LordHavoc: HalfLife maps have freaky skypolys...
733 if (ent->model->brush.ishlbsp)
738 skyrendernow = false;
743 R_Mesh_Matrix(&ent->matrix);
745 GL_Color(fogcolor[0] * r_colorscale, fogcolor[1] * r_colorscale, fogcolor[2] * r_colorscale, 1);
748 // depth-only (masking)
749 qglColorMask(0,0,0,0);
750 // just to make sure that braindead drivers don't draw anything
751 // despite that colormask...
752 GL_BlendFunc(GL_ZERO, GL_ONE);
757 GL_BlendFunc(GL_ONE, GL_ZERO);
762 memset(&m, 0, sizeof(m));
763 R_Mesh_State_Texture(&m);
765 while((surf = *surfchain++) != NULL)
767 if (surf->visframe == r_framecount)
769 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
771 GL_VertexPointer(mesh->vertex3f);
772 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
776 qglColorMask(1,1,1,1);
779 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
781 const entity_render_t *ent = calldata1;
782 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
784 const surfmesh_t *mesh;
789 matrix4x4_t tempmatrix;
790 float args[4] = {0.05f,0,0,0.04f};
792 if (r_waterscroll.value)
794 // scrolling in texture matrix
795 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
796 if (gl_textureshader && r_watershader.integer)
798 R_Mesh_TextureMatrix(1, &tempmatrix);
799 Matrix4x4_CreateTranslate(&tempmatrix, -sin(cl.time) * 0.025 * r_waterscroll.value, -sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
801 R_Mesh_TextureMatrix(0, &tempmatrix);
804 R_Mesh_Matrix(&ent->matrix);
805 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
807 memset(&m, 0, sizeof(m));
808 texture = surf->texinfo->texture->currentframe;
809 alpha = texture->currentalpha;
810 if (texture->rendertype == SURFRENDER_ADD)
812 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
815 else if (texture->rendertype == SURFRENDER_ALPHA)
817 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
822 GL_BlendFunc(GL_ONE, GL_ZERO);
825 if (gl_textureshader && r_watershader.integer)
827 m.tex[0] = R_GetTexture(mod_shared_distorttexture);
828 m.tex[1] = R_GetTexture(texture->skin.base);
831 m.tex[0] = R_GetTexture(texture->skin.base);
832 colorscale = r_colorscale;
833 if (gl_combine.integer)
835 m.texrgbscale[0] = 4;
839 GL_ColorPointer(varray_color4f);
840 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
842 GL_VertexPointer(mesh->vertex3f);
843 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
844 m.pointer_texcoord[1] = mesh->texcoordtexture2f;
845 m.texcombinergb[1] = GL_REPLACE;
846 R_Mesh_State_Texture(&m);
847 f = surf->flags & SURF_DRAWFULLBRIGHT ? 1.0f : ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f);
848 R_FillColors(varray_color4f, mesh->numverts, f, f, f, alpha);
849 if (!(surf->flags & SURF_DRAWFULLBRIGHT || ent->effects & EF_FULLBRIGHT))
851 if (surf->dlightframe == r_framecount)
852 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, mesh->numverts, mesh->vertex3f, varray_color4f, 1);
853 if (surf->flags & SURF_LIGHTMAP)
854 RSurf_AddLightmapToVertexColors_Color4f(mesh->lightmapoffsets, varray_color4f, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
856 RSurf_FogColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, colorscale, mesh->numverts, modelorg);
857 if (gl_textureshader && r_watershader.integer)
859 GL_ActiveTexture (0);
860 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
861 GL_ActiveTexture (1);
862 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
863 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
864 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
865 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
866 qglEnable (GL_TEXTURE_SHADER_NV);
868 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
869 if (gl_textureshader && r_watershader.integer)
871 qglDisable (GL_TEXTURE_SHADER_NV);
872 GL_ActiveTexture (0);
878 memset(&m, 0, sizeof(m));
879 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
882 m.tex[0] = R_GetTexture(texture->skin.fog);
883 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
885 GL_VertexPointer(mesh->vertex3f);
886 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
887 GL_ColorPointer(varray_color4f);
888 R_Mesh_State_Texture(&m);
889 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, r_colorscale, mesh->numverts, modelorg);
890 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
894 if (r_waterscroll.value)
896 Matrix4x4_CreateIdentity(&tempmatrix);
897 R_Mesh_TextureMatrix(0, &tempmatrix);
898 R_Mesh_TextureMatrix(1, &tempmatrix);
902 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
904 const msurface_t *surf;
907 if (texture->rendertype != SURFRENDER_OPAQUE)
909 for (chain = surfchain;(surf = *chain) != NULL;chain++)
911 if (surf->visframe == r_framecount)
913 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
914 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
919 for (chain = surfchain;(surf = *chain) != NULL;chain++)
920 if (surf->visframe == r_framecount)
921 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
924 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
926 float base, colorscale;
927 const surfmesh_t *mesh;
930 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
931 memset(&m, 0, sizeof(m));
932 if (rendertype == SURFRENDER_ADD)
934 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
937 else if (rendertype == SURFRENDER_ALPHA)
939 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
944 GL_BlendFunc(GL_ONE, GL_ZERO);
947 m.tex[0] = R_GetTexture(texture->skin.base);
948 colorscale = r_colorscale;
949 if (gl_combine.integer)
951 m.texrgbscale[0] = 4;
954 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
956 GL_ColorPointer(varray_color4f);
957 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
959 GL_VertexPointer(mesh->vertex3f);
960 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
961 R_Mesh_State_Texture(&m);
962 R_FillColors(varray_color4f, mesh->numverts, base, base, base, currentalpha);
963 if (!(ent->effects & EF_FULLBRIGHT))
965 if (surf->dlightframe == r_framecount)
966 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, mesh->numverts, mesh->vertex3f, varray_color4f, 1);
967 if (surf->flags & SURF_LIGHTMAP)
968 RSurf_AddLightmapToVertexColors_Color4f(mesh->lightmapoffsets, varray_color4f, mesh->numverts, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
970 RSurf_FogColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, colorscale, mesh->numverts, modelorg);
971 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
975 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
977 const surfmesh_t *mesh;
980 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
981 memset(&m, 0, sizeof(m));
982 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
985 m.tex[0] = R_GetTexture(texture->skin.glow);
986 GL_ColorPointer(varray_color4f);
987 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
989 GL_VertexPointer(mesh->vertex3f);
991 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
992 R_Mesh_State_Texture(&m);
993 RSurf_FoggedColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, 1, 1, 1, currentalpha, r_colorscale, mesh->numverts, modelorg);
994 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
998 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
1000 const surfmesh_t *mesh;
1003 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1004 memset(&m, 0, sizeof(m));
1005 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1006 GL_DepthMask(false);
1008 m.tex[0] = R_GetTexture(texture->skin.fog);
1009 GL_ColorPointer(varray_color4f);
1010 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1012 GL_VertexPointer(mesh->vertex3f);
1014 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1015 R_Mesh_State_Texture(&m);
1016 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, r_colorscale, mesh->numverts, modelorg);
1017 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1021 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1023 const msurface_t *surf;
1024 const surfmesh_t *mesh;
1026 int lightmaptexturenum;
1028 memset(&m, 0, sizeof(m));
1029 GL_BlendFunc(GL_ONE, GL_ZERO);
1032 m.tex[0] = R_GetTexture(texture->skin.base);
1033 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1034 m.tex[2] = R_GetTexture(texture->skin.detail);
1035 m.texrgbscale[0] = 1;
1036 m.texrgbscale[1] = 4;
1037 m.texrgbscale[2] = 2;
1038 cl = (float) (1 << r_lightmapscalebit) * r_colorscale;
1039 GL_Color(cl, cl, cl, 1);
1041 while((surf = *surfchain++) != NULL)
1043 if (surf->visframe == r_framecount)
1045 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1046 //if (m.tex[1] != lightmaptexturenum)
1048 m.tex[1] = lightmaptexturenum;
1049 // R_Mesh_State_Texture(&m);
1051 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1053 GL_VertexPointer(mesh->vertex3f);
1054 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1055 m.pointer_texcoord[1] = mesh->texcoordlightmap2f;
1056 m.pointer_texcoord[2] = mesh->texcoorddetail2f;
1057 R_Mesh_State_Texture(&m);
1058 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1064 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTex(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1066 const msurface_t *surf;
1067 const surfmesh_t *mesh;
1069 int lightmaptexturenum;
1070 memset(&m, 0, sizeof(m));
1071 GL_BlendFunc(GL_ONE, GL_ZERO);
1074 m.tex[0] = R_GetTexture(texture->skin.base);
1075 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1076 if (gl_combine.integer)
1077 m.texrgbscale[1] = 4;
1078 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1079 while((surf = *surfchain++) != NULL)
1081 if (surf->visframe == r_framecount)
1083 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1084 //if (m.tex[1] != lightmaptexturenum)
1086 m.tex[1] = lightmaptexturenum;
1087 // R_Mesh_State_Texture(&m);
1089 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1091 GL_VertexPointer(mesh->vertex3f);
1092 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1093 m.pointer_texcoord[1] = mesh->texcoordlightmap2f;
1094 R_Mesh_State_Texture(&m);
1095 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1101 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1103 const msurface_t *surf;
1104 const surfmesh_t *mesh;
1106 memset(&m, 0, sizeof(m));
1109 GL_BlendFunc(GL_ONE, GL_ZERO);
1110 m.tex[0] = R_GetTexture(texture->skin.base);
1111 GL_Color(1, 1, 1, 1);
1112 while((surf = *surfchain++) != NULL)
1114 if (surf->visframe == r_framecount)
1116 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1118 GL_VertexPointer(mesh->vertex3f);
1119 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1120 R_Mesh_State_Texture(&m);
1121 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1127 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1129 const msurface_t *surf;
1130 const surfmesh_t *mesh;
1132 int lightmaptexturenum;
1133 memset(&m, 0, sizeof(m));
1134 GL_BlendFunc(GL_ZERO, GL_SRC_COLOR);
1135 GL_DepthMask(false);
1137 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1138 if (gl_combine.integer)
1139 m.texrgbscale[0] = 4;
1140 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1141 while((surf = *surfchain++) != NULL)
1143 if (surf->visframe == r_framecount)
1145 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1146 //if (m.tex[0] != lightmaptexturenum)
1148 m.tex[0] = lightmaptexturenum;
1149 // R_Mesh_State_Texture(&m);
1151 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1153 GL_VertexPointer(mesh->vertex3f);
1154 m.pointer_texcoord[0] = mesh->texcoordlightmap2f;
1155 R_Mesh_State_Texture(&m);
1156 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1162 static void RSurfShader_OpaqueWall_Pass_Light(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1164 const msurface_t *surf;
1165 const surfmesh_t *mesh;
1169 memset(&m, 0, sizeof(m));
1170 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1171 GL_DepthMask(false);
1173 m.tex[0] = R_GetTexture(texture->skin.base);
1174 colorscale = r_colorscale;
1175 if (gl_combine.integer)
1177 m.texrgbscale[0] = 4;
1178 colorscale *= 0.25f;
1180 GL_ColorPointer(varray_color4f);
1181 while((surf = *surfchain++) != NULL)
1183 if (surf->visframe == r_framecount && surf->dlightframe == r_framecount)
1185 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1187 if (RSurf_LightCheck(&ent->inversematrix, surf->dlightbits, mesh))
1189 GL_VertexPointer(mesh->vertex3f);
1190 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1191 R_FillColors(varray_color4f, mesh->numverts, 0, 0, 0, 1);
1192 R_Mesh_State_Texture(&m);
1193 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, mesh->numverts, mesh->vertex3f, varray_color4f, colorscale);
1194 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1201 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1203 const msurface_t *surf;
1204 const surfmesh_t *mesh;
1207 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1208 memset(&m, 0, sizeof(m));
1209 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1210 GL_DepthMask(false);
1212 GL_ColorPointer(varray_color4f);
1213 while((surf = *surfchain++) != NULL)
1215 if (surf->visframe == r_framecount)
1217 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1219 GL_VertexPointer(mesh->vertex3f);
1221 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1222 R_Mesh_State_Texture(&m);
1223 RSurf_FogPassColors_Vertex3f_Color4f(mesh->vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, r_colorscale, mesh->numverts, modelorg);
1224 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1230 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1232 const msurface_t *surf;
1233 const surfmesh_t *mesh;
1235 memset(&m, 0, sizeof(m));
1236 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1237 GL_DepthMask(false);
1239 m.tex[0] = R_GetTexture(texture->skin.detail);
1240 GL_Color(1, 1, 1, 1);
1241 while((surf = *surfchain++) != NULL)
1243 if (surf->visframe == r_framecount)
1245 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1247 GL_VertexPointer(mesh->vertex3f);
1248 m.pointer_texcoord[0] = mesh->texcoorddetail2f;
1249 R_Mesh_State_Texture(&m);
1250 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1256 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1258 const msurface_t *surf;
1259 const surfmesh_t *mesh;
1261 memset(&m, 0, sizeof(m));
1262 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1263 GL_DepthMask(false);
1265 m.tex[0] = R_GetTexture(texture->skin.glow);
1266 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1267 while((surf = *surfchain++) != NULL)
1269 if (surf->visframe == r_framecount)
1271 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1273 GL_VertexPointer(mesh->vertex3f);
1274 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1275 R_Mesh_State_Texture(&m);
1276 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1282 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1284 const msurface_t *surf;
1285 const surfmesh_t *mesh;
1287 memset(&m, 0, sizeof(m));
1288 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1290 m.tex[0] = R_GetTexture(texture->skin.glow);
1292 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1294 GL_Color(0, 0, 0, 1);
1295 while((surf = *surfchain++) != NULL)
1297 if (surf->visframe == r_framecount)
1299 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1301 GL_VertexPointer(mesh->vertex3f);
1302 m.pointer_texcoord[0] = mesh->texcoordtexture2f;
1303 R_Mesh_State_Texture(&m);
1304 R_Mesh_Draw(mesh->numverts, mesh->numtriangles, mesh->element3i);
1310 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1312 const entity_render_t *ent = calldata1;
1313 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1317 R_Mesh_Matrix(&ent->matrix);
1319 texture = surf->texinfo->texture;
1320 if (texture->animated)
1321 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];
1323 currentalpha = ent->alpha;
1324 if (texture->flags & SURF_WATERALPHA)
1325 currentalpha *= r_wateralpha.value;
1326 if (ent->effects & EF_ADDITIVE)
1327 rendertype = SURFRENDER_ADD;
1328 else if (currentalpha < 1 || texture->skin.fog != NULL)
1329 rendertype = SURFRENDER_ALPHA;
1331 rendertype = SURFRENDER_OPAQUE;
1333 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1334 if (texture->skin.glow)
1335 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1337 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1340 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1342 const msurface_t *surf;
1345 if (texture->rendertype != SURFRENDER_OPAQUE)
1347 // transparent vertex shaded from lightmap
1348 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1350 if (surf->visframe == r_framecount)
1352 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1353 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1357 else if (r_shadow_realtime_world.integer)
1359 // opaque base lighting
1360 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1362 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1364 else if (r_vertexsurfaces.integer)
1366 // opaque vertex shaded from lightmap
1367 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1368 if (surf->visframe == r_framecount)
1369 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, texture->rendertype, texture->currentalpha);
1370 if (texture->skin.glow)
1371 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1372 if (surf->visframe == r_framecount)
1373 RSurfShader_Wall_Pass_Glow(ent, surf, texture, texture->rendertype, texture->currentalpha);
1375 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1376 if (surf->visframe == r_framecount)
1377 RSurfShader_Wall_Pass_Fog(ent, surf, texture, texture->rendertype, texture->currentalpha);
1381 // opaque lightmapped
1382 if (r_textureunits.integer >= 2)
1384 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1385 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1388 RSurfShader_OpaqueWall_Pass_BaseDoubleTex(ent, texture, surfchain);
1389 if (r_detailtextures.integer)
1390 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1395 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1396 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1397 if (r_detailtextures.integer)
1398 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1400 if (!r_dlightmap.integer && !(ent->effects & EF_FULLBRIGHT))
1401 RSurfShader_OpaqueWall_Pass_Light(ent, texture, surfchain);
1402 if (texture->skin.glow)
1403 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1405 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1409 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1410 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1411 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1413 int Cshader_count = 3;
1414 Cshader_t *Cshaders[3] =
1416 &Cshader_wall_lightmap,
1421 void R_UpdateTextureInfo(entity_render_t *ent)
1423 int i, texframe, alttextures;
1429 alttextures = ent->frame != 0;
1430 texframe = (int)(cl.time * 5.0f);
1431 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1433 t = ent->model->brushq1.textures + i;
1434 t->currentalpha = ent->alpha;
1435 if (t->flags & SURF_WATERALPHA)
1436 t->currentalpha *= r_wateralpha.value;
1437 if (ent->effects & EF_ADDITIVE)
1438 t->rendertype = SURFRENDER_ADD;
1439 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1440 t->rendertype = SURFRENDER_ALPHA;
1442 t->rendertype = SURFRENDER_OPAQUE;
1443 // we don't need to set currentframe if t->animated is false because
1444 // it was already set up by the texture loader for non-animating
1446 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1450 void R_PrepareSurfaces(entity_render_t *ent)
1452 int i, numsurfaces, *surfacevisframes;
1454 msurface_t *surf, *surfaces, **surfchain;
1461 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1462 numsurfaces = model->brushq1.nummodelsurfaces;
1463 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1464 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1466 R_UpdateTextureInfo(ent);
1468 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1471 if (model->brushq1.light_ambient != r_ambient.value || model->brushq1.light_scalebit != r_lightmapscalebit)
1473 model->brushq1.light_ambient = r_ambient.value;
1474 model->brushq1.light_scalebit = r_lightmapscalebit;
1475 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1476 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1480 for (i = 0;i < model->brushq1.light_styles;i++)
1482 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1484 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1485 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1486 (**surfchain).cached_dlight = true;
1491 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1493 if (surfacevisframes[i] == r_framecount)
1495 #if !WORLDNODECULLBACKFACES
1496 // mark any backface surfaces as not visible
1497 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1499 if (!(surf->flags & SURF_PLANEBACK))
1500 surfacevisframes[i] = -1;
1504 if ((surf->flags & SURF_PLANEBACK))
1505 surfacevisframes[i] = -1;
1507 if (surfacevisframes[i] == r_framecount)
1511 surf->visframe = r_framecount;
1512 if (surf->cached_dlight && surf->lightmaptexture != NULL && !r_vertexsurfaces.integer)
1513 R_BuildLightMap(ent, surf);
1519 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1523 if (ent->model == NULL)
1525 R_Mesh_Matrix(&ent->matrix);
1526 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1527 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1528 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1531 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1536 const entity_render_t *ent = calldata1;
1537 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1538 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1539 GL_DepthMask(false);
1541 R_Mesh_Matrix(&ent->matrix);
1542 GL_VertexPointer(varray_vertex3f);
1544 memset(&m, 0, sizeof(m));
1545 R_Mesh_State_Texture(&m);
1547 i = portal - ent->model->brushq1.portals;
1548 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f) * r_colorscale,
1549 ((i & 0x0038) >> 3) * (1.0f / 7.0f) * r_colorscale,
1550 ((i & 0x01C0) >> 6) * (1.0f / 7.0f) * r_colorscale,
1552 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1554 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1555 VectorCopy(portal->points[i].position, v);
1558 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1559 VectorCopy(portal->points[i].position, v);
1560 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1563 // LordHavoc: this is just a nice debugging tool, very slow
1564 static void R_DrawPortals(entity_render_t *ent)
1567 mportal_t *portal, *endportal;
1568 float temp[3], center[3], f;
1569 if (ent->model == NULL)
1571 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1573 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1576 for (i = 0;i < portal->numpoints;i++)
1577 VectorAdd(temp, portal->points[i].position, temp);
1578 f = ixtable[portal->numpoints];
1579 VectorScale(temp, f, temp);
1580 Matrix4x4_Transform(&ent->matrix, temp, center);
1581 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1586 void R_PrepareBrushModel(entity_render_t *ent)
1588 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1591 #if WORLDNODECULLBACKFACES
1595 // because bmodels can be reused, we have to decide which things to render
1596 // from scratch every time
1600 #if WORLDNODECULLBACKFACES
1601 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1603 numsurfaces = model->brushq1.nummodelsurfaces;
1604 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1605 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1606 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1607 for (i = 0;i < numsurfaces;i++, surf++)
1609 #if WORLDNODECULLBACKFACES
1610 // mark any backface surfaces as not visible
1611 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1613 if ((surf->flags & SURF_PLANEBACK))
1614 surfacevisframes[i] = r_framecount;
1616 else if (!(surf->flags & SURF_PLANEBACK))
1617 surfacevisframes[i] = r_framecount;
1619 surfacevisframes[i] = r_framecount;
1621 surf->dlightframe = -1;
1623 R_PrepareSurfaces(ent);
1626 void R_SurfaceWorldNode (entity_render_t *ent)
1628 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1634 // equivilant to quake's RecursiveWorldNode but faster and more effective
1638 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1639 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1640 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1642 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1644 if (!R_CullBox (leaf->mins, leaf->maxs))
1647 leaf->visframe = r_framecount;
1651 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1653 surfnum = model->brushq1.pvssurflist[i];
1654 surf = model->brushq1.surfaces + surfnum;
1655 #if WORLDNODECULLBACKFACES
1656 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1658 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1659 surfacevisframes[surfnum] = r_framecount;
1663 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1664 surfacevisframes[surfnum] = r_framecount;
1667 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1668 surfacevisframes[surfnum] = r_framecount;
1673 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1675 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1676 #if WORLDNODECULLBACKFACES
1680 mleaf_t *leaf, *leafstack[8192];
1683 msurface_t *surfaces;
1684 if (ent->model == NULL)
1686 // LordHavoc: portal-passage worldnode with PVS;
1687 // follows portals leading outward from viewleaf, does not venture
1688 // offscreen or into leafs that are not visible, faster than Quake's
1689 // RecursiveWorldNode
1690 surfaces = ent->model->brushq1.surfaces;
1691 surfacevisframes = ent->model->brushq1.surfacevisframes;
1692 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1693 viewleaf->worldnodeframe = r_framecount;
1694 leafstack[0] = viewleaf;
1696 while (leafstackpos)
1699 leaf = leafstack[--leafstackpos];
1700 leaf->visframe = r_framecount;
1701 // draw any surfaces bounding this leaf
1702 if (leaf->nummarksurfaces)
1704 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1706 #if WORLDNODECULLBACKFACES
1708 if (surfacevisframes[n] != r_framecount)
1710 surf = surfaces + n;
1711 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1713 if ((surf->flags & SURF_PLANEBACK))
1714 surfacevisframes[n] = r_framecount;
1718 if (!(surf->flags & SURF_PLANEBACK))
1719 surfacevisframes[n] = r_framecount;
1723 surfacevisframes[*mark++] = r_framecount;
1727 // follow portals into other leafs
1728 for (p = leaf->portals;p;p = p->next)
1730 // LordHavoc: this DotProduct hurts less than a cache miss
1731 // (which is more likely to happen if backflowing through leafs)
1732 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1735 if (leaf->worldnodeframe != r_framecount)
1737 leaf->worldnodeframe = r_framecount;
1738 // FIXME: R_CullBox is absolute, should be done relative
1739 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1740 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1741 leafstack[leafstackpos++] = leaf;
1748 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1750 int j, c, *surfacepvsframes, *mark;
1755 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1757 model->brushq1.pvsframecount++;
1758 model->brushq1.pvsviewleaf = viewleaf;
1759 model->brushq1.pvsviewleafnovis = r_novis.integer;
1760 model->brushq1.pvsleafchain = NULL;
1761 model->brushq1.pvssurflistlength = 0;
1764 surfacepvsframes = model->brushq1.surfacepvsframes;
1765 for (j = 0;j < model->brushq1.visleafs;j++)
1767 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1769 leaf = model->brushq1.leafs + j + 1;
1770 leaf->pvsframe = model->brushq1.pvsframecount;
1771 leaf->pvschain = model->brushq1.pvsleafchain;
1772 model->brushq1.pvsleafchain = leaf;
1773 // mark surfaces bounding this leaf as visible
1774 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1775 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1778 model->brushq1.BuildPVSTextureChains(model);
1783 void R_WorldVisibility(entity_render_t *ent)
1788 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
1789 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1790 R_PVSUpdate(ent, viewleaf);
1795 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1796 R_SurfaceWorldNode (ent);
1798 R_PortalWorldNode (ent, viewleaf);
1801 void R_DrawWorld(entity_render_t *ent)
1803 if (ent->model == NULL)
1805 if (!ent->model->brushq1.numleafs && ent->model->Draw)
1806 ent->model->Draw(ent);
1809 R_PrepareSurfaces(ent);
1810 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1811 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1812 if (r_drawportals.integer)
1817 void R_Model_Brush_DrawSky(entity_render_t *ent)
1819 if (ent->model == NULL)
1821 if (ent != &cl_entities[0].render)
1822 R_PrepareBrushModel(ent);
1823 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1826 void R_Model_Brush_Draw(entity_render_t *ent)
1828 if (ent->model == NULL)
1831 if (ent != &cl_entities[0].render)
1832 R_PrepareBrushModel(ent);
1833 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1836 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1840 float projectdistance, f, temp[3], lightradius2;
1842 if (ent->model == NULL)
1844 R_Mesh_Matrix(&ent->matrix);
1845 lightradius2 = lightradius * lightradius;
1846 R_UpdateTextureInfo(ent);
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 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1863 R_Shadow_Volume(mesh->numverts, mesh->numtriangles, mesh->vertex3f, mesh->element3i, mesh->neighbor3i, relativelightorigin, lightradius, projectdistance);
1869 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)
1875 if (ent->model == NULL)
1877 R_Mesh_Matrix(&ent->matrix);
1878 R_UpdateTextureInfo(ent);
1879 for (surfnum = 0;surfnum < numsurfaces;surfnum++)
1881 surf = surflist[surfnum];
1882 if (surf->visframe == r_framecount)
1884 t = surf->texinfo->texture->currentframe;
1885 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1887 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1889 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);
1890 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);
1897 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)
1902 float f, lightmins[3], lightmaxs[3];
1904 if (ent->model == NULL)
1906 R_Mesh_Matrix(&ent->matrix);
1907 lightmins[0] = relativelightorigin[0] - lightradius;
1908 lightmins[1] = relativelightorigin[1] - lightradius;
1909 lightmins[2] = relativelightorigin[2] - lightradius;
1910 lightmaxs[0] = relativelightorigin[0] + lightradius;
1911 lightmaxs[1] = relativelightorigin[1] + lightradius;
1912 lightmaxs[2] = relativelightorigin[2] + lightradius;
1913 R_UpdateTextureInfo(ent);
1914 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1916 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1918 f = PlaneDiff(relativelightorigin, surf->plane);
1919 if (surf->flags & SURF_PLANEBACK)
1921 if (f >= -0.1 && f < lightradius)
1923 t = surf->texinfo->texture->currentframe;
1924 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1926 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1928 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);
1929 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);
1937 void R_DrawCollisionBrush(colbrushf_t *brush)
1940 i = ((int)brush) / sizeof(colbrushf_t);
1941 GL_Color((i & 31) * (1.0f / 32.0f) * r_colorscale, ((i >> 5) & 31) * (1.0f / 32.0f) * r_colorscale, ((i >> 10) & 31) * (1.0f / 32.0f) * r_colorscale, 0.2f);
1942 GL_VertexPointer(brush->points->v);
1943 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1946 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
1949 if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW) || !face->numtriangles)
1951 face->visframe = r_framecount;
1952 memset(&m, 0, sizeof(m));
1953 GL_BlendFunc(GL_ONE, GL_ZERO);
1956 m.tex[0] = R_GetTexture(face->texture->skin.base);
1957 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1958 if (face->lightmaptexture)
1960 m.tex[1] = R_GetTexture(face->lightmaptexture);
1961 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1962 m.texrgbscale[1] = 2;
1963 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
1967 m.texrgbscale[0] = 2;
1968 GL_ColorPointer(face->data_color4f);
1970 R_Mesh_State_Texture(&m);
1971 GL_VertexPointer(face->data_vertex3f);
1972 R_Mesh_Draw(face->numvertices, face->numtriangles, face->data_element3i);
1976 void R_Q3BSP_DrawSky(entity_render_t *ent)
1981 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
1986 while (node->isnode)
1988 if (R_CullBox(node->mins, node->maxs))
1990 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
1991 node = node->children[1];
1993 if (R_CullBox(node->mins, node->maxs))
1995 leaf = (q3mleaf_t *)node;
1996 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
1998 for (i = 0;i < leaf->numleaffaces;i++)
2000 face = leaf->firstleafface[i];
2001 if (face->markframe != markframe)
2003 face->markframe = markframe;
2004 if (!R_CullBox(face->mins, face->maxs))
2005 R_Q3BSP_DrawFace(ent, face);
2013 void R_Q3BSP_Draw(entity_render_t *ent)
2020 static int markframe = 0;
2021 R_Mesh_Matrix(&ent->matrix);
2023 if (r_drawcollisionbrushes.integer < 2)
2025 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2026 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2027 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2029 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2030 R_Q3BSP_DrawFace(ent, face);
2032 if (r_drawcollisionbrushes.integer >= 1)
2035 memset(&m, 0, sizeof(m));
2036 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2037 GL_DepthMask(false);
2039 R_Mesh_State_Texture(&m);
2040 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2041 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2042 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2047 void R_Q3BSP_DrawFakeShadow(entity_render_t *ent)
2052 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2056 vec3_t modelorg, lightmins, lightmaxs;
2058 float projectdistance;
2059 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2060 if (r_drawcollisionbrushes.integer < 2)
2063 R_Mesh_Matrix(&ent->matrix);
2064 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2065 lightmins[0] = relativelightorigin[0] - lightradius;
2066 lightmins[1] = relativelightorigin[1] - lightradius;
2067 lightmins[2] = relativelightorigin[2] - lightradius;
2068 lightmaxs[0] = relativelightorigin[0] + lightradius;
2069 lightmaxs[1] = relativelightorigin[1] + lightradius;
2070 lightmaxs[2] = relativelightorigin[2] + lightradius;
2071 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2072 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2074 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2075 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2076 R_Shadow_Volume(face->numvertices, face->numtriangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2080 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)
2082 if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW) || !face->numtriangles)
2084 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);
2085 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);
2088 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)
2092 vec3_t modelorg, lightmins, lightmaxs;
2095 //static int markframe = 0;
2096 if (r_drawcollisionbrushes.integer < 2)
2099 R_Mesh_Matrix(&ent->matrix);
2100 Matrix4x4_Transform(&ent->inversematrix, r_origin, modelorg);
2101 lightmins[0] = relativelightorigin[0] - lightradius;
2102 lightmins[1] = relativelightorigin[1] - lightradius;
2103 lightmins[2] = relativelightorigin[2] - lightradius;
2104 lightmaxs[0] = relativelightorigin[0] + lightradius;
2105 lightmaxs[1] = relativelightorigin[1] + lightradius;
2106 lightmaxs[2] = relativelightorigin[2] + lightradius;
2107 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2108 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2110 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2111 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2112 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2116 static void gl_surf_start(void)
2120 static void gl_surf_shutdown(void)
2124 static void gl_surf_newmap(void)
2128 void GL_Surf_Init(void)
2131 dlightdivtable[0] = 4194304;
2132 for (i = 1;i < 32768;i++)
2133 dlightdivtable[i] = 4194304 / (i << 7);
2135 Cvar_RegisterVariable(&r_ambient);
2136 Cvar_RegisterVariable(&r_vertexsurfaces);
2137 Cvar_RegisterVariable(&r_dlightmap);
2138 Cvar_RegisterVariable(&r_drawportals);
2139 Cvar_RegisterVariable(&r_testvis);
2140 Cvar_RegisterVariable(&r_floatbuildlightmap);
2141 Cvar_RegisterVariable(&r_detailtextures);
2142 Cvar_RegisterVariable(&r_surfaceworldnode);
2143 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2145 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);