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];
52 smax = (surf->extents[0] >> 4) + 1;
53 tmax = (surf->extents[1] >> 4) + 1;
56 for (lnum = 0; lnum < r_numdlights; lnum++)
58 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
59 continue; // not lit by this light
61 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
62 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
64 // for comparisons to minimum acceptable light
65 // compensate for LIGHTOFFSET
66 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
73 if (surf->plane->type < 3)
75 VectorCopy(local, impact);
76 impact[surf->plane->type] -= dist;
80 impact[0] = local[0] - surf->plane->normal[0] * dist;
81 impact[1] = local[1] - surf->plane->normal[1] * dist;
82 impact[2] = local[2] - surf->plane->normal[2] * dist;
85 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
86 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
88 s = bound(0, impacts, smax * 16) - impacts;
89 t = bound(0, impactt, tmax * 16) - impactt;
90 i = s * s + t * t + dist2;
94 // reduce calculations
95 for (s = 0, i = impacts; s < smax; s++, i -= 16)
96 sdtable[s] = i * i + dist2;
98 maxdist3 = maxdist - dist2;
100 // convert to 8.8 blocklights format
101 red = r_dlight[lnum].light[0] * (1.0f / 128.0f);
102 green = r_dlight[lnum].light[1] * (1.0f / 128.0f);
103 blue = r_dlight[lnum].light[2] * (1.0f / 128.0f);
104 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
108 for (t = 0;t < tmax;t++, i -= 16)
111 // make sure some part of it is visible on this line
114 maxdist2 = maxdist - td;
115 for (s = 0;s < smax;s++)
117 if (sdtable[s] < maxdist2)
119 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
123 bl[1] += (green * k);
138 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
140 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
141 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
145 smax = (surf->extents[0] >> 4) + 1;
146 tmax = (surf->extents[1] >> 4) + 1;
149 for (lnum = 0; lnum < r_numdlights; lnum++)
151 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
152 continue; // not lit by this light
154 Matrix4x4_Transform(matrix, r_dlight[lnum].origin, local);
155 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
157 // for comparisons to minimum acceptable light
158 // compensate for LIGHTOFFSET
159 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
162 dist2 += LIGHTOFFSET;
163 if (dist2 >= maxdist)
166 if (surf->plane->type < 3)
168 VectorCopy(local, impact);
169 impact[surf->plane->type] -= dist;
173 impact[0] = local[0] - surf->plane->normal[0] * dist;
174 impact[1] = local[1] - surf->plane->normal[1] * dist;
175 impact[2] = local[2] - surf->plane->normal[2] * dist;
178 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
179 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
181 td = bound(0, impacts, smax * 16) - impacts;
182 td1 = bound(0, impactt, tmax * 16) - impactt;
183 td = td * td + td1 * td1 + dist2;
187 // reduce calculations
188 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
189 sdtable[s] = td1 * td1 + dist2;
191 maxdist3 = maxdist - dist2;
193 // convert to 8.8 blocklights format
194 red = r_dlight[lnum].light[0];
195 green = r_dlight[lnum].light[1];
196 blue = r_dlight[lnum].light[2];
197 subtract = r_dlight[lnum].subtract * 32768.0f;
198 bl = floatblocklights;
201 for (t = 0;t < tmax;t++, td1 -= 16.0f)
204 // make sure some part of it is visible on this line
207 maxdist2 = maxdist - td;
208 for (s = 0;s < smax;s++)
210 if (sdtable[s] < maxdist2)
212 k = (32768.0f / (sdtable[s] + td)) - subtract;
232 Combine and scale multiple lightmaps into the 8.8 format in blocklights
235 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
237 if (!r_floatbuildlightmap.integer)
239 int smax, tmax, i, j, size, size3, maps, stride, l;
240 unsigned int *bl, scale;
241 qbyte *lightmap, *out, *stain;
243 // update cached lighting info
244 surf->cached_dlight = 0;
246 smax = (surf->extents[0]>>4)+1;
247 tmax = (surf->extents[1]>>4)+1;
250 lightmap = surf->samples;
252 // set to full bright if no light data
254 if (!ent->model->brushq1.lightdata)
256 for (i = 0;i < size3;i++)
262 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
265 for (i = 0;i < size3;i++)
269 memset(bl, 0, size*3*sizeof(unsigned int));
271 if (surf->dlightframe == r_framecount)
273 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
274 if (surf->cached_dlight)
278 // add all the lightmaps
282 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
283 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
284 bl[i] += lightmap[i] * scale;
288 stain = surf->stainsamples;
291 // the >> 16 shift adjusts down 8 bits to account for the stainmap
292 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
293 // be doubled during rendering to achieve 2x overbright
294 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
295 if (ent->model->brushq1.lightmaprgba)
297 stride = (surf->lightmaptexturestride - smax) * 4;
298 for (i = 0;i < tmax;i++, out += stride)
300 for (j = 0;j < smax;j++)
302 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
303 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
304 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
311 stride = (surf->lightmaptexturestride - smax) * 3;
312 for (i = 0;i < tmax;i++, out += stride)
314 for (j = 0;j < smax;j++)
316 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
317 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
318 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
323 R_UpdateTexture(surf->lightmaptexture, templight);
327 int smax, tmax, i, j, size, size3, maps, stride, l;
329 qbyte *lightmap, *out, *stain;
331 // update cached lighting info
332 surf->cached_dlight = 0;
334 smax = (surf->extents[0]>>4)+1;
335 tmax = (surf->extents[1]>>4)+1;
338 lightmap = surf->samples;
340 // set to full bright if no light data
341 bl = floatblocklights;
342 if (!ent->model->brushq1.lightdata)
345 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
350 for (i = 0;i < size3;i++)
354 memset(bl, 0, size*3*sizeof(float));
356 if (surf->dlightframe == r_framecount)
358 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
359 if (surf->cached_dlight)
363 // add all the lightmaps
366 bl = floatblocklights;
367 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
368 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
369 bl[i] += lightmap[i] * scale;
372 stain = surf->stainsamples;
373 bl = floatblocklights;
375 // this scaling adjusts down 8 bits to account for the stainmap
376 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
377 // be doubled during rendering to achieve 2x overbright
378 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
379 scale = 1.0f / (1 << 16);
380 if (ent->model->brushq1.lightmaprgba)
382 stride = (surf->lightmaptexturestride - smax) * 4;
383 for (i = 0;i < tmax;i++, out += stride)
385 for (j = 0;j < smax;j++)
387 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
388 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
389 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
396 stride = (surf->lightmaptexturestride - smax) * 3;
397 for (i = 0;i < tmax;i++, out += stride)
399 for (j = 0;j < smax;j++)
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
408 R_UpdateTexture(surf->lightmaptexture, templight);
412 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
414 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
415 msurface_t *surf, *endsurf;
416 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
420 maxdist = radius * radius;
421 invradius = 1.0f / radius;
424 if (node->contents < 0)
426 ndist = PlaneDiff(origin, node->plane);
429 node = node->children[0];
434 node = node->children[1];
438 dist2 = ndist * ndist;
439 maxdist3 = maxdist - dist2;
441 if (node->plane->type < 3)
443 VectorCopy(origin, impact);
444 impact[node->plane->type] -= ndist;
448 impact[0] = origin[0] - node->plane->normal[0] * ndist;
449 impact[1] = origin[1] - node->plane->normal[1] * ndist;
450 impact[2] = origin[2] - node->plane->normal[2] * ndist;
453 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
455 if (surf->stainsamples)
457 smax = (surf->extents[0] >> 4) + 1;
458 tmax = (surf->extents[1] >> 4) + 1;
460 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
461 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
463 s = bound(0, impacts, smax * 16) - impacts;
464 t = bound(0, impactt, tmax * 16) - impactt;
465 i = s * s + t * t + dist2;
469 // reduce calculations
470 for (s = 0, i = impacts; s < smax; s++, i -= 16)
471 sdtable[s] = i * i + dist2;
473 bl = surf->stainsamples;
478 for (t = 0;t < tmax;t++, i -= 16)
481 // make sure some part of it is visible on this line
484 maxdist2 = maxdist - td;
485 for (s = 0;s < smax;s++)
487 if (sdtable[s] < maxdist2)
489 ratio = lhrandom(0.0f, 1.0f);
490 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
491 if (a >= (1.0f / 64.0f))
495 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
496 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
497 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
507 // force lightmap upload
509 surf->cached_dlight = true;
513 if (node->children[0]->contents >= 0)
515 if (node->children[1]->contents >= 0)
517 R_StainNode(node->children[0], model, origin, radius, fcolor);
518 node = node->children[1];
523 node = node->children[0];
527 else if (node->children[1]->contents >= 0)
529 node = node->children[1];
534 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)
538 entity_render_t *ent;
541 if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
546 fcolor[3] = ca1 * (1.0f / 64.0f);
547 fcolor[4] = cr2 - cr1;
548 fcolor[5] = cg2 - cg1;
549 fcolor[6] = cb2 - cb1;
550 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
552 R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
554 // look for embedded bmodels
555 for (n = 0;n < cl_num_brushmodel_entities;n++)
557 ent = cl_brushmodel_entities[n];
559 if (model && model->name[0] == '*')
561 Mod_CheckLoaded(model);
562 if (model->brushq1.nodes)
564 Matrix4x4_Transform(&ent->inversematrix, origin, org);
565 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
573 =============================================================
577 =============================================================
580 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
585 if (styles[0] != 255)
587 for (i = 0;i < numverts;i++, c += 4)
589 lm = samples + lightmapoffsets[i];
590 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
591 VectorMA(c, scale, lm, c);
592 if (styles[1] != 255)
595 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
596 VectorMA(c, scale, lm, c);
597 if (styles[2] != 255)
600 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
601 VectorMA(c, scale, lm, c);
602 if (styles[3] != 255)
605 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
606 VectorMA(c, scale, lm, c);
614 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
620 for (i = 0;i < numverts;i++, v += 3, c += 4)
622 VectorSubtract(v, modelorg, diff);
623 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
624 VectorScale(c, f, c);
627 else if (colorscale != 1)
628 for (i = 0;i < numverts;i++, c += 4)
629 VectorScale(c, colorscale, c);
632 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)
641 for (i = 0;i < numverts;i++, v += 3, c += 4)
643 VectorSubtract(v, modelorg, diff);
644 f = 1 - exp(fogdensity/DotProduct(diff, diff));
653 for (i = 0;i < numverts;i++, c += 4)
663 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)
670 for (i = 0;i < numverts;i++, v += 3, c += 4)
672 VectorSubtract(v, modelorg, diff);
673 f = exp(fogdensity/DotProduct(diff, diff));
681 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
686 int i, l, lit = false;
689 for (l = 0;l < r_numdlights;l++)
691 if (dlightbits[l >> 5] & (1 << (l & 31)))
694 Matrix4x4_Transform(matrix, rd->origin, lightorigin);
695 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
697 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
698 if (f < rd->cullradius2)
700 f = ((1.0f / f) - rd->subtract) * scale;
701 VectorMA(c, f, rd->light, c);
710 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
712 const msurface_t *surf;
715 // LordHavoc: HalfLife maps have freaky skypolys...
716 if (ent->model->brush.ishlbsp)
721 skyrendernow = false;
726 R_Mesh_Matrix(&ent->matrix);
728 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
731 // depth-only (masking)
732 qglColorMask(0,0,0,0);
733 // just to make sure that braindead drivers don't draw anything
734 // despite that colormask...
735 GL_BlendFunc(GL_ZERO, GL_ONE);
740 GL_BlendFunc(GL_ONE, GL_ZERO);
745 memset(&m, 0, sizeof(m));
746 R_Mesh_State_Texture(&m);
748 while((surf = *surfchain++) != NULL)
750 if (surf->visframe == r_framecount)
752 GL_VertexPointer(surf->mesh.data_vertex3f);
753 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
756 qglColorMask(1,1,1,1);
759 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
761 const entity_render_t *ent = calldata1;
762 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
767 matrix4x4_t tempmatrix;
768 float args[4] = {0.05f,0,0,0.04f};
770 if (gl_textureshader && r_watershader.value && !fogenabled)
772 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
773 R_Mesh_TextureMatrix(1, &tempmatrix);
774 Matrix4x4_CreateFromQuakeEntity(&tempmatrix, 0, 0, 0, 0, 0, 0, r_watershader.value);
775 R_Mesh_TextureMatrix(0, &tempmatrix);
777 else if (r_waterscroll.value)
779 // scrolling in texture matrix
780 Matrix4x4_CreateTranslate(&tempmatrix, sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
781 R_Mesh_TextureMatrix(0, &tempmatrix);
784 R_Mesh_Matrix(&ent->matrix);
785 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
787 memset(&m, 0, sizeof(m));
788 texture = surf->texinfo->texture->currentframe;
789 alpha = texture->currentalpha;
790 if (texture->rendertype == SURFRENDER_ADD)
792 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
795 else if (texture->rendertype == SURFRENDER_ALPHA)
797 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
802 GL_BlendFunc(GL_ONE, GL_ZERO);
805 if (gl_textureshader && r_watershader.value && !fogenabled)
807 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
808 m.tex[1] = R_GetTexture(texture->skin.base);
811 m.tex[0] = R_GetTexture(texture->skin.base);
814 GL_ColorPointer(varray_color4f);
816 GL_Color(1, 1, 1, alpha);
817 if (gl_textureshader && r_watershader.value && !fogenabled)
819 GL_ActiveTexture (0);
820 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
821 GL_ActiveTexture (1);
822 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
823 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
824 qglTexEnvi (GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
825 qglTexEnvfv (GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
826 qglEnable (GL_TEXTURE_SHADER_NV);
829 GL_VertexPointer(surf->mesh.data_vertex3f);
830 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
831 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
832 m.texcombinergb[1] = GL_REPLACE;
833 R_Mesh_State_Texture(&m);
836 R_FillColors(varray_color4f, surf->mesh.num_vertices, 1, 1, 1, alpha);
837 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, surf->mesh.num_vertices, modelorg);
839 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
841 if (gl_textureshader && r_watershader.value && !fogenabled)
843 qglDisable (GL_TEXTURE_SHADER_NV);
844 GL_ActiveTexture (0);
849 memset(&m, 0, sizeof(m));
850 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
853 m.tex[0] = R_GetTexture(texture->skin.fog);
854 GL_VertexPointer(surf->mesh.data_vertex3f);
855 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
856 GL_ColorPointer(varray_color4f);
857 R_Mesh_State_Texture(&m);
858 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], alpha, 1, surf->mesh.num_vertices, modelorg);
859 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
862 if ((gl_textureshader && r_watershader.value && !fogenabled) || r_waterscroll.value)
864 Matrix4x4_CreateIdentity(&tempmatrix);
865 R_Mesh_TextureMatrix(0, &tempmatrix);
866 R_Mesh_TextureMatrix(1, &tempmatrix);
870 static void RSurfShader_Water(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
872 const msurface_t *surf;
875 if (texture->rendertype != SURFRENDER_OPAQUE)
877 for (chain = surfchain;(surf = *chain) != NULL;chain++)
879 if (surf->visframe == r_framecount)
881 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
882 R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
887 for (chain = surfchain;(surf = *chain) != NULL;chain++)
888 if (surf->visframe == r_framecount)
889 RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
892 static void RSurfShader_Wall_Pass_BaseVertex(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
894 float base, colorscale;
897 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
898 memset(&m, 0, sizeof(m));
899 if (rendertype == SURFRENDER_ADD)
901 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
904 else if (rendertype == SURFRENDER_ALPHA)
906 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
911 GL_BlendFunc(GL_ONE, GL_ZERO);
914 m.tex[0] = R_GetTexture(texture->skin.base);
916 if (gl_combine.integer)
918 m.texrgbscale[0] = 4;
921 base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
923 GL_ColorPointer(varray_color4f);
925 GL_VertexPointer(surf->mesh.data_vertex3f);
926 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
927 R_Mesh_State_Texture(&m);
928 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
929 if (!(ent->effects & EF_FULLBRIGHT))
931 if (surf->dlightframe == r_framecount)
932 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
933 if (surf->flags & SURF_LIGHTMAP)
934 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);
936 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
937 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
940 static void RSurfShader_Wall_Pass_Glow(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
944 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
945 memset(&m, 0, sizeof(m));
946 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
949 m.tex[0] = R_GetTexture(texture->skin.glow);
950 GL_ColorPointer(varray_color4f);
952 GL_VertexPointer(surf->mesh.data_vertex3f);
954 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
955 R_Mesh_State_Texture(&m);
956 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
957 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
960 static void RSurfShader_Wall_Pass_Fog(const entity_render_t *ent, const msurface_t *surf, const texture_t *texture, int rendertype, float currentalpha)
964 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
965 memset(&m, 0, sizeof(m));
966 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
969 m.tex[0] = R_GetTexture(texture->skin.fog);
970 GL_ColorPointer(varray_color4f);
972 GL_VertexPointer(surf->mesh.data_vertex3f);
974 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
975 R_Mesh_State_Texture(&m);
976 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
977 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
980 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
982 const msurface_t *surf;
984 int lightmaptexturenum;
985 memset(&m, 0, sizeof(m));
986 GL_BlendFunc(GL_ONE, GL_ZERO);
989 m.tex[0] = R_GetTexture(texture->skin.base);
990 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
991 m.texrgbscale[1] = 2;
992 m.tex[2] = R_GetTexture(texture->skin.detail);
993 m.texrgbscale[2] = 2;
994 if (texture->skin.glow)
996 m.tex[3] = R_GetTexture(texture->skin.glow);
997 m.texcombinergb[3] = GL_ADD;
999 if (r_shadow_realtime_world.integer)
1000 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1002 GL_Color(1, 1, 1, 1);
1004 while((surf = *surfchain++) != NULL)
1006 if (surf->visframe == r_framecount)
1008 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1009 //if (m.tex[1] != lightmaptexturenum)
1011 m.tex[1] = lightmaptexturenum;
1012 // R_Mesh_State_Texture(&m);
1014 GL_VertexPointer(surf->mesh.data_vertex3f);
1015 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1016 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1017 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1018 m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
1019 R_Mesh_State_Texture(&m);
1020 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1025 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1027 const msurface_t *surf;
1029 int lightmaptexturenum;
1030 memset(&m, 0, sizeof(m));
1031 GL_BlendFunc(GL_ONE, GL_ZERO);
1034 m.tex[0] = R_GetTexture(texture->skin.base);
1035 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1036 m.texrgbscale[1] = 2;
1037 m.tex[2] = R_GetTexture(texture->skin.detail);
1038 m.texrgbscale[2] = 2;
1039 if (r_shadow_realtime_world.integer)
1040 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1042 GL_Color(1, 1, 1, 1);
1044 while((surf = *surfchain++) != NULL)
1046 if (surf->visframe == r_framecount)
1048 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1049 //if (m.tex[1] != lightmaptexturenum)
1051 m.tex[1] = lightmaptexturenum;
1052 // R_Mesh_State_Texture(&m);
1054 GL_VertexPointer(surf->mesh.data_vertex3f);
1055 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1056 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1057 m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1058 R_Mesh_State_Texture(&m);
1059 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1064 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1066 const msurface_t *surf;
1068 int lightmaptexturenum;
1069 memset(&m, 0, sizeof(m));
1070 GL_BlendFunc(GL_ONE, GL_ZERO);
1073 m.tex[0] = R_GetTexture(texture->skin.base);
1074 m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1075 m.texrgbscale[1] = 2;
1076 if (r_shadow_realtime_world.integer)
1077 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1079 GL_Color(1, 1, 1, 1);
1080 while((surf = *surfchain++) != NULL)
1082 if (surf->visframe == r_framecount)
1084 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1085 //if (m.tex[1] != lightmaptexturenum)
1087 m.tex[1] = lightmaptexturenum;
1088 // R_Mesh_State_Texture(&m);
1090 GL_VertexPointer(surf->mesh.data_vertex3f);
1091 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1092 m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1093 R_Mesh_State_Texture(&m);
1094 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1099 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1101 const msurface_t *surf;
1103 memset(&m, 0, sizeof(m));
1106 GL_BlendFunc(GL_ONE, GL_ZERO);
1107 m.tex[0] = R_GetTexture(texture->skin.base);
1108 if (r_shadow_realtime_world.integer)
1109 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1111 GL_Color(1, 1, 1, 1);
1112 while((surf = *surfchain++) != NULL)
1114 if (surf->visframe == r_framecount)
1116 GL_VertexPointer(surf->mesh.data_vertex3f);
1117 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1118 R_Mesh_State_Texture(&m);
1119 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1124 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1126 const msurface_t *surf;
1128 int lightmaptexturenum;
1129 memset(&m, 0, sizeof(m));
1130 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1131 GL_DepthMask(false);
1133 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1134 GL_Color(1, 1, 1, 1);
1135 while((surf = *surfchain++) != NULL)
1137 if (surf->visframe == r_framecount)
1139 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1140 //if (m.tex[0] != lightmaptexturenum)
1142 m.tex[0] = lightmaptexturenum;
1143 // R_Mesh_State_Texture(&m);
1145 GL_VertexPointer(surf->mesh.data_vertex3f);
1146 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1147 R_Mesh_State_Texture(&m);
1148 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1153 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1155 const msurface_t *surf;
1158 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1159 memset(&m, 0, sizeof(m));
1160 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1161 GL_DepthMask(false);
1163 GL_ColorPointer(varray_color4f);
1164 while((surf = *surfchain++) != NULL)
1166 if (surf->visframe == r_framecount)
1168 GL_VertexPointer(surf->mesh.data_vertex3f);
1170 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1171 R_Mesh_State_Texture(&m);
1172 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1173 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1178 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1180 const msurface_t *surf;
1182 memset(&m, 0, sizeof(m));
1183 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1184 GL_DepthMask(false);
1186 m.tex[0] = R_GetTexture(texture->skin.detail);
1187 GL_Color(1, 1, 1, 1);
1188 while((surf = *surfchain++) != NULL)
1190 if (surf->visframe == r_framecount)
1192 GL_VertexPointer(surf->mesh.data_vertex3f);
1193 m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1194 R_Mesh_State_Texture(&m);
1195 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1200 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1202 const msurface_t *surf;
1204 memset(&m, 0, sizeof(m));
1205 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1206 GL_DepthMask(false);
1208 m.tex[0] = R_GetTexture(texture->skin.glow);
1209 GL_Color(1, 1, 1, 1);
1210 while((surf = *surfchain++) != NULL)
1212 if (surf->visframe == r_framecount)
1214 GL_VertexPointer(surf->mesh.data_vertex3f);
1215 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1216 R_Mesh_State_Texture(&m);
1217 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1223 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1225 const msurface_t *surf;
1227 memset(&m, 0, sizeof(m));
1228 GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1230 m.tex[0] = R_GetTexture(texture->skin.glow);
1232 GL_Color(1, 1, 1, 1);
1234 GL_Color(0, 0, 0, 1);
1235 while((surf = *surfchain++) != NULL)
1237 if (surf->visframe == r_framecount)
1239 GL_VertexPointer(surf->mesh.data_vertex3f);
1240 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1241 R_Mesh_State_Texture(&m);
1242 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1248 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1250 const msurface_t *surf;
1252 int lightmaptexturenum;
1253 memset(&m, 0, sizeof(m));
1254 GL_BlendFunc(GL_ONE, GL_ZERO);
1257 m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1258 if (r_shadow_realtime_world.integer)
1259 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1261 GL_Color(1, 1, 1, 1);
1262 while((surf = *surfchain++) != NULL)
1264 if (surf->visframe == r_framecount)
1266 lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1267 //if (m.tex[0] != lightmaptexturenum)
1269 m.tex[0] = lightmaptexturenum;
1270 // R_Mesh_State_Texture(&m);
1272 GL_VertexPointer(surf->mesh.data_vertex3f);
1273 m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1274 R_Mesh_State_Texture(&m);
1275 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1280 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
1282 const entity_render_t *ent = calldata1;
1283 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
1287 R_Mesh_Matrix(&ent->matrix);
1289 texture = surf->texinfo->texture;
1290 if (texture->animated)
1291 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];
1293 currentalpha = ent->alpha;
1294 if (ent->effects & EF_ADDITIVE)
1295 rendertype = SURFRENDER_ADD;
1296 else if (currentalpha < 1 || texture->skin.fog != NULL)
1297 rendertype = SURFRENDER_ALPHA;
1299 rendertype = SURFRENDER_OPAQUE;
1301 RSurfShader_Wall_Pass_BaseVertex(ent, surf, texture, rendertype, currentalpha);
1302 if (texture->skin.glow)
1303 RSurfShader_Wall_Pass_Glow(ent, surf, texture, rendertype, currentalpha);
1305 RSurfShader_Wall_Pass_Fog(ent, surf, texture, rendertype, currentalpha);
1308 static void RSurfShader_Wall_Lightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1310 const msurface_t *surf;
1313 if (texture->rendertype != SURFRENDER_OPAQUE)
1315 // transparent vertex shaded from lightmap
1316 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1318 if (surf->visframe == r_framecount)
1320 Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1321 R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1325 else if (ent->effects & EF_FULLBRIGHT)
1327 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1328 if (r_detailtextures.integer)
1329 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1330 if (texture->skin.glow)
1331 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1333 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1336 // opaque base lighting
1337 else if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
1339 if (r_ambient.value > 0)
1343 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1345 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1348 // opaque lightmapped
1349 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1351 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1353 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1355 else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && !gl_lightmaps.integer)
1357 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1358 if (texture->skin.glow)
1359 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1361 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1363 else if (r_textureunits.integer >= 2 && gl_combine.integer && !gl_lightmaps.integer)
1365 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1366 if (r_detailtextures.integer)
1367 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1368 if (texture->skin.glow)
1369 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1371 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1373 else if (!gl_lightmaps.integer)
1375 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1376 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1377 if (r_detailtextures.integer)
1378 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1379 if (texture->skin.glow)
1380 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1382 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1386 RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1388 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1392 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1393 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1394 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1396 int Cshader_count = 3;
1397 Cshader_t *Cshaders[3] =
1399 &Cshader_wall_lightmap,
1404 void R_UpdateTextureInfo(entity_render_t *ent)
1406 int i, texframe, alttextures;
1412 alttextures = ent->frame != 0;
1413 texframe = (int)(cl.time * 5.0f);
1414 for (i = 0;i < ent->model->brushq1.numtextures;i++)
1416 t = ent->model->brushq1.textures + i;
1417 t->currentalpha = ent->alpha;
1418 if (t->flags & SURF_WATERALPHA)
1419 t->currentalpha *= r_wateralpha.value;
1420 if (ent->effects & EF_ADDITIVE)
1421 t->rendertype = SURFRENDER_ADD;
1422 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1423 t->rendertype = SURFRENDER_ALPHA;
1425 t->rendertype = SURFRENDER_OPAQUE;
1426 // we don't need to set currentframe if t->animated is false because
1427 // it was already set up by the texture loader for non-animating
1429 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1433 void R_PrepareSurfaces(entity_render_t *ent)
1435 int i, numsurfaces, *surfacevisframes;
1437 msurface_t *surf, *surfaces, **surfchain;
1444 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1445 numsurfaces = model->brushq1.nummodelsurfaces;
1446 surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1447 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1449 R_UpdateTextureInfo(ent);
1451 if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1454 if (model->brushq1.light_ambient != r_ambient.value)
1456 model->brushq1.light_ambient = r_ambient.value;
1457 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1458 model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1462 for (i = 0;i < model->brushq1.light_styles;i++)
1464 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1466 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1467 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1468 (**surfchain).cached_dlight = true;
1473 for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1475 if (surfacevisframes[i] == r_framecount)
1477 #if !WORLDNODECULLBACKFACES
1478 // mark any backface surfaces as not visible
1479 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1481 if (!(surf->flags & SURF_PLANEBACK))
1482 surfacevisframes[i] = -1;
1486 if ((surf->flags & SURF_PLANEBACK))
1487 surfacevisframes[i] = -1;
1489 if (surfacevisframes[i] == r_framecount)
1493 surf->visframe = r_framecount;
1494 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1495 R_BuildLightMap(ent, surf);
1501 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1505 if (ent->model == NULL)
1507 R_Mesh_Matrix(&ent->matrix);
1508 for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1509 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1510 t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1513 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1518 const entity_render_t *ent = calldata1;
1519 const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1520 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1521 GL_DepthMask(false);
1523 R_Mesh_Matrix(&ent->matrix);
1524 GL_VertexPointer(varray_vertex3f);
1526 memset(&m, 0, sizeof(m));
1527 R_Mesh_State_Texture(&m);
1529 i = portal - ent->model->brushq1.portals;
1530 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1531 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1532 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1534 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1536 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1537 VectorCopy(portal->points[i].position, v);
1540 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1541 VectorCopy(portal->points[i].position, v);
1542 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1545 // LordHavoc: this is just a nice debugging tool, very slow
1546 static void R_DrawPortals(entity_render_t *ent)
1549 mportal_t *portal, *endportal;
1550 float temp[3], center[3], f;
1551 if (ent->model == NULL)
1553 for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1555 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1558 for (i = 0;i < portal->numpoints;i++)
1559 VectorAdd(temp, portal->points[i].position, temp);
1560 f = ixtable[portal->numpoints];
1561 VectorScale(temp, f, temp);
1562 Matrix4x4_Transform(&ent->matrix, temp, center);
1563 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1568 void R_PrepareBrushModel(entity_render_t *ent)
1570 int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1573 #if WORLDNODECULLBACKFACES
1577 // because bmodels can be reused, we have to decide which things to render
1578 // from scratch every time
1582 #if WORLDNODECULLBACKFACES
1583 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1585 numsurfaces = model->brushq1.nummodelsurfaces;
1586 surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1587 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1588 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1589 for (i = 0;i < numsurfaces;i++, surf++)
1591 #if WORLDNODECULLBACKFACES
1592 // mark any backface surfaces as not visible
1593 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1595 if ((surf->flags & SURF_PLANEBACK))
1596 surfacevisframes[i] = r_framecount;
1598 else if (!(surf->flags & SURF_PLANEBACK))
1599 surfacevisframes[i] = r_framecount;
1601 surfacevisframes[i] = r_framecount;
1603 surf->dlightframe = -1;
1605 R_PrepareSurfaces(ent);
1608 void R_SurfaceWorldNode (entity_render_t *ent)
1610 int i, *surfacevisframes, *surfacepvsframes, surfnum;
1616 // equivilant to quake's RecursiveWorldNode but faster and more effective
1620 surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1621 surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1622 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1624 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1626 if (!R_CullBox (leaf->mins, leaf->maxs))
1629 leaf->visframe = r_framecount;
1633 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1635 surfnum = model->brushq1.pvssurflist[i];
1636 surf = model->brushq1.surfaces + surfnum;
1637 #if WORLDNODECULLBACKFACES
1638 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1640 if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1641 surfacevisframes[surfnum] = r_framecount;
1645 if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1646 surfacevisframes[surfnum] = r_framecount;
1649 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1650 surfacevisframes[surfnum] = r_framecount;
1655 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1657 int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1658 #if WORLDNODECULLBACKFACES
1662 mleaf_t *leaf, *leafstack[8192];
1665 msurface_t *surfaces;
1666 if (ent->model == NULL)
1668 // LordHavoc: portal-passage worldnode with PVS;
1669 // follows portals leading outward from viewleaf, does not venture
1670 // offscreen or into leafs that are not visible, faster than Quake's
1671 // RecursiveWorldNode
1672 surfaces = ent->model->brushq1.surfaces;
1673 surfacevisframes = ent->model->brushq1.surfacevisframes;
1674 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1675 viewleaf->worldnodeframe = r_framecount;
1676 leafstack[0] = viewleaf;
1678 while (leafstackpos)
1681 leaf = leafstack[--leafstackpos];
1682 leaf->visframe = r_framecount;
1683 // draw any surfaces bounding this leaf
1684 if (leaf->nummarksurfaces)
1686 for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1688 #if WORLDNODECULLBACKFACES
1690 if (surfacevisframes[n] != r_framecount)
1692 surf = surfaces + n;
1693 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1695 if ((surf->flags & SURF_PLANEBACK))
1696 surfacevisframes[n] = r_framecount;
1700 if (!(surf->flags & SURF_PLANEBACK))
1701 surfacevisframes[n] = r_framecount;
1705 surfacevisframes[*mark++] = r_framecount;
1709 // follow portals into other leafs
1710 for (p = leaf->portals;p;p = p->next)
1712 // LordHavoc: this DotProduct hurts less than a cache miss
1713 // (which is more likely to happen if backflowing through leafs)
1714 if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1717 if (leaf->worldnodeframe != r_framecount)
1719 leaf->worldnodeframe = r_framecount;
1720 // FIXME: R_CullBox is absolute, should be done relative
1721 bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1722 if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1723 leafstack[leafstackpos++] = leaf;
1730 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1732 int j, c, *surfacepvsframes, *mark;
1737 if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1739 model->brushq1.pvsframecount++;
1740 model->brushq1.pvsviewleaf = viewleaf;
1741 model->brushq1.pvsviewleafnovis = r_novis.integer;
1742 model->brushq1.pvsleafchain = NULL;
1743 model->brushq1.pvssurflistlength = 0;
1746 surfacepvsframes = model->brushq1.surfacepvsframes;
1747 for (j = 0;j < model->brushq1.visleafs;j++)
1749 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1751 leaf = model->brushq1.leafs + j + 1;
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.numleafs)
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, lightradius, 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, lightradius, 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 (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
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 (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
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->brushq3.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->brushq3.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->brushq3.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, lightradius, 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, lightradius, 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->brushq3.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);