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