2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
25 #define MAX_LIGHTMAP_SIZE 256
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
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, 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->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)
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 // the >> 16 shift adjusts down 8 bits to account for the stainmap
291 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
292 // be doubled during rendering to achieve 2x overbright
293 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
294 if (ent->model->brushq1.lightmaprgba)
296 stride = (surf->lightmaptexturestride - smax) * 4;
297 for (i = 0;i < tmax;i++, out += stride)
299 for (j = 0;j < smax;j++)
301 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
302 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
303 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
310 stride = (surf->lightmaptexturestride - smax) * 3;
311 for (i = 0;i < tmax;i++, out += stride)
313 for (j = 0;j < smax;j++)
315 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
316 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
317 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
322 R_UpdateTexture(surf->lightmaptexture, templight);
326 int smax, tmax, i, j, size, size3, maps, stride, l;
328 qbyte *lightmap, *out, *stain;
330 // update cached lighting info
331 surf->cached_dlight = 0;
333 smax = (surf->extents[0]>>4)+1;
334 tmax = (surf->extents[1]>>4)+1;
337 lightmap = surf->samples;
339 // set to full bright if no light data
340 bl = floatblocklights;
341 if (!ent->model->brushq1.lightdata)
344 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
349 for (i = 0;i < size3;i++)
353 memset(bl, 0, size*3*sizeof(float));
355 if (surf->dlightframe == r_framecount)
357 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
358 if (surf->cached_dlight)
362 // add all the lightmaps
365 bl = floatblocklights;
366 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
367 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
368 bl[i] += lightmap[i] * scale;
371 stain = surf->stainsamples;
372 bl = floatblocklights;
374 // this scaling adjusts down 8 bits to account for the stainmap
375 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
376 // be doubled during rendering to achieve 2x overbright
377 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
378 scale = 1.0f / (1 << 16);
379 if (ent->model->brushq1.lightmaprgba)
381 stride = (surf->lightmaptexturestride - smax) * 4;
382 for (i = 0;i < tmax;i++, out += stride)
384 for (j = 0;j < smax;j++)
386 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
387 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
388 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
395 stride = (surf->lightmaptexturestride - smax) * 3;
396 for (i = 0;i < tmax;i++, out += stride)
398 for (j = 0;j < smax;j++)
400 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
407 R_UpdateTexture(surf->lightmaptexture, templight);
411 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
413 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
414 msurface_t *surf, *endsurf;
415 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
419 maxdist = radius * radius;
420 invradius = 1.0f / radius;
423 if (node->contents < 0)
425 ndist = PlaneDiff(origin, node->plane);
428 node = node->children[0];
433 node = node->children[1];
437 dist2 = ndist * ndist;
438 maxdist3 = maxdist - dist2;
440 if (node->plane->type < 3)
442 VectorCopy(origin, impact);
443 impact[node->plane->type] -= ndist;
447 impact[0] = origin[0] - node->plane->normal[0] * ndist;
448 impact[1] = origin[1] - node->plane->normal[1] * ndist;
449 impact[2] = origin[2] - node->plane->normal[2] * ndist;
452 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
454 if (surf->stainsamples)
456 smax = (surf->extents[0] >> 4) + 1;
457 tmax = (surf->extents[1] >> 4) + 1;
459 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
460 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
462 s = bound(0, impacts, smax * 16) - impacts;
463 t = bound(0, impactt, tmax * 16) - impactt;
464 i = s * s + t * t + dist2;
468 // reduce calculations
469 for (s = 0, i = impacts; s < smax; s++, i -= 16)
470 sdtable[s] = i * i + dist2;
472 bl = surf->stainsamples;
477 for (t = 0;t < tmax;t++, i -= 16)
480 // make sure some part of it is visible on this line
483 maxdist2 = maxdist - td;
484 for (s = 0;s < smax;s++)
486 if (sdtable[s] < maxdist2)
488 ratio = lhrandom(0.0f, 1.0f);
489 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
490 if (a >= (1.0f / 64.0f))
494 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
495 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
496 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
506 // force lightmap upload
508 surf->cached_dlight = true;
512 if (node->children[0]->contents >= 0)
514 if (node->children[1]->contents >= 0)
516 R_StainNode(node->children[0], model, origin, radius, fcolor);
517 node = node->children[1];
522 node = node->children[0];
526 else if (node->children[1]->contents >= 0)
528 node = node->children[1];
533 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)
537 entity_render_t *ent;
540 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
545 fcolor[3] = ca1 * (1.0f / 64.0f);
546 fcolor[4] = cr2 - cr1;
547 fcolor[5] = cg2 - cg1;
548 fcolor[6] = cb2 - cb1;
549 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
551 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
553 // look for embedded bmodels
554 for (n = 0;n < cl_num_brushmodel_entities;n++)
556 ent = cl_brushmodel_entities[n];
558 if (model && model->name[0] == '*')
560 Mod_CheckLoaded(model);
561 if (model->brushq1.nodes)
563 Matrix4x4_Transform(&ent->inversematrix, origin, org);
564 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
572 =============================================================
576 =============================================================
579 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
584 if (styles[0] != 255)
586 for (i = 0;i < numverts;i++, c += 4)
588 lm = samples + lightmapoffsets[i];
589 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
590 VectorMA(c, scale, lm, c);
591 if (styles[1] != 255)
594 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
595 VectorMA(c, scale, lm, c);
596 if (styles[2] != 255)
599 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
600 VectorMA(c, scale, lm, c);
601 if (styles[3] != 255)
604 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
605 VectorMA(c, scale, lm, c);
613 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
619 for (i = 0;i < numverts;i++, v += 3, c += 4)
621 VectorSubtract(v, modelorg, diff);
622 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
623 VectorScale(c, f, c);
626 else if (colorscale != 1)
627 for (i = 0;i < numverts;i++, c += 4)
628 VectorScale(c, colorscale, c);
631 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)
640 for (i = 0;i < numverts;i++, v += 3, c += 4)
642 VectorSubtract(v, modelorg, diff);
643 f = 1 - exp(fogdensity/DotProduct(diff, diff));
652 for (i = 0;i < numverts;i++, c += 4)
662 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)
669 for (i = 0;i < numverts;i++, v += 3, c += 4)
671 VectorSubtract(v, modelorg, diff);
672 f = exp(fogdensity/DotProduct(diff, diff));
680 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
685 int i, l, lit = false;
688 for (l = 0;l < r_numdlights;l++)
690 if (dlightbits[l >> 5] & (1 << (l & 31)))
693 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
694 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
696 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
697 if (f < rd->cullradius2)
699 f = ((1.0f / f) - rd->subtract) * scale;
700 VectorMA(c, f, rd->light, c);
709 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
711 const msurface_t *surf;
714 // LordHavoc: HalfLife maps have freaky skypolys...
715 if (ent->model->brush.ishlbsp)
720 skyrendernow = false;
725 R_Mesh_Matrix(&ent->matrix);
727 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
730 // depth-only (masking)
731 qglColorMask(0,0,0,0);
732 // just to make sure that braindead drivers don't draw anything
733 // despite that colormask...
734 GL_BlendFunc(GL_ZERO, GL_ONE);
739 GL_BlendFunc(GL_ONE, GL_ZERO);
744 memset(&m, 0, sizeof(m));
745 R_Mesh_State_Texture(&m);
747 while((surf = *surfchain++) != NULL)
749 if (surf->visframe == r_framecount)
751 GL_VertexPointer(surf->mesh.data_vertex3f);
752 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
755 qglColorMask(1,1,1,1);
758 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
760 const entity_render_t *ent = calldata1;
761 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
766 matrix4x4_t tempmatrix;
767 float args[4] = {0.05f,0,0,0.04f};
769 if (gl_textureshader && r_watershader.value && !fogenabled)
771 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
772 R_Mesh_TextureMatrix(1, &tempmatrix);
773 Matrix4x4_CreateFromQuakeEntity(&tempmatrix, 0, 0, 0, 0, 0, 0, r_watershader.value);
774 R_Mesh_TextureMatrix(0, &tempmatrix);
776 else if (r_waterscroll.value)
778 // scrolling in texture matrix
779 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
780 R_Mesh_TextureMatrix(0, &tempmatrix);
783 R_Mesh_Matrix(&ent->matrix);
784 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
786 memset(&m, 0, sizeof(m));
787 texture = surf->texinfo->texture->currentframe;
788 alpha = texture->currentalpha;
789 if (texture->rendertype == SURFRENDER_ADD)
791 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
794 else if (texture->rendertype == SURFRENDER_ALPHA)
796 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
801 GL_BlendFunc(GL_ONE, GL_ZERO);
804 if (gl_textureshader && r_watershader.value && !fogenabled)
806 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
807 m.tex[1] = R_GetTexture(texture->skin.base);
810 m.tex[0] = R_GetTexture(texture->skin.base);
813 GL_ColorPointer(varray_color4f);
815 GL_Color(1, 1, 1, alpha);
816 if (gl_textureshader && r_watershader.value && !fogenabled)
818 GL_ActiveTexture (0);
819 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
820 GL_ActiveTexture (1);
821 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
822 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
823 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
824 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
825 qglEnable (GL_TEXTURE_SHADER_NV);
828 GL_VertexPointer(surf->mesh.data_vertex3f);
829 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
830 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
831 m.texcombinergb[1] = GL_REPLACE;
832 R_Mesh_State_Texture(&m);
835 R_FillColors(varray_color4f, surf->mesh.num_vertices, 1, 1, 1, alpha);
836 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, surf->mesh.num_vertices, modelorg);
838 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
840 if (gl_textureshader && r_watershader.value && !fogenabled)
842 qglDisable (GL_TEXTURE_SHADER_NV);
843 GL_ActiveTexture (0);
848 memset(&m, 0, sizeof(m));
849 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
852 m.tex[0] = R_GetTexture(texture->skin.fog);
853 GL_VertexPointer(surf->mesh.data_vertex3f);
854 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
855 GL_ColorPointer(varray_color4f);
856 R_Mesh_State_Texture(&m);
857 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, surf->mesh.num_vertices, modelorg);
858 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
861 if ((gl_textureshader && r_watershader.value && !fogenabled) || r_waterscroll.value)
863 Matrix4x4_CreateIdentity(&tempmatrix);
864 R_Mesh_TextureMatrix(0, &tempmatrix);
865 R_Mesh_TextureMatrix(1, &tempmatrix);
869 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
871 const msurface_t *surf;
874 if (texture->rendertype != SURFRENDER_OPAQUE)
876 for (chain = surfchain;(surf = *chain) != NULL;chain++)
878 if (surf->visframe == r_framecount)
880 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
881 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
886 for (chain = surfchain;(surf = *chain) != NULL;chain++)
887 if (surf->visframe == r_framecount)
888 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
891 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
893 float base, colorscale;
896 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
897 memset(&m, 0, sizeof(m));
898 if (rendertype == SURFRENDER_ADD)
900 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
903 else if (rendertype == SURFRENDER_ALPHA)
905 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
910 GL_BlendFunc(GL_ONE, GL_ZERO);
913 m.tex[0] = R_GetTexture(texture->skin.base);
915 if (gl_combine.integer)
917 m.texrgbscale[0] = 4;
920 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
922 GL_ColorPointer(varray_color4f);
924 GL_VertexPointer(surf->mesh.data_vertex3f);
925 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
926 R_Mesh_State_Texture(&m);
927 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
928 if (!(ent->effects & EF_FULLBRIGHT))
930 if (surf->dlightframe == r_framecount)
931 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
932 if (surf->flags & SURF_LIGHTMAP)
933 RSurf_AddLightmapToVertexColors_Color4f(surf->mesh.data_lightmapoffsets, varray_color4f,surf->mesh.num_vertices, surf->samples, ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3, surf->styles);
935 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
936 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
939 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
943 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
944 memset(&m, 0, sizeof(m));
945 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
948 m.tex[0] = R_GetTexture(texture->skin.glow);
949 GL_ColorPointer(varray_color4f);
951 GL_VertexPointer(surf->mesh.data_vertex3f);
953 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
954 R_Mesh_State_Texture(&m);
955 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
956 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
959 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
963 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
964 memset(&m, 0, sizeof(m));
965 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
968 m.tex[0] = R_GetTexture(texture->skin.fog);
969 GL_ColorPointer(varray_color4f);
971 GL_VertexPointer(surf->mesh.data_vertex3f);
973 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
974 R_Mesh_State_Texture(&m);
975 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
976 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
979 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
981 const msurface_t *surf;
983 int lightmaptexturenum;
984 memset(&m, 0, sizeof(m));
985 GL_BlendFunc(GL_ONE, GL_ZERO);
988 m.tex[0] = R_GetTexture(texture->skin.base);
989 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
990 m.texrgbscale[1] = 2;
991 m.tex[2] = R_GetTexture(texture->skin.detail);
992 m.texrgbscale[2] = 2;
993 GL_Color(1, 1, 1, 1);
995 while((surf = *surfchain++) != NULL)
997 if (surf->visframe == r_framecount)
999 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1000 //if (m.tex[1] != lightmaptexturenum)
1002 m.tex[1] = lightmaptexturenum;
1003 // R_Mesh_State_Texture(&m);
1005 GL_VertexPointer(surf->mesh.data_vertex3f);
1006 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1007 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1008 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1009 R_Mesh_State_Texture(&m);
1010 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1015 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1017 const msurface_t *surf;
1019 int lightmaptexturenum;
1020 memset(&m, 0, sizeof(m));
1021 GL_BlendFunc(GL_ONE, GL_ZERO);
1024 m.tex[0] = R_GetTexture(texture->skin.base);
1025 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1026 m.texrgbscale[1] = 2;
1027 GL_Color(1, 1, 1, 1);
1028 while((surf = *surfchain++) != NULL)
1030 if (surf->visframe == r_framecount)
1032 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1033 //if (m.tex[1] != lightmaptexturenum)
1035 m.tex[1] = lightmaptexturenum;
1036 // R_Mesh_State_Texture(&m);
1038 GL_VertexPointer(surf->mesh.data_vertex3f);
1039 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1040 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1041 R_Mesh_State_Texture(&m);
1042 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1047 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1049 const msurface_t *surf;
1051 memset(&m, 0, sizeof(m));
1054 GL_BlendFunc(GL_ONE, GL_ZERO);
1055 m.tex[0] = R_GetTexture(texture->skin.base);
1056 GL_Color(1, 1, 1, 1);
1057 while((surf = *surfchain++) != NULL)
1059 if (surf->visframe == r_framecount)
1061 GL_VertexPointer(surf->mesh.data_vertex3f);
1062 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1063 R_Mesh_State_Texture(&m);
1064 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1069 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1071 const msurface_t *surf;
1073 int lightmaptexturenum;
1074 memset(&m, 0, sizeof(m));
1075 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1076 GL_DepthMask(false);
1078 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1079 GL_Color(1, 1, 1, 1);
1080 while((surf = *surfchain++) != NULL)
1082 if (surf->visframe == r_framecount)
1084 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1085 //if (m.tex[0] != lightmaptexturenum)
1087 m.tex[0] = lightmaptexturenum;
1088 // R_Mesh_State_Texture(&m);
1090 GL_VertexPointer(surf->mesh.data_vertex3f);
1091 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1092 R_Mesh_State_Texture(&m);
1093 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1098 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1100 const msurface_t *surf;
1103 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1104 memset(&m, 0, sizeof(m));
1105 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1106 GL_DepthMask(false);
1108 GL_ColorPointer(varray_color4f);
1109 while((surf = *surfchain++) != NULL)
1111 if (surf->visframe == r_framecount)
1113 GL_VertexPointer(surf->mesh.data_vertex3f);
1115 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1116 R_Mesh_State_Texture(&m);
1117 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1118 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1123 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1125 const msurface_t *surf;
1127 memset(&m, 0, sizeof(m));
1128 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1129 GL_DepthMask(false);
1131 m.tex[0] = R_GetTexture(texture->skin.detail);
1132 GL_Color(1, 1, 1, 1);
1133 while((surf = *surfchain++) != NULL)
1135 if (surf->visframe == r_framecount)
1137 GL_VertexPointer(surf->mesh.data_vertex3f);
1138 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1139 R_Mesh_State_Texture(&m);
1140 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1145 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1147 const msurface_t *surf;
1149 memset(&m, 0, sizeof(m));
1150 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1151 GL_DepthMask(false);
1153 m.tex[0] = R_GetTexture(texture->skin.glow);
1154 GL_Color(1, 1, 1, 1);
1155 while((surf = *surfchain++) != NULL)
1157 if (surf->visframe == r_framecount)
1159 GL_VertexPointer(surf->mesh.data_vertex3f);
1160 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1161 R_Mesh_State_Texture(&m);
1162 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1167 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1169 const msurface_t *surf;
1171 memset(&m, 0, sizeof(m));
1172 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1174 m.tex[0] = R_GetTexture(texture->skin.glow);
1176 GL_Color(1, 1, 1, 1);
1178 GL_Color(0, 0, 0, 1);
1179 while((surf = *surfchain++) != NULL)
1181 if (surf->visframe == r_framecount)
1183 GL_VertexPointer(surf->mesh.data_vertex3f);
1184 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1185 R_Mesh_State_Texture(&m);
1186 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1191 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1193 const entity_render_t *ent = calldata1;
1194 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1198 R_Mesh_Matrix(&ent->matrix);
1200 texture = surf->texinfo->texture;
1201 if (texture->animated)
1202 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];
1204 currentalpha = ent->alpha;
1205 if (ent->effects & EF_ADDITIVE)
1206 rendertype = SURFRENDER_ADD;
1207 else if (currentalpha < 1 || texture->skin.fog != NULL)
1208 rendertype = SURFRENDER_ALPHA;
1210 rendertype = SURFRENDER_OPAQUE;
1212 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1213 if (texture->skin.glow)
1214 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1216 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1219 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1221 const msurface_t *surf;
1224 if (texture->rendertype != SURFRENDER_OPAQUE)
1226 // transparent vertex shaded from lightmap
1227 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1229 if (surf->visframe == r_framecount)
1231 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1232 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1236 else if (ent->effects & EF_FULLBRIGHT)
1238 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1239 if (r_detailtextures.integer)
1240 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1241 if (texture->skin.glow)
1242 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1244 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1246 else if (r_shadow_realtime_world.integer)
1248 // opaque base lighting
1249 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1251 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1255 // opaque lightmapped
1256 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1258 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1259 if (texture->skin.glow)
1260 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1262 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1264 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1266 RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(ent, texture, surfchain);
1267 if (r_detailtextures.integer)
1268 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1269 if (texture->skin.glow)
1270 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1272 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1276 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1277 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1278 if (r_detailtextures.integer)
1279 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1280 if (texture->skin.glow)
1281 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1283 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1288 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1289 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1290 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1292 int Cshader_count = 3;
1293 Cshader_t *Cshaders[3] =
1295 &Cshader_wall_lightmap,
1300 void R_UpdateTextureInfo(entity_render_t *ent)
1302 int i, texframe, alttextures;
1308 alttextures = ent->frame != 0;
1309 texframe = (int)(cl.time * 5.0f);
1310 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1312 t = ent->model->brushq1.textures + i;
1313 t->currentalpha = ent->alpha;
1314 if (t->flags & SURF_WATERALPHA)
1315 t->currentalpha *= r_wateralpha.value;
1316 if (ent->effects & EF_ADDITIVE)
1317 t->rendertype = SURFRENDER_ADD;
1318 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1319 t->rendertype = SURFRENDER_ALPHA;
1321 t->rendertype = SURFRENDER_OPAQUE;
1322 // we don't need to set currentframe if t->animated is false because
1323 // it was already set up by the texture loader for non-animating
1325 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1329 void R_PrepareSurfaces(entity_render_t *ent)
1331 int i, numsurfaces, *surfacevisframes;
1333 msurface_t *surf, *surfaces, **surfchain;
1340 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1341 numsurfaces = model->brushq1.nummodelsurfaces;
1342 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1343 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1345 R_UpdateTextureInfo(ent);
1347 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1350 if (model->brushq1.light_ambient != r_ambient.value)
1352 model->brushq1.light_ambient = r_ambient.value;
1353 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1354 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1358 for (i = 0;i < model->brushq1.light_styles;i++)
1360 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1362 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1363 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1364 (**surfchain).cached_dlight = true;
1369 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1371 if (surfacevisframes[i] == r_framecount)
1373 #if !WORLDNODECULLBACKFACES
1374 // mark any backface surfaces as not visible
1375 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1377 if (!(surf->flags & SURF_PLANEBACK))
1378 surfacevisframes[i] = -1;
1382 if ((surf->flags & SURF_PLANEBACK))
1383 surfacevisframes[i] = -1;
1385 if (surfacevisframes[i] == r_framecount)
1389 surf->visframe = r_framecount;
1390 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1391 R_BuildLightMap(ent, surf);
1397 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1401 if (ent->model == NULL)
1403 R_Mesh_Matrix(&ent->matrix);
1404 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1405 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1406 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1409 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1414 const entity_render_t *ent = calldata1;
1415 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1416 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1417 GL_DepthMask(false);
1419 R_Mesh_Matrix(&ent->matrix);
1420 GL_VertexPointer(varray_vertex3f);
1422 memset(&m, 0, sizeof(m));
1423 R_Mesh_State_Texture(&m);
1425 i = portal - ent->model->brushq1.portals;
1426 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1427 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1428 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1430 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1432 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1433 VectorCopy(portal->points[i].position, v);
1436 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1437 VectorCopy(portal->points[i].position, v);
1438 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1441 // LordHavoc: this is just a nice debugging tool, very slow
1442 static void R_DrawPortals(entity_render_t *ent)
1445 mportal_t *portal, *endportal;
1446 float temp[3], center[3], f;
1447 if (ent->model == NULL)
1449 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1451 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1454 for (i = 0;i < portal->numpoints;i++)
1455 VectorAdd(temp, portal->points[i].position, temp);
1456 f = ixtable[portal->numpoints];
1457 VectorScale(temp, f, temp);
1458 Matrix4x4_Transform(&ent->matrix, temp, center);
1459 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1464 void R_PrepareBrushModel(entity_render_t *ent)
1466 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1469 #if WORLDNODECULLBACKFACES
1473 // because bmodels can be reused, we have to decide which things to render
1474 // from scratch every time
1478 #if WORLDNODECULLBACKFACES
1479 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1481 numsurfaces = model->brushq1.nummodelsurfaces;
1482 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1483 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1484 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1485 for (i = 0;i < numsurfaces;i++, surf++)
1487 #if WORLDNODECULLBACKFACES
1488 // mark any backface surfaces as not visible
1489 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1491 if ((surf->flags & SURF_PLANEBACK))
1492 surfacevisframes[i] = r_framecount;
1494 else if (!(surf->flags & SURF_PLANEBACK))
1495 surfacevisframes[i] = r_framecount;
1497 surfacevisframes[i] = r_framecount;
1499 surf->dlightframe = -1;
1501 R_PrepareSurfaces(ent);
1504 void R_SurfaceWorldNode (entity_render_t *ent)
1506 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1512 // equivilant to quake's RecursiveWorldNode but faster and more effective
1516 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1517 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1518 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1520 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1522 if (!R_CullBox (leaf->mins, leaf->maxs))
1525 leaf->visframe = r_framecount;
1529 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1531 surfnum = model->brushq1.pvssurflist[i];
1532 surf = model->brushq1.surfaces + surfnum;
1533 #if WORLDNODECULLBACKFACES
1534 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1536 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1537 surfacevisframes[surfnum] = r_framecount;
1541 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1542 surfacevisframes[surfnum] = r_framecount;
1545 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1546 surfacevisframes[surfnum] = r_framecount;
1551 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1553 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1554 #if WORLDNODECULLBACKFACES
1558 mleaf_t *leaf, *leafstack[8192];
1561 msurface_t *surfaces;
1562 if (ent->model == NULL)
1564 // LordHavoc: portal-passage worldnode with PVS;
1565 // follows portals leading outward from viewleaf, does not venture
1566 // offscreen or into leafs that are not visible, faster than Quake's
1567 // RecursiveWorldNode
1568 surfaces = ent->model->brushq1.surfaces;
1569 surfacevisframes = ent->model->brushq1.surfacevisframes;
1570 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1571 viewleaf->worldnodeframe = r_framecount;
1572 leafstack[0] = viewleaf;
1574 while (leafstackpos)
1577 leaf = leafstack[--leafstackpos];
1578 leaf->visframe = r_framecount;
1579 // draw any surfaces bounding this leaf
1580 if (leaf->nummarksurfaces)
1582 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1584 #if WORLDNODECULLBACKFACES
1586 if (surfacevisframes[n] != r_framecount)
1588 surf = surfaces + n;
1589 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1591 if ((surf->flags & SURF_PLANEBACK))
1592 surfacevisframes[n] = r_framecount;
1596 if (!(surf->flags & SURF_PLANEBACK))
1597 surfacevisframes[n] = r_framecount;
1601 surfacevisframes[*mark++] = r_framecount;
1605 // follow portals into other leafs
1606 for (p = leaf->portals;p;p = p->next)
1608 // LordHavoc: this DotProduct hurts less than a cache miss
1609 // (which is more likely to happen if backflowing through leafs)
1610 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1613 if (leaf->worldnodeframe != r_framecount)
1615 leaf->worldnodeframe = r_framecount;
1616 // FIXME: R_CullBox is absolute, should be done relative
1617 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1618 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1619 leafstack[leafstackpos++] = leaf;
1626 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1628 int j, c, *surfacepvsframes, *mark;
1633 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1635 model->brushq1.pvsframecount++;
1636 model->brushq1.pvsviewleaf = viewleaf;
1637 model->brushq1.pvsviewleafnovis = r_novis.integer;
1638 model->brushq1.pvsleafchain = NULL;
1639 model->brushq1.pvssurflistlength = 0;
1642 surfacepvsframes = model->brushq1.surfacepvsframes;
1643 for (j = 0;j < model->brushq1.visleafs;j++)
1645 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1647 leaf = model->brushq1.leafs + j + 1;
1648 leaf->pvsframe = model->brushq1.pvsframecount;
1649 leaf->pvschain = model->brushq1.pvsleafchain;
1650 model->brushq1.pvsleafchain = leaf;
1651 // mark surfaces bounding this leaf as visible
1652 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1653 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1656 model->brushq1.BuildPVSTextureChains(model);
1661 void R_WorldVisibility(entity_render_t *ent)
1666 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1667 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1668 R_PVSUpdate(ent, viewleaf);
1673 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1674 R_SurfaceWorldNode (ent);
1676 R_PortalWorldNode (ent, viewleaf);
1679 void R_DrawWorld(entity_render_t *ent)
1681 if (ent->model == NULL)
1683 if (!ent->model->brushq1.numleafs)
1685 if (ent->model->DrawSky)
1686 ent->model->DrawSky(ent);
1687 if (ent->model->Draw)
1688 ent->model->Draw(ent);
1692 R_PrepareSurfaces(ent);
1693 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1694 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1695 if (r_drawportals.integer)
1700 void R_Model_Brush_DrawSky(entity_render_t *ent)
1702 if (ent->model == NULL)
1704 if (ent != &cl_entities[0].render)
1705 R_PrepareBrushModel(ent);
1706 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1709 void R_Model_Brush_Draw(entity_render_t *ent)
1711 if (ent->model == NULL)
1714 if (ent != &cl_entities[0].render)
1715 R_PrepareBrushModel(ent);
1716 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1719 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1723 float projectdistance, f, temp[3], lightradius2;
1724 if (ent->model == NULL)
1726 R_Mesh_Matrix(&ent->matrix);
1727 lightradius2 = lightradius * lightradius;
1728 R_UpdateTextureInfo(ent);
1729 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1730 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1732 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1734 f = PlaneDiff(relativelightorigin, surf->plane);
1735 if (surf->flags & SURF_PLANEBACK)
1737 // draw shadows only for frontfaces and only if they are close
1738 if (f >= 0.1 && f < lightradius)
1740 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1741 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1742 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1743 if (DotProduct(temp, temp) < lightradius2)
1744 R_Shadow_Volume(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_vertex3f, surf->mesh.data_element3i, surf->mesh.data_neighbor3i, relativelightorigin, lightradius, projectdistance);
1750 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)
1755 float f, lightmins[3], lightmaxs[3];
1756 if (ent->model == NULL)
1758 R_Mesh_Matrix(&ent->matrix);
1759 lightmins[0] = relativelightorigin[0] - lightradius;
1760 lightmins[1] = relativelightorigin[1] - lightradius;
1761 lightmins[2] = relativelightorigin[2] - lightradius;
1762 lightmaxs[0] = relativelightorigin[0] + lightradius;
1763 lightmaxs[1] = relativelightorigin[1] + lightradius;
1764 lightmaxs[2] = relativelightorigin[2] + lightradius;
1765 R_UpdateTextureInfo(ent);
1766 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1768 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1770 f = PlaneDiff(relativelightorigin, surf->plane);
1771 if (surf->flags & SURF_PLANEBACK)
1773 if (f >= -0.1 && f < lightradius)
1775 t = surf->texinfo->texture->currentframe;
1776 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1778 R_Shadow_DiffuseLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, NULL);
1779 R_Shadow_SpecularLighting(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i, surf->mesh.data_vertex3f, surf->mesh.data_svector3f, surf->mesh.data_tvector3f, surf->mesh.data_normal3f, surf->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, NULL);
1786 void R_DrawCollisionBrush(colbrushf_t *brush)
1789 i = ((int)brush) / sizeof(colbrushf_t);
1790 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1791 GL_VertexPointer(brush->points->v);
1792 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1795 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1798 if (!face->num_triangles)
1803 skyrendernow = false;
1804 if (skyrendermasked)
1808 R_Mesh_Matrix(&ent->matrix);
1810 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1811 if (skyrendermasked)
1813 // depth-only (masking)
1814 qglColorMask(0,0,0,0);
1815 // just to make sure that braindead drivers don't draw anything
1816 // despite that colormask...
1817 GL_BlendFunc(GL_ZERO, GL_ONE);
1822 GL_BlendFunc(GL_ONE, GL_ZERO);
1827 memset(&m, 0, sizeof(m));
1828 R_Mesh_State_Texture(&m);
1830 GL_VertexPointer(face->data_vertex3f);
1831 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1832 qglColorMask(1,1,1,1);
1835 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
1838 memset(&m, 0, sizeof(m));
1839 GL_BlendFunc(GL_ONE, GL_ZERO);
1842 if (face->texture->skin.glow)
1844 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1845 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1846 GL_Color(1, 1, 1, 1);
1849 GL_Color(0, 0, 0, 1);
1850 R_Mesh_State_Texture(&m);
1851 GL_VertexPointer(face->data_vertex3f);
1852 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1855 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
1858 memset(&m, 0, sizeof(m));
1859 GL_BlendFunc(GL_ONE, GL_ZERO);
1862 m.tex[0] = R_GetTexture(face->texture->skin.base);
1863 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1864 m.tex[1] = R_GetTexture(face->lightmaptexture);
1865 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1866 m.texrgbscale[1] = 2;
1867 GL_Color(1, 1, 1, 1);
1868 R_Mesh_State_Texture(&m);
1869 GL_VertexPointer(face->data_vertex3f);
1870 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1873 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
1876 memset(&m, 0, sizeof(m));
1877 GL_BlendFunc(GL_ONE, GL_ZERO);
1880 m.tex[0] = R_GetTexture(face->texture->skin.base);
1881 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1882 GL_Color(1, 1, 1, 1);
1883 R_Mesh_State_Texture(&m);
1884 GL_VertexPointer(face->data_vertex3f);
1885 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1888 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
1891 memset(&m, 0, sizeof(m));
1892 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1895 m.tex[0] = R_GetTexture(face->lightmaptexture);
1896 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
1897 GL_Color(1, 1, 1, 1);
1898 R_Mesh_State_Texture(&m);
1899 GL_VertexPointer(face->data_vertex3f);
1900 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1903 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
1906 memset(&m, 0, sizeof(m));
1907 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1910 if (face->texture->skin.glow)
1912 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1913 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1914 GL_Color(1, 1, 1, 1);
1917 GL_Color(0, 0, 0, 1);
1918 R_Mesh_State_Texture(&m);
1919 GL_VertexPointer(face->data_vertex3f);
1920 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1923 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
1926 memset(&m, 0, sizeof(m));
1927 GL_BlendFunc(GL_ONE, GL_ZERO);
1930 m.tex[0] = R_GetTexture(face->texture->skin.base);
1931 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1932 if (gl_combine.integer)
1934 m.texrgbscale[0] = 2;
1935 GL_ColorPointer(face->data_color4f);
1940 for (i = 0;i < face->num_vertices;i++)
1942 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
1943 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
1944 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
1945 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
1947 GL_ColorPointer(varray_color4f);
1949 R_Mesh_State_Texture(&m);
1950 GL_VertexPointer(face->data_vertex3f);
1951 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1954 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
1957 memset(&m, 0, sizeof(m));
1958 GL_BlendFunc(GL_ONE, GL_ONE);
1961 m.tex[0] = R_GetTexture(face->texture->skin.base);
1962 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1963 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1964 R_Mesh_State_Texture(&m);
1965 GL_VertexPointer(face->data_vertex3f);
1966 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1969 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1971 const entity_render_t *ent = voident;
1972 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
1974 R_Mesh_Matrix(&ent->matrix);
1975 memset(&m, 0, sizeof(m));
1976 if (ent->effects & EF_ADDITIVE)
1977 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1979 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1980 GL_DepthMask(false);
1982 m.tex[0] = R_GetTexture(face->texture->skin.base);
1983 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1984 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1985 if (gl_combine.integer)
1987 m.texrgbscale[0] = 2;
1988 if (r_textureunits.integer >= 2)
1990 m.tex[1] = R_GetTexture(face->lightmaptexture);
1991 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1992 GL_Color(1, 1, 1, ent->alpha);
1996 if (ent->alpha == 1)
1997 GL_ColorPointer(face->data_color4f);
2001 for (i = 0;i < face->num_vertices;i++)
2003 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2004 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2005 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2006 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2008 GL_ColorPointer(varray_color4f);
2015 for (i = 0;i < face->num_vertices;i++)
2017 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2018 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2019 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2020 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2022 GL_ColorPointer(varray_color4f);
2024 R_Mesh_State_Texture(&m);
2025 GL_VertexPointer(face->data_vertex3f);
2026 qglDisable(GL_CULL_FACE);
2027 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2028 qglEnable(GL_CULL_FACE);
2031 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2033 if (!face->num_triangles)
2035 if (face->texture->surfaceparms)
2037 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2041 face->visframe = r_framecount;
2042 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2044 vec3_t facecenter, center;
2045 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2046 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2047 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2048 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2049 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2052 R_Mesh_Matrix(&ent->matrix);
2053 if (r_shadow_realtime_world.integer)
2054 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2055 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2057 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2058 if (face->texture->skin.glow)
2059 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2061 else if (face->lightmaptexture)
2063 if (r_textureunits.integer >= 2 && gl_combine.integer)
2064 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2067 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2068 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2070 if (face->texture->skin.glow)
2071 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2075 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2076 if (face->texture->skin.glow)
2077 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2079 if (r_ambient.value)
2080 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2083 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2089 if (R_CullBox(node->mins, node->maxs))
2094 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2095 node = node->children[1];
2097 leaf = (q3mleaf_t *)node;
2098 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2101 for (i = 0;i < leaf->numleaffaces;i++)
2102 leaf->firstleafface[i]->markframe = markframe;
2106 // FIXME: num_leafs needs to be recalculated at load time to include only
2107 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2108 // the submodels (which would render the submodels occasionally, as part of
2109 // the world - not good)
2110 void R_Q3BSP_MarkLeafPVS(entity_render_t *ent, qbyte *pvs, int markframe)
2114 for (j = 0, leaf = ent->model->brushq3.data_leafs;j < ent->model->brushq3.num_leafs;j++, leaf++)
2116 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2119 for (i = 0;i < leaf->numleaffaces;i++)
2120 leaf->firstleafface[i]->markframe = markframe;
2125 static int r_q3bsp_framecount = -1;
2127 void R_Q3BSP_DrawSky(entity_render_t *ent)
2134 R_Mesh_Matrix(&ent->matrix);
2136 if (r_drawcollisionbrushes.integer < 2)
2138 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2139 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2141 if (r_q3bsp_framecount != r_framecount)
2143 r_q3bsp_framecount = r_framecount;
2144 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2145 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2147 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2148 if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2149 R_Q3BSP_DrawSkyFace(ent, face);
2152 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2153 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2154 R_Q3BSP_DrawSkyFace(ent, face);
2158 void R_Q3BSP_Draw(entity_render_t *ent)
2165 R_Mesh_Matrix(&ent->matrix);
2167 if (r_drawcollisionbrushes.integer < 2)
2169 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2170 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2172 if (r_q3bsp_framecount != r_framecount)
2174 r_q3bsp_framecount = r_framecount;
2175 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2176 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2178 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2179 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2180 R_Q3BSP_DrawFace(ent, face);
2183 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2184 R_Q3BSP_DrawFace(ent, face);
2186 if (r_drawcollisionbrushes.integer >= 1)
2189 memset(&m, 0, sizeof(m));
2190 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2191 GL_DepthMask(false);
2193 R_Mesh_State_Texture(&m);
2194 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2195 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2196 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2197 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2198 qglPolygonOffset(0, 0);
2202 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2206 vec3_t modelorg, lightmins, lightmaxs;
2208 float projectdistance;
2209 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2210 if (r_drawcollisionbrushes.integer < 2)
2213 R_Mesh_Matrix(&ent->matrix);
2214 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2215 lightmins[0] = relativelightorigin[0] - lightradius;
2216 lightmins[1] = relativelightorigin[1] - lightradius;
2217 lightmins[2] = relativelightorigin[2] - lightradius;
2218 lightmaxs[0] = relativelightorigin[0] + lightradius;
2219 lightmaxs[1] = relativelightorigin[1] + lightradius;
2220 lightmaxs[2] = relativelightorigin[2] + lightradius;
2221 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2222 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2224 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2225 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2226 R_Shadow_Volume(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2230 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)
2232 if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
2234 R_Shadow_DiffuseLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, NULL);
2235 R_Shadow_SpecularLighting(face->num_vertices, face->num_triangles, face->data_element3i, face->data_vertex3f, face->data_svector3f, face->data_tvector3f, face->data_normal3f, face->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, NULL);
2238 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)
2242 vec3_t modelorg, lightmins, lightmaxs;
2245 //static int markframe = 0;
2246 if (r_drawcollisionbrushes.integer < 2)
2249 R_Mesh_Matrix(&ent->matrix);
2250 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2251 lightmins[0] = relativelightorigin[0] - lightradius;
2252 lightmins[1] = relativelightorigin[1] - lightradius;
2253 lightmins[2] = relativelightorigin[2] - lightradius;
2254 lightmaxs[0] = relativelightorigin[0] + lightradius;
2255 lightmaxs[1] = relativelightorigin[1] + lightradius;
2256 lightmaxs[2] = relativelightorigin[2] + lightradius;
2257 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2258 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2260 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2261 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2262 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2266 static void gl_surf_start(void)
2270 static void gl_surf_shutdown(void)
2274 static void gl_surf_newmap(void)
2278 void GL_Surf_Init(void)
2281 dlightdivtable[0] = 4194304;
2282 for (i = 1;i < 32768;i++)
2283 dlightdivtable[i] = 4194304 / (i << 7);
2285 Cvar_RegisterVariable(&r_ambient);
2286 Cvar_RegisterVariable(&r_drawportals);
2287 Cvar_RegisterVariable(&r_testvis);
2288 Cvar_RegisterVariable(&r_floatbuildlightmap);
2289 Cvar_RegisterVariable(&r_detailtextures);
2290 Cvar_RegisterVariable(&r_surfaceworldnode);
2291 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2292 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2294 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);