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