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 static mempool_t *r_surf_mempool = NULL;
33 static int r_surf_surfacevisiblelimit = 0;
34 static qbyte *r_surf_surfacevisible = NULL;
36 cvar_t r_ambient = {0, "r_ambient", "0"};
37 cvar_t r_drawportals = {0, "r_drawportals", "0"};
38 cvar_t r_testvis = {0, "r_testvis", "0"};
39 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
40 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
41 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
42 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
43 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
44 cvar_t r_q3bsp_renderskydepth = {0, "r_q3bsp_renderskydepth", "0"};
45 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
48 // FIXME: these arrays are huge!
49 int r_q1bsp_maxmarkleafs;
50 int r_q1bsp_nummarkleafs;
51 mleaf_t *r_q1bsp_maxleaflist[65536];
52 int r_q1bsp_maxmarksurfaces;
53 int r_q1bsp_nummarksurfaces;
54 msurface_t *r_q1bsp_maxsurfacelist[65536];
56 // FIXME: these arrays are huge!
57 int r_q3bsp_maxmarkleafs;
58 int r_q3bsp_nummarkleafs;
59 q3mleaf_t *r_q3bsp_maxleaflist[65536];
60 int r_q3bsp_maxmarksurfaces;
61 int r_q3bsp_nummarksurfaces;
62 q3msurface_t *r_q3bsp_maxsurfacelist[65536];
65 void R_Surf_ClearSurfaceVisible(int num)
67 if (r_surf_surfacevisiblelimit < num)
69 Mem_Free(r_surf_surfacevisible);
70 r_surf_surfacevisiblelimit = num;
71 r_surf_surfacevisible = Mem_Alloc(r_surf_mempool, r_surf_surfacevisiblelimit);
73 memset(r_surf_surfacevisible, 0, num);
76 static int dlightdivtable[32768];
78 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
80 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
82 float dist, impact[3], local[3];
87 smax = (surf->extents[0] >> 4) + 1;
88 tmax = (surf->extents[1] >> 4) + 1;
91 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
93 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
94 continue; // not lit by this light
96 Matrix4x4_Transform(matrix, light->origin, local);
97 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
99 // for comparisons to minimum acceptable light
100 // compensate for LIGHTOFFSET
101 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
104 dist2 += LIGHTOFFSET;
105 if (dist2 >= maxdist)
108 if (surf->plane->type < 3)
110 VectorCopy(local, impact);
111 impact[surf->plane->type] -= dist;
115 impact[0] = local[0] - surf->plane->normal[0] * dist;
116 impact[1] = local[1] - surf->plane->normal[1] * dist;
117 impact[2] = local[2] - surf->plane->normal[2] * dist;
120 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
121 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
123 s = bound(0, impacts, smax * 16) - impacts;
124 t = bound(0, impactt, tmax * 16) - impactt;
125 i = s * s + t * t + dist2;
129 // reduce calculations
130 for (s = 0, i = impacts; s < smax; s++, i -= 16)
131 sdtable[s] = i * i + dist2;
133 maxdist3 = maxdist - dist2;
135 // convert to 8.8 blocklights format
136 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
137 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
138 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
139 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
143 for (t = 0;t < tmax;t++, i -= 16)
146 // make sure some part of it is visible on this line
149 maxdist2 = maxdist - td;
150 for (s = 0;s < smax;s++)
152 if (sdtable[s] < maxdist2)
154 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
158 bl[1] += (green * k);
173 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
175 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
176 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
181 smax = (surf->extents[0] >> 4) + 1;
182 tmax = (surf->extents[1] >> 4) + 1;
185 for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
187 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
188 continue; // not lit by this light
190 Matrix4x4_Transform(matrix, light->origin, local);
191 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
193 // for comparisons to minimum acceptable light
194 // compensate for LIGHTOFFSET
195 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
198 dist2 += LIGHTOFFSET;
199 if (dist2 >= maxdist)
202 if (surf->plane->type < 3)
204 VectorCopy(local, impact);
205 impact[surf->plane->type] -= dist;
209 impact[0] = local[0] - surf->plane->normal[0] * dist;
210 impact[1] = local[1] - surf->plane->normal[1] * dist;
211 impact[2] = local[2] - surf->plane->normal[2] * dist;
214 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
215 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
217 td = bound(0, impacts, smax * 16) - impacts;
218 td1 = bound(0, impactt, tmax * 16) - impactt;
219 td = td * td + td1 * td1 + dist2;
223 // reduce calculations
224 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
225 sdtable[s] = td1 * td1 + dist2;
227 maxdist3 = maxdist - dist2;
229 // convert to 8.8 blocklights format
230 red = light->rtlight.lightmap_light[0];
231 green = light->rtlight.lightmap_light[1];
232 blue = light->rtlight.lightmap_light[2];
233 subtract = light->rtlight.lightmap_subtract * 32768.0f;
234 bl = floatblocklights;
237 for (t = 0;t < tmax;t++, td1 -= 16.0f)
240 // make sure some part of it is visible on this line
243 maxdist2 = maxdist - td;
244 for (s = 0;s < smax;s++)
246 if (sdtable[s] < maxdist2)
248 k = (32768.0f / (sdtable[s] + td)) - subtract;
268 Combine and scale multiple lightmaps into the 8.8 format in blocklights
271 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
273 if (!r_floatbuildlightmap.integer)
275 int smax, tmax, i, j, size, size3, maps, stride, l;
276 unsigned int *bl, scale;
277 qbyte *lightmap, *out, *stain;
279 // update cached lighting info
280 surf->cached_dlight = 0;
282 smax = (surf->extents[0]>>4)+1;
283 tmax = (surf->extents[1]>>4)+1;
286 lightmap = surf->samples;
288 // set to full bright if no light data
290 if (!ent->model->brushq1.lightdata)
292 for (i = 0;i < size3;i++)
298 memset(bl, 0, size*3*sizeof(unsigned int));
300 if (surf->dlightframe == r_framecount)
302 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
303 if (surf->cached_dlight)
307 // add all the lightmaps
311 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
312 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
313 bl[i] += lightmap[i] * scale;
317 stain = surf->stainsamples;
320 // the >> 16 shift adjusts down 8 bits to account for the stainmap
321 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
322 // be doubled during rendering to achieve 2x overbright
323 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
324 if (ent->model->brushq1.lightmaprgba)
326 stride = (surf->lightmaptexturestride - smax) * 4;
327 for (i = 0;i < tmax;i++, out += stride)
329 for (j = 0;j < smax;j++)
331 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
332 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
333 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
340 stride = (surf->lightmaptexturestride - smax) * 3;
341 for (i = 0;i < tmax;i++, out += stride)
343 for (j = 0;j < smax;j++)
345 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
346 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
347 l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
352 R_UpdateTexture(surf->lightmaptexture, templight);
356 int smax, tmax, i, j, size, size3, maps, stride, l;
358 qbyte *lightmap, *out, *stain;
360 // update cached lighting info
361 surf->cached_dlight = 0;
363 smax = (surf->extents[0]>>4)+1;
364 tmax = (surf->extents[1]>>4)+1;
367 lightmap = surf->samples;
369 // set to full bright if no light data
370 bl = floatblocklights;
371 if (!ent->model->brushq1.lightdata)
373 for (i = 0;i < size3;i++)
378 memset(bl, 0, size*3*sizeof(float));
380 if (surf->dlightframe == r_framecount)
382 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
383 if (surf->cached_dlight)
387 // add all the lightmaps
390 bl = floatblocklights;
391 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
392 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
393 bl[i] += lightmap[i] * scale;
397 stain = surf->stainsamples;
398 bl = floatblocklights;
400 // this scaling adjusts down 8 bits to account for the stainmap
401 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
402 // be doubled during rendering to achieve 2x overbright
403 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
404 scale = 1.0f / (1 << 16);
405 if (ent->model->brushq1.lightmaprgba)
407 stride = (surf->lightmaptexturestride - smax) * 4;
408 for (i = 0;i < tmax;i++, out += stride)
410 for (j = 0;j < smax;j++)
412 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
413 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
414 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
421 stride = (surf->lightmaptexturestride - smax) * 3;
422 for (i = 0;i < tmax;i++, out += stride)
424 for (j = 0;j < smax;j++)
426 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
427 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
428 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
433 R_UpdateTexture(surf->lightmaptexture, templight);
437 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
439 float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
440 msurface_t *surf, *endsurf;
441 int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
445 maxdist = radius * radius;
446 invradius = 1.0f / radius;
449 if (node->contents < 0)
451 ndist = PlaneDiff(origin, node->plane);
454 node = node->children[0];
459 node = node->children[1];
463 dist2 = ndist * ndist;
464 maxdist3 = maxdist - dist2;
466 if (node->plane->type < 3)
468 VectorCopy(origin, impact);
469 impact[node->plane->type] -= ndist;
473 impact[0] = origin[0] - node->plane->normal[0] * ndist;
474 impact[1] = origin[1] - node->plane->normal[1] * ndist;
475 impact[2] = origin[2] - node->plane->normal[2] * ndist;
478 for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
480 if (surf->stainsamples)
482 smax = (surf->extents[0] >> 4) + 1;
483 tmax = (surf->extents[1] >> 4) + 1;
485 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
486 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
488 s = bound(0, impacts, smax * 16) - impacts;
489 t = bound(0, impactt, tmax * 16) - impactt;
490 i = s * s + t * t + dist2;
494 // reduce calculations
495 for (s = 0, i = impacts; s < smax; s++, i -= 16)
496 sdtable[s] = i * i + dist2;
498 bl = surf->stainsamples;
503 for (t = 0;t < tmax;t++, i -= 16)
506 // make sure some part of it is visible on this line
509 maxdist2 = maxdist - td;
510 for (s = 0;s < smax;s++)
512 if (sdtable[s] < maxdist2)
514 ratio = lhrandom(0.0f, 1.0f);
515 a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
516 if (a >= (1.0f / 64.0f))
520 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
521 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
522 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
532 // force lightmap upload
534 surf->cached_dlight = true;
538 if (node->children[0]->contents >= 0)
540 if (node->children[1]->contents >= 0)
542 R_StainNode(node->children[0], model, origin, radius, fcolor);
543 node = node->children[1];
548 node = node->children[0];
552 else if (node->children[1]->contents >= 0)
554 node = node->children[1];
559 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)
563 entity_render_t *ent;
566 if (r_refdef.worldmodel == NULL || !r_refdef.worldmodel->brushq1.nodes)
571 fcolor[3] = ca1 * (1.0f / 64.0f);
572 fcolor[4] = cr2 - cr1;
573 fcolor[5] = cg2 - cg1;
574 fcolor[6] = cb2 - cb1;
575 fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
577 R_StainNode(r_refdef.worldmodel->brushq1.nodes + r_refdef.worldmodel->brushq1.hulls[0].firstclipnode, r_refdef.worldmodel, origin, radius, fcolor);
579 // look for embedded bmodels
580 for (n = 0;n < cl_num_brushmodel_entities;n++)
582 ent = cl_brushmodel_entities[n];
584 if (model && model->name[0] == '*')
586 Mod_CheckLoaded(model);
587 if (model->brushq1.nodes)
589 Matrix4x4_Transform(&ent->inversematrix, origin, org);
590 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
598 =============================================================
602 =============================================================
605 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
610 if (styles[0] != 255)
612 for (i = 0;i < numverts;i++, c += 4)
614 lm = samples + lightmapoffsets[i];
615 scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
616 VectorMA(c, scale, lm, c);
617 if (styles[1] != 255)
620 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
621 VectorMA(c, scale, lm, c);
622 if (styles[2] != 255)
625 scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
626 VectorMA(c, scale, lm, c);
627 if (styles[3] != 255)
630 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
631 VectorMA(c, scale, lm, c);
639 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
645 for (i = 0;i < numverts;i++, v += 3, c += 4)
647 VectorSubtract(v, modelorg, diff);
648 f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
649 VectorScale(c, f, c);
652 else if (colorscale != 1)
653 for (i = 0;i < numverts;i++, c += 4)
654 VectorScale(c, colorscale, c);
657 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)
666 for (i = 0;i < numverts;i++, v += 3, c += 4)
668 VectorSubtract(v, modelorg, diff);
669 f = 1 - exp(fogdensity/DotProduct(diff, diff));
678 for (i = 0;i < numverts;i++, c += 4)
688 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)
695 for (i = 0;i < numverts;i++, v += 3, c += 4)
697 VectorSubtract(v, modelorg, diff);
698 f = exp(fogdensity/DotProduct(diff, diff));
706 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
711 int i, l, lit = false;
712 const dlight_t *light;
714 for (l = 0;l < r_numdlights;l++)
716 if (dlightbits[l >> 5] & (1 << (l & 31)))
718 light = &r_dlight[l];
719 Matrix4x4_Transform(matrix, light->origin, lightorigin);
720 for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
722 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
723 if (f < light->rtlight.lightmap_cullradius2)
725 f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
726 VectorMA(c, f, light->rtlight.lightmap_light, c);
735 static void RSurfShader_Transparent_Callback(const void *calldata1, int calldata2)
737 const entity_render_t *ent = calldata1;
738 const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
741 float base, colorscale;
744 float args[4] = {0.05f,0,0,0.04f};
745 int rendertype, turb, fullbright;
747 R_Mesh_Matrix(&ent->matrix);
748 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
750 texture = surf->texinfo->texture;
751 if (texture->animated)
752 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];
753 currentalpha = ent->alpha;
754 if (surf->flags & SURF_WATERALPHA)
755 currentalpha *= r_wateralpha.value;
757 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
758 if (ent->effects & EF_ADDITIVE)
760 rendertype = SURFRENDER_ADD;
761 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
764 else if (currentalpha < 1 || texture->skin.fog != NULL)
766 rendertype = SURFRENDER_ALPHA;
767 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
772 rendertype = SURFRENDER_OPAQUE;
773 GL_BlendFunc(GL_ONE, GL_ZERO);
774 GL_DepthMask(!(ent->effects & EF_NODEPTHTEST));
777 turb = (surf->flags & SURF_DRAWTURB) && r_waterscroll.value;
778 fullbright = !(ent->flags & RENDER_LIGHT) || (surf->flags & SURF_DRAWFULLBRIGHT) || !surf->samples;
779 base = fullbright ? 2.0f : r_ambient.value * (1.0f / 64.0f);
780 if (surf->flags & SURF_DRAWTURB)
782 if ((surf->flags & SURF_DRAWTURB) && gl_textureshader && r_watershader.value && !fogenabled && fullbright && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
784 // NVIDIA Geforce3 distortion texture shader on water
785 GL_Color(1, 1, 1, currentalpha);
786 memset(&m, 0, sizeof(m));
787 m.pointer_vertex = surf->mesh.data_vertex3f;
788 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(r_refdef.time * 16)&63]);
789 m.tex[1] = R_GetTexture(texture->skin.base);
790 m.texcombinergb[0] = GL_REPLACE;
791 m.texcombinergb[1] = GL_REPLACE;
792 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
793 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
794 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
795 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);
799 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
801 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
802 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
803 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
804 qglEnable(GL_TEXTURE_SHADER_NV);
806 GL_LockArrays(0, surf->mesh.num_vertices);
807 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
810 qglDisable(GL_TEXTURE_SHADER_NV);
811 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
816 memset(&m, 0, sizeof(m));
817 m.pointer_vertex = surf->mesh.data_vertex3f;
818 m.pointer_color = varray_color4f;
819 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
820 m.tex[0] = R_GetTexture(texture->skin.base);
823 // scrolling in texture matrix
824 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);
827 if (gl_combine.integer)
829 m.texrgbscale[0] = 4;
832 R_FillColors(varray_color4f, surf->mesh.num_vertices, base * ent->colormod[0], base * ent->colormod[1], base * ent->colormod[2], currentalpha);
835 if (surf->dlightframe == r_framecount)
836 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
838 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);
840 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
842 GL_LockArrays(0, surf->mesh.num_vertices);
843 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
845 if (texture->skin.glow)
847 memset(&m, 0, sizeof(m));
848 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
850 m.pointer_color = varray_color4f;
851 m.tex[0] = R_GetTexture(texture->skin.glow);
852 m.pointer_vertex = surf->mesh.data_vertex3f;
855 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
858 // scrolling in texture matrix
859 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);
863 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
864 GL_LockArrays(0, surf->mesh.num_vertices);
865 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
868 if (fogenabled && rendertype != SURFRENDER_ADD)
870 memset(&m, 0, sizeof(m));
871 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
873 m.pointer_color = varray_color4f;
874 m.tex[0] = R_GetTexture(texture->skin.fog);
875 m.pointer_vertex = surf->mesh.data_vertex3f;
878 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
881 // scrolling in texture matrix
882 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);
886 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
887 GL_LockArrays(0, surf->mesh.num_vertices);
888 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
894 void R_UpdateTextureInfo(entity_render_t *ent)
896 int i, texframe, alttextures;
902 alttextures = ent->frame != 0;
903 texframe = (int)(r_refdef.time * 5.0f);
904 for (i = 0;i < ent->model->brushq1.numtextures;i++)
906 t = ent->model->brushq1.textures + i;
907 t->currentalpha = ent->alpha;
908 if (t->flags & SURF_WATERALPHA)
909 t->currentalpha *= r_wateralpha.value;
910 if (ent->effects & EF_ADDITIVE)
911 t->rendertype = SURFRENDER_ADD;
912 else if (t->currentalpha < 1 || t->skin.fog != NULL)
913 t->rendertype = SURFRENDER_ALPHA;
915 t->rendertype = SURFRENDER_OPAQUE;
916 // we don't need to set currentframe if t->animated is false because
917 // it was already set up by the texture loader for non-animating
919 t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
923 void R_DrawSurfaceList(entity_render_t *ent, texture_t *texture, int texturenumsurfaces, msurface_t **texturesurfacelist)
925 int texturesurfaceindex;
926 vec3_t center, modelorg;
927 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
929 if (gl_lightmaps.integer)
931 GL_BlendFunc(GL_ONE, GL_ZERO);
934 GL_Color(1, 1, 1, 1);
935 memset(&m, 0, sizeof(m));
936 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
938 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
939 m.tex[0] = R_GetTexture(surface->lightmaptexture);
940 m.pointer_vertex = surface->mesh.data_vertex3f;
941 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
943 GL_LockArrays(0, surface->mesh.num_vertices);
944 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
948 else if (texture->rendertype != SURFRENDER_OPAQUE)
950 // transparent vertex shaded from lightmap
951 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
953 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
954 Matrix4x4_Transform(&ent->matrix, surface->poly_center, center);
955 R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : center, RSurfShader_Transparent_Callback, ent, surface - ent->model->brushq1.surfaces);
958 else if (texture->flags & SURF_LIGHTMAP)
960 qboolean dolightmap = (ent->flags & RENDER_LIGHT);
961 qboolean dobase = true;
962 qboolean doambient = r_ambient.value > 0;
963 qboolean dodetail = r_detailtextures.integer != 0;
964 qboolean doglow = texture->skin.glow != NULL;
965 qboolean dofog = fogenabled;
966 // multitexture cases
967 if (r_textureunits.integer >= 2 && gl_combine.integer && dobase && dolightmap)
971 GL_BlendFunc(GL_ONE, GL_ZERO);
974 GL_Color(1, 1, 1, 1);
975 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
976 memset(&m, 0, sizeof(m));
977 m.tex[0] = R_GetTexture(texture->skin.base);
978 m.texrgbscale[1] = 2;
979 if (r_textureunits.integer >= 3 && !doambient && dodetail)
981 m.tex[2] = R_GetTexture(texture->skin.detail);
982 m.texrgbscale[2] = 2;
984 if (r_textureunits.integer >= 3 && texture->skin.glow)
986 m.tex[3] = R_GetTexture(texture->skin.glow);
987 m.texcombinergb[3] = GL_ADD;
989 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
991 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
992 m.tex[1] = R_GetTexture(surface->lightmaptexture);
993 m.pointer_vertex = surface->mesh.data_vertex3f;
994 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
995 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
996 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
997 m.pointer_texcoord[3] = surface->mesh.data_texcoordtexture2f;
999 GL_LockArrays(0, surface->mesh.num_vertices);
1000 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1001 GL_LockArrays(0, 0);
1006 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1008 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1009 m.tex[1] = R_GetTexture(surface->lightmaptexture);
1010 m.pointer_vertex = surface->mesh.data_vertex3f;
1011 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1012 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1013 m.pointer_texcoord[2] = surface->mesh.data_texcoorddetail2f;
1015 GL_LockArrays(0, surface->mesh.num_vertices);
1016 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1017 GL_LockArrays(0, 0);
1021 else if (r_textureunits.integer >= 3 && !doambient && !dodetail && doglow)
1023 m.tex[2] = R_GetTexture(texture->skin.glow);
1024 m.texcombinergb[2] = GL_ADD;
1026 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1028 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1029 m.tex[1] = R_GetTexture(surface->lightmaptexture);
1030 m.pointer_vertex = surface->mesh.data_vertex3f;
1031 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1032 m.pointer_texcoord[1] = surface->mesh.data_texcoordlightmap2f;
1033 m.pointer_texcoord[2] = surface->mesh.data_texcoordtexture2f;
1035 GL_LockArrays(0, surface->mesh.num_vertices);
1036 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1037 GL_LockArrays(0, 0);
1041 // anything not handled above
1044 GL_BlendFunc(GL_ONE, GL_ZERO);
1047 GL_Color(1, 1, 1, 1);
1048 if (ent->flags & RENDER_LIGHT)
1049 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1051 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1052 memset(&m, 0, sizeof(m));
1053 m.tex[0] = R_GetTexture(texture->skin.base);
1054 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1056 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1057 m.pointer_vertex = surface->mesh.data_vertex3f;
1058 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1060 GL_LockArrays(0, surface->mesh.num_vertices);
1061 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1062 GL_LockArrays(0, 0);
1065 GL_DepthMask(false);
1068 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1069 GL_DepthMask(false);
1071 GL_Color(1, 1, 1, 1);
1072 memset(&m, 0, sizeof(m));
1073 m.tex[0] = R_GetTexture(texture->skin.base);
1074 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1076 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1077 m.tex[0] = R_GetTexture(surface->lightmaptexture);
1078 m.pointer_vertex = surface->mesh.data_vertex3f;
1079 m.pointer_texcoord[0] = surface->mesh.data_texcoordlightmap2f;
1081 GL_LockArrays(0, surface->mesh.num_vertices);
1082 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1083 GL_LockArrays(0, 0);
1088 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1089 GL_DepthMask(false);
1091 memset(&m, 0, sizeof(m));
1092 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);
1093 m.tex[0] = R_GetTexture(texture->skin.base);
1094 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1096 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1097 m.pointer_vertex = surface->mesh.data_vertex3f;
1098 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1100 GL_LockArrays(0, surface->mesh.num_vertices);
1101 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1102 GL_LockArrays(0, 0);
1107 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1108 GL_DepthMask(false);
1110 GL_Color(1, 1, 1, 1);
1111 memset(&m, 0, sizeof(m));
1112 m.tex[0] = R_GetTexture(texture->skin.detail);
1113 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1115 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1116 m.pointer_vertex = surface->mesh.data_vertex3f;
1117 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1119 GL_LockArrays(0, surface->mesh.num_vertices);
1120 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1121 GL_LockArrays(0, 0);
1126 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1127 GL_DepthMask(false);
1129 GL_Color(1, 1, 1, 1);
1130 memset(&m, 0, sizeof(m));
1131 m.tex[0] = R_GetTexture(texture->skin.glow);
1132 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1134 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1135 m.pointer_vertex = surface->mesh.data_vertex3f;
1136 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1138 GL_LockArrays(0, surface->mesh.num_vertices);
1139 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1140 GL_LockArrays(0, 0);
1145 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1146 GL_DepthMask(false);
1148 memset(&m, 0, sizeof(m));
1149 m.pointer_color = varray_color4f;
1150 m.tex[0] = R_GetTexture(texture->skin.glow);
1151 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1153 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1154 m.pointer_vertex = surface->mesh.data_vertex3f;
1156 m.pointer_texcoord[0] = surface->mesh.data_texcoordtexture2f;
1158 RSurf_FogPassColors_Vertex3f_Color4f(surface->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surface->mesh.num_vertices, modelorg);
1159 GL_LockArrays(0, surface->mesh.num_vertices);
1160 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1161 GL_LockArrays(0, 0);
1165 else if (texture->flags & SURF_DRAWTURB)
1167 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1169 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1170 RSurfShader_Transparent_Callback(ent, surface - ent->model->brushq1.surfaces);
1173 else if (texture->flags & SURF_DRAWSKY)
1177 skyrendernow = false;
1178 if (skyrendermasked)
1181 // LordHavoc: HalfLife maps have freaky skypolys...
1182 if (!ent->model->brush.ishlbsp)
1184 R_Mesh_Matrix(&ent->matrix);
1185 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1186 if (skyrendermasked)
1188 // depth-only (masking)
1189 GL_ColorMask(0,0,0,0);
1190 // just to make sure that braindead drivers don't draw anything
1191 // despite that colormask...
1192 GL_BlendFunc(GL_ZERO, GL_ONE);
1197 GL_BlendFunc(GL_ONE, GL_ZERO);
1201 memset(&m, 0, sizeof(m));
1202 for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
1204 msurface_t *surface = texturesurfacelist[texturesurfaceindex];
1205 m.pointer_vertex = surface->mesh.data_vertex3f;
1207 GL_LockArrays(0, surface->mesh.num_vertices);
1208 R_Mesh_Draw(surface->mesh.num_vertices, surface->mesh.num_triangles, surface->mesh.data_element3i);
1209 GL_LockArrays(0, 0);
1211 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1216 void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
1218 int i, *surfacevisframes, texturenumber, flagsmask;
1219 msurface_t *surface, **surfacechain;
1220 texture_t *t, *texture;
1221 model_t *model = ent->model;
1223 const int maxsurfacelist = 1024;
1224 int numsurfacelist = 0;
1225 msurface_t *surfacelist[1024];
1228 R_Mesh_Matrix(&ent->matrix);
1229 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1231 if (ent != r_refdef.worldentity)
1233 // because bmodels can be reused, we have to decide which things to render
1234 // from scratch every time
1235 int *mark = model->brushq1.surfacevisframes + model->firstmodelsurface;
1236 surface = model->brushq1.surfaces + model->firstmodelsurface;
1237 for (i = 0;i < model->nummodelsurfaces;i++, mark++, surface++)
1239 *mark = r_framecount;
1240 surface->dlightframe = -1;
1244 // update light styles
1247 if (r_dynamic.integer && !r_rtdlight)
1249 for (i = 0;i < model->brushq1.light_styles;i++)
1251 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1253 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1254 if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
1255 for (;(surface = *surfacechain);surfacechain++)
1256 surface->cached_dlight = true;
1261 R_UpdateTextureInfo(ent);
1262 surfacevisframes = model->brushq1.surfacevisframes;
1263 flagsmask = skysurfaces ? SURF_DRAWSKY : (SURF_DRAWTURB | SURF_LIGHTMAP);
1264 for (texturenumber = 0, t = model->brushq1.textures;texturenumber < model->brushq1.numtextures;texturenumber++, t++)
1266 if ((t->flags & flagsmask) && (texture = t->currentframe) && (surfacechain = model->brushq1.pvstexturechains[texturenumber]) != NULL)
1269 for (i = 0;(surface = surfacechain[i]);i++)
1271 if (surfacevisframes[surface - model->brushq1.surfaces] == r_framecount)
1273 // mark any backface surfaces as not visible
1274 if (PlaneDist(modelorg, surface->plane) < surface->plane->dist)
1276 if (!(surface->flags & SURF_PLANEBACK))
1277 surfacevisframes[surface - model->brushq1.surfaces] = -1;
1281 if ((surface->flags & SURF_PLANEBACK))
1282 surfacevisframes[surface - model->brushq1.surfaces] = -1;
1284 // add face to draw list and update lightmap if necessary
1286 surface->visframe = r_framecount;
1287 if (surface->cached_dlight && surface->lightmaptexture != NULL)
1288 R_BuildLightMap(ent, surface);
1289 surfacelist[numsurfacelist++] = surface;
1290 if (numsurfacelist >= maxsurfacelist)
1292 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1298 R_DrawSurfaceList(ent, texture, numsurfacelist, surfacelist);
1303 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1308 const mportal_t *portal = calldata1;
1309 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1310 GL_DepthMask(false);
1312 R_Mesh_Matrix(&r_identitymatrix);
1314 memset(&m, 0, sizeof(m));
1315 m.pointer_vertex = varray_vertex3f;
1319 GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1320 ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1321 ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1323 if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1325 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1326 VectorCopy(portal->points[i].position, v);
1329 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1330 VectorCopy(portal->points[i].position, v);
1331 GL_LockArrays(0, portal->numpoints);
1332 R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1333 GL_LockArrays(0, 0);
1336 // LordHavoc: this is just a nice debugging tool, very slow
1337 static void R_DrawPortals(void)
1342 model_t *model = r_refdef.worldmodel;
1345 for (portalnum = 0, portal = model->brushq1.portals;portalnum < model->brushq1.numportals;portalnum++, portal++)
1347 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1349 VectorClear(center);
1350 for (i = 0;i < portal->numpoints;i++)
1351 VectorAdd(center, portal->points[i].position, center);
1352 f = ixtable[portal->numpoints];
1353 VectorScale(center, f, center);
1354 R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, portal, portalnum);
1359 void R_WorldVisibility(void)
1364 model_t *model = r_refdef.worldmodel;
1365 int *surfacevisframes = model->brushq1.surfacevisframes;
1367 if (!model || !model->brushq1.PointInLeaf)
1370 viewleaf = model->brushq1.PointInLeaf(model, r_vieworigin);
1374 if (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer)
1376 int *surfacepvsframes = model->brushq1.surfacepvsframes;
1377 model->brushq1.pvsframecount++;
1378 model->brushq1.pvsviewleaf = viewleaf;
1379 model->brushq1.pvsviewleafnovis = r_novis.integer;
1380 model->brushq1.pvsleafchain = NULL;
1381 model->brushq1.pvssurflistlength = 0;
1382 for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1384 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1386 leaf->pvsframe = model->brushq1.pvsframecount;
1387 leaf->pvschain = model->brushq1.pvsleafchain;
1388 model->brushq1.pvsleafchain = leaf;
1389 // mark surfaces bounding this leaf as visible
1390 for (i = 0, mark = leaf->firstmarksurface;i < leaf->nummarksurfaces;i++, mark++)
1391 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1394 model->brushq1.BuildPVSTextureChains(model);
1397 if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1399 // equivilant to quake's RecursiveWorldNode but faster and more effective
1400 for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1402 if (!R_CullBox (leaf->mins, leaf->maxs))
1405 leaf->visframe = r_framecount;
1408 for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1410 int surfacenum = model->brushq1.pvssurflist[i];
1411 msurface_t *surface = model->brushq1.surfaces + surfacenum;
1412 if (!R_CullBox (surface->poly_mins, surface->poly_maxs))
1413 surfacevisframes[surfacenum] = r_framecount;
1420 mleaf_t *leafstack[8192];
1421 // LordHavoc: portal-passage worldnode with PVS;
1422 // follows portals leading outward from viewleaf, does not venture
1423 // offscreen or into leafs that are not visible, faster than Quake's
1424 // RecursiveWorldNode
1425 leafstack[0] = viewleaf;
1427 while (leafstackpos)
1430 leaf = leafstack[--leafstackpos];
1431 leaf->visframe = r_framecount;
1432 // draw any surfaces bounding this leaf
1433 if (leaf->nummarksurfaces)
1434 for (i = 0, mark = leaf->firstmarksurface;i < leaf->nummarksurfaces;i++, mark++)
1435 surfacevisframes[*mark] = r_framecount;
1436 // follow portals into other leafs
1437 for (p = leaf->portals;p;p = p->next)
1438 if (DotProduct(r_vieworigin, p->plane.normal) < (p->plane.dist + 1) && p->past->visframe != r_framecount && !R_CullBox(p->mins, p->maxs) && CHECKPVSBIT(r_pvsbits, p->past->clusterindex))
1439 leafstack[leafstackpos++] = p->past;
1443 if (r_drawportals.integer)
1447 void R_Q1BSP_DrawSky(entity_render_t *ent)
1449 if (ent->model == NULL)
1451 R_DrawSurfaces(ent, true);
1454 void R_Q1BSP_Draw(entity_render_t *ent)
1456 if (ent->model == NULL)
1459 R_DrawSurfaces(ent, false);
1462 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)
1464 model_t *model = ent->model;
1465 vec3_t lightmins, lightmaxs;
1466 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1469 msurface_t *surface;
1472 lightmins[0] = relativelightorigin[0] - lightradius;
1473 lightmins[1] = relativelightorigin[1] - lightradius;
1474 lightmins[2] = relativelightorigin[2] - lightradius;
1475 lightmaxs[0] = relativelightorigin[0] + lightradius;
1476 lightmaxs[1] = relativelightorigin[1] + lightradius;
1477 lightmaxs[2] = relativelightorigin[2] + lightradius;
1478 *outnumclusterspointer = 0;
1479 *outnumsurfacespointer = 0;
1480 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1481 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1484 VectorCopy(lightmins, outmins);
1485 VectorCopy(lightmaxs, outmaxs);
1488 VectorCopy(relativelightorigin, outmins);
1489 VectorCopy(relativelightorigin, outmaxs);
1490 if (model->brush.GetPVS)
1491 pvs = model->brush.GetPVS(model, relativelightorigin);
1494 // FIXME: use BSP recursion as lights are often small
1495 for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1497 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1499 outmins[0] = min(outmins[0], leaf->mins[0]);
1500 outmins[1] = min(outmins[1], leaf->mins[1]);
1501 outmins[2] = min(outmins[2], leaf->mins[2]);
1502 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1503 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1504 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1507 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1509 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1510 outclusterlist[outnumclusters++] = leaf->clusterindex;
1515 for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1517 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1518 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1520 surface = model->brushq1.surfaces + surfaceindex;
1521 if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1523 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1525 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1526 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1527 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1528 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])))
1530 SETPVSBIT(outsurfacepvs, surfaceindex);
1531 outsurfacelist[outnumsurfaces++] = surfaceindex;
1542 // limit combined leaf box to light boundaries
1543 outmins[0] = max(outmins[0], lightmins[0]);
1544 outmins[1] = max(outmins[1], lightmins[1]);
1545 outmins[2] = max(outmins[2], lightmins[2]);
1546 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1547 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1548 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1550 *outnumclusterspointer = outnumclusters;
1551 *outnumsurfacespointer = outnumsurfaces;
1554 void R_Q1BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1556 model_t *model = ent->model;
1557 vec3_t lightmins, lightmaxs;
1558 msurface_t *surface;
1559 int surfacelistindex;
1560 if (r_drawcollisionbrushes.integer < 2)
1562 lightmins[0] = relativelightorigin[0] - lightradius;
1563 lightmins[1] = relativelightorigin[1] - lightradius;
1564 lightmins[2] = relativelightorigin[2] - lightradius;
1565 lightmaxs[0] = relativelightorigin[0] + lightradius;
1566 lightmaxs[1] = relativelightorigin[1] + lightradius;
1567 lightmaxs[2] = relativelightorigin[2] + lightradius;
1568 R_Mesh_Matrix(&ent->matrix);
1569 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1570 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1572 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1573 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);
1575 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);
1579 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)
1581 model_t *model = ent->model;
1582 vec3_t lightmins, lightmaxs, modelorg;
1583 msurface_t *surface;
1585 int surfacelistindex;
1586 if (r_drawcollisionbrushes.integer < 2)
1588 lightmins[0] = relativelightorigin[0] - lightradius;
1589 lightmins[1] = relativelightorigin[1] - lightradius;
1590 lightmins[2] = relativelightorigin[2] - lightradius;
1591 lightmaxs[0] = relativelightorigin[0] + lightradius;
1592 lightmaxs[1] = relativelightorigin[1] + lightradius;
1593 lightmaxs[2] = relativelightorigin[2] + lightradius;
1594 R_Mesh_Matrix(&ent->matrix);
1595 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1596 R_UpdateTextureInfo(ent);
1597 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1599 surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1600 if (r_shadow_compilingrtlight)
1602 // if compiling an rtlight, capture the mesh
1603 t = surface->texinfo->texture;
1604 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1605 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);
1607 else if (ent != r_refdef.worldentity || surface->visframe == r_framecount)
1609 t = surface->texinfo->texture->currentframe;
1610 // FIXME: transparent surfaces need to be lit later
1611 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1612 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);
1618 void R_DrawCollisionBrush(colbrushf_t *brush)
1622 memset(&m, 0, sizeof(m));
1623 m.pointer_vertex = brush->points->v;
1625 i = (int)(((size_t)brush) / sizeof(colbrushf_t));
1626 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1627 GL_LockArrays(0, brush->numpoints);
1628 R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1629 GL_LockArrays(0, 0);
1632 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1636 if (!face->mesh.num_collisiontriangles)
1638 memset(&m, 0, sizeof(m));
1639 m.pointer_vertex = face->mesh.data_collisionvertex3f;
1641 i = (int)(((size_t)face) / sizeof(q3msurface_t));
1642 GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1643 GL_LockArrays(0, face->mesh.num_collisionvertices);
1644 R_Mesh_Draw(face->mesh.num_collisionvertices, face->mesh.num_collisiontriangles, face->mesh.data_collisionelement3i);
1645 GL_LockArrays(0, 0);
1648 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1650 const entity_render_t *ent = voident;
1651 q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
1653 R_Mesh_Matrix(&ent->matrix);
1654 memset(&m, 0, sizeof(m));
1655 if ((ent->effects & EF_ADDITIVE) || (face->texture->textureflags & Q3TEXTUREFLAG_ADDITIVE))
1656 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1658 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1659 GL_DepthMask(false);
1660 GL_DepthTest(!(ent->effects & EF_NODEPTHTEST));
1661 m.tex[0] = R_GetTexture(face->texture->skin.base);
1662 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1663 // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1664 if (gl_combine.integer)
1666 m.texrgbscale[0] = 2;
1667 if (r_textureunits.integer >= 2)
1669 m.tex[1] = R_GetTexture(face->lightmaptexture);
1670 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1671 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha);
1675 if (ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1 && ent->alpha == 1)
1676 m.pointer_color = face->mesh.data_lightmapcolor4f;
1680 for (i = 0;i < face->mesh.num_vertices;i++)
1682 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0];
1683 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1];
1684 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2];
1685 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1687 m.pointer_color = varray_color4f;
1694 for (i = 0;i < face->mesh.num_vertices;i++)
1696 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * 2.0f;
1697 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * 2.0f;
1698 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * 2.0f;
1699 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3] * ent->alpha;
1701 m.pointer_color = varray_color4f;
1703 if (face->texture->textureflags & (Q3TEXTUREFLAG_AUTOSPRITE | Q3TEXTUREFLAG_AUTOSPRITE2))
1706 float center[3], center2[3], forward[3], right[3], up[3], v[4][3];
1707 matrix4x4_t matrix1, imatrix1;
1708 R_Mesh_Matrix(&r_identitymatrix);
1709 // a single autosprite surface can contain multiple sprites...
1710 for (j = 0;j < face->mesh.num_vertices - 3;j += 4)
1712 VectorClear(center);
1713 for (i = 0;i < 4;i++)
1714 VectorAdd(center, face->mesh.data_vertex3f + (j+i) * 3, center);
1715 VectorScale(center, 0.25f, center);
1716 Matrix4x4_Transform(&ent->matrix, center, center2);
1717 // FIXME: calculate vectors from triangle edges instead of using texture vectors as an easy way out?
1718 Matrix4x4_FromVectors(&matrix1, face->mesh.data_normal3f + j*3, face->mesh.data_svector3f + j*3, face->mesh.data_tvector3f + j*3, center);
1719 Matrix4x4_Invert_Simple(&imatrix1, &matrix1);
1720 for (i = 0;i < 4;i++)
1721 Matrix4x4_Transform(&imatrix1, face->mesh.data_vertex3f + (j+i)*3, v[i]);
1722 if (face->texture->textureflags & Q3TEXTUREFLAG_AUTOSPRITE2)
1724 forward[0] = r_vieworigin[0] - center2[0];
1725 forward[1] = r_vieworigin[1] - center2[1];
1727 VectorNormalize(forward);
1728 right[0] = forward[1];
1729 right[1] = -forward[0];
1737 VectorCopy(r_viewforward, forward);
1738 VectorCopy(r_viewright, right);
1739 VectorCopy(r_viewup, up);
1741 for (i = 0;i < 4;i++)
1742 VectorMAMAMAM(1, center2, v[i][0], forward, v[i][1], right, v[i][2], up, varray_vertex3f + (i+j) * 3);
1744 m.pointer_vertex = varray_vertex3f;
1747 m.pointer_vertex = face->mesh.data_vertex3f;
1749 if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1750 qglDisable(GL_CULL_FACE);
1751 GL_LockArrays(0, face->mesh.num_vertices);
1752 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1753 GL_LockArrays(0, 0);
1754 if (face->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1755 qglEnable(GL_CULL_FACE);
1758 void R_Q3BSP_DrawFaceList(entity_render_t *ent, q3mtexture_t *t, int texturenumfaces, q3msurface_t **texturefacelist)
1760 int i, texturefaceindex;
1762 if (!texturenumfaces)
1764 c_faces += texturenumfaces;
1765 // gl_lightmaps debugging mode skips normal texturing
1766 if (gl_lightmaps.integer)
1770 GL_BlendFunc(GL_ONE, GL_ZERO);
1771 qglDisable(GL_CULL_FACE);
1772 memset(&m, 0, sizeof(m));
1773 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1775 q3msurface_t *face = texturefacelist[texturefaceindex];
1776 m.tex[0] = R_GetTexture(face->lightmaptexture);
1777 m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1778 if (face->lightmaptexture)
1780 GL_Color(1, 1, 1, 1);
1781 m.pointer_color = NULL;
1784 m.pointer_color = face->mesh.data_lightmapcolor4f;
1785 m.pointer_vertex = face->mesh.data_vertex3f;
1787 GL_LockArrays(0, face->mesh.num_vertices);
1788 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1789 GL_LockArrays(0, 0);
1791 qglEnable(GL_CULL_FACE);
1794 // transparent surfaces get sorted for later drawing
1795 if ((t->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
1797 vec3_t facecenter, center;
1798 // drawing sky transparently would be too difficult
1799 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1801 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1803 q3msurface_t *face = texturefacelist[texturefaceindex];
1804 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
1805 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
1806 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
1807 Matrix4x4_Transform(&ent->matrix, facecenter, center);
1808 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
1812 // sky surfaces draw sky if needed and render themselves as a depth mask
1813 if (t->surfaceparms & Q3SURFACEPARM_SKY)
1817 skyrendernow = false;
1818 if (skyrendermasked)
1821 if (!r_q3bsp_renderskydepth.integer)
1824 R_Mesh_Matrix(&ent->matrix);
1826 GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1827 if (skyrendermasked)
1829 // depth-only (masking)
1830 GL_ColorMask(0,0,0,0);
1831 // just to make sure that braindead drivers don't draw anything
1832 // despite that colormask...
1833 GL_BlendFunc(GL_ZERO, GL_ONE);
1838 GL_BlendFunc(GL_ONE, GL_ZERO);
1843 memset(&m, 0, sizeof(m));
1844 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1846 q3msurface_t *face = texturefacelist[texturefaceindex];
1847 m.pointer_vertex = face->mesh.data_vertex3f;
1849 GL_LockArrays(0, face->mesh.num_vertices);
1850 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1851 GL_LockArrays(0, 0);
1853 GL_ColorMask(r_refdef.colormask[0], r_refdef.colormask[1], r_refdef.colormask[2], 1);
1856 // anything else is a typical wall, lightmap * texture + glow
1857 qboolean dolightmap = (ent->flags & RENDER_LIGHT);
1858 qboolean dobase = true;
1859 qboolean doambient = r_ambient.value > 0;
1860 qboolean doglow = t->skin.glow != NULL;
1861 qboolean dofog = fogenabled;
1862 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1863 qglDisable(GL_CULL_FACE);
1864 if (!dolightmap && dobase)
1870 GL_BlendFunc(GL_ONE, GL_ZERO);
1871 GL_Color(ent->colormod[0], ent->colormod[1], ent->colormod[2], 1);
1872 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
1873 qglDisable(GL_CULL_FACE);
1874 memset(&m, 0, sizeof(m));
1875 m.tex[0] = R_GetTexture(t->skin.base);
1876 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1878 q3msurface_t *face = texturefacelist[texturefaceindex];
1879 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1880 m.pointer_vertex = face->mesh.data_vertex3f;
1882 GL_LockArrays(0, face->mesh.num_vertices);
1883 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1884 GL_LockArrays(0, 0);
1887 if (r_lightmapintensity <= 0 && dolightmap && dobase)
1893 GL_BlendFunc(GL_ONE, GL_ZERO);
1894 GL_Color(0, 0, 0, 1);
1895 memset(&m, 0, sizeof(m));
1896 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1898 q3msurface_t *face = texturefacelist[texturefaceindex];
1899 m.pointer_vertex = face->mesh.data_vertex3f;
1901 GL_LockArrays(0, face->mesh.num_vertices);
1902 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1903 GL_LockArrays(0, 0);
1906 if (r_textureunits.integer >= 2 && gl_combine.integer && dolightmap && dobase)
1908 // dualtexture combine
1913 GL_BlendFunc(GL_ONE, GL_ZERO);
1914 memset(&m, 0, sizeof(m));
1915 m.tex[0] = R_GetTexture(t->skin.base);
1916 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
1917 m.pointer_color = NULL;
1918 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1920 q3msurface_t *face = texturefacelist[texturefaceindex];
1921 if (!face->lightmaptexture)
1923 m.tex[1] = R_GetTexture(face->lightmaptexture);
1924 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1925 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1926 m.texrgbscale[1] = 2;
1927 m.pointer_vertex = face->mesh.data_vertex3f;
1929 GL_LockArrays(0, face->mesh.num_vertices);
1930 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1931 GL_LockArrays(0, 0);
1933 if (r_lightmapintensity == 1 && ent->colormod[0] == 1 && ent->colormod[1] == 1 && ent->colormod[2] == 1)
1935 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1937 q3msurface_t *face = texturefacelist[texturefaceindex];
1938 if (face->lightmaptexture)
1940 m.tex[1] = R_GetTexture(face->lightmaptexture);
1941 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1942 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1943 m.texrgbscale[1] = 2;
1944 m.pointer_color = face->mesh.data_lightmapcolor4f;
1945 m.pointer_vertex = face->mesh.data_vertex3f;
1947 GL_LockArrays(0, face->mesh.num_vertices);
1948 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1949 GL_LockArrays(0, 0);
1954 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1956 q3msurface_t *face = texturefacelist[texturefaceindex];
1957 if (face->lightmaptexture)
1959 m.tex[1] = R_GetTexture(face->lightmaptexture);
1960 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
1961 m.pointer_texcoord[1] = face->mesh.data_texcoordlightmap2f;
1962 m.texrgbscale[1] = 2;
1963 m.pointer_color = varray_color4f;
1964 for (i = 0;i < face->mesh.num_vertices;i++)
1966 varray_color4f[i*4+0] = face->mesh.data_lightmapcolor4f[i*4+0] * ent->colormod[0] * r_lightmapintensity;
1967 varray_color4f[i*4+1] = face->mesh.data_lightmapcolor4f[i*4+1] * ent->colormod[1] * r_lightmapintensity;
1968 varray_color4f[i*4+2] = face->mesh.data_lightmapcolor4f[i*4+2] * ent->colormod[2] * r_lightmapintensity;
1969 varray_color4f[i*4+3] = face->mesh.data_lightmapcolor4f[i*4+3];
1971 m.pointer_vertex = face->mesh.data_vertex3f;
1973 GL_LockArrays(0, face->mesh.num_vertices);
1974 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1975 GL_LockArrays(0, 0);
1984 GL_BlendFunc(GL_ONE, GL_ZERO);
1985 memset(&m, 0, sizeof(m));
1986 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
1988 q3msurface_t *face = texturefacelist[texturefaceindex];
1989 m.tex[0] = R_GetTexture(face->lightmaptexture);
1990 m.pointer_texcoord[0] = face->mesh.data_texcoordlightmap2f;
1991 if (face->lightmaptexture)
1992 m.pointer_color = NULL;
1994 m.pointer_color = face->mesh.data_lightmapcolor4f;
1995 m.pointer_vertex = face->mesh.data_vertex3f;
1997 GL_LockArrays(0, face->mesh.num_vertices);
1998 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
1999 GL_LockArrays(0, 0);
2004 GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2005 GL_DepthMask(false);
2007 GL_Color(r_lightmapintensity * ent->colormod[0], r_lightmapintensity * ent->colormod[1], r_lightmapintensity * ent->colormod[2], 1);
2008 memset(&m, 0, sizeof(m));
2009 m.tex[0] = R_GetTexture(t->skin.base);
2010 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2012 q3msurface_t *face = texturefacelist[texturefaceindex];
2013 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2014 m.pointer_vertex = face->mesh.data_vertex3f;
2016 GL_LockArrays(0, face->mesh.num_vertices);
2017 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2018 GL_LockArrays(0, 0);
2023 GL_BlendFunc(GL_ONE, GL_ONE);
2024 GL_DepthMask(false);
2026 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);
2027 memset(&m, 0, sizeof(m));
2028 m.tex[0] = R_GetTexture(t->skin.base);
2029 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2031 q3msurface_t *face = texturefacelist[texturefaceindex];
2032 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2033 m.pointer_vertex = face->mesh.data_vertex3f;
2035 GL_LockArrays(0, face->mesh.num_vertices);
2036 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2037 GL_LockArrays(0, 0);
2042 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2043 GL_DepthMask(false);
2045 GL_Color(1, 1, 1, 1);
2046 memset(&m, 0, sizeof(m));
2047 m.tex[0] = R_GetTexture(t->skin.glow);
2048 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2050 q3msurface_t *face = texturefacelist[texturefaceindex];
2051 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2052 m.pointer_vertex = face->mesh.data_vertex3f;
2054 GL_LockArrays(0, face->mesh.num_vertices);
2055 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2056 GL_LockArrays(0, 0);
2062 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2063 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2064 GL_DepthMask(false);
2066 GL_Color(1, 1, 1, 1);
2067 memset(&m, 0, sizeof(m));
2068 m.tex[0] = R_GetTexture(t->skin.fog);
2069 m.pointer_color = varray_color4f;
2070 for (texturefaceindex = 0;texturefaceindex < texturenumfaces;texturefaceindex++)
2072 q3msurface_t *face = texturefacelist[texturefaceindex];
2074 m.pointer_texcoord[0] = face->mesh.data_texcoordtexture2f;
2075 m.pointer_vertex = face->mesh.data_vertex3f;
2077 RSurf_FogPassColors_Vertex3f_Color4f(face->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, face->mesh.num_vertices, modelorg);
2078 GL_LockArrays(0, face->mesh.num_vertices);
2079 R_Mesh_Draw(face->mesh.num_vertices, face->mesh.num_triangles, face->mesh.data_element3i);
2080 GL_LockArrays(0, 0);
2083 if (t->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2084 qglEnable(GL_CULL_FACE);
2087 void R_Q3BSP_DrawFaces(entity_render_t *ent, int skyfaces)
2089 int i, ti, flagsmask, flags;
2093 const int maxfaces = 1024;
2095 q3msurface_t *facelist[1024];
2096 R_Mesh_Matrix(&ent->matrix);
2098 flagsmask = Q3SURFACEFLAG_NODRAW | Q3SURFACEFLAG_SKY;
2100 flags = Q3SURFACEFLAG_SKY;
2103 if (ent == r_refdef.worldentity)
2107 R_Surf_ClearSurfaceVisible(r_refdef.worldmodel->brushq3.num_faces);
2108 for (j = 0, leaf = r_refdef.worldmodel->brushq3.data_leafs;j < r_refdef.worldmodel->brushq3.num_leafs;j++, leaf++)
2110 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
2113 for (i = 0;i < leaf->numleaffaces;i++)
2114 r_surf_surfacevisible[leaf->firstleaffacenum[i]] = 1;
2117 for (ti = 0, t = model->brushq3.data_textures;ti < model->brushq3.num_textures;ti++, t++)
2119 if ((t->surfaceflags & flagsmask) == flags)
2122 for (i = 0;i < t->numfaces;i++)
2124 if (r_surf_surfacevisible[t->facenumlist[i]])
2126 face = t->facelist[i];
2127 //if (!R_CullBox(face->mins, face->maxs))
2128 if (face->mesh.num_triangles)
2130 if (numfaces >= maxfaces)
2133 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2136 facelist[numfaces++] = face;
2141 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2149 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2151 if ((face->texture->surfaceflags & flagsmask) == flags && face->mesh.num_triangles)
2153 if (t != face->texture || numfaces >= maxfaces)
2156 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2160 facelist[numfaces++] = face;
2164 R_Q3BSP_DrawFaceList(ent, t, numfaces, facelist);
2168 void R_Q3BSP_DrawSky(entity_render_t *ent)
2170 if (r_drawcollisionbrushes.integer < 2)
2171 R_Q3BSP_DrawFaces(ent, true);
2174 void R_Q3BSP_Draw(entity_render_t *ent)
2176 if (r_drawcollisionbrushes.integer < 2)
2177 R_Q3BSP_DrawFaces(ent, false);
2178 if (r_drawcollisionbrushes.integer >= 1)
2181 model_t *model = ent->model;
2183 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2184 GL_DepthMask(false);
2186 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2187 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2188 if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2189 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2190 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2191 if (face->mesh.num_collisiontriangles)
2192 R_Q3BSP_DrawCollisionFace(ent, face);
2193 qglPolygonOffset(0, 0);
2197 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)
2199 model_t *model = ent->model;
2200 vec3_t lightmins, lightmaxs;
2201 int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2204 q3msurface_t *surface;
2207 lightmins[0] = relativelightorigin[0] - lightradius;
2208 lightmins[1] = relativelightorigin[1] - lightradius;
2209 lightmins[2] = relativelightorigin[2] - lightradius;
2210 lightmaxs[0] = relativelightorigin[0] + lightradius;
2211 lightmaxs[1] = relativelightorigin[1] + lightradius;
2212 lightmaxs[2] = relativelightorigin[2] + lightradius;
2213 *outnumclusterspointer = 0;
2214 *outnumsurfacespointer = 0;
2215 memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2216 memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2219 VectorCopy(lightmins, outmins);
2220 VectorCopy(lightmaxs, outmaxs);
2223 VectorCopy(relativelightorigin, outmins);
2224 VectorCopy(relativelightorigin, outmaxs);
2225 if (model->brush.GetPVS)
2226 pvs = model->brush.GetPVS(model, relativelightorigin);
2229 // FIXME: use BSP recursion as lights are often small
2230 for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2232 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2234 outmins[0] = min(outmins[0], leaf->mins[0]);
2235 outmins[1] = min(outmins[1], leaf->mins[1]);
2236 outmins[2] = min(outmins[2], leaf->mins[2]);
2237 outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2238 outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2239 outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2242 if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2244 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2245 outclusterlist[outnumclusters++] = leaf->clusterindex;
2250 for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2252 surface = leaf->firstleafface[marksurfaceindex];
2253 surfaceindex = surface - model->brushq3.data_faces;
2254 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2256 if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)) && surface->mesh.num_triangles)
2258 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2260 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2262 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2263 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2264 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2265 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])))
2267 SETPVSBIT(outsurfacepvs, surfaceindex);
2268 outsurfacelist[outnumsurfaces++] = surfaceindex;
2275 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
2277 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2278 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2279 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2280 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])))
2282 SETPVSBIT(outsurfacepvs, surfaceindex);
2283 outsurfacelist[outnumsurfaces++] = surfaceindex;
2295 // limit combined leaf box to light boundaries
2296 outmins[0] = max(outmins[0], lightmins[0]);
2297 outmins[1] = max(outmins[1], lightmins[1]);
2298 outmins[2] = max(outmins[2], lightmins[2]);
2299 outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2300 outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2301 outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2303 *outnumclusterspointer = outnumclusters;
2304 *outnumsurfacespointer = outnumsurfaces;
2307 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2309 model_t *model = ent->model;
2310 vec3_t lightmins, lightmaxs;
2311 q3msurface_t *surface;
2312 int surfacelistindex;
2313 if (r_drawcollisionbrushes.integer < 2)
2315 lightmins[0] = relativelightorigin[0] - lightradius;
2316 lightmins[1] = relativelightorigin[1] - lightradius;
2317 lightmins[2] = relativelightorigin[2] - lightradius;
2318 lightmaxs[0] = relativelightorigin[0] + lightradius;
2319 lightmaxs[1] = relativelightorigin[1] + lightradius;
2320 lightmaxs[2] = relativelightorigin[2] + lightradius;
2321 R_Mesh_Matrix(&ent->matrix);
2322 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2323 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2325 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2326 // FIXME: check some manner of face->rendermode here?
2327 if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && !(surface->texture->surfaceparms & (Q3SURFACEPARM_SKY | Q3SURFACEPARM_TRANS)) && !(surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED))
2328 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);
2330 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);
2334 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)
2336 model_t *model = ent->model;
2337 vec3_t lightmins, lightmaxs, modelorg;
2338 q3msurface_t *surface;
2339 int surfacelistindex;
2340 if (r_drawcollisionbrushes.integer < 2)
2342 lightmins[0] = relativelightorigin[0] - lightradius;
2343 lightmins[1] = relativelightorigin[1] - lightradius;
2344 lightmins[2] = relativelightorigin[2] - lightradius;
2345 lightmaxs[0] = relativelightorigin[0] + lightradius;
2346 lightmaxs[1] = relativelightorigin[1] + lightradius;
2347 lightmaxs[2] = relativelightorigin[2] + lightradius;
2348 R_Mesh_Matrix(&ent->matrix);
2349 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2350 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2352 surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2353 if (r_shadow_compilingrtlight)
2355 // if compiling an rtlight, capture the mesh
2356 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);
2358 else if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->mesh.num_triangles)
2360 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2361 qglDisable(GL_CULL_FACE);
2362 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);
2363 if (surface->texture->textureflags & Q3TEXTUREFLAG_TWOSIDED)
2364 qglEnable(GL_CULL_FACE);
2370 static void gl_surf_start(void)
2372 r_surf_mempool = Mem_AllocPool("gl_rsurf", 0, NULL);
2373 r_surf_surfacevisiblelimit = 65536;
2374 r_surf_surfacevisible = Mem_Alloc(r_surf_mempool, r_surf_surfacevisiblelimit);
2377 static void gl_surf_shutdown(void)
2379 r_surf_surfacevisiblelimit = 0;
2380 r_surf_surfacevisible = NULL;
2381 Mem_FreePool(&r_surf_mempool);
2384 static void gl_surf_newmap(void)
2388 void GL_Surf_Init(void)
2391 dlightdivtable[0] = 4194304;
2392 for (i = 1;i < 32768;i++)
2393 dlightdivtable[i] = 4194304 / (i << 7);
2395 Cvar_RegisterVariable(&r_ambient);
2396 Cvar_RegisterVariable(&r_drawportals);
2397 Cvar_RegisterVariable(&r_testvis);
2398 Cvar_RegisterVariable(&r_floatbuildlightmap);
2399 Cvar_RegisterVariable(&r_detailtextures);
2400 Cvar_RegisterVariable(&r_surfaceworldnode);
2401 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2402 Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2403 Cvar_RegisterVariable(&r_q3bsp_renderskydepth);
2404 Cvar_RegisterVariable(&gl_lightmaps);
2406 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);