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