]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
made the player setup menu actually change the rate cvar (like it was supposed to)
[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_Water_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 modelorg[3];
776         texture_t *texture;
777         float args[4] = {0.05f,0,0,0.04f};
778         int rendertype;
779
780         R_Mesh_Matrix(&ent->matrix);
781         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
782
783         texture = surf->texinfo->texture->currentframe;
784         currentalpha = texture->currentalpha;
785         if (ent->effects & EF_ADDITIVE)
786         {
787                 rendertype = SURFRENDER_ADD;
788                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
789                 GL_DepthMask(false);
790         }
791         else if (currentalpha < 1 || texture->skin.fog != NULL)
792         {
793                 rendertype = SURFRENDER_ALPHA;
794                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
795                 GL_DepthMask(false);
796         }
797         else
798         {
799                 rendertype = SURFRENDER_OPAQUE;
800                 GL_BlendFunc(GL_ONE, GL_ZERO);
801                 GL_DepthMask(true);
802         }
803         GL_DepthTest(true);
804         GL_Color(1, 1, 1, currentalpha);
805         memset(&m, 0, sizeof(m));
806         m.pointer_vertex = surf->mesh.data_vertex3f;
807         if (gl_textureshader && r_watershader.value && !fogenabled)
808         {
809                 m.tex[0] = R_GetTexture(mod_shared_distorttexture[(int)(cl.time * 16)&63]);
810                 m.tex[1] = R_GetTexture(texture->skin.base);
811                 m.texcombinergb[0] = GL_REPLACE;
812                 m.texcombinergb[1] = GL_REPLACE;
813                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
814                 m.pointer_texcoord[1] = surf->mesh.data_texcoordtexture2f;
815                 Matrix4x4_CreateFromQuakeEntity(&m.texmatrix[0], 0, 0, 0, 0, 0, 0, r_watershader.value);
816                 Matrix4x4_CreateTranslate(&m.texmatrix[1], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
817                 R_Mesh_State(&m);
818
819                 GL_ActiveTexture(0);
820                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
821                 GL_ActiveTexture(1);
822                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_OFFSET_TEXTURE_2D_NV);
823                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_PREVIOUS_TEXTURE_INPUT_NV, GL_TEXTURE0_ARB);
824                 qglTexEnvfv(GL_TEXTURE_SHADER_NV, GL_OFFSET_TEXTURE_MATRIX_NV, &args[0]);
825                 qglEnable(GL_TEXTURE_SHADER_NV);
826
827                 GL_LockArrays(0, surf->mesh.num_vertices);
828                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
829                 GL_LockArrays(0, 0);
830
831                 qglDisable(GL_TEXTURE_SHADER_NV);
832                 qglTexEnvi(GL_TEXTURE_SHADER_NV, GL_SHADER_OPERATION_NV, GL_TEXTURE_2D);
833                 GL_ActiveTexture(0);
834         }
835         else
836         {
837                 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
838                 m.pointer_color = varray_color4f;
839                 m.tex[0] = R_GetTexture(texture->skin.base);
840                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
841                 if (r_waterscroll.value)
842                 {
843                         // scrolling in texture matrix
844                         Matrix4x4_CreateTranslate(&m.texmatrix[0], sin(cl.time) * 0.025 * r_waterscroll.value, sin(cl.time * 0.8f) * 0.025 * r_waterscroll.value, 0);
845                 }
846                 R_Mesh_State(&m);
847                 GL_LockArrays(0, surf->mesh.num_vertices);
848                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
849                 GL_LockArrays(0, 0);
850                 if (fogenabled && rendertype != SURFRENDER_ADD)
851                 {
852                         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
853                         GL_DepthMask(false);
854                         RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
855                         m.tex[0] = R_GetTexture(texture->skin.fog);
856                         R_Mesh_State(&m);
857                         GL_LockArrays(0, surf->mesh.num_vertices);
858                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
859                         GL_LockArrays(0, 0);
860                 }
861         }
862 }
863
864 static void RSurfShader_Wall_Vertex_Callback(const void *calldata1, int calldata2)
865 {
866         const entity_render_t *ent = calldata1;
867         const msurface_t *surf = ent->model->brushq1.surfaces + calldata2;
868         int rendertype;
869         float currentalpha;
870         texture_t *texture;
871         float base, colorscale;
872         rmeshstate_t m;
873         vec3_t modelorg;
874         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
875         R_Mesh_Matrix(&ent->matrix);
876
877         texture = surf->texinfo->texture;
878         if (texture->animated)
879                 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];
880
881         currentalpha = ent->alpha;
882         base = ent->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
883         GL_DepthTest(true);
884         if (ent->effects & EF_ADDITIVE)
885         {
886                 rendertype = SURFRENDER_ADD;
887                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
888                 GL_DepthMask(false);
889         }
890         else if (currentalpha < 1 || texture->skin.fog != NULL)
891         {
892                 rendertype = SURFRENDER_ALPHA;
893                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
894                 GL_DepthMask(false);
895         }
896         else
897         {
898                 rendertype = SURFRENDER_OPAQUE;
899                 GL_BlendFunc(GL_ONE, GL_ZERO);
900                 GL_DepthMask(true);
901         }
902
903         {
904                 memset(&m, 0, sizeof(m));
905                 m.tex[0] = R_GetTexture(texture->skin.base);
906                 colorscale = 1;
907                 if (gl_combine.integer)
908                 {
909                         m.texrgbscale[0] = 4;
910                         colorscale *= 0.25f;
911                 }
912                 m.pointer_color = varray_color4f;
913                 m.pointer_vertex = surf->mesh.data_vertex3f;
914                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
915                 R_Mesh_State(&m);
916                 R_FillColors(varray_color4f, surf->mesh.num_vertices, base, base, base, currentalpha);
917                 if (!(ent->effects & EF_FULLBRIGHT))
918                 {
919                         if (surf->dlightframe == r_framecount)
920                                 RSurf_LightSeparate_Vertex3f_Color4f(&ent->inversematrix, surf->dlightbits, surf->mesh.num_vertices, surf->mesh.data_vertex3f, varray_color4f, 1);
921                         if (surf->flags & SURF_LIGHTMAP)
922                                 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);
923                 }
924                 RSurf_FogColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, colorscale, surf->mesh.num_vertices, modelorg);
925                 GL_LockArrays(0, surf->mesh.num_vertices);
926                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
927                 GL_LockArrays(0, 0);
928         }
929         if (texture->skin.glow)
930         {
931                 memset(&m, 0, sizeof(m));
932                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
933                 GL_DepthMask(false);
934                 m.pointer_color = varray_color4f;
935                 m.tex[0] = R_GetTexture(texture->skin.glow);
936                 m.pointer_vertex = surf->mesh.data_vertex3f;
937                 if (m.tex[0])
938                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
939                 R_Mesh_State(&m);
940                 RSurf_FoggedColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, 1, 1, 1, currentalpha, 1, surf->mesh.num_vertices, modelorg);
941                 GL_LockArrays(0, surf->mesh.num_vertices);
942                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
943                 GL_LockArrays(0, 0);
944         }
945         if (fogenabled && rendertype != SURFRENDER_ADD)
946         {
947                 memset(&m, 0, sizeof(m));
948                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
949                 GL_DepthMask(false);
950                 m.pointer_color = varray_color4f;
951                 m.tex[0] = R_GetTexture(texture->skin.fog);
952                 m.pointer_vertex = surf->mesh.data_vertex3f;
953                 if (m.tex[0])
954                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
955                 R_Mesh_State(&m);
956                 RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], currentalpha, 1, surf->mesh.num_vertices, modelorg);
957                 GL_LockArrays(0, surf->mesh.num_vertices);
958                 R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
959                 GL_LockArrays(0, 0);
960         }
961 }
962
963 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
964 {
965         const msurface_t *surf;
966         rmeshstate_t m;
967         int lightmaptexturenum;
968         memset(&m, 0, sizeof(m));
969         GL_BlendFunc(GL_ONE, GL_ZERO);
970         GL_DepthMask(true);
971         GL_DepthTest(true);
972         m.tex[0] = R_GetTexture(texture->skin.base);
973         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
974         m.texrgbscale[1] = 2;
975         m.tex[2] = R_GetTexture(texture->skin.detail);
976         m.texrgbscale[2] = 2;
977         if (texture->skin.glow)
978         {
979                 m.tex[3] = R_GetTexture(texture->skin.glow);
980                 m.texcombinergb[3] = GL_ADD;
981         }
982         if (r_shadow_realtime_world.integer)
983                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
984         else
985                 GL_Color(1, 1, 1, 1);
986
987         while((surf = *surfchain++) != NULL)
988         {
989                 if (surf->visframe == r_framecount)
990                 {
991                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
992                         m.tex[1] = lightmaptexturenum;
993                         m.pointer_vertex = surf->mesh.data_vertex3f;
994                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
995                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
996                         m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
997                         m.pointer_texcoord[3] = surf->mesh.data_texcoordtexture2f;
998                         R_Mesh_State(&m);
999                         GL_LockArrays(0, surf->mesh.num_vertices);
1000                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1001                         GL_LockArrays(0, 0);
1002                 }
1003         }
1004 }
1005
1006 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1007 {
1008         const msurface_t *surf;
1009         rmeshstate_t m;
1010         int lightmaptexturenum;
1011         memset(&m, 0, sizeof(m));
1012         GL_BlendFunc(GL_ONE, GL_ZERO);
1013         GL_DepthMask(true);
1014         GL_DepthTest(true);
1015         m.tex[0] = R_GetTexture(texture->skin.base);
1016         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1017         m.texrgbscale[1] = 2;
1018         m.tex[2] = R_GetTexture(texture->skin.detail);
1019         m.texrgbscale[2] = 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
1025         while((surf = *surfchain++) != NULL)
1026         {
1027                 if (surf->visframe == r_framecount)
1028                 {
1029                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1030                         m.tex[1] = lightmaptexturenum;
1031                         m.pointer_vertex = surf->mesh.data_vertex3f;
1032                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1033                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1034                         m.pointer_texcoord[2] = surf->mesh.data_texcoorddetail2f;
1035                         R_Mesh_State(&m);
1036                         GL_LockArrays(0, surf->mesh.num_vertices);
1037                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1038                         GL_LockArrays(0, 0);
1039                 }
1040         }
1041 }
1042
1043 static void RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1044 {
1045         const msurface_t *surf;
1046         rmeshstate_t m;
1047         int lightmaptexturenum;
1048         memset(&m, 0, sizeof(m));
1049         GL_BlendFunc(GL_ONE, GL_ZERO);
1050         GL_DepthMask(true);
1051         GL_DepthTest(true);
1052         m.tex[0] = R_GetTexture(texture->skin.base);
1053         m.tex[1] = R_GetTexture((**surfchain).lightmaptexture);
1054         m.texrgbscale[1] = 2;
1055         if (r_shadow_realtime_world.integer)
1056                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1057         else
1058                 GL_Color(1, 1, 1, 1);
1059         while((surf = *surfchain++) != NULL)
1060         {
1061                 if (surf->visframe == r_framecount)
1062                 {
1063                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1064                         m.tex[1] = lightmaptexturenum;
1065                         m.pointer_vertex = surf->mesh.data_vertex3f;
1066                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1067                         m.pointer_texcoord[1] = surf->mesh.data_texcoordlightmap2f;
1068                         R_Mesh_State(&m);
1069                         GL_LockArrays(0, surf->mesh.num_vertices);
1070                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1071                         GL_LockArrays(0, 0);
1072                 }
1073         }
1074 }
1075
1076 static void RSurfShader_OpaqueWall_Pass_BaseTexture(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1077 {
1078         const msurface_t *surf;
1079         rmeshstate_t m;
1080         memset(&m, 0, sizeof(m));
1081         GL_DepthMask(true);
1082         GL_DepthTest(true);
1083         GL_BlendFunc(GL_ONE, GL_ZERO);
1084         m.tex[0] = R_GetTexture(texture->skin.base);
1085         if (r_shadow_realtime_world.integer)
1086                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1087         else
1088                 GL_Color(1, 1, 1, 1);
1089         while((surf = *surfchain++) != NULL)
1090         {
1091                 if (surf->visframe == r_framecount)
1092                 {
1093                         m.pointer_vertex = surf->mesh.data_vertex3f;
1094                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1095                         R_Mesh_State(&m);
1096                         GL_LockArrays(0, surf->mesh.num_vertices);
1097                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1098                         GL_LockArrays(0, 0);
1099                 }
1100         }
1101 }
1102
1103 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1104 {
1105         const msurface_t *surf;
1106         rmeshstate_t m;
1107         int lightmaptexturenum;
1108         memset(&m, 0, sizeof(m));
1109         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1110         GL_DepthMask(false);
1111         GL_DepthTest(true);
1112         m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1113         GL_Color(1, 1, 1, 1);
1114         while((surf = *surfchain++) != NULL)
1115         {
1116                 if (surf->visframe == r_framecount)
1117                 {
1118                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1119                         m.tex[0] = lightmaptexturenum;
1120                         m.pointer_vertex = surf->mesh.data_vertex3f;
1121                         m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1122                         R_Mesh_State(&m);
1123                         GL_LockArrays(0, surf->mesh.num_vertices);
1124                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1125                         GL_LockArrays(0, 0);
1126                 }
1127         }
1128 }
1129
1130 static void RSurfShader_OpaqueWall_Pass_BaseAmbient(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1131 {
1132         const msurface_t *surf;
1133         rmeshstate_t m;
1134         memset(&m, 0, sizeof(m));
1135         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1136         GL_DepthMask(false);
1137         GL_DepthTest(true);
1138         m.tex[0] = R_GetTexture(texture->skin.base);
1139         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1140         while((surf = *surfchain++) != NULL)
1141         {
1142                 if (surf->visframe == r_framecount)
1143                 {
1144                         m.pointer_vertex = surf->mesh.data_vertex3f;
1145                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1146                         R_Mesh_State(&m);
1147                         GL_LockArrays(0, surf->mesh.num_vertices);
1148                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1149                         GL_LockArrays(0, 0);
1150                 }
1151         }
1152 }
1153
1154 static void RSurfShader_OpaqueWall_Pass_Fog(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1155 {
1156         const msurface_t *surf;
1157         rmeshstate_t m;
1158         float modelorg[3];
1159         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1160         memset(&m, 0, sizeof(m));
1161         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1162         GL_DepthMask(false);
1163         GL_DepthTest(true);
1164         m.pointer_color = varray_color4f;
1165         while((surf = *surfchain++) != NULL)
1166         {
1167                 if (surf->visframe == r_framecount)
1168                 {
1169                         m.pointer_vertex = surf->mesh.data_vertex3f;
1170                         if (m.tex[0])
1171                                 m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1172                         R_Mesh_State(&m);
1173                         RSurf_FogPassColors_Vertex3f_Color4f(surf->mesh.data_vertex3f, varray_color4f, fogcolor[0], fogcolor[1], fogcolor[2], 1, 1, surf->mesh.num_vertices, modelorg);
1174                         GL_LockArrays(0, surf->mesh.num_vertices);
1175                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1176                         GL_LockArrays(0, 0);
1177                 }
1178         }
1179 }
1180
1181 static void RSurfShader_OpaqueWall_Pass_BaseDetail(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1182 {
1183         const msurface_t *surf;
1184         rmeshstate_t m;
1185         memset(&m, 0, sizeof(m));
1186         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
1187         GL_DepthMask(false);
1188         GL_DepthTest(true);
1189         m.tex[0] = R_GetTexture(texture->skin.detail);
1190         GL_Color(1, 1, 1, 1);
1191         while((surf = *surfchain++) != NULL)
1192         {
1193                 if (surf->visframe == r_framecount)
1194                 {
1195                         m.pointer_vertex = surf->mesh.data_vertex3f;
1196                         m.pointer_texcoord[0] = surf->mesh.data_texcoorddetail2f;
1197                         R_Mesh_State(&m);
1198                         GL_LockArrays(0, surf->mesh.num_vertices);
1199                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1200                         GL_LockArrays(0, 0);
1201                 }
1202         }
1203 }
1204
1205 static void RSurfShader_OpaqueWall_Pass_Glow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1206 {
1207         const msurface_t *surf;
1208         rmeshstate_t m;
1209         memset(&m, 0, sizeof(m));
1210         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1211         GL_DepthMask(false);
1212         GL_DepthTest(true);
1213         m.tex[0] = R_GetTexture(texture->skin.glow);
1214         GL_Color(1, 1, 1, 1);
1215         while((surf = *surfchain++) != NULL)
1216         {
1217                 if (surf->visframe == r_framecount)
1218                 {
1219                         m.pointer_vertex = surf->mesh.data_vertex3f;
1220                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1221                         R_Mesh_State(&m);
1222                         GL_LockArrays(0, surf->mesh.num_vertices);
1223                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1224                         GL_LockArrays(0, 0);
1225                 }
1226         }
1227 }
1228
1229 /*
1230 static void RSurfShader_OpaqueWall_Pass_OpaqueGlow(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1231 {
1232         const msurface_t *surf;
1233         rmeshstate_t m;
1234         memset(&m, 0, sizeof(m));
1235         GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
1236         GL_DepthMask(true);
1237         m.tex[0] = R_GetTexture(texture->skin.glow);
1238         if (m.tex[0])
1239                 GL_Color(1, 1, 1, 1);
1240         else
1241                 GL_Color(0, 0, 0, 1);
1242         while((surf = *surfchain++) != NULL)
1243         {
1244                 if (surf->visframe == r_framecount)
1245                 {
1246                         m.pointer_vertex = surf->mesh.data_vertex3f;
1247                         m.pointer_texcoord[0] = surf->mesh.data_texcoordtexture2f;
1248                         R_Mesh_State(&m);
1249                         GL_LockArrays(0, surf->mesh.num_vertices);
1250                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1251                         GL_LockArrays(0, 0);
1252                 }
1253         }
1254 }
1255 */
1256
1257 static void RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1258 {
1259         const msurface_t *surf;
1260         rmeshstate_t m;
1261         int lightmaptexturenum;
1262         memset(&m, 0, sizeof(m));
1263         GL_BlendFunc(GL_ONE, GL_ZERO);
1264         GL_DepthMask(true);
1265         GL_DepthTest(true);
1266         m.tex[0] = R_GetTexture((**surfchain).lightmaptexture);
1267         if (r_shadow_realtime_world.integer)
1268                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
1269         else
1270                 GL_Color(1, 1, 1, 1);
1271         while((surf = *surfchain++) != NULL)
1272         {
1273                 if (surf->visframe == r_framecount)
1274                 {
1275                         lightmaptexturenum = R_GetTexture(surf->lightmaptexture);
1276                         m.tex[0] = lightmaptexturenum;
1277                         m.pointer_vertex = surf->mesh.data_vertex3f;
1278                         m.pointer_texcoord[0] = surf->mesh.data_texcoordlightmap2f;
1279                         R_Mesh_State(&m);
1280                         GL_LockArrays(0, surf->mesh.num_vertices);
1281                         R_Mesh_Draw(surf->mesh.num_vertices, surf->mesh.num_triangles, surf->mesh.data_element3i);
1282                         GL_LockArrays(0, 0);
1283                 }
1284         }
1285 }
1286
1287 static void R_DrawSurfaceChain(const entity_render_t *ent, const texture_t *texture, msurface_t **surfchain)
1288 {
1289         const msurface_t *surf;
1290         msurface_t **chain;
1291         vec3_t center;
1292         if (texture->flags & SURF_LIGHTMAP)
1293         {
1294                 if (gl_lightmaps.integer)
1295                 {
1296                         RSurfShader_OpaqueWall_Pass_BaseLightmapOnly(ent, texture, surfchain);
1297                         if (fogenabled)
1298                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1299                 }
1300                 else if (texture->rendertype != SURFRENDER_OPAQUE)
1301                 {
1302                         // transparent vertex shaded from lightmap
1303                         for (chain = surfchain;(surf = *chain) != NULL;chain++)
1304                         {
1305                                 if (surf->visframe == r_framecount)
1306                                 {
1307                                         Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1308                                         R_MeshQueue_AddTransparent(center, RSurfShader_Wall_Vertex_Callback, ent, surf - ent->model->brushq1.surfaces);
1309                                 }
1310                         }
1311                 }
1312                 else if (ent->effects & EF_FULLBRIGHT || r_fullbright.integer)
1313                 {
1314                         RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1315                         if (r_detailtextures.integer)
1316                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1317                         if (texture->skin.glow)
1318                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1319                         if (fogenabled)
1320                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1321                 }
1322                 else if (r_ambient.value > 0)
1323                 {
1324                         RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1325                         RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1326                         RSurfShader_OpaqueWall_Pass_BaseAmbient(ent, texture, surfchain);
1327                         if (r_detailtextures.integer)
1328                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1329                         if (texture->skin.glow)
1330                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1331                         if (fogenabled)
1332                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1333                 }
1334                 else if (r_textureunits.integer >= 4 && gl_combine.integer && r_detailtextures.integer)
1335                 {
1336                         RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetailGlow(ent, texture, surfchain);
1337                         if (fogenabled)
1338                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1339                 }
1340                 else if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1341                 {
1342                         RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmapDetail(ent, texture, surfchain);
1343                         if (texture->skin.glow)
1344                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1345                         if (fogenabled)
1346                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1347                 }
1348                 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1349                 {
1350                         RSurfShader_OpaqueWall_Pass_BaseCombine_TextureLightmap(ent, texture, surfchain);
1351                         if (r_detailtextures.integer)
1352                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1353                         if (texture->skin.glow)
1354                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1355                         if (fogenabled)
1356                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1357                 }
1358                 else
1359                 {
1360                         RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1361                         RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1362                         if (r_detailtextures.integer)
1363                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1364                         if (texture->skin.glow)
1365                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1366                         if (fogenabled)
1367                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1368                 }
1369         }
1370         else if (texture->flags & SURF_DRAWTURB)
1371         {
1372                 for (chain = surfchain;(surf = *chain) != NULL;chain++)
1373                 {
1374                         if (surf->visframe == r_framecount)
1375                         {
1376                                 if (texture->rendertype == SURFRENDER_OPAQUE)
1377                                         RSurfShader_Water_Callback(ent, surf - ent->model->brushq1.surfaces);
1378                                 else
1379                                 {
1380                                         Matrix4x4_Transform(&ent->matrix, surf->poly_center, center);
1381                                         R_MeshQueue_AddTransparent(center, RSurfShader_Water_Callback, ent, surf - ent->model->brushq1.surfaces);
1382                                 }
1383                         }
1384                 }
1385         }
1386         else if (texture->flags & SURF_DRAWSKY)
1387                 RSurfShader_Sky(ent, texture, surfchain);
1388 }
1389
1390 void R_UpdateTextureInfo(entity_render_t *ent)
1391 {
1392         int i, texframe, alttextures;
1393         texture_t *t;
1394
1395         if (!ent->model)
1396                 return;
1397
1398         alttextures = ent->frame != 0;
1399         texframe = (int)(cl.time * 5.0f);
1400         for (i = 0;i < ent->model->brushq1.numtextures;i++)
1401         {
1402                 t = ent->model->brushq1.textures + i;
1403                 t->currentalpha = ent->alpha;
1404                 if (t->flags & SURF_WATERALPHA)
1405                         t->currentalpha *= r_wateralpha.value;
1406                 if (ent->effects & EF_ADDITIVE)
1407                         t->rendertype = SURFRENDER_ADD;
1408                 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1409                         t->rendertype = SURFRENDER_ALPHA;
1410                 else
1411                         t->rendertype = SURFRENDER_OPAQUE;
1412                 // we don't need to set currentframe if t->animated is false because
1413                 // it was already set up by the texture loader for non-animating
1414                 if (t->animated)
1415                         t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1416         }
1417 }
1418
1419 void R_UpdateLightmapInfo(entity_render_t *ent)
1420 {
1421         int i;
1422         msurface_t *surface, **surfacechain;
1423         model_t *model = ent->model;
1424         if (!model)
1425                 return;
1426         if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1427                 R_MarkLights(ent);
1428         for (i = 0;i < model->brushq1.light_styles;i++)
1429         {
1430                 if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1431                 {
1432                         model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1433                         for (surfacechain = model->brushq1.light_styleupdatechains[i];(surface = *surfacechain);surfacechain++)
1434                                 surface->cached_dlight = true;
1435                 }
1436         }
1437 }
1438
1439 void R_PrepareSurfaces(entity_render_t *ent)
1440 {
1441         int i, numsurfaces, *surfacevisframes;
1442         model_t *model;
1443         msurface_t *surf, *surfaces;
1444         vec3_t modelorg;
1445
1446         if (!ent->model)
1447                 return;
1448
1449         model = ent->model;
1450         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1451         numsurfaces = model->nummodelsurfaces;
1452         surfaces = model->brushq1.surfaces + model->firstmodelsurface;
1453         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1454         for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1455         {
1456                 if (surfacevisframes[i] == r_framecount)
1457                 {
1458 #if !WORLDNODECULLBACKFACES
1459                         // mark any backface surfaces as not visible
1460                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1461                         {
1462                                 if (!(surf->flags & SURF_PLANEBACK))
1463                                         surfacevisframes[i] = -1;
1464                         }
1465                         else
1466                         {
1467                                 if ((surf->flags & SURF_PLANEBACK))
1468                                         surfacevisframes[i] = -1;
1469                         }
1470                         if (surfacevisframes[i] == r_framecount)
1471 #endif
1472                         {
1473                                 c_faces++;
1474                                 surf->visframe = r_framecount;
1475                                 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1476                                         R_BuildLightMap(ent, surf);
1477                         }
1478                 }
1479         }
1480 }
1481
1482 void R_DrawSurfaces(entity_render_t *ent, int flagsmask)
1483 {
1484         int i, f;
1485         texture_t *t;
1486         model_t *model = ent->model;
1487         if (model == NULL)
1488                 return;
1489         R_Mesh_Matrix(&ent->matrix);
1490         for (i = 0, t = model->brushq1.textures;i < model->brushq1.numtextures;i++, t++)
1491         {
1492                 f = t->flags & flagsmask;
1493                 if (f && t->currentframe && model->brushq1.pvstexturechains[i] != NULL)
1494                         R_DrawSurfaceChain(ent, t->currentframe, model->brushq1.pvstexturechains[i]);
1495         }
1496 }
1497
1498 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1499 {
1500         int i;
1501         float *v;
1502         rmeshstate_t m;
1503         const entity_render_t *ent = calldata1;
1504         const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1505         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1506         GL_DepthMask(false);
1507         GL_DepthTest(true);
1508         R_Mesh_Matrix(&ent->matrix);
1509
1510         memset(&m, 0, sizeof(m));
1511         m.pointer_vertex = varray_vertex3f;
1512         R_Mesh_State(&m);
1513
1514         i = portal - ent->model->brushq1.portals;
1515         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1516                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1517                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1518                          0.125f);
1519         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1520         {
1521                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1522                         VectorCopy(portal->points[i].position, v);
1523         }
1524         else
1525                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1526                         VectorCopy(portal->points[i].position, v);
1527         GL_LockArrays(0, portal->numpoints);
1528         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1529         GL_LockArrays(0, 0);
1530 }
1531
1532 // LordHavoc: this is just a nice debugging tool, very slow
1533 static void R_DrawPortals(entity_render_t *ent)
1534 {
1535         int i;
1536         mportal_t *portal, *endportal;
1537         float temp[3], center[3], f;
1538         if (ent->model == NULL)
1539                 return;
1540         for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1541         {
1542                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1543                 {
1544                         VectorClear(temp);
1545                         for (i = 0;i < portal->numpoints;i++)
1546                                 VectorAdd(temp, portal->points[i].position, temp);
1547                         f = ixtable[portal->numpoints];
1548                         VectorScale(temp, f, temp);
1549                         Matrix4x4_Transform(&ent->matrix, temp, center);
1550                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1551                 }
1552         }
1553 }
1554
1555 void R_PrepareBrushModel(entity_render_t *ent)
1556 {
1557         int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1558         msurface_t *surf;
1559         model_t *model;
1560 #if WORLDNODECULLBACKFACES
1561         vec3_t modelorg;
1562 #endif
1563
1564         // because bmodels can be reused, we have to decide which things to render
1565         // from scratch every time
1566         model = ent->model;
1567         if (model == NULL)
1568                 return;
1569 #if WORLDNODECULLBACKFACES
1570         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1571 #endif
1572         numsurfaces = model->nummodelsurfaces;
1573         surf = model->brushq1.surfaces + model->firstmodelsurface;
1574         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1575         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1576         for (i = 0;i < numsurfaces;i++, surf++)
1577         {
1578 #if WORLDNODECULLBACKFACES
1579                 // mark any backface surfaces as not visible
1580                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1581                 {
1582                         if ((surf->flags & SURF_PLANEBACK))
1583                                 surfacevisframes[i] = r_framecount;
1584                 }
1585                 else if (!(surf->flags & SURF_PLANEBACK))
1586                         surfacevisframes[i] = r_framecount;
1587 #else
1588                 surfacevisframes[i] = r_framecount;
1589 #endif
1590                 surf->dlightframe = -1;
1591         }
1592         R_UpdateTextureInfo(ent);
1593         R_UpdateLightmapInfo(ent);
1594 }
1595
1596 void R_SurfaceWorldNode (entity_render_t *ent)
1597 {
1598         int i, *surfacevisframes, *surfacepvsframes, surfnum;
1599         msurface_t *surf;
1600         mleaf_t *leaf;
1601         model_t *model;
1602         vec3_t modelorg;
1603
1604         // equivilant to quake's RecursiveWorldNode but faster and more effective
1605         model = ent->model;
1606         if (model == NULL)
1607                 return;
1608         surfacevisframes = model->brushq1.surfacevisframes + model->firstmodelsurface;
1609         surfacepvsframes = model->brushq1.surfacepvsframes + model->firstmodelsurface;
1610         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1611
1612         for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1613         {
1614                 if (!R_CullBox (leaf->mins, leaf->maxs))
1615                 {
1616                         c_leafs++;
1617                         leaf->visframe = r_framecount;
1618                 }
1619         }
1620
1621         for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1622         {
1623                 surfnum = model->brushq1.pvssurflist[i];
1624                 surf = model->brushq1.surfaces + surfnum;
1625 #if WORLDNODECULLBACKFACES
1626                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1627                 {
1628                         if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1629                                 surfacevisframes[surfnum] = r_framecount;
1630                 }
1631                 else
1632                 {
1633                         if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1634                                 surfacevisframes[surfnum] = r_framecount;
1635                 }
1636 #else
1637                 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1638                         surfacevisframes[surfnum] = r_framecount;
1639 #endif
1640         }
1641 }
1642
1643 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1644 {
1645         int c, leafstackpos, *mark, *surfacevisframes;
1646 #if WORLDNODECULLBACKFACES
1647         int n;
1648         msurface_t *surf;
1649 #endif
1650         mleaf_t *leaf, *leafstack[8192];
1651         mportal_t *p;
1652         vec3_t modelorg;
1653         msurface_t *surfaces;
1654         if (ent->model == NULL)
1655                 return;
1656         // LordHavoc: portal-passage worldnode with PVS;
1657         // follows portals leading outward from viewleaf, does not venture
1658         // offscreen or into leafs that are not visible, faster than Quake's
1659         // RecursiveWorldNode
1660         surfaces = ent->model->brushq1.surfaces;
1661         surfacevisframes = ent->model->brushq1.surfacevisframes;
1662         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1663         viewleaf->worldnodeframe = r_framecount;
1664         leafstack[0] = viewleaf;
1665         leafstackpos = 1;
1666         while (leafstackpos)
1667         {
1668                 c_leafs++;
1669                 leaf = leafstack[--leafstackpos];
1670                 leaf->visframe = r_framecount;
1671                 // draw any surfaces bounding this leaf
1672                 if (leaf->nummarksurfaces)
1673                 {
1674                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1675                         {
1676 #if WORLDNODECULLBACKFACES
1677                                 n = *mark++;
1678                                 if (surfacevisframes[n] != r_framecount)
1679                                 {
1680                                         surf = surfaces + n;
1681                                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1682                                         {
1683                                                 if ((surf->flags & SURF_PLANEBACK))
1684                                                         surfacevisframes[n] = r_framecount;
1685                                         }
1686                                         else
1687                                         {
1688                                                 if (!(surf->flags & SURF_PLANEBACK))
1689                                                         surfacevisframes[n] = r_framecount;
1690                                         }
1691                                 }
1692 #else
1693                                 surfacevisframes[*mark++] = r_framecount;
1694 #endif
1695                         }
1696                 }
1697                 // follow portals into other leafs
1698                 for (p = leaf->portals;p;p = p->next)
1699                 {
1700                         // LordHavoc: this DotProduct hurts less than a cache miss
1701                         // (which is more likely to happen if backflowing through leafs)
1702                         if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1703                         {
1704                                 leaf = p->past;
1705                                 if (leaf->worldnodeframe != r_framecount)
1706                                 {
1707                                         leaf->worldnodeframe = r_framecount;
1708                                         // FIXME: R_CullBox is absolute, should be done relative
1709                                         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex) && !R_CullBox(leaf->mins, leaf->maxs))
1710                                                 leafstack[leafstackpos++] = leaf;
1711                                 }
1712                         }
1713                 }
1714         }
1715 }
1716
1717 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1718 {
1719         int j, c, *surfacepvsframes, *mark;
1720         mleaf_t *leaf;
1721         model_t *model;
1722
1723         model = ent->model;
1724         if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1725         {
1726                 model->brushq1.pvsframecount++;
1727                 model->brushq1.pvsviewleaf = viewleaf;
1728                 model->brushq1.pvsviewleafnovis = r_novis.integer;
1729                 model->brushq1.pvsleafchain = NULL;
1730                 model->brushq1.pvssurflistlength = 0;
1731                 if (viewleaf)
1732                 {
1733                         surfacepvsframes = model->brushq1.surfacepvsframes;
1734                         for (j = 0, leaf = model->brushq1.data_leafs;j < model->brushq1.num_leafs;j++, leaf++)
1735                         {
1736                                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
1737                                 {
1738                                         leaf->pvsframe = model->brushq1.pvsframecount;
1739                                         leaf->pvschain = model->brushq1.pvsleafchain;
1740                                         model->brushq1.pvsleafchain = leaf;
1741                                         // mark surfaces bounding this leaf as visible
1742                                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1743                                                 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1744                                 }
1745                         }
1746                         model->brushq1.BuildPVSTextureChains(model);
1747                 }
1748         }
1749 }
1750
1751 void R_WorldVisibility(entity_render_t *ent)
1752 {
1753         vec3_t modelorg;
1754         mleaf_t *viewleaf;
1755
1756         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1757         viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1758         R_PVSUpdate(ent, viewleaf);
1759
1760         if (!viewleaf)
1761                 return;
1762
1763         if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1764                 R_SurfaceWorldNode (ent);
1765         else
1766                 R_PortalWorldNode (ent, viewleaf);
1767
1768         if (r_drawportals.integer)
1769                 R_DrawPortals(ent);
1770 }
1771
1772 void R_Model_Brush_DrawSky(entity_render_t *ent)
1773 {
1774         if (ent->model == NULL)
1775                 return;
1776         if (ent != &cl_entities[0].render)
1777                 R_PrepareBrushModel(ent);
1778         R_PrepareSurfaces(ent);
1779         R_DrawSurfaces(ent, SURF_DRAWSKY);
1780 }
1781
1782 void R_Model_Brush_Draw(entity_render_t *ent)
1783 {
1784         if (ent->model == NULL)
1785                 return;
1786         c_bmodels++;
1787         if (ent != &cl_entities[0].render)
1788                 R_PrepareBrushModel(ent);
1789         R_PrepareSurfaces(ent);
1790         R_UpdateTextureInfo(ent);
1791         R_UpdateLightmapInfo(ent);
1792         R_DrawSurfaces(ent, SURF_DRAWTURB | SURF_LIGHTMAP);
1793 }
1794
1795 void R_Model_Brush_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)
1796 {
1797         model_t *model = ent->model;
1798         vec3_t lightmins, lightmaxs;
1799         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
1800         const int *e;
1801         const float *v[3];
1802         msurface_t *surface;
1803         mleaf_t *leaf;
1804         const qbyte *pvs;
1805         lightmins[0] = relativelightorigin[0] - lightradius;
1806         lightmins[1] = relativelightorigin[1] - lightradius;
1807         lightmins[2] = relativelightorigin[2] - lightradius;
1808         lightmaxs[0] = relativelightorigin[0] + lightradius;
1809         lightmaxs[1] = relativelightorigin[1] + lightradius;
1810         lightmaxs[2] = relativelightorigin[2] + lightradius;
1811         *outnumclusterspointer = 0;
1812         *outnumsurfacespointer = 0;
1813         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
1814         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
1815         if (model == NULL)
1816         {
1817                 VectorCopy(lightmins, outmins);
1818                 VectorCopy(lightmaxs, outmaxs);
1819                 return;
1820         }
1821         VectorCopy(relativelightorigin, outmins);
1822         VectorCopy(relativelightorigin, outmaxs);
1823         if (model->brush.GetPVS)
1824                 pvs = model->brush.GetPVS(model, relativelightorigin);
1825         else
1826                 pvs = NULL;
1827         // FIXME: use BSP recursion as lights are often small
1828         for (leafindex = 0, leaf = model->brushq1.data_leafs;leafindex < model->brushq1.num_leafs;leafindex++, leaf++)
1829         {
1830                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
1831                 {
1832                         outmins[0] = min(outmins[0], leaf->mins[0]);
1833                         outmins[1] = min(outmins[1], leaf->mins[1]);
1834                         outmins[2] = min(outmins[2], leaf->mins[2]);
1835                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
1836                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
1837                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
1838                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
1839                         {
1840                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
1841                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
1842                         }
1843                         for (marksurfaceindex = 0;marksurfaceindex < leaf->nummarksurfaces;marksurfaceindex++)
1844                         {
1845                                 surfaceindex = leaf->firstmarksurface[marksurfaceindex];
1846                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
1847                                 {
1848                                         surface = model->brushq1.surfaces + surfaceindex;
1849                                         if (BoxesOverlap(lightmins, lightmaxs, surface->poly_mins, surface->poly_maxs) && (surface->flags & SURF_LIGHTMAP) && !surface->texinfo->texture->skin.fog)
1850                                         {
1851                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->mesh.num_triangles;triangleindex++, t++, e += 3)
1852                                                 {
1853                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1854                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1855                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1856                                                         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])))
1857                                                         {
1858                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
1859                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
1860                                                                 break;
1861                                                         }
1862                                                 }
1863                                         }
1864                                 }
1865                         }
1866                 }
1867         }
1868
1869         // limit combined leaf box to light boundaries
1870         outmins[0] = max(outmins[0], lightmins[0]);
1871         outmins[1] = max(outmins[1], lightmins[1]);
1872         outmins[2] = max(outmins[2], lightmins[2]);
1873         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
1874         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
1875         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
1876
1877         *outnumclusterspointer = outnumclusters;
1878         *outnumsurfacespointer = outnumsurfaces;
1879 }
1880
1881 void R_Model_Brush_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
1882 {
1883         model_t *model = ent->model;
1884         vec3_t lightmins, lightmaxs;
1885         msurface_t *surface;
1886         int surfacelistindex, j, t;
1887         const int *e;
1888         const float *v[3];
1889         if (r_drawcollisionbrushes.integer < 2)
1890         {
1891                 lightmins[0] = relativelightorigin[0] - lightradius;
1892                 lightmins[1] = relativelightorigin[1] - lightradius;
1893                 lightmins[2] = relativelightorigin[2] - lightradius;
1894                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1895                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1896                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1897                 R_Mesh_Matrix(&ent->matrix);
1898                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
1899                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1900                 {
1901                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1902                         for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->mesh.num_triangles;j++, t++, e += 3)
1903                         {
1904                                 v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
1905                                 v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
1906                                 v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
1907                                 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])))
1908                                         shadowmarklist[numshadowmark++] = t;
1909                         }
1910                 }
1911                 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);
1912         }
1913 }
1914
1915 void R_Model_Brush_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)
1916 {
1917         model_t *model = ent->model;
1918         vec3_t lightmins, lightmaxs, modelorg;
1919         msurface_t *surface;
1920         texture_t *t;
1921         int surfacelistindex;
1922         if (r_drawcollisionbrushes.integer < 2)
1923         {
1924                 lightmins[0] = relativelightorigin[0] - lightradius;
1925                 lightmins[1] = relativelightorigin[1] - lightradius;
1926                 lightmins[2] = relativelightorigin[2] - lightradius;
1927                 lightmaxs[0] = relativelightorigin[0] + lightradius;
1928                 lightmaxs[1] = relativelightorigin[1] + lightradius;
1929                 lightmaxs[2] = relativelightorigin[2] + lightradius;
1930                 R_Mesh_Matrix(&ent->matrix);
1931                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1932                 R_UpdateTextureInfo(ent);
1933                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
1934                 {
1935                         surface = model->brushq1.surfaces + surfacelist[surfacelistindex];
1936                         if (r_shadow_compilingrtlight)
1937                         {
1938                                 // if compiling an rtlight, capture the mesh
1939                                 t = surface->texinfo->texture;
1940                                 if (t->flags & SURF_LIGHTMAP && t->skin.fog == NULL)
1941                                         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);
1942                         }
1943                         else if (ent != &cl_entities[0].render || surface->visframe == r_framecount)
1944                         {
1945                                 t = surface->texinfo->texture->currentframe;
1946                                 if (t->flags & SURF_LIGHTMAP && t->rendertype == SURFRENDER_OPAQUE)
1947                                         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);
1948                         }
1949                 }
1950         }
1951 }
1952
1953 void R_DrawCollisionBrush(colbrushf_t *brush)
1954 {
1955         int i;
1956         rmeshstate_t m;
1957         memset(&m, 0, sizeof(m));
1958         m.pointer_vertex = brush->points->v;
1959         R_Mesh_State(&m);
1960         i = ((int)brush) / sizeof(colbrushf_t);
1961         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1962         GL_LockArrays(0, brush->numpoints);
1963         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1964         GL_LockArrays(0, 0);
1965 }
1966
1967 void R_Q3BSP_DrawCollisionFace(entity_render_t *ent, q3msurface_t *face)
1968 {
1969         int i;
1970         rmeshstate_t m;
1971         if (!face->num_collisiontriangles)
1972                 return;
1973         memset(&m, 0, sizeof(m));
1974         m.pointer_vertex = face->data_collisionvertex3f;
1975         R_Mesh_State(&m);
1976         i = ((int)face) / sizeof(q3msurface_t);
1977         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1978         GL_LockArrays(0, face->num_collisionvertices);
1979         R_Mesh_Draw(face->num_collisionvertices, face->num_collisiontriangles, face->data_collisionelement3i);
1980         GL_LockArrays(0, 0);
1981 }
1982
1983 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3msurface_t *face)
1984 {
1985         rmeshstate_t m;
1986         if (!face->num_triangles)
1987                 return;
1988         c_faces++;
1989         if (skyrendernow)
1990         {
1991                 skyrendernow = false;
1992                 if (skyrendermasked)
1993                         R_Sky();
1994         }
1995
1996         R_Mesh_Matrix(&ent->matrix);
1997
1998         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1999         if (skyrendermasked)
2000         {
2001                 // depth-only (masking)
2002                 GL_ColorMask(0,0,0,0);
2003                 // just to make sure that braindead drivers don't draw anything
2004                 // despite that colormask...
2005                 GL_BlendFunc(GL_ZERO, GL_ONE);
2006         }
2007         else
2008         {
2009                 // fog sky
2010                 GL_BlendFunc(GL_ONE, GL_ZERO);
2011         }
2012         GL_DepthMask(true);
2013         GL_DepthTest(true);
2014
2015         memset(&m, 0, sizeof(m));
2016         m.pointer_vertex = face->data_vertex3f;
2017         R_Mesh_State(&m);
2018
2019         GL_LockArrays(0, face->num_vertices);
2020         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2021         GL_LockArrays(0, 0);
2022         GL_ColorMask(1,1,1,1);
2023 }
2024
2025 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3msurface_t *face)
2026 {
2027         rmeshstate_t m;
2028         memset(&m, 0, sizeof(m));
2029         GL_BlendFunc(GL_ONE, GL_ZERO);
2030         GL_DepthMask(true);
2031         GL_DepthTest(true);
2032         if (face->texture->skin.glow)
2033         {
2034                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2035                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2036                 GL_Color(1, 1, 1, 1);
2037         }
2038         else
2039                 GL_Color(0, 0, 0, 1);
2040         m.pointer_vertex = face->data_vertex3f;
2041         R_Mesh_State(&m);
2042         GL_LockArrays(0, face->num_vertices);
2043         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2044         GL_LockArrays(0, 0);
2045 }
2046
2047 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3msurface_t *face)
2048 {
2049         rmeshstate_t m;
2050         memset(&m, 0, sizeof(m));
2051         GL_BlendFunc(GL_ONE, GL_ZERO);
2052         GL_DepthMask(true);
2053         GL_DepthTest(true);
2054         m.tex[0] = R_GetTexture(face->texture->skin.base);
2055         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2056         m.tex[1] = R_GetTexture(face->lightmaptexture);
2057         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2058         m.texrgbscale[1] = 2;
2059         GL_Color(1, 1, 1, 1);
2060         m.pointer_vertex = face->data_vertex3f;
2061         R_Mesh_State(&m);
2062         GL_LockArrays(0, face->num_vertices);
2063         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2064         GL_LockArrays(0, 0);
2065 }
2066
2067 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3msurface_t *face)
2068 {
2069         rmeshstate_t m;
2070         memset(&m, 0, sizeof(m));
2071         GL_BlendFunc(GL_ONE, GL_ZERO);
2072         GL_DepthMask(true);
2073         GL_DepthTest(true);
2074         m.tex[0] = R_GetTexture(face->texture->skin.base);
2075         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2076         GL_Color(1, 1, 1, 1);
2077         m.pointer_vertex = face->data_vertex3f;
2078         R_Mesh_State(&m);
2079         GL_LockArrays(0, face->num_vertices);
2080         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2081         GL_LockArrays(0, 0);
2082 }
2083
2084 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3msurface_t *face)
2085 {
2086         rmeshstate_t m;
2087         memset(&m, 0, sizeof(m));
2088         GL_BlendFunc(GL_DST_COLOR, GL_SRC_COLOR);
2089         GL_DepthMask(false);
2090         GL_DepthTest(true);
2091         m.tex[0] = R_GetTexture(face->lightmaptexture);
2092         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2093         GL_Color(1, 1, 1, 1);
2094         m.pointer_vertex = face->data_vertex3f;
2095         R_Mesh_State(&m);
2096         GL_LockArrays(0, face->num_vertices);
2097         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2098         GL_LockArrays(0, 0);
2099 }
2100
2101 void R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(entity_render_t *ent, q3msurface_t *face)
2102 {
2103         rmeshstate_t m;
2104         memset(&m, 0, sizeof(m));
2105         GL_BlendFunc(GL_ONE, GL_ZERO);
2106         GL_DepthMask(true);
2107         GL_DepthTest(true);
2108         m.tex[0] = R_GetTexture(face->lightmaptexture);
2109         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
2110         if (r_shadow_realtime_world.integer)
2111                 GL_Color(r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, r_shadow_realtime_world_lightmaps.value, 1);
2112         else
2113                 GL_Color(1, 1, 1, 1);
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_Glow(entity_render_t *ent, q3msurface_t *face)
2122 {
2123         rmeshstate_t m;
2124         memset(&m, 0, sizeof(m));
2125         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2126         GL_DepthMask(false);
2127         GL_DepthTest(true);
2128         if (face->texture->skin.glow)
2129         {
2130                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
2131                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2132                 GL_Color(1, 1, 1, 1);
2133         }
2134         else
2135                 GL_Color(0, 0, 0, 1);
2136         m.pointer_vertex = face->data_vertex3f;
2137         R_Mesh_State(&m);
2138         GL_LockArrays(0, face->num_vertices);
2139         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2140         GL_LockArrays(0, 0);
2141 }
2142
2143 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3msurface_t *face)
2144 {
2145         int i;
2146         float mul;
2147         rmeshstate_t m;
2148         memset(&m, 0, sizeof(m));
2149         GL_BlendFunc(GL_ONE, GL_ZERO);
2150         GL_DepthMask(true);
2151         GL_DepthTest(true);
2152         m.tex[0] = R_GetTexture(face->texture->skin.base);
2153         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2154         mul = 2.0f;
2155         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2156                 mul *= r_shadow_realtime_world_lightmaps.value;
2157         if (mul == 2 && gl_combine.integer)
2158         {
2159                 m.texrgbscale[0] = 2;
2160                 m.pointer_color = face->data_color4f;
2161         }
2162         else if (mul == 1)
2163                 m.pointer_color = face->data_color4f;
2164         else
2165         {
2166                 for (i = 0;i < face->num_vertices;i++)
2167                 {
2168                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * mul;
2169                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * mul;
2170                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * mul;
2171                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2172                 }
2173                 m.pointer_color = varray_color4f;
2174         }
2175         m.pointer_vertex = face->data_vertex3f;
2176         R_Mesh_State(&m);
2177         GL_LockArrays(0, face->num_vertices);
2178         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2179         GL_LockArrays(0, 0);
2180 }
2181
2182 void R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(entity_render_t *ent, q3msurface_t *face)
2183 {
2184         int i;
2185         float mul;
2186         rmeshstate_t m;
2187         memset(&m, 0, sizeof(m));
2188         GL_BlendFunc(GL_ONE, GL_ZERO);
2189         GL_DepthMask(true);
2190         GL_DepthTest(true);
2191         mul = 2.0f;
2192         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value != 1)
2193                 mul *= r_shadow_realtime_world_lightmaps.value;
2194         if (mul == 1)
2195                 m.pointer_color = face->data_color4f;
2196         else
2197         {
2198                 for (i = 0;i < face->num_vertices;i++)
2199                 {
2200                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2201                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2202                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2203                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
2204                 }
2205                 m.pointer_color = varray_color4f;
2206         }
2207         m.pointer_vertex = face->data_vertex3f;
2208         R_Mesh_State(&m);
2209         GL_LockArrays(0, face->num_vertices);
2210         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2211         GL_LockArrays(0, 0);
2212 }
2213
2214 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3msurface_t *face)
2215 {
2216         rmeshstate_t m;
2217         memset(&m, 0, sizeof(m));
2218         GL_BlendFunc(GL_ONE, GL_ONE);
2219         GL_DepthMask(true);
2220         GL_DepthTest(true);
2221         m.tex[0] = R_GetTexture(face->texture->skin.base);
2222         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2223         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
2224         m.pointer_vertex = face->data_vertex3f;
2225         R_Mesh_State(&m);
2226         GL_LockArrays(0, face->num_vertices);
2227         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2228         GL_LockArrays(0, 0);
2229 }
2230
2231 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
2232 {
2233         const entity_render_t *ent = voident;
2234         q3msurface_t *face = ent->model->brushq3.data_faces + facenumber;
2235         rmeshstate_t m;
2236         R_Mesh_Matrix(&ent->matrix);
2237         memset(&m, 0, sizeof(m));
2238         if (ent->effects & EF_ADDITIVE)
2239                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2240         else
2241                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2242         GL_DepthMask(false);
2243         GL_DepthTest(true);
2244         m.tex[0] = R_GetTexture(face->texture->skin.base);
2245         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
2246         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
2247         if (gl_combine.integer)
2248         {
2249                 m.texrgbscale[0] = 2;
2250                 if (r_textureunits.integer >= 2)
2251                 {
2252                         m.tex[1] = R_GetTexture(face->lightmaptexture);
2253                         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
2254                         GL_Color(1, 1, 1, ent->alpha);
2255                 }
2256                 else
2257                 {
2258                         if (ent->alpha == 1)
2259                                 m.pointer_color = face->data_color4f;
2260                         else
2261                         {
2262                                 int i;
2263                                 for (i = 0;i < face->num_vertices;i++)
2264                                 {
2265                                         varray_color4f[i*4+0] = face->data_color4f[i*4+0];
2266                                         varray_color4f[i*4+1] = face->data_color4f[i*4+1];
2267                                         varray_color4f[i*4+2] = face->data_color4f[i*4+2];
2268                                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2269                                 }
2270                                 m.pointer_color = varray_color4f;
2271                         }
2272                 }
2273         }
2274         else
2275         {
2276                 int i;
2277                 for (i = 0;i < face->num_vertices;i++)
2278                 {
2279                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2280                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2281                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2282                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2283                 }
2284                 m.pointer_color = varray_color4f;
2285         }
2286         m.pointer_vertex = face->data_vertex3f;
2287         R_Mesh_State(&m);
2288         qglDisable(GL_CULL_FACE);
2289         GL_LockArrays(0, face->num_vertices);
2290         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2291         GL_LockArrays(0, 0);
2292         qglEnable(GL_CULL_FACE);
2293 }
2294
2295 void R_Q3BSP_DrawFace(entity_render_t *ent, q3msurface_t *face)
2296 {
2297         if (!face->num_triangles)
2298                 return;
2299         if (face->texture->surfaceflags && (face->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2300                 return;
2301         c_faces++;
2302         if ((face->texture->surfaceparms & Q3SURFACEPARM_TRANS) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2303         {
2304                 vec3_t facecenter, center;
2305                 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2306                 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2307                 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2308                 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2309                 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2310                 return;
2311         }
2312         R_Mesh_Matrix(&ent->matrix);
2313         if (r_shadow_realtime_world.integer && r_shadow_realtime_world_lightmaps.value <= 0)
2314                 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2315         else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2316         {
2317                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2318                 if (face->texture->skin.glow)
2319                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2320         }
2321         else if (face->lightmaptexture)
2322         {
2323                 if (gl_lightmaps.integer)
2324                         R_Q3BSP_DrawFace_OpaqueWall_Pass_LightmapOnly(ent, face);
2325                 else
2326                 {
2327                         if (r_textureunits.integer >= 2 && gl_combine.integer)
2328                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2329                         else
2330                         {
2331                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2332                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2333                         }
2334                         if (face->texture->skin.glow)
2335                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2336                 }
2337         }
2338         else
2339         {
2340                 if (gl_lightmaps.integer)
2341                         R_Q3BSP_DrawFace_OpaqueWall_Pass_VertexOnly(ent, face);
2342                 else
2343                 {
2344                         R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2345                         if (face->texture->skin.glow)
2346                                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2347                 }
2348         }
2349         if (r_ambient.value)
2350                 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2351 }
2352
2353 void R_Q3BSP_RecursiveWorldNode(q3mnode_t *node)
2354 {
2355         int i;
2356         q3mleaf_t *leaf;
2357         for (;;)
2358         {
2359                 if (R_CullBox(node->mins, node->maxs))
2360                         return;
2361                 if (!node->plane)
2362                         break;
2363                 c_nodes++;
2364                 R_Q3BSP_RecursiveWorldNode(node->children[0]);
2365                 node = node->children[1];
2366         }
2367         leaf = (q3mleaf_t *)node;
2368         if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2369         {
2370                 c_leafs++;
2371                 for (i = 0;i < leaf->numleaffaces;i++)
2372                         leaf->firstleafface[i]->visframe = r_framecount;
2373         }
2374 }
2375
2376 // FIXME: num_leafs needs to be recalculated at load time to include only
2377 // node-referenced leafs, as some maps are incorrectly compiled with leafs for
2378 // the submodels (which would render the submodels occasionally, as part of
2379 // the world - not good)
2380 void R_Q3BSP_MarkLeafPVS(void)
2381 {
2382         int i, j;
2383         q3mleaf_t *leaf;
2384         for (j = 0, leaf = cl.worldmodel->brushq3.data_leafs;j < cl.worldmodel->brushq3.num_leafs;j++, leaf++)
2385         {
2386                 if (CHECKPVSBIT(r_pvsbits, leaf->clusterindex))
2387                 {
2388                         c_leafs++;
2389                         for (i = 0;i < leaf->numleaffaces;i++)
2390                                 leaf->firstleafface[i]->visframe = r_framecount;
2391                 }
2392         }
2393 }
2394
2395 static int r_q3bsp_framecount = -1;
2396
2397 void R_Q3BSP_DrawSky(entity_render_t *ent)
2398 {
2399         int i;
2400         q3msurface_t *face;
2401         vec3_t modelorg;
2402         model_t *model;
2403         R_Mesh_Matrix(&ent->matrix);
2404         model = ent->model;
2405         if (r_drawcollisionbrushes.integer < 2)
2406         {
2407                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2408                 if (ent == &cl_entities[0].render)
2409                 {
2410                         if (r_q3bsp_framecount != r_framecount)
2411                         {
2412                                 r_q3bsp_framecount = r_framecount;
2413                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2414                                 //R_Q3BSP_MarkLeafPVS();
2415                         }
2416                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2417                                 if (face->visframe == r_framecount && (face->texture->surfaceflags & Q3SURFACEFLAG_SKY) && !R_CullBox(face->mins, face->maxs))
2418                                         R_Q3BSP_DrawSkyFace(ent, face);
2419                 }
2420                 else
2421                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2422                                 if ((face->texture->surfaceflags & Q3SURFACEFLAG_SKY))
2423                                         R_Q3BSP_DrawSkyFace(ent, face);
2424         }
2425 }
2426
2427 void R_Q3BSP_Draw(entity_render_t *ent)
2428 {
2429         int i;
2430         q3msurface_t *face;
2431         vec3_t modelorg;
2432         model_t *model;
2433         qbyte *pvs;
2434         R_Mesh_Matrix(&ent->matrix);
2435         model = ent->model;
2436         if (r_drawcollisionbrushes.integer < 2)
2437         {
2438                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2439                 if (ent == &cl_entities[0].render)
2440                 {
2441                         if (model->brush.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2442                         if (r_q3bsp_framecount != r_framecount)
2443                         {
2444                                 r_q3bsp_framecount = r_framecount;
2445                                 R_Q3BSP_RecursiveWorldNode(model->brushq3.data_nodes);
2446                                 //R_Q3BSP_MarkLeafPVS();
2447                         }
2448                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2449                                 if (face->visframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2450                                         R_Q3BSP_DrawFace(ent, face);
2451                 }
2452                 else
2453                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2454                                 R_Q3BSP_DrawFace(ent, face);
2455         }
2456         if (r_drawcollisionbrushes.integer >= 1)
2457         {
2458                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2459                 GL_DepthMask(false);
2460                 GL_DepthTest(true);
2461                 qglPolygonOffset(r_drawcollisionbrushes_polygonfactor.value, r_drawcollisionbrushes_polygonoffset.value);
2462                 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2463                         if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2464                                 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2465                 for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2466                         if (face->num_collisiontriangles)
2467                                 R_Q3BSP_DrawCollisionFace(ent, face);
2468                 qglPolygonOffset(0, 0);
2469         }
2470 }
2471
2472 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)
2473 {
2474         model_t *model = ent->model;
2475         vec3_t lightmins, lightmaxs;
2476         int t, leafindex, marksurfaceindex, surfaceindex, triangleindex, outnumclusters = 0, outnumsurfaces = 0;
2477         const int *e;
2478         const float *v[3];
2479         q3msurface_t *surface;
2480         q3mleaf_t *leaf;
2481         const qbyte *pvs;
2482         lightmins[0] = relativelightorigin[0] - lightradius;
2483         lightmins[1] = relativelightorigin[1] - lightradius;
2484         lightmins[2] = relativelightorigin[2] - lightradius;
2485         lightmaxs[0] = relativelightorigin[0] + lightradius;
2486         lightmaxs[1] = relativelightorigin[1] + lightradius;
2487         lightmaxs[2] = relativelightorigin[2] + lightradius;
2488         *outnumclusterspointer = 0;
2489         *outnumsurfacespointer = 0;
2490         memset(outclusterpvs, 0, model->brush.num_pvsclusterbytes);
2491         memset(outsurfacepvs, 0, (model->nummodelsurfaces + 7) >> 3);
2492         if (model == NULL)
2493         {
2494                 VectorCopy(lightmins, outmins);
2495                 VectorCopy(lightmaxs, outmaxs);
2496                 return;
2497         }
2498         VectorCopy(relativelightorigin, outmins);
2499         VectorCopy(relativelightorigin, outmaxs);
2500         if (model->brush.GetPVS)
2501                 pvs = model->brush.GetPVS(model, relativelightorigin);
2502         else
2503                 pvs = NULL;
2504         // FIXME: use BSP recursion as lights are often small
2505         for (leafindex = 0, leaf = model->brushq3.data_leafs;leafindex < model->brushq3.num_leafs;leafindex++, leaf++)
2506         {
2507                 if (BoxesOverlap(lightmins, lightmaxs, leaf->mins, leaf->maxs) && (pvs == NULL || CHECKPVSBIT(pvs, leaf->clusterindex)))
2508                 {
2509                         outmins[0] = min(outmins[0], leaf->mins[0]);
2510                         outmins[1] = min(outmins[1], leaf->mins[1]);
2511                         outmins[2] = min(outmins[2], leaf->mins[2]);
2512                         outmaxs[0] = max(outmaxs[0], leaf->maxs[0]);
2513                         outmaxs[1] = max(outmaxs[1], leaf->maxs[1]);
2514                         outmaxs[2] = max(outmaxs[2], leaf->maxs[2]);
2515                         if (!CHECKPVSBIT(outclusterpvs, leaf->clusterindex))
2516                         {
2517                                 SETPVSBIT(outclusterpvs, leaf->clusterindex);
2518                                 outclusterlist[outnumclusters++] = leaf->clusterindex;
2519                         }
2520                         for (marksurfaceindex = 0;marksurfaceindex < leaf->numleaffaces;marksurfaceindex++)
2521                         {
2522                                 surface = leaf->firstleafface[marksurfaceindex];
2523                                 surfaceindex = surface - model->brushq3.data_faces;
2524                                 if (!CHECKPVSBIT(outsurfacepvs, surfaceindex))
2525                                 {
2526                                         if (BoxesOverlap(lightmins, lightmaxs, surface->mins, surface->maxs) && !(surface->texture->surfaceparms & Q3SURFACEPARM_TRANS) && !(surface->texture->surfaceflags & (Q3SURFACEFLAG_SKY | Q3SURFACEFLAG_NODRAW)))
2527                                         {
2528                                                 for (triangleindex = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;triangleindex < surface->num_triangles;triangleindex++, t++, e += 3)
2529                                                 {
2530                                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2531                                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2532                                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2533                                                         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])))
2534                                                         {
2535                                                                 SETPVSBIT(outsurfacepvs, surfaceindex);
2536                                                                 outsurfacelist[outnumsurfaces++] = surfaceindex;
2537                                                                 break;
2538                                                         }
2539                                                 }
2540                                         }
2541                                 }
2542                         }
2543                 }
2544         }
2545
2546         // limit combined leaf box to light boundaries
2547         outmins[0] = max(outmins[0], lightmins[0]);
2548         outmins[1] = max(outmins[1], lightmins[1]);
2549         outmins[2] = max(outmins[2], lightmins[2]);
2550         outmaxs[0] = min(outmaxs[0], lightmaxs[0]);
2551         outmaxs[1] = min(outmaxs[1], lightmaxs[1]);
2552         outmaxs[2] = min(outmaxs[2], lightmaxs[2]);
2553
2554         *outnumclusterspointer = outnumclusters;
2555         *outnumsurfacespointer = outnumsurfaces;
2556 }
2557
2558 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, int numsurfaces, const int *surfacelist)
2559 {
2560         model_t *model = ent->model;
2561         vec3_t lightmins, lightmaxs;
2562         q3msurface_t *surface;
2563         int surfacelistindex, j, t;
2564         const int *e;
2565         const float *v[3];
2566         if (r_drawcollisionbrushes.integer < 2)
2567         {
2568                 lightmins[0] = relativelightorigin[0] - lightradius;
2569                 lightmins[1] = relativelightorigin[1] - lightradius;
2570                 lightmins[2] = relativelightorigin[2] - lightradius;
2571                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2572                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2573                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2574                 R_Mesh_Matrix(&ent->matrix);
2575                 R_Shadow_PrepareShadowMark(model->brush.shadowmesh->numtriangles);
2576                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2577                 {
2578                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2579                         // FIXME: check some manner of face->rendermode here?
2580                         if (!(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles && !surface->texture->skin.fog)
2581                         {
2582                                 for (j = 0, t = surface->num_firstshadowmeshtriangle, e = model->brush.shadowmesh->element3i + t * 3;j < surface->num_triangles;j++, t++, e += 3)
2583                                 {
2584                                         v[0] = model->brush.shadowmesh->vertex3f + e[0] * 3;
2585                                         v[1] = model->brush.shadowmesh->vertex3f + e[1] * 3;
2586                                         v[2] = model->brush.shadowmesh->vertex3f + e[2] * 3;
2587                                         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])))
2588                                                 shadowmarklist[numshadowmark++] = t;
2589                                 }
2590                         }
2591                 }
2592                 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);
2593         }
2594 }
2595
2596 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)
2597 {
2598         model_t *model = ent->model;
2599         vec3_t lightmins, lightmaxs, modelorg;
2600         q3msurface_t *surface;
2601         int surfacelistindex;
2602         if (r_drawcollisionbrushes.integer < 2)
2603         {
2604                 lightmins[0] = relativelightorigin[0] - lightradius;
2605                 lightmins[1] = relativelightorigin[1] - lightradius;
2606                 lightmins[2] = relativelightorigin[2] - lightradius;
2607                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2608                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2609                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2610                 R_Mesh_Matrix(&ent->matrix);
2611                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2612                 for (surfacelistindex = 0;surfacelistindex < numsurfaces;surfacelistindex++)
2613                 {
2614                         surface = model->brushq3.data_faces + surfacelist[surfacelistindex];
2615                         if (r_shadow_compilingrtlight)
2616                         {
2617                                 // if compiling an rtlight, capture the mesh
2618                                 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);
2619                         }
2620                         else if ((ent != &cl_entities[0].render || surface->visframe == r_framecount) && !(surface->texture->surfaceflags & Q3SURFACEFLAG_NODRAW) && surface->num_triangles)
2621                                 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);
2622                 }
2623         }
2624 }
2625
2626 static void gl_surf_start(void)
2627 {
2628 }
2629
2630 static void gl_surf_shutdown(void)
2631 {
2632 }
2633
2634 static void gl_surf_newmap(void)
2635 {
2636 }
2637
2638 void GL_Surf_Init(void)
2639 {
2640         int i;
2641         dlightdivtable[0] = 4194304;
2642         for (i = 1;i < 32768;i++)
2643                 dlightdivtable[i] = 4194304 / (i << 7);
2644
2645         Cvar_RegisterVariable(&r_ambient);
2646         Cvar_RegisterVariable(&r_drawportals);
2647         Cvar_RegisterVariable(&r_testvis);
2648         Cvar_RegisterVariable(&r_floatbuildlightmap);
2649         Cvar_RegisterVariable(&r_detailtextures);
2650         Cvar_RegisterVariable(&r_surfaceworldnode);
2651         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonfactor);
2652         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2653         Cvar_RegisterVariable(&gl_lightmaps);
2654
2655         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2656 }
2657