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