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