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