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