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