2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
25 #define MAX_LIGHTMAP_SIZE 256
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
40 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
42 static int dlightdivtable[32768];
44 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
46 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
48 float dist, impact[3], local[3];
53 smax = (surf->extents[0] >> 4) + 1;
54 tmax = (surf->extents[1] >> 4) + 1;
57 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
59 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
60 continue; // not lit by this light
62 Matrix4x4_Transform(matrix, light->origin, local);
63 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
65 // for comparisons to minimum acceptable light
66 // compensate for LIGHTOFFSET
67 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
74 if (surf->plane->type < 3)
76 VectorCopy(local, impact);
77 impact[surf->plane->type] -= dist;
81 impact[0] = local[0] - surf->plane->normal[0] * dist;
82 impact[1] = local[1] - surf->plane->normal[1] * dist;
83 impact[2] = local[2] - surf->plane->normal[2] * dist;
86 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
87 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
89 s = bound(0, impacts, smax * 16) - impacts;
90 t = bound(0, impactt, tmax * 16) - impactt;
91 i = s * s + t * t + dist2;
95 // reduce calculations
96 for (s = 0, i = impacts; s < smax; s++, i -= 16)
97 sdtable[s] = i * i + dist2;
99 maxdist3 = maxdist - dist2;
101 // convert to 8.8 blocklights format
102 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
103 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
104 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
105 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
109 for (t = 0;t < tmax;t++, i -= 16)
112 // make sure some part of it is visible on this line
115 maxdist2 = maxdist - td;
116 for (s = 0;s < smax;s++)
118 if (sdtable[s] < maxdist2)
120 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
124 bl[1] += (green * k);
139 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
141 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
142 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
147 smax = (surf->extents[0] >> 4) + 1;
148 tmax = (surf->extents[1] >> 4) + 1;
151 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
153 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
154 continue; // not lit by this light
156 Matrix4x4_Transform(matrix, light->origin, local);
157 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
159 // for comparisons to minimum acceptable light
160 // compensate for LIGHTOFFSET
161 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
164 dist2 += LIGHTOFFSET;
165 if (dist2 >= maxdist)
168 if (surf->plane->type < 3)
170 VectorCopy(local, impact);
171 impact[surf->plane->type] -= dist;
175 impact[0] = local[0] - surf->plane->normal[0] * dist;
176 impact[1] = local[1] - surf->plane->normal[1] * dist;
177 impact[2] = local[2] - surf->plane->normal[2] * dist;
180 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
181 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
183 td = bound(0, impacts, smax * 16) - impacts;
184 td1 = bound(0, impactt, tmax * 16) - impactt;
185 td = td * td + td1 * td1 + dist2;
189 // reduce calculations
190 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
191 sdtable[s] = td1 * td1 + dist2;
193 maxdist3 = maxdist - dist2;
195 // convert to 8.8 blocklights format
196 red = light->rtlight.lightmap_light[0];
197 green = light->rtlight.lightmap_light[1];
198 blue = light->rtlight.lightmap_light[2];
199 subtract = light->rtlight.lightmap_subtract * 32768.0f;
200 bl = floatblocklights;
203 for (t = 0;t < tmax;t++, td1 -= 16.0f)
206 // make sure some part of it is visible on this line
209 maxdist2 = maxdist - td;
210 for (s = 0;s < smax;s++)
212 if (sdtable[s] < maxdist2)
214 k = (32768.0f / (sdtable[s] + td)) - subtract;
234 Combine and scale multiple lightmaps into the 8.8 format in blocklights
237 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
239 if (!r_floatbuildlightmap.integer)
241 int smax, tmax, i, j, size, size3, maps, stride, l;
242 unsigned int *bl, scale;
243 qbyte *lightmap, *out, *stain;
245 // update cached lighting info
246 surf->cached_dlight = 0;
248 smax = (surf->extents[0]>>4)+1;
249 tmax = (surf->extents[1]>>4)+1;
252 lightmap = surf->samples;
254 // set to full bright if no light data
256 if (!ent->model->brushq1.lightdata)
258 for (i = 0;i < size3;i++)
264 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
267 for (i = 0;i < size3;i++)
271 memset(bl, 0, size*3*sizeof(unsigned int));
273 if (surf->dlightframe == r_framecount)
275 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
276 if (surf->cached_dlight)
280 // add all the lightmaps
284 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
285 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
286 bl[i] += lightmap[i] * scale;
290 stain = surf->stainsamples;
293 // the >> 16 shift adjusts down 8 bits to account for the stainmap
294 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
295 // be doubled during rendering to achieve 2x overbright
296 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
297 if (ent->model->brushq1.lightmaprgba)
299 stride = (surf->lightmaptexturestride - smax) * 4;
300 for (i = 0;i < tmax;i++, out += stride)
302 for (j = 0;j < smax;j++)
304 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
305 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
306 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
313 stride = (surf->lightmaptexturestride - smax) * 3;
314 for (i = 0;i < tmax;i++, out += stride)
316 for (j = 0;j < smax;j++)
318 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
319 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
320 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
325 R_UpdateTexture(surf->lightmaptexture, templight);
329 int smax, tmax, i, j, size, size3, maps, stride, l;
331 qbyte *lightmap, *out, *stain;
333 // update cached lighting info
334 surf->cached_dlight = 0;
336 smax = (surf->extents[0]>>4)+1;
337 tmax = (surf->extents[1]>>4)+1;
340 lightmap = surf->samples;
342 // set to full bright if no light data
343 bl = floatblocklights;
344 if (!ent->model->brushq1.lightdata)
347 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
352 for (i = 0;i < size3;i++)
356 memset(bl, 0, size*3*sizeof(float));
358 if (surf->dlightframe == r_framecount)
360 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
361 if (surf->cached_dlight)
365 // add all the lightmaps
368 bl = floatblocklights;
369 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
370 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
371 bl[i] += lightmap[i] * scale;
374 stain = surf->stainsamples;
375 bl = floatblocklights;
377 // this scaling adjusts down 8 bits to account for the stainmap
378 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
379 // be doubled during rendering to achieve 2x overbright
380 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
381 scale = 1.0f / (1 << 16);
382 if (ent->model->brushq1.lightmaprgba)
384 stride = (surf->lightmaptexturestride - smax) * 4;
385 for (i = 0;i < tmax;i++, out += stride)
387 for (j = 0;j < smax;j++)
389 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
390 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
391 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398 stride = (surf->lightmaptexturestride - smax) * 3;
399 for (i = 0;i < tmax;i++, out += stride)
401 for (j = 0;j < smax;j++)
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
404 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
405 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 R_UpdateTexture(surf->lightmaptexture, templight);
414 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
416 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
417 msurface_t *surf, *endsurf;
418 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
422 maxdist = radius * radius;
423 invradius = 1.0f / radius;
426 if (node->contents < 0)
428 ndist = PlaneDiff(origin, node->plane);
431 node = node->children[0];
436 node = node->children[1];
440 dist2 = ndist * ndist;
441 maxdist3 = maxdist - dist2;
443 if (node->plane->type < 3)
445 VectorCopy(origin, impact);
446 impact[node->plane->type] -= ndist;
450 impact[0] = origin[0] - node->plane->normal[0] * ndist;
451 impact[1] = origin[1] - node->plane->normal[1] * ndist;
452 impact[2] = origin[2] - node->plane->normal[2] * ndist;
455 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
457 if (surf->stainsamples)
459 smax = (surf->extents[0] >> 4) + 1;
460 tmax = (surf->extents[1] >> 4) + 1;
462 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
463 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
465 s = bound(0, impacts, smax * 16) - impacts;
466 t = bound(0, impactt, tmax * 16) - impactt;
467 i = s * s + t * t + dist2;
471 // reduce calculations
472 for (s = 0, i = impacts; s < smax; s++, i -= 16)
473 sdtable[s] = i * i + dist2;
475 bl = surf->stainsamples;
480 for (t = 0;t < tmax;t++, i -= 16)
483 // make sure some part of it is visible on this line
486 maxdist2 = maxdist - td;
487 for (s = 0;s < smax;s++)
489 if (sdtable[s] < maxdist2)
491 ratio = lhrandom(0.0f, 1.0f);
492 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
493 if (a >= (1.0f / 64.0f))
497 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
498 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
499 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
509 // force lightmap upload
511 surf->cached_dlight = true;
515 if (node->children[0]->contents >= 0)
517 if (node->children[1]->contents >= 0)
519 R_StainNode(node->children[0], model, origin, radius, fcolor);
520 node = node->children[1];
525 node = node->children[0];
529 else if (node->children[1]->contents >= 0)
531 node = node->children[1];
536 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)
540 entity_render_t *ent;
543 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
548 fcolor[3] = ca1 * (1.0f / 64.0f);
549 fcolor[4] = cr2 - cr1;
550 fcolor[5] = cg2 - cg1;
551 fcolor[6] = cb2 - cb1;
552 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
554 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
556 // look for embedded bmodels
557 for (n = 0;n < cl_num_brushmodel_entities;n++)
559 ent = cl_brushmodel_entities[n];
561 if (model && model->name[0] == '*')
563 Mod_CheckLoaded(model);
564 if (model->brushq1.nodes)
566 Matrix4x4_Transform(&ent->inversematrix, origin, org);
567 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
575 =============================================================
579 =============================================================
582 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
587 if (styles[0] != 255)
589 for (i = 0;i < numverts;i++, c += 4)
591 lm = samples + lightmapoffsets[i];
592 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
593 VectorMA(c, scale, lm, c);
594 if (styles[1] != 255)
597 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
598 VectorMA(c, scale, lm, c);
599 if (styles[2] != 255)
602 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
603 VectorMA(c, scale, lm, c);
604 if (styles[3] != 255)
607 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
608 VectorMA(c, scale, lm, c);
616 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
622 for (i = 0;i < numverts;i++, v += 3, c += 4)
624 VectorSubtract(v, modelorg, diff);
625 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
626 VectorScale(c, f, c);
629 else if (colorscale != 1)
630 for (i = 0;i < numverts;i++, c += 4)
631 VectorScale(c, colorscale, c);
634 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)
643 for (i = 0;i < numverts;i++, v += 3, c += 4)
645 VectorSubtract(v, modelorg, diff);
646 f = 1 - exp(fogdensity/DotProduct(diff, diff));
655 for (i = 0;i < numverts;i++, c += 4)
665 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)
672 for (i = 0;i < numverts;i++, v += 3, c += 4)
674 VectorSubtract(v, modelorg, diff);
675 f = exp(fogdensity/DotProduct(diff, diff));
683 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
688 int i, l, lit = false;
689 const dlight_t *light;
691 for (l = 0;l < r_numdlights;l++)
693 if (dlightbits[l >> 5] & (1 << (l & 31)))
695 light = &r_dlight[l];
696 Matrix4x4_Transform(matrix, light->origin, lightorigin);
697 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
699 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
700 if (f < light->rtlight.lightmap_cullradius2)
702 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
703 VectorMA(c, f, light->rtlight.lightmap_light, c);
712 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
714 const msurface_t *surf;
717 // LordHavoc: HalfLife maps have freaky skypolys...
718 if (ent->model->brush.ishlbsp)
723 skyrendernow = false;
728 R_Mesh_Matrix(&ent->matrix);
730 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
733 // depth-only (masking)
734 qglColorMask(0,0,0,0);
735 // just to make sure that braindead drivers don't draw anything
736 // despite that colormask...
737 GL_BlendFunc(GL_ZERO, GL_ONE);
742 GL_BlendFunc(GL_ONE, GL_ZERO);
747 memset(&m, 0, sizeof(m));
748 R_Mesh_State_Texture(&m);
750 while((surf = *surfchain++) != NULL)
752 if (surf->visframe == r_framecount)
754 GL_VertexPointer(surf->mesh.data_vertex3f);
755 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
758 qglColorMask(1,1,1,1);
761 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
763 const entity_render_t *ent = calldata1;
764 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
769 matrix4x4_t tempmatrix;
770 float args[4] = {0.05f,0,0,0.04f};
772 if (gl_textureshader && r_watershader.value && !fogenabled)
774 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
775 R_Mesh_TextureMatrix(1, &tempmatrix);
776 Matrix4x4_CreateFromQuakeEntity(&tempmatrix, 0, 0, 0, 0, 0, 0, r_watershader.value);
777 R_Mesh_TextureMatrix(0, &tempmatrix);
779 else if (r_waterscroll.value)
781 // scrolling in texture matrix
782 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
783 R_Mesh_TextureMatrix(0, &tempmatrix);
786 R_Mesh_Matrix(&ent->matrix);
787 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
789 memset(&m, 0, sizeof(m));
790 texture = surf->texinfo->texture->currentframe;
791 alpha = texture->currentalpha;
792 if (texture->rendertype == SURFRENDER_ADD)
794 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
797 else if (texture->rendertype == SURFRENDER_ALPHA)
799 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
804 GL_BlendFunc(GL_ONE, GL_ZERO);
807 if (gl_textureshader && r_watershader.value && !fogenabled)
809 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
810 m.tex[1] = R_GetTexture(texture->skin.base);
813 m.tex[0] = R_GetTexture(texture->skin.base);
816 GL_ColorPointer(varray_color4f);
818 GL_Color(1, 1, 1, alpha);
819 if (gl_textureshader && r_watershader.value && !fogenabled)
821 GL_ActiveTexture (0);
822 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
823 GL_ActiveTexture (1);
824 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
825 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
826 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
827 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
828 qglEnable (GL_TEXTURE_SHADER_NV);
831 GL_VertexPointer(surf->mesh.data_vertex3f);
832 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
833 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
834 m.texcombinergb[1] = GL_REPLACE;
835 R_Mesh_State_Texture(&m);
838 R_FillColors(varray_color4f, surf->mesh.num_vertices, 1, 1, 1, alpha);
839 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, surf->mesh.num_vertices, modelorg);
841 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
843 if (gl_textureshader && r_watershader.value && !fogenabled)
845 qglDisable (GL_TEXTURE_SHADER_NV);
846 GL_ActiveTexture (0);
851 memset(&m, 0, sizeof(m));
852 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
855 m.tex[0] = R_GetTexture(texture->skin.fog);
856 GL_VertexPointer(surf->mesh.data_vertex3f);
857 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
858 GL_ColorPointer(varray_color4f);
859 R_Mesh_State_Texture(&m);
860 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, surf->mesh.num_vertices, modelorg);
861 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
864 if ((gl_textureshader && r_watershader.value && !fogenabled) || r_waterscroll.value)
866 Matrix4x4_CreateIdentity(&tempmatrix);
867 R_Mesh_TextureMatrix(0, &tempmatrix);
868 R_Mesh_TextureMatrix(1, &tempmatrix);
872 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
874 const msurface_t *surf;
877 if (texture->rendertype != SURFRENDER_OPAQUE)
879 for (chain = surfchain;(surf = *chain) != NULL;chain++)
881 if (surf->visframe == r_framecount)
883 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
884 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
889 for (chain = surfchain;(surf = *chain) != NULL;chain++)
890 if (surf->visframe == r_framecount)
891 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
894 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
896 float base, colorscale;
899 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
900 memset(&m, 0, sizeof(m));
901 if (rendertype == SURFRENDER_ADD)
903 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
906 else if (rendertype == SURFRENDER_ALPHA)
908 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
913 GL_BlendFunc(GL_ONE, GL_ZERO);
916 m.tex[0] = R_GetTexture(texture->skin.base);
918 if (gl_combine.integer)
920 m.texrgbscale[0] = 4;
923 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
925 GL_ColorPointer(varray_color4f);
927 GL_VertexPointer(surf->mesh.data_vertex3f);
928 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
929 R_Mesh_State_Texture(&m);
930 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
931 if (!(ent->effects & EF_FULLBRIGHT))
933 if (surf->dlightframe == r_framecount)
934 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
935 if (surf->flags & SURF_LIGHTMAP)
936 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);
938 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
939 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
942 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
946 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
947 memset(&m, 0, sizeof(m));
948 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
951 m.tex[0] = R_GetTexture(texture->skin.glow);
952 GL_ColorPointer(varray_color4f);
954 GL_VertexPointer(surf->mesh.data_vertex3f);
956 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
957 R_Mesh_State_Texture(&m);
958 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
959 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
962 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
966 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
967 memset(&m, 0, sizeof(m));
968 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
971 m.tex[0] = R_GetTexture(texture->skin.fog);
972 GL_ColorPointer(varray_color4f);
974 GL_VertexPointer(surf->mesh.data_vertex3f);
976 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
977 R_Mesh_State_Texture(&m);
978 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
979 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
982 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
984 const msurface_t *surf;
986 int lightmaptexturenum;
987 memset(&m, 0, sizeof(m));
988 GL_BlendFunc(GL_ONE, GL_ZERO);
991 m.tex[0] = R_GetTexture(texture->skin.base);
992 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
993 m.texrgbscale[1] = 2;
994 m.tex[2] = R_GetTexture(texture->skin.detail);
995 m.texrgbscale[2] = 2;
996 if (texture->skin.glow)
998 m.tex[3] = R_GetTexture(texture->skin.glow);
999 m.texcombinergb[3] = GL_ADD;
1001 if (r_shadow_realtime_world.integer)
1002 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1004 GL_Color(1, 1, 1, 1);
1006 while((surf = *surfchain++) != NULL)
1008 if (surf->visframe == r_framecount)
1010 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1011 //if (m.tex[1] != lightmaptexturenum)
1013 m.tex[1] = lightmaptexturenum;
1014 // R_Mesh_State_Texture(&m);
1016 GL_VertexPointer(surf->mesh.data_vertex3f);
1017 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1018 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1019 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1020 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
1021 R_Mesh_State_Texture(&m);
1022 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1027 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1029 const msurface_t *surf;
1031 int lightmaptexturenum;
1032 memset(&m, 0, sizeof(m));
1033 GL_BlendFunc(GL_ONE, GL_ZERO);
1036 m.tex[0] = R_GetTexture(texture->skin.base);
1037 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1038 m.texrgbscale[1] = 2;
1039 m.tex[2] = R_GetTexture(texture->skin.detail);
1040 m.texrgbscale[2] = 2;
1041 if (r_shadow_realtime_world.integer)
1042 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1044 GL_Color(1, 1, 1, 1);
1046 while((surf = *surfchain++) != NULL)
1048 if (surf->visframe == r_framecount)
1050 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1051 //if (m.tex[1] != lightmaptexturenum)
1053 m.tex[1] = lightmaptexturenum;
1054 // R_Mesh_State_Texture(&m);
1056 GL_VertexPointer(surf->mesh.data_vertex3f);
1057 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1058 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1059 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1060 R_Mesh_State_Texture(&m);
1061 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1066 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1068 const msurface_t *surf;
1070 int lightmaptexturenum;
1071 memset(&m, 0, sizeof(m));
1072 GL_BlendFunc(GL_ONE, GL_ZERO);
1075 m.tex[0] = R_GetTexture(texture->skin.base);
1076 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1077 m.texrgbscale[1] = 2;
1078 if (r_shadow_realtime_world.integer)
1079 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1081 GL_Color(1, 1, 1, 1);
1082 while((surf = *surfchain++) != NULL)
1084 if (surf->visframe == r_framecount)
1086 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1087 //if (m.tex[1] != lightmaptexturenum)
1089 m.tex[1] = lightmaptexturenum;
1090 // R_Mesh_State_Texture(&m);
1092 GL_VertexPointer(surf->mesh.data_vertex3f);
1093 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1094 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1095 R_Mesh_State_Texture(&m);
1096 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1101 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1103 const msurface_t *surf;
1105 memset(&m, 0, sizeof(m));
1108 GL_BlendFunc(GL_ONE, GL_ZERO);
1109 m.tex[0] = R_GetTexture(texture->skin.base);
1110 if (r_shadow_realtime_world.integer)
1111 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1113 GL_Color(1, 1, 1, 1);
1114 while((surf = *surfchain++) != NULL)
1116 if (surf->visframe == r_framecount)
1118 GL_VertexPointer(surf->mesh.data_vertex3f);
1119 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1120 R_Mesh_State_Texture(&m);
1121 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1126 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1128 const msurface_t *surf;
1130 int lightmaptexturenum;
1131 memset(&m, 0, sizeof(m));
1132 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1133 GL_DepthMask(false);
1135 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1136 GL_Color(1, 1, 1, 1);
1137 while((surf = *surfchain++) != NULL)
1139 if (surf->visframe == r_framecount)
1141 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1142 //if (m.tex[0] != lightmaptexturenum)
1144 m.tex[0] = lightmaptexturenum;
1145 // R_Mesh_State_Texture(&m);
1147 GL_VertexPointer(surf->mesh.data_vertex3f);
1148 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1149 R_Mesh_State_Texture(&m);
1150 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1155 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1157 const msurface_t *surf;
1160 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1161 memset(&m, 0, sizeof(m));
1162 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1163 GL_DepthMask(false);
1165 GL_ColorPointer(varray_color4f);
1166 while((surf = *surfchain++) != NULL)
1168 if (surf->visframe == r_framecount)
1170 GL_VertexPointer(surf->mesh.data_vertex3f);
1172 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1173 R_Mesh_State_Texture(&m);
1174 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1175 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1180 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1182 const msurface_t *surf;
1184 memset(&m, 0, sizeof(m));
1185 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1186 GL_DepthMask(false);
1188 m.tex[0] = R_GetTexture(texture->skin.detail);
1189 GL_Color(1, 1, 1, 1);
1190 while((surf = *surfchain++) != NULL)
1192 if (surf->visframe == r_framecount)
1194 GL_VertexPointer(surf->mesh.data_vertex3f);
1195 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1196 R_Mesh_State_Texture(&m);
1197 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1202 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1204 const msurface_t *surf;
1206 memset(&m, 0, sizeof(m));
1207 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1208 GL_DepthMask(false);
1210 m.tex[0] = R_GetTexture(texture->skin.glow);
1211 GL_Color(1, 1, 1, 1);
1212 while((surf = *surfchain++) != NULL)
1214 if (surf->visframe == r_framecount)
1216 GL_VertexPointer(surf->mesh.data_vertex3f);
1217 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1218 R_Mesh_State_Texture(&m);
1219 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1225 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1227 const msurface_t *surf;
1229 memset(&m, 0, sizeof(m));
1230 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1232 m.tex[0] = R_GetTexture(texture->skin.glow);
1234 GL_Color(1, 1, 1, 1);
1236 GL_Color(0, 0, 0, 1);
1237 while((surf = *surfchain++) != NULL)
1239 if (surf->visframe == r_framecount)
1241 GL_VertexPointer(surf->mesh.data_vertex3f);
1242 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1243 R_Mesh_State_Texture(&m);
1244 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1250 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1252 const msurface_t *surf;
1254 int lightmaptexturenum;
1255 memset(&m, 0, sizeof(m));
1256 GL_BlendFunc(GL_ONE, GL_ZERO);
1259 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1260 if (r_shadow_realtime_world.integer)
1261 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1263 GL_Color(1, 1, 1, 1);
1264 while((surf = *surfchain++) != NULL)
1266 if (surf->visframe == r_framecount)
1268 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1269 //if (m.tex[0] != lightmaptexturenum)
1271 m.tex[0] = lightmaptexturenum;
1272 // R_Mesh_State_Texture(&m);
1274 GL_VertexPointer(surf->mesh.data_vertex3f);
1275 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1276 R_Mesh_State_Texture(&m);
1277 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1282 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1284 const entity_render_t *ent = calldata1;
1285 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1289 R_Mesh_Matrix(&ent->matrix);
1291 texture = surf->texinfo->texture;
1292 if (texture->animated)
1293 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];
1295 currentalpha = ent->alpha;
1296 if (ent->effects & EF_ADDITIVE)
1297 rendertype = SURFRENDER_ADD;
1298 else if (currentalpha < 1 || texture->skin.fog != NULL)
1299 rendertype = SURFRENDER_ALPHA;
1301 rendertype = SURFRENDER_OPAQUE;
1303 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1304 if (texture->skin.glow)
1305 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1307 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1310 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1312 const msurface_t *surf;
1315 if (texture->rendertype != SURFRENDER_OPAQUE)
1317 // transparent vertex shaded from lightmap
1318 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1320 if (surf->visframe == r_framecount)
1322 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1323 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1327 else if (ent->effects & EF_FULLBRIGHT)
1329 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1330 if (r_detailtextures.integer)
1331 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1332 if (texture->skin.glow)
1333 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1335 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1338 // opaque base lighting
1339 else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
1341 if (r_ambient.value > 0)
1345 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1347 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1350 // opaque lightmapped
1351 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1353 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1355 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1357 else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1359 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1360 if (texture->skin.glow)
1361 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1363 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1365 else if (r_textureunits.integer >= 2 && gl_combine.integer && !gl_lightmaps.integer)
1367 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1368 if (r_detailtextures.integer)
1369 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1370 if (texture->skin.glow)
1371 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1373 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1375 else if (!gl_lightmaps.integer)
1377 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1378 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1379 if (r_detailtextures.integer)
1380 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1381 if (texture->skin.glow)
1382 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1384 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1388 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1390 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1394 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1395 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1396 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1398 int Cshader_count = 3;
1399 Cshader_t *Cshaders[3] =
1401 &Cshader_wall_lightmap,
1406 void R_UpdateTextureInfo(entity_render_t *ent)
1408 int i, texframe, alttextures;
1414 alttextures = ent->frame != 0;
1415 texframe = (int)(cl.time * 5.0f);
1416 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1418 t = ent->model->brushq1.textures + i;
1419 t->currentalpha = ent->alpha;
1420 if (t->flags & SURF_WATERALPHA)
1421 t->currentalpha *= r_wateralpha.value;
1422 if (ent->effects & EF_ADDITIVE)
1423 t->rendertype = SURFRENDER_ADD;
1424 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1425 t->rendertype = SURFRENDER_ALPHA;
1427 t->rendertype = SURFRENDER_OPAQUE;
1428 // we don't need to set currentframe if t->animated is false because
1429 // it was already set up by the texture loader for non-animating
1431 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1435 void R_PrepareSurfaces(entity_render_t *ent)
1437 int i, numsurfaces, *surfacevisframes;
1439 msurface_t *surf, *surfaces, **surfchain;
1446 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1447 numsurfaces = model->brushq1.nummodelsurfaces;
1448 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1449 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1451 R_UpdateTextureInfo(ent);
1453 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1456 if (model->brushq1.light_ambient != r_ambient.value)
1458 model->brushq1.light_ambient = r_ambient.value;
1459 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1460 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1464 for (i = 0;i < model->brushq1.light_styles;i++)
1466 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1468 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1469 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1470 (**surfchain).cached_dlight = true;
1475 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1477 if (surfacevisframes[i] == r_framecount)
1479 #if !WORLDNODECULLBACKFACES
1480 // mark any backface surfaces as not visible
1481 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1483 if (!(surf->flags & SURF_PLANEBACK))
1484 surfacevisframes[i] = -1;
1488 if ((surf->flags & SURF_PLANEBACK))
1489 surfacevisframes[i] = -1;
1491 if (surfacevisframes[i] == r_framecount)
1495 surf->visframe = r_framecount;
1496 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1497 R_BuildLightMap(ent, surf);
1503 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1507 if (ent->model == NULL)
1509 R_Mesh_Matrix(&ent->matrix);
1510 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1511 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1512 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1515 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1520 const entity_render_t *ent = calldata1;
1521 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1522 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1523 GL_DepthMask(false);
1525 R_Mesh_Matrix(&ent->matrix);
1526 GL_VertexPointer(varray_vertex3f);
1528 memset(&m, 0, sizeof(m));
1529 R_Mesh_State_Texture(&m);
1531 i = portal - ent->model->brushq1.portals;
1532 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1533 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1534 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1536 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1538 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1539 VectorCopy(portal->points[i].position, v);
1542 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1543 VectorCopy(portal->points[i].position, v);
1544 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1547 // LordHavoc: this is just a nice debugging tool, very slow
1548 static void R_DrawPortals(entity_render_t *ent)
1551 mportal_t *portal, *endportal;
1552 float temp[3], center[3], f;
1553 if (ent->model == NULL)
1555 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1557 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1560 for (i = 0;i < portal->numpoints;i++)
1561 VectorAdd(temp, portal->points[i].position, temp);
1562 f = ixtable[portal->numpoints];
1563 VectorScale(temp, f, temp);
1564 Matrix4x4_Transform(&ent->matrix, temp, center);
1565 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1570 void R_PrepareBrushModel(entity_render_t *ent)
1572 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1575 #if WORLDNODECULLBACKFACES
1579 // because bmodels can be reused, we have to decide which things to render
1580 // from scratch every time
1584 #if WORLDNODECULLBACKFACES
1585 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1587 numsurfaces = model->brushq1.nummodelsurfaces;
1588 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1589 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1590 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1591 for (i = 0;i < numsurfaces;i++, surf++)
1593 #if WORLDNODECULLBACKFACES
1594 // mark any backface surfaces as not visible
1595 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1597 if ((surf->flags & SURF_PLANEBACK))
1598 surfacevisframes[i] = r_framecount;
1600 else if (!(surf->flags & SURF_PLANEBACK))
1601 surfacevisframes[i] = r_framecount;
1603 surfacevisframes[i] = r_framecount;
1605 surf->dlightframe = -1;
1607 R_PrepareSurfaces(ent);
1610 void R_SurfaceWorldNode (entity_render_t *ent)
1612 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1618 // equivilant to quake's RecursiveWorldNode but faster and more effective
1622 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1623 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1624 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1626 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1628 if (!R_CullBox (leaf->mins, leaf->maxs))
1631 leaf->visframe = r_framecount;
1635 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1637 surfnum = model->brushq1.pvssurflist[i];
1638 surf = model->brushq1.surfaces + surfnum;
1639 #if WORLDNODECULLBACKFACES
1640 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1642 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1643 surfacevisframes[surfnum] = r_framecount;
1647 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1648 surfacevisframes[surfnum] = r_framecount;
1651 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1652 surfacevisframes[surfnum] = r_framecount;
1657 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1659 int c, leafstackpos, *mark, *surfacevisframes;
1660 #if WORLDNODECULLBACKFACES
1664 mleaf_t *leaf, *leafstack[8192];
1667 msurface_t *surfaces;
1668 if (ent->model == NULL)
1670 // LordHavoc: portal-passage worldnode with PVS;
1671 // follows portals leading outward from viewleaf, does not venture
1672 // offscreen or into leafs that are not visible, faster than Quake's
1673 // RecursiveWorldNode
1674 surfaces = ent->model->brushq1.surfaces;
1675 surfacevisframes = ent->model->brushq1.surfacevisframes;
1676 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1677 viewleaf->worldnodeframe = r_framecount;
1678 leafstack[0] = viewleaf;
1680 while (leafstackpos)
1683 leaf = leafstack[--leafstackpos];
1684 leaf->visframe = r_framecount;
1685 // draw any surfaces bounding this leaf
1686 if (leaf->nummarksurfaces)
1688 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1690 #if WORLDNODECULLBACKFACES
1692 if (surfacevisframes[n] != r_framecount)
1694 surf = surfaces + n;
1695 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1697 if ((surf->flags & SURF_PLANEBACK))
1698 surfacevisframes[n] = r_framecount;
1702 if (!(surf->flags & SURF_PLANEBACK))
1703 surfacevisframes[n] = r_framecount;
1707 surfacevisframes[*mark++] = r_framecount;
1711 // follow portals into other leafs
1712 for (p = leaf->portals;p;p = p->next)
1714 // LordHavoc: this DotProduct hurts less than a cache miss
1715 // (which is more likely to happen if backflowing through leafs)
1716 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1719 if (leaf->worldnodeframe != r_framecount)
1721 leaf->worldnodeframe = r_framecount;
1722 // FIXME: R_CullBox is absolute, should be done relative
1723 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1724 leafstack[leafstackpos++] = leaf;
1731 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1733 int j, c, *surfacepvsframes, *mark;
1738 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1740 model->brushq1.pvsframecount++;
1741 model->brushq1.pvsviewleaf = viewleaf;
1742 model->brushq1.pvsviewleafnovis = r_novis.integer;
1743 model->brushq1.pvsleafchain = NULL;
1744 model->brushq1.pvssurflistlength = 0;
1747 surfacepvsframes = model->brushq1.surfacepvsframes;
1748 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1750 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1752 leaf->pvsframe = model->brushq1.pvsframecount;
1753 leaf->pvschain = model->brushq1.pvsleafchain;
1754 model->brushq1.pvsleafchain = leaf;
1755 // mark surfaces bounding this leaf as visible
1756 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1757 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1760 model->brushq1.BuildPVSTextureChains(model);
1765 void R_WorldVisibility(entity_render_t *ent)
1770 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1771 viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1772 R_PVSUpdate(ent, viewleaf);
1777 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1778 R_SurfaceWorldNode (ent);
1780 R_PortalWorldNode (ent, viewleaf);
1783 void R_DrawWorld(entity_render_t *ent)
1785 if (ent->model == NULL)
1787 if (!ent->model->brushq1.num_leafs)
1789 if (ent->model->DrawSky)
1790 ent->model->DrawSky(ent);
1791 if (ent->model->Draw)
1792 ent->model->Draw(ent);
1796 R_PrepareSurfaces(ent);
1797 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1798 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1799 if (r_drawportals.integer)
1804 void R_Model_Brush_DrawSky(entity_render_t *ent)
1806 if (ent->model == NULL)
1808 if (ent != &cl_entities[0].render)
1809 R_PrepareBrushModel(ent);
1810 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1813 void R_Model_Brush_Draw(entity_render_t *ent)
1815 if (ent->model == NULL)
1818 if (ent != &cl_entities[0].render)
1819 R_PrepareBrushModel(ent);
1820 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1823 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1827 float projectdistance, f, temp[3], lightradius2;
1828 if (ent->model == NULL)
1830 R_Mesh_Matrix(&ent->matrix);
1831 lightradius2 = lightradius * lightradius;
1832 R_UpdateTextureInfo(ent);
1833 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1834 for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1836 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1838 f = PlaneDiff(relativelightorigin, surf->plane);
1839 if (surf->flags & SURF_PLANEBACK)
1841 // draw shadows only for frontfaces and only if they are close
1842 if (f >= 0.1 && f < lightradius)
1844 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1845 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1846 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1847 if (DotProduct(temp, temp) < lightradius2)
1848 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);
1854 void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
1859 float f, lightmins[3], lightmaxs[3];
1860 if (ent->model == NULL)
1862 R_Mesh_Matrix(&ent->matrix);
1863 lightmins[0] = relativelightorigin[0] - lightradius;
1864 lightmins[1] = relativelightorigin[1] - lightradius;
1865 lightmins[2] = relativelightorigin[2] - lightradius;
1866 lightmaxs[0] = relativelightorigin[0] + lightradius;
1867 lightmaxs[1] = relativelightorigin[1] + lightradius;
1868 lightmaxs[2] = relativelightorigin[2] + lightradius;
1869 R_UpdateTextureInfo(ent);
1870 for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1872 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1874 f = PlaneDiff(relativelightorigin, surf->plane);
1875 if (surf->flags & SURF_PLANEBACK)
1877 if (f >= -0.1 && f < lightradius)
1879 t = surf->texinfo->texture->currentframe;
1880 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1882 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, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, lightcubemap);
1883 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, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.gloss, t->skin.nmap, lightcubemap);
1890 void R_DrawCollisionBrush(colbrushf_t *brush)
1893 i = ((int)brush) / sizeof(colbrushf_t);
1894 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1895 GL_VertexPointer(brush->points->v);
1896 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1899 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1902 if (!face->num_triangles)
1907 skyrendernow = false;
1908 if (skyrendermasked)
1912 R_Mesh_Matrix(&ent->matrix);
1914 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1915 if (skyrendermasked)
1917 // depth-only (masking)
1918 qglColorMask(0,0,0,0);
1919 // just to make sure that braindead drivers don't draw anything
1920 // despite that colormask...
1921 GL_BlendFunc(GL_ZERO, GL_ONE);
1926 GL_BlendFunc(GL_ONE, GL_ZERO);
1931 memset(&m, 0, sizeof(m));
1932 R_Mesh_State_Texture(&m);
1934 GL_VertexPointer(face->data_vertex3f);
1935 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1936 qglColorMask(1,1,1,1);
1939 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
1942 memset(&m, 0, sizeof(m));
1943 GL_BlendFunc(GL_ONE, GL_ZERO);
1946 if (face->texture->skin.glow)
1948 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1949 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1950 GL_Color(1, 1, 1, 1);
1953 GL_Color(0, 0, 0, 1);
1954 R_Mesh_State_Texture(&m);
1955 GL_VertexPointer(face->data_vertex3f);
1956 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1959 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
1962 memset(&m, 0, sizeof(m));
1963 GL_BlendFunc(GL_ONE, GL_ZERO);
1966 m.tex[0] = R_GetTexture(face->texture->skin.base);
1967 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1968 m.tex[1] = R_GetTexture(face->lightmaptexture);
1969 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1970 m.texrgbscale[1] = 2;
1971 GL_Color(1, 1, 1, 1);
1972 R_Mesh_State_Texture(&m);
1973 GL_VertexPointer(face->data_vertex3f);
1974 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1977 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
1980 memset(&m, 0, sizeof(m));
1981 GL_BlendFunc(GL_ONE, GL_ZERO);
1984 m.tex[0] = R_GetTexture(face->texture->skin.base);
1985 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1986 GL_Color(1, 1, 1, 1);
1987 R_Mesh_State_Texture(&m);
1988 GL_VertexPointer(face->data_vertex3f);
1989 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1992 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
1995 memset(&m, 0, sizeof(m));
1996 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1997 GL_DepthMask(false);
1999 m.tex[0] = R_GetTexture(face->lightmaptexture);
2000 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2001 GL_Color(1, 1, 1, 1);
2002 R_Mesh_State_Texture(&m);
2003 GL_VertexPointer(face->data_vertex3f);
2004 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2007 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3mface_t *face)
2010 memset(&m, 0, sizeof(m));
2011 GL_BlendFunc(GL_ONE, GL_ZERO);
2014 m.tex[0] = R_GetTexture(face->lightmaptexture);
2015 m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2016 if (r_shadow_realtime_world.integer)
2017 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2019 GL_Color(1, 1, 1, 1);
2020 R_Mesh_State_Texture(&m);
2021 GL_VertexPointer(face->data_vertex3f);
2022 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2025 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
2028 memset(&m, 0, sizeof(m));
2029 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2030 GL_DepthMask(false);
2032 if (face->texture->skin.glow)
2034 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2035 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2036 GL_Color(1, 1, 1, 1);
2039 GL_Color(0, 0, 0, 1);
2040 R_Mesh_State_Texture(&m);
2041 GL_VertexPointer(face->data_vertex3f);
2042 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2045 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
2050 memset(&m, 0, sizeof(m));
2051 GL_BlendFunc(GL_ONE, GL_ZERO);
2054 m.tex[0] = R_GetTexture(face->texture->skin.base);
2055 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2057 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2058 mul *= r_shadow_realtime_world_lightmaps.value;
2059 if (mul == 2 && gl_combine.integer)
2061 m.texrgbscale[0] = 2;
2062 GL_ColorPointer(face->data_color4f);
2065 GL_ColorPointer(face->data_color4f);
2068 for (i = 0;i < face->num_vertices;i++)
2070 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2071 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2072 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2073 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2075 GL_ColorPointer(varray_color4f);
2077 R_Mesh_State_Texture(&m);
2078 GL_VertexPointer(face->data_vertex3f);
2079 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2082 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3mface_t *face)
2087 memset(&m, 0, sizeof(m));
2088 GL_BlendFunc(GL_ONE, GL_ZERO);
2091 R_Mesh_State_Texture(&m);
2093 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2094 mul *= r_shadow_realtime_world_lightmaps.value;
2096 GL_ColorPointer(face->data_color4f);
2099 for (i = 0;i < face->num_vertices;i++)
2101 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2102 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2103 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2104 varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2106 GL_ColorPointer(varray_color4f);
2108 GL_VertexPointer(face->data_vertex3f);
2109 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2112 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
2115 memset(&m, 0, sizeof(m));
2116 GL_BlendFunc(GL_ONE, GL_ONE);
2119 m.tex[0] = R_GetTexture(face->texture->skin.base);
2120 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2121 GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2122 R_Mesh_State_Texture(&m);
2123 GL_VertexPointer(face->data_vertex3f);
2124 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2127 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2129 const entity_render_t *ent = voident;
2130 q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
2132 R_Mesh_Matrix(&ent->matrix);
2133 memset(&m, 0, sizeof(m));
2134 if (ent->effects & EF_ADDITIVE)
2135 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2137 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2138 GL_DepthMask(false);
2140 m.tex[0] = R_GetTexture(face->texture->skin.base);
2141 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2142 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2143 if (gl_combine.integer)
2145 m.texrgbscale[0] = 2;
2146 if (r_textureunits.integer >= 2)
2148 m.tex[1] = R_GetTexture(face->lightmaptexture);
2149 m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2150 GL_Color(1, 1, 1, ent->alpha);
2154 if (ent->alpha == 1)
2155 GL_ColorPointer(face->data_color4f);
2159 for (i = 0;i < face->num_vertices;i++)
2161 varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2162 varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2163 varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2164 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2166 GL_ColorPointer(varray_color4f);
2173 for (i = 0;i < face->num_vertices;i++)
2175 varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2176 varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2177 varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2178 varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2180 GL_ColorPointer(varray_color4f);
2182 R_Mesh_State_Texture(&m);
2183 GL_VertexPointer(face->data_vertex3f);
2184 qglDisable(GL_CULL_FACE);
2185 R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2186 qglEnable(GL_CULL_FACE);
2189 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2191 if (!face->num_triangles)
2193 if (face->texture->surfaceparms)
2195 if (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW))
2199 face->visframe = r_framecount;
2200 if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2202 vec3_t facecenter, center;
2203 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2204 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2205 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2206 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2207 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2210 R_Mesh_Matrix(&ent->matrix);
2211 if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2212 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2213 else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2215 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2216 if (face->texture->skin.glow)
2217 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2219 else if (face->lightmaptexture)
2221 if (gl_lightmaps.integer)
2222 R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2225 if (r_textureunits.integer >= 2 && gl_combine.integer)
2226 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2229 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2230 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2232 if (face->texture->skin.glow)
2233 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2238 if (gl_lightmaps.integer)
2239 R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2242 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2243 if (face->texture->skin.glow)
2244 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2247 if (r_ambient.value)
2248 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2251 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2257 if (R_CullBox(node->mins, node->maxs))
2262 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2263 node = node->children[1];
2265 leaf = (q3mleaf_t *)node;
2266 if (CHECKPVSBIT(pvs, leaf->clusterindex))
2269 for (i = 0;i < leaf->numleaffaces;i++)
2270 leaf->firstleafface[i]->markframe = markframe;
2274 // FIXME: num_leafs needs to be recalculated at load time to include only
2275 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2276 // the submodels (which would render the submodels occasionally, as part of
2277 // the world - not good)
2278 void R_Q3BSP_MarkLeafPVS(entity_render_t *ent, qbyte *pvs, int markframe)
2282 for (j = 0, leaf = ent->model->brushq3.data_leafs;j < ent->model->brushq3.num_leafs;j++, leaf++)
2284 if (CHECKPVSBIT(pvs, leaf->clusterindex))
2287 for (i = 0;i < leaf->numleaffaces;i++)
2288 leaf->firstleafface[i]->markframe = markframe;
2293 static int r_q3bsp_framecount = -1;
2295 void R_Q3BSP_DrawSky(entity_render_t *ent)
2302 R_Mesh_Matrix(&ent->matrix);
2304 if (r_drawcollisionbrushes.integer < 2)
2306 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2307 if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2309 if (r_q3bsp_framecount != r_framecount)
2311 r_q3bsp_framecount = r_framecount;
2312 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2313 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2315 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2316 if (face->markframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2317 R_Q3BSP_DrawSkyFace(ent, face);
2320 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2321 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2322 R_Q3BSP_DrawSkyFace(ent, face);
2326 void R_Q3BSP_Draw(entity_render_t *ent)
2333 R_Mesh_Matrix(&ent->matrix);
2335 if (r_drawcollisionbrushes.integer < 2)
2337 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2338 if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2340 if (r_q3bsp_framecount != r_framecount)
2342 r_q3bsp_framecount = r_framecount;
2343 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2344 //R_Q3BSP_MarkLeafPVS(ent, pvs, r_framecount);
2346 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2347 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2348 R_Q3BSP_DrawFace(ent, face);
2351 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2352 R_Q3BSP_DrawFace(ent, face);
2354 if (r_drawcollisionbrushes.integer >= 1)
2357 memset(&m, 0, sizeof(m));
2358 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2359 GL_DepthMask(false);
2361 R_Mesh_State_Texture(&m);
2362 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2363 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2364 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2365 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2366 qglPolygonOffset(0, 0);
2370 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2374 vec3_t modelorg, lightmins, lightmaxs;
2376 float projectdistance;
2377 projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2378 if (r_drawcollisionbrushes.integer < 2)
2381 R_Mesh_Matrix(&ent->matrix);
2382 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2383 lightmins[0] = relativelightorigin[0] - lightradius;
2384 lightmins[1] = relativelightorigin[1] - lightradius;
2385 lightmins[2] = relativelightorigin[2] - lightradius;
2386 lightmaxs[0] = relativelightorigin[0] + lightradius;
2387 lightmaxs[1] = relativelightorigin[1] + lightradius;
2388 lightmaxs[2] = relativelightorigin[2] + lightradius;
2389 //if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2390 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2392 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2393 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2394 R_Shadow_Volume(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2398 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_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
2400 if ((face->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) || !face->num_triangles)
2402 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, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.base, face->texture->skin.nmap, lightcubemap);
2403 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, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, face->texture->skin.gloss, face->texture->skin.nmap, lightcubemap);
2406 void R_Q3BSP_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap)
2410 vec3_t modelorg, lightmins, lightmaxs;
2413 //static int markframe = 0;
2414 if (r_drawcollisionbrushes.integer < 2)
2417 R_Mesh_Matrix(&ent->matrix);
2418 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2419 lightmins[0] = relativelightorigin[0] - lightradius;
2420 lightmins[1] = relativelightorigin[1] - lightradius;
2421 lightmins[2] = relativelightorigin[2] - lightradius;
2422 lightmaxs[0] = relativelightorigin[0] + lightradius;
2423 lightmaxs[1] = relativelightorigin[1] + lightradius;
2424 lightmaxs[2] = relativelightorigin[2] + lightradius;
2425 //if (ent == &cl_entities[0].render && model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2426 // R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2428 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2429 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2430 R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, lightcubemap);
2434 static void gl_surf_start(void)
2438 static void gl_surf_shutdown(void)
2442 static void gl_surf_newmap(void)
2446 void GL_Surf_Init(void)
2449 dlightdivtable[0] = 4194304;
2450 for (i = 1;i < 32768;i++)
2451 dlightdivtable[i] = 4194304 / (i << 7);
2453 Cvar_RegisterVariable(&r_ambient);
2454 Cvar_RegisterVariable(&r_drawportals);
2455 Cvar_RegisterVariable(&r_testvis);
2456 Cvar_RegisterVariable(&r_floatbuildlightmap);
2457 Cvar_RegisterVariable(&r_detailtextures);
2458 Cvar_RegisterVariable(&r_surfaceworldnode);
2459 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2460 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2461 Cvar_RegisterVariable(&gl_lightmaps);
2463 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);