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