]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
renamed q3mface_t to q3msurface_t
[divverent/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23 #include "r_shadow.h"
24
25 #define MAX_LIGHTMAP_SIZE 256
26
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
36 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
37 cvar_t r_surfaceworldnode = {0, "r_surfaceworldnode", "1"};
38 cvar_t r_drawcollisionbrushes_polygonfactor = {0, "r_drawcollisionbrushes_polygonfactor", "-1"};
39 cvar_t r_drawcollisionbrushes_polygonoffset = {0, "r_drawcollisionbrushes_polygonoffset", "0"};
40 cvar_t gl_lightmaps = {0, "gl_lightmaps", "0"};
41
42 /*
43 // FIXME: these arrays are huge!
44 int r_q1bsp_maxmarkleafs;
45 int r_q1bsp_nummarkleafs;
46 mleaf_t *r_q1bsp_maxleaflist[65536];
47 int r_q1bsp_maxmarksurfaces;
48 int r_q1bsp_nummarksurfaces;
49 msurface_t *r_q1bsp_maxsurfacelist[65536];
50
51 // FIXME: these arrays are huge!
52 int r_q3bsp_maxmarkleafs;
53 int r_q3bsp_nummarkleafs;
54 q3mleaf_t *r_q3bsp_maxleaflist[65536];
55 int r_q3bsp_maxmarksurfaces;
56 int r_q3bsp_nummarksurfaces;
57 q3msurface_t *r_q3bsp_maxsurfacelist[65536];
58 */
59
60 static int dlightdivtable[32768];
61
62 static int R_IntAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
63 {
64         int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract, k;
65         unsigned int *bl;
66         float dist, impact[3], local[3];
67         dlight_t *light;
68
69         lit = false;
70
71         smax = (surf->extents[0] >> 4) + 1;
72         tmax = (surf->extents[1] >> 4) + 1;
73         smax3 = smax * 3;
74
75         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
76         {
77                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
78                         continue;                                       // not lit by this light
79
80                 Matrix4x4_Transform(matrix, light->origin, local);
81                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
82
83                 // for comparisons to minimum acceptable light
84                 // compensate for LIGHTOFFSET
85                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
86
87                 dist2 = dist * dist;
88                 dist2 += LIGHTOFFSET;
89                 if (dist2 >= maxdist)
90                         continue;
91
92                 if (surf->plane->type < 3)
93                 {
94                         VectorCopy(local, impact);
95                         impact[surf->plane->type] -= dist;
96                 }
97                 else
98                 {
99                         impact[0] = local[0] - surf->plane->normal[0] * dist;
100                         impact[1] = local[1] - surf->plane->normal[1] * dist;
101                         impact[2] = local[2] - surf->plane->normal[2] * dist;
102                 }
103
104                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
105                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
106
107                 s = bound(0, impacts, smax * 16) - impacts;
108                 t = bound(0, impactt, tmax * 16) - impactt;
109                 i = s * s + t * t + dist2;
110                 if (i > maxdist)
111                         continue;
112
113                 // reduce calculations
114                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
115                         sdtable[s] = i * i + dist2;
116
117                 maxdist3 = maxdist - dist2;
118
119                 // convert to 8.8 blocklights format
120                 red = light->rtlight.lightmap_light[0] * (1.0f / 128.0f);
121                 green = light->rtlight.lightmap_light[1] * (1.0f / 128.0f);
122                 blue = light->rtlight.lightmap_light[2] * (1.0f / 128.0f);
123                 subtract = (int) (light->rtlight.lightmap_subtract * 4194304.0f);
124                 bl = intblocklights;
125
126                 i = impactt;
127                 for (t = 0;t < tmax;t++, i -= 16)
128                 {
129                         td = i * i;
130                         // make sure some part of it is visible on this line
131                         if (td < maxdist3)
132                         {
133                                 maxdist2 = maxdist - td;
134                                 for (s = 0;s < smax;s++)
135                                 {
136                                         if (sdtable[s] < maxdist2)
137                                         {
138                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
139                                                 if (k > 0)
140                                                 {
141                                                         bl[0] += (red   * k);
142                                                         bl[1] += (green * k);
143                                                         bl[2] += (blue  * k);
144                                                         lit = true;
145                                                 }
146                                         }
147                                         bl += 3;
148                                 }
149                         }
150                         else // skip line
151                                 bl += smax3;
152                 }
153         }
154         return lit;
155 }
156
157 static int R_FloatAddDynamicLights (const matrix4x4_t *matrix, msurface_t *surf)
158 {
159         int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
160         float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
161         dlight_t *light;
162
163         lit = false;
164
165         smax = (surf->extents[0] >> 4) + 1;
166         tmax = (surf->extents[1] >> 4) + 1;
167         smax3 = smax * 3;
168
169         for (lnum = 0, light = r_dlight;lnum < r_numdlights;lnum++, light++)
170         {
171                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
172                         continue;                                       // not lit by this light
173
174                 Matrix4x4_Transform(matrix, light->origin, local);
175                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
176
177                 // for comparisons to minimum acceptable light
178                 // compensate for LIGHTOFFSET
179                 maxdist = (int) light->rtlight.lightmap_cullradius2 + LIGHTOFFSET;
180
181                 dist2 = dist * dist;
182                 dist2 += LIGHTOFFSET;
183                 if (dist2 >= maxdist)
184                         continue;
185
186                 if (surf->plane->type < 3)
187                 {
188                         VectorCopy(local, impact);
189                         impact[surf->plane->type] -= dist;
190                 }
191                 else
192                 {
193                         impact[0] = local[0] - surf->plane->normal[0] * dist;
194                         impact[1] = local[1] - surf->plane->normal[1] * dist;
195                         impact[2] = local[2] - surf->plane->normal[2] * dist;
196                 }
197
198                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
199                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
200
201                 td = bound(0, impacts, smax * 16) - impacts;
202                 td1 = bound(0, impactt, tmax * 16) - impactt;
203                 td = td * td + td1 * td1 + dist2;
204                 if (td > maxdist)
205                         continue;
206
207                 // reduce calculations
208                 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
209                         sdtable[s] = td1 * td1 + dist2;
210
211                 maxdist3 = maxdist - dist2;
212
213                 // convert to 8.8 blocklights format
214                 red = light->rtlight.lightmap_light[0];
215                 green = light->rtlight.lightmap_light[1];
216                 blue = light->rtlight.lightmap_light[2];
217                 subtract = light->rtlight.lightmap_subtract * 32768.0f;
218                 bl = floatblocklights;
219
220                 td1 = impactt;
221                 for (t = 0;t < tmax;t++, td1 -= 16.0f)
222                 {
223                         td = td1 * td1;
224                         // make sure some part of it is visible on this line
225                         if (td < maxdist3)
226                         {
227                                 maxdist2 = maxdist - td;
228                                 for (s = 0;s < smax;s++)
229                                 {
230                                         if (sdtable[s] < maxdist2)
231                                         {
232                                                 k = (32768.0f / (sdtable[s] + td)) - subtract;
233                                                 bl[0] += red   * k;
234                                                 bl[1] += green * k;
235                                                 bl[2] += blue  * k;
236                                                 lit = true;
237                                         }
238                                         bl += 3;
239                                 }
240                         }
241                         else // skip line
242                                 bl += smax3;
243                 }
244         }
245         return lit;
246 }
247
248 /*
249 ===============
250 R_BuildLightMap
251
252 Combine and scale multiple lightmaps into the 8.8 format in blocklights
253 ===============
254 */
255 static void R_BuildLightMap (const entity_render_t *ent, msurface_t *surf)
256 {
257         if (!r_floatbuildlightmap.integer)
258         {
259                 int smax, tmax, i, j, size, size3, maps, stride, l;
260                 unsigned int *bl, scale;
261                 qbyte *lightmap, *out, *stain;
262
263                 // update cached lighting info
264                 surf->cached_dlight = 0;
265
266                 smax = (surf->extents[0]>>4)+1;
267                 tmax = (surf->extents[1]>>4)+1;
268                 size = smax*tmax;
269                 size3 = size*3;
270                 lightmap = surf->samples;
271
272         // set to full bright if no light data
273                 bl = intblocklights;
274                 if (!ent->model->brushq1.lightdata)
275                 {
276                         for (i = 0;i < size3;i++)
277                                 bl[i] = 255*256;
278                 }
279                 else
280                 {
281         // clear to no light
282                         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 || r_fullbright.integer)
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_UpdateLightmapInfo(entity_render_t *ent)
1395 {
1396         int i;
1397         msurface_t *surface, **surfacechain;
1398         model_t *model = ent->model;
1399         if (!model)
1400                 return;
1401         if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1402                 R_MarkLights(ent);
1403         if (model->brushq1.light_ambient != r_ambient.value)
1404         {
1405                 model->brushq1.light_ambient = r_ambient.value;
1406                 for (i = 0;i < model->nummodelsurfaces;i++)
1407                         model->brushq1.surfaces[i + model->firstmodelsurface].cached_dlight = true;
1408         }
1409         else
1410         {
1411                 for (i = 0;i < model->brushq1.light_styles;i++)
1412                 {
1413                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1414                         {
1415                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1416                                 for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1417                                         surface->cached_dlight = true;
1418                         }
1419                 }
1420         }
1421 }
1422
1423 void R_PrepareSurfaces(entity_render_t *ent)
1424 {
1425         int i, numsurfaces, *surfacevisframes;
1426         model_t *model;
1427         msurface_t *surf, *surfaces;
1428         vec3_t modelorg;
1429
1430         if (!ent->model)
1431                 return;
1432
1433         model = ent->model;
1434         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1435         numsurfaces = model->nummodelsurfaces;
1436         surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1437         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1438         for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1439         {
1440                 if (surfacevisframes[i] == r_framecount)
1441                 {
1442 #if !WORLDNODECULLBACKFACES
1443                         // mark any backface surfaces as not visible
1444                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1445                         {
1446                                 if (!(surf->flags & SURF_PLANEBACK))
1447                                         surfacevisframes[i] = -1;
1448                         }
1449                         else
1450                         {
1451                                 if ((surf->flags & SURF_PLANEBACK))
1452                                         surfacevisframes[i] = -1;
1453                         }
1454                         if (surfacevisframes[i] == r_framecount)
1455 #endif
1456                         {
1457                                 c_faces++;
1458                                 surf->visframe = r_framecount;
1459                                 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1460                                         R_BuildLightMap(ent, surf);
1461                         }
1462                 }
1463         }
1464 }
1465
1466 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1467 {
1468         int i, f;
1469         texture_t *t;
1470         model_t *model = ent->model;
1471         if (model == NULL)
1472                 return;
1473         R_Mesh_Matrix(&ent->matrix);
1474         for (i = 0, t = model->brushq1.textures;i < model->brushq1.numtextures;i++, t++)
1475         {
1476                 f = t->flags & flagsmask;
1477                 if (f && t->currentframe && model->brushq1.pvstexturechains[i] != NULL)
1478                         R_DrawSurfaceChain(ent, t->currentframe, model->brushq1.pvstexturechains[i]);
1479         }
1480 }
1481
1482 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1483 {
1484         int i;
1485         float *v;
1486         rmeshstate_t m;
1487         const entity_render_t *ent = calldata1;
1488         const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1489         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1490         GL_DepthMask(false);
1491         GL_DepthTest(true);
1492         R_Mesh_Matrix(&ent->matrix);
1493
1494         memset(&m, 0, sizeof(m));
1495         m.pointer_vertex = varray_vertex3f;
1496         R_Mesh_State(&m);
1497
1498         i = portal - ent->model->brushq1.portals;
1499         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1500                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1501                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1502                          0.125f);
1503         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1504         {
1505                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1506                         VectorCopy(portal->points[i].position, v);
1507         }
1508         else
1509                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1510                         VectorCopy(portal->points[i].position, v);
1511         GL_LockArrays(0, portal->numpoints);
1512         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1513         GL_LockArrays(0, 0);
1514 }
1515
1516 // LordHavoc: this is just a nice debugging tool, very slow
1517 static void R_DrawPortals(entity_render_t *ent)
1518 {
1519         int i;
1520         mportal_t *portal, *endportal;
1521         float temp[3], center[3], f;
1522         if (ent->model == NULL)
1523                 return;
1524         for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1525         {
1526                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1527                 {
1528                         VectorClear(temp);
1529                         for (i = 0;i < portal->numpoints;i++)
1530                                 VectorAdd(temp, portal->points[i].position, temp);
1531                         f = ixtable[portal->numpoints];
1532                         VectorScale(temp, f, temp);
1533                         Matrix4x4_Transform(&ent->matrix, temp, center);
1534                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1535                 }
1536         }
1537 }
1538
1539 void R_PrepareBrushModel(entity_render_t *ent)
1540 {
1541         int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1542         msurface_t *surf;
1543         model_t *model;
1544 #if WORLDNODECULLBACKFACES
1545         vec3_t modelorg;
1546 #endif
1547
1548         // because bmodels can be reused, we have to decide which things to render
1549         // from scratch every time
1550         model = ent->model;
1551         if (model == NULL)
1552                 return;
1553 #if WORLDNODECULLBACKFACES
1554         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1555 #endif
1556         numsurfaces = model->nummodelsurfaces;
1557         surf = model->brushq1.surfaces + model->firstmodelsurface;
1558         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1559         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1560         for (i = 0;i < numsurfaces;i++, surf++)
1561         {
1562 #if WORLDNODECULLBACKFACES
1563                 // mark any backface surfaces as not visible
1564                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1565                 {
1566                         if ((surf->flags & SURF_PLANEBACK))
1567                                 surfacevisframes[i] = r_framecount;
1568                 }
1569                 else if (!(surf->flags & SURF_PLANEBACK))
1570                         surfacevisframes[i] = r_framecount;
1571 #else
1572                 surfacevisframes[i] = r_framecount;
1573 #endif
1574                 surf->dlightframe = -1;
1575         }
1576         R_UpdateTextureInfo(ent);
1577         R_UpdateLightmapInfo(ent);
1578 }
1579
1580 void R_SurfaceWorldNode (entity_render_t *ent)
1581 {
1582         int i, *surfacevisframes, *surfacepvsframes, surfnum;
1583         msurface_t *surf;
1584         mleaf_t *leaf;
1585         model_t *model;
1586         vec3_t modelorg;
1587
1588         // equivilant to quake's RecursiveWorldNode but faster and more effective
1589         model = ent->model;
1590         if (model == NULL)
1591                 return;
1592         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1593         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1594         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1595
1596         for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1597         {
1598                 if (!R_CullBox (leaf->mins, leaf->maxs))
1599                 {
1600                         c_leafs++;
1601                         leaf->visframe = r_framecount;
1602                 }
1603         }
1604
1605         for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1606         {
1607                 surfnum = model->brushq1.pvssurflist[i];
1608                 surf = model->brushq1.surfaces + surfnum;
1609 #if WORLDNODECULLBACKFACES
1610                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1611                 {
1612                         if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1613                                 surfacevisframes[surfnum] = r_framecount;
1614                 }
1615                 else
1616                 {
1617                         if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1618                                 surfacevisframes[surfnum] = r_framecount;
1619                 }
1620 #else
1621                 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1622                         surfacevisframes[surfnum] = r_framecount;
1623 #endif
1624         }
1625 }
1626
1627 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1628 {
1629         int c, leafstackpos, *mark, *surfacevisframes;
1630 #if WORLDNODECULLBACKFACES
1631         int n;
1632         msurface_t *surf;
1633 #endif
1634         mleaf_t *leaf, *leafstack[8192];
1635         mportal_t *p;
1636         vec3_t modelorg;
1637         msurface_t *surfaces;
1638         if (ent->model == NULL)
1639                 return;
1640         // LordHavoc: portal-passage worldnode with PVS;
1641         // follows portals leading outward from viewleaf, does not venture
1642         // offscreen or into leafs that are not visible, faster than Quake's
1643         // RecursiveWorldNode
1644         surfaces = ent->model->brushq1.surfaces;
1645         surfacevisframes = ent->model->brushq1.surfacevisframes;
1646         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1647         viewleaf->worldnodeframe = r_framecount;
1648         leafstack[0] = viewleaf;
1649         leafstackpos = 1;
1650         while (leafstackpos)
1651         {
1652                 c_leafs++;
1653                 leaf = leafstack[--leafstackpos];
1654                 leaf->visframe = r_framecount;
1655                 // draw any surfaces bounding this leaf
1656                 if (leaf->nummarksurfaces)
1657                 {
1658                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1659                         {
1660 #if WORLDNODECULLBACKFACES
1661                                 n = *mark++;
1662                                 if (surfacevisframes[n] != r_framecount)
1663                                 {
1664                                         surf = surfaces + n;
1665                                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1666                                         {
1667                                                 if ((surf->flags & SURF_PLANEBACK))
1668                                                         surfacevisframes[n] = r_framecount;
1669                                         }
1670                                         else
1671                                         {
1672                                                 if (!(surf->flags & SURF_PLANEBACK))
1673                                                         surfacevisframes[n] = r_framecount;
1674                                         }
1675                                 }
1676 #else
1677                                 surfacevisframes[*mark++] = r_framecount;
1678 #endif
1679                         }
1680                 }
1681                 // follow portals into other leafs
1682                 for (p = leaf->portals;p;p = p->next)
1683                 {
1684                         // LordHavoc: this DotProduct hurts less than a cache miss
1685                         // (which is more likely to happen if backflowing through leafs)
1686                         if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1687                         {
1688                                 leaf = p->past;
1689                                 if (leaf->worldnodeframe != r_framecount)
1690                                 {
1691                                         leaf->worldnodeframe = r_framecount;
1692                                         // FIXME: R_CullBox is absolute, should be done relative
1693                                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1694                                                 leafstack[leafstackpos++] = leaf;
1695                                 }
1696                         }
1697                 }
1698         }
1699 }
1700
1701 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1702 {
1703         int j, c, *surfacepvsframes, *mark;
1704         mleaf_t *leaf;
1705         model_t *model;
1706
1707         model = ent->model;
1708         if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1709         {
1710                 model->brushq1.pvsframecount++;
1711                 model->brushq1.pvsviewleaf = viewleaf;
1712                 model->brushq1.pvsviewleafnovis = r_novis.integer;
1713                 model->brushq1.pvsleafchain = NULL;
1714                 model->brushq1.pvssurflistlength = 0;
1715                 if (viewleaf)
1716                 {
1717                         surfacepvsframes = model->brushq1.surfacepvsframes;
1718                         for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1719                         {
1720                                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1721                                 {
1722                                         leaf->pvsframe = model->brushq1.pvsframecount;
1723                                         leaf->pvschain = model->brushq1.pvsleafchain;
1724                                         model->brushq1.pvsleafchain = leaf;
1725                                         // mark surfaces bounding this leaf as visible
1726                                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1727                                                 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1728                                 }
1729                         }
1730                         model->brushq1.BuildPVSTextureChains(model);
1731                 }
1732         }
1733 }
1734
1735 void R_WorldVisibility(entity_render_t *ent)
1736 {
1737         vec3_t modelorg;
1738         mleaf_t *viewleaf;
1739
1740         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1741         viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1742         R_PVSUpdate(ent, viewleaf);
1743
1744         if (!viewleaf)
1745                 return;
1746
1747         if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1748                 R_SurfaceWorldNode (ent);
1749         else
1750                 R_PortalWorldNode (ent, viewleaf);
1751
1752         if (r_drawportals.integer)
1753                 R_DrawPortals(ent);
1754 }
1755
1756 void R_Model_Brush_DrawSky(entity_render_t *ent)
1757 {
1758         if (ent->model == NULL)
1759                 return;
1760         if (ent != &cl_entities[0].render)
1761                 R_PrepareBrushModel(ent);
1762         R_PrepareSurfaces(ent);
1763         R_DrawSurfaces(ent, SURF_DRAWSKY);
1764 }
1765
1766 void R_Model_Brush_Draw(entity_render_t *ent)
1767 {
1768         if (ent->model == NULL)
1769                 return;
1770         c_bmodels++;
1771         if (ent != &cl_entities[0].render)
1772                 R_PrepareBrushModel(ent);
1773         R_PrepareSurfaces(ent);
1774         R_UpdateTextureInfo(ent);
1775         R_UpdateLightmapInfo(ent);
1776         R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1777 }
1778
1779 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)
1780 {
1781         model_t *model = ent->model;
1782         vec3_t lightmins, lightmaxs;
1783         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1784         const int *e;
1785         const float *v[3];
1786         msurface_t *surface;
1787         mleaf_t *leaf;
1788         const qbyte *pvs;
1789         lightmins[0] = relativelightorigin[0] - lightradius;
1790         lightmins[1] = relativelightorigin[1] - lightradius;
1791         lightmins[2] = relativelightorigin[2] - lightradius;
1792         lightmaxs[0] = relativelightorigin[0] + lightradius;
1793         lightmaxs[1] = relativelightorigin[1] + lightradius;
1794         lightmaxs[2] = relativelightorigin[2] + lightradius;
1795         *outnumclusterspointer = 0;
1796         *outnumsurfacespointer = 0;
1797         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1798         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1799         if (model == NULL)
1800         {
1801                 VectorCopy(lightmins, outmins);
1802                 VectorCopy(lightmaxs, outmaxs);
1803                 return;
1804         }
1805         VectorCopy(relativelightorigin, outmins);
1806         VectorCopy(relativelightorigin, outmaxs);
1807         if (model->brush.GetPVS)
1808                 pvs = model->brush.GetPVS(model, relativelightorigin);
1809         else
1810                 pvs = NULL;
1811         // FIXME: use BSP recursion as lights are often small
1812         for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1813         {
1814                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1815                 {
1816                         outmins[0] = min(outmins[0], leaf->mins[0]);
1817                         outmins[1] = min(outmins[1], leaf->mins[1]);
1818                         outmins[2] = min(outmins[2], leaf->mins[2]);
1819                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1820                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1821                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1822                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1823                         {
1824                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1825                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
1826                         }
1827                         for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1828                         {
1829                                 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1830                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1831                                 {
1832                                         surface = model->brushq1.surfaces + surfaceindex;
1833                                         if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1834                                         {
1835                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1836                                                 {
1837                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1838                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1839                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1840                                                         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])))
1841                                                         {
1842                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
1843                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
1844                                                                 break;
1845                                                         }
1846                                                 }
1847                                         }
1848                                 }
1849                         }
1850                 }
1851         }
1852
1853         // limit combined leaf box to light boundaries
1854         outmins[0] = max(outmins[0], lightmins[0]);
1855         outmins[1] = max(outmins[1], lightmins[1]);
1856         outmins[2] = max(outmins[2], lightmins[2]);
1857         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1858         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1859         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1860
1861         *outnumclusterspointer = outnumclusters;
1862         *outnumsurfacespointer = outnumsurfaces;
1863 }
1864
1865 void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1866 {
1867         model_t *model = ent->model;
1868         vec3_t lightmins, lightmaxs;
1869         msurface_t *surface;
1870         int surfacelistindex, j, t;
1871         const int *e;
1872         const float *v[3];
1873         if (r_drawcollisionbrushes.integer < 2)
1874         {
1875                 lightmins[0] = relativelightorigin[0] - lightradius;
1876                 lightmins[1] = relativelightorigin[1] - lightradius;
1877                 lightmins[2] = relativelightorigin[2] - lightradius;
1878                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1879                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1880                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1881                 R_Mesh_Matrix(&ent->matrix);
1882                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1883                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1884                 {
1885                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1886                         for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1887                         {
1888                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1889                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1890                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1891                                 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])))
1892                                         shadowmarklist[numshadowmark++] = t;
1893                         }
1894                 }
1895                 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
1896         }
1897 }
1898
1899 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)
1900 {
1901         model_t *model = ent->model;
1902         vec3_t lightmins, lightmaxs, modelorg;
1903         msurface_t *surface;
1904         texture_t *t;
1905         int surfacelistindex;
1906         if (r_drawcollisionbrushes.integer < 2)
1907         {
1908                 lightmins[0] = relativelightorigin[0] - lightradius;
1909                 lightmins[1] = relativelightorigin[1] - lightradius;
1910                 lightmins[2] = relativelightorigin[2] - lightradius;
1911                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1912                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1913                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1914                 R_Mesh_Matrix(&ent->matrix);
1915                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1916                 R_UpdateTextureInfo(ent);
1917                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1918                 {
1919                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1920                         if (r_shadow_compilingrtlight)
1921                         {
1922                                 // if compiling an rtlight, capture the mesh
1923                                 t = surface->texinfo->texture;
1924                                 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1925                                         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);
1926                         }
1927                         else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1928                         {
1929                                 t = surface->texinfo->texture->currentframe;
1930                                 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1931                                         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);
1932                         }
1933                 }
1934         }
1935 }
1936
1937 void R_DrawCollisionBrush(colbrushf_t *brush)
1938 {
1939         int i;
1940         rmeshstate_t m;
1941         memset(&m, 0, sizeof(m));
1942         m.pointer_vertex = brush->points->v;
1943         R_Mesh_State(&m);
1944         i = ((int)brush) / sizeof(colbrushf_t);
1945         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1946         GL_LockArrays(0, brush->numpoints);
1947         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1948         GL_LockArrays(0, 0);
1949 }
1950
1951 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1952 {
1953         int i;
1954         rmeshstate_t m;
1955         if (!face->num_collisiontriangles)
1956                 return;
1957         memset(&m, 0, sizeof(m));
1958         m.pointer_vertex = face->data_collisionvertex3f;
1959         R_Mesh_State(&m);
1960         i = ((int)face) / sizeof(q3msurface_t);
1961         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1962         GL_LockArrays(0, face->num_collisionvertices);
1963         R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1964         GL_LockArrays(0, 0);
1965 }
1966
1967 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1968 {
1969         rmeshstate_t m;
1970         if (!face->num_triangles)
1971                 return;
1972         c_faces++;
1973         if (skyrendernow)
1974         {
1975                 skyrendernow = false;
1976                 if (skyrendermasked)
1977                         R_Sky();
1978         }
1979
1980         R_Mesh_Matrix(&ent->matrix);
1981
1982         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1983         if (skyrendermasked)
1984         {
1985                 // depth-only (masking)
1986                 GL_ColorMask(0,0,0,0);
1987                 // just to make sure that braindead drivers don't draw anything
1988                 // despite that colormask...
1989                 GL_BlendFunc(GL_ZERO, GL_ONE);
1990         }
1991         else
1992         {
1993                 // fog sky
1994                 GL_BlendFunc(GL_ONE, GL_ZERO);
1995         }
1996         GL_DepthMask(true);
1997         GL_DepthTest(true);
1998
1999         memset(&m, 0, sizeof(m));
2000         m.pointer_vertex = face->data_vertex3f;
2001         R_Mesh_State(&m);
2002
2003         GL_LockArrays(0, face->num_vertices);
2004         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2005         GL_LockArrays(0, 0);
2006         GL_ColorMask(1,1,1,1);
2007 }
2008
2009 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
2010 {
2011         rmeshstate_t m;
2012         memset(&m, 0, sizeof(m));
2013         GL_BlendFunc(GL_ONE, GL_ZERO);
2014         GL_DepthMask(true);
2015         GL_DepthTest(true);
2016         if (face->texture->skin.glow)
2017         {
2018                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2019                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2020                 GL_Color(1, 1, 1, 1);
2021         }
2022         else
2023                 GL_Color(0, 0, 0, 1);
2024         m.pointer_vertex = face->data_vertex3f;
2025         R_Mesh_State(&m);
2026         GL_LockArrays(0, face->num_vertices);
2027         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2028         GL_LockArrays(0, 0);
2029 }
2030
2031 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
2032 {
2033         rmeshstate_t m;
2034         memset(&m, 0, sizeof(m));
2035         GL_BlendFunc(GL_ONE, GL_ZERO);
2036         GL_DepthMask(true);
2037         GL_DepthTest(true);
2038         m.tex[0] = R_GetTexture(face->texture->skin.base);
2039         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2040         m.tex[1] = R_GetTexture(face->lightmaptexture);
2041         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2042         m.texrgbscale[1] = 2;
2043         GL_Color(1, 1, 1, 1);
2044         m.pointer_vertex = face->data_vertex3f;
2045         R_Mesh_State(&m);
2046         GL_LockArrays(0, face->num_vertices);
2047         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2048         GL_LockArrays(0, 0);
2049 }
2050
2051 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2052 {
2053         rmeshstate_t m;
2054         memset(&m, 0, sizeof(m));
2055         GL_BlendFunc(GL_ONE, GL_ZERO);
2056         GL_DepthMask(true);
2057         GL_DepthTest(true);
2058         m.tex[0] = R_GetTexture(face->texture->skin.base);
2059         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2060         GL_Color(1, 1, 1, 1);
2061         m.pointer_vertex = face->data_vertex3f;
2062         R_Mesh_State(&m);
2063         GL_LockArrays(0, face->num_vertices);
2064         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2065         GL_LockArrays(0, 0);
2066 }
2067
2068 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2069 {
2070         rmeshstate_t m;
2071         memset(&m, 0, sizeof(m));
2072         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2073         GL_DepthMask(false);
2074         GL_DepthTest(true);
2075         m.tex[0] = R_GetTexture(face->lightmaptexture);
2076         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2077         GL_Color(1, 1, 1, 1);
2078         m.pointer_vertex = face->data_vertex3f;
2079         R_Mesh_State(&m);
2080         GL_LockArrays(0, face->num_vertices);
2081         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2082         GL_LockArrays(0, 0);
2083 }
2084
2085 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2086 {
2087         rmeshstate_t m;
2088         memset(&m, 0, sizeof(m));
2089         GL_BlendFunc(GL_ONE, GL_ZERO);
2090         GL_DepthMask(true);
2091         GL_DepthTest(true);
2092         m.tex[0] = R_GetTexture(face->lightmaptexture);
2093         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2094         if (r_shadow_realtime_world.integer)
2095                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2096         else
2097                 GL_Color(1, 1, 1, 1);
2098         m.pointer_vertex = face->data_vertex3f;
2099         R_Mesh_State(&m);
2100         GL_LockArrays(0, face->num_vertices);
2101         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2102         GL_LockArrays(0, 0);
2103 }
2104
2105 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3msurface_t *face)
2106 {
2107         rmeshstate_t m;
2108         memset(&m, 0, sizeof(m));
2109         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2110         GL_DepthMask(false);
2111         GL_DepthTest(true);
2112         if (face->texture->skin.glow)
2113         {
2114                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2115                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2116                 GL_Color(1, 1, 1, 1);
2117         }
2118         else
2119                 GL_Color(0, 0, 0, 1);
2120         m.pointer_vertex = face->data_vertex3f;
2121         R_Mesh_State(&m);
2122         GL_LockArrays(0, face->num_vertices);
2123         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2124         GL_LockArrays(0, 0);
2125 }
2126
2127 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
2128 {
2129         int i;
2130         float mul;
2131         rmeshstate_t m;
2132         memset(&m, 0, sizeof(m));
2133         GL_BlendFunc(GL_ONE, GL_ZERO);
2134         GL_DepthMask(true);
2135         GL_DepthTest(true);
2136         m.tex[0] = R_GetTexture(face->texture->skin.base);
2137         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2138         mul = 2.0f;
2139         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2140                 mul *= r_shadow_realtime_world_lightmaps.value;
2141         if (mul == 2 && gl_combine.integer)
2142         {
2143                 m.texrgbscale[0] = 2;
2144                 m.pointer_color = face->data_color4f;
2145         }
2146         else if (mul == 1)
2147                 m.pointer_color = face->data_color4f;
2148         else
2149         {
2150                 for (i = 0;i < face->num_vertices;i++)
2151                 {
2152                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2153                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2154                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2155                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2156                 }
2157                 m.pointer_color = varray_color4f;
2158         }
2159         m.pointer_vertex = face->data_vertex3f;
2160         R_Mesh_State(&m);
2161         GL_LockArrays(0, face->num_vertices);
2162         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2163         GL_LockArrays(0, 0);
2164 }
2165
2166 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2167 {
2168         int i;
2169         float mul;
2170         rmeshstate_t m;
2171         memset(&m, 0, sizeof(m));
2172         GL_BlendFunc(GL_ONE, GL_ZERO);
2173         GL_DepthMask(true);
2174         GL_DepthTest(true);
2175         mul = 2.0f;
2176         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2177                 mul *= r_shadow_realtime_world_lightmaps.value;
2178         if (mul == 1)
2179                 m.pointer_color = face->data_color4f;
2180         else
2181         {
2182                 for (i = 0;i < face->num_vertices;i++)
2183                 {
2184                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2185                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2186                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2187                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2188                 }
2189                 m.pointer_color = varray_color4f;
2190         }
2191         m.pointer_vertex = face->data_vertex3f;
2192         R_Mesh_State(&m);
2193         GL_LockArrays(0, face->num_vertices);
2194         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2195         GL_LockArrays(0, 0);
2196 }
2197
2198 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2199 {
2200         rmeshstate_t m;
2201         memset(&m, 0, sizeof(m));
2202         GL_BlendFunc(GL_ONE, GL_ONE);
2203         GL_DepthMask(true);
2204         GL_DepthTest(true);
2205         m.tex[0] = R_GetTexture(face->texture->skin.base);
2206         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2207         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2208         m.pointer_vertex = face->data_vertex3f;
2209         R_Mesh_State(&m);
2210         GL_LockArrays(0, face->num_vertices);
2211         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2212         GL_LockArrays(0, 0);
2213 }
2214
2215 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2216 {
2217         const entity_render_t *ent = voident;
2218         q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2219         rmeshstate_t m;
2220         R_Mesh_Matrix(&ent->matrix);
2221         memset(&m, 0, sizeof(m));
2222         if (ent->effects & EF_ADDITIVE)
2223                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2224         else
2225                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2226         GL_DepthMask(false);
2227         GL_DepthTest(true);
2228         m.tex[0] = R_GetTexture(face->texture->skin.base);
2229         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2230         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2231         if (gl_combine.integer)
2232         {
2233                 m.texrgbscale[0] = 2;
2234                 if (r_textureunits.integer >= 2)
2235                 {
2236                         m.tex[1] = R_GetTexture(face->lightmaptexture);
2237                         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2238                         GL_Color(1, 1, 1, ent->alpha);
2239                 }
2240                 else
2241                 {
2242                         if (ent->alpha == 1)
2243                                 m.pointer_color = face->data_color4f;
2244                         else
2245                         {
2246                                 int i;
2247                                 for (i = 0;i < face->num_vertices;i++)
2248                                 {
2249                                         varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2250                                         varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2251                                         varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2252                                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2253                                 }
2254                                 m.pointer_color = varray_color4f;
2255                         }
2256                 }
2257         }
2258         else
2259         {
2260                 int i;
2261                 for (i = 0;i < face->num_vertices;i++)
2262                 {
2263                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2264                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2265                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2266                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2267                 }
2268                 m.pointer_color = varray_color4f;
2269         }
2270         m.pointer_vertex = face->data_vertex3f;
2271         R_Mesh_State(&m);
2272         qglDisable(GL_CULL_FACE);
2273         GL_LockArrays(0, face->num_vertices);
2274         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2275         GL_LockArrays(0, 0);
2276         qglEnable(GL_CULL_FACE);
2277 }
2278
2279 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2280 {
2281         if (!face->num_triangles)
2282                 return;
2283         if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2284                 return;
2285         c_faces++;
2286         if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2287         {
2288                 vec3_t facecenter, center;
2289                 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2290                 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2291                 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2292                 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2293                 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2294                 return;
2295         }
2296         R_Mesh_Matrix(&ent->matrix);
2297         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2298                 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2299         else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2300         {
2301                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2302                 if (face->texture->skin.glow)
2303                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2304         }
2305         else if (face->lightmaptexture)
2306         {
2307                 if (gl_lightmaps.integer)
2308                         R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2309                 else
2310                 {
2311                         if (r_textureunits.integer >= 2 && gl_combine.integer)
2312                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2313                         else
2314                         {
2315                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2316                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2317                         }
2318                         if (face->texture->skin.glow)
2319                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2320                 }
2321         }
2322         else
2323         {
2324                 if (gl_lightmaps.integer)
2325                         R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2326                 else
2327                 {
2328                         R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2329                         if (face->texture->skin.glow)
2330                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2331                 }
2332         }
2333         if (r_ambient.value)
2334                 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2335 }
2336
2337 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2338 {
2339         int i;
2340         q3mleaf_t *leaf;
2341         for (;;)
2342         {
2343                 if (R_CullBox(node->mins, node->maxs))
2344                         return;
2345                 if (!node->plane)
2346                         break;
2347                 c_nodes++;
2348                 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2349                 node = node->children[1];
2350         }
2351         leaf = (q3mleaf_t *)node;
2352         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2353         {
2354                 c_leafs++;
2355                 for (i = 0;i < leaf->numleaffaces;i++)
2356                         leaf->firstleafface[i]->visframe = r_framecount;
2357         }
2358 }
2359
2360 // FIXME: num_leafs needs to be recalculated at load time to include only
2361 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2362 // the submodels (which would render the submodels occasionally, as part of
2363 // the world - not good)
2364 void R_Q3BSP_MarkLeafPVS(void)
2365 {
2366         int i, j;
2367         q3mleaf_t *leaf;
2368         for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2369         {
2370                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2371                 {
2372                         c_leafs++;
2373                         for (i = 0;i < leaf->numleaffaces;i++)
2374                                 leaf->firstleafface[i]->visframe = r_framecount;
2375                 }
2376         }
2377 }
2378
2379 static int r_q3bsp_framecount = -1;
2380
2381 void R_Q3BSP_DrawSky(entity_render_t *ent)
2382 {
2383         int i;
2384         q3msurface_t *face;
2385         vec3_t modelorg;
2386         model_t *model;
2387         R_Mesh_Matrix(&ent->matrix);
2388         model = ent->model;
2389         if (r_drawcollisionbrushes.integer < 2)
2390         {
2391                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2392                 if (ent == &cl_entities[0].render)
2393                 {
2394                         if (r_q3bsp_framecount != r_framecount)
2395                         {
2396                                 r_q3bsp_framecount = r_framecount;
2397                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2398                                 //R_Q3BSP_MarkLeafPVS();
2399                         }
2400                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2401                                 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2402                                         R_Q3BSP_DrawSkyFace(ent, face);
2403                 }
2404                 else
2405                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2406                                 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2407                                         R_Q3BSP_DrawSkyFace(ent, face);
2408         }
2409 }
2410
2411 void R_Q3BSP_Draw(entity_render_t *ent)
2412 {
2413         int i;
2414         q3msurface_t *face;
2415         vec3_t modelorg;
2416         model_t *model;
2417         qbyte *pvs;
2418         R_Mesh_Matrix(&ent->matrix);
2419         model = ent->model;
2420         if (r_drawcollisionbrushes.integer < 2)
2421         {
2422                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2423                 if (ent == &cl_entities[0].render)
2424                 {
2425                         if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2426                         if (r_q3bsp_framecount != r_framecount)
2427                         {
2428                                 r_q3bsp_framecount = r_framecount;
2429                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2430                                 //R_Q3BSP_MarkLeafPVS();
2431                         }
2432                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2433                                 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2434                                         R_Q3BSP_DrawFace(ent, face);
2435                 }
2436                 else
2437                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2438                                 R_Q3BSP_DrawFace(ent, face);
2439         }
2440         if (r_drawcollisionbrushes.integer >= 1)
2441         {
2442                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2443                 GL_DepthMask(false);
2444                 GL_DepthTest(true);
2445                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2446                 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2447                         if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2448                                 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2449                 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2450                         if (face->num_collisiontriangles)
2451                                 R_Q3BSP_DrawCollisionFace(ent, face);
2452                 qglPolygonOffset(0, 0);
2453         }
2454 }
2455
2456 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)
2457 {
2458         model_t *model = ent->model;
2459         vec3_t lightmins, lightmaxs;
2460         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2461         const int *e;
2462         const float *v[3];
2463         q3msurface_t *surface;
2464         q3mleaf_t *leaf;
2465         const qbyte *pvs;
2466         lightmins[0] = relativelightorigin[0] - lightradius;
2467         lightmins[1] = relativelightorigin[1] - lightradius;
2468         lightmins[2] = relativelightorigin[2] - lightradius;
2469         lightmaxs[0] = relativelightorigin[0] + lightradius;
2470         lightmaxs[1] = relativelightorigin[1] + lightradius;
2471         lightmaxs[2] = relativelightorigin[2] + lightradius;
2472         *outnumclusterspointer = 0;
2473         *outnumsurfacespointer = 0;
2474         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2475         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2476         if (model == NULL)
2477         {
2478                 VectorCopy(lightmins, outmins);
2479                 VectorCopy(lightmaxs, outmaxs);
2480                 return;
2481         }
2482         VectorCopy(relativelightorigin, outmins);
2483         VectorCopy(relativelightorigin, outmaxs);
2484         if (model->brush.GetPVS)
2485                 pvs = model->brush.GetPVS(model, relativelightorigin);
2486         else
2487                 pvs = NULL;
2488         // FIXME: use BSP recursion as lights are often small
2489         for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2490         {
2491                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2492                 {
2493                         outmins[0] = min(outmins[0], leaf->mins[0]);
2494                         outmins[1] = min(outmins[1], leaf->mins[1]);
2495                         outmins[2] = min(outmins[2], leaf->mins[2]);
2496                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2497                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2498                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2499                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2500                         {
2501                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2502                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
2503                         }
2504                         for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2505                         {
2506                                 surface = leaf->firstleafface[marksurfaceindex];
2507                                 surfaceindex = surface - model->brushq3.data_faces;
2508                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2509                                 {
2510                                         if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2511                                         {
2512                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2513                                                 {
2514                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2515                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2516                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2517                                                         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])))
2518                                                         {
2519                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2520                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2521                                                                 break;
2522                                                         }
2523                                                 }
2524                                         }
2525                                 }
2526                         }
2527                 }
2528         }
2529
2530         // limit combined leaf box to light boundaries
2531         outmins[0] = max(outmins[0], lightmins[0]);
2532         outmins[1] = max(outmins[1], lightmins[1]);
2533         outmins[2] = max(outmins[2], lightmins[2]);
2534         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2535         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2536         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2537
2538         *outnumclusterspointer = outnumclusters;
2539         *outnumsurfacespointer = outnumsurfaces;
2540 }
2541
2542 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2543 {
2544         model_t *model = ent->model;
2545         vec3_t lightmins, lightmaxs;
2546         q3msurface_t *surface;
2547         int surfacelistindex, j, t;
2548         const int *e;
2549         const float *v[3];
2550         if (r_drawcollisionbrushes.integer < 2)
2551         {
2552                 lightmins[0] = relativelightorigin[0] - lightradius;
2553                 lightmins[1] = relativelightorigin[1] - lightradius;
2554                 lightmins[2] = relativelightorigin[2] - lightradius;
2555                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2556                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2557                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2558                 R_Mesh_Matrix(&ent->matrix);
2559                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2560                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2561                 {
2562                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2563                         // FIXME: check some manner of face->rendermode here?
2564                         if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2565                         {
2566                                 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2567                                 {
2568                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2569                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2570                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2571                                         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])))
2572                                                 shadowmarklist[numshadowmark++] = t;
2573                                 }
2574                         }
2575                 }
2576                 R_Shadow_VolumeFromList(model->brush.shadowmesh->numverts, model->brush.shadowmesh->numtriangles, model->brush.shadowmesh->vertex3f, model->brush.shadowmesh->element3i, model->brush.shadowmesh->neighbor3i, relativelightorigin, lightradius + model->radius + r_shadow_projectdistance.value, numshadowmark, shadowmarklist);
2577         }
2578 }
2579
2580 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)
2581 {
2582         model_t *model = ent->model;
2583         vec3_t lightmins, lightmaxs, modelorg;
2584         q3msurface_t *surface;
2585         int surfacelistindex;
2586         if (r_drawcollisionbrushes.integer < 2)
2587         {
2588                 lightmins[0] = relativelightorigin[0] - lightradius;
2589                 lightmins[1] = relativelightorigin[1] - lightradius;
2590                 lightmins[2] = relativelightorigin[2] - lightradius;
2591                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2592                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2593                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2594                 R_Mesh_Matrix(&ent->matrix);
2595                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2596                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2597                 {
2598                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2599                         if (r_shadow_compilingrtlight)
2600                         {
2601                                 // if compiling an rtlight, capture the mesh
2602                                 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);
2603                         }
2604                         else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2605                                 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);
2606                 }
2607         }
2608 }
2609
2610 static void gl_surf_start(void)
2611 {
2612 }
2613
2614 static void gl_surf_shutdown(void)
2615 {
2616 }
2617
2618 static void gl_surf_newmap(void)
2619 {
2620 }
2621
2622 void GL_Surf_Init(void)
2623 {
2624         int i;
2625         dlightdivtable[0] = 4194304;
2626         for (i = 1;i < 32768;i++)
2627                 dlightdivtable[i] = 4194304 / (i << 7);
2628
2629         Cvar_RegisterVariable(&r_ambient);
2630         Cvar_RegisterVariable(&r_drawportals);
2631         Cvar_RegisterVariable(&r_testvis);
2632         Cvar_RegisterVariable(&r_floatbuildlightmap);
2633         Cvar_RegisterVariable(&r_detailtextures);
2634         Cvar_RegisterVariable(&r_surfaceworldnode);
2635         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2636         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2637         Cvar_RegisterVariable(&gl_lightmaps);
2638
2639         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2640 }
2641