]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
fix an input bug that prevented function keys from working inside console
[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->effects & EF_FULLBRIGHT) || !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->effects & EF_FULLBRIGHT) || !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 (r_shadow_realtime_world.integer)
1236         {
1237                 // opaque base lighting
1238                 RSurfShader_OpaqueWall_Pass_OpaqueGlow(ent, texture, surfchain);
1239                 if (fogenabled)
1240                         RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1241         }
1242         else
1243         {
1244                 // opaque lightmapped
1245                 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1246                 {
1247                         RSurfShader_OpaqueWall_Pass_BaseTripleTexCombine(ent, texture, surfchain);
1248                         if (texture->skin.glow)
1249                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1250                         if (fogenabled)
1251                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1252                 }
1253                 else if (r_textureunits.integer >= 2 && gl_combine.integer)
1254                 {
1255                         RSurfShader_OpaqueWall_Pass_BaseDoubleTexCombine(ent, texture, surfchain);
1256                         if (r_detailtextures.integer)
1257                                 RSurfShader_OpaqueWall_Pass_BaseDetail(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
1264                 {
1265                         RSurfShader_OpaqueWall_Pass_BaseTexture(ent, texture, surfchain);
1266                         RSurfShader_OpaqueWall_Pass_BaseLightmap(ent, texture, surfchain);
1267                         if (r_detailtextures.integer)
1268                                 RSurfShader_OpaqueWall_Pass_BaseDetail(ent, texture, surfchain);
1269                         if (texture->skin.glow)
1270                                 RSurfShader_OpaqueWall_Pass_Glow(ent, texture, surfchain);
1271                         if (fogenabled)
1272                                 RSurfShader_OpaqueWall_Pass_Fog(ent, texture, surfchain);
1273                 }
1274         }
1275 }
1276
1277 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, SHADERFLAGS_NEEDLIGHTMAP};
1278 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, 0};
1279 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, 0};
1280
1281 int Cshader_count = 3;
1282 Cshader_t *Cshaders[3] =
1283 {
1284         &Cshader_wall_lightmap,
1285         &Cshader_water,
1286         &Cshader_sky
1287 };
1288
1289 void R_UpdateTextureInfo(entity_render_t *ent)
1290 {
1291         int i, texframe, alttextures;
1292         texture_t *t;
1293
1294         if (!ent->model)
1295                 return;
1296
1297         alttextures = ent->frame != 0;
1298         texframe = (int)(cl.time * 5.0f);
1299         for (i = 0;i < ent->model->brushq1.numtextures;i++)
1300         {
1301                 t = ent->model->brushq1.textures + i;
1302                 t->currentalpha = ent->alpha;
1303                 if (t->flags & SURF_WATERALPHA)
1304                         t->currentalpha *= r_wateralpha.value;
1305                 if (ent->effects & EF_ADDITIVE)
1306                         t->rendertype = SURFRENDER_ADD;
1307                 else if (t->currentalpha < 1 || t->skin.fog != NULL)
1308                         t->rendertype = SURFRENDER_ALPHA;
1309                 else
1310                         t->rendertype = SURFRENDER_OPAQUE;
1311                 // we don't need to set currentframe if t->animated is false because
1312                 // it was already set up by the texture loader for non-animating
1313                 if (t->animated)
1314                         t->currentframe = t->anim_frames[alttextures][(t->anim_total[alttextures] >= 2) ? (texframe % t->anim_total[alttextures]) : 0];
1315         }
1316 }
1317
1318 void R_PrepareSurfaces(entity_render_t *ent)
1319 {
1320         int i, numsurfaces, *surfacevisframes;
1321         model_t *model;
1322         msurface_t *surf, *surfaces, **surfchain;
1323         vec3_t modelorg;
1324
1325         if (!ent->model)
1326                 return;
1327
1328         model = ent->model;
1329         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1330         numsurfaces = model->brushq1.nummodelsurfaces;
1331         surfaces = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1332         surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1333
1334         R_UpdateTextureInfo(ent);
1335
1336         if (r_dynamic.integer && !r_shadow_realtime_dlight.integer)
1337                 R_MarkLights(ent);
1338
1339         if (model->brushq1.light_ambient != r_ambient.value)
1340         {
1341                 model->brushq1.light_ambient = r_ambient.value;
1342                 for (i = 0;i < model->brushq1.nummodelsurfaces;i++)
1343                         model->brushq1.surfaces[i + model->brushq1.firstmodelsurface].cached_dlight = true;
1344         }
1345         else
1346         {
1347                 for (i = 0;i < model->brushq1.light_styles;i++)
1348                 {
1349                         if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
1350                         {
1351                                 model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
1352                                 for (surfchain = model->brushq1.light_styleupdatechains[i];*surfchain;surfchain++)
1353                                         (**surfchain).cached_dlight = true;
1354                         }
1355                 }
1356         }
1357
1358         for (i = 0, surf = surfaces;i < numsurfaces;i++, surf++)
1359         {
1360                 if (surfacevisframes[i] == r_framecount)
1361                 {
1362 #if !WORLDNODECULLBACKFACES
1363                         // mark any backface surfaces as not visible
1364                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1365                         {
1366                                 if (!(surf->flags & SURF_PLANEBACK))
1367                                         surfacevisframes[i] = -1;
1368                         }
1369                         else
1370                         {
1371                                 if ((surf->flags & SURF_PLANEBACK))
1372                                         surfacevisframes[i] = -1;
1373                         }
1374                         if (surfacevisframes[i] == r_framecount)
1375 #endif
1376                         {
1377                                 c_faces++;
1378                                 surf->visframe = r_framecount;
1379                                 if (surf->cached_dlight && surf->lightmaptexture != NULL)
1380                                         R_BuildLightMap(ent, surf);
1381                         }
1382                 }
1383         }
1384 }
1385
1386 void R_DrawSurfaces(entity_render_t *ent, int type, msurface_t ***chains)
1387 {
1388         int i;
1389         texture_t *t;
1390         if (ent->model == NULL)
1391                 return;
1392         R_Mesh_Matrix(&ent->matrix);
1393         for (i = 0, t = ent->model->brushq1.textures;i < ent->model->brushq1.numtextures;i++, t++)
1394                 if (t->shader->shaderfunc[type] && t->currentframe && chains[i] != NULL)
1395                         t->shader->shaderfunc[type](ent, t->currentframe, chains[i]);
1396 }
1397
1398 static void R_DrawPortal_Callback(const void *calldata1, int calldata2)
1399 {
1400         int i;
1401         float *v;
1402         rmeshstate_t m;
1403         const entity_render_t *ent = calldata1;
1404         const mportal_t *portal = ent->model->brushq1.portals + calldata2;
1405         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1406         GL_DepthMask(false);
1407         GL_DepthTest(true);
1408         R_Mesh_Matrix(&ent->matrix);
1409         GL_VertexPointer(varray_vertex3f);
1410
1411         memset(&m, 0, sizeof(m));
1412         R_Mesh_State_Texture(&m);
1413
1414         i = portal - ent->model->brushq1.portals;
1415         GL_Color(((i & 0x0007) >> 0) * (1.0f / 7.0f),
1416                          ((i & 0x0038) >> 3) * (1.0f / 7.0f),
1417                          ((i & 0x01C0) >> 6) * (1.0f / 7.0f),
1418                          0.125f);
1419         if (PlaneDiff(r_vieworigin, (&portal->plane)) < 0)
1420         {
1421                 for (i = portal->numpoints - 1, v = varray_vertex3f;i >= 0;i--, v += 3)
1422                         VectorCopy(portal->points[i].position, v);
1423         }
1424         else
1425                 for (i = 0, v = varray_vertex3f;i < portal->numpoints;i++, v += 3)
1426                         VectorCopy(portal->points[i].position, v);
1427         R_Mesh_Draw(portal->numpoints, portal->numpoints - 2, polygonelements);
1428 }
1429
1430 // LordHavoc: this is just a nice debugging tool, very slow
1431 static void R_DrawPortals(entity_render_t *ent)
1432 {
1433         int i;
1434         mportal_t *portal, *endportal;
1435         float temp[3], center[3], f;
1436         if (ent->model == NULL)
1437                 return;
1438         for (portal = ent->model->brushq1.portals, endportal = portal + ent->model->brushq1.numportals;portal < endportal;portal++)
1439         {
1440                 if (portal->numpoints <= POLYGONELEMENTS_MAXPOINTS)
1441                 {
1442                         VectorClear(temp);
1443                         for (i = 0;i < portal->numpoints;i++)
1444                                 VectorAdd(temp, portal->points[i].position, temp);
1445                         f = ixtable[portal->numpoints];
1446                         VectorScale(temp, f, temp);
1447                         Matrix4x4_Transform(&ent->matrix, temp, center);
1448                         R_MeshQueue_AddTransparent(center, R_DrawPortal_Callback, ent, portal - ent->model->brushq1.portals);
1449                 }
1450         }
1451 }
1452
1453 void R_PrepareBrushModel(entity_render_t *ent)
1454 {
1455         int i, numsurfaces, *surfacevisframes, *surfacepvsframes;
1456         msurface_t *surf;
1457         model_t *model;
1458 #if WORLDNODECULLBACKFACES
1459         vec3_t modelorg;
1460 #endif
1461
1462         // because bmodels can be reused, we have to decide which things to render
1463         // from scratch every time
1464         model = ent->model;
1465         if (model == NULL)
1466                 return;
1467 #if WORLDNODECULLBACKFACES
1468         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1469 #endif
1470         numsurfaces = model->brushq1.nummodelsurfaces;
1471         surf = model->brushq1.surfaces + model->brushq1.firstmodelsurface;
1472         surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1473         surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1474         for (i = 0;i < numsurfaces;i++, surf++)
1475         {
1476 #if WORLDNODECULLBACKFACES
1477                 // mark any backface surfaces as not visible
1478                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1479                 {
1480                         if ((surf->flags & SURF_PLANEBACK))
1481                                 surfacevisframes[i] = r_framecount;
1482                 }
1483                 else if (!(surf->flags & SURF_PLANEBACK))
1484                         surfacevisframes[i] = r_framecount;
1485 #else
1486                 surfacevisframes[i] = r_framecount;
1487 #endif
1488                 surf->dlightframe = -1;
1489         }
1490         R_PrepareSurfaces(ent);
1491 }
1492
1493 void R_SurfaceWorldNode (entity_render_t *ent)
1494 {
1495         int i, *surfacevisframes, *surfacepvsframes, surfnum;
1496         msurface_t *surf;
1497         mleaf_t *leaf;
1498         model_t *model;
1499         vec3_t modelorg;
1500
1501         // equivilant to quake's RecursiveWorldNode but faster and more effective
1502         model = ent->model;
1503         if (model == NULL)
1504                 return;
1505         surfacevisframes = model->brushq1.surfacevisframes + model->brushq1.firstmodelsurface;
1506         surfacepvsframes = model->brushq1.surfacepvsframes + model->brushq1.firstmodelsurface;
1507         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1508
1509         for (leaf = model->brushq1.pvsleafchain;leaf;leaf = leaf->pvschain)
1510         {
1511                 if (!R_CullBox (leaf->mins, leaf->maxs))
1512                 {
1513                         c_leafs++;
1514                         leaf->visframe = r_framecount;
1515                 }
1516         }
1517
1518         for (i = 0;i < model->brushq1.pvssurflistlength;i++)
1519         {
1520                 surfnum = model->brushq1.pvssurflist[i];
1521                 surf = model->brushq1.surfaces + surfnum;
1522 #if WORLDNODECULLBACKFACES
1523                 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1524                 {
1525                         if ((surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1526                                 surfacevisframes[surfnum] = r_framecount;
1527                 }
1528                 else
1529                 {
1530                         if (!(surf->flags & SURF_PLANEBACK) && !R_CullBox (surf->poly_mins, surf->poly_maxs))
1531                                 surfacevisframes[surfnum] = r_framecount;
1532                 }
1533 #else
1534                 if (!R_CullBox (surf->poly_mins, surf->poly_maxs))
1535                         surfacevisframes[surfnum] = r_framecount;
1536 #endif
1537         }
1538 }
1539
1540 static void R_PortalWorldNode(entity_render_t *ent, mleaf_t *viewleaf)
1541 {
1542         int c, leafstackpos, *mark, *surfacevisframes, bitnum;
1543 #if WORLDNODECULLBACKFACES
1544         int n;
1545         msurface_t *surf;
1546 #endif
1547         mleaf_t *leaf, *leafstack[8192];
1548         mportal_t *p;
1549         vec3_t modelorg;
1550         msurface_t *surfaces;
1551         if (ent->model == NULL)
1552                 return;
1553         // LordHavoc: portal-passage worldnode with PVS;
1554         // follows portals leading outward from viewleaf, does not venture
1555         // offscreen or into leafs that are not visible, faster than Quake's
1556         // RecursiveWorldNode
1557         surfaces = ent->model->brushq1.surfaces;
1558         surfacevisframes = ent->model->brushq1.surfacevisframes;
1559         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1560         viewleaf->worldnodeframe = r_framecount;
1561         leafstack[0] = viewleaf;
1562         leafstackpos = 1;
1563         while (leafstackpos)
1564         {
1565                 c_leafs++;
1566                 leaf = leafstack[--leafstackpos];
1567                 leaf->visframe = r_framecount;
1568                 // draw any surfaces bounding this leaf
1569                 if (leaf->nummarksurfaces)
1570                 {
1571                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--)
1572                         {
1573 #if WORLDNODECULLBACKFACES
1574                                 n = *mark++;
1575                                 if (surfacevisframes[n] != r_framecount)
1576                                 {
1577                                         surf = surfaces + n;
1578                                         if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1579                                         {
1580                                                 if ((surf->flags & SURF_PLANEBACK))
1581                                                         surfacevisframes[n] = r_framecount;
1582                                         }
1583                                         else
1584                                         {
1585                                                 if (!(surf->flags & SURF_PLANEBACK))
1586                                                         surfacevisframes[n] = r_framecount;
1587                                         }
1588                                 }
1589 #else
1590                                 surfacevisframes[*mark++] = r_framecount;
1591 #endif
1592                         }
1593                 }
1594                 // follow portals into other leafs
1595                 for (p = leaf->portals;p;p = p->next)
1596                 {
1597                         // LordHavoc: this DotProduct hurts less than a cache miss
1598                         // (which is more likely to happen if backflowing through leafs)
1599                         if (DotProduct(modelorg, p->plane.normal) < (p->plane.dist + 1))
1600                         {
1601                                 leaf = p->past;
1602                                 if (leaf->worldnodeframe != r_framecount)
1603                                 {
1604                                         leaf->worldnodeframe = r_framecount;
1605                                         // FIXME: R_CullBox is absolute, should be done relative
1606                                         bitnum = (leaf - ent->model->brushq1.leafs) - 1;
1607                                         if ((r_pvsbits[bitnum >> 3] & (1 << (bitnum & 7))) && !R_CullBox(leaf->mins, leaf->maxs))
1608                                                 leafstack[leafstackpos++] = leaf;
1609                                 }
1610                         }
1611                 }
1612         }
1613 }
1614
1615 void R_PVSUpdate (entity_render_t *ent, mleaf_t *viewleaf)
1616 {
1617         int j, c, *surfacepvsframes, *mark;
1618         mleaf_t *leaf;
1619         model_t *model;
1620
1621         model = ent->model;
1622         if (model && (model->brushq1.pvsviewleaf != viewleaf || model->brushq1.pvsviewleafnovis != r_novis.integer))
1623         {
1624                 model->brushq1.pvsframecount++;
1625                 model->brushq1.pvsviewleaf = viewleaf;
1626                 model->brushq1.pvsviewleafnovis = r_novis.integer;
1627                 model->brushq1.pvsleafchain = NULL;
1628                 model->brushq1.pvssurflistlength = 0;
1629                 if (viewleaf)
1630                 {
1631                         surfacepvsframes = model->brushq1.surfacepvsframes;
1632                         for (j = 0;j < model->brushq1.visleafs;j++)
1633                         {
1634                                 if (r_pvsbits[j >> 3] & (1 << (j & 7)))
1635                                 {
1636                                         leaf = model->brushq1.leafs + j + 1;
1637                                         leaf->pvsframe = model->brushq1.pvsframecount;
1638                                         leaf->pvschain = model->brushq1.pvsleafchain;
1639                                         model->brushq1.pvsleafchain = leaf;
1640                                         // mark surfaces bounding this leaf as visible
1641                                         for (c = leaf->nummarksurfaces, mark = leaf->firstmarksurface;c;c--, mark++)
1642                                                 surfacepvsframes[*mark] = model->brushq1.pvsframecount;
1643                                 }
1644                         }
1645                         model->brushq1.BuildPVSTextureChains(model);
1646                 }
1647         }
1648 }
1649
1650 void R_WorldVisibility(entity_render_t *ent)
1651 {
1652         vec3_t modelorg;
1653         mleaf_t *viewleaf;
1654
1655         Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
1656         viewleaf = (ent->model && ent->model->brushq1.PointInLeaf) ? ent->model->brushq1.PointInLeaf(ent->model, modelorg) : NULL;
1657         R_PVSUpdate(ent, viewleaf);
1658
1659         if (!viewleaf)
1660                 return;
1661
1662         if (r_surfaceworldnode.integer || viewleaf->contents == CONTENTS_SOLID)
1663                 R_SurfaceWorldNode (ent);
1664         else
1665                 R_PortalWorldNode (ent, viewleaf);
1666 }
1667
1668 void R_DrawWorld(entity_render_t *ent)
1669 {
1670         if (ent->model == NULL)
1671                 return;
1672         if (!ent->model->brushq1.numleafs)
1673         {
1674                 if (ent->model->DrawSky)
1675                         ent->model->DrawSky(ent);
1676                 if (ent->model->Draw)
1677                         ent->model->Draw(ent);
1678         }
1679         else
1680         {
1681                 R_PrepareSurfaces(ent);
1682                 R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1683                 R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1684                 if (r_drawportals.integer)
1685                         R_DrawPortals(ent);
1686         }
1687 }
1688
1689 void R_Model_Brush_DrawSky(entity_render_t *ent)
1690 {
1691         if (ent->model == NULL)
1692                 return;
1693         if (ent != &cl_entities[0].render)
1694                 R_PrepareBrushModel(ent);
1695         R_DrawSurfaces(ent, SHADERSTAGE_SKY, ent->model->brushq1.pvstexturechains);
1696 }
1697
1698 void R_Model_Brush_Draw(entity_render_t *ent)
1699 {
1700         if (ent->model == NULL)
1701                 return;
1702         c_bmodels++;
1703         if (ent != &cl_entities[0].render)
1704                 R_PrepareBrushModel(ent);
1705         R_DrawSurfaces(ent, SHADERSTAGE_NORMAL, ent->model->brushq1.pvstexturechains);
1706 }
1707
1708 void R_Model_Brush_DrawShadowVolume (entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
1709 {
1710         int i;
1711         msurface_t *surf;
1712         float projectdistance, f, temp[3], lightradius2;
1713         if (ent->model == NULL)
1714                 return;
1715         R_Mesh_Matrix(&ent->matrix);
1716         lightradius2 = lightradius * lightradius;
1717         R_UpdateTextureInfo(ent);
1718         projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
1719         for (i = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;i < ent->model->brushq1.nummodelsurfaces;i++, surf++)
1720         {
1721                 if (surf->texinfo->texture->rendertype == SURFRENDER_OPAQUE && surf->flags & SURF_SHADOWCAST)
1722                 {
1723                         f = PlaneDiff(relativelightorigin, surf->plane);
1724                         if (surf->flags & SURF_PLANEBACK)
1725                                 f = -f;
1726                         // draw shadows only for frontfaces and only if they are close
1727                         if (f >= 0.1 && f < lightradius)
1728                         {
1729                                 temp[0] = bound(surf->poly_mins[0], relativelightorigin[0], surf->poly_maxs[0]) - relativelightorigin[0];
1730                                 temp[1] = bound(surf->poly_mins[1], relativelightorigin[1], surf->poly_maxs[1]) - relativelightorigin[1];
1731                                 temp[2] = bound(surf->poly_mins[2], relativelightorigin[2], surf->poly_maxs[2]) - relativelightorigin[2];
1732                                 if (DotProduct(temp, temp) < lightradius2)
1733                                         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);
1734                         }
1735                 }
1736         }
1737 }
1738
1739 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)
1740 {
1741         int surfnum;
1742         msurface_t *surf;
1743         texture_t *t;
1744         float f, lightmins[3], lightmaxs[3];
1745         if (ent->model == NULL)
1746                 return;
1747         R_Mesh_Matrix(&ent->matrix);
1748         lightmins[0] = relativelightorigin[0] - lightradius;
1749         lightmins[1] = relativelightorigin[1] - lightradius;
1750         lightmins[2] = relativelightorigin[2] - lightradius;
1751         lightmaxs[0] = relativelightorigin[0] + lightradius;
1752         lightmaxs[1] = relativelightorigin[1] + lightradius;
1753         lightmaxs[2] = relativelightorigin[2] + lightradius;
1754         R_UpdateTextureInfo(ent);
1755         for (surfnum = 0, surf = ent->model->brushq1.surfaces + ent->model->brushq1.firstmodelsurface;surfnum < ent->model->brushq1.nummodelsurfaces;surfnum++, surf++)
1756         {
1757                 if ((ent != &cl_entities[0].render || surf->visframe == r_framecount) && BoxesOverlap(surf->poly_mins, surf->poly_maxs, lightmins, lightmaxs))
1758                 {
1759                         f = PlaneDiff(relativelightorigin, surf->plane);
1760                         if (surf->flags & SURF_PLANEBACK)
1761                                 f = -f;
1762                         if (f >= -0.1 && f < lightradius)
1763                         {
1764                                 t = surf->texinfo->texture->currentframe;
1765                                 if (t->rendertype == SURFRENDER_OPAQUE && t->flags & SURF_SHADOWLIGHT)
1766                                 {
1767                                         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);
1768                                         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);
1769                                 }
1770                         }
1771                 }
1772         }
1773 }
1774
1775 void R_DrawCollisionBrush(colbrushf_t *brush)
1776 {
1777         int i;
1778         i = ((int)brush) / sizeof(colbrushf_t);
1779         GL_Color((i & 31) * (1.0f / 32.0f), ((i >> 5) & 31) * (1.0f / 32.0f), ((i >> 10) & 31) * (1.0f / 32.0f), 0.2f);
1780         GL_VertexPointer(brush->points->v);
1781         R_Mesh_Draw(brush->numpoints, brush->numtriangles, brush->elements);
1782 }
1783
1784 void R_Q3BSP_DrawSkyFace(entity_render_t *ent, q3mface_t *face)
1785 {
1786         rmeshstate_t m;
1787         if (!face->num_triangles)
1788                 return;
1789         if (skyrendernow)
1790         {
1791                 skyrendernow = false;
1792                 if (skyrendermasked)
1793                         R_Sky();
1794         }
1795
1796         R_Mesh_Matrix(&ent->matrix);
1797
1798         GL_Color(fogcolor[0], fogcolor[1], fogcolor[2], 1);
1799         if (skyrendermasked)
1800         {
1801                 // depth-only (masking)
1802                 qglColorMask(0,0,0,0);
1803                 // just to make sure that braindead drivers don't draw anything
1804                 // despite that colormask...
1805                 GL_BlendFunc(GL_ZERO, GL_ONE);
1806         }
1807         else
1808         {
1809                 // fog sky
1810                 GL_BlendFunc(GL_ONE, GL_ZERO);
1811         }
1812         GL_DepthMask(true);
1813         GL_DepthTest(true);
1814
1815         memset(&m, 0, sizeof(m));
1816         R_Mesh_State_Texture(&m);
1817
1818         GL_VertexPointer(face->data_vertex3f);
1819         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1820         qglColorMask(1,1,1,1);
1821 }
1822
1823 void R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(entity_render_t *ent, q3mface_t *face)
1824 {
1825         rmeshstate_t m;
1826         memset(&m, 0, sizeof(m));
1827         GL_BlendFunc(GL_ONE, GL_ZERO);
1828         GL_DepthMask(true);
1829         GL_DepthTest(true);
1830         if (face->texture->skin.glow)
1831         {
1832                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1833                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1834                 GL_Color(1, 1, 1, 1);
1835         }
1836         else
1837                 GL_Color(0, 0, 0, 1);
1838         R_Mesh_State_Texture(&m);
1839         GL_VertexPointer(face->data_vertex3f);
1840         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1841 }
1842
1843 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(entity_render_t *ent, q3mface_t *face)
1844 {
1845         rmeshstate_t m;
1846         memset(&m, 0, sizeof(m));
1847         GL_BlendFunc(GL_ONE, GL_ZERO);
1848         GL_DepthMask(true);
1849         GL_DepthTest(true);
1850         m.tex[0] = R_GetTexture(face->texture->skin.base);
1851         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1852         m.tex[1] = R_GetTexture(face->lightmaptexture);
1853         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1854         m.texrgbscale[1] = 2;
1855         GL_Color(1, 1, 1, 1);
1856         R_Mesh_State_Texture(&m);
1857         GL_VertexPointer(face->data_vertex3f);
1858         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1859 }
1860
1861 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(entity_render_t *ent, q3mface_t *face)
1862 {
1863         rmeshstate_t m;
1864         memset(&m, 0, sizeof(m));
1865         GL_BlendFunc(GL_ONE, GL_ZERO);
1866         GL_DepthMask(true);
1867         GL_DepthTest(true);
1868         m.tex[0] = R_GetTexture(face->texture->skin.base);
1869         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1870         GL_Color(1, 1, 1, 1);
1871         R_Mesh_State_Texture(&m);
1872         GL_VertexPointer(face->data_vertex3f);
1873         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1874 }
1875
1876 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(entity_render_t *ent, q3mface_t *face)
1877 {
1878         rmeshstate_t m;
1879         memset(&m, 0, sizeof(m));
1880         GL_BlendFunc(GL_ONE, GL_SRC_COLOR);
1881         GL_DepthMask(true);
1882         GL_DepthTest(true);
1883         m.tex[0] = R_GetTexture(face->lightmaptexture);
1884         m.pointer_texcoord[0] = face->data_texcoordlightmap2f;
1885         GL_Color(1, 1, 1, 1);
1886         R_Mesh_State_Texture(&m);
1887         GL_VertexPointer(face->data_vertex3f);
1888         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1889 }
1890
1891 void R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(entity_render_t *ent, q3mface_t *face)
1892 {
1893         rmeshstate_t m;
1894         memset(&m, 0, sizeof(m));
1895         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1896         GL_DepthMask(true);
1897         GL_DepthTest(true);
1898         if (face->texture->skin.glow)
1899         {
1900                 m.tex[0] = R_GetTexture(face->texture->skin.glow);
1901                 m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1902                 GL_Color(1, 1, 1, 1);
1903         }
1904         else
1905                 GL_Color(0, 0, 0, 1);
1906         R_Mesh_State_Texture(&m);
1907         GL_VertexPointer(face->data_vertex3f);
1908         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1909 }
1910
1911 void R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(entity_render_t *ent, q3mface_t *face)
1912 {
1913         rmeshstate_t m;
1914         memset(&m, 0, sizeof(m));
1915         GL_BlendFunc(GL_ONE, GL_ZERO);
1916         GL_DepthMask(true);
1917         GL_DepthTest(true);
1918         m.tex[0] = R_GetTexture(face->texture->skin.base);
1919         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1920         if (gl_combine.integer)
1921         {
1922                 m.texrgbscale[0] = 2;
1923                 GL_ColorPointer(face->data_color4f);
1924         }
1925         else
1926         {
1927                 int i;
1928                 for (i = 0;i < face->num_vertices;i++)
1929                 {
1930                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
1931                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
1932                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
1933                         varray_color4f[i*4+3] = face->data_color4f[i*4+3];
1934                 }
1935                 GL_ColorPointer(varray_color4f);
1936         }
1937         R_Mesh_State_Texture(&m);
1938         GL_VertexPointer(face->data_vertex3f);
1939         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1940 }
1941
1942 void R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(entity_render_t *ent, q3mface_t *face)
1943 {
1944         rmeshstate_t m;
1945         memset(&m, 0, sizeof(m));
1946         GL_BlendFunc(GL_ONE, GL_ONE);
1947         GL_DepthMask(true);
1948         GL_DepthTest(true);
1949         m.tex[0] = R_GetTexture(face->texture->skin.base);
1950         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1951         GL_Color(r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), r_ambient.value * (1.0f / 128.0f), 1);
1952         R_Mesh_State_Texture(&m);
1953         GL_VertexPointer(face->data_vertex3f);
1954         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
1955 }
1956
1957 void R_Q3BSP_DrawFace_TransparentCallback(const void *voident, int facenumber)
1958 {
1959         const entity_render_t *ent = voident;
1960         q3mface_t *face = ent->model->brushq3.data_faces + facenumber;
1961         rmeshstate_t m;
1962         R_Mesh_Matrix(&ent->matrix);
1963         memset(&m, 0, sizeof(m));
1964         if (ent->effects & EF_ADDITIVE)
1965                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1966         else
1967                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1968         GL_DepthMask(false);
1969         GL_DepthTest(true);
1970         m.tex[0] = R_GetTexture(face->texture->skin.base);
1971         m.pointer_texcoord[0] = face->data_texcoordtexture2f;
1972         // LordHavoc: quake3 was not able to do this; lit transparent surfaces
1973         if (gl_combine.integer)
1974         {
1975                 m.texrgbscale[0] = 2;
1976                 if (r_textureunits.integer >= 2)
1977                 {
1978                         m.tex[1] = R_GetTexture(face->lightmaptexture);
1979                         m.pointer_texcoord[1] = face->data_texcoordlightmap2f;
1980                         GL_Color(1, 1, 1, ent->alpha);
1981                 }
1982                 else
1983                 {
1984                         if (ent->alpha == 1)
1985                                 GL_ColorPointer(face->data_color4f);
1986                         else
1987                         {
1988                                 int i;
1989                                 for (i = 0;i < face->num_vertices;i++)
1990                                 {
1991                                         varray_color4f[i*4+0] = face->data_color4f[i*4+0];
1992                                         varray_color4f[i*4+1] = face->data_color4f[i*4+1];
1993                                         varray_color4f[i*4+2] = face->data_color4f[i*4+2];
1994                                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
1995                                 }
1996                                 GL_ColorPointer(varray_color4f);
1997                         }
1998                 }
1999         }
2000         else
2001         {
2002                 int i;
2003                 for (i = 0;i < face->num_vertices;i++)
2004                 {
2005                         varray_color4f[i*4+0] = face->data_color4f[i*4+0] * 2.0f;
2006                         varray_color4f[i*4+1] = face->data_color4f[i*4+1] * 2.0f;
2007                         varray_color4f[i*4+2] = face->data_color4f[i*4+2] * 2.0f;
2008                         varray_color4f[i*4+3] = face->data_color4f[i*4+3] * ent->alpha;
2009                 }
2010                 GL_ColorPointer(varray_color4f);
2011         }
2012         R_Mesh_State_Texture(&m);
2013         GL_VertexPointer(face->data_vertex3f);
2014         qglDisable(GL_CULL_FACE);
2015         R_Mesh_Draw(face->num_vertices, face->num_triangles, face->data_element3i);
2016         qglEnable(GL_CULL_FACE);
2017 }
2018
2019 void R_Q3BSP_DrawFace(entity_render_t *ent, q3mface_t *face)
2020 {
2021         if (!face->num_triangles)
2022                 return;
2023         if (face->texture->renderflags)
2024         {
2025                 if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY)
2026                         return;
2027                 if (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW)
2028                         return;
2029         }
2030         face->visframe = r_framecount;
2031         if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_TRANSPARENT) || ent->alpha < 1 || (ent->effects & EF_ADDITIVE))
2032         {
2033                 vec3_t facecenter, center;
2034                 facecenter[0] = (face->mins[0] + face->maxs[0]) * 0.5f;
2035                 facecenter[1] = (face->mins[1] + face->maxs[1]) * 0.5f;
2036                 facecenter[2] = (face->mins[2] + face->maxs[2]) * 0.5f;
2037                 Matrix4x4_Transform(&ent->matrix, facecenter, center);
2038                 R_MeshQueue_AddTransparent(center, R_Q3BSP_DrawFace_TransparentCallback, ent, face - ent->model->brushq3.data_faces);
2039                 return;
2040         }
2041         R_Mesh_Matrix(&ent->matrix);
2042         if (r_shadow_realtime_world.integer)
2043                 R_Q3BSP_DrawFace_OpaqueWall_Pass_OpaqueGlow(ent, face);
2044         else if ((ent->effects & EF_FULLBRIGHT) || r_fullbright.integer)
2045         {
2046                 R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2047                 if (face->texture->skin.glow)
2048                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2049         }
2050         else if (face->lightmaptexture)
2051         {
2052                 if (r_textureunits.integer >= 2 && gl_combine.integer)
2053                         R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureLightmapCombine(ent, face);
2054                 else
2055                 {
2056                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Texture(ent, face);
2057                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Lightmap(ent, face);
2058                 }
2059                 if (face->texture->skin.glow)
2060                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2061         }
2062         else
2063         {
2064                 R_Q3BSP_DrawFace_OpaqueWall_Pass_TextureVertex(ent, face);
2065                 if (face->texture->skin.glow)
2066                         R_Q3BSP_DrawFace_OpaqueWall_Pass_Glow(ent, face);
2067         }
2068         if (r_ambient.value)
2069                 R_Q3BSP_DrawFace_OpaqueWall_Pass_AddTextureAmbient(ent, face);
2070 }
2071
2072 void R_Q3BSP_RecursiveWorldNode(entity_render_t *ent, q3mnode_t *node, const vec3_t modelorg, qbyte *pvs, int markframe)
2073 {
2074         int i;
2075         q3mleaf_t *leaf;
2076         while (node->isnode)
2077         {
2078                 if (R_CullBox(node->mins, node->maxs))
2079                         return;
2080                 R_Q3BSP_RecursiveWorldNode(ent, node->children[0], modelorg, pvs, markframe);
2081                 node = node->children[1];
2082         }
2083         if (R_CullBox(node->mins, node->maxs))
2084                 return;
2085         leaf = (q3mleaf_t *)node;
2086         if (pvs[leaf->clusterindex >> 3] & (1 << (leaf->clusterindex & 7)))
2087                 for (i = 0;i < leaf->numleaffaces;i++)
2088                         leaf->firstleafface[i]->markframe = markframe;
2089 }
2090
2091 static int r_q3bsp_framecount = -1;
2092
2093 void R_Q3BSP_DrawSky(entity_render_t *ent)
2094 {
2095         int i;
2096         q3mface_t *face;
2097         vec3_t modelorg;
2098         model_t *model;
2099         qbyte *pvs;
2100         R_Mesh_Matrix(&ent->matrix);
2101         model = ent->model;
2102         if (r_drawcollisionbrushes.integer < 2)
2103         {
2104                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2105                 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2106                 {
2107                         if (r_q3bsp_framecount != r_framecount)
2108                         {
2109                                 r_q3bsp_framecount = r_framecount;
2110                                 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2111                         }
2112                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2113                                 if (face->markframe == r_framecount && (face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY) && !R_CullBox(face->mins, face->maxs))
2114                                         R_Q3BSP_DrawSkyFace(ent, face);
2115                 }
2116                 else
2117                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2118                                 if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_SKY))
2119                                         R_Q3BSP_DrawSkyFace(ent, face);
2120         }
2121 }
2122
2123 void R_Q3BSP_Draw(entity_render_t *ent)
2124 {
2125         int i;
2126         q3mface_t *face;
2127         vec3_t modelorg;
2128         model_t *model;
2129         qbyte *pvs;
2130         R_Mesh_Matrix(&ent->matrix);
2131         model = ent->model;
2132         if (r_drawcollisionbrushes.integer < 2)
2133         {
2134                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2135                 if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2136                 {
2137                         if (r_q3bsp_framecount != r_framecount)
2138                         {
2139                                 r_q3bsp_framecount = r_framecount;
2140                                 R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, r_framecount);
2141                         }
2142                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2143                                 if (face->markframe == r_framecount && !R_CullBox(face->mins, face->maxs))
2144                                         R_Q3BSP_DrawFace(ent, face);
2145                 }
2146                 else
2147                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2148                                 R_Q3BSP_DrawFace(ent, face);
2149         }
2150         if (r_drawcollisionbrushes.integer >= 1)
2151         {
2152                 rmeshstate_t m;
2153                 memset(&m, 0, sizeof(m));
2154                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
2155                 GL_DepthMask(false);
2156                 GL_DepthTest(true);
2157                 R_Mesh_State_Texture(&m);
2158                 for (i = 0;i < model->brushq3.data_thismodel->numbrushes;i++)
2159                         if (model->brushq3.data_thismodel->firstbrush[i].colbrushf && model->brushq3.data_thismodel->firstbrush[i].colbrushf->numtriangles)
2160                                 R_DrawCollisionBrush(model->brushq3.data_thismodel->firstbrush[i].colbrushf);
2161         }
2162 }
2163
2164 void R_Q3BSP_DrawShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, float lightradius)
2165 {
2166         int i;
2167         q3mface_t *face;
2168         vec3_t modelorg, lightmins, lightmaxs;
2169         model_t *model;
2170         float projectdistance;
2171         projectdistance = 1000000000.0f;//lightradius + ent->model->radius;
2172         if (r_drawcollisionbrushes.integer < 2)
2173         {
2174                 model = ent->model;
2175                 R_Mesh_Matrix(&ent->matrix);
2176                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2177                 lightmins[0] = relativelightorigin[0] - lightradius;
2178                 lightmins[1] = relativelightorigin[1] - lightradius;
2179                 lightmins[2] = relativelightorigin[2] - lightradius;
2180                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2181                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2182                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2183                 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2184                 //      R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2185                 //else
2186                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2187                                 if (BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2188                                         R_Shadow_Volume(face->num_vertices, face->num_triangles, face->data_vertex3f, face->data_element3i, face->data_neighbor3i, relativelightorigin, lightradius, projectdistance);
2189         }
2190 }
2191
2192 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)
2193 {
2194         if ((face->texture->renderflags & Q3MTEXTURERENDERFLAGS_NODRAW) || !face->num_triangles)
2195                 return;
2196         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);
2197         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);
2198 }
2199
2200 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)
2201 {
2202         int i;
2203         q3mface_t *face;
2204         vec3_t modelorg, lightmins, lightmaxs;
2205         model_t *model;
2206         //qbyte *pvs;
2207         //static int markframe = 0;
2208         if (r_drawcollisionbrushes.integer < 2)
2209         {
2210                 model = ent->model;
2211                 R_Mesh_Matrix(&ent->matrix);
2212                 Matrix4x4_Transform(&ent->inversematrix, r_vieworigin, modelorg);
2213                 lightmins[0] = relativelightorigin[0] - lightradius;
2214                 lightmins[1] = relativelightorigin[1] - lightradius;
2215                 lightmins[2] = relativelightorigin[2] - lightradius;
2216                 lightmaxs[0] = relativelightorigin[0] + lightradius;
2217                 lightmaxs[1] = relativelightorigin[1] + lightradius;
2218                 lightmaxs[2] = relativelightorigin[2] + lightradius;
2219                 //if (ent == &cl_entities[0].render && model->brushq3.num_pvsclusters && !r_novis.integer && (pvs = model->brush.GetPVS(model, modelorg)))
2220                 //      R_Q3BSP_RecursiveWorldNode(ent, model->brushq3.data_nodes, modelorg, pvs, ++markframe);
2221                 //else
2222                         for (i = 0, face = model->brushq3.data_thismodel->firstface;i < model->brushq3.data_thismodel->numfaces;i++, face++)
2223                                 if ((ent != &cl_entities[0].render || face->visframe == r_framecount) && BoxesOverlap(lightmins, lightmaxs, face->mins, face->maxs))
2224                                         R_Q3BSP_DrawFaceLight(ent, face, relativelightorigin, relativeeyeorigin, lightradius, lightcolor, matrix_modeltofilter, matrix_modeltoattenuationxyz, matrix_modeltoattenuationz);
2225         }
2226 }
2227
2228 static void gl_surf_start(void)
2229 {
2230 }
2231
2232 static void gl_surf_shutdown(void)
2233 {
2234 }
2235
2236 static void gl_surf_newmap(void)
2237 {
2238 }
2239
2240 void GL_Surf_Init(void)
2241 {
2242         int i;
2243         dlightdivtable[0] = 4194304;
2244         for (i = 1;i < 32768;i++)
2245                 dlightdivtable[i] = 4194304 / (i << 7);
2246
2247         Cvar_RegisterVariable(&r_ambient);
2248         Cvar_RegisterVariable(&r_drawportals);
2249         Cvar_RegisterVariable(&r_testvis);
2250         Cvar_RegisterVariable(&r_floatbuildlightmap);
2251         Cvar_RegisterVariable(&r_detailtextures);
2252         Cvar_RegisterVariable(&r_surfaceworldnode);
2253         Cvar_RegisterVariable(&r_drawcollisionbrushes_polygonoffset);
2254
2255         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2256 }
2257