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