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