]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_rsurf.c
rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/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
24 #define MAX_LIGHTMAP_SIZE 256
25
26 static signed int blocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27
28 static byte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
29
30 cvar_t r_ambient = {0, "r_ambient", "0"};
31 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
32 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35
36 static void gl_surf_start(void)
37 {
38 }
39
40 static void gl_surf_shutdown(void)
41 {
42 }
43
44 static void gl_surf_newmap(void)
45 {
46 }
47
48 void GL_Surf_Init(void)
49 {
50         Cvar_RegisterVariable(&r_ambient);
51         Cvar_RegisterVariable(&r_vertexsurfaces);
52         Cvar_RegisterVariable(&r_dlightmap);
53         Cvar_RegisterVariable(&r_drawportals);
54         Cvar_RegisterVariable(&r_testvis);
55
56         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
57 }
58
59 static int dlightdivtable[32768];
60
61 static int R_AddDynamicLights (msurface_t *surf)
62 {
63         int         sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
64         unsigned int *bl;
65         float       dist;
66         vec3_t      impact, local;
67
68         // LordHavoc: use 64bit integer...  shame it's not very standardized...
69 #if _MSC_VER || __BORLANDC__
70         __int64     k;
71 #else
72         long long   k;
73 #endif
74
75         lit = false;
76
77         if (!dlightdivtable[1])
78         {
79                 dlightdivtable[0] = 4194304;
80                 for (s = 1; s < 32768; s++)
81                         dlightdivtable[s] = 4194304 / (s << 7);
82         }
83
84         smax = (surf->extents[0] >> 4) + 1;
85         tmax = (surf->extents[1] >> 4) + 1;
86
87         for (lnum = 0; lnum < r_numdlights; lnum++)
88         {
89                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
90                         continue;                                       // not lit by this light
91
92                 softwareuntransform(r_dlight[lnum].origin, local);
93 //              VectorSubtract (r_dlight[lnum].origin, currentrenderentity->origin, local);
94                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
95
96                 // for comparisons to minimum acceptable light
97                 maxdist = (int) r_dlight[lnum].cullradius2;
98
99                 // already clamped, skip this
100                 // clamp radius to avoid exceeding 32768 entry division table
101                 //if (maxdist > 4194304)
102                 //      maxdist = 4194304;
103
104                 dist2 = dist * dist;
105                 dist2 += LIGHTOFFSET;
106                 if (dist2 >= maxdist)
107                         continue;
108
109                 impact[0] = local[0] - surf->plane->normal[0] * dist;
110                 impact[1] = local[1] - surf->plane->normal[1] * dist;
111                 impact[2] = local[2] - surf->plane->normal[2] * dist;
112
113                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
114                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
115
116                 s = bound(0, impacts, smax * 16) - impacts;
117                 t = bound(0, impactt, tmax * 16) - impactt;
118                 i = s * s + t * t + dist2;
119                 if (i > maxdist)
120                         continue;
121
122                 // reduce calculations
123                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
124                         sdtable[s] = i * i + dist2;
125
126                 maxdist3 = maxdist - dist2;
127
128                 // convert to 8.8 blocklights format
129                 red = r_dlight[lnum].light[0];
130                 green = r_dlight[lnum].light[1];
131                 blue = r_dlight[lnum].light[2];
132                 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
133                 bl = blocklights;
134                 smax3 = smax * 3;
135
136                 i = impactt;
137                 for (t = 0;t < tmax;t++, i -= 16)
138                 {
139                         td = i * i;
140                         // make sure some part of it is visible on this line
141                         if (td < maxdist3)
142                         {
143                                 maxdist2 = maxdist - td;
144                                 for (s = 0;s < smax;s++)
145                                 {
146                                         if (sdtable[s] < maxdist2)
147                                         {
148                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
149                                                 if (k > 0)
150                                                 {
151                                                         bl[0] += (red   * k) >> 8;
152                                                         bl[1] += (green * k) >> 8;
153                                                         bl[2] += (blue  * k) >> 8;
154                                                         lit = true;
155                                                 }
156                                         }
157                                         bl += 3;
158                                 }
159                         }
160                         else // skip line
161                                 bl += smax3;
162                 }
163         }
164         return lit;
165 }
166
167 /*
168 ===============
169 R_BuildLightMap
170
171 Combine and scale multiple lightmaps into the 8.8 format in blocklights
172 ===============
173 */
174 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
175 {
176         int             smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
177         byte    *lightmap, *out;
178
179         // update cached lighting info
180         surf->cached_dlight = 0;
181         surf->cached_lightscalebit = lightscalebit;
182         surf->cached_ambient = r_ambient.value;
183         surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
184         surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
185         surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
186         surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
187
188         smax = (surf->extents[0]>>4)+1;
189         tmax = (surf->extents[1]>>4)+1;
190         size = smax*tmax;
191         size3 = size*3;
192         lightmap = surf->samples;
193
194 // set to full bright if no light data
195         if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
196         {
197                 bl = blocklights;
198                 for (i = 0;i < size;i++)
199                 {
200                         *bl++ = 255*256;
201                         *bl++ = 255*256;
202                         *bl++ = 255*256;
203                 }
204         }
205         else
206         {
207 // clear to no light
208                 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
209                 if (j)
210                 {
211                         bl = blocklights;
212                         for (i = 0;i < size3;i++)
213                                 *bl++ = j;
214                 }
215                 else
216                         memset(&blocklights[0], 0, size*3*sizeof(int));
217
218                 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
219                 {
220                         surf->cached_dlight = R_AddDynamicLights(surf);
221                         if (surf->cached_dlight)
222                                 c_light_polys++;
223                         else if (dlightchanged)
224                                 return; // don't upload if only updating dlights and none mattered
225                 }
226
227 // add all the lightmaps
228                 if (lightmap)
229                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
230                                 for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
231                                         *bl++ += *lightmap++ * scale;
232         }
233
234         bl = blocklights;
235         out = templight;
236         // deal with lightmap brightness scale
237         shift = 7 + lightscalebit;
238         if (currentrenderentity->model->lightmaprgba)
239         {
240                 stride = (surf->lightmaptexturestride - smax) * 4;
241                 for (i = 0;i < tmax;i++, out += stride)
242                 {
243                         for (j = 0;j < smax;j++)
244                         {
245                                 l = *bl++ >> shift;*out++ = min(l, 255);
246                                 l = *bl++ >> shift;*out++ = min(l, 255);
247                                 l = *bl++ >> shift;*out++ = min(l, 255);
248                                 *out++ = 255;
249                         }
250                 }
251         }
252         else
253         {
254                 stride = (surf->lightmaptexturestride - smax) * 3;
255                 for (i = 0;i < tmax;i++, out += stride)
256                 {
257                         for (j = 0;j < smax;j++)
258                         {
259                                 l = *bl++ >> shift;*out++ = min(l, 255);
260                                 l = *bl++ >> shift;*out++ = min(l, 255);
261                                 l = *bl++ >> shift;*out++ = min(l, 255);
262                         }
263                 }
264         }
265
266         R_UpdateTexture(surf->lightmaptexture, templight);
267 }
268
269 /*
270 ===============
271 R_TextureAnimation
272
273 Returns the proper texture for a given time and base texture
274 ===============
275 */
276 /*
277 // note: this was manually inlined in R_PrepareSurfaces
278 static texture_t *R_TextureAnimation (texture_t *base)
279 {
280         if (currentrenderentity->frame && base->alternate_anims != NULL)
281                 base = base->alternate_anims;
282
283         if (base->anim_total < 2)
284                 return base;
285
286         return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
287 }
288 */
289
290
291 /*
292 =============================================================
293
294         BRUSH MODELS
295
296 =============================================================
297 */
298
299
300 static float turbsin[256] =
301 {
302         #include "gl_warp_sin.h"
303 };
304 #define TURBSCALE (256.0 / (2 * M_PI))
305
306 #define MAX_SURFVERTS 1024
307 typedef struct
308 {
309         float v[4];
310         float st[2];
311         float uv[2];
312         float c[4];
313 }
314 surfvert_t;
315 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
316
317 static int RSurfShader_Sky(int stage, msurface_t *s)
318 {
319         int                             i;
320         float                   number, length, dir[3], speedscale;
321         surfvertex_t    *v;
322         surfvert_t              *sv;
323         rmeshinfo_t             m;
324
325         // LordHavoc: HalfLife maps have freaky skypolys...
326         if (currentrenderentity->model->ishlbsp)
327                 return true;
328
329         if (stage == 0)
330         {
331                 if (skyrendermasked)
332                 {
333                         if (skyrendernow)
334                         {
335                                 skyrendernow = false;
336                                 R_Sky();
337                         }
338                         // draw depth-only polys
339                         memset(&m, 0, sizeof(m));
340                         m.transparent = false;
341                         m.blendfunc1 = GL_ZERO;
342                         m.blendfunc2 = GL_ONE;
343                         m.depthwrite = true;
344                         m.numtriangles = s->mesh.numtriangles;
345                         m.numverts = s->mesh.numverts;
346                         m.index = s->mesh.index;
347                         //m.cr = 0;
348                         //m.cg = 0;
349                         //m.cb = 0;
350                         //m.ca = 0;
351                         if (softwaretransform_complexity)
352                         {
353                                 m.vertex = &svert[0].v[0];
354                                 m.vertexstep = sizeof(surfvert_t);
355                                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
356                                         softwaretransform(v->v, sv->v);
357                         }
358                         else
359                         {
360                                 m.vertex = &s->mesh.vertex[0].v[0];
361                                 m.vertexstep = sizeof(surfvertex_t);
362                         }
363                         R_Mesh_Draw(&m);
364                 }
365                 else if (skyrenderglquake)
366                 {
367                         memset(&m, 0, sizeof(m));
368                         m.transparent = false;
369                         m.blendfunc1 = GL_ONE;
370                         m.blendfunc2 = GL_ZERO;
371                         m.numtriangles = s->mesh.numtriangles;
372                         m.numverts = s->mesh.numverts;
373                         m.index = s->mesh.index;
374                         m.vertex = &svert[0].v[0];
375                         m.vertexstep = sizeof(surfvert_t);
376                         m.cr = 1;
377                         m.cg = 1;
378                         m.cb = 1;
379                         m.ca = 1;
380                         if (r_mergesky.integer)
381                                 m.tex[0] = R_GetTexture(mergeskytexture);
382                         else
383                                 m.tex[0] = R_GetTexture(solidskytexture);
384                         m.texcoords[0] = &svert[0].st[0];
385                         m.texcoordstep[0] = sizeof(surfvert_t);
386                         speedscale = cl.time * (8.0/128.0);
387                         speedscale -= (int)speedscale;
388                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
389                         {
390                                 softwaretransform(v->v, sv->v);
391                                 VectorSubtract (sv->v, r_origin, dir);
392                                 // flatten the sphere
393                                 dir[2] *= 3;
394
395                                 number = DotProduct(dir, dir);
396                                 #if SLOWMATH
397                                 length = 3.0f / sqrt(number);
398                                 #else
399                                 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
400                                 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
401                                 #endif
402
403                                 sv->st[0] = speedscale + dir[0] * length;
404                                 sv->st[1] = speedscale + dir[1] * length;
405                         }
406                         R_Mesh_Draw(&m);
407                 }
408                 else
409                 {
410                         // flat color
411                         memset(&m, 0, sizeof(m));
412                         m.transparent = false;
413                         m.blendfunc1 = GL_ONE;
414                         m.blendfunc2 = GL_ZERO;
415                         m.numtriangles = s->mesh.numtriangles;
416                         m.numverts = s->mesh.numverts;
417                         m.index = s->mesh.index;
418                         m.cr = fogcolor[0];
419                         m.cg = fogcolor[1];
420                         m.cb = fogcolor[2];
421                         m.ca = 1;
422                         if (softwaretransform_complexity)
423                         {
424                                 m.vertex = &svert[0].v[0];
425                                 m.vertexstep = sizeof(surfvert_t);
426                                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
427                                         softwaretransform(v->v, sv->v);
428                         }
429                         else
430                         {
431                                 m.vertex = &s->mesh.vertex[0].v[0];
432                                 m.vertexstep = sizeof(surfvertex_t);
433                         }
434                         R_Mesh_Draw(&m);
435                 }
436                 return false;
437         }
438         else if (stage == 1)
439         {
440                 if (skyrenderglquake && !r_mergesky.integer)
441                 {
442                         memset(&m, 0, sizeof(m));
443                         m.transparent = false;
444                         m.blendfunc1 = GL_SRC_ALPHA;
445                         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
446                         m.numtriangles = s->mesh.numtriangles;
447                         m.numverts = s->mesh.numverts;
448                         m.index = s->mesh.index;
449                         m.vertex = &svert[0].v[0];
450                         m.vertexstep = sizeof(surfvert_t);
451                         m.cr = 1;
452                         m.cg = 1;
453                         m.cb = 1;
454                         m.ca = 1;
455                         m.tex[0] = R_GetTexture(alphaskytexture);
456                         m.texcoords[0] = &svert[0].st[0];
457                         m.texcoordstep[0] = sizeof(surfvert_t);
458                         speedscale = cl.time * (16.0/128.0);
459                         speedscale -= (int)speedscale;
460                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
461                         {
462                                 softwaretransform(v->v, sv->v);
463                                 VectorSubtract (sv->v, r_origin, dir);
464                                 // flatten the sphere
465                                 dir[2] *= 3;
466
467                                 number = DotProduct(dir, dir);
468                                 #if SLOWMATH
469                                 length = 3.0f / sqrt(number);
470                                 #else
471                                 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
472                                 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
473                                 #endif
474
475                                 sv->st[0] = speedscale + dir[0] * length;
476                                 sv->st[1] = speedscale + dir[1] * length;
477                         }
478                         R_Mesh_Draw(&m);
479                         return false;
480                 }
481                 return true;
482         }
483         else
484                 return true;
485 }
486
487 static int RSurf_Light(int *dlightbits, int numverts)
488 {
489         float           f;
490         int                     i, l, lit = false;
491         rdlight_t       *rd;
492         vec3_t          lightorigin;
493         surfvert_t      *sv;
494         for (l = 0;l < r_numdlights;l++)
495         {
496                 if (dlightbits[l >> 5] & (1 << (l & 31)))
497                 {
498                         rd = &r_dlight[l];
499                         // FIXME: support softwareuntransform here and make bmodels use hardware transform?
500                         VectorCopy(rd->origin, lightorigin);
501                         for (i = 0, sv = svert;i < numverts;i++, sv++)
502                         {
503                                 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
504                                 if (f < rd->cullradius2)
505                                 {
506                                         f = (1.0f / f) - rd->lightsubtract;
507                                         sv->c[0] += rd->light[0] * f;
508                                         sv->c[1] += rd->light[1] * f;
509                                         sv->c[2] += rd->light[2] * f;
510                                         lit = true;
511                                 }
512                         }
513                 }
514         }
515         return lit;
516 }
517
518 static void RSurfShader_Water_Pass_Base(msurface_t *s)
519 {
520         int                             i;
521         float                   diff[3], alpha, ifog;
522         surfvertex_t    *v;
523         surfvert_t              *sv;
524         rmeshinfo_t             m;
525         alpha = s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value;
526
527         memset(&m, 0, sizeof(m));
528         if (alpha != 1 || s->currenttexture->fogtexture != NULL)
529         {
530                 m.transparent = true;
531                 m.blendfunc1 = GL_SRC_ALPHA;
532                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
533         }
534         else
535         {
536                 m.transparent = false;
537                 m.blendfunc1 = GL_SRC_ALPHA;
538                 m.blendfunc2 = GL_ZERO;
539         }
540         m.numtriangles = s->mesh.numtriangles;
541         m.numverts = s->mesh.numverts;
542         m.index = s->mesh.index;
543         m.vertex = &svert[0].v[0];
544         m.vertexstep = sizeof(surfvert_t);
545         m.color = &svert[0].c[0];
546         m.colorstep = sizeof(surfvert_t);
547         m.tex[0] = R_GetTexture(s->currenttexture->texture);
548         m.texcoords[0] = &svert[0].st[0];
549         m.texcoordstep[0] = sizeof(surfvert_t);
550         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
551         {
552                 softwaretransform(v->v, sv->v);
553                 if (r_waterripple.value)
554                         sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
555                 if (s->flags & SURF_DRAWFULLBRIGHT)
556                 {
557                         sv->c[0] = 1;
558                         sv->c[1] = 1;
559                         sv->c[2] = 1;
560                         sv->c[3] = alpha;
561                 }
562                 else
563                 {
564                         sv->c[0] = 0.5f;
565                         sv->c[1] = 0.5f;
566                         sv->c[2] = 0.5f;
567                         sv->c[3] = alpha;
568                 }
569                 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
570                 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
571         }
572         if (s->dlightframe == r_framecount && !(s->flags & SURF_DRAWFULLBRIGHT))
573                 RSurf_Light(s->dlightbits, m.numverts);
574         if (fogenabled)
575         {
576                 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
577                 {
578                         VectorSubtract(sv->v, r_origin, diff);
579                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
580                         sv->c[0] *= ifog;
581                         sv->c[1] *= ifog;
582                         sv->c[2] *= ifog;
583                 }
584         }
585         R_Mesh_Draw(&m);
586 }
587
588 static void RSurfShader_Water_Pass_Glow(msurface_t *s)
589 {
590         int                             i;
591         float                   diff[3], alpha, ifog;
592         surfvertex_t    *v;
593         surfvert_t              *sv;
594         rmeshinfo_t             m;
595         alpha = s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value;
596
597         memset(&m, 0, sizeof(m));
598         m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
599         m.blendfunc1 = GL_SRC_ALPHA;
600         m.blendfunc2 = GL_ONE;
601         m.numtriangles = s->mesh.numtriangles;
602         m.numverts = s->mesh.numverts;
603         m.index = s->mesh.index;
604         m.vertex = &svert[0].v[0];
605         m.vertexstep = sizeof(surfvert_t);
606         m.cr = 1;
607         m.cg = 1;
608         m.cb = 1;
609         m.ca = alpha;
610         m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
611         m.texcoords[0] = &svert[0].st[0];
612         m.texcoordstep[0] = sizeof(surfvert_t);
613         if (fogenabled)
614         {
615                 m.color = &svert[0].c[0];
616                 m.colorstep = sizeof(surfvert_t);
617                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
618                 {
619                         softwaretransform(v->v, sv->v);
620                         if (r_waterripple.value)
621                                 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
622                         sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
623                         sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
624                         VectorSubtract(sv->v, r_origin, diff);
625                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
626                         sv->c[0] = m.cr * ifog;
627                         sv->c[1] = m.cg * ifog;
628                         sv->c[2] = m.cb * ifog;
629                         sv->c[3] = m.ca;
630                 }
631         }
632         else
633         {
634                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
635                 {
636                         softwaretransform(v->v, sv->v);
637                         if (r_waterripple.value)
638                                 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
639                         sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
640                         sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
641                 }
642         }
643         R_Mesh_Draw(&m);
644 }
645
646 static void RSurfShader_Water_Pass_Fog(msurface_t *s)
647 {
648         int                             i;
649         float                   alpha;
650         surfvertex_t    *v;
651         surfvert_t              *sv;
652         rmeshinfo_t             m;
653         vec3_t                  diff;
654         alpha = s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value;
655
656         memset(&m, 0, sizeof(m));
657         m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
658         m.blendfunc1 = GL_SRC_ALPHA;
659         m.blendfunc2 = GL_ONE;
660         m.numtriangles = s->mesh.numtriangles;
661         m.numverts = s->mesh.numverts;
662         m.index = s->mesh.index;
663         m.vertex = &svert[0].v[0];
664         m.vertexstep = sizeof(surfvert_t);
665         m.color = &svert[0].c[0];
666         m.colorstep = sizeof(surfvert_t);
667         m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
668         m.texcoords[0] = &svert[0].st[0];
669         m.texcoordstep[0] = sizeof(surfvert_t);
670
671         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
672         {
673                 softwaretransform(v->v, sv->v);
674                 if (r_waterripple.value)
675                         sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
676                 if (m.tex[0])
677                 {
678                         sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
679                         sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
680                 }
681                 VectorSubtract(sv->v, r_origin, diff);
682                 sv->c[0] = fogcolor[0];
683                 sv->c[1] = fogcolor[1];
684                 sv->c[2] = fogcolor[2];
685                 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
686         }
687         R_Mesh_Draw(&m);
688 }
689
690 static int RSurfShader_Water(int stage, msurface_t *s)
691 {
692         switch(stage)
693         {
694         case 0:
695                 RSurfShader_Water_Pass_Base(s);
696                 return false;
697         case 1:
698                 if (s->currenttexture->glowtexture)
699                         RSurfShader_Water_Pass_Glow(s);
700                 return false;
701         case 2:
702                 if (fogenabled)
703                 {
704                         RSurfShader_Water_Pass_Fog(s);
705                         return false;
706                 }
707                 else
708                         return true;
709         default:
710                 return true;
711         }
712 }
713
714 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *s)
715 {
716         int                             i;
717         float                   diff[3], ifog;
718         surfvertex_t    *v;
719         surfvert_t              *sv;
720         rmeshinfo_t             m;
721
722         memset(&m, 0, sizeof(m));
723         if (currentrenderentity->effects & EF_ADDITIVE)
724         {
725                 m.transparent = true;
726                 m.blendfunc1 = GL_SRC_ALPHA;
727                 m.blendfunc2 = GL_ONE;
728         }
729         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1))
730         {
731                 m.transparent = true;
732                 m.blendfunc1 = GL_SRC_ALPHA;
733                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
734         }
735         else
736         {
737                 m.transparent = false;
738                 m.blendfunc1 = GL_ONE;
739                 m.blendfunc2 = GL_ZERO;
740         }
741         m.numtriangles = s->mesh.numtriangles;
742         m.numverts = s->mesh.numverts;
743         m.index = s->mesh.index;
744         m.cr = 1;
745         if (lighthalf)
746                 m.cr *= 2;
747         if (gl_combine.integer)
748                 m.cr *= 4;
749         m.cg = m.cr;
750         m.cb = m.cr;
751         m.ca = 1;
752         m.tex[0] = R_GetTexture(s->currenttexture->texture);
753         m.tex[1] = R_GetTexture(s->lightmaptexture);
754         m.texcoords[0] = &s->mesh.vertex->st[0];
755         m.texcoords[1] = &s->mesh.vertex->uv[0];
756         m.texcoordstep[0] = sizeof(surfvertex_t);
757         m.texcoordstep[1] = sizeof(surfvertex_t);
758         if (fogenabled)
759         {
760                 m.color = &svert[0].c[0];
761                 m.colorstep = sizeof(surfvert_t);
762                 if (softwaretransform_complexity)
763                 {
764                         m.vertex = &svert[0].v[0];
765                         m.vertexstep = sizeof(surfvert_t);
766                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
767                         {
768                                 softwaretransform(v->v, sv->v);
769                                 VectorSubtract(sv->v, r_origin, diff);
770                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
771                                 sv->c[0] = m.cr * ifog;
772                                 sv->c[1] = m.cg * ifog;
773                                 sv->c[2] = m.cb * ifog;
774                                 sv->c[3] = m.ca;
775                         }
776                 }
777                 else
778                 {
779                         m.vertex = &s->mesh.vertex->v[0];
780                         m.vertexstep = sizeof(surfvertex_t);
781                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
782                         {
783                                 VectorSubtract(v->v, r_origin, diff);
784                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
785                                 sv->c[0] = m.cr * ifog;
786                                 sv->c[1] = m.cg * ifog;
787                                 sv->c[2] = m.cb * ifog;
788                                 sv->c[3] = m.ca;
789                         }
790                 }
791         }
792         else
793         {
794                 if (softwaretransform_complexity)
795                 {
796                         m.vertex = &svert[0].v[0];
797                         m.vertexstep = sizeof(surfvert_t);
798                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
799                                 softwaretransform(v->v, sv->v);
800                 }
801                 else
802                 {
803                         m.vertex = &s->mesh.vertex->v[0];
804                         m.vertexstep = sizeof(surfvertex_t);
805                 }
806         }
807         R_Mesh_Draw(&m);
808 }
809
810 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
811 {
812         int                             i;
813         surfvertex_t    *v;
814         surfvert_t              *sv;
815         rmeshinfo_t             m;
816
817         memset(&m, 0, sizeof(m));
818         m.transparent = false;
819         m.blendfunc1 = GL_ONE;
820         m.blendfunc2 = GL_ZERO;
821         m.numtriangles = s->mesh.numtriangles;
822         m.numverts = s->mesh.numverts;
823         m.index = s->mesh.index;
824         if (lighthalf)
825         {
826                 m.cr = 2;
827                 m.cg = 2;
828                 m.cb = 2;
829         }
830         else
831         {
832                 m.cr = 1;
833                 m.cg = 1;
834                 m.cb = 1;
835         }
836         m.ca = 1;
837         m.tex[0] = R_GetTexture(s->currenttexture->texture);
838         m.texcoords[0] = &s->mesh.vertex->st[0];
839         m.texcoordstep[0] = sizeof(surfvertex_t);
840         if (softwaretransform_complexity)
841         {
842                 m.vertex = &svert[0].v[0];
843                 m.vertexstep = sizeof(surfvert_t);
844                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
845                         softwaretransform(v->v, sv->v);
846         }
847         else
848         {
849                 m.vertex = &s->mesh.vertex->v[0];
850                 m.vertexstep = sizeof(surfvertex_t);
851         }
852         R_Mesh_Draw(&m);
853 }
854
855 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
856 {
857         int                             i;
858         float                   diff[3], ifog;
859         surfvertex_t    *v;
860         surfvert_t              *sv;
861         rmeshinfo_t             m;
862
863         memset(&m, 0, sizeof(m));
864         m.transparent = false;
865         m.blendfunc1 = GL_ZERO;
866         m.blendfunc2 = GL_SRC_COLOR;
867         m.numtriangles = s->mesh.numtriangles;
868         m.numverts = s->mesh.numverts;
869         m.index = s->mesh.index;
870         m.cr = 1;
871         if (lighthalf)
872                 m.cr *= 2.0f;
873         m.cg = m.cr;
874         m.cb = m.cr;
875         m.ca = 1;
876         m.tex[0] = R_GetTexture(s->lightmaptexture);
877         m.texcoords[0] = &s->mesh.vertex->uv[0];
878         m.texcoordstep[0] = sizeof(surfvertex_t);
879         if (fogenabled)
880         {
881                 m.color = &svert[0].c[0];
882                 m.colorstep = sizeof(surfvert_t);
883                 if (softwaretransform_complexity)
884                 {
885                         m.vertex = &svert[0].v[0];
886                         m.vertexstep = sizeof(surfvert_t);
887                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
888                         {
889                                 softwaretransform(v->v, sv->v);
890                                 VectorSubtract(sv->v, r_origin, diff);
891                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
892                                 sv->c[0] = m.cr * ifog;
893                                 sv->c[1] = m.cg * ifog;
894                                 sv->c[2] = m.cb * ifog;
895                                 sv->c[3] = m.ca;
896                         }
897                 }
898                 else
899                 {
900                         m.vertex = &s->mesh.vertex->v[0];
901                         m.vertexstep = sizeof(surfvertex_t);
902                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
903                         {
904                                 VectorSubtract(v->v, r_origin, diff);
905                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
906                                 sv->c[0] = m.cr * ifog;
907                                 sv->c[1] = m.cg * ifog;
908                                 sv->c[2] = m.cb * ifog;
909                                 sv->c[3] = m.ca;
910                         }
911                 }
912         }
913         else
914         {
915                 if (softwaretransform_complexity)
916                 {
917                         m.vertex = &svert[0].v[0];
918                         m.vertexstep = sizeof(surfvert_t);
919                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
920                                 softwaretransform(v->v, sv->v);
921                 }
922                 else
923                 {
924                         m.vertex = &s->mesh.vertex->v[0];
925                         m.vertexstep = sizeof(surfvertex_t);
926                 }
927         }
928         R_Mesh_Draw(&m);
929 }
930
931 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
932 {
933         int                             i, size3;
934         float                   c[3], base[3], scale, diff[3], ifog;
935         surfvertex_t    *v;
936         surfvert_t              *sv;
937         rmeshinfo_t             m;
938         byte                    *lm;
939
940         size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
941
942         base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
943
944         memset(&m, 0, sizeof(m));
945         if (currentrenderentity->effects & EF_ADDITIVE)
946         {
947                 m.transparent = true;
948                 m.blendfunc1 = GL_SRC_ALPHA;
949                 m.blendfunc2 = GL_ONE;
950         }
951         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1))
952         {
953                 m.transparent = true;
954                 m.blendfunc1 = GL_SRC_ALPHA;
955                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
956         }
957         else
958         {
959                 m.transparent = false;
960                 m.blendfunc1 = GL_ONE;
961                 m.blendfunc2 = GL_ZERO;
962         }
963         m.numtriangles = s->mesh.numtriangles;
964         m.numverts = s->mesh.numverts;
965         m.index = s->mesh.index;
966         m.vertex = &svert[0].v[0];
967         m.vertexstep = sizeof(surfvert_t);
968         m.color = &svert[0].c[0];
969         m.colorstep = sizeof(surfvert_t);
970         m.tex[0] = R_GetTexture(s->currenttexture->texture);
971         m.texcoords[0] = &s->mesh.vertex->st[0];
972         m.texcoordstep[0] = sizeof(surfvertex_t);
973         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
974         {
975                 softwaretransform(v->v, sv->v);
976                 VectorCopy(base, c);
977                 if (s->styles[0] != 255)
978                 {
979                         lm = s->samples + v->lightmapoffset;
980                         scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
981                         VectorMA(c, scale, lm, c);
982                         if (s->styles[1] != 255)
983                         {
984                                 lm += size3;
985                                 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
986                                 VectorMA(c, scale, lm, c);
987                                 if (s->styles[2] != 255)
988                                 {
989                                         lm += size3;
990                                         scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
991                                         VectorMA(c, scale, lm, c);
992                                         if (s->styles[3] != 255)
993                                         {
994                                                 lm += size3;
995                                                 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
996                                                 VectorMA(c, scale, lm, c);
997                                         }
998                                 }
999                         }
1000                 }
1001                 sv->c[0] = c[0];
1002                 sv->c[1] = c[1];
1003                 sv->c[2] = c[2];
1004                 sv->c[3] = currentrenderentity->alpha;
1005         }
1006         if (s->dlightframe == r_framecount)
1007                 RSurf_Light(s->dlightbits, m.numverts);
1008         if (fogenabled)
1009         {
1010                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1011                 {
1012                         VectorSubtract(sv->v, r_origin, diff);
1013                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1014                         sv->c[0] *= ifog;
1015                         sv->c[1] *= ifog;
1016                         sv->c[2] *= ifog;
1017                 }
1018         }
1019         R_Mesh_Draw(&m);
1020 }
1021
1022 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1023 {
1024         int                             i;
1025         float                   diff[3], ifog;
1026         surfvertex_t    *v;
1027         surfvert_t              *sv;
1028         rmeshinfo_t             m;
1029
1030         memset(&m, 0, sizeof(m));
1031         if (currentrenderentity->effects & EF_ADDITIVE)
1032                 m.transparent = true;
1033         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1))
1034                 m.transparent = true;
1035         else
1036                 m.transparent = false;
1037         m.blendfunc1 = GL_SRC_ALPHA;
1038         m.blendfunc2 = GL_ONE;
1039         m.numtriangles = s->mesh.numtriangles;
1040         m.numverts = s->mesh.numverts;
1041         m.index = s->mesh.index;
1042         m.vertex = &svert[0].v[0];
1043         m.vertexstep = sizeof(surfvert_t);
1044         m.color = &svert[0].c[0];
1045         m.colorstep = sizeof(surfvert_t);
1046         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1047         m.texcoords[0] = &s->mesh.vertex->st[0];
1048         m.texcoordstep[0] = sizeof(surfvertex_t);
1049         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1050         {
1051                 softwaretransform(v->v, sv->v);
1052                 sv->c[0] = 0;
1053                 sv->c[1] = 0;
1054                 sv->c[2] = 0;
1055                 sv->c[3] = currentrenderentity->alpha;
1056         }
1057         if (RSurf_Light(s->dlightbits, m.numverts))
1058         {
1059                 if (fogenabled)
1060                 {
1061                         for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1062                         {
1063                                 VectorSubtract(sv->v, r_origin, diff);
1064                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1065                                 sv->c[0] *= ifog;
1066                                 sv->c[1] *= ifog;
1067                                 sv->c[2] *= ifog;
1068                         }
1069                 }
1070                 R_Mesh_Draw(&m);
1071         }
1072 }
1073
1074 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1075 {
1076         int                             i;
1077         float                   diff[3], ifog;
1078         surfvertex_t    *v;
1079         surfvert_t              *sv;
1080         rmeshinfo_t             m;
1081
1082         memset(&m, 0, sizeof(m));
1083         if (currentrenderentity->effects & EF_ADDITIVE)
1084                 m.transparent = true;
1085         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1))
1086                 m.transparent = true;
1087         else
1088                 m.transparent = false;
1089         m.blendfunc1 = GL_SRC_ALPHA;
1090         m.blendfunc2 = GL_ONE;
1091         m.numtriangles = s->mesh.numtriangles;
1092         m.numverts = s->mesh.numverts;
1093         m.index = s->mesh.index;
1094         m.cr = 1;
1095         m.cg = 1;
1096         m.cb = 1;
1097         m.ca = currentrenderentity->alpha;
1098         m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1099         m.texcoords[0] = &s->mesh.vertex->st[0];
1100         m.texcoordstep[0] = sizeof(surfvertex_t);
1101         if (fogenabled)
1102         {
1103                 m.color = &svert[0].c[0];
1104                 m.colorstep = sizeof(surfvert_t);
1105                 if (softwaretransform_complexity)
1106                 {
1107                         m.vertex = &svert[0].v[0];
1108                         m.vertexstep = sizeof(surfvert_t);
1109                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1110                         {
1111                                 softwaretransform(v->v, sv->v);
1112                                 VectorSubtract(sv->v, r_origin, diff);
1113                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1114                                 sv->c[0] = m.cr * ifog;
1115                                 sv->c[1] = m.cg * ifog;
1116                                 sv->c[2] = m.cb * ifog;
1117                                 sv->c[3] = m.ca;
1118                         }
1119                 }
1120                 else
1121                 {
1122                         m.vertex = &s->mesh.vertex->v[0];
1123                         m.vertexstep = sizeof(surfvertex_t);
1124                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1125                         {
1126                                 VectorSubtract(v->v, r_origin, diff);
1127                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1128                                 sv->c[0] = m.cr * ifog;
1129                                 sv->c[1] = m.cg * ifog;
1130                                 sv->c[2] = m.cb * ifog;
1131                                 sv->c[3] = m.ca;
1132                         }
1133                 }
1134         }
1135         else
1136         {
1137                 if (softwaretransform_complexity)
1138                 {
1139                         m.vertex = &svert[0].v[0];
1140                         m.vertexstep = sizeof(surfvert_t);
1141                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1142                                 softwaretransform(v->v, sv->v);
1143                 }
1144                 else
1145                 {
1146                         m.vertex = &s->mesh.vertex->v[0];
1147                         m.vertexstep = sizeof(surfvertex_t);
1148                 }
1149         }
1150         R_Mesh_Draw(&m);
1151 }
1152
1153 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1154 {
1155         int                             i;
1156         surfvertex_t    *v;
1157         surfvert_t              *sv;
1158         rmeshinfo_t             m;
1159         vec3_t                  diff;
1160
1161         memset(&m, 0, sizeof(m));
1162         if (currentrenderentity->effects & EF_ADDITIVE)
1163                 m.transparent = true;
1164         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1))
1165                 m.transparent = true;
1166         else
1167                 m.transparent = false;
1168         m.blendfunc1 = GL_SRC_ALPHA;
1169         m.blendfunc2 = GL_ONE;
1170         m.numtriangles = s->mesh.numtriangles;
1171         m.numverts = s->mesh.numverts;
1172         m.index = s->mesh.index;
1173         m.color = &svert[0].c[0];
1174         m.colorstep = sizeof(surfvert_t);
1175         m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1176         m.texcoords[0] = &s->mesh.vertex->st[0];
1177         m.texcoordstep[0] = sizeof(surfvertex_t);
1178         if (softwaretransform_complexity)
1179         {
1180                 m.vertex = &svert[0].v[0];
1181                 m.vertexstep = sizeof(surfvert_t);
1182                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1183                 {
1184                         softwaretransform(v->v, sv->v);
1185                         VectorSubtract(sv->v, r_origin, diff);
1186                         sv->c[0] = fogcolor[0];
1187                         sv->c[1] = fogcolor[1];
1188                         sv->c[2] = fogcolor[2];
1189                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1190                 }
1191         }
1192         else
1193         {
1194                 m.vertex = &s->mesh.vertex->v[0];
1195                 m.vertexstep = sizeof(surfvertex_t);
1196                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1197                 {
1198                         VectorSubtract(v->v, r_origin, diff);
1199                         sv->c[0] = fogcolor[0];
1200                         sv->c[1] = fogcolor[1];
1201                         sv->c[2] = fogcolor[2];
1202                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1203                 }
1204         }
1205         R_Mesh_Draw(&m);
1206 }
1207
1208 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1209 {
1210         switch(stage)
1211         {
1212         case 0:
1213                 RSurfShader_Wall_Pass_BaseVertex(s);
1214                 return false;
1215         case 1:
1216                 if (s->currenttexture->glowtexture)
1217                         RSurfShader_Wall_Pass_Glow(s);
1218                 return false;
1219         default:
1220                 return true;
1221         }
1222 }
1223
1224 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1225 {
1226         if (r_vertexsurfaces.integer)
1227         {
1228                 switch(stage)
1229                 {
1230                 case 0:
1231                         RSurfShader_Wall_Pass_BaseVertex(s);
1232                         return false;
1233                 case 1:
1234                         if (s->currenttexture->glowtexture)
1235                                 RSurfShader_Wall_Pass_Glow(s);
1236                         return false;
1237                 default:
1238                         return true;
1239                 }
1240         }
1241         else if (r_multitexture.integer)
1242         {
1243                 if (r_dlightmap.integer)
1244                 {
1245                         switch(stage)
1246                         {
1247                         case 0:
1248                                 RSurfShader_Wall_Pass_BaseMTex(s);
1249                                 return false;
1250                         case 1:
1251                                 if (s->currenttexture->glowtexture)
1252                                         RSurfShader_Wall_Pass_Glow(s);
1253                                 return false;
1254                         default:
1255                                 return true;
1256                         }
1257                 }
1258                 else
1259                 {
1260                         switch(stage)
1261                         {
1262                         case 0:
1263                                 RSurfShader_Wall_Pass_BaseMTex(s);
1264                                 return false;
1265                         case 1:
1266                                 if (s->dlightframe == r_framecount)
1267                                         RSurfShader_Wall_Pass_Light(s);
1268                                 return false;
1269                         case 2:
1270                                 if (s->currenttexture->glowtexture)
1271                                         RSurfShader_Wall_Pass_Glow(s);
1272                                 return false;
1273                         default:
1274                                 return true;
1275                         }
1276                 }
1277         }
1278         else if (currentrenderentity != &cl_entities[0].render && (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE))
1279         {
1280                 switch(stage)
1281                 {
1282                 case 0:
1283                         RSurfShader_Wall_Pass_BaseVertex(s);
1284                         return false;
1285                 case 1:
1286                         if (s->currenttexture->glowtexture)
1287                                 RSurfShader_Wall_Pass_Glow(s);
1288                         return false;
1289                 default:
1290                         return true;
1291                 }
1292         }
1293         else
1294         {
1295                 if (r_dlightmap.integer)
1296                 {
1297                         switch(stage)
1298                         {
1299                         case 0:
1300                                 RSurfShader_Wall_Pass_BaseTexture(s);
1301                                 return false;
1302                         case 1:
1303                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1304                                 return false;
1305                         case 2:
1306                                 if (s->currenttexture->glowtexture)
1307                                         RSurfShader_Wall_Pass_Glow(s);
1308                                 return false;
1309                         default:
1310                                 return true;
1311                         }
1312                 }
1313                 else
1314                 {
1315                         switch(stage)
1316                         {
1317                         case 0:
1318                                 RSurfShader_Wall_Pass_BaseTexture(s);
1319                                 return false;
1320                         case 1:
1321                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1322                                 return false;
1323                         case 2:
1324                                 if (s->dlightframe == r_framecount)
1325                                         RSurfShader_Wall_Pass_Light(s);
1326                                 return false;
1327                         case 3:
1328                                 if (s->currenttexture->glowtexture)
1329                                         RSurfShader_Wall_Pass_Glow(s);
1330                                 return false;
1331                         default:
1332                                 return true;
1333                         }
1334                 }
1335         }
1336 }
1337
1338 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1339 {
1340         if (stage == 0 && fogenabled)
1341         {
1342                 RSurfShader_Wall_Pass_Fog(s);
1343                 return false;
1344         }
1345         else
1346                 return true;
1347 }
1348
1349 /*
1350 =============================================================
1351
1352         WORLD MODEL
1353
1354 =============================================================
1355 */
1356
1357 static void RSurf_Callback(void *data, void *junk)
1358 {
1359         ((msurface_t *)data)->visframe = r_framecount;
1360 }
1361
1362 static void R_SolidWorldNode (void)
1363 {
1364         if (r_viewleaf->contents != CONTENTS_SOLID)
1365         {
1366                 int portalstack;
1367                 mportal_t *p, *pstack[8192];
1368                 msurface_t *surf, **mark, **endmark;
1369                 mleaf_t *leaf;
1370                 tinyplane_t plane;
1371                 // LordHavoc: portal-passage worldnode; follows portals leading
1372                 // outward from viewleaf, if a portal leads offscreen it is not
1373                 // followed, in indoor maps this can often cull a great deal of
1374                 // geometry away when pvs data is not present (useful with pvs as well)
1375
1376                 leaf = r_viewleaf;
1377                 leaf->worldnodeframe = r_framecount;
1378                 portalstack = 0;
1379         loc0:
1380                 c_leafs++;
1381
1382                 leaf->visframe = r_framecount;
1383
1384                 if (leaf->nummarksurfaces)
1385                 {
1386                         mark = leaf->firstmarksurface;
1387                         endmark = mark + leaf->nummarksurfaces;
1388                         if (r_ser.integer)
1389                         {
1390                                 do
1391                                 {
1392                                         surf = *mark++;
1393                                         // make sure surfaces are only processed once
1394                                         if (surf->worldnodeframe == r_framecount)
1395                                                 continue;
1396                                         surf->worldnodeframe = r_framecount;
1397                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1398                                         {
1399                                                 if (surf->flags & SURF_PLANEBACK)
1400                                                 {
1401                                                         VectorNegate(surf->plane->normal, plane.normal);
1402                                                         plane.dist = -surf->plane->dist;
1403                                                         R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1404                                                 }
1405                                         }
1406                                         else
1407                                         {
1408                                                 if (!(surf->flags & SURF_PLANEBACK))
1409                                                         R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1410                                         }
1411                                 }
1412                                 while (mark < endmark);
1413                         }
1414                         else
1415                         {
1416                                 do
1417                                 {
1418                                         surf = *mark++;
1419                                         // make sure surfaces are only processed once
1420                                         if (surf->worldnodeframe == r_framecount)
1421                                                 continue;
1422                                         surf->worldnodeframe = r_framecount;
1423                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1424                                         {
1425                                                 if (surf->flags & SURF_PLANEBACK)
1426                                                         surf->visframe = r_framecount;
1427                                         }
1428                                         else
1429                                         {
1430                                                 if (!(surf->flags & SURF_PLANEBACK))
1431                                                         surf->visframe = r_framecount;
1432                                         }
1433                                 }
1434                                 while (mark < endmark);
1435                         }
1436                 }
1437
1438                 // follow portals into other leafs
1439                 p = leaf->portals;
1440                 for (;p;p = p->next)
1441                 {
1442                         if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1443                         {
1444                                 leaf = p->past;
1445                                 if (leaf->worldnodeframe != r_framecount)
1446                                 {
1447                                         leaf->worldnodeframe = r_framecount;
1448                                         if (leaf->contents != CONTENTS_SOLID)
1449                                         {
1450                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1451                                                 {
1452                                                         p->visframe = r_framecount;
1453                                                         pstack[portalstack++] = p;
1454                                                         goto loc0;
1455
1456         loc1:
1457                                                         p = pstack[--portalstack];
1458                                                 }
1459                                         }
1460                                 }
1461                         }
1462                 }
1463
1464                 if (portalstack)
1465                         goto loc1;
1466         }
1467         else
1468         {
1469                 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1470                 int nodestackpos = 0;
1471                 // LordHavoc: recursive descending worldnode; if portals are not
1472                 // available, this is a good last resort, can cull large amounts of
1473                 // geometry, but is more time consuming than portal-passage and renders
1474                 // things behind walls
1475
1476 loc2:
1477                 if (R_NotCulledBox(node->mins, node->maxs))
1478                 {
1479                         if (node->numsurfaces)
1480                         {
1481                                 if (r_ser.integer)
1482                                 {
1483                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1484                                         tinyplane_t plane;
1485                                         if (PlaneDiff (r_origin, node->plane) < 0)
1486                                         {
1487                                                 for (;surf < surfend;surf++)
1488                                                 {
1489                                                         if (surf->flags & SURF_PLANEBACK)
1490                                                         {
1491                                                                 VectorNegate(surf->plane->normal, plane.normal);
1492                                                                 plane.dist = -surf->plane->dist;
1493                                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1494                                                         }
1495                                                 }
1496                                         }
1497                                         else
1498                                         {
1499                                                 for (;surf < surfend;surf++)
1500                                                 {
1501                                                         if (!(surf->flags & SURF_PLANEBACK))
1502                                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1503                                                 }
1504                                         }
1505                                 }
1506                                 else
1507                                 {
1508                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1509                                         if (PlaneDiff (r_origin, node->plane) < 0)
1510                                         {
1511                                                 for (;surf < surfend;surf++)
1512                                                 {
1513                                                         if (surf->flags & SURF_PLANEBACK)
1514                                                                 surf->visframe = r_framecount;
1515                                                 }
1516                                         }
1517                                         else
1518                                         {
1519                                                 for (;surf < surfend;surf++)
1520                                                 {
1521                                                         if (!(surf->flags & SURF_PLANEBACK))
1522                                                                 surf->visframe = r_framecount;
1523                                                 }
1524                                         }
1525                                 }
1526                         }
1527
1528                         // recurse down the children
1529                         if (node->children[0]->contents >= 0)
1530                         {
1531                                 if (node->children[1]->contents >= 0)
1532                                 {
1533                                         if (nodestackpos < 8192)
1534                                                 nodestack[nodestackpos++] = node->children[1];
1535                                         node = node->children[0];
1536                                         goto loc2;
1537                                 }
1538                                 else
1539                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1540                                 node = node->children[0];
1541                                 goto loc2;
1542                         }
1543                         else
1544                         {
1545                                 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1546                                 if (node->children[1]->contents >= 0)
1547                                 {
1548                                         node = node->children[1];
1549                                         goto loc2;
1550                                 }
1551                                 else if (nodestackpos > 0)
1552                                 {
1553                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1554                                         node = nodestack[--nodestackpos];
1555                                         goto loc2;
1556                                 }
1557                         }
1558                 }
1559                 else if (nodestackpos > 0)
1560                 {
1561                         node = nodestack[--nodestackpos];
1562                         goto loc2;
1563                 }
1564         }
1565 }
1566
1567 static int r_portalframecount = 0;
1568
1569 static void R_PVSWorldNode()
1570 {
1571         int portalstack, i;
1572         mportal_t *p, *pstack[8192];
1573         msurface_t *surf, **mark, **endmark;
1574         mleaf_t *leaf;
1575         tinyplane_t plane;
1576         byte *worldvis;
1577
1578         worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1579
1580         leaf = r_viewleaf;
1581         leaf->worldnodeframe = r_framecount;
1582         portalstack = 0;
1583 loc0:
1584         c_leafs++;
1585
1586         leaf->visframe = r_framecount;
1587
1588         if (leaf->nummarksurfaces)
1589         {
1590                 mark = leaf->firstmarksurface;
1591                 endmark = mark + leaf->nummarksurfaces;
1592                 if (r_ser.integer)
1593                 {
1594                         do
1595                         {
1596                                 surf = *mark++;
1597                                 // make sure surfaces are only processed once
1598                                 if (surf->worldnodeframe == r_framecount)
1599                                         continue;
1600                                 surf->worldnodeframe = r_framecount;
1601                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1602                                 {
1603                                         if (surf->flags & SURF_PLANEBACK)
1604                                         {
1605                                                 VectorNegate(surf->plane->normal, plane.normal);
1606                                                 plane.dist = -surf->plane->dist;
1607                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1608                                         }
1609                                 }
1610                                 else
1611                                 {
1612                                         if (!(surf->flags & SURF_PLANEBACK))
1613                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1614                                 }
1615                         }
1616                         while (mark < endmark);
1617                 }
1618                 else
1619                 {
1620                         do
1621                         {
1622                                 surf = *mark++;
1623                                 // make sure surfaces are only processed once
1624                                 if (surf->worldnodeframe == r_framecount)
1625                                         continue;
1626                                 surf->worldnodeframe = r_framecount;
1627                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1628                                 {
1629                                         if (surf->flags & SURF_PLANEBACK)
1630                                                 surf->visframe = r_framecount;
1631                                 }
1632                                 else
1633                                 {
1634                                         if (!(surf->flags & SURF_PLANEBACK))
1635                                                 surf->visframe = r_framecount;
1636                                 }
1637                         }
1638                         while (mark < endmark);
1639                 }
1640         }
1641
1642         // follow portals into other leafs
1643         for (p = leaf->portals;p;p = p->next)
1644         {
1645                 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1646                 {
1647                         leaf = p->past;
1648                         if (leaf->worldnodeframe != r_framecount)
1649                         {
1650                                 leaf->worldnodeframe = r_framecount;
1651                                 if (leaf->contents != CONTENTS_SOLID)
1652                                 {
1653                                         i = (leaf - cl.worldmodel->leafs) - 1;
1654                                         if (worldvis[i>>3] & (1<<(i&7)))
1655                                         {
1656                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1657                                                 {
1658                                                         pstack[portalstack++] = p;
1659                                                         goto loc0;
1660
1661 loc1:
1662                                                         p = pstack[--portalstack];
1663                                                 }
1664                                         }
1665                                 }
1666                         }
1667                 }
1668         }
1669
1670         if (portalstack)
1671                 goto loc1;
1672 }
1673
1674 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1675 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1676 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1677 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1678
1679 int Cshader_count = 4;
1680 Cshader_t *Cshaders[4] =
1681 {
1682         &Cshader_wall_vertex,
1683         &Cshader_wall_lightmap,
1684         &Cshader_water,
1685         &Cshader_sky
1686 };
1687
1688 void R_PrepareSurfaces(void)
1689 {
1690         int i;
1691         texture_t *t;
1692         model_t *model;
1693         msurface_t *surf;
1694
1695         for (i = 0;i < Cshader_count;i++)
1696                 Cshaders[i]->chain = NULL;
1697
1698         model = currentrenderentity->model;
1699
1700         for (i = 0;i < model->nummodelsurfaces;i++)
1701         {
1702                 surf = model->modelsortedsurfaces[i];
1703                 if (surf->visframe == r_framecount)
1704                 {
1705                         if (surf->insertframe != r_framecount)
1706                         {
1707                                 surf->insertframe = r_framecount;
1708                                 c_faces++;
1709                                 // manually inlined R_TextureAnimation
1710                                 //t = R_TextureAnimation(surf->texinfo->texture);
1711                                 t = surf->texinfo->texture;
1712                                 if (t->alternate_anims != NULL && currentrenderentity->frame)
1713                                         t = t->alternate_anims;
1714                                 if (t->anim_total >= 2)
1715                                         t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1716                                 surf->currenttexture = t;
1717                         }
1718
1719                         surf->chain = surf->shader->chain;
1720                         surf->shader->chain = surf;
1721                 }
1722         }
1723 }
1724
1725 void R_DrawSurfaces (int type)
1726 {
1727         int                     i, stage;
1728         msurface_t      *surf;
1729         Cshader_t       *shader;
1730
1731         for (i = 0;i < Cshader_count;i++)
1732         {
1733                 shader = Cshaders[i];
1734                 if (shader->chain && shader->shaderfunc[type])
1735                         for (stage = 0;stage < 1000;stage++)
1736                                 for (surf = shader->chain;surf;surf = surf->chain)
1737                                         if (shader->shaderfunc[type](stage, surf))
1738                                                 goto done;
1739 done:;
1740         }
1741 }
1742
1743 void R_DrawSurfacesAll (void)
1744 {
1745         R_DrawSurfaces(SHADERSTAGE_SKY);
1746         R_DrawSurfaces(SHADERSTAGE_NORMAL);
1747         R_DrawSurfaces(SHADERSTAGE_FOG);
1748 }
1749
1750 static float portalpointbuffer[256][3];
1751
1752 void R_DrawPortals(void)
1753 {
1754         int drawportals, i;
1755 //      mleaf_t *leaf, *endleaf;
1756         mportal_t *portal, *endportal;
1757         mvertex_t *point/*, *endpoint*/;
1758         rmeshinfo_t m;
1759         drawportals = r_drawportals.integer;
1760         if (drawportals < 1)
1761                 return;
1762         /*
1763         leaf = cl.worldmodel->leafs;
1764         endleaf = leaf + cl.worldmodel->numleafs;
1765         for (;leaf < endleaf;leaf++)
1766         {
1767                 if (leaf->visframe == r_framecount && leaf->portals)
1768                 {
1769                         i = leaf - cl.worldmodel->leafs;
1770                         r = (i & 0x0007) << 5;
1771                         g = (i & 0x0038) << 2;
1772                         b = (i & 0x01C0) >> 1;
1773                         portal = leaf->portals;
1774                         while (portal)
1775                         {
1776                                 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1777                                 point = portal->points + portal->numpoints - 1;
1778                                 endpoint = portal->points;
1779                                 for (;point >= endpoint;point--)
1780                                         transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1781                                 transpolyend();
1782                                 portal = portal->next;
1783                         }
1784                 }
1785         }
1786         */
1787         memset(&m, 0, sizeof(m));
1788         m.transparent = true;
1789         m.blendfunc1 = GL_SRC_ALPHA;
1790         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1791         m.vertex = &portalpointbuffer[0][0];
1792         m.vertexstep = sizeof(float[3]);
1793         m.ca = 0.125;
1794         for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1795         {
1796                 if (portal->visframe == r_portalframecount)
1797                 {
1798                         if (portal->numpoints <= 256)
1799                         {
1800                                 i = portal - cl.worldmodel->portals;
1801                                 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
1802                                 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
1803                                 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
1804                                 point = portal->points;
1805                                 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1806                                 {
1807                                         for (i = portal->numpoints - 1;i >= 0;i--)
1808                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
1809                                 }
1810                                 else
1811                                 {
1812                                         for (i = 0;i < portal->numpoints;i++)
1813                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
1814                                 }
1815                                 R_Mesh_DrawPolygon(&m, portal->numpoints);
1816                         }
1817                 }
1818         }
1819 }
1820
1821 void R_SetupForBModelRendering(void)
1822 {
1823         int                     i;
1824         msurface_t      *s;
1825         model_t         *model;
1826         vec3_t          modelorg;
1827
1828         // because bmodels can be reused, we have to decide which things to render
1829         // from scratch every time
1830
1831         model = currentrenderentity->model;
1832
1833         softwaretransformforentity (currentrenderentity);
1834         softwareuntransform(r_origin, modelorg);
1835
1836         for (i = 0;i < model->nummodelsurfaces;i++)
1837         {
1838                 s = model->modelsortedsurfaces[i];
1839                 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
1840                         s->visframe = r_framecount;
1841                 else
1842                         s->visframe = -1;
1843                 s->worldnodeframe = -1;
1844                 s->lightframe = -1;
1845                 s->dlightframe = -1;
1846                 s->insertframe = -1;
1847         }
1848 }
1849
1850 void R_SetupForWorldRendering(void)
1851 {
1852         // there is only one instance of the world, but it can be rendered in
1853         // multiple stages
1854
1855         currentrenderentity = &cl_entities[0].render;
1856         softwaretransformidentity();
1857 }
1858
1859 static void R_SurfMarkLights (void)
1860 {
1861         int                     i;
1862         msurface_t      *s;
1863
1864         if (r_dynamic.integer)
1865                 R_MarkLights();
1866
1867         if (!r_vertexsurfaces.integer)
1868         {
1869                 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
1870                 {
1871                         s = currentrenderentity->model->modelsortedsurfaces[i];
1872                         if (s->visframe == r_framecount && s->lightmaptexture != NULL)
1873                         {
1874                                 if (s->cached_dlight
1875                                  || s->cached_ambient != r_ambient.value
1876                                  || s->cached_lightscalebit != lightscalebit)
1877                                         R_BuildLightMap(s, false); // base lighting changed
1878                                 else if (r_dynamic.integer)
1879                                 {
1880                                         if  (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
1881                                          || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
1882                                          || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
1883                                          || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
1884                                         //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
1885                                         // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
1886                                         // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
1887                                         // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
1888                                                 R_BuildLightMap(s, false); // base lighting changed
1889                                         else if (s->dlightframe == r_framecount && r_dlightmap.integer)
1890                                                 R_BuildLightMap(s, true); // only dlights
1891                                 }
1892                         }
1893                 }
1894         }
1895 }
1896
1897 void R_MarkWorldLights(void)
1898 {
1899         R_SetupForWorldRendering();
1900         R_SurfMarkLights();
1901 }
1902
1903 /*
1904 =============
1905 R_DrawWorld
1906 =============
1907 */
1908 void R_DrawWorld (void)
1909 {
1910         R_SetupForWorldRendering();
1911
1912         if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
1913                 R_SolidWorldNode ();
1914         else
1915                 R_PVSWorldNode ();
1916 }
1917
1918 /*
1919 =================
1920 R_DrawBrushModel
1921 =================
1922 */
1923 void R_DrawBrushModelSky (void)
1924 {
1925         R_SetupForBModelRendering();
1926
1927         R_PrepareSurfaces();
1928         R_DrawSurfaces(SHADERSTAGE_SKY);
1929 }
1930
1931 void R_DrawBrushModelNormal (void)
1932 {
1933         c_bmodels++;
1934
1935         // have to flush queue because of possible lightmap reuse
1936         R_Mesh_Render();
1937
1938         R_SetupForBModelRendering();
1939
1940         R_SurfMarkLights();
1941
1942         R_PrepareSurfaces();
1943
1944         if (!skyrendermasked)
1945                 R_DrawSurfaces(SHADERSTAGE_SKY);
1946         R_DrawSurfaces(SHADERSTAGE_NORMAL);
1947         R_DrawSurfaces(SHADERSTAGE_FOG);
1948 }