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