]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
credited Elric for some of his great contributions, and removed the warnings about...
[divverent/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24
25 #define MAX_LIGHTMAP_SIZE 256
26
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
29
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
40 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
41
42 /*
43 // FIXME: these arrays are huge!
44 int r_q1bsp_maxmarkleafs;
45 int r_q1bsp_nummarkleafs;
46 mleaf_t *r_q1bsp_maxleaflist[65536];
47 int r_q1bsp_maxmarksurfaces;
48 int r_q1bsp_nummarksurfaces;
49 msurface_t *r_q1bsp_maxsurfacelist[65536];
50
51 // FIXME: these arrays are huge!
52 int r_q3bsp_maxmarkleafs;
53 int r_q3bsp_nummarkleafs;
54 q3mleaf_t *r_q3bsp_maxleaflist[65536];
55 int r_q3bsp_maxmarksurfaces;
56 int r_q3bsp_nummarksurfaces;
57 q3msurface_t *r_q3bsp_maxsurfacelist[65536];
58 */
59
60 static int dlightdivtable[32768];
61
62 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
63 {
64         int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
65         unsigned int *bl;
66         float dist, impact[3], local[3];
67         dlight_t *light;
68
69         lit = false;
70
71         smax = (surf->extents[0] >> 4) + 1;
72         tmax = (surf->extents[1] >> 4) + 1;
73         smax3 = smax * 3;
74
75         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
76         {
77                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
78                         continue;                                       // not lit by this light
79
80                 Matrix4x4_Transform(matrix, light->origin, local);
81                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
82
83                 // for comparisons to minimum acceptable light
84                 // compensate for LIGHTOFFSET
85                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
86
87                 dist2 = dist * dist;
88                 dist2 += LIGHTOFFSET;
89                 if (dist2 >= maxdist)
90                         continue;
91
92                 if (surf->plane->type < 3)
93                 {
94                         VectorCopy(local, impact);
95                         impact[surf->plane->type] -= dist;
96                 }
97                 else
98                 {
99                         impact[0] = local[0] - surf->plane->normal[0] * dist;
100                         impact[1] = local[1] - surf->plane->normal[1] * dist;
101                         impact[2] = local[2] - surf->plane->normal[2] * dist;
102                 }
103
104                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
105                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
106
107                 s = bound(0, impacts, smax * 16) - impacts;
108                 t = bound(0, impactt, tmax * 16) - impactt;
109                 i = s * s + t * t + dist2;
110                 if (i > maxdist)
111                         continue;
112
113                 // reduce calculations
114                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
115                         sdtable[s] = i * i + dist2;
116
117                 maxdist3 = maxdist - dist2;
118
119                 // convert to 8.8 blocklights format
120                 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
121                 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
122                 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
123                 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
124                 bl = intblocklights;
125
126                 i = impactt;
127                 for (t = 0;t < tmax;t++, i -= 16)
128                 {
129                         td = i * i;
130                         // make sure some part of it is visible on this line
131                         if (td < maxdist3)
132                         {
133                                 maxdist2 = maxdist - td;
134                                 for (s = 0;s < smax;s++)
135                                 {
136                                         if (sdtable[s] < maxdist2)
137                                         {
138                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
139                                                 if (k > 0)
140                                                 {
141                                                         bl[0] += (red   * k);
142                                                         bl[1] += (green * k);
143                                                         bl[2] += (blue  * k);
144                                                         lit = true;
145                                                 }
146                                         }
147                                         bl += 3;
148                                 }
149                         }
150                         else // skip line
151                                 bl += smax3;
152                 }
153         }
154         return lit;
155 }
156
157 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
158 {
159         int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
160         float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
161         dlight_t *light;
162
163         lit = false;
164
165         smax = (surf->extents[0] >> 4) + 1;
166         tmax = (surf->extents[1] >> 4) + 1;
167         smax3 = smax * 3;
168
169         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
170         {
171                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
172                         continue;                                       // not lit by this light
173
174                 Matrix4x4_Transform(matrix, light->origin, local);
175                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
176
177                 // for comparisons to minimum acceptable light
178                 // compensate for LIGHTOFFSET
179                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
180
181                 dist2 = dist * dist;
182                 dist2 += LIGHTOFFSET;
183                 if (dist2 >= maxdist)
184                         continue;
185
186                 if (surf->plane->type < 3)
187                 {
188                         VectorCopy(local, impact);
189                         impact[surf->plane->type] -= dist;
190                 }
191                 else
192                 {
193                         impact[0] = local[0] - surf->plane->normal[0] * dist;
194                         impact[1] = local[1] - surf->plane->normal[1] * dist;
195                         impact[2] = local[2] - surf->plane->normal[2] * dist;
196                 }
197
198                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
199                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
200
201                 td = bound(0, impacts, smax * 16) - impacts;
202                 td1 = bound(0, impactt, tmax * 16) - impactt;
203                 td = td * td + td1 * td1 + dist2;
204                 if (td > maxdist)
205                         continue;
206
207                 // reduce calculations
208                 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
209                         sdtable[s] = td1 * td1 + dist2;
210
211                 maxdist3 = maxdist - dist2;
212
213                 // convert to 8.8 blocklights format
214                 red = light->rtlight.lightmap_light[0];
215                 green = light->rtlight.lightmap_light[1];
216                 blue = light->rtlight.lightmap_light[2];
217                 subtract = light->rtlight.lightmap_subtract * 32768.0f;
218                 bl = floatblocklights;
219
220                 td1 = impactt;
221                 for (t = 0;t < tmax;t++, td1 -= 16.0f)
222                 {
223                         td = td1 * td1;
224                         // make sure some part of it is visible on this line
225                         if (td < maxdist3)
226                         {
227                                 maxdist2 = maxdist - td;
228                                 for (s = 0;s < smax;s++)
229                                 {
230                                         if (sdtable[s] < maxdist2)
231                                         {
232                                                 k = (32768.0f / (sdtable[s] + td)) - subtract;
233                                                 bl[0] += red   * k;
234                                                 bl[1] += green * k;
235                                                 bl[2] += blue  * k;
236                                                 lit = true;
237                                         }
238                                         bl += 3;
239                                 }
240                         }
241                         else // skip line
242                                 bl += smax3;
243                 }
244         }
245         return lit;
246 }
247
248 /*
249 ===============
250 R_BuildLightMap
251
252 Combine and scale multiple lightmaps into the 8.8 format in blocklights
253 ===============
254 */
255 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
256 {
257         if (!r_floatbuildlightmap.integer)
258         {
259                 int smax, tmax, i, j, size, size3, maps, stride, l;
260                 unsigned int *bl, scale;
261                 qbyte *lightmap, *out, *stain;
262
263                 // update cached lighting info
264                 surf->cached_dlight = 0;
265
266                 smax = (surf->extents[0]>>4)+1;
267                 tmax = (surf->extents[1]>>4)+1;
268                 size = smax*tmax;
269                 size3 = size*3;
270                 lightmap = surf->samples;
271
272         // set to full bright if no light data
273                 bl = intblocklights;
274                 if (!ent->model->brushq1.lightdata)
275                 {
276                         for (i = 0;i < size3;i++)
277                                 bl[i] = 255*256;
278                 }
279                 else
280                 {
281         // clear to no light
282                         memset(bl, 0, size*3*sizeof(unsigned int));
283
284                         if (surf->dlightframe == r_framecount)
285                         {
286                                 surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
287                                 if (surf->cached_dlight)
288                                         c_light_polys++;
289                         }
290
291         // add all the lightmaps
292                         if (lightmap)
293                         {
294                                 bl = intblocklights;
295                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
296                                         for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
297                                                 bl[i] += lightmap[i] * scale;
298                         }
299                 }
300
301                 stain = surf->stainsamples;
302                 bl = intblocklights;
303                 out = templight;
304                 // the >> 16 shift adjusts down 8 bits to account for the stainmap
305                 // scaling, and remaps the 0-65536 (2x overbright) to 0-256, it will
306                 // be doubled during rendering to achieve 2x overbright
307                 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
308                 if (ent->model->brushq1.lightmaprgba)
309                 {
310                         stride = (surf->lightmaptexturestride - smax) * 4;
311                         for (i = 0;i < tmax;i++, out += stride)
312                         {
313                                 for (j = 0;j < smax;j++)
314                                 {
315                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
316                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
317                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
318                                         *out++ = 255;
319                                 }
320                         }
321                 }
322                 else
323                 {
324                         stride = (surf->lightmaptexturestride - smax) * 3;
325                         for (i = 0;i < tmax;i++, out += stride)
326                         {
327                                 for (j = 0;j < smax;j++)
328                                 {
329                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
330                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
331                                         l = (*bl++ * *stain++) >> 16;*out++ = min(l, 255);
332                                 }
333                         }
334                 }
335
336                 R_UpdateTexture(surf->lightmaptexture, templight);
337         }
338         else
339         {
340                 int smax, tmax, i, j, size, size3, maps, stride, l;
341                 float *bl, scale;
342                 qbyte *lightmap, *out, *stain;
343
344                 // update cached lighting info
345                 surf->cached_dlight = 0;
346
347                 smax = (surf->extents[0]>>4)+1;
348                 tmax = (surf->extents[1]>>4)+1;
349                 size = smax*tmax;
350                 size3 = size*3;
351                 lightmap = surf->samples;
352
353         // set to full bright if no light data
354                 bl = floatblocklights;
355                 if (!ent->model->brushq1.lightdata)
356                 {
357                         for (i = 0;i < size3;i++)
358                                 bl[i] = 255*256;
359                 }
360                 else
361                 {
362                         memset(bl, 0, size*3*sizeof(float));
363         
364                         if (surf->dlightframe == r_framecount)
365                         {
366                                 surf->cached_dlight = R_FloatAddDynamicLights(&ent->inversematrix, surf);
367                                 if (surf->cached_dlight)
368                                         c_light_polys++;
369                         }
370         
371                         // add all the lightmaps
372                         if (lightmap)
373                         {
374                                 bl = floatblocklights;
375                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
376                                         for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
377                                                 bl[i] += lightmap[i] * scale;
378                         }
379                 }
380
381                 stain = surf->stainsamples;
382                 bl = floatblocklights;
383                 out = templight;
384                 // this scaling adjusts down 8 bits to account for the stainmap
385                 // scaling, and remaps the 0.0-2.0 (2x overbright) to 0-256, it will
386                 // be doubled during rendering to achieve 2x overbright
387                 // (0 = 0.0, 128 = 1.0, 256 = 2.0)
388                 scale = 1.0f / (1 << 16);
389                 if (ent->model->brushq1.lightmaprgba)
390                 {
391                         stride = (surf->lightmaptexturestride - smax) * 4;
392                         for (i = 0;i < tmax;i++, out += stride)
393                         {
394                                 for (j = 0;j < smax;j++)
395                                 {
396                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
397                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
398                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
399                                         *out++ = 255;
400                                 }
401                         }
402                 }
403                 else
404                 {
405                         stride = (surf->lightmaptexturestride - smax) * 3;
406                         for (i = 0;i < tmax;i++, out += stride)
407                         {
408                                 for (j = 0;j < smax;j++)
409                                 {
410                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
412                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
413                                 }
414                         }
415                 }
416
417                 R_UpdateTexture(surf->lightmaptexture, templight);
418         }
419 }
420
421 void R_StainNode (mnode_t *node, model_t *model, const vec3_t origin, float radius, const float fcolor[8])
422 {
423         float ndist, a, ratio, maxdist, maxdist2, maxdist3, invradius, sdtable[256], td, dist2;
424         msurface_t *surf, *endsurf;
425         int i, s, t, smax, tmax, smax3, impacts, impactt, stained;
426         qbyte *bl;
427         vec3_t impact;
428
429         maxdist = radius * radius;
430         invradius = 1.0f / radius;
431
432 loc0:
433         if (node->contents < 0)
434                 return;
435         ndist = PlaneDiff(origin, node->plane);
436         if (ndist > radius)
437         {
438                 node = node->children[0];
439                 goto loc0;
440         }
441         if (ndist < -radius)
442         {
443                 node = node->children[1];
444                 goto loc0;
445         }
446
447         dist2 = ndist * ndist;
448         maxdist3 = maxdist - dist2;
449
450         if (node->plane->type < 3)
451         {
452                 VectorCopy(origin, impact);
453                 impact[node->plane->type] -= ndist;
454         }
455         else
456         {
457                 impact[0] = origin[0] - node->plane->normal[0] * ndist;
458                 impact[1] = origin[1] - node->plane->normal[1] * ndist;
459                 impact[2] = origin[2] - node->plane->normal[2] * ndist;
460         }
461
462         for (surf = model->brushq1.surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
463         {
464                 if (surf->stainsamples)
465                 {
466                         smax = (surf->extents[0] >> 4) + 1;
467                         tmax = (surf->extents[1] >> 4) + 1;
468
469                         impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
470                         impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
471
472                         s = bound(0, impacts, smax * 16) - impacts;
473                         t = bound(0, impactt, tmax * 16) - impactt;
474                         i = s * s + t * t + dist2;
475                         if (i > maxdist)
476                                 continue;
477
478                         // reduce calculations
479                         for (s = 0, i = impacts; s < smax; s++, i -= 16)
480                                 sdtable[s] = i * i + dist2;
481
482                         bl = surf->stainsamples;
483                         smax3 = smax * 3;
484                         stained = false;
485
486                         i = impactt;
487                         for (t = 0;t < tmax;t++, i -= 16)
488                         {
489                                 td = i * i;
490                                 // make sure some part of it is visible on this line
491                                 if (td < maxdist3)
492                                 {
493                                         maxdist2 = maxdist - td;
494                                         for (s = 0;s < smax;s++)
495                                         {
496                                                 if (sdtable[s] < maxdist2)
497                                                 {
498                                                         ratio = lhrandom(0.0f, 1.0f);
499                                                         a = (fcolor[3] + ratio * fcolor[7]) * (1.0f - sqrt(sdtable[s] + td) * invradius);
500                                                         if (a >= (1.0f / 64.0f))
501                                                         {
502                                                                 if (a > 1)
503                                                                         a = 1;
504                                                                 bl[0] = (qbyte) ((float) bl[0] + a * ((fcolor[0] + ratio * fcolor[4]) - (float) bl[0]));
505                                                                 bl[1] = (qbyte) ((float) bl[1] + a * ((fcolor[1] + ratio * fcolor[5]) - (float) bl[1]));
506                                                                 bl[2] = (qbyte) ((float) bl[2] + a * ((fcolor[2] + ratio * fcolor[6]) - (float) bl[2]));
507                                                                 stained = true;
508                                                         }
509                                                 }
510                                                 bl += 3;
511                                         }
512                                 }
513                                 else // skip line
514                                         bl += smax3;
515                         }
516                         // force lightmap upload
517                         if (stained)
518                                 surf->cached_dlight = true;
519                 }
520         }
521
522         if (node->children[0]->contents >= 0)
523         {
524                 if (node->children[1]->contents >= 0)
525                 {
526                         R_StainNode(node->children[0], model, origin, radius, fcolor);
527                         node = node->children[1];
528                         goto loc0;
529                 }
530                 else
531                 {
532                         node = node->children[0];
533                         goto loc0;
534                 }
535         }
536         else if (node->children[1]->contents >= 0)
537         {
538                 node = node->children[1];
539                 goto loc0;
540         }
541 }
542
543 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)
544 {
545         int n;
546         float fcolor[8];
547         entity_render_t *ent;
548         model_t *model;
549         vec3_t org;
550         if (cl.worldmodel == NULL || !cl.worldmodel->brushq1.nodes)
551                 return;
552         fcolor[0] = cr1;
553         fcolor[1] = cg1;
554         fcolor[2] = cb1;
555         fcolor[3] = ca1 * (1.0f / 64.0f);
556         fcolor[4] = cr2 - cr1;
557         fcolor[5] = cg2 - cg1;
558         fcolor[6] = cb2 - cb1;
559         fcolor[7] = (ca2 - ca1) * (1.0f / 64.0f);
560
561         R_StainNode(cl.worldmodel->brushq1.nodes + cl.worldmodel->brushq1.hulls[0].firstclipnode, cl.worldmodel, origin, radius, fcolor);
562
563         // look for embedded bmodels
564         for (n = 0;n < cl_num_brushmodel_entities;n++)
565         {
566                 ent = cl_brushmodel_entities[n];
567                 model = ent->model;
568                 if (model && model->name[0] == '*')
569                 {
570                         Mod_CheckLoaded(model);
571                         if (model->brushq1.nodes)
572                         {
573                                 Matrix4x4_Transform(&ent->inversematrix, origin, org);
574                                 R_StainNode(model->brushq1.nodes + model->brushq1.hulls[0].firstclipnode, model, org, radius, fcolor);
575                         }
576                 }
577         }
578 }
579
580
581 /*
582 =============================================================
583
584         BRUSH MODELS
585
586 =============================================================
587 */
588
589 static void RSurf_AddLightmapToVertexColors_Color4f(const int *lightmapoffsets, float *c, int numverts, const qbyte *samples, int size3, const qbyte *styles)
590 {
591         int i;
592         float scale;
593         const qbyte *lm;
594         if (styles[0] != 255)
595         {
596                 for (i = 0;i < numverts;i++, c += 4)
597                 {
598                         lm = samples + lightmapoffsets[i];
599                         scale = d_lightstylevalue[styles[0]] * (1.0f / 32768.0f);
600                         VectorMA(c, scale, lm, c);
601                         if (styles[1] != 255)
602                         {
603                                 lm += size3;
604                                 scale = d_lightstylevalue[styles[1]] * (1.0f / 32768.0f);
605                                 VectorMA(c, scale, lm, c);
606                                 if (styles[2] != 255)
607                                 {
608                                         lm += size3;
609                                         scale = d_lightstylevalue[styles[2]] * (1.0f / 32768.0f);
610                                         VectorMA(c, scale, lm, c);
611                                         if (styles[3] != 255)
612                                         {
613                                                 lm += size3;
614                                                 scale = d_lightstylevalue[styles[3]] * (1.0f / 32768.0f);
615                                                 VectorMA(c, scale, lm, c);
616                                         }
617                                 }
618                         }
619                 }
620         }
621 }
622
623 static void RSurf_FogColors_Vertex3f_Color4f(const float *v, float *c, float colorscale, int numverts, const float *modelorg)
624 {
625         int i;
626         float diff[3], f;
627         if (fogenabled)
628         {
629                 for (i = 0;i < numverts;i++, v += 3, c += 4)
630                 {
631                         VectorSubtract(v, modelorg, diff);
632                         f = colorscale * (1 - exp(fogdensity/DotProduct(diff, diff)));
633                         VectorScale(c, f, c);
634                 }
635         }
636         else if (colorscale != 1)
637                 for (i = 0;i < numverts;i++, c += 4)
638                         VectorScale(c, colorscale, c);
639 }
640
641 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)
642 {
643         int i;
644         float diff[3], f;
645         r *= colorscale;
646         g *= colorscale;
647         b *= colorscale;
648         if (fogenabled)
649         {
650                 for (i = 0;i < numverts;i++, v += 3, c += 4)
651                 {
652                         VectorSubtract(v, modelorg, diff);
653                         f = 1 - exp(fogdensity/DotProduct(diff, diff));
654                         c[0] = r * f;
655                         c[1] = g * f;
656                         c[2] = b * f;
657                         c[3] = a;
658                 }
659         }
660         else
661         {
662                 for (i = 0;i < numverts;i++, c += 4)
663                 {
664                         c[0] = r;
665                         c[1] = g;
666                         c[2] = b;
667                         c[3] = a;
668                 }
669         }
670 }
671
672 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)
673 {
674         int i;
675         float diff[3], f;
676         r *= colorscale;
677         g *= colorscale;
678         b *= colorscale;
679         for (i = 0;i < numverts;i++, v += 3, c += 4)
680         {
681                 VectorSubtract(v, modelorg, diff);
682                 f = exp(fogdensity/DotProduct(diff, diff));
683                 c[0] = r;
684                 c[1] = g;
685                 c[2] = b;
686                 c[3] = a * f;
687         }
688 }
689
690 static int RSurf_LightSeparate_Vertex3f_Color4f(const matrix4x4_t *matrix, const int *dlightbits, int numverts, const float *vert, float *color, float scale)
691 {
692         float f;
693         const float *v;
694         float *c;
695         int i, l, lit = false;
696         const dlight_t *light;
697         vec3_t lightorigin;
698         for (l = 0;l < r_numdlights;l++)
699         {
700                 if (dlightbits[l >> 5] & (1 << (l & 31)))
701                 {
702                         light = &r_dlight[l];
703                         Matrix4x4_Transform(matrix, light->origin, lightorigin);
704                         for (i = 0, v = vert, c = color;i < numverts;i++, v += 3, c += 4)
705                         {
706                                 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
707                                 if (f < light->rtlight.lightmap_cullradius2)
708                                 {
709                                         f = ((1.0f / f) - light->rtlight.lightmap_subtract) * scale;
710                                         VectorMA(c, f, light->rtlight.lightmap_light, c);
711                                         lit = true;
712                                 }
713                         }
714                 }
715         }
716         return lit;
717 }
718
719 static void RSurfShader_Sky(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
720 {
721         const msurface_t *surf;
722         rmeshstate_t m;
723
724         // LordHavoc: HalfLife maps have freaky skypolys...
725         if (ent->model->brush.ishlbsp)
726                 return;
727
728         if (skyrendernow)
729         {
730                 skyrendernow = false;
731                 if (skyrendermasked)
732                         R_Sky();
733         }
734
735         R_Mesh_Matrix(&ent->matrix);
736
737         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
738         if (skyrendermasked)
739         {
740                 // depth-only (masking)
741                 GL_ColorMask(0,0,0,0);
742                 // just to make sure that braindead drivers don't draw anything
743                 // despite that colormask...
744                 GL_BlendFunc(GL_ZERO, GL_ONE);
745         }
746         else
747         {
748                 // fog sky
749                 GL_BlendFunc(GL_ONE, GL_ZERO);
750         }
751         GL_DepthMask(true);
752         GL_DepthTest(true);
753
754         memset(&m, 0, sizeof(m));
755         while((surf = *surfchain++) != NULL)
756         {
757                 if (surf->visframe == r_framecount)
758                 {
759                         m.pointer_vertex = surf->mesh.data_vertex3f;
760                         R_Mesh_State(&m);
761                         GL_LockArrays(0, surf->mesh.num_vertices);
762                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
763                         GL_LockArrays(0, 0);
764                 }
765         }
766         GL_ColorMask(1,1,1,1);
767 }
768
769 static void RSurfShader_Water_Callback(const void *calldata1, int calldata2)
770 {
771         const entity_render_t *ent = calldata1;
772         const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
773         rmeshstate_t m;
774         float currentalpha;
775         float modelorg[3];
776         texture_t *texture;
777         float args[4] = {0.05f,0,0,0.04f};
778         int rendertype;
779
780         R_Mesh_Matrix(&ent->matrix);
781         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
782
783         texture = surf->texinfo->texture->currentframe;
784         currentalpha = texture->currentalpha;
785         if (ent->effects & EF_ADDITIVE)
786         {
787                 rendertype = SURFRENDER_ADD;
788                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
789                 GL_DepthMask(false);
790         }
791         else if (currentalpha < 1 || texture->skin.fog != NULL)
792         {
793                 rendertype = SURFRENDER_ALPHA;
794                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
795                 GL_DepthMask(false);
796         }
797         else
798         {
799                 rendertype = SURFRENDER_OPAQUE;
800                 GL_BlendFunc(GL_ONE, GL_ZERO);
801                 GL_DepthMask(true);
802         }
803         GL_DepthTest(true);
804         GL_Color(1, 1, 1, currentalpha);
805         memset(&m, 0, sizeof(m));
806         m.pointer_vertex = surf->mesh.data_vertex3f;
807         if (gl_textureshader && r_watershader.value && !fogenabled)
808         {
809                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
810                 m.tex[1] = R_GetTexture(texture->skin.base);
811                 m.texcombinergb[0] = GL_REPLACE;
812                 m.texcombinergb[1] = GL_REPLACE;
813                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
814                 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
815                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
816                 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
817                 R_Mesh_State(&m);
818
819                 GL_ActiveTexture(0);
820                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
821                 GL_ActiveTexture(1);
822                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
823                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
824                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
825                 qglEnable(GL_TEXTURE_SHADER_NV);
826
827                 GL_LockArrays(0, surf->mesh.num_vertices);
828                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
829                 GL_LockArrays(0, 0);
830
831                 qglDisable(GL_TEXTURE_SHADER_NV);
832                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
833                 GL_ActiveTexture(0);
834         }
835         else
836         {
837                 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
838                 m.pointer_color = varray_color4f;
839                 m.tex[0] = R_GetTexture(texture->skin.base);
840                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
841                 if (r_waterscroll.value)
842                 {
843                         // scrolling in texture matrix
844                         Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
845                 }
846                 R_Mesh_State(&m);
847                 GL_LockArrays(0, surf->mesh.num_vertices);
848                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
849                 GL_LockArrays(0, 0);
850                 if (fogenabled && rendertype != SURFRENDER_ADD)
851                 {
852                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
853                         GL_DepthMask(false);
854                         RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
855                         m.tex[0] = R_GetTexture(texture->skin.fog);
856                         R_Mesh_State(&m);
857                         GL_LockArrays(0, surf->mesh.num_vertices);
858                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
859                         GL_LockArrays(0, 0);
860                 }
861         }
862 }
863
864 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
865 {
866         const entity_render_t *ent = calldata1;
867         const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
868         int rendertype;
869         float currentalpha;
870         texture_t *texture;
871         float base, colorscale;
872         rmeshstate_t m;
873         vec3_t modelorg;
874         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
875         R_Mesh_Matrix(&ent->matrix);
876
877         texture = surf->texinfo->texture;
878         if (texture->animated)
879                 texture = texture->anim_frames[ent->frame != 0][(texture->anim_total[ent->frame != 0] >= 2) ? ((int) (cl.time * 5.0f) % texture->anim_total[ent->frame != 0]) : 0];
880
881         currentalpha = ent->alpha;
882         base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
883         GL_DepthTest(true);
884         if (ent->effects & EF_ADDITIVE)
885         {
886                 rendertype = SURFRENDER_ADD;
887                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
888                 GL_DepthMask(false);
889         }
890         else if (currentalpha < 1 || texture->skin.fog != NULL)
891         {
892                 rendertype = SURFRENDER_ALPHA;
893                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
894                 GL_DepthMask(false);
895         }
896         else
897         {
898                 rendertype = SURFRENDER_OPAQUE;
899                 GL_BlendFunc(GL_ONE, GL_ZERO);
900                 GL_DepthMask(true);
901         }
902
903         {
904                 memset(&m, 0, sizeof(m));
905                 m.tex[0] = R_GetTexture(texture->skin.base);
906                 colorscale = 1;
907                 if (gl_combine.integer)
908                 {
909                         m.texrgbscale[0] = 4;
910                         colorscale *= 0.25f;
911                 }
912                 m.pointer_color = varray_color4f;
913                 m.pointer_vertex = surf->mesh.data_vertex3f;
914                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
915                 R_Mesh_State(&m);
916                 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
917                 if (!(ent->effects & EF_FULLBRIGHT))
918                 {
919                         if (surf->dlightframe == r_framecount)
920                                 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
921                         if (surf->flags & SURF_LIGHTMAP)
922                                 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);
923                 }
924                 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
925                 GL_LockArrays(0, surf->mesh.num_vertices);
926                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
927                 GL_LockArrays(0, 0);
928         }
929         if (texture->skin.glow)
930         {
931                 memset(&m, 0, sizeof(m));
932                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
933                 GL_DepthMask(false);
934                 m.pointer_color = varray_color4f;
935                 m.tex[0] = R_GetTexture(texture->skin.glow);
936                 m.pointer_vertex = surf->mesh.data_vertex3f;
937                 if (m.tex[0])
938                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
939                 R_Mesh_State(&m);
940                 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
941                 GL_LockArrays(0, surf->mesh.num_vertices);
942                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
943                 GL_LockArrays(0, 0);
944         }
945         if (fogenabled && rendertype != SURFRENDER_ADD)
946         {
947                 memset(&m, 0, sizeof(m));
948                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
949                 GL_DepthMask(false);
950                 m.pointer_color = varray_color4f;
951                 m.tex[0] = R_GetTexture(texture->skin.fog);
952                 m.pointer_vertex = surf->mesh.data_vertex3f;
953                 if (m.tex[0])
954                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
955                 R_Mesh_State(&m);
956                 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
957                 GL_LockArrays(0, surf->mesh.num_vertices);
958                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
959                 GL_LockArrays(0, 0);
960         }
961 }
962
963 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
964 {
965         const msurface_t *surf;
966         rmeshstate_t m;
967         int lightmaptexturenum;
968         memset(&m, 0, sizeof(m));
969         GL_BlendFunc(GL_ONE, GL_ZERO);
970         GL_DepthMask(true);
971         GL_DepthTest(true);
972         m.tex[0] = R_GetTexture(texture->skin.base);
973         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
974         m.texrgbscale[1] = 2;
975         m.tex[2] = R_GetTexture(texture->skin.detail);
976         m.texrgbscale[2] = 2;
977         if (texture->skin.glow)
978         {
979                 m.tex[3] = R_GetTexture(texture->skin.glow);
980                 m.texcombinergb[3] = GL_ADD;
981         }
982         if (r_shadow_realtime_world.integer)
983                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
984         else
985                 GL_Color(1, 1, 1, 1);
986
987         while((surf = *surfchain++) != NULL)
988         {
989                 if (surf->visframe == r_framecount)
990                 {
991                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
992                         m.tex[1] = lightmaptexturenum;
993                         m.pointer_vertex = surf->mesh.data_vertex3f;
994                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
995                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
996                         m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
997                         m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
998                         R_Mesh_State(&m);
999                         GL_LockArrays(0, surf->mesh.num_vertices);
1000                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1001                         GL_LockArrays(0, 0);
1002                 }
1003         }
1004 }
1005
1006 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1007 {
1008         const msurface_t *surf;
1009         rmeshstate_t m;
1010         int lightmaptexturenum;
1011         memset(&m, 0, sizeof(m));
1012         GL_BlendFunc(GL_ONE, GL_ZERO);
1013         GL_DepthMask(true);
1014         GL_DepthTest(true);
1015         m.tex[0] = R_GetTexture(texture->skin.base);
1016         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1017         m.texrgbscale[1] = 2;
1018         m.tex[2] = R_GetTexture(texture->skin.detail);
1019         m.texrgbscale[2] = 2;
1020         if (r_shadow_realtime_world.integer)
1021                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1022         else
1023                 GL_Color(1, 1, 1, 1);
1024
1025         while((surf = *surfchain++) != NULL)
1026         {
1027                 if (surf->visframe == r_framecount)
1028                 {
1029                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1030                         m.tex[1] = lightmaptexturenum;
1031                         m.pointer_vertex = surf->mesh.data_vertex3f;
1032                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1033                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1034                         m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1035                         R_Mesh_State(&m);
1036                         GL_LockArrays(0, surf->mesh.num_vertices);
1037                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1038                         GL_LockArrays(0, 0);
1039                 }
1040         }
1041 }
1042
1043 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1044 {
1045         const msurface_t *surf;
1046         rmeshstate_t m;
1047         int lightmaptexturenum;
1048         memset(&m, 0, sizeof(m));
1049         GL_BlendFunc(GL_ONE, GL_ZERO);
1050         GL_DepthMask(true);
1051         GL_DepthTest(true);
1052         m.tex[0] = R_GetTexture(texture->skin.base);
1053         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1054         m.texrgbscale[1] = 2;
1055         if (r_shadow_realtime_world.integer)
1056                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1057         else
1058                 GL_Color(1, 1, 1, 1);
1059         while((surf = *surfchain++) != NULL)
1060         {
1061                 if (surf->visframe == r_framecount)
1062                 {
1063                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1064                         m.tex[1] = lightmaptexturenum;
1065                         m.pointer_vertex = surf->mesh.data_vertex3f;
1066                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1067                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1068                         R_Mesh_State(&m);
1069                         GL_LockArrays(0, surf->mesh.num_vertices);
1070                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1071                         GL_LockArrays(0, 0);
1072                 }
1073         }
1074 }
1075
1076 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1077 {
1078         const msurface_t *surf;
1079         rmeshstate_t m;
1080         memset(&m, 0, sizeof(m));
1081         GL_DepthMask(true);
1082         GL_DepthTest(true);
1083         GL_BlendFunc(GL_ONE, GL_ZERO);
1084         m.tex[0] = R_GetTexture(texture->skin.base);
1085         if (r_shadow_realtime_world.integer)
1086                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1087         else
1088                 GL_Color(1, 1, 1, 1);
1089         while((surf = *surfchain++) != NULL)
1090         {
1091                 if (surf->visframe == r_framecount)
1092                 {
1093                         m.pointer_vertex = surf->mesh.data_vertex3f;
1094                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1095                         R_Mesh_State(&m);
1096                         GL_LockArrays(0, surf->mesh.num_vertices);
1097                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1098                         GL_LockArrays(0, 0);
1099                 }
1100         }
1101 }
1102
1103 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1104 {
1105         const msurface_t *surf;
1106         rmeshstate_t m;
1107         int lightmaptexturenum;
1108         memset(&m, 0, sizeof(m));
1109         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1110         GL_DepthMask(false);
1111         GL_DepthTest(true);
1112         m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1113         GL_Color(1, 1, 1, 1);
1114         while((surf = *surfchain++) != NULL)
1115         {
1116                 if (surf->visframe == r_framecount)
1117                 {
1118                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1119                         m.tex[0] = lightmaptexturenum;
1120                         m.pointer_vertex = surf->mesh.data_vertex3f;
1121                         m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1122                         R_Mesh_State(&m);
1123                         GL_LockArrays(0, surf->mesh.num_vertices);
1124                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1125                         GL_LockArrays(0, 0);
1126                 }
1127         }
1128 }
1129
1130 static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1131 {
1132         const msurface_t *surf;
1133         rmeshstate_t m;
1134         memset(&m, 0, sizeof(m));
1135         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1136         GL_DepthMask(false);
1137         GL_DepthTest(true);
1138         m.tex[0] = R_GetTexture(texture->skin.base);
1139         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1140         while((surf = *surfchain++) != NULL)
1141         {
1142                 if (surf->visframe == r_framecount)
1143                 {
1144                         m.pointer_vertex = surf->mesh.data_vertex3f;
1145                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1146                         R_Mesh_State(&m);
1147                         GL_LockArrays(0, surf->mesh.num_vertices);
1148                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1149                         GL_LockArrays(0, 0);
1150                 }
1151         }
1152 }
1153
1154 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1155 {
1156         const msurface_t *surf;
1157         rmeshstate_t m;
1158         float modelorg[3];
1159         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1160         memset(&m, 0, sizeof(m));
1161         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1162         GL_DepthMask(false);
1163         GL_DepthTest(true);
1164         m.pointer_color = varray_color4f;
1165         while((surf = *surfchain++) != NULL)
1166         {
1167                 if (surf->visframe == r_framecount)
1168                 {
1169                         m.pointer_vertex = surf->mesh.data_vertex3f;
1170                         if (m.tex[0])
1171                                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1172                         R_Mesh_State(&m);
1173                         RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1174                         GL_LockArrays(0, surf->mesh.num_vertices);
1175                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1176                         GL_LockArrays(0, 0);
1177                 }
1178         }
1179 }
1180
1181 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1182 {
1183         const msurface_t *surf;
1184         rmeshstate_t m;
1185         memset(&m, 0, sizeof(m));
1186         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1187         GL_DepthMask(false);
1188         GL_DepthTest(true);
1189         m.tex[0] = R_GetTexture(texture->skin.detail);
1190         GL_Color(1, 1, 1, 1);
1191         while((surf = *surfchain++) != NULL)
1192         {
1193                 if (surf->visframe == r_framecount)
1194                 {
1195                         m.pointer_vertex = surf->mesh.data_vertex3f;
1196                         m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1197                         R_Mesh_State(&m);
1198                         GL_LockArrays(0, surf->mesh.num_vertices);
1199                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1200                         GL_LockArrays(0, 0);
1201                 }
1202         }
1203 }
1204
1205 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1206 {
1207         const msurface_t *surf;
1208         rmeshstate_t m;
1209         memset(&m, 0, sizeof(m));
1210         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1211         GL_DepthMask(false);
1212         GL_DepthTest(true);
1213         m.tex[0] = R_GetTexture(texture->skin.glow);
1214         GL_Color(1, 1, 1, 1);
1215         while((surf = *surfchain++) != NULL)
1216         {
1217                 if (surf->visframe == r_framecount)
1218                 {
1219                         m.pointer_vertex = surf->mesh.data_vertex3f;
1220                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1221                         R_Mesh_State(&m);
1222                         GL_LockArrays(0, surf->mesh.num_vertices);
1223                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1224                         GL_LockArrays(0, 0);
1225                 }
1226         }
1227 }
1228
1229 /*
1230 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1231 {
1232         const msurface_t *surf;
1233         rmeshstate_t m;
1234         memset(&m, 0, sizeof(m));
1235         GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1236         GL_DepthMask(true);
1237         m.tex[0] = R_GetTexture(texture->skin.glow);
1238         if (m.tex[0])
1239                 GL_Color(1, 1, 1, 1);
1240         else
1241                 GL_Color(0, 0, 0, 1);
1242         while((surf = *surfchain++) != NULL)
1243         {
1244                 if (surf->visframe == r_framecount)
1245                 {
1246                         m.pointer_vertex = surf->mesh.data_vertex3f;
1247                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1248                         R_Mesh_State(&m);
1249                         GL_LockArrays(0, surf->mesh.num_vertices);
1250                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1251                         GL_LockArrays(0, 0);
1252                 }
1253         }
1254 }
1255 */
1256
1257 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1258 {
1259         const msurface_t *surf;
1260         rmeshstate_t m;
1261         int lightmaptexturenum;
1262         memset(&m, 0, sizeof(m));
1263         GL_BlendFunc(GL_ONE, GL_ZERO);
1264         GL_DepthMask(true);
1265         GL_DepthTest(true);
1266         m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1267         if (r_shadow_realtime_world.integer)
1268                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1269         else
1270                 GL_Color(1, 1, 1, 1);
1271         while((surf = *surfchain++) != NULL)
1272         {
1273                 if (surf->visframe == r_framecount)
1274                 {
1275                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1276                         m.tex[0] = lightmaptexturenum;
1277                         m.pointer_vertex = surf->mesh.data_vertex3f;
1278                         m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1279                         R_Mesh_State(&m);
1280                         GL_LockArrays(0, surf->mesh.num_vertices);
1281                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1282                         GL_LockArrays(0, 0);
1283                 }
1284         }
1285 }
1286
1287 void R_UpdateTextureInfo(entity_render_t *ent)
1288 {
1289         int i, texframe, alttextures;
1290         texture_t *t;
1291
1292         if (!ent->model)
1293                 return;
1294
1295         alttextures = ent->frame != 0;
1296         texframe = (int)(cl.time * 5.0f);
1297         for (i = 0;i < ent->model->brushq1.numtextures;i++)
1298         {
1299                 t = ent->model->brushq1.textures + i;
1300                 t->currentalpha = ent->alpha;
1301                 if (t->flags & SURF_WATERALPHA)
1302                         t->currentalpha *= r_wateralpha.value;
1303                 if (ent->effects & EF_ADDITIVE)
1304                         t->rendertype = SURFRENDER_ADD;
1305                 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1306                         t->rendertype = SURFRENDER_ALPHA;
1307                 else
1308                         t->rendertype = SURFRENDER_OPAQUE;
1309                 // we don't need to set currentframe if t->animated is false because
1310                 // it was already set up by the texture loader for non-animating
1311                 if (t->animated)
1312                         t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1313         }
1314 }
1315
1316 void R_UpdateLightmapInfo(entity_render_t *ent)
1317 {
1318         int i;
1319         msurface_t *surface, **surfacechain;
1320         model_t *model = ent->model;
1321         if (!model)
1322                 return;
1323         if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1324                 R_MarkLights(ent);
1325         for (i = 0;i < model->brushq1.light_styles;i++)
1326         {
1327                 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1328                 {
1329                         model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1330                         for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1331                                 surface->cached_dlight = true;
1332                 }
1333         }
1334 }
1335
1336 void R_PrepareSurfaces(entity_render_t *ent)
1337 {
1338         int i, numsurfaces, *surfacevisframes;
1339         model_t *model;
1340         msurface_t *surf, *surfaces;
1341         vec3_t modelorg;
1342
1343         if (!ent->model)
1344                 return;
1345
1346         model = ent->model;
1347         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1348         numsurfaces = model->nummodelsurfaces;
1349         surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1350         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1351         for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1352         {
1353                 if (surfacevisframes[i] == r_framecount)
1354                 {
1355 #if !WORLDNODECULLBACKFACES
1356                         // mark any backface surfaces as not visible
1357                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1358                         {
1359                                 if (!(surf->flags & SURF_PLANEBACK))
1360                                         surfacevisframes[i] = -1;
1361                         }
1362                         else
1363                         {
1364                                 if ((surf->flags & SURF_PLANEBACK))
1365                                         surfacevisframes[i] = -1;
1366                         }
1367                         if (surfacevisframes[i] == r_framecount)
1368 #endif
1369                         {
1370                                 c_faces++;
1371                                 surf->visframe = r_framecount;
1372                                 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1373                                         R_BuildLightMap(ent, surf);
1374                         }
1375                 }
1376         }
1377 }
1378
1379 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1380 {
1381         int texturenumber, f;
1382         vec3_t center;
1383         msurface_t *surf, **chain, **surfchain;
1384         texture_t *t, *texture;
1385         model_t *model = ent->model;
1386         if (model == NULL)
1387                 return;
1388         R_Mesh_Matrix(&ent->matrix);
1389         for (texturenumber = 0, t = model->brushq1.textures;texturenumber < model->brushq1.numtextures;texturenumber++, t++)
1390         {
1391                 if ((f = t->flags & flagsmask) && (texture = t->currentframe) && (surfchain = model->brushq1.pvstexturechains[texturenumber]) != NULL)
1392                 {
1393                         if (texture->flags & SURF_LIGHTMAP)
1394                         {
1395                                 if (gl_lightmaps.integer)
1396                                 {
1397                                         RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1398                                         if (fogenabled)
1399                                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1400                                 }
1401                                 else if (texture->rendertype != SURFRENDER_OPAQUE)
1402                                 {
1403                                         // transparent vertex shaded from lightmap
1404                                         for (chain = surfchain;(surf = *chain) != NULL;chain++)
1405                                         {
1406                                                 if (surf->visframe == r_framecount)
1407                                                 {
1408                                                         Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1409                                                         R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1410                                                 }
1411                                         }
1412                                 }
1413                                 else
1414                                 {
1415                                         if (ent->effects & EF_FULLBRIGHT || r_fullbright.integer)
1416                                         {
1417                                                 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1418                                                 if (r_detailtextures.integer)
1419                                                         RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1420                                                 if (texture->skin.glow)
1421                                                         RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1422                                         }
1423                                         else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1424                                                 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1425                                         else
1426                                         {
1427                                                 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer && r_ambient.value <= 0)
1428                                                         RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1429                                                 else
1430                                                 {
1431                                                         if (r_textureunits.integer >= 2 && gl_combine.integer)
1432                                                                 RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1433                                                         else
1434                                                         {
1435                                                                 RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1436                                                                 RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1437                                                         }
1438                                                         if (r_ambient.value > 0)
1439                                                                 RSurfShader_OpaqueWall_Pass_BaseAmbient(ent, texture, surfchain);
1440                                                         if (r_detailtextures.integer)
1441                                                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1442                                                 }
1443                                                 if (texture->skin.glow)
1444                                                         RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1445                                         }
1446                                         if (fogenabled)
1447                                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1448                                 }
1449                         }
1450                         else if (texture->flags & SURF_DRAWTURB)
1451                         {
1452                                 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1453                                 {
1454                                         if (surf->visframe == r_framecount)
1455                                         {
1456                                                 if (texture->rendertype == SURFRENDER_OPAQUE)
1457                                                         RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
1458                                                 else
1459                                                 {
1460                                                         Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1461                                                         R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
1462                                                 }
1463                                         }
1464                                 }
1465                         }
1466                         else if (texture->flags & SURF_DRAWSKY)
1467                                 RSurfShader_Sky(ent, texture, surfchain);
1468                 }
1469         }
1470 }
1471
1472 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1473 {
1474         int i;
1475         float *v;
1476         rmeshstate_t m;
1477         const entity_render_t *ent = calldata1;
1478         const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1479         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1480         GL_DepthMask(false);
1481         GL_DepthTest(true);
1482         R_Mesh_Matrix(&ent->matrix);
1483
1484         memset(&m, 0, sizeof(m));
1485         m.pointer_vertex = varray_vertex3f;
1486         R_Mesh_State(&m);
1487
1488         i = portal - ent->model->brushq1.portals;
1489         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1490                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1491                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1492                          0.125f);
1493         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1494         {
1495                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1496                         VectorCopy(portal->points[i].position, v);
1497         }
1498         else
1499                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1500                         VectorCopy(portal->points[i].position, v);
1501         GL_LockArrays(0, portal->numpoints);
1502         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1503         GL_LockArrays(0, 0);
1504 }
1505
1506 // LordHavoc: this is just a nice debugging tool, very slow
1507 static void R_DrawPortals(entity_render_t *ent)
1508 {
1509         int i;
1510         mportal_t *portal, *endportal;
1511         float temp[3], center[3], f;
1512         if (ent->model == NULL)
1513                 return;
1514         for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1515         {
1516                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1517                 {
1518                         VectorClear(temp);
1519                         for (i = 0;i < portal->numpoints;i++)
1520                                 VectorAdd(temp, portal->points[i].position, temp);
1521                         f = ixtable[portal->numpoints];
1522                         VectorScale(temp, f, temp);
1523                         Matrix4x4_Transform(&ent->matrix, temp, center);
1524                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1525                 }
1526         }
1527 }
1528
1529 void R_PrepareBrushModel(entity_render_t *ent)
1530 {
1531         int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1532         msurface_t *surf;
1533         model_t *model;
1534 #if WORLDNODECULLBACKFACES
1535         vec3_t modelorg;
1536 #endif
1537
1538         // because bmodels can be reused, we have to decide which things to render
1539         // from scratch every time
1540         model = ent->model;
1541         if (model == NULL)
1542                 return;
1543 #if WORLDNODECULLBACKFACES
1544         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1545 #endif
1546         numsurfaces = model->nummodelsurfaces;
1547         surf = model->brushq1.surfaces + model->firstmodelsurface;
1548         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1549         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1550         for (i = 0;i < numsurfaces;i++, surf++)
1551         {
1552 #if WORLDNODECULLBACKFACES
1553                 // mark any backface surfaces as not visible
1554                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1555                 {
1556                         if ((surf->flags & SURF_PLANEBACK))
1557                                 surfacevisframes[i] = r_framecount;
1558                 }
1559                 else if (!(surf->flags & SURF_PLANEBACK))
1560                         surfacevisframes[i] = r_framecount;
1561 #else
1562                 surfacevisframes[i] = r_framecount;
1563 #endif
1564                 surf->dlightframe = -1;
1565         }
1566         R_UpdateTextureInfo(ent);
1567         R_UpdateLightmapInfo(ent);
1568 }
1569
1570 void R_SurfaceWorldNode (entity_render_t *ent)
1571 {
1572         int i, *surfacevisframes, *surfacepvsframes, surfnum;
1573         msurface_t *surf;
1574         mleaf_t *leaf;
1575         model_t *model;
1576         vec3_t modelorg;
1577
1578         // equivilant to quake's RecursiveWorldNode but faster and more effective
1579         model = ent->model;
1580         if (model == NULL)
1581                 return;
1582         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1583         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1584         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1585
1586         for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1587         {
1588                 if (!R_CullBox (leaf->mins, leaf->maxs))
1589                 {
1590                         c_leafs++;
1591                         leaf->visframe = r_framecount;
1592                 }
1593         }
1594
1595         for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1596         {
1597                 surfnum = model->brushq1.pvssurflist[i];
1598                 surf = model->brushq1.surfaces + surfnum;
1599 #if WORLDNODECULLBACKFACES
1600                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1601                 {
1602                         if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1603                                 surfacevisframes[surfnum] = r_framecount;
1604                 }
1605                 else
1606                 {
1607                         if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1608                                 surfacevisframes[surfnum] = r_framecount;
1609                 }
1610 #else
1611                 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1612                         surfacevisframes[surfnum] = r_framecount;
1613 #endif
1614         }
1615 }
1616
1617 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1618 {
1619         int c, leafstackpos, *mark, *surfacevisframes;
1620 #if WORLDNODECULLBACKFACES
1621         int n;
1622         msurface_t *surf;
1623 #endif
1624         mleaf_t *leaf, *leafstack[8192];
1625         mportal_t *p;
1626         vec3_t modelorg;
1627         msurface_t *surfaces;
1628         if (ent->model == NULL)
1629                 return;
1630         // LordHavoc: portal-passage worldnode with PVS;
1631         // follows portals leading outward from viewleaf, does not venture
1632         // offscreen or into leafs that are not visible, faster than Quake's
1633         // RecursiveWorldNode
1634         surfaces = ent->model->brushq1.surfaces;
1635         surfacevisframes = ent->model->brushq1.surfacevisframes;
1636         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1637         viewleaf->worldnodeframe = r_framecount;
1638         leafstack[0] = viewleaf;
1639         leafstackpos = 1;
1640         while (leafstackpos)
1641         {
1642                 c_leafs++;
1643                 leaf = leafstack[--leafstackpos];
1644                 leaf->visframe = r_framecount;
1645                 // draw any surfaces bounding this leaf
1646                 if (leaf->nummarksurfaces)
1647                 {
1648                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1649                         {
1650 #if WORLDNODECULLBACKFACES
1651                                 n = *mark++;
1652                                 if (surfacevisframes[n] != r_framecount)
1653                                 {
1654                                         surf = surfaces + n;
1655                                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1656                                         {
1657                                                 if ((surf->flags & SURF_PLANEBACK))
1658                                                         surfacevisframes[n] = r_framecount;
1659                                         }
1660                                         else
1661                                         {
1662                                                 if (!(surf->flags & SURF_PLANEBACK))
1663                                                         surfacevisframes[n] = r_framecount;
1664                                         }
1665                                 }
1666 #else
1667                                 surfacevisframes[*mark++] = r_framecount;
1668 #endif
1669                         }
1670                 }
1671                 // follow portals into other leafs
1672                 for (p = leaf->portals;p;p = p->next)
1673                 {
1674                         // LordHavoc: this DotProduct hurts less than a cache miss
1675                         // (which is more likely to happen if backflowing through leafs)
1676                         if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1677                         {
1678                                 leaf = p->past;
1679                                 if (leaf->worldnodeframe != r_framecount)
1680                                 {
1681                                         leaf->worldnodeframe = r_framecount;
1682                                         // FIXME: R_CullBox is absolute, should be done relative
1683                                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1684                                                 leafstack[leafstackpos++] = leaf;
1685                                 }
1686                         }
1687                 }
1688         }
1689 }
1690
1691 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1692 {
1693         int j, c, *surfacepvsframes, *mark;
1694         mleaf_t *leaf;
1695         model_t *model;
1696
1697         model = ent->model;
1698         if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1699         {
1700                 model->brushq1.pvsframecount++;
1701                 model->brushq1.pvsviewleaf = viewleaf;
1702                 model->brushq1.pvsviewleafnovis = r_novis.integer;
1703                 model->brushq1.pvsleafchain = NULL;
1704                 model->brushq1.pvssurflistlength = 0;
1705                 if (viewleaf)
1706                 {
1707                         surfacepvsframes = model->brushq1.surfacepvsframes;
1708                         for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1709                         {
1710                                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1711                                 {
1712                                         leaf->pvsframe = model->brushq1.pvsframecount;
1713                                         leaf->pvschain = model->brushq1.pvsleafchain;
1714                                         model->brushq1.pvsleafchain = leaf;
1715                                         // mark surfaces bounding this leaf as visible
1716                                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1717                                                 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1718                                 }
1719                         }
1720                         model->brushq1.BuildPVSTextureChains(model);
1721                 }
1722         }
1723 }
1724
1725 void R_WorldVisibility(entity_render_t *ent)
1726 {
1727         vec3_t modelorg;
1728         mleaf_t *viewleaf;
1729
1730         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1731         viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1732         R_PVSUpdate(ent, viewleaf);
1733
1734         if (!viewleaf)
1735                 return;
1736
1737         if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1738                 R_SurfaceWorldNode (ent);
1739         else
1740                 R_PortalWorldNode (ent, viewleaf);
1741
1742         if (r_drawportals.integer)
1743                 R_DrawPortals(ent);
1744 }
1745
1746 void R_Model_Brush_DrawSky(entity_render_t *ent)
1747 {
1748         if (ent->model == NULL)
1749                 return;
1750         if (ent != &cl_entities[0].render)
1751                 R_PrepareBrushModel(ent);
1752         R_PrepareSurfaces(ent);
1753         R_DrawSurfaces(ent, SURF_DRAWSKY);
1754 }
1755
1756 void R_Model_Brush_Draw(entity_render_t *ent)
1757 {
1758         if (ent->model == NULL)
1759                 return;
1760         c_bmodels++;
1761         if (ent != &cl_entities[0].render)
1762                 R_PrepareBrushModel(ent);
1763         R_PrepareSurfaces(ent);
1764         R_UpdateTextureInfo(ent);
1765         R_UpdateLightmapInfo(ent);
1766         R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1767 }
1768
1769 void R_Model_Brush_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)
1770 {
1771         model_t *model = ent->model;
1772         vec3_t lightmins, lightmaxs;
1773         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1774         const int *e;
1775         const float *v[3];
1776         msurface_t *surface;
1777         mleaf_t *leaf;
1778         const qbyte *pvs;
1779         lightmins[0] = relativelightorigin[0] - lightradius;
1780         lightmins[1] = relativelightorigin[1] - lightradius;
1781         lightmins[2] = relativelightorigin[2] - lightradius;
1782         lightmaxs[0] = relativelightorigin[0] + lightradius;
1783         lightmaxs[1] = relativelightorigin[1] + lightradius;
1784         lightmaxs[2] = relativelightorigin[2] + lightradius;
1785         *outnumclusterspointer = 0;
1786         *outnumsurfacespointer = 0;
1787         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1788         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1789         if (model == NULL)
1790         {
1791                 VectorCopy(lightmins, outmins);
1792                 VectorCopy(lightmaxs, outmaxs);
1793                 return;
1794         }
1795         VectorCopy(relativelightorigin, outmins);
1796         VectorCopy(relativelightorigin, outmaxs);
1797         if (model->brush.GetPVS)
1798                 pvs = model->brush.GetPVS(model, relativelightorigin);
1799         else
1800                 pvs = NULL;
1801         // FIXME: use BSP recursion as lights are often small
1802         for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1803         {
1804                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1805                 {
1806                         outmins[0] = min(outmins[0], leaf->mins[0]);
1807                         outmins[1] = min(outmins[1], leaf->mins[1]);
1808                         outmins[2] = min(outmins[2], leaf->mins[2]);
1809                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1810                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1811                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1812                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1813                         {
1814                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1815                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
1816                         }
1817                         for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1818                         {
1819                                 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1820                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1821                                 {
1822                                         surface = model->brushq1.surfaces + surfaceindex;
1823                                         if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1824                                         {
1825                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1826                                                 {
1827                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1828                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1829                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1830                                                         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])))
1831                                                         {
1832                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
1833                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
1834                                                                 break;
1835                                                         }
1836                                                 }
1837                                         }
1838                                 }
1839                         }
1840                 }
1841         }
1842
1843         // limit combined leaf box to light boundaries
1844         outmins[0] = max(outmins[0], lightmins[0]);
1845         outmins[1] = max(outmins[1], lightmins[1]);
1846         outmins[2] = max(outmins[2], lightmins[2]);
1847         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1848         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1849         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1850
1851         *outnumclusterspointer = outnumclusters;
1852         *outnumsurfacespointer = outnumsurfaces;
1853 }
1854
1855 void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1856 {
1857         model_t *model = ent->model;
1858         vec3_t lightmins, lightmaxs;
1859         msurface_t *surface;
1860         int surfacelistindex, j, t;
1861         const int *e;
1862         const float *v[3];
1863         if (r_drawcollisionbrushes.integer < 2)
1864         {
1865                 lightmins[0] = relativelightorigin[0] - lightradius;
1866                 lightmins[1] = relativelightorigin[1] - lightradius;
1867                 lightmins[2] = relativelightorigin[2] - lightradius;
1868                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1869                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1870                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1871                 R_Mesh_Matrix(&ent->matrix);
1872                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1873                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1874                 {
1875                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1876                         for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1877                         {
1878                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1879                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1880                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1881                                 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])))
1882                                         shadowmarklist[numshadowmark++] = t;
1883                         }
1884                 }
1885                 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);
1886         }
1887 }
1888
1889 void R_Model_Brush_DrawLight(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativeeyeorigin, float lightradius, float *lightcolor, const matrix4x4_t *matrix_modeltolight, const matrix4x4_t *matrix_modeltoattenuationxyz, const matrix4x4_t *matrix_modeltoattenuationz, rtexture_t *lightcubemap, int numsurfaces, const int *surfacelist)
1890 {
1891         model_t *model = ent->model;
1892         vec3_t lightmins, lightmaxs, modelorg;
1893         msurface_t *surface;
1894         texture_t *t;
1895         int surfacelistindex;
1896         if (r_drawcollisionbrushes.integer < 2)
1897         {
1898                 lightmins[0] = relativelightorigin[0] - lightradius;
1899                 lightmins[1] = relativelightorigin[1] - lightradius;
1900                 lightmins[2] = relativelightorigin[2] - lightradius;
1901                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1902                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1903                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1904                 R_Mesh_Matrix(&ent->matrix);
1905                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1906                 R_UpdateTextureInfo(ent);
1907                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1908                 {
1909                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1910                         if (r_shadow_compilingrtlight)
1911                         {
1912                                 // if compiling an rtlight, capture the mesh
1913                                 t = surface->texinfo->texture;
1914                                 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1915                                         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);
1916                         }
1917                         else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1918                         {
1919                                 t = surface->texinfo->texture->currentframe;
1920                                 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1921                                         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, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
1922                         }
1923                 }
1924         }
1925 }
1926
1927 void R_DrawCollisionBrush(colbrushf_t *brush)
1928 {
1929         int i;
1930         rmeshstate_t m;
1931         memset(&m, 0, sizeof(m));
1932         m.pointer_vertex = brush->points->v;
1933         R_Mesh_State(&m);
1934         i = ((int)brush) / sizeof(colbrushf_t);
1935         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1936         GL_LockArrays(0, brush->numpoints);
1937         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1938         GL_LockArrays(0, 0);
1939 }
1940
1941 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1942 {
1943         int i;
1944         rmeshstate_t m;
1945         if (!face->num_collisiontriangles)
1946                 return;
1947         memset(&m, 0, sizeof(m));
1948         m.pointer_vertex = face->data_collisionvertex3f;
1949         R_Mesh_State(&m);
1950         i = ((int)face) / sizeof(q3msurface_t);
1951         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1952         GL_LockArrays(0, face->num_collisionvertices);
1953         R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1954         GL_LockArrays(0, 0);
1955 }
1956
1957 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1958 {
1959         rmeshstate_t m;
1960         if (!face->num_triangles)
1961                 return;
1962         c_faces++;
1963         if (skyrendernow)
1964         {
1965                 skyrendernow = false;
1966                 if (skyrendermasked)
1967                         R_Sky();
1968         }
1969
1970         R_Mesh_Matrix(&ent->matrix);
1971
1972         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1973         if (skyrendermasked)
1974         {
1975                 // depth-only (masking)
1976                 GL_ColorMask(0,0,0,0);
1977                 // just to make sure that braindead drivers don't draw anything
1978                 // despite that colormask...
1979                 GL_BlendFunc(GL_ZERO, GL_ONE);
1980         }
1981         else
1982         {
1983                 // fog sky
1984                 GL_BlendFunc(GL_ONE, GL_ZERO);
1985         }
1986         GL_DepthMask(true);
1987         GL_DepthTest(true);
1988
1989         memset(&m, 0, sizeof(m));
1990         m.pointer_vertex = face->data_vertex3f;
1991         R_Mesh_State(&m);
1992
1993         GL_LockArrays(0, face->num_vertices);
1994         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1995         GL_LockArrays(0, 0);
1996         GL_ColorMask(1,1,1,1);
1997 }
1998
1999 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
2000 {
2001         rmeshstate_t m;
2002         memset(&m, 0, sizeof(m));
2003         GL_BlendFunc(GL_ONE, GL_ZERO);
2004         GL_DepthMask(true);
2005         GL_DepthTest(true);
2006         if (face->texture->skin.glow)
2007         {
2008                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2009                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2010                 GL_Color(1, 1, 1, 1);
2011         }
2012         else
2013                 GL_Color(0, 0, 0, 1);
2014         m.pointer_vertex = face->data_vertex3f;
2015         R_Mesh_State(&m);
2016         GL_LockArrays(0, face->num_vertices);
2017         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2018         GL_LockArrays(0, 0);
2019 }
2020
2021 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
2022 {
2023         rmeshstate_t m;
2024         memset(&m, 0, sizeof(m));
2025         GL_BlendFunc(GL_ONE, GL_ZERO);
2026         GL_DepthMask(true);
2027         GL_DepthTest(true);
2028         m.tex[0] = R_GetTexture(face->texture->skin.base);
2029         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2030         m.tex[1] = R_GetTexture(face->lightmaptexture);
2031         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2032         m.texrgbscale[1] = 2;
2033         GL_Color(1, 1, 1, 1);
2034         m.pointer_vertex = face->data_vertex3f;
2035         R_Mesh_State(&m);
2036         GL_LockArrays(0, face->num_vertices);
2037         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2038         GL_LockArrays(0, 0);
2039 }
2040
2041 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2042 {
2043         rmeshstate_t m;
2044         memset(&m, 0, sizeof(m));
2045         GL_BlendFunc(GL_ONE, GL_ZERO);
2046         GL_DepthMask(true);
2047         GL_DepthTest(true);
2048         m.tex[0] = R_GetTexture(face->texture->skin.base);
2049         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2050         GL_Color(1, 1, 1, 1);
2051         m.pointer_vertex = face->data_vertex3f;
2052         R_Mesh_State(&m);
2053         GL_LockArrays(0, face->num_vertices);
2054         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2055         GL_LockArrays(0, 0);
2056 }
2057
2058 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2059 {
2060         rmeshstate_t m;
2061         memset(&m, 0, sizeof(m));
2062         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2063         GL_DepthMask(false);
2064         GL_DepthTest(true);
2065         m.tex[0] = R_GetTexture(face->lightmaptexture);
2066         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2067         GL_Color(1, 1, 1, 1);
2068         m.pointer_vertex = face->data_vertex3f;
2069         R_Mesh_State(&m);
2070         GL_LockArrays(0, face->num_vertices);
2071         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2072         GL_LockArrays(0, 0);
2073 }
2074
2075 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2076 {
2077         rmeshstate_t m;
2078         memset(&m, 0, sizeof(m));
2079         GL_BlendFunc(GL_ONE, GL_ZERO);
2080         GL_DepthMask(true);
2081         GL_DepthTest(true);
2082         m.tex[0] = R_GetTexture(face->lightmaptexture);
2083         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2084         if (r_shadow_realtime_world.integer)
2085                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2086         else
2087                 GL_Color(1, 1, 1, 1);
2088         m.pointer_vertex = face->data_vertex3f;
2089         R_Mesh_State(&m);
2090         GL_LockArrays(0, face->num_vertices);
2091         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2092         GL_LockArrays(0, 0);
2093 }
2094
2095 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
2096 {
2097         rmeshstate_t m;
2098         memset(&m, 0, sizeof(m));
2099         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2100         GL_DepthMask(false);
2101         GL_DepthTest(true);
2102         if (face->texture->skin.glow)
2103         {
2104                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2105                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2106                 GL_Color(1, 1, 1, 1);
2107         }
2108         else
2109                 GL_Color(0, 0, 0, 1);
2110         m.pointer_vertex = face->data_vertex3f;
2111         R_Mesh_State(&m);
2112         GL_LockArrays(0, face->num_vertices);
2113         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2114         GL_LockArrays(0, 0);
2115 }
2116
2117 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
2118 {
2119         int i;
2120         float mul;
2121         rmeshstate_t m;
2122         memset(&m, 0, sizeof(m));
2123         GL_BlendFunc(GL_ONE, GL_ZERO);
2124         GL_DepthMask(true);
2125         GL_DepthTest(true);
2126         m.tex[0] = R_GetTexture(face->texture->skin.base);
2127         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2128         mul = 2.0f;
2129         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2130                 mul *= r_shadow_realtime_world_lightmaps.value;
2131         if (mul == 2 && gl_combine.integer)
2132         {
2133                 m.texrgbscale[0] = 2;
2134                 m.pointer_color = face->data_color4f;
2135         }
2136         else if (mul == 1)
2137                 m.pointer_color = face->data_color4f;
2138         else
2139         {
2140                 for (i = 0;i < face->num_vertices;i++)
2141                 {
2142                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2143                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2144                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2145                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2146                 }
2147                 m.pointer_color = varray_color4f;
2148         }
2149         m.pointer_vertex = face->data_vertex3f;
2150         R_Mesh_State(&m);
2151         GL_LockArrays(0, face->num_vertices);
2152         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2153         GL_LockArrays(0, 0);
2154 }
2155
2156 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2157 {
2158         int i;
2159         float mul;
2160         rmeshstate_t m;
2161         memset(&m, 0, sizeof(m));
2162         GL_BlendFunc(GL_ONE, GL_ZERO);
2163         GL_DepthMask(true);
2164         GL_DepthTest(true);
2165         mul = 2.0f;
2166         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2167                 mul *= r_shadow_realtime_world_lightmaps.value;
2168         if (mul == 1)
2169                 m.pointer_color = face->data_color4f;
2170         else
2171         {
2172                 for (i = 0;i < face->num_vertices;i++)
2173                 {
2174                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2175                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2176                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2177                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2178                 }
2179                 m.pointer_color = varray_color4f;
2180         }
2181         m.pointer_vertex = face->data_vertex3f;
2182         R_Mesh_State(&m);
2183         GL_LockArrays(0, face->num_vertices);
2184         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2185         GL_LockArrays(0, 0);
2186 }
2187
2188 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2189 {
2190         rmeshstate_t m;
2191         memset(&m, 0, sizeof(m));
2192         GL_BlendFunc(GL_ONE, GL_ONE);
2193         GL_DepthMask(true);
2194         GL_DepthTest(true);
2195         m.tex[0] = R_GetTexture(face->texture->skin.base);
2196         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2197         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2198         m.pointer_vertex = face->data_vertex3f;
2199         R_Mesh_State(&m);
2200         GL_LockArrays(0, face->num_vertices);
2201         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2202         GL_LockArrays(0, 0);
2203 }
2204
2205 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2206 {
2207         const entity_render_t *ent = voident;
2208         q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2209         rmeshstate_t m;
2210         R_Mesh_Matrix(&ent->matrix);
2211         memset(&m, 0, sizeof(m));
2212         if (ent->effects & EF_ADDITIVE)
2213                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2214         else
2215                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2216         GL_DepthMask(false);
2217         GL_DepthTest(true);
2218         m.tex[0] = R_GetTexture(face->texture->skin.base);
2219         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2220         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2221         if (gl_combine.integer)
2222         {
2223                 m.texrgbscale[0] = 2;
2224                 if (r_textureunits.integer >= 2)
2225                 {
2226                         m.tex[1] = R_GetTexture(face->lightmaptexture);
2227                         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2228                         GL_Color(1, 1, 1, ent->alpha);
2229                 }
2230                 else
2231                 {
2232                         if (ent->alpha == 1)
2233                                 m.pointer_color = face->data_color4f;
2234                         else
2235                         {
2236                                 int i;
2237                                 for (i = 0;i < face->num_vertices;i++)
2238                                 {
2239                                         varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2240                                         varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2241                                         varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2242                                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2243                                 }
2244                                 m.pointer_color = varray_color4f;
2245                         }
2246                 }
2247         }
2248         else
2249         {
2250                 int i;
2251                 for (i = 0;i < face->num_vertices;i++)
2252                 {
2253                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2254                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2255                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2256                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2257                 }
2258                 m.pointer_color = varray_color4f;
2259         }
2260         m.pointer_vertex = face->data_vertex3f;
2261         R_Mesh_State(&m);
2262         qglDisable(GL_CULL_FACE);
2263         GL_LockArrays(0, face->num_vertices);
2264         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2265         GL_LockArrays(0, 0);
2266         qglEnable(GL_CULL_FACE);
2267 }
2268
2269 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2270 {
2271         if (!face->num_triangles)
2272                 return;
2273         if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2274                 return;
2275         c_faces++;
2276         if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2277         {
2278                 vec3_t facecenter, center;
2279                 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2280                 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2281                 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2282                 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2283                 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2284                 return;
2285         }
2286         R_Mesh_Matrix(&ent->matrix);
2287         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2288                 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2289         else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2290         {
2291                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2292                 if (face->texture->skin.glow)
2293                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2294         }
2295         else if (face->lightmaptexture)
2296         {
2297                 if (gl_lightmaps.integer)
2298                         R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2299                 else
2300                 {
2301                         if (r_textureunits.integer >= 2 && gl_combine.integer)
2302                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2303                         else
2304                         {
2305                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2306                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2307                         }
2308                         if (face->texture->skin.glow)
2309                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2310                 }
2311         }
2312         else
2313         {
2314                 if (gl_lightmaps.integer)
2315                         R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2316                 else
2317                 {
2318                         R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2319                         if (face->texture->skin.glow)
2320                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2321                 }
2322         }
2323         if (r_ambient.value)
2324                 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2325 }
2326
2327 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2328 {
2329         int i;
2330         q3mleaf_t *leaf;
2331         for (;;)
2332         {
2333                 if (R_CullBox(node->mins, node->maxs))
2334                         return;
2335                 if (!node->plane)
2336                         break;
2337                 c_nodes++;
2338                 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2339                 node = node->children[1];
2340         }
2341         leaf = (q3mleaf_t *)node;
2342         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2343         {
2344                 c_leafs++;
2345                 for (i = 0;i < leaf->numleaffaces;i++)
2346                         leaf->firstleafface[i]->visframe = r_framecount;
2347         }
2348 }
2349
2350 // FIXME: num_leafs needs to be recalculated at load time to include only
2351 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2352 // the submodels (which would render the submodels occasionally, as part of
2353 // the world - not good)
2354 void R_Q3BSP_MarkLeafPVS(void)
2355 {
2356         int i, j;
2357         q3mleaf_t *leaf;
2358         for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2359         {
2360                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2361                 {
2362                         c_leafs++;
2363                         for (i = 0;i < leaf->numleaffaces;i++)
2364                                 leaf->firstleafface[i]->visframe = r_framecount;
2365                 }
2366         }
2367 }
2368
2369 static int r_q3bsp_framecount = -1;
2370
2371 void R_Q3BSP_DrawSky(entity_render_t *ent)
2372 {
2373         int i;
2374         q3msurface_t *face;
2375         vec3_t modelorg;
2376         model_t *model;
2377         R_Mesh_Matrix(&ent->matrix);
2378         model = ent->model;
2379         if (r_drawcollisionbrushes.integer < 2)
2380         {
2381                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2382                 if (ent == &cl_entities[0].render)
2383                 {
2384                         if (r_q3bsp_framecount != r_framecount)
2385                         {
2386                                 r_q3bsp_framecount = r_framecount;
2387                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2388                                 //R_Q3BSP_MarkLeafPVS();
2389                         }
2390                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2391                                 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2392                                         R_Q3BSP_DrawSkyFace(ent, face);
2393                 }
2394                 else
2395                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2396                                 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2397                                         R_Q3BSP_DrawSkyFace(ent, face);
2398         }
2399 }
2400
2401 void R_Q3BSP_Draw(entity_render_t *ent)
2402 {
2403         int i;
2404         q3msurface_t *face;
2405         vec3_t modelorg;
2406         model_t *model;
2407         qbyte *pvs;
2408         R_Mesh_Matrix(&ent->matrix);
2409         model = ent->model;
2410         if (r_drawcollisionbrushes.integer < 2)
2411         {
2412                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2413                 if (ent == &cl_entities[0].render)
2414                 {
2415                         if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2416                         if (r_q3bsp_framecount != r_framecount)
2417                         {
2418                                 r_q3bsp_framecount = r_framecount;
2419                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2420                                 //R_Q3BSP_MarkLeafPVS();
2421                         }
2422                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2423                                 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2424                                         R_Q3BSP_DrawFace(ent, face);
2425                 }
2426                 else
2427                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2428                                 R_Q3BSP_DrawFace(ent, face);
2429         }
2430         if (r_drawcollisionbrushes.integer >= 1)
2431         {
2432                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2433                 GL_DepthMask(false);
2434                 GL_DepthTest(true);
2435                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2436                 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2437                         if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2438                                 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2439                 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2440                         if (face->num_collisiontriangles)
2441                                 R_Q3BSP_DrawCollisionFace(ent, face);
2442                 qglPolygonOffset(0, 0);
2443         }
2444 }
2445
2446 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)
2447 {
2448         model_t *model = ent->model;
2449         vec3_t lightmins, lightmaxs;
2450         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2451         const int *e;
2452         const float *v[3];
2453         q3msurface_t *surface;
2454         q3mleaf_t *leaf;
2455         const qbyte *pvs;
2456         lightmins[0] = relativelightorigin[0] - lightradius;
2457         lightmins[1] = relativelightorigin[1] - lightradius;
2458         lightmins[2] = relativelightorigin[2] - lightradius;
2459         lightmaxs[0] = relativelightorigin[0] + lightradius;
2460         lightmaxs[1] = relativelightorigin[1] + lightradius;
2461         lightmaxs[2] = relativelightorigin[2] + lightradius;
2462         *outnumclusterspointer = 0;
2463         *outnumsurfacespointer = 0;
2464         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2465         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2466         if (model == NULL)
2467         {
2468                 VectorCopy(lightmins, outmins);
2469                 VectorCopy(lightmaxs, outmaxs);
2470                 return;
2471         }
2472         VectorCopy(relativelightorigin, outmins);
2473         VectorCopy(relativelightorigin, outmaxs);
2474         if (model->brush.GetPVS)
2475                 pvs = model->brush.GetPVS(model, relativelightorigin);
2476         else
2477                 pvs = NULL;
2478         // FIXME: use BSP recursion as lights are often small
2479         for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2480         {
2481                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2482                 {
2483                         outmins[0] = min(outmins[0], leaf->mins[0]);
2484                         outmins[1] = min(outmins[1], leaf->mins[1]);
2485                         outmins[2] = min(outmins[2], leaf->mins[2]);
2486                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2487                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2488                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2489                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2490                         {
2491                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2492                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
2493                         }
2494                         for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2495                         {
2496                                 surface = leaf->firstleafface[marksurfaceindex];
2497                                 surfaceindex = surface - model->brushq3.data_faces;
2498                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2499                                 {
2500                                         if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2501                                         {
2502                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2503                                                 {
2504                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2505                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2506                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2507                                                         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])))
2508                                                         {
2509                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2510                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2511                                                                 break;
2512                                                         }
2513                                                 }
2514                                         }
2515                                 }
2516                         }
2517                 }
2518         }
2519
2520         // limit combined leaf box to light boundaries
2521         outmins[0] = max(outmins[0], lightmins[0]);
2522         outmins[1] = max(outmins[1], lightmins[1]);
2523         outmins[2] = max(outmins[2], lightmins[2]);
2524         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2525         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2526         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2527
2528         *outnumclusterspointer = outnumclusters;
2529         *outnumsurfacespointer = outnumsurfaces;
2530 }
2531
2532 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2533 {
2534         model_t *model = ent->model;
2535         vec3_t lightmins, lightmaxs;
2536         q3msurface_t *surface;
2537         int surfacelistindex, j, t;
2538         const int *e;
2539         const float *v[3];
2540         if (r_drawcollisionbrushes.integer < 2)
2541         {
2542                 lightmins[0] = relativelightorigin[0] - lightradius;
2543                 lightmins[1] = relativelightorigin[1] - lightradius;
2544                 lightmins[2] = relativelightorigin[2] - lightradius;
2545                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2546                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2547                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2548                 R_Mesh_Matrix(&ent->matrix);
2549                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2550                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2551                 {
2552                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2553                         // FIXME: check some manner of face->rendermode here?
2554                         if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2555                         {
2556                                 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2557                                 {
2558                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2559                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2560                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2561                                         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])))
2562                                                 shadowmarklist[numshadowmark++] = t;
2563                                 }
2564                         }
2565                 }
2566                 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);
2567         }
2568 }
2569
2570 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, int numsurfaces, const int *surfacelist)
2571 {
2572         model_t *model = ent->model;
2573         vec3_t lightmins, lightmaxs, modelorg;
2574         q3msurface_t *surface;
2575         int surfacelistindex;
2576         if (r_drawcollisionbrushes.integer < 2)
2577         {
2578                 lightmins[0] = relativelightorigin[0] - lightradius;
2579                 lightmins[1] = relativelightorigin[1] - lightradius;
2580                 lightmins[2] = relativelightorigin[2] - lightradius;
2581                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2582                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2583                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2584                 R_Mesh_Matrix(&ent->matrix);
2585                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2586                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2587                 {
2588                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2589                         if (r_shadow_compilingrtlight)
2590                         {
2591                                 // if compiling an rtlight, capture the mesh
2592                                 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->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, surface->num_triangles, surface->data_element3i);
2593                         }
2594                         else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2595                                 R_Shadow_RenderLighting(surface->num_vertices, surface->num_triangles, surface->data_element3i, surface->data_vertex3f, surface->data_svector3f, surface->data_tvector3f, surface->data_normal3f, surface->data_texcoordtexture2f, relativelightorigin, relativeeyeorigin, lightcolor, matrix_modeltolight, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz, surface->texture->skin.base, surface->texture->skin.nmap, surface->texture->skin.gloss, lightcubemap, LIGHTING_DIFFUSE | LIGHTING_SPECULAR);
2596                 }
2597         }
2598 }
2599
2600 static void gl_surf_start(void)
2601 {
2602 }
2603
2604 static void gl_surf_shutdown(void)
2605 {
2606 }
2607
2608 static void gl_surf_newmap(void)
2609 {
2610 }
2611
2612 void GL_Surf_Init(void)
2613 {
2614         int i;
2615         dlightdivtable[0] = 4194304;
2616         for (i = 1;i < 32768;i++)
2617                 dlightdivtable[i] = 4194304 / (i << 7);
2618
2619         Cvar_RegisterVariable(&r_ambient);
2620         Cvar_RegisterVariable(&r_drawportals);
2621         Cvar_RegisterVariable(&r_testvis);
2622         Cvar_RegisterVariable(&r_floatbuildlightmap);
2623         Cvar_RegisterVariable(&r_detailtextures);
2624         Cvar_RegisterVariable(&r_surfaceworldnode);
2625         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2626         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2627         Cvar_RegisterVariable(&gl_lightmaps);
2628
2629         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2630 }
2631