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", "0"};
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 r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
41 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
43 static int dlightdivtable[32768];
45 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
47 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
49 float dist, impact[3], local[3];
54 smax = (surf->extents[0] >> 4) + 1;
55 tmax = (surf->extents[1] >> 4) + 1;
58 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
60 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
61 continue; // not lit by this light
63 Matrix4x4_Transform(matrix, light->origin, local);
64 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
66 // for comparisons to minimum acceptable light
67 // compensate for LIGHTOFFSET
68 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
75 if (surf->plane->type < 3)
77 VectorCopy(local, impact);
78 impact[surf->plane->type] -= dist;
82 impact[0] = local[0] - surf->plane->normal[0] * dist;
83 impact[1] = local[1] - surf->plane->normal[1] * dist;
84 impact[2] = local[2] - surf->plane->normal[2] * dist;
87 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
88 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
90 s = bound(0, impacts, smax * 16) - impacts;
91 t = bound(0, impactt, tmax * 16) - impactt;
92 i = s * s + t * t + dist2;
96 // reduce calculations
97 for (s = 0, i = impacts; s < smax; s++, i -= 16)
98 sdtable[s] = i * i + dist2;
100 maxdist3 = maxdist - dist2;
102 // convert to 8.8 blocklights format
103 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
104 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
105 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
106 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
110 for (t = 0;t < tmax;t++, i -= 16)
113 // make sure some part of it is visible on this line
116 maxdist2 = maxdist - td;
117 for (s = 0;s < smax;s++)
119 if (sdtable[s] < maxdist2)
121 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
125 bl[1] += (green * k);
140 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
142 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
143 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
148 smax = (surf->extents[0] >> 4) + 1;
149 tmax = (surf->extents[1] >> 4) + 1;
152 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
154 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155 continue; // not lit by this light
157 Matrix4x4_Transform(matrix, light->origin, local);
158 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
160 // for comparisons to minimum acceptable light
161 // compensate for LIGHTOFFSET
162 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
165 dist2 += LIGHTOFFSET;
166 if (dist2 >= maxdist)
169 if (surf->plane->type < 3)
171 VectorCopy(local, impact);
172 impact[surf->plane->type] -= dist;
176 impact[0] = local[0] - surf->plane->normal[0] * dist;
177 impact[1] = local[1] - surf->plane->normal[1] * dist;
178 impact[2] = local[2] - surf->plane->normal[2] * dist;
181 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
184 td = bound(0, impacts, smax * 16) - impacts;
185 td1 = bound(0, impactt, tmax * 16) - impactt;
186 td = td * td + td1 * td1 + dist2;
190 // reduce calculations
191 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192 sdtable[s] = td1 * td1 + dist2;
194 maxdist3 = maxdist - dist2;
196 // convert to 8.8 blocklights format
197 red = light->rtlight.lightmap_light[0];
198 green = light->rtlight.lightmap_light[1];
199 blue = light->rtlight.lightmap_light[2];
200 subtract = light->rtlight.lightmap_subtract * 32768.0f;
201 bl = floatblocklights;
204 for (t = 0;t < tmax;t++, td1 -= 16.0f)
207 // make sure some part of it is visible on this line
210 maxdist2 = maxdist - td;
211 for (s = 0;s < smax;s++)
213 if (sdtable[s] < maxdist2)
215 k = (32768.0f / (sdtable[s] + td)) - subtract;
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
238 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
240 if (!r_floatbuildlightmap.integer)
242 int smax, tmax, i, j, size, size3, maps, stride, l;
243 unsigned int *bl, scale;
244 qbyte *lightmap, *out, *stain;
246 // update cached lighting info
247 surf->cached_dlight = 0;
249 smax = (surf->extents[0]>>4)+1;
250 tmax = (surf->extents[1]>>4)+1;
253 lightmap = surf->samples;
255 // set to full bright if no light data
257 if (!ent->model->brushq1.lightdata)
259 for (i = 0;i < size3;i++)
265 memset(bl, 0, size*3*sizeof(unsigned int));
267 if (surf->dlightframe == r_framecount)
269 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
270 if (surf->cached_dlight)
274 // add all the lightmaps
278 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
279 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
280 bl[i] += lightmap[i] * scale;
284 stain = surf->stainsamples;
287 // the >> 16 shift adjusts down 8 bits to account for the stainmap
288 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
289 // be doubled during rendering to achieve 2x overbright
290 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
291 if (ent->model->brushq1.lightmaprgba)
293 stride = (surf->lightmaptexturestride - smax) * 4;
294 for (i = 0;i < tmax;i++, out += stride)
296 for (j = 0;j < smax;j++)
298 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
299 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
300 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
307 stride = (surf->lightmaptexturestride - smax) * 3;
308 for (i = 0;i < tmax;i++, out += stride)
310 for (j = 0;j < smax;j++)
312 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
319 R_UpdateTexture(surf->lightmaptexture, templight);
323 int smax, tmax, i, j, size, size3, maps, stride, l;
325 qbyte *lightmap, *out, *stain;
327 // update cached lighting info
328 surf->cached_dlight = 0;
330 smax = (surf->extents[0]>>4)+1;
331 tmax = (surf->extents[1]>>4)+1;
334 lightmap = surf->samples;
336 // set to full bright if no light data
337 bl = floatblocklights;
338 if (!ent->model->brushq1.lightdata)
340 for (i = 0;i < size3;i++)
345 memset(bl, 0, size*3*sizeof(float));
347 if (surf->dlightframe == r_framecount)
349 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
350 if (surf->cached_dlight)
354 // add all the lightmaps
357 bl = floatblocklights;
358 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
359 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
360 bl[i] += lightmap[i] * scale;
364 stain = surf->stainsamples;
365 bl = floatblocklights;
367 // this scaling adjusts down 8 bits to account for the stainmap
368 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
369 // be doubled during rendering to achieve 2x overbright
370 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
371 scale = 1.0f / (1 << 16);
372 if (ent->model->brushq1.lightmaprgba)
374 stride = (surf->lightmaptexturestride - smax) * 4;
375 for (i = 0;i < tmax;i++, out += stride)
377 for (j = 0;j < smax;j++)
379 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
380 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
381 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
388 stride = (surf->lightmaptexturestride - smax) * 3;
389 for (i = 0;i < tmax;i++, out += stride)
391 for (j = 0;j < smax;j++)
393 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
394 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
395 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
400 R_UpdateTexture(surf->lightmaptexture, templight);
404 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
406 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
407 msurface_t *surf, *endsurf;
408 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
412 maxdist = radius * radius;
413 invradius = 1.0f / radius;
418 ndist = PlaneDiff(origin, node->plane);
421 node = node->children[0];
426 node = node->children[1];
430 dist2 = ndist * ndist;
431 maxdist3 = maxdist - dist2;
433 if (node->plane->type < 3)
435 VectorCopy(origin, impact);
436 impact[node->plane->type] -= ndist;
440 impact[0] = origin[0] - node->plane->normal[0] * ndist;
441 impact[1] = origin[1] - node->plane->normal[1] * ndist;
442 impact[2] = origin[2] - node->plane->normal[2] * ndist;
445 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
447 if (surf->stainsamples)
449 smax = (surf->extents[0] >> 4) + 1;
450 tmax = (surf->extents[1] >> 4) + 1;
452 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
453 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
455 s = bound(0, impacts, smax * 16) - impacts;
456 t = bound(0, impactt, tmax * 16) - impactt;
457 i = s * s + t * t + dist2;
461 // reduce calculations
462 for (s = 0, i = impacts; s < smax; s++, i -= 16)
463 sdtable[s] = i * i + dist2;
465 bl = surf->stainsamples;
470 for (t = 0;t < tmax;t++, i -= 16)
473 // make sure some part of it is visible on this line
476 maxdist2 = maxdist - td;
477 for (s = 0;s < smax;s++)
479 if (sdtable[s] < maxdist2)
481 ratio = lhrandom(0.0f, 1.0f);
482 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
483 if (a >= (1.0f / 64.0f))
487 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
488 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
489 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
499 // force lightmap upload
501 surf->cached_dlight = true;
505 if (node->children[0]->plane)
507 if (node->children[1]->plane)
509 R_StainNode(node->children[0], model, origin, radius, fcolor);
510 node = node->children[1];
515 node = node->children[0];
519 else if (node->children[1]->plane)
521 node = node->children[1];
526 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)
530 entity_render_t *ent;
533 if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brush.data_nodes)
538 fcolor[3] = ca1 * (1.0f / 64.0f);
539 fcolor[4] = cr2 - cr1;
540 fcolor[5] = cg2 - cg1;
541 fcolor[6] = cb2 - cb1;
542 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
544 R_StainNode(r_refdef.worldmodel->brush.data_nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
546 // look for embedded bmodels
547 for (n = 0;n < cl_num_brushmodel_entities;n++)
549 ent = cl_brushmodel_entities[n];
551 if (model && model->name[0] == '*')
553 Mod_CheckLoaded(model);
554 if (model->brush.data_nodes)
556 Matrix4x4_Transform(&ent->inversematrix, origin, org);
557 R_StainNode(model->brush.data_nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
565 =============================================================
569 =============================================================
572 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
577 if (styles[0] != 255)
579 for (i = 0;i < numverts;i++, c += 4)
581 lm = samples + lightmapoffsets[i];
582 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
583 VectorMA(c, scale, lm, c);
584 if (styles[1] != 255)
587 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
588 VectorMA(c, scale, lm, c);
589 if (styles[2] != 255)
592 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
593 VectorMA(c, scale, lm, c);
594 if (styles[3] != 255)
597 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
598 VectorMA(c, scale, lm, c);
606 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
612 for (i = 0;i < numverts;i++, v += 3, c += 4)
614 VectorSubtract(v, modelorg, diff);
615 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
616 VectorScale(c, f, c);
619 else if (colorscale != 1)
620 for (i = 0;i < numverts;i++, c += 4)
621 VectorScale(c, colorscale, c);
624 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)
633 for (i = 0;i < numverts;i++, v += 3, c += 4)
635 VectorSubtract(v, modelorg, diff);
636 f = 1 - exp(fogdensity/DotProduct(diff, diff));
645 for (i = 0;i < numverts;i++, c += 4)
655 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)
662 for (i = 0;i < numverts;i++, v += 3, c += 4)
664 VectorSubtract(v, modelorg, diff);
665 f = exp(fogdensity/DotProduct(diff, diff));
673 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
678 int i, l, lit = false;
679 const dlight_t *light;
681 for (l = 0;l < r_numdlights;l++)
683 if (dlightbits[l >> 5] & (1 << (l & 31)))
685 light = &r_dlight[l];
686 Matrix4x4_Transform(matrix, light->origin, lightorigin);
687 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
689 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
690 if (f < light->rtlight.lightmap_cullradius2)
692 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
693 VectorMA(c, f, light->rtlight.lightmap_light, c);
702 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
704 const entity_render_t *ent = calldata1;
705 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
708 float base, colorscale;
711 float args[4] = {0.05f,0,0,0.04f};
712 int rendertype, turb, fullbright;
714 R_Mesh_Matrix(&ent->matrix);
715 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
717 texture = surf->texinfo->texture;
718 if (texture->animated)
719 texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (r_refdef.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
720 currentalpha = ent->alpha;
721 if (surf->flags & SURF_WATERALPHA)
722 currentalpha *= r_wateralpha.value;
724 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
725 if (ent->effects & EF_ADDITIVE)
727 rendertype = SURFRENDER_ADD;
728 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
731 else if (currentalpha < 1 || texture->skin.fog != NULL)
733 rendertype = SURFRENDER_ALPHA;
734 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
739 rendertype = SURFRENDER_OPAQUE;
740 GL_BlendFunc(GL_ONE, GL_ZERO);
741 GL_DepthMask(!(ent->effects & EF_NODEPTHTEST));
744 turb = (surf->flags & SURF_DRAWTURB) && r_waterscroll.value;
745 fullbright = !(ent->flags & RENDER_LIGHT) || (surf->flags & SURF_DRAWFULLBRIGHT) || !surf->samples;
746 base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
747 if (surf->flags & SURF_DRAWTURB)
749 if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled && fullbright && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
751 // NVIDIA Geforce3 distortion texture shader on water
752 GL_Color(1, 1, 1, currentalpha);
753 memset(&m, 0, sizeof(m));
754 m.pointer_vertex = surf->mesh.data_vertex3f;
755 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
756 m.tex[1] = R_GetTexture(texture->skin.base);
757 m.texcombinergb[0] = GL_REPLACE;
758 m.texcombinergb[1] = GL_REPLACE;
759 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
760 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
761 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
762 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
766 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
768 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
769 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
770 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
771 qglEnable(GL_TEXTURE_SHADER_NV);
773 GL_LockArrays(0, surf->mesh.num_vertices);
774 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
777 qglDisable(GL_TEXTURE_SHADER_NV);
778 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
783 memset(&m, 0, sizeof(m));
784 m.pointer_vertex = surf->mesh.data_vertex3f;
785 m.pointer_color = varray_color4f;
786 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
787 m.tex[0] = R_GetTexture(texture->skin.base);
790 // scrolling in texture matrix
791 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
794 if (gl_combine.integer)
796 m.texrgbscale[0] = 4;
799 R_FillColors(varray_color4f, surf->mesh.num_vertices, base * ent->colormod[0], base * ent->colormod[1], base * ent->colormod[2], currentalpha);
802 if (surf->dlightframe == r_framecount)
803 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
805 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);
807 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
809 GL_LockArrays(0, surf->mesh.num_vertices);
810 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
812 if (texture->skin.glow)
814 memset(&m, 0, sizeof(m));
815 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
817 m.pointer_color = varray_color4f;
818 m.tex[0] = R_GetTexture(texture->skin.glow);
819 m.pointer_vertex = surf->mesh.data_vertex3f;
822 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
825 // scrolling in texture matrix
826 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
830 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
831 GL_LockArrays(0, surf->mesh.num_vertices);
832 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
835 if (fogenabled && rendertype != SURFRENDER_ADD)
837 memset(&m, 0, sizeof(m));
838 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
840 m.pointer_color = varray_color4f;
841 m.tex[0] = R_GetTexture(texture->skin.fog);
842 m.pointer_vertex = surf->mesh.data_vertex3f;
845 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
848 // scrolling in texture matrix
849 Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(r_refdef.time) * 0.025 * r_waterscroll.value, sin(r_refdef.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
853 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
854 GL_LockArrays(0, surf->mesh.num_vertices);
855 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
861 void R_UpdateTextureInfo(entity_render_t *ent)
863 int i, texframe, alttextures;
869 alttextures = ent->frame != 0;
870 texframe = (int)(r_refdef.time * 5.0f);
871 for (i = 0;i < ent->model->brush.num_textures;i++)
873 t = ent->model->brush.data_textures + i;
874 t->currentalpha = ent->alpha;
875 if (t->flags & SURF_WATERALPHA)
876 t->currentalpha *= r_wateralpha.value;
877 if (ent->effects & EF_ADDITIVE)
878 t->rendertype = SURFRENDER_ADD;
879 else if (t->currentalpha < 1 || t->skin.fog != NULL)
880 t->rendertype = SURFRENDER_ALPHA;
882 t->rendertype = SURFRENDER_OPAQUE;
883 // we don't need to set currentframe if t->animated is false because
884 // it was already set up by the texture loader for non-animating
886 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
890 void R_DrawSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, msurface_t **texturesurfacelist)
892 int texturesurfaceindex;
893 vec3_t center, modelorg;
895 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
896 if (gl_lightmaps.integer)
898 GL_BlendFunc(GL_ONE, GL_ZERO);
901 GL_Color(1, 1, 1, 1);
902 memset(&m, 0, sizeof(m));
903 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
905 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
906 m.tex[0] = R_GetTexture(surface->lightmaptexture);
907 m.pointer_vertex = surface->mesh.data_vertex3f;
908 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
910 GL_LockArrays(0, surface->mesh.num_vertices);
911 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
915 else if (texture->rendertype != SURFRENDER_OPAQUE)
917 // transparent vertex shaded from lightmap
918 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
920 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
921 Matrix4x4_Transform(&ent->matrix, surface->poly_center, center);
922 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brushq1.surfaces);
925 else if (texture->flags & SURF_LIGHTMAP)
927 qboolean dolightmap = (ent->flags & RENDER_LIGHT);
928 qboolean dobase = true;
929 qboolean doambient = r_ambient.value > 0;
930 qboolean dodetail = r_detailtextures.integer != 0;
931 qboolean doglow = texture->skin.glow != NULL;
932 qboolean dofog = fogenabled;
933 // multitexture cases
934 if (r_textureunits.integer >= 2 && gl_combine.integer && dobase && dolightmap)
938 GL_BlendFunc(GL_ONE, GL_ZERO);
941 GL_Color(1, 1, 1, 1);
942 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
943 memset(&m, 0, sizeof(m));
944 m.tex[0] = R_GetTexture(texture->skin.base);
945 m.texrgbscale[1] = 2;
946 if (r_textureunits.integer >= 3 && !doambient && dodetail)
948 m.tex[2] = R_GetTexture(texture->skin.detail);
949 m.texrgbscale[2] = 2;
951 if (r_textureunits.integer >= 3 && texture->skin.glow)
953 m.tex[3] = R_GetTexture(texture->skin.glow);
954 m.texcombinergb[3] = GL_ADD;
956 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
958 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
959 m.tex[1] = R_GetTexture(surface->lightmaptexture);
960 m.pointer_vertex = surface->mesh.data_vertex3f;
961 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
962 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
963 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
964 m.pointer_texcoord[3] = surface->mesh.data_texcoordtexture2f;
966 GL_LockArrays(0, surface->mesh.num_vertices);
967 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
973 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
975 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
976 m.tex[1] = R_GetTexture(surface->lightmaptexture);
977 m.pointer_vertex = surface->mesh.data_vertex3f;
978 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
979 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
980 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
982 GL_LockArrays(0, surface->mesh.num_vertices);
983 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
988 else if (r_textureunits.integer >= 3 && !doambient && !dodetail && doglow)
990 m.tex[2] = R_GetTexture(texture->skin.glow);
991 m.texcombinergb[2] = GL_ADD;
993 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
995 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
996 m.tex[1] = R_GetTexture(surface->lightmaptexture);
997 m.pointer_vertex = surface->mesh.data_vertex3f;
998 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
999 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1000 m.pointer_texcoord[2] = surface->mesh.data_texcoordtexture2f;
1002 GL_LockArrays(0, surface->mesh.num_vertices);
1003 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1004 GL_LockArrays(0, 0);
1008 // anything not handled above
1011 GL_BlendFunc(GL_ONE, GL_ZERO);
1014 GL_Color(1, 1, 1, 1);
1015 if (ent->flags & RENDER_LIGHT)
1016 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1018 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1019 memset(&m, 0, sizeof(m));
1020 m.tex[0] = R_GetTexture(texture->skin.base);
1021 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1023 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1024 m.pointer_vertex = surface->mesh.data_vertex3f;
1025 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1027 GL_LockArrays(0, surface->mesh.num_vertices);
1028 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1029 GL_LockArrays(0, 0);
1032 GL_DepthMask(false);
1035 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1036 GL_DepthMask(false);
1038 GL_Color(1, 1, 1, 1);
1039 memset(&m, 0, sizeof(m));
1040 m.tex[0] = R_GetTexture(texture->skin.base);
1041 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1043 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1044 m.tex[0] = R_GetTexture(surface->lightmaptexture);
1045 m.pointer_vertex = surface->mesh.data_vertex3f;
1046 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1048 GL_LockArrays(0, surface->mesh.num_vertices);
1049 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1050 GL_LockArrays(0, 0);
1055 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1056 GL_DepthMask(false);
1058 memset(&m, 0, sizeof(m));
1059 GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
1060 m.tex[0] = R_GetTexture(texture->skin.base);
1061 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1063 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1064 m.pointer_vertex = surface->mesh.data_vertex3f;
1065 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1067 GL_LockArrays(0, surface->mesh.num_vertices);
1068 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1069 GL_LockArrays(0, 0);
1074 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1075 GL_DepthMask(false);
1077 GL_Color(1, 1, 1, 1);
1078 memset(&m, 0, sizeof(m));
1079 m.tex[0] = R_GetTexture(texture->skin.detail);
1080 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1082 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1083 m.pointer_vertex = surface->mesh.data_vertex3f;
1084 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1086 GL_LockArrays(0, surface->mesh.num_vertices);
1087 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1088 GL_LockArrays(0, 0);
1093 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1094 GL_DepthMask(false);
1096 GL_Color(1, 1, 1, 1);
1097 memset(&m, 0, sizeof(m));
1098 m.tex[0] = R_GetTexture(texture->skin.glow);
1099 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1101 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1102 m.pointer_vertex = surface->mesh.data_vertex3f;
1103 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1105 GL_LockArrays(0, surface->mesh.num_vertices);
1106 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1107 GL_LockArrays(0, 0);
1112 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1113 GL_DepthMask(false);
1115 memset(&m, 0, sizeof(m));
1116 m.pointer_color = varray_color4f;
1117 m.tex[0] = R_GetTexture(texture->skin.glow);
1118 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1120 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1121 m.pointer_vertex = surface->mesh.data_vertex3f;
1123 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1125 RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surface->mesh.num_vertices, modelorg);
1126 GL_LockArrays(0, surface->mesh.num_vertices);
1127 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1128 GL_LockArrays(0, 0);
1132 else if (texture->flags & SURF_DRAWTURB)
1134 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1136 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1137 RSurfShader_Transparent_Callback(ent, surface - ent->model->brushq1.surfaces);
1140 else if (texture->flags & SURF_DRAWSKY)
1144 skyrendernow = false;
1145 if (skyrendermasked)
1148 // LordHavoc: HalfLife maps have freaky skypolys...
1149 if (!ent->model->brush.ishlbsp)
1151 R_Mesh_Matrix(&ent->matrix);
1152 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1153 if (skyrendermasked)
1155 // depth-only (masking)
1156 GL_ColorMask(0,0,0,0);
1157 // just to make sure that braindead drivers don't draw anything
1158 // despite that colormask...
1159 GL_BlendFunc(GL_ZERO, GL_ONE);
1164 GL_BlendFunc(GL_ONE, GL_ZERO);
1168 memset(&m, 0, sizeof(m));
1169 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1171 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1172 m.pointer_vertex = surface->mesh.data_vertex3f;
1174 GL_LockArrays(0, surface->mesh.num_vertices);
1175 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1176 GL_LockArrays(0, 0);
1178 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1183 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1185 int i, j, f, flagsmask;
1186 msurface_t *surface, **surfacechain;
1187 texture_t *t, *texture;
1188 model_t *model = ent->model;
1190 const int maxsurfacelist = 1024;
1191 int numsurfacelist = 0;
1192 msurface_t *surfacelist[1024];
1195 R_Mesh_Matrix(&ent->matrix);
1196 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1198 if (ent != r_refdef.worldentity)
1200 // because bmodels can be reused, we have to clear dlightframe every time
1201 surface = model->brushq1.surfaces + model->firstmodelsurface;
1202 for (i = 0;i < model->nummodelsurfaces;i++, surface++)
1203 surface->dlightframe = -1;
1206 // update light styles
1209 if (r_dynamic.integer && !r_rtdlight)
1211 for (i = 0;i < model->brushq1.light_styles;i++)
1213 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1215 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1216 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1217 for (;(surface = *surfacechain);surfacechain++)
1218 surface->cached_dlight = true;
1223 R_UpdateTextureInfo(ent);
1224 flagsmask = skysurfaces ? SURF_DRAWSKY : (SURF_DRAWTURB | SURF_LIGHTMAP);
1228 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
1230 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
1232 surface = model->brushq1.surfaces + j;
1233 if (t != surface->texinfo->texture)
1237 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1240 t = surface->texinfo->texture;
1241 f = t->flags & flagsmask;
1242 texture = t->currentframe;
1246 // add face to draw list and update lightmap if necessary
1248 if (surface->cached_dlight && surface->lightmaptexture != NULL)
1249 R_BuildLightMap(ent, surface);
1250 surfacelist[numsurfacelist++] = surface;
1251 if (numsurfacelist >= maxsurfacelist)
1253 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1260 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1263 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1268 const mportal_t *portal = calldata1;
1269 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1270 GL_DepthMask(false);
1272 R_Mesh_Matrix(&r_identitymatrix);
1274 memset(&m, 0, sizeof(m));
1275 m.pointer_vertex = varray_vertex3f;
1279 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1280 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1281 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1283 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1285 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1286 VectorCopy(portal->points[i].position, v);
1289 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1290 VectorCopy(portal->points[i].position, v);
1291 GL_LockArrays(0, portal->numpoints);
1292 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1293 GL_LockArrays(0, 0);
1296 // LordHavoc: this is just a nice debugging tool, very slow
1297 static void R_DrawPortals(void)
1302 model_t *model = r_refdef.worldmodel;
1305 for (portalnum = 0, portal = model->brush.data_portals;portalnum < model->brush.num_portals;portalnum++, portal++)
1307 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1308 if (!R_CullBox(portal->mins, portal->maxs))
1310 VectorClear(center);
1311 for (i = 0;i < portal->numpoints;i++)
1312 VectorAdd(center, portal->points[i].position, center);
1313 f = ixtable[portal->numpoints];
1314 VectorScale(center, f, center);
1315 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1320 void R_WorldVisibility(void)
1322 model_t *model = r_refdef.worldmodel;
1327 if (model->type == mod_brushq3)
1331 memset(r_worldsurfacevisible, 0, r_refdef.worldmodel->brushq3.num_faces);
1332 for (j = 0, leaf = r_refdef.worldmodel->brush.data_leafs;j < r_refdef.worldmodel->brush.num_leafs;j++, leaf++)
1334 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1337 for (i = 0;i < leaf->numleaffaces;i++)
1338 r_worldsurfacevisible[leaf->firstleafface[i]] = 1;
1342 else if (model->type == mod_brushq1)
1349 mleaf_t *leafstack[8192];
1350 qbyte leafvisited[32768];
1352 viewleaf = model->brushq1.PointInLeaf(model, r_vieworigin);
1356 memset(r_worldsurfacevisible, 0, r_refdef.worldmodel->brushq1.numsurfaces);
1357 if (viewleaf->clusterindex < 0 || r_surfaceworldnode.integer)
1359 // equivilant to quake's RecursiveWorldNode but faster and more effective
1360 for (j = 0, leaf = model->brush.data_leafs;j < model->brush.num_leafs;j++, leaf++)
1362 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox (leaf->mins, leaf->maxs))
1365 if (leaf->numleaffaces)
1366 for (i = 0, mark = leaf->firstleafface;i < leaf->numleaffaces;i++, mark++)
1367 r_worldsurfacevisible[*mark] = true;
1373 // LordHavoc: portal-passage worldnode with PVS;
1374 // follows portals leading outward from viewleaf, does not venture
1375 // offscreen or into leafs that are not visible, faster than Quake's
1376 // RecursiveWorldNode
1377 leafstack[0] = viewleaf;
1379 memset(leafvisited, 0, r_refdef.worldmodel->brush.num_leafs);
1380 while (leafstackpos)
1383 leaf = leafstack[--leafstackpos];
1384 leafvisited[leaf - r_refdef.worldmodel->brush.data_leafs] = 1;
1385 // draw any surfaces bounding this leaf
1386 if (leaf->numleaffaces)
1387 for (i = 0, mark = leaf->firstleafface;i < leaf->numleaffaces;i++, mark++)
1388 r_worldsurfacevisible[*mark] = true;
1389 // follow portals into other leafs
1390 for (p = leaf->portals;p;p = p->next)
1391 if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1) && !leafvisited[p->past - r_refdef.worldmodel->brush.data_leafs] && CHECKPVSBIT(r_pvsbits, p->past->clusterindex) && !R_CullBox(p->mins, p->maxs))
1392 leafstack[leafstackpos++] = p->past;
1396 if (r_drawportals.integer)
1401 void R_Q1BSP_DrawSky(entity_render_t *ent)
1403 if (ent->model == NULL)
1405 R_DrawSurfaces(ent, true);
1408 void R_Q1BSP_Draw(entity_render_t *ent)
1410 if (ent->model == NULL)
1413 R_DrawSurfaces(ent, false);
1416 void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
1418 model_t *model = ent->model;
1419 vec3_t lightmins, lightmaxs;
1420 int t, leafindex, leaffaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1423 msurface_t *surface;
1426 lightmins[0] = relativelightorigin[0] - lightradius;
1427 lightmins[1] = relativelightorigin[1] - lightradius;
1428 lightmins[2] = relativelightorigin[2] - lightradius;
1429 lightmaxs[0] = relativelightorigin[0] + lightradius;
1430 lightmaxs[1] = relativelightorigin[1] + lightradius;
1431 lightmaxs[2] = relativelightorigin[2] + lightradius;
1432 *outnumclusterspointer = 0;
1433 *outnumsurfacespointer = 0;
1434 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1435 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1438 VectorCopy(lightmins, outmins);
1439 VectorCopy(lightmaxs, outmaxs);
1442 VectorCopy(relativelightorigin, outmins);
1443 VectorCopy(relativelightorigin, outmaxs);
1444 if (model->brush.GetPVS)
1445 pvs = model->brush.GetPVS(model, relativelightorigin);
1448 // FIXME: use BSP recursion as lights are often small
1449 for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
1451 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1453 outmins[0] = min(outmins[0], leaf->mins[0]);
1454 outmins[1] = min(outmins[1], leaf->mins[1]);
1455 outmins[2] = min(outmins[2], leaf->mins[2]);
1456 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1457 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1458 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1461 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1463 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1464 outclusterlist[outnumclusters++] = leaf->clusterindex;
1469 for (leaffaceindex = 0;leaffaceindex < leaf->numleaffaces;leaffaceindex++)
1471 surfaceindex = leaf->firstleafface[leaffaceindex];
1472 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1474 surface = model->brushq1.surfaces + surfaceindex;
1475 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1477 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1479 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1480 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1481 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1482 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
1484 SETPVSBIT(outsurfacepvs, surfaceindex);
1485 outsurfacelist[outnumsurfaces++] = surfaceindex;
1496 // limit combined leaf box to light boundaries
1497 outmins[0] = max(outmins[0], lightmins[0]);
1498 outmins[1] = max(outmins[1], lightmins[1]);
1499 outmins[2] = max(outmins[2], lightmins[2]);
1500 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1501 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1502 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1504 *outnumclusterspointer = outnumclusters;
1505 *outnumsurfacespointer = outnumsurfaces;
1508 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1510 model_t *model = ent->model;
1511 vec3_t lightmins, lightmaxs;
1512 msurface_t *surface;
1513 int surfacelistindex;
1514 if (r_drawcollisionbrushes.integer < 2)
1516 lightmins[0] = relativelightorigin[0] - lightradius;
1517 lightmins[1] = relativelightorigin[1] - lightradius;
1518 lightmins[2] = relativelightorigin[2] - lightradius;
1519 lightmaxs[0] = relativelightorigin[0] + lightradius;
1520 lightmaxs[1] = relativelightorigin[1] + lightradius;
1521 lightmaxs[2] = relativelightorigin[2] + lightradius;
1522 R_Mesh_Matrix(&ent->matrix);
1523 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1524 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1526 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1527 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->mesh.num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs);
1529 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
1533 void R_Q1BSP_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, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
1535 model_t *model = ent->model;
1536 vec3_t lightmins, lightmaxs, modelorg;
1537 msurface_t *surface;
1539 int surfacelistindex;
1540 if (r_drawcollisionbrushes.integer < 2)
1542 lightmins[0] = relativelightorigin[0] - lightradius;
1543 lightmins[1] = relativelightorigin[1] - lightradius;
1544 lightmins[2] = relativelightorigin[2] - lightradius;
1545 lightmaxs[0] = relativelightorigin[0] + lightradius;
1546 lightmaxs[1] = relativelightorigin[1] + lightradius;
1547 lightmaxs[2] = relativelightorigin[2] + lightradius;
1548 R_Mesh_Matrix(&ent->matrix);
1549 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1550 R_UpdateTextureInfo(ent);
1551 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1553 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1554 if (r_shadow_compilingrtlight)
1556 // if compiling an rtlight, capture the mesh
1557 t = surface->texinfo->texture;
1558 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1559 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texinfo->texture->skin.base, surface->texinfo->texture->skin.gloss, surface->texinfo->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
1561 else if (ent != r_refdef.worldentity || r_worldsurfacevisible[surface - ent->model->brushq1.surfaces])
1563 t = surface->texinfo->texture->currentframe;
1564 // FIXME: transparent surfaces need to be lit later
1565 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1566 R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, t->skin.base, t->skin.nmap, t->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
1572 void R_DrawCollisionBrush(colbrushf_t *brush)
1576 memset(&m, 0, sizeof(m));
1577 m.pointer_vertex = brush->points->v;
1579 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1580 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1581 GL_LockArrays(0, brush->numpoints);
1582 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1583 GL_LockArrays(0, 0);
1586 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1590 if (!face->mesh.num_collisiontriangles)
1592 memset(&m, 0, sizeof(m));
1593 m.pointer_vertex = face->mesh.data_collisionvertex3f;
1595 i = (int)(((size_t)face) / sizeof(q3msurface_t));
1596 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1597 GL_LockArrays(0, face->mesh.num_collisionvertices);
1598 R_Mesh_Draw(face->mesh.num_collisionvertices, face->mesh.num_collisiontriangles, face->mesh.data_collisionelement3i);
1599 GL_LockArrays(0, 0);
1602 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1604 const entity_render_t *ent = voident;
1605 q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
1607 R_Mesh_Matrix(&ent->matrix);
1608 memset(&m, 0, sizeof(m));
1609 if ((ent->effects & EF_ADDITIVE) || (face->texture->textureflags & Q3TEXTUREFLAG_ADDITIVE))
1610 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1612 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1613 GL_DepthMask(false);
1614 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
1615 m.tex[0] = R_GetTexture(face->texture->skin.base);
1616 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1617 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1618 if (gl_combine.integer)
1620 m.texrgbscale[0] = 2;
1621 if (r_textureunits.integer >= 2)
1623 m.tex[1] = R_GetTexture(face->lightmaptexture);
1624 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1625 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha);
1629 if (ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1 && ent->alpha == 1)
1630 m.pointer_color = face->mesh.data_lightmapcolor4f;
1634 for (i = 0;i < face->mesh.num_vertices;i++)
1636 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0];
1637 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1];
1638 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2];
1639 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1641 m.pointer_color = varray_color4f;
1648 for (i = 0;i < face->mesh.num_vertices;i++)
1650 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * 2.0f;
1651 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * 2.0f;
1652 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * 2.0f;
1653 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1655 m.pointer_color = varray_color4f;
1657 if (face->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1660 float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
1661 matrix4x4_t matrix1, imatrix1;
1662 R_Mesh_Matrix(&r_identitymatrix);
1663 // a single autosprite surface can contain multiple sprites...
1664 for (j = 0;j < face->mesh.num_vertices - 3;j += 4)
1666 VectorClear(center);
1667 for (i = 0;i < 4;i++)
1668 VectorAdd(center, face->mesh.data_vertex3f + (j+i) * 3, center);
1669 VectorScale(center, 0.25f, center);
1670 Matrix4x4_Transform(&ent->matrix, center, center2);
1671 // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
1672 Matrix4x4_FromVectors(&matrix1, face->mesh.data_normal3f + j*3, face->mesh.data_svector3f + j*3, face->mesh.data_tvector3f + j*3, center);
1673 Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
1674 for (i = 0;i < 4;i++)
1675 Matrix4x4_Transform(&imatrix1, face->mesh.data_vertex3f + (j+i)*3, v[i]);
1676 if (face->texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
1678 forward[0] = r_vieworigin[0] - center2[0];
1679 forward[1] = r_vieworigin[1] - center2[1];
1681 VectorNormalize(forward);
1682 right[0] = forward[1];
1683 right[1] = -forward[0];
1691 VectorCopy(r_viewforward, forward);
1692 VectorCopy(r_viewright, right);
1693 VectorCopy(r_viewup, up);
1695 for (i = 0;i < 4;i++)
1696 VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
1698 m.pointer_vertex = varray_vertex3f;
1701 m.pointer_vertex = face->mesh.data_vertex3f;
1703 if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1704 qglDisable(GL_CULL_FACE);
1705 GL_LockArrays(0, face->mesh.num_vertices);
1706 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1707 GL_LockArrays(0, 0);
1708 if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1709 qglEnable(GL_CULL_FACE);
1712 void R_Q3BSP_DrawFaceList(entity_render_t *ent, texture_t *t, int texturenumfaces, q3msurface_t **texturefacelist)
1714 int i, texturefaceindex;
1715 qboolean dolightmap;
1721 if (!texturenumfaces)
1723 c_faces += texturenumfaces;
1724 // gl_lightmaps debugging mode skips normal texturing
1725 if (gl_lightmaps.integer)
1729 GL_BlendFunc(GL_ONE, GL_ZERO);
1730 qglDisable(GL_CULL_FACE);
1731 memset(&m, 0, sizeof(m));
1732 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1734 q3msurface_t *face = texturefacelist[texturefaceindex];
1735 m.tex[0] = R_GetTexture(face->lightmaptexture);
1736 m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1737 if (face->lightmaptexture)
1739 GL_Color(1, 1, 1, 1);
1740 m.pointer_color = NULL;
1743 m.pointer_color = face->mesh.data_lightmapcolor4f;
1744 m.pointer_vertex = face->mesh.data_vertex3f;
1746 GL_LockArrays(0, face->mesh.num_vertices);
1747 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1748 GL_LockArrays(0, 0);
1750 qglEnable(GL_CULL_FACE);
1753 // transparent surfaces get sorted for later drawing
1754 if ((t->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
1756 vec3_t facecenter, center;
1757 // drawing sky transparently would be too difficult
1758 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1760 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1762 q3msurface_t *face = texturefacelist[texturefaceindex];
1763 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
1764 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
1765 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
1766 Matrix4x4_Transform(&ent->matrix, facecenter, center);
1767 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
1771 // sky surfaces draw sky if needed and render themselves as a depth mask
1772 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1776 skyrendernow = false;
1777 if (skyrendermasked)
1780 if (!r_q3bsp_renderskydepth.integer)
1783 R_Mesh_Matrix(&ent->matrix);
1785 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1786 if (skyrendermasked)
1788 // depth-only (masking)
1789 GL_ColorMask(0,0,0,0);
1790 // just to make sure that braindead drivers don't draw anything
1791 // despite that colormask...
1792 GL_BlendFunc(GL_ZERO, GL_ONE);
1797 GL_BlendFunc(GL_ONE, GL_ZERO);
1802 memset(&m, 0, sizeof(m));
1803 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1805 q3msurface_t *face = texturefacelist[texturefaceindex];
1806 m.pointer_vertex = face->mesh.data_vertex3f;
1808 GL_LockArrays(0, face->mesh.num_vertices);
1809 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1810 GL_LockArrays(0, 0);
1812 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1815 // anything else is a typical wall, lightmap * texture + glow
1816 dolightmap = (ent->flags & RENDER_LIGHT);
1818 doambient = r_ambient.value > 0;
1819 doglow = t->skin.glow != NULL;
1821 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1822 qglDisable(GL_CULL_FACE);
1823 if (!dolightmap && dobase)
1829 GL_BlendFunc(GL_ONE, GL_ZERO);
1830 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1831 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1832 qglDisable(GL_CULL_FACE);
1833 memset(&m, 0, sizeof(m));
1834 m.tex[0] = R_GetTexture(t->skin.base);
1835 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1837 q3msurface_t *face = texturefacelist[texturefaceindex];
1838 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1839 m.pointer_vertex = face->mesh.data_vertex3f;
1841 GL_LockArrays(0, face->mesh.num_vertices);
1842 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1843 GL_LockArrays(0, 0);
1846 if (r_lightmapintensity <= 0 && dolightmap && dobase)
1852 GL_BlendFunc(GL_ONE, GL_ZERO);
1853 GL_Color(0, 0, 0, 1);
1854 memset(&m, 0, sizeof(m));
1855 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1857 q3msurface_t *face = texturefacelist[texturefaceindex];
1858 m.pointer_vertex = face->mesh.data_vertex3f;
1860 GL_LockArrays(0, face->mesh.num_vertices);
1861 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1862 GL_LockArrays(0, 0);
1865 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
1867 // dualtexture combine
1872 GL_BlendFunc(GL_ONE, GL_ZERO);
1873 memset(&m, 0, sizeof(m));
1874 m.tex[0] = R_GetTexture(t->skin.base);
1875 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1876 m.pointer_color = NULL;
1877 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1879 q3msurface_t *face = texturefacelist[texturefaceindex];
1880 if (!face->lightmaptexture)
1882 m.tex[1] = R_GetTexture(face->lightmaptexture);
1883 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1884 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1885 m.texrgbscale[1] = 2;
1886 m.pointer_vertex = face->mesh.data_vertex3f;
1888 GL_LockArrays(0, face->mesh.num_vertices);
1889 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1890 GL_LockArrays(0, 0);
1892 if (r_lightmapintensity == 1 && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
1894 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1896 q3msurface_t *face = texturefacelist[texturefaceindex];
1897 if (face->lightmaptexture)
1899 m.tex[1] = R_GetTexture(face->lightmaptexture);
1900 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1901 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1902 m.texrgbscale[1] = 2;
1903 m.pointer_color = face->mesh.data_lightmapcolor4f;
1904 m.pointer_vertex = face->mesh.data_vertex3f;
1906 GL_LockArrays(0, face->mesh.num_vertices);
1907 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1908 GL_LockArrays(0, 0);
1913 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1915 q3msurface_t *face = texturefacelist[texturefaceindex];
1916 if (face->lightmaptexture)
1918 m.tex[1] = R_GetTexture(face->lightmaptexture);
1919 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1920 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1921 m.texrgbscale[1] = 2;
1922 m.pointer_color = varray_color4f;
1923 for (i = 0;i < face->mesh.num_vertices;i++)
1925 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * r_lightmapintensity;
1926 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * r_lightmapintensity;
1927 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * r_lightmapintensity;
1928 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3];
1930 m.pointer_vertex = face->mesh.data_vertex3f;
1932 GL_LockArrays(0, face->mesh.num_vertices);
1933 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1934 GL_LockArrays(0, 0);
1943 GL_BlendFunc(GL_ONE, GL_ZERO);
1944 memset(&m, 0, sizeof(m));
1945 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1947 q3msurface_t *face = texturefacelist[texturefaceindex];
1948 m.tex[0] = R_GetTexture(face->lightmaptexture);
1949 m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1950 if (face->lightmaptexture)
1951 m.pointer_color = NULL;
1953 m.pointer_color = face->mesh.data_lightmapcolor4f;
1954 m.pointer_vertex = face->mesh.data_vertex3f;
1956 GL_LockArrays(0, face->mesh.num_vertices);
1957 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1958 GL_LockArrays(0, 0);
1963 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1964 GL_DepthMask(false);
1966 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1967 memset(&m, 0, sizeof(m));
1968 m.tex[0] = R_GetTexture(t->skin.base);
1969 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1971 q3msurface_t *face = texturefacelist[texturefaceindex];
1972 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1973 m.pointer_vertex = face->mesh.data_vertex3f;
1975 GL_LockArrays(0, face->mesh.num_vertices);
1976 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1977 GL_LockArrays(0, 0);
1982 GL_BlendFunc(GL_ONE, GL_ONE);
1983 GL_DepthMask(false);
1985 GL_Color(r_ambient.value * (1.0f / 128.0f) * ent->colormod[0], r_ambient.value * (1.0f / 128.0f) * ent->colormod[1], r_ambient.value * (1.0f / 128.0f) * ent->colormod[2], 1);
1986 memset(&m, 0, sizeof(m));
1987 m.tex[0] = R_GetTexture(t->skin.base);
1988 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1990 q3msurface_t *face = texturefacelist[texturefaceindex];
1991 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1992 m.pointer_vertex = face->mesh.data_vertex3f;
1994 GL_LockArrays(0, face->mesh.num_vertices);
1995 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1996 GL_LockArrays(0, 0);
2001 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2002 GL_DepthMask(false);
2004 GL_Color(1, 1, 1, 1);
2005 memset(&m, 0, sizeof(m));
2006 m.tex[0] = R_GetTexture(t->skin.glow);
2007 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2009 q3msurface_t *face = texturefacelist[texturefaceindex];
2010 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2011 m.pointer_vertex = face->mesh.data_vertex3f;
2013 GL_LockArrays(0, face->mesh.num_vertices);
2014 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2015 GL_LockArrays(0, 0);
2021 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2022 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2023 GL_DepthMask(false);
2025 GL_Color(1, 1, 1, 1);
2026 memset(&m, 0, sizeof(m));
2027 m.tex[0] = R_GetTexture(t->skin.fog);
2028 m.pointer_color = varray_color4f;
2029 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2031 q3msurface_t *face = texturefacelist[texturefaceindex];
2033 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2034 m.pointer_vertex = face->mesh.data_vertex3f;
2036 RSurf_FogPassColors_Vertex3f_Color4f(face->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, face->mesh.num_vertices, modelorg);
2037 GL_LockArrays(0, face->mesh.num_vertices);
2038 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2039 GL_LockArrays(0, 0);
2042 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2043 qglEnable(GL_CULL_FACE);
2046 void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
2048 int i, j, f, flagsmask, flags;
2050 model_t *model = ent->model;
2052 const int maxfaces = 1024;
2054 q3msurface_t *facelist[1024];
2055 R_Mesh_Matrix(&ent->matrix);
2056 flagsmask = Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY;
2058 flags = Q3SURFACEFLAG_SKY;
2064 for (i = 0, j = model->firstmodelsurface;i < model->nummodelsurfaces;i++, j++)
2066 if (ent != r_refdef.worldentity || r_worldsurfacevisible[j])
2068 face = model->brushq3.data_faces + j;
2069 if (t != face->texture)
2073 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2077 f = t->surfaceflags & flagsmask;
2081 if (!face->mesh.num_triangles)
2083 facelist[numfaces++] = face;
2084 if (numfaces >= maxfaces)
2086 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2093 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2096 void R_Q3BSP_DrawSky(entity_render_t *ent)
2098 if (r_drawcollisionbrushes.integer < 2)
2099 R_Q3BSP_DrawFaces(ent, true);
2102 void R_Q3BSP_Draw(entity_render_t *ent)
2104 if (r_drawcollisionbrushes.integer < 2)
2105 R_Q3BSP_DrawFaces(ent, false);
2106 if (r_drawcollisionbrushes.integer >= 1)
2109 model_t *model = ent->model;
2111 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2112 GL_DepthMask(false);
2114 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2115 for (i = 0;i < model->brushq3.data_models[model->brush.submodel].numbrushes;i++)
2116 if (model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf && model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf->numtriangles)
2117 R_DrawCollisionBrush(model->brushq3.data_models[model->brush.submodel].firstbrush[i].colbrushf);
2118 for (i = 0, face = model->brushq3.data_models[model->brush.submodel].firstface;i < model->brushq3.data_models[model->brush.submodel].numfaces;i++, face++)
2119 if (face->mesh.num_collisiontriangles)
2120 R_Q3BSP_DrawCollisionFace(ent, face);
2121 qglPolygonOffset(0, 0);
2125 void R_Q3BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outclusterlist, qbyte *outclusterpvs, int *outnumclusterspointer, int *outsurfacelist, qbyte *outsurfacepvs, int *outnumsurfacespointer)
2127 model_t *model = ent->model;
2128 vec3_t lightmins, lightmaxs;
2129 int t, leafindex, leaffaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2132 q3msurface_t *surface;
2135 lightmins[0] = relativelightorigin[0] - lightradius;
2136 lightmins[1] = relativelightorigin[1] - lightradius;
2137 lightmins[2] = relativelightorigin[2] - lightradius;
2138 lightmaxs[0] = relativelightorigin[0] + lightradius;
2139 lightmaxs[1] = relativelightorigin[1] + lightradius;
2140 lightmaxs[2] = relativelightorigin[2] + lightradius;
2141 *outnumclusterspointer = 0;
2142 *outnumsurfacespointer = 0;
2143 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2144 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2147 VectorCopy(lightmins, outmins);
2148 VectorCopy(lightmaxs, outmaxs);
2151 VectorCopy(relativelightorigin, outmins);
2152 VectorCopy(relativelightorigin, outmaxs);
2153 if (model->brush.GetPVS)
2154 pvs = model->brush.GetPVS(model, relativelightorigin);
2157 // FIXME: use BSP recursion as lights are often small
2158 for (leafindex = 0, leaf = model->brush.data_leafs;leafindex < model->brush.num_leafs;leafindex++, leaf++)
2160 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2162 outmins[0] = min(outmins[0], leaf->mins[0]);
2163 outmins[1] = min(outmins[1], leaf->mins[1]);
2164 outmins[2] = min(outmins[2], leaf->mins[2]);
2165 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2166 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2167 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2170 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2172 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2173 outclusterlist[outnumclusters++] = leaf->clusterindex;
2178 for (leaffaceindex = 0;leaffaceindex < leaf->numleaffaces;leaffaceindex++)
2180 surfaceindex = leaf->firstleafface[leaffaceindex];
2181 surface = model->brushq3.data_faces + surfaceindex;
2182 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2184 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->mesh.num_triangles)
2186 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2188 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2190 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2191 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2192 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2193 if (lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2195 SETPVSBIT(outsurfacepvs, surfaceindex);
2196 outsurfacelist[outnumsurfaces++] = surfaceindex;
2203 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2205 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2206 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2207 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2208 if (PointInfrontOfTriangle(relativelightorigin, v[0], v[1], v[2]) && lightmaxs[0] > min(v[0][0], min(v[1][0], v[2][0])) && lightmins[0] < max(v[0][0], max(v[1][0], v[2][0])) && lightmaxs[1] > min(v[0][1], min(v[1][1], v[2][1])) && lightmins[1] < max(v[0][1], max(v[1][1], v[2][1])) && lightmaxs[2] > min(v[0][2], min(v[1][2], v[2][2])) && lightmins[2] < max(v[0][2], max(v[1][2], v[2][2])))
2210 SETPVSBIT(outsurfacepvs, surfaceindex);
2211 outsurfacelist[outnumsurfaces++] = surfaceindex;
2223 // limit combined leaf box to light boundaries
2224 outmins[0] = max(outmins[0], lightmins[0]);
2225 outmins[1] = max(outmins[1], lightmins[1]);
2226 outmins[2] = max(outmins[2], lightmins[2]);
2227 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2228 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2229 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2231 *outnumclusterspointer = outnumclusters;
2232 *outnumsurfacespointer = outnumsurfaces;
2235 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2237 model_t *model = ent->model;
2238 vec3_t lightmins, lightmaxs;
2239 q3msurface_t *surface;
2240 int surfacelistindex;
2241 if (r_drawcollisionbrushes.integer < 2)
2243 lightmins[0] = relativelightorigin[0] - lightradius;
2244 lightmins[1] = relativelightorigin[1] - lightradius;
2245 lightmins[2] = relativelightorigin[2] - lightradius;
2246 lightmaxs[0] = relativelightorigin[0] + lightradius;
2247 lightmaxs[1] = relativelightorigin[1] + lightradius;
2248 lightmaxs[2] = relativelightorigin[2] + lightradius;
2249 R_Mesh_Matrix(&ent->matrix);
2250 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2251 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2253 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2254 // FIXME: check some manner of face->rendermode here?
2255 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & (Q3SURFACEPARM_SKY | Q3SURFACEPARM_TRANS)) && !(surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED))
2256 R_Shadow_MarkVolumeFromBox(surface->num_firstshadowmeshtriangle, surface->mesh.num_triangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, relativelightorigin, lightmins, lightmaxs, surface->mins, surface->maxs);
2258 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
2262 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, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int numsurfaces, const int *surfacelist)
2264 model_t *model = ent->model;
2265 vec3_t lightmins, lightmaxs, modelorg;
2266 q3msurface_t *surface;
2267 int surfacelistindex;
2268 if (r_drawcollisionbrushes.integer < 2)
2270 lightmins[0] = relativelightorigin[0] - lightradius;
2271 lightmins[1] = relativelightorigin[1] - lightradius;
2272 lightmins[2] = relativelightorigin[2] - lightradius;
2273 lightmaxs[0] = relativelightorigin[0] + lightradius;
2274 lightmaxs[1] = relativelightorigin[1] + lightradius;
2275 lightmaxs[2] = relativelightorigin[2] + lightradius;
2276 R_Mesh_Matrix(&ent->matrix);
2277 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2278 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2280 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2281 if (r_shadow_compilingrtlight)
2283 // if compiling an rtlight, capture the mesh
2284 Mod_ShadowMesh_AddMesh(r_shadow_mempool, r_shadow_compilingrtlight->static_meshchain_light, surface->texture->skin.base, surface->texture->skin.gloss, surface->texture->skin.nmap, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, surface->mesh.num_triangles, surface->mesh.data_element3i);
2286 else if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->mesh.num_triangles)
2288 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2289 qglDisable(GL_CULL_FACE);
2290 R_Shadow_RenderLighting(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i, surface->mesh.data_vertex3f, surface->mesh.data_svector3f, surface->mesh.data_tvector3f, surface->mesh.data_normal3f, surface->mesh.data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, ambientscale, diffusescale, specularscale);
2291 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2292 qglEnable(GL_CULL_FACE);
2299 static void gl_surf_start(void)
2303 static void gl_surf_shutdown(void)
2307 static void gl_surf_newmap(void)
2312 void GL_Surf_Init(void)
2315 dlightdivtable[0] = 4194304;
2316 for (i = 1;i < 32768;i++)
2317 dlightdivtable[i] = 4194304 / (i << 7);
2319 Cvar_RegisterVariable(&r_ambient);
2320 Cvar_RegisterVariable(&r_drawportals);
2321 Cvar_RegisterVariable(&r_testvis);
2322 Cvar_RegisterVariable(&r_floatbuildlightmap);
2323 Cvar_RegisterVariable(&r_detailtextures);
2324 Cvar_RegisterVariable(&r_surfaceworldnode);
2325 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2326 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2327 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
2328 Cvar_RegisterVariable(&gl_lightmaps);
2330 //R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);