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