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->effects & EF_FULLBRIGHT) || !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->effects & EF_FULLBRIGHT) || !ent->model->brushq1.lightdata)
343 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
348 for (i = 0;i < size3;i++)
352 memset(bl, 0, size*3*sizeof(float));
354 if (surf->dlightframe == r_framecount)
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 (r_shadow_realtime_world.integer)
1237 // opaque base lighting
1238 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1240 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1244 // opaque lightmapped
1245 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1247 RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1248 if (texture->skin.glow)
1249 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1251 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1253 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1255 RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(ent, texture, surfchain);
1256 if (r_detailtextures.integer)
1257 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1258 if (texture->skin.glow)
1259 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1261 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1265 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1266 RSurfShader_OpaqueWall_Pass_BaseLightmap(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);
1277 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1278 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1279 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1281 int Cshader_count = 3;
1282 Cshader_t *Cshaders[3] =
1284 &Cshader_wall_lightmap,
1289 void R_UpdateTextureInfo(entity_render_t *ent)
1291 int i, texframe, alttextures;
1297 alttextures = ent->frame != 0;
1298 texframe = (int)(cl.time * 5.0f);
1299 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1301 t = ent->model->brushq1.textures + i;
1302 t->currentalpha = ent->alpha;
1303 if (t->flags & SURF_WATERALPHA)
1304 t->currentalpha *= r_wateralpha.value;
1305 if (ent->effects & EF_ADDITIVE)
1306 t->rendertype = SURFRENDER_ADD;
1307 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1308 t->rendertype = SURFRENDER_ALPHA;
1310 t->rendertype = SURFRENDER_OPAQUE;
1311 // we don't need to set currentframe if t->animated is false because
1312 // it was already set up by the texture loader for non-animating
1314 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1318 void R_PrepareSurfaces(entity_render_t *ent)
1320 int i, numsurfaces, *surfacevisframes;
1322 msurface_t *surf, *surfaces, **surfchain;
1329 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1330 numsurfaces = model->brushq1.nummodelsurfaces;
1331 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1332 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1334 R_UpdateTextureInfo(ent);
1336 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1339 if (model->brushq1.light_ambient != r_ambient.value)
1341 model->brushq1.light_ambient = r_ambient.value;
1342 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1343 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1347 for (i = 0;i < model->brushq1.light_styles;i++)
1349 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1351 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1352 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1353 (**surfchain).cached_dlight = true;
1358 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1360 if (surfacevisframes[i] == r_framecount)
1362 #if !WORLDNODECULLBACKFACES
1363 // mark any backface surfaces as not visible
1364 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1366 if (!(surf->flags & SURF_PLANEBACK))
1367 surfacevisframes[i] = -1;
1371 if ((surf->flags & SURF_PLANEBACK))
1372 surfacevisframes[i] = -1;
1374 if (surfacevisframes[i] == r_framecount)
1378 surf->visframe = r_framecount;
1379 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1380 R_BuildLightMap(ent, surf);
1386 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1390 if (ent->model == NULL)
1392 R_Mesh_Matrix(&ent->matrix);
1393 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1394 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1395 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1398 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1403 const entity_render_t *ent = calldata1;
1404 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1405 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1406 GL_DepthMask(false);
1408 R_Mesh_Matrix(&ent->matrix);
1409 GL_VertexPointer(varray_vertex3f);
1411 memset(&m, 0, sizeof(m));
1412 R_Mesh_State_Texture(&m);
1414 i = portal - ent->model->brushq1.portals;
1415 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1416 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1417 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1419 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1421 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1422 VectorCopy(portal->points[i].position, v);
1425 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1426 VectorCopy(portal->points[i].position, v);
1427 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1430 // LordHavoc: this is just a nice debugging tool, very slow
1431 static void R_DrawPortals(entity_render_t *ent)
1434 mportal_t *portal, *endportal;
1435 float temp[3], center[3], f;
1436 if (ent->model == NULL)
1438 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1440 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1443 for (i = 0;i < portal->numpoints;i++)
1444 VectorAdd(temp, portal->points[i].position, temp);
1445 f = ixtable[portal->numpoints];
1446 VectorScale(temp, f, temp);
1447 Matrix4x4_Transform(&ent->matrix, temp, center);
1448 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1453 void R_PrepareBrushModel(entity_render_t *ent)
1455 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1458 #if WORLDNODECULLBACKFACES
1462 // because bmodels can be reused, we have to decide which things to render
1463 // from scratch every time
1467 #if WORLDNODECULLBACKFACES
1468 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1470 numsurfaces = model->brushq1.nummodelsurfaces;
1471 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1472 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1473 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1474 for (i = 0;i < numsurfaces;i++, surf++)
1476 #if WORLDNODECULLBACKFACES
1477 // mark any backface surfaces as not visible
1478 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1480 if ((surf->flags & SURF_PLANEBACK))
1481 surfacevisframes[i] = r_framecount;
1483 else if (!(surf->flags & SURF_PLANEBACK))
1484 surfacevisframes[i] = r_framecount;
1486 surfacevisframes[i] = r_framecount;
1488 surf->dlightframe = -1;
1490 R_PrepareSurfaces(ent);
1493 void R_SurfaceWorldNode (entity_render_t *ent)
1495 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1501 // equivilant to quake's RecursiveWorldNode but faster and more effective
1505 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1506 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1507 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1509 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1511 if (!R_CullBox (leaf->mins, leaf->maxs))
1514 leaf->visframe = r_framecount;
1518 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1520 surfnum = model->brushq1.pvssurflist[i];
1521 surf = model->brushq1.surfaces + surfnum;
1522 #if WORLDNODECULLBACKFACES
1523 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1525 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1526 surfacevisframes[surfnum] = r_framecount;
1530 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1531 surfacevisframes[surfnum] = r_framecount;
1534 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1535 surfacevisframes[surfnum] = r_framecount;
1540 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1542 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1543 #if WORLDNODECULLBACKFACES
1547 mleaf_t *leaf, *leafstack[8192];
1550 msurface_t *surfaces;
1551 if (ent->model == NULL)
1553 // LordHavoc: portal-passage worldnode with PVS;
1554 // follows portals leading outward from viewleaf, does not venture
1555 // offscreen or into leafs that are not visible, faster than Quake's
1556 // RecursiveWorldNode
1557 surfaces = ent->model->brushq1.surfaces;
1558 surfacevisframes = ent->model->brushq1.surfacevisframes;
1559 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1560 viewleaf->worldnodeframe = r_framecount;
1561 leafstack[0] = viewleaf;
1563 while (leafstackpos)
1566 leaf = leafstack[--leafstackpos];
1567 leaf->visframe = r_framecount;
1568 // draw any surfaces bounding this leaf
1569 if (leaf->nummarksurfaces)
1571 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1573 #if WORLDNODECULLBACKFACES
1575 if (surfacevisframes[n] != r_framecount)
1577 surf = surfaces + n;
1578 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1580 if ((surf->flags & SURF_PLANEBACK))
1581 surfacevisframes[n] = r_framecount;
1585 if (!(surf->flags & SURF_PLANEBACK))
1586 surfacevisframes[n] = r_framecount;
1590 surfacevisframes[*mark++] = r_framecount;
1594 // follow portals into other leafs
1595 for (p = leaf->portals;p;p = p->next)
1597 // LordHavoc: this DotProduct hurts less than a cache miss
1598 // (which is more likely to happen if backflowing through leafs)
1599 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1602 if (leaf->worldnodeframe != r_framecount)
1604 leaf->worldnodeframe = r_framecount;
1605 // FIXME: R_CullBox is absolute, should be done relative
1606 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1607 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1608 leafstack[leafstackpos++] = leaf;
1615 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1617 int j, c, *surfacepvsframes, *mark;
1622 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1624 model->brushq1.pvsframecount++;
1625 model->brushq1.pvsviewleaf = viewleaf;
1626 model->brushq1.pvsviewleafnovis = r_novis.integer;
1627 model->brushq1.pvsleafchain = NULL;
1628 model->brushq1.pvssurflistlength = 0;
1631 surfacepvsframes = model->brushq1.surfacepvsframes;
1632 for (j = 0;j < model->brushq1.visleafs;j++)
1634 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1636 leaf = model->brushq1.leafs + j + 1;
1637 leaf->pvsframe = model->brushq1.pvsframecount;
1638 leaf->pvschain = model->brushq1.pvsleafchain;
1639 model->brushq1.pvsleafchain = leaf;
1640 // mark surfaces bounding this leaf as visible
1641 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1642 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1645 model->brushq1.BuildPVSTextureChains(model);
1650 void R_WorldVisibility(entity_render_t *ent)
1655 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1656 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1657 R_PVSUpdate(ent, viewleaf);
1662 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1663 R_SurfaceWorldNode (ent);
1665 R_PortalWorldNode (ent, viewleaf);
1668 void R_DrawWorld(entity_render_t *ent)
1670 if (ent->model == NULL)
1672 if (!ent->model->brushq1.numleafs)
1674 if (ent->model->DrawSky)
1675 ent->model->DrawSky(ent);
1676 if (ent->model->Draw)
1677 ent->model->Draw(ent);
1681 R_PrepareSurfaces(ent);
1682 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1683 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1684 if (r_drawportals.integer)
1689 void R_Model_Brush_DrawSky(entity_render_t *ent)
1691 if (ent->model == NULL)
1693 if (ent != &cl_entities[0].render)
1694 R_PrepareBrushModel(ent);
1695 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1698 void R_Model_Brush_Draw(entity_render_t *ent)
1700 if (ent->model == NULL)
1703 if (ent != &cl_entities[0].render)
1704 R_PrepareBrushModel(ent);
1705 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1708 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1712 float projectdistance, f, temp[3], lightradius2;
1713 if (ent->model == NULL)
1715 R_Mesh_Matrix(&ent->matrix);
1716 lightradius2 = lightradius * lightradius;
1717 R_UpdateTextureInfo(ent);
1718 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1719 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1721 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1723 f = PlaneDiff(relativelightorigin, surf->plane);
1724 if (surf->flags & SURF_PLANEBACK)
1726 // draw shadows only for frontfaces and only if they are close
1727 if (f >= 0.1 && f < lightradius)
1729 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1730 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1731 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1732 if (DotProduct(temp, temp) < lightradius2)
1733 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);
1739 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)
1744 float f, lightmins[3], lightmaxs[3];
1745 if (ent->model == NULL)
1747 R_Mesh_Matrix(&ent->matrix);
1748 lightmins[0] = relativelightorigin[0] - lightradius;
1749 lightmins[1] = relativelightorigin[1] - lightradius;
1750 lightmins[2] = relativelightorigin[2] - lightradius;
1751 lightmaxs[0] = relativelightorigin[0] + lightradius;
1752 lightmaxs[1] = relativelightorigin[1] + lightradius;
1753 lightmaxs[2] = relativelightorigin[2] + lightradius;
1754 R_UpdateTextureInfo(ent);
1755 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1757 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1759 f = PlaneDiff(relativelightorigin, surf->plane);
1760 if (surf->flags & SURF_PLANEBACK)
1762 if (f >= -0.1 && f < lightradius)
1764 t = surf->texinfo->texture->currentframe;
1765 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1767 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);
1768 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);
1775 void R_DrawCollisionBrush(colbrushf_t *brush)
1778 i = ((int)brush) / sizeof(colbrushf_t);
1779 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1780 GL_VertexPointer(brush->points->v);
1781 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1784 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1787 if (!face->num_triangles)
1791 skyrendernow = false;
1792 if (skyrendermasked)
1796 R_Mesh_Matrix(&ent->matrix);
1798 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1799 if (skyrendermasked)
1801 // depth-only (masking)
1802 qglColorMask(0,0,0,0);
1803 // just to make sure that braindead drivers don't draw anything
1804 // despite that colormask...
1805 GL_BlendFunc(GL_ZERO, GL_ONE);
1810 GL_BlendFunc(GL_ONE, GL_ZERO);
1815 memset(&m, 0, sizeof(m));
1816 R_Mesh_State_Texture(&m);
1818 GL_VertexPointer(face->data_vertex3f);
1819 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1820 qglColorMask(1,1,1,1);
1823 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
1826 memset(&m, 0, sizeof(m));
1827 GL_BlendFunc(GL_ONE, GL_ZERO);
1830 if (face->texture->skin.glow)
1832 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1833 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1834 GL_Color(1, 1, 1, 1);
1837 GL_Color(0, 0, 0, 1);
1838 R_Mesh_State_Texture(&m);
1839 GL_VertexPointer(face->data_vertex3f);
1840 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1843 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
1846 memset(&m, 0, sizeof(m));
1847 GL_BlendFunc(GL_ONE, GL_ZERO);
1850 m.tex[0] = R_GetTexture(face->texture->skin.base);
1851 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1852 m.tex[1] = R_GetTexture(face->lightmaptexture);
1853 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1854 m.texrgbscale[1] = 2;
1855 GL_Color(1, 1, 1, 1);
1856 R_Mesh_State_Texture(&m);
1857 GL_VertexPointer(face->data_vertex3f);
1858 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1861 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
1864 memset(&m, 0, sizeof(m));
1865 GL_BlendFunc(GL_ONE, GL_ZERO);
1868 m.tex[0] = R_GetTexture(face->texture->skin.base);
1869 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1870 GL_Color(1, 1, 1, 1);
1871 R_Mesh_State_Texture(&m);
1872 GL_VertexPointer(face->data_vertex3f);
1873 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1876 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
1879 memset(&m, 0, sizeof(m));
1880 GL_BlendFunc(GL_ONE, GL_SRC_COLOR);
1883 m.tex[0] = R_GetTexture(face->lightmaptexture);
1884 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
1885 GL_Color(1, 1, 1, 1);
1886 R_Mesh_State_Texture(&m);
1887 GL_VertexPointer(face->data_vertex3f);
1888 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1891 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
1894 memset(&m, 0, sizeof(m));
1895 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1898 if (face->texture->skin.glow)
1900 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1901 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1902 GL_Color(1, 1, 1, 1);
1905 GL_Color(0, 0, 0, 1);
1906 R_Mesh_State_Texture(&m);
1907 GL_VertexPointer(face->data_vertex3f);
1908 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1911 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
1914 memset(&m, 0, sizeof(m));
1915 GL_BlendFunc(GL_ONE, GL_ZERO);
1918 m.tex[0] = R_GetTexture(face->texture->skin.base);
1919 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1920 if (gl_combine.integer)
1922 m.texrgbscale[0] = 2;
1923 GL_ColorPointer(face->data_color4f);
1928 for (i = 0;i < face->num_vertices;i++)
1930 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
1931 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
1932 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
1933 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
1935 GL_ColorPointer(varray_color4f);
1937 R_Mesh_State_Texture(&m);
1938 GL_VertexPointer(face->data_vertex3f);
1939 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1942 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
1945 memset(&m, 0, sizeof(m));
1946 GL_BlendFunc(GL_ONE, GL_ONE);
1949 m.tex[0] = R_GetTexture(face->texture->skin.base);
1950 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1951 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1952 R_Mesh_State_Texture(&m);
1953 GL_VertexPointer(face->data_vertex3f);
1954 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1957 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1959 const entity_render_t *ent = voident;
1960 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
1962 R_Mesh_Matrix(&ent->matrix);
1963 memset(&m, 0, sizeof(m));
1964 if (ent->effects & EF_ADDITIVE)
1965 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1967 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1968 GL_DepthMask(false);
1970 m.tex[0] = R_GetTexture(face->texture->skin.base);
1971 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1972 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1973 if (gl_combine.integer)
1975 m.texrgbscale[0] = 2;
1976 if (r_textureunits.integer >= 2)
1978 m.tex[1] = R_GetTexture(face->lightmaptexture);
1979 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1980 GL_Color(1, 1, 1, ent->alpha);
1984 if (ent->alpha == 1)
1985 GL_ColorPointer(face->data_color4f);
1989 for (i = 0;i < face->num_vertices;i++)
1991 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
1992 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
1993 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
1994 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
1996 GL_ColorPointer(varray_color4f);
2003 for (i = 0;i < face->num_vertices;i++)
2005 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2006 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2007 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2008 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2010 GL_ColorPointer(varray_color4f);
2012 R_Mesh_State_Texture(&m);
2013 GL_VertexPointer(face->data_vertex3f);
2014 qglDisable(GL_CULL_FACE);
2015 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2016 qglEnable(GL_CULL_FACE);
2019 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2021 if (!face->num_triangles)
2023 if (face->texture->surfaceparms)
2025 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2028 face->visframe = r_framecount;
2029 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2031 vec3_t facecenter, center;
2032 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2033 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2034 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2035 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2036 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2039 R_Mesh_Matrix(&ent->matrix);
2040 if (r_shadow_realtime_world.integer)
2041 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2042 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2044 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2045 if (face->texture->skin.glow)
2046 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2048 else if (face->lightmaptexture)
2050 if (r_textureunits.integer >= 2 && gl_combine.integer)
2051 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2054 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2055 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2057 if (face->texture->skin.glow)
2058 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2062 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2063 if (face->texture->skin.glow)
2064 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2066 if (r_ambient.value)
2067 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2070 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2074 while (node->isnode)
2076 if (R_CullBox(node->mins, node->maxs))
2078 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2079 node = node->children[1];
2081 if (R_CullBox(node->mins, node->maxs))
2083 leaf = (q3mleaf_t *)node;
2084 if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2085 for (i = 0;i < leaf->numleaffaces;i++)
2086 leaf->firstleafface[i]->markframe = markframe;
2089 static int r_q3bsp_framecount = -1;
2091 void R_Q3BSP_DrawSky(entity_render_t *ent)
2098 R_Mesh_Matrix(&ent->matrix);
2100 if (r_drawcollisionbrushes.integer < 2)
2102 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2103 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2105 if (r_q3bsp_framecount != r_framecount)
2107 r_q3bsp_framecount = r_framecount;
2108 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2110 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2111 if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2112 R_Q3BSP_DrawSkyFace(ent, face);
2115 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2116 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2117 R_Q3BSP_DrawSkyFace(ent, face);
2121 void R_Q3BSP_Draw(entity_render_t *ent)
2128 R_Mesh_Matrix(&ent->matrix);
2130 if (r_drawcollisionbrushes.integer < 2)
2132 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2133 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2135 if (r_q3bsp_framecount != r_framecount)
2137 r_q3bsp_framecount = r_framecount;
2138 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2140 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2141 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2142 R_Q3BSP_DrawFace(ent, face);
2145 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2146 R_Q3BSP_DrawFace(ent, face);
2148 if (r_drawcollisionbrushes.integer >= 1)
2151 memset(&m, 0, sizeof(m));
2152 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2153 GL_DepthMask(false);
2155 R_Mesh_State_Texture(&m);
2156 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2157 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2158 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2162 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2166 vec3_t modelorg, lightmins, lightmaxs;
2168 float projectdistance;
2169 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2170 if (r_drawcollisionbrushes.integer < 2)
2173 R_Mesh_Matrix(&ent->matrix);
2174 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2175 lightmins[0] = relativelightorigin[0] - lightradius;
2176 lightmins[1] = relativelightorigin[1] - lightradius;
2177 lightmins[2] = relativelightorigin[2] - lightradius;
2178 lightmaxs[0] = relativelightorigin[0] + lightradius;
2179 lightmaxs[1] = relativelightorigin[1] + lightradius;
2180 lightmaxs[2] = relativelightorigin[2] + lightradius;
2181 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2182 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2184 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2185 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2186 R_Shadow_Volume(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2190 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)
2192 if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
2194 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);
2195 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);
2198 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)
2202 vec3_t modelorg, lightmins, lightmaxs;
2205 //static int markframe = 0;
2206 if (r_drawcollisionbrushes.integer < 2)
2209 R_Mesh_Matrix(&ent->matrix);
2210 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2211 lightmins[0] = relativelightorigin[0] - lightradius;
2212 lightmins[1] = relativelightorigin[1] - lightradius;
2213 lightmins[2] = relativelightorigin[2] - lightradius;
2214 lightmaxs[0] = relativelightorigin[0] + lightradius;
2215 lightmaxs[1] = relativelightorigin[1] + lightradius;
2216 lightmaxs[2] = relativelightorigin[2] + lightradius;
2217 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2218 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2220 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2221 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2222 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2226 static void gl_surf_start(void)
2230 static void gl_surf_shutdown(void)
2234 static void gl_surf_newmap(void)
2238 void GL_Surf_Init(void)
2241 dlightdivtable[0] = 4194304;
2242 for (i = 1;i < 32768;i++)
2243 dlightdivtable[i] = 4194304 / (i << 7);
2245 Cvar_RegisterVariable(&r_ambient);
2246 Cvar_RegisterVariable(&r_drawportals);
2247 Cvar_RegisterVariable(&r_testvis);
2248 Cvar_RegisterVariable(&r_floatbuildlightmap);
2249 Cvar_RegisterVariable(&r_detailtextures);
2250 Cvar_RegisterVariable(&r_surfaceworldnode);
2251 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2253 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);