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