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_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "-4"};
40 static int dlightdivtable[32768];
42 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
44 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
46 float dist, impact[3], local[3];
50 smax = (surf->extents[0] >> 4) + 1;
51 tmax = (surf->extents[1] >> 4) + 1;
54 for (lnum = 0; lnum < r_numdlights; lnum++)
56 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
57 continue; // not lit by this light
59 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
60 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
62 // for comparisons to minimum acceptable light
63 // compensate for LIGHTOFFSET
64 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
71 if (surf->plane->type < 3)
73 VectorCopy(local, impact);
74 impact[surf->plane->type] -= dist;
78 impact[0] = local[0] - surf->plane->normal[0] * dist;
79 impact[1] = local[1] - surf->plane->normal[1] * dist;
80 impact[2] = local[2] - surf->plane->normal[2] * dist;
83 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
84 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
86 s = bound(0, impacts, smax * 16) - impacts;
87 t = bound(0, impactt, tmax * 16) - impactt;
88 i = s * s + t * t + dist2;
92 // reduce calculations
93 for (s = 0, i = impacts; s < smax; s++, i -= 16)
94 sdtable[s] = i * i + dist2;
96 maxdist3 = maxdist - dist2;
98 // convert to 8.8 blocklights format
99 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
100 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
101 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
102 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
106 for (t = 0;t < tmax;t++, i -= 16)
109 // make sure some part of it is visible on this line
112 maxdist2 = maxdist - td;
113 for (s = 0;s < smax;s++)
115 if (sdtable[s] < maxdist2)
117 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
121 bl[1] += (green * k);
136 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
138 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
139 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
143 smax = (surf->extents[0] >> 4) + 1;
144 tmax = (surf->extents[1] >> 4) + 1;
147 for (lnum = 0; lnum < r_numdlights; lnum++)
149 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
150 continue; // not lit by this light
152 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
153 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
155 // for comparisons to minimum acceptable light
156 // compensate for LIGHTOFFSET
157 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
160 dist2 += LIGHTOFFSET;
161 if (dist2 >= maxdist)
164 if (surf->plane->type < 3)
166 VectorCopy(local, impact);
167 impact[surf->plane->type] -= dist;
171 impact[0] = local[0] - surf->plane->normal[0] * dist;
172 impact[1] = local[1] - surf->plane->normal[1] * dist;
173 impact[2] = local[2] - surf->plane->normal[2] * dist;
176 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
177 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
179 td = bound(0, impacts, smax * 16) - impacts;
180 td1 = bound(0, impactt, tmax * 16) - impactt;
181 td = td * td + td1 * td1 + dist2;
185 // reduce calculations
186 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
187 sdtable[s] = td1 * td1 + dist2;
189 maxdist3 = maxdist - dist2;
191 // convert to 8.8 blocklights format
192 red = r_dlight[lnum].light[0];
193 green = r_dlight[lnum].light[1];
194 blue = r_dlight[lnum].light[2];
195 subtract = r_dlight[lnum].subtract * 32768.0f;
196 bl = floatblocklights;
199 for (t = 0;t < tmax;t++, td1 -= 16.0f)
202 // make sure some part of it is visible on this line
205 maxdist2 = maxdist - td;
206 for (s = 0;s < smax;s++)
208 if (sdtable[s] < maxdist2)
210 k = (32768.0f / (sdtable[s] + td)) - subtract;
230 Combine and scale multiple lightmaps into the 8.8 format in blocklights
233 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
235 if (!r_floatbuildlightmap.integer)
237 int smax, tmax, i, j, size, size3, maps, stride, l;
238 unsigned int *bl, scale;
239 qbyte *lightmap, *out, *stain;
241 // update cached lighting info
242 surf->cached_dlight = 0;
244 smax = (surf->extents[0]>>4)+1;
245 tmax = (surf->extents[1]>>4)+1;
248 lightmap = surf->samples;
250 // set to full bright if no light data
252 if (!ent->model->brushq1.lightdata)
254 for (i = 0;i < size3;i++)
260 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
263 for (i = 0;i < size3;i++)
267 memset(bl, 0, size*3*sizeof(unsigned int));
269 if (surf->dlightframe == r_framecount)
271 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
272 if (surf->cached_dlight)
276 // add all the lightmaps
280 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
281 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
282 bl[i] += lightmap[i] * scale;
286 stain = surf->stainsamples;
289 // the >> 16 shift adjusts down 8 bits to account for the stainmap
290 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
291 // be doubled during rendering to achieve 2x overbright
292 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
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++) >> 16;*out++ = min(l, 255);
301 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
302 l = (*bl++ * *stain++) >> 16;*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++) >> 16;*out++ = min(l, 255);
315 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
316 l = (*bl++ * *stain++) >> 16;*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->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)
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 // this scaling adjusts down 8 bits to account for the stainmap
374 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
375 // be doubled during rendering to achieve 2x overbright
376 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
377 scale = 1.0f / (1 << 16);
378 if (ent->model->brushq1.lightmaprgba)
380 stride = (surf->lightmaptexturestride - smax) * 4;
381 for (i = 0;i < tmax;i++, out += stride)
383 for (j = 0;j < smax;j++)
385 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
386 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
387 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
394 stride = (surf->lightmaptexturestride - smax) * 3;
395 for (i = 0;i < tmax;i++, out += stride)
397 for (j = 0;j < smax;j++)
399 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
400 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
406 R_UpdateTexture(surf->lightmaptexture, templight);
410 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
412 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
413 msurface_t *surf, *endsurf;
414 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
418 maxdist = radius * radius;
419 invradius = 1.0f / radius;
422 if (node->contents < 0)
424 ndist = PlaneDiff(origin, node->plane);
427 node = node->children[0];
432 node = node->children[1];
436 dist2 = ndist * ndist;
437 maxdist3 = maxdist - dist2;
439 if (node->plane->type < 3)
441 VectorCopy(origin, impact);
442 impact[node->plane->type] -= ndist;
446 impact[0] = origin[0] - node->plane->normal[0] * ndist;
447 impact[1] = origin[1] - node->plane->normal[1] * ndist;
448 impact[2] = origin[2] - node->plane->normal[2] * ndist;
451 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
453 if (surf->stainsamples)
455 smax = (surf->extents[0] >> 4) + 1;
456 tmax = (surf->extents[1] >> 4) + 1;
458 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
459 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
461 s = bound(0, impacts, smax * 16) - impacts;
462 t = bound(0, impactt, tmax * 16) - impactt;
463 i = s * s + t * t + dist2;
467 // reduce calculations
468 for (s = 0, i = impacts; s < smax; s++, i -= 16)
469 sdtable[s] = i * i + dist2;
471 bl = surf->stainsamples;
476 for (t = 0;t < tmax;t++, i -= 16)
479 // make sure some part of it is visible on this line
482 maxdist2 = maxdist - td;
483 for (s = 0;s < smax;s++)
485 if (sdtable[s] < maxdist2)
487 ratio = lhrandom(0.0f, 1.0f);
488 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
489 if (a >= (1.0f / 64.0f))
493 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
494 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
495 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
505 // force lightmap upload
507 surf->cached_dlight = true;
511 if (node->children[0]->contents >= 0)
513 if (node->children[1]->contents >= 0)
515 R_StainNode(node->children[0], model, origin, radius, fcolor);
516 node = node->children[1];
521 node = node->children[0];
525 else if (node->children[1]->contents >= 0)
527 node = node->children[1];
532 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)
536 entity_render_t *ent;
539 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
544 fcolor[3] = ca1 * (1.0f / 64.0f);
545 fcolor[4] = cr2 - cr1;
546 fcolor[5] = cg2 - cg1;
547 fcolor[6] = cb2 - cb1;
548 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
550 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
552 // look for embedded bmodels
553 for (n = 0;n < cl_num_brushmodel_entities;n++)
555 ent = cl_brushmodel_entities[n];
557 if (model && model->name[0] == '*')
559 Mod_CheckLoaded(model);
560 if (model->brushq1.nodes)
562 Matrix4x4_Transform(&ent->inversematrix, origin, org);
563 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
571 =============================================================
575 =============================================================
578 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
583 if (styles[0] != 255)
585 for (i = 0;i < numverts;i++, c += 4)
587 lm = samples + lightmapoffsets[i];
588 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
589 VectorMA(c, scale, lm, c);
590 if (styles[1] != 255)
593 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
594 VectorMA(c, scale, lm, c);
595 if (styles[2] != 255)
598 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
599 VectorMA(c, scale, lm, c);
600 if (styles[3] != 255)
603 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
604 VectorMA(c, scale, lm, c);
612 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
618 for (i = 0;i < numverts;i++, v += 3, c += 4)
620 VectorSubtract(v, modelorg, diff);
621 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
622 VectorScale(c, f, c);
625 else if (colorscale != 1)
626 for (i = 0;i < numverts;i++, c += 4)
627 VectorScale(c, colorscale, c);
630 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)
639 for (i = 0;i < numverts;i++, v += 3, c += 4)
641 VectorSubtract(v, modelorg, diff);
642 f = 1 - exp(fogdensity/DotProduct(diff, diff));
651 for (i = 0;i < numverts;i++, c += 4)
661 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)
668 for (i = 0;i < numverts;i++, v += 3, c += 4)
670 VectorSubtract(v, modelorg, diff);
671 f = exp(fogdensity/DotProduct(diff, diff));
679 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
684 int i, l, lit = false;
687 for (l = 0;l < r_numdlights;l++)
689 if (dlightbits[l >> 5] & (1 << (l & 31)))
692 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
693 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
695 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
696 if (f < rd->cullradius2)
698 f = ((1.0f / f) - rd->subtract) * scale;
699 VectorMA(c, f, rd->light, c);
708 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
710 const msurface_t *surf;
713 // LordHavoc: HalfLife maps have freaky skypolys...
714 if (ent->model->brush.ishlbsp)
719 skyrendernow = false;
724 R_Mesh_Matrix(&ent->matrix);
726 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
729 // depth-only (masking)
730 qglColorMask(0,0,0,0);
731 // just to make sure that braindead drivers don't draw anything
732 // despite that colormask...
733 GL_BlendFunc(GL_ZERO, GL_ONE);
738 GL_BlendFunc(GL_ONE, GL_ZERO);
743 memset(&m, 0, sizeof(m));
744 R_Mesh_State_Texture(&m);
746 while((surf = *surfchain++) != NULL)
748 if (surf->visframe == r_framecount)
750 GL_VertexPointer(surf->mesh.data_vertex3f);
751 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
754 qglColorMask(1,1,1,1);
757 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
759 const entity_render_t *ent = calldata1;
760 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
765 matrix4x4_t tempmatrix;
766 float args[4] = {0.05f,0,0,0.04f};
768 if (gl_textureshader && r_watershader.value && !fogenabled)
770 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
771 R_Mesh_TextureMatrix(1, &tempmatrix);
772 Matrix4x4_CreateFromQuakeEntity(&tempmatrix, 0, 0, 0, 0, 0, 0, r_watershader.value);
773 R_Mesh_TextureMatrix(0, &tempmatrix);
775 else if (r_waterscroll.value)
777 // scrolling in texture matrix
778 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
779 R_Mesh_TextureMatrix(0, &tempmatrix);
782 R_Mesh_Matrix(&ent->matrix);
783 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
785 memset(&m, 0, sizeof(m));
786 texture = surf->texinfo->texture->currentframe;
787 alpha = texture->currentalpha;
788 if (texture->rendertype == SURFRENDER_ADD)
790 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
793 else if (texture->rendertype == SURFRENDER_ALPHA)
795 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
800 GL_BlendFunc(GL_ONE, GL_ZERO);
803 if (gl_textureshader && r_watershader.value && !fogenabled)
805 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
806 m.tex[1] = R_GetTexture(texture->skin.base);
809 m.tex[0] = R_GetTexture(texture->skin.base);
812 GL_ColorPointer(varray_color4f);
814 GL_Color(1, 1, 1, alpha);
815 if (gl_textureshader && r_watershader.value && !fogenabled)
817 GL_ActiveTexture (0);
818 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
819 GL_ActiveTexture (1);
820 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
821 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
822 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
823 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
824 qglEnable (GL_TEXTURE_SHADER_NV);
827 GL_VertexPointer(surf->mesh.data_vertex3f);
828 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
829 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
830 m.texcombinergb[1] = GL_REPLACE;
831 R_Mesh_State_Texture(&m);
834 R_FillColors(varray_color4f, surf->mesh.num_vertices, 1, 1, 1, alpha);
835 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, surf->mesh.num_vertices, modelorg);
837 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
839 if (gl_textureshader && r_watershader.value && !fogenabled)
841 qglDisable (GL_TEXTURE_SHADER_NV);
842 GL_ActiveTexture (0);
847 memset(&m, 0, sizeof(m));
848 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
851 m.tex[0] = R_GetTexture(texture->skin.fog);
852 GL_VertexPointer(surf->mesh.data_vertex3f);
853 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
854 GL_ColorPointer(varray_color4f);
855 R_Mesh_State_Texture(&m);
856 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, surf->mesh.num_vertices, modelorg);
857 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
860 if ((gl_textureshader && r_watershader.value && !fogenabled) || r_waterscroll.value)
862 Matrix4x4_CreateIdentity(&tempmatrix);
863 R_Mesh_TextureMatrix(0, &tempmatrix);
864 R_Mesh_TextureMatrix(1, &tempmatrix);
868 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
870 const msurface_t *surf;
873 if (texture->rendertype != SURFRENDER_OPAQUE)
875 for (chain = surfchain;(surf = *chain) != NULL;chain++)
877 if (surf->visframe == r_framecount)
879 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
880 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
885 for (chain = surfchain;(surf = *chain) != NULL;chain++)
886 if (surf->visframe == r_framecount)
887 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
890 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
892 float base, colorscale;
895 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
896 memset(&m, 0, sizeof(m));
897 if (rendertype == SURFRENDER_ADD)
899 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
902 else if (rendertype == SURFRENDER_ALPHA)
904 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
909 GL_BlendFunc(GL_ONE, GL_ZERO);
912 m.tex[0] = R_GetTexture(texture->skin.base);
914 if (gl_combine.integer)
916 m.texrgbscale[0] = 4;
919 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
921 GL_ColorPointer(varray_color4f);
923 GL_VertexPointer(surf->mesh.data_vertex3f);
924 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
925 R_Mesh_State_Texture(&m);
926 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
927 if (!(ent->effects & EF_FULLBRIGHT))
929 if (surf->dlightframe == r_framecount)
930 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
931 if (surf->flags & SURF_LIGHTMAP)
932 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);
934 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
935 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
938 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
942 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
943 memset(&m, 0, sizeof(m));
944 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
947 m.tex[0] = R_GetTexture(texture->skin.glow);
948 GL_ColorPointer(varray_color4f);
950 GL_VertexPointer(surf->mesh.data_vertex3f);
952 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
953 R_Mesh_State_Texture(&m);
954 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
955 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
958 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
962 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
963 memset(&m, 0, sizeof(m));
964 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
967 m.tex[0] = R_GetTexture(texture->skin.fog);
968 GL_ColorPointer(varray_color4f);
970 GL_VertexPointer(surf->mesh.data_vertex3f);
972 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
973 R_Mesh_State_Texture(&m);
974 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
975 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
978 static void RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
980 const msurface_t *surf;
982 int lightmaptexturenum;
983 memset(&m, 0, sizeof(m));
984 GL_BlendFunc(GL_ONE, GL_ZERO);
987 m.tex[0] = R_GetTexture(texture->skin.base);
988 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
989 m.texrgbscale[1] = 2;
990 m.tex[2] = R_GetTexture(texture->skin.detail);
991 m.texrgbscale[2] = 2;
992 GL_Color(1, 1, 1, 1);
994 while((surf = *surfchain++) != NULL)
996 if (surf->visframe == r_framecount)
998 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
999 //if (m.tex[1] != lightmaptexturenum)
1001 m.tex[1] = lightmaptexturenum;
1002 // R_Mesh_State_Texture(&m);
1004 GL_VertexPointer(surf->mesh.data_vertex3f);
1005 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1006 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1007 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1008 R_Mesh_State_Texture(&m);
1009 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1014 static void RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1016 const msurface_t *surf;
1018 int lightmaptexturenum;
1019 memset(&m, 0, sizeof(m));
1020 GL_BlendFunc(GL_ONE, GL_ZERO);
1023 m.tex[0] = R_GetTexture(texture->skin.base);
1024 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1025 m.texrgbscale[1] = 2;
1026 GL_Color(1, 1, 1, 1);
1027 while((surf = *surfchain++) != NULL)
1029 if (surf->visframe == r_framecount)
1031 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1032 //if (m.tex[1] != lightmaptexturenum)
1034 m.tex[1] = lightmaptexturenum;
1035 // R_Mesh_State_Texture(&m);
1037 GL_VertexPointer(surf->mesh.data_vertex3f);
1038 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1039 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1040 R_Mesh_State_Texture(&m);
1041 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1046 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1048 const msurface_t *surf;
1050 memset(&m, 0, sizeof(m));
1053 GL_BlendFunc(GL_ONE, GL_ZERO);
1054 m.tex[0] = R_GetTexture(texture->skin.base);
1055 GL_Color(1, 1, 1, 1);
1056 while((surf = *surfchain++) != NULL)
1058 if (surf->visframe == r_framecount)
1060 GL_VertexPointer(surf->mesh.data_vertex3f);
1061 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1062 R_Mesh_State_Texture(&m);
1063 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1068 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1070 const msurface_t *surf;
1072 int lightmaptexturenum;
1073 memset(&m, 0, sizeof(m));
1074 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1075 GL_DepthMask(false);
1077 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1078 GL_Color(1, 1, 1, 1);
1079 while((surf = *surfchain++) != NULL)
1081 if (surf->visframe == r_framecount)
1083 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1084 //if (m.tex[0] != lightmaptexturenum)
1086 m.tex[0] = lightmaptexturenum;
1087 // R_Mesh_State_Texture(&m);
1089 GL_VertexPointer(surf->mesh.data_vertex3f);
1090 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1091 R_Mesh_State_Texture(&m);
1092 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1097 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1099 const msurface_t *surf;
1102 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1103 memset(&m, 0, sizeof(m));
1104 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1105 GL_DepthMask(false);
1107 GL_ColorPointer(varray_color4f);
1108 while((surf = *surfchain++) != NULL)
1110 if (surf->visframe == r_framecount)
1112 GL_VertexPointer(surf->mesh.data_vertex3f);
1114 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1115 R_Mesh_State_Texture(&m);
1116 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1117 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1122 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1124 const msurface_t *surf;
1126 memset(&m, 0, sizeof(m));
1127 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1128 GL_DepthMask(false);
1130 m.tex[0] = R_GetTexture(texture->skin.detail);
1131 GL_Color(1, 1, 1, 1);
1132 while((surf = *surfchain++) != NULL)
1134 if (surf->visframe == r_framecount)
1136 GL_VertexPointer(surf->mesh.data_vertex3f);
1137 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1138 R_Mesh_State_Texture(&m);
1139 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1144 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1146 const msurface_t *surf;
1148 memset(&m, 0, sizeof(m));
1149 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1150 GL_DepthMask(false);
1152 m.tex[0] = R_GetTexture(texture->skin.glow);
1153 GL_Color(1, 1, 1, 1);
1154 while((surf = *surfchain++) != NULL)
1156 if (surf->visframe == r_framecount)
1158 GL_VertexPointer(surf->mesh.data_vertex3f);
1159 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1160 R_Mesh_State_Texture(&m);
1161 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1166 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1168 const msurface_t *surf;
1170 memset(&m, 0, sizeof(m));
1171 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1173 m.tex[0] = R_GetTexture(texture->skin.glow);
1175 GL_Color(1, 1, 1, 1);
1177 GL_Color(0, 0, 0, 1);
1178 while((surf = *surfchain++) != NULL)
1180 if (surf->visframe == r_framecount)
1182 GL_VertexPointer(surf->mesh.data_vertex3f);
1183 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1184 R_Mesh_State_Texture(&m);
1185 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1190 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1192 const entity_render_t *ent = calldata1;
1193 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1197 R_Mesh_Matrix(&ent->matrix);
1199 texture = surf->texinfo->texture;
1200 if (texture->animated)
1201 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];
1203 currentalpha = ent->alpha;
1204 if (ent->effects & EF_ADDITIVE)
1205 rendertype = SURFRENDER_ADD;
1206 else if (currentalpha < 1 || texture->skin.fog != NULL)
1207 rendertype = SURFRENDER_ALPHA;
1209 rendertype = SURFRENDER_OPAQUE;
1211 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1212 if (texture->skin.glow)
1213 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1215 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1218 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1220 const msurface_t *surf;
1223 if (texture->rendertype != SURFRENDER_OPAQUE)
1225 // transparent vertex shaded from lightmap
1226 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1228 if (surf->visframe == r_framecount)
1230 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1231 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1235 else if (ent->effects & EF_FULLBRIGHT)
1237 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1238 if (r_detailtextures.integer)
1239 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1240 if (texture->skin.glow)
1241 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1243 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1245 else if (r_shadow_realtime_world.integer)
1247 // opaque base lighting
1248 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1250 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1254 // opaque lightmapped
1255 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1257 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1258 if (texture->skin.glow)
1259 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1261 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1263 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1265 RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(ent, texture, surfchain);
1266 if (r_detailtextures.integer)
1267 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1268 if (texture->skin.glow)
1269 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1271 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1275 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1276 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1277 if (r_detailtextures.integer)
1278 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1279 if (texture->skin.glow)
1280 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1282 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1287 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1288 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1289 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1291 int Cshader_count = 3;
1292 Cshader_t *Cshaders[3] =
1294 &Cshader_wall_lightmap,
1299 void R_UpdateTextureInfo(entity_render_t *ent)
1301 int i, texframe, alttextures;
1307 alttextures = ent->frame != 0;
1308 texframe = (int)(cl.time * 5.0f);
1309 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1311 t = ent->model->brushq1.textures + i;
1312 t->currentalpha = ent->alpha;
1313 if (t->flags & SURF_WATERALPHA)
1314 t->currentalpha *= r_wateralpha.value;
1315 if (ent->effects & EF_ADDITIVE)
1316 t->rendertype = SURFRENDER_ADD;
1317 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1318 t->rendertype = SURFRENDER_ALPHA;
1320 t->rendertype = SURFRENDER_OPAQUE;
1321 // we don't need to set currentframe if t->animated is false because
1322 // it was already set up by the texture loader for non-animating
1324 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1328 void R_PrepareSurfaces(entity_render_t *ent)
1330 int i, numsurfaces, *surfacevisframes;
1332 msurface_t *surf, *surfaces, **surfchain;
1339 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1340 numsurfaces = model->brushq1.nummodelsurfaces;
1341 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1342 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1344 R_UpdateTextureInfo(ent);
1346 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1349 if (model->brushq1.light_ambient != r_ambient.value)
1351 model->brushq1.light_ambient = r_ambient.value;
1352 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1353 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1357 for (i = 0;i < model->brushq1.light_styles;i++)
1359 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1361 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1362 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1363 (**surfchain).cached_dlight = true;
1368 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1370 if (surfacevisframes[i] == r_framecount)
1372 #if !WORLDNODECULLBACKFACES
1373 // mark any backface surfaces as not visible
1374 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1376 if (!(surf->flags & SURF_PLANEBACK))
1377 surfacevisframes[i] = -1;
1381 if ((surf->flags & SURF_PLANEBACK))
1382 surfacevisframes[i] = -1;
1384 if (surfacevisframes[i] == r_framecount)
1388 surf->visframe = r_framecount;
1389 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1390 R_BuildLightMap(ent, surf);
1396 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1400 if (ent->model == NULL)
1402 R_Mesh_Matrix(&ent->matrix);
1403 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1404 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1405 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1408 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1413 const entity_render_t *ent = calldata1;
1414 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1415 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1416 GL_DepthMask(false);
1418 R_Mesh_Matrix(&ent->matrix);
1419 GL_VertexPointer(varray_vertex3f);
1421 memset(&m, 0, sizeof(m));
1422 R_Mesh_State_Texture(&m);
1424 i = portal - ent->model->brushq1.portals;
1425 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1426 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1427 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1429 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1431 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1432 VectorCopy(portal->points[i].position, v);
1435 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1436 VectorCopy(portal->points[i].position, v);
1437 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1440 // LordHavoc: this is just a nice debugging tool, very slow
1441 static void R_DrawPortals(entity_render_t *ent)
1444 mportal_t *portal, *endportal;
1445 float temp[3], center[3], f;
1446 if (ent->model == NULL)
1448 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1450 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1453 for (i = 0;i < portal->numpoints;i++)
1454 VectorAdd(temp, portal->points[i].position, temp);
1455 f = ixtable[portal->numpoints];
1456 VectorScale(temp, f, temp);
1457 Matrix4x4_Transform(&ent->matrix, temp, center);
1458 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1463 void R_PrepareBrushModel(entity_render_t *ent)
1465 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1468 #if WORLDNODECULLBACKFACES
1472 // because bmodels can be reused, we have to decide which things to render
1473 // from scratch every time
1477 #if WORLDNODECULLBACKFACES
1478 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1480 numsurfaces = model->brushq1.nummodelsurfaces;
1481 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1482 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1483 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1484 for (i = 0;i < numsurfaces;i++, surf++)
1486 #if WORLDNODECULLBACKFACES
1487 // mark any backface surfaces as not visible
1488 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1490 if ((surf->flags & SURF_PLANEBACK))
1491 surfacevisframes[i] = r_framecount;
1493 else if (!(surf->flags & SURF_PLANEBACK))
1494 surfacevisframes[i] = r_framecount;
1496 surfacevisframes[i] = r_framecount;
1498 surf->dlightframe = -1;
1500 R_PrepareSurfaces(ent);
1503 void R_SurfaceWorldNode (entity_render_t *ent)
1505 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1511 // equivilant to quake's RecursiveWorldNode but faster and more effective
1515 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1516 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1517 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1519 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1521 if (!R_CullBox (leaf->mins, leaf->maxs))
1524 leaf->visframe = r_framecount;
1528 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1530 surfnum = model->brushq1.pvssurflist[i];
1531 surf = model->brushq1.surfaces + surfnum;
1532 #if WORLDNODECULLBACKFACES
1533 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1535 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1536 surfacevisframes[surfnum] = r_framecount;
1540 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1541 surfacevisframes[surfnum] = r_framecount;
1544 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1545 surfacevisframes[surfnum] = r_framecount;
1550 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1552 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1553 #if WORLDNODECULLBACKFACES
1557 mleaf_t *leaf, *leafstack[8192];
1560 msurface_t *surfaces;
1561 if (ent->model == NULL)
1563 // LordHavoc: portal-passage worldnode with PVS;
1564 // follows portals leading outward from viewleaf, does not venture
1565 // offscreen or into leafs that are not visible, faster than Quake's
1566 // RecursiveWorldNode
1567 surfaces = ent->model->brushq1.surfaces;
1568 surfacevisframes = ent->model->brushq1.surfacevisframes;
1569 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1570 viewleaf->worldnodeframe = r_framecount;
1571 leafstack[0] = viewleaf;
1573 while (leafstackpos)
1576 leaf = leafstack[--leafstackpos];
1577 leaf->visframe = r_framecount;
1578 // draw any surfaces bounding this leaf
1579 if (leaf->nummarksurfaces)
1581 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1583 #if WORLDNODECULLBACKFACES
1585 if (surfacevisframes[n] != r_framecount)
1587 surf = surfaces + n;
1588 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1590 if ((surf->flags & SURF_PLANEBACK))
1591 surfacevisframes[n] = r_framecount;
1595 if (!(surf->flags & SURF_PLANEBACK))
1596 surfacevisframes[n] = r_framecount;
1600 surfacevisframes[*mark++] = r_framecount;
1604 // follow portals into other leafs
1605 for (p = leaf->portals;p;p = p->next)
1607 // LordHavoc: this DotProduct hurts less than a cache miss
1608 // (which is more likely to happen if backflowing through leafs)
1609 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1612 if (leaf->worldnodeframe != r_framecount)
1614 leaf->worldnodeframe = r_framecount;
1615 // FIXME: R_CullBox is absolute, should be done relative
1616 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1617 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1618 leafstack[leafstackpos++] = leaf;
1625 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1627 int j, c, *surfacepvsframes, *mark;
1632 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1634 model->brushq1.pvsframecount++;
1635 model->brushq1.pvsviewleaf = viewleaf;
1636 model->brushq1.pvsviewleafnovis = r_novis.integer;
1637 model->brushq1.pvsleafchain = NULL;
1638 model->brushq1.pvssurflistlength = 0;
1641 surfacepvsframes = model->brushq1.surfacepvsframes;
1642 for (j = 0;j < model->brushq1.visleafs;j++)
1644 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1646 leaf = model->brushq1.leafs + j + 1;
1647 leaf->pvsframe = model->brushq1.pvsframecount;
1648 leaf->pvschain = model->brushq1.pvsleafchain;
1649 model->brushq1.pvsleafchain = leaf;
1650 // mark surfaces bounding this leaf as visible
1651 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1652 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1655 model->brushq1.BuildPVSTextureChains(model);
1660 void R_WorldVisibility(entity_render_t *ent)
1665 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1666 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1667 R_PVSUpdate(ent, viewleaf);
1672 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1673 R_SurfaceWorldNode (ent);
1675 R_PortalWorldNode (ent, viewleaf);
1678 void R_DrawWorld(entity_render_t *ent)
1680 if (ent->model == NULL)
1682 if (!ent->model->brushq1.numleafs)
1684 if (ent->model->DrawSky)
1685 ent->model->DrawSky(ent);
1686 if (ent->model->Draw)
1687 ent->model->Draw(ent);
1691 R_PrepareSurfaces(ent);
1692 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1693 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1694 if (r_drawportals.integer)
1699 void R_Model_Brush_DrawSky(entity_render_t *ent)
1701 if (ent->model == NULL)
1703 if (ent != &cl_entities[0].render)
1704 R_PrepareBrushModel(ent);
1705 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1708 void R_Model_Brush_Draw(entity_render_t *ent)
1710 if (ent->model == NULL)
1713 if (ent != &cl_entities[0].render)
1714 R_PrepareBrushModel(ent);
1715 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1718 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1722 float projectdistance, f, temp[3], lightradius2;
1723 if (ent->model == NULL)
1725 R_Mesh_Matrix(&ent->matrix);
1726 lightradius2 = lightradius * lightradius;
1727 R_UpdateTextureInfo(ent);
1728 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1729 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1731 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1733 f = PlaneDiff(relativelightorigin, surf->plane);
1734 if (surf->flags & SURF_PLANEBACK)
1736 // draw shadows only for frontfaces and only if they are close
1737 if (f >= 0.1 && f < lightradius)
1739 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1740 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1741 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1742 if (DotProduct(temp, temp) < lightradius2)
1743 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);
1749 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)
1754 float f, lightmins[3], lightmaxs[3];
1755 if (ent->model == NULL)
1757 R_Mesh_Matrix(&ent->matrix);
1758 lightmins[0] = relativelightorigin[0] - lightradius;
1759 lightmins[1] = relativelightorigin[1] - lightradius;
1760 lightmins[2] = relativelightorigin[2] - lightradius;
1761 lightmaxs[0] = relativelightorigin[0] + lightradius;
1762 lightmaxs[1] = relativelightorigin[1] + lightradius;
1763 lightmaxs[2] = relativelightorigin[2] + lightradius;
1764 R_UpdateTextureInfo(ent);
1765 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1767 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1769 f = PlaneDiff(relativelightorigin, surf->plane);
1770 if (surf->flags & SURF_PLANEBACK)
1772 if (f >= -0.1 && f < lightradius)
1774 t = surf->texinfo->texture->currentframe;
1775 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1777 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);
1778 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);
1785 void R_DrawCollisionBrush(colbrushf_t *brush)
1788 i = ((int)brush) / sizeof(colbrushf_t);
1789 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1790 GL_VertexPointer(brush->points->v);
1791 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1794 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1797 if (!face->num_triangles)
1801 skyrendernow = false;
1802 if (skyrendermasked)
1806 R_Mesh_Matrix(&ent->matrix);
1808 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1809 if (skyrendermasked)
1811 // depth-only (masking)
1812 qglColorMask(0,0,0,0);
1813 // just to make sure that braindead drivers don't draw anything
1814 // despite that colormask...
1815 GL_BlendFunc(GL_ZERO, GL_ONE);
1820 GL_BlendFunc(GL_ONE, GL_ZERO);
1825 memset(&m, 0, sizeof(m));
1826 R_Mesh_State_Texture(&m);
1828 GL_VertexPointer(face->data_vertex3f);
1829 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1830 qglColorMask(1,1,1,1);
1833 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
1836 memset(&m, 0, sizeof(m));
1837 GL_BlendFunc(GL_ONE, GL_ZERO);
1840 if (face->texture->skin.glow)
1842 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1843 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1844 GL_Color(1, 1, 1, 1);
1847 GL_Color(0, 0, 0, 1);
1848 R_Mesh_State_Texture(&m);
1849 GL_VertexPointer(face->data_vertex3f);
1850 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1853 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
1856 memset(&m, 0, sizeof(m));
1857 GL_BlendFunc(GL_ONE, GL_ZERO);
1860 m.tex[0] = R_GetTexture(face->texture->skin.base);
1861 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1862 m.tex[1] = R_GetTexture(face->lightmaptexture);
1863 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1864 m.texrgbscale[1] = 2;
1865 GL_Color(1, 1, 1, 1);
1866 R_Mesh_State_Texture(&m);
1867 GL_VertexPointer(face->data_vertex3f);
1868 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1871 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
1874 memset(&m, 0, sizeof(m));
1875 GL_BlendFunc(GL_ONE, GL_ZERO);
1878 m.tex[0] = R_GetTexture(face->texture->skin.base);
1879 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1880 GL_Color(1, 1, 1, 1);
1881 R_Mesh_State_Texture(&m);
1882 GL_VertexPointer(face->data_vertex3f);
1883 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1886 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
1889 memset(&m, 0, sizeof(m));
1890 GL_BlendFunc(GL_ONE, GL_SRC_COLOR);
1893 m.tex[0] = R_GetTexture(face->lightmaptexture);
1894 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
1895 GL_Color(1, 1, 1, 1);
1896 R_Mesh_State_Texture(&m);
1897 GL_VertexPointer(face->data_vertex3f);
1898 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1901 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
1904 memset(&m, 0, sizeof(m));
1905 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1908 if (face->texture->skin.glow)
1910 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1911 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1912 GL_Color(1, 1, 1, 1);
1915 GL_Color(0, 0, 0, 1);
1916 R_Mesh_State_Texture(&m);
1917 GL_VertexPointer(face->data_vertex3f);
1918 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1921 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
1924 memset(&m, 0, sizeof(m));
1925 GL_BlendFunc(GL_ONE, GL_ZERO);
1928 m.tex[0] = R_GetTexture(face->texture->skin.base);
1929 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1930 if (gl_combine.integer)
1932 m.texrgbscale[0] = 2;
1933 GL_ColorPointer(face->data_color4f);
1938 for (i = 0;i < face->num_vertices;i++)
1940 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
1941 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
1942 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
1943 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
1945 GL_ColorPointer(varray_color4f);
1947 R_Mesh_State_Texture(&m);
1948 GL_VertexPointer(face->data_vertex3f);
1949 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1952 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
1955 memset(&m, 0, sizeof(m));
1956 GL_BlendFunc(GL_ONE, GL_ONE);
1959 m.tex[0] = R_GetTexture(face->texture->skin.base);
1960 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1961 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1962 R_Mesh_State_Texture(&m);
1963 GL_VertexPointer(face->data_vertex3f);
1964 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1967 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1969 const entity_render_t *ent = voident;
1970 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
1972 R_Mesh_Matrix(&ent->matrix);
1973 memset(&m, 0, sizeof(m));
1974 if (ent->effects & EF_ADDITIVE)
1975 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1977 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1978 GL_DepthMask(false);
1980 m.tex[0] = R_GetTexture(face->texture->skin.base);
1981 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1982 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1983 if (gl_combine.integer)
1985 m.texrgbscale[0] = 2;
1986 if (r_textureunits.integer >= 2)
1988 m.tex[1] = R_GetTexture(face->lightmaptexture);
1989 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1990 GL_Color(1, 1, 1, ent->alpha);
1994 if (ent->alpha == 1)
1995 GL_ColorPointer(face->data_color4f);
1999 for (i = 0;i < face->num_vertices;i++)
2001 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2002 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2003 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2004 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2006 GL_ColorPointer(varray_color4f);
2013 for (i = 0;i < face->num_vertices;i++)
2015 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2016 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2017 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2018 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2020 GL_ColorPointer(varray_color4f);
2022 R_Mesh_State_Texture(&m);
2023 GL_VertexPointer(face->data_vertex3f);
2024 qglDisable(GL_CULL_FACE);
2025 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2026 qglEnable(GL_CULL_FACE);
2029 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2031 if (!face->num_triangles)
2033 if (face->texture->surfaceparms)
2035 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2038 face->visframe = r_framecount;
2039 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2041 vec3_t facecenter, center;
2042 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2043 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2044 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2045 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2046 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2049 R_Mesh_Matrix(&ent->matrix);
2050 if (r_shadow_realtime_world.integer)
2051 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2052 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2054 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2055 if (face->texture->skin.glow)
2056 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2058 else if (face->lightmaptexture)
2060 if (r_textureunits.integer >= 2 && gl_combine.integer)
2061 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2064 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2065 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2067 if (face->texture->skin.glow)
2068 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2072 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2073 if (face->texture->skin.glow)
2074 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2076 if (r_ambient.value)
2077 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2080 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2084 while (node->isnode)
2086 if (R_CullBox(node->mins, node->maxs))
2088 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2089 node = node->children[1];
2091 if (R_CullBox(node->mins, node->maxs))
2093 leaf = (q3mleaf_t *)node;
2094 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2095 for (i = 0;i < leaf->numleaffaces;i++)
2096 leaf->firstleafface[i]->markframe = markframe;
2099 static int r_q3bsp_framecount = -1;
2101 void R_Q3BSP_DrawSky(entity_render_t *ent)
2108 R_Mesh_Matrix(&ent->matrix);
2110 if (r_drawcollisionbrushes.integer < 2)
2112 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2113 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2115 if (r_q3bsp_framecount != r_framecount)
2117 r_q3bsp_framecount = r_framecount;
2118 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2120 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2121 if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2122 R_Q3BSP_DrawSkyFace(ent, face);
2125 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2126 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2127 R_Q3BSP_DrawSkyFace(ent, face);
2131 void R_Q3BSP_Draw(entity_render_t *ent)
2138 R_Mesh_Matrix(&ent->matrix);
2140 if (r_drawcollisionbrushes.integer < 2)
2142 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2143 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2145 if (r_q3bsp_framecount != r_framecount)
2147 r_q3bsp_framecount = r_framecount;
2148 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2150 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2151 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2152 R_Q3BSP_DrawFace(ent, face);
2155 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2156 R_Q3BSP_DrawFace(ent, face);
2158 if (r_drawcollisionbrushes.integer >= 1)
2161 memset(&m, 0, sizeof(m));
2162 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2163 GL_DepthMask(false);
2165 R_Mesh_State_Texture(&m);
2166 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2167 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2168 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2172 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2176 vec3_t modelorg, lightmins, lightmaxs;
2178 float projectdistance;
2179 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2180 if (r_drawcollisionbrushes.integer < 2)
2183 R_Mesh_Matrix(&ent->matrix);
2184 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2185 lightmins[0] = relativelightorigin[0] - lightradius;
2186 lightmins[1] = relativelightorigin[1] - lightradius;
2187 lightmins[2] = relativelightorigin[2] - lightradius;
2188 lightmaxs[0] = relativelightorigin[0] + lightradius;
2189 lightmaxs[1] = relativelightorigin[1] + lightradius;
2190 lightmaxs[2] = relativelightorigin[2] + lightradius;
2191 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2192 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2194 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2195 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2196 R_Shadow_Volume(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2200 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)
2202 if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
2204 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);
2205 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);
2208 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)
2212 vec3_t modelorg, lightmins, lightmaxs;
2215 //static int markframe = 0;
2216 if (r_drawcollisionbrushes.integer < 2)
2219 R_Mesh_Matrix(&ent->matrix);
2220 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2221 lightmins[0] = relativelightorigin[0] - lightradius;
2222 lightmins[1] = relativelightorigin[1] - lightradius;
2223 lightmins[2] = relativelightorigin[2] - lightradius;
2224 lightmaxs[0] = relativelightorigin[0] + lightradius;
2225 lightmaxs[1] = relativelightorigin[1] + lightradius;
2226 lightmaxs[2] = relativelightorigin[2] + lightradius;
2227 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2228 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2230 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2231 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2232 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2236 static void gl_surf_start(void)
2240 static void gl_surf_shutdown(void)
2244 static void gl_surf_newmap(void)
2248 void GL_Surf_Init(void)
2251 dlightdivtable[0] = 4194304;
2252 for (i = 1;i < 32768;i++)
2253 dlightdivtable[i] = 4194304 / (i << 7);
2255 Cvar_RegisterVariable(&r_ambient);
2256 Cvar_RegisterVariable(&r_drawportals);
2257 Cvar_RegisterVariable(&r_testvis);
2258 Cvar_RegisterVariable(&r_floatbuildlightmap);
2259 Cvar_RegisterVariable(&r_detailtextures);
2260 Cvar_RegisterVariable(&r_surfaceworldnode);
2261 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2263 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);