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