added in_pitch_min and in_pitch_max cvars to limit pitch (default: -90 to +90)
[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 byte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
29
30 cvar_t r_ambient = {0, "r_ambient", "0"};
31 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
32 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
35
36 static void gl_surf_start(void)
37 {
38 }
39
40 static void gl_surf_shutdown(void)
41 {
42 }
43
44 static void gl_surf_newmap(void)
45 {
46 }
47
48 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         byte *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] = (byte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
266                                                                                 bl[1] = (byte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
267                                                                                 bl[2] = (byte) ((((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         byte    *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/* && m.blendfunc2 == GL_ONE_MINUS_SRC_ALPHA*/)
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 && (s->flags & SURF_DRAWNOALPHA))
882                 {
883                         RSurfShader_Water_Pass_Fog(s);
884                         return false;
885                 }
886                 else
887                         return true;
888         default:
889                 return true;
890         }
891 }
892
893 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *s)
894 {
895         int                             i;
896         float                   diff[3], ifog;
897         surfvertex_t    *v;
898         surfvert_t              *sv;
899         rmeshinfo_t             m;
900
901         memset(&m, 0, sizeof(m));
902         if (currentrenderentity->effects & EF_ADDITIVE)
903         {
904                 m.transparent = true;
905                 m.blendfunc1 = GL_SRC_ALPHA;
906                 m.blendfunc2 = GL_ONE;
907         }
908         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
909         {
910                 m.transparent = true;
911                 m.blendfunc1 = GL_SRC_ALPHA;
912                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
913         }
914         else
915         {
916                 m.transparent = false;
917                 m.blendfunc1 = GL_ONE;
918                 m.blendfunc2 = GL_ZERO;
919         }
920         m.numtriangles = s->mesh.numtriangles;
921         m.numverts = s->mesh.numverts;
922         m.index = s->mesh.index;
923         m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
924         m.ca = currentrenderentity->alpha;
925         m.tex[0] = R_GetTexture(s->currenttexture->texture);
926         m.tex[1] = R_GetTexture(s->lightmaptexture);
927         m.texcoords[0] = &s->mesh.vertex->st[0];
928         m.texcoords[1] = &s->mesh.vertex->uv[0];
929         m.texcoordstep[0] = sizeof(surfvertex_t);
930         m.texcoordstep[1] = sizeof(surfvertex_t);
931         if (fogenabled)
932         {
933                 m.color = &svert[0].c[0];
934                 m.colorstep = sizeof(surfvert_t);
935                 if (softwaretransform_complexity)
936                 {
937                         m.vertex = &svert[0].v[0];
938                         m.vertexstep = sizeof(surfvert_t);
939                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
940                         {
941                                 softwaretransform(v->v, sv->v);
942                                 VectorSubtract(sv->v, r_origin, diff);
943                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
944                                 sv->c[0] = m.cr * ifog;
945                                 sv->c[1] = m.cg * ifog;
946                                 sv->c[2] = m.cb * ifog;
947                                 sv->c[3] = m.ca;
948                         }
949                 }
950                 else
951                 {
952                         m.vertex = &s->mesh.vertex->v[0];
953                         m.vertexstep = sizeof(surfvertex_t);
954                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
955                         {
956                                 VectorSubtract(v->v, r_origin, diff);
957                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
958                                 sv->c[0] = m.cr * ifog;
959                                 sv->c[1] = m.cg * ifog;
960                                 sv->c[2] = m.cb * ifog;
961                                 sv->c[3] = m.ca;
962                         }
963                 }
964         }
965         else
966         {
967                 if (softwaretransform_complexity)
968                 {
969                         m.vertex = &svert[0].v[0];
970                         m.vertexstep = sizeof(surfvert_t);
971                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
972                                 softwaretransform(v->v, sv->v);
973                 }
974                 else
975                 {
976                         m.vertex = &s->mesh.vertex->v[0];
977                         m.vertexstep = sizeof(surfvertex_t);
978                 }
979         }
980         R_Mesh_Draw(&m);
981 }
982
983 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
984 {
985         int                             i;
986         surfvertex_t    *v;
987         surfvert_t              *sv;
988         rmeshinfo_t             m;
989
990         memset(&m, 0, sizeof(m));
991         m.transparent = false;
992         m.blendfunc1 = GL_ONE;
993         m.blendfunc2 = GL_ZERO;
994         m.numtriangles = s->mesh.numtriangles;
995         m.numverts = s->mesh.numverts;
996         m.index = s->mesh.index;
997         m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
998         m.ca = 1;
999         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1000         m.texcoords[0] = &s->mesh.vertex->st[0];
1001         m.texcoordstep[0] = sizeof(surfvertex_t);
1002         if (softwaretransform_complexity)
1003         {
1004                 m.vertex = &svert[0].v[0];
1005                 m.vertexstep = sizeof(surfvert_t);
1006                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1007                         softwaretransform(v->v, sv->v);
1008         }
1009         else
1010         {
1011                 m.vertex = &s->mesh.vertex->v[0];
1012                 m.vertexstep = sizeof(surfvertex_t);
1013         }
1014         R_Mesh_Draw(&m);
1015 }
1016
1017 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
1018 {
1019         int                             i;
1020         float                   diff[3], ifog;
1021         surfvertex_t    *v;
1022         surfvert_t              *sv;
1023         rmeshinfo_t             m;
1024
1025         memset(&m, 0, sizeof(m));
1026         m.transparent = false;
1027         m.blendfunc1 = GL_ZERO;
1028         m.blendfunc2 = GL_SRC_COLOR;
1029         m.numtriangles = s->mesh.numtriangles;
1030         m.numverts = s->mesh.numverts;
1031         m.index = s->mesh.index;
1032         m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1033         m.ca = 1;
1034         m.tex[0] = R_GetTexture(s->lightmaptexture);
1035         m.texcoords[0] = &s->mesh.vertex->uv[0];
1036         m.texcoordstep[0] = sizeof(surfvertex_t);
1037         if (fogenabled)
1038         {
1039                 m.color = &svert[0].c[0];
1040                 m.colorstep = sizeof(surfvert_t);
1041                 if (softwaretransform_complexity)
1042                 {
1043                         m.vertex = &svert[0].v[0];
1044                         m.vertexstep = sizeof(surfvert_t);
1045                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1046                         {
1047                                 softwaretransform(v->v, sv->v);
1048                                 VectorSubtract(sv->v, r_origin, diff);
1049                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1050                                 sv->c[0] = m.cr * ifog;
1051                                 sv->c[1] = m.cg * ifog;
1052                                 sv->c[2] = m.cb * ifog;
1053                                 sv->c[3] = m.ca;
1054                         }
1055                 }
1056                 else
1057                 {
1058                         m.vertex = &s->mesh.vertex->v[0];
1059                         m.vertexstep = sizeof(surfvertex_t);
1060                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1061                         {
1062                                 VectorSubtract(v->v, r_origin, diff);
1063                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1064                                 sv->c[0] = m.cr * ifog;
1065                                 sv->c[1] = m.cg * ifog;
1066                                 sv->c[2] = m.cb * ifog;
1067                                 sv->c[3] = m.ca;
1068                         }
1069                 }
1070         }
1071         else
1072         {
1073                 if (softwaretransform_complexity)
1074                 {
1075                         m.vertex = &svert[0].v[0];
1076                         m.vertexstep = sizeof(surfvert_t);
1077                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1078                                 softwaretransform(v->v, sv->v);
1079                 }
1080                 else
1081                 {
1082                         m.vertex = &s->mesh.vertex->v[0];
1083                         m.vertexstep = sizeof(surfvertex_t);
1084                 }
1085         }
1086         R_Mesh_Draw(&m);
1087 }
1088
1089 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
1090 {
1091         int                             i, size3;
1092         float                   c[3], base[3], scale, diff[3], ifog;
1093         surfvertex_t    *v;
1094         surfvert_t              *sv;
1095         rmeshinfo_t             m;
1096         byte                    *lm;
1097
1098         size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
1099
1100         base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1101
1102         memset(&m, 0, sizeof(m));
1103         if (currentrenderentity->effects & EF_ADDITIVE)
1104         {
1105                 m.transparent = true;
1106                 m.blendfunc1 = GL_SRC_ALPHA;
1107                 m.blendfunc2 = GL_ONE;
1108         }
1109         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1110         {
1111                 m.transparent = true;
1112                 m.blendfunc1 = GL_SRC_ALPHA;
1113                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1114         }
1115         else
1116         {
1117                 m.transparent = false;
1118                 m.blendfunc1 = GL_ONE;
1119                 m.blendfunc2 = GL_ZERO;
1120         }
1121         m.numtriangles = s->mesh.numtriangles;
1122         m.numverts = s->mesh.numverts;
1123         m.index = s->mesh.index;
1124         m.vertex = &svert[0].v[0];
1125         m.vertexstep = sizeof(surfvert_t);
1126         m.color = &svert[0].c[0];
1127         m.colorstep = sizeof(surfvert_t);
1128         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1129         m.texcoords[0] = &s->mesh.vertex->st[0];
1130         m.texcoordstep[0] = sizeof(surfvertex_t);
1131         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1132         {
1133                 softwaretransform(v->v, sv->v);
1134                 VectorCopy(base, c);
1135                 if (s->styles[0] != 255)
1136                 {
1137                         lm = s->samples + v->lightmapoffset;
1138                         scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
1139                         VectorMA(c, scale, lm, c);
1140                         if (s->styles[1] != 255)
1141                         {
1142                                 lm += size3;
1143                                 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
1144                                 VectorMA(c, scale, lm, c);
1145                                 if (s->styles[2] != 255)
1146                                 {
1147                                         lm += size3;
1148                                         scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
1149                                         VectorMA(c, scale, lm, c);
1150                                         if (s->styles[3] != 255)
1151                                         {
1152                                                 lm += size3;
1153                                                 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
1154                                                 VectorMA(c, scale, lm, c);
1155                                         }
1156                                 }
1157                         }
1158                 }
1159                 sv->c[0] = c[0];
1160                 sv->c[1] = c[1];
1161                 sv->c[2] = c[2];
1162                 sv->c[3] = currentrenderentity->alpha;
1163         }
1164         if (s->dlightframe == r_framecount)
1165                 RSurf_Light(s->dlightbits, m.numverts);
1166         if (fogenabled)
1167         {
1168                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1169                 {
1170                         VectorSubtract(sv->v, r_origin, diff);
1171                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1172                         sv->c[0] *= ifog;
1173                         sv->c[1] *= ifog;
1174                         sv->c[2] *= ifog;
1175                 }
1176         }
1177         R_Mesh_Draw(&m);
1178 }
1179
1180 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *s)
1181 {
1182         int                             i;
1183         float                   diff[3], ifog;
1184         surfvertex_t    *v;
1185         surfvert_t              *sv;
1186         rmeshinfo_t             m;
1187
1188         memset(&m, 0, sizeof(m));
1189         if (currentrenderentity->effects & EF_ADDITIVE)
1190         {
1191                 m.transparent = true;
1192                 m.blendfunc1 = GL_SRC_ALPHA;
1193                 m.blendfunc2 = GL_ONE;
1194         }
1195         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1196         {
1197                 m.transparent = true;
1198                 m.blendfunc1 = GL_SRC_ALPHA;
1199                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1200         }
1201         else
1202         {
1203                 m.transparent = false;
1204                 m.blendfunc1 = GL_ONE;
1205                 m.blendfunc2 = GL_ZERO;
1206         }
1207         m.numtriangles = s->mesh.numtriangles;
1208         m.numverts = s->mesh.numverts;
1209         m.index = s->mesh.index;
1210         m.vertex = &svert[0].v[0];
1211         m.vertexstep = sizeof(surfvert_t);
1212         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1213         m.texcoords[0] = &s->mesh.vertex->st[0];
1214         m.texcoordstep[0] = sizeof(surfvertex_t);
1215         if (fogenabled)
1216         {
1217                 m.color = &svert[0].c[0];
1218                 m.colorstep = sizeof(surfvert_t);
1219                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1220                 {
1221                         softwaretransform(v->v, sv->v);
1222                         VectorSubtract(sv->v, r_origin, diff);
1223                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1224                         sv->c[0] = ifog;
1225                         sv->c[1] = ifog;
1226                         sv->c[2] = ifog;
1227                         sv->c[3] = currentrenderentity->alpha;
1228                 }
1229         }
1230         else
1231         {
1232                 m.cr = m.cg = m.cb = 1;
1233                 m.ca = currentrenderentity->alpha;
1234                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1235                         softwaretransform(v->v, sv->v);
1236         }
1237         R_Mesh_Draw(&m);
1238 }
1239
1240 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1241 {
1242         int                             i;
1243         float                   diff[3], ifog;
1244         surfvertex_t    *v;
1245         surfvert_t              *sv;
1246         rmeshinfo_t             m;
1247
1248         memset(&m, 0, sizeof(m));
1249         if (currentrenderentity->effects & EF_ADDITIVE)
1250                 m.transparent = true;
1251         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1252                 m.transparent = true;
1253         else
1254                 m.transparent = false;
1255         m.blendfunc1 = GL_SRC_ALPHA;
1256         m.blendfunc2 = GL_ONE;
1257         m.numtriangles = s->mesh.numtriangles;
1258         m.numverts = s->mesh.numverts;
1259         m.index = s->mesh.index;
1260         m.vertex = &svert[0].v[0];
1261         m.vertexstep = sizeof(surfvert_t);
1262         m.color = &svert[0].c[0];
1263         m.colorstep = sizeof(surfvert_t);
1264         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1265         m.texcoords[0] = &s->mesh.vertex->st[0];
1266         m.texcoordstep[0] = sizeof(surfvertex_t);
1267         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1268         {
1269                 softwaretransform(v->v, sv->v);
1270                 sv->c[0] = 0;
1271                 sv->c[1] = 0;
1272                 sv->c[2] = 0;
1273                 sv->c[3] = currentrenderentity->alpha;
1274         }
1275         if (RSurf_Light(s->dlightbits, m.numverts))
1276         {
1277                 if (fogenabled)
1278                 {
1279                         for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1280                         {
1281                                 VectorSubtract(sv->v, r_origin, diff);
1282                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1283                                 sv->c[0] *= ifog;
1284                                 sv->c[1] *= ifog;
1285                                 sv->c[2] *= ifog;
1286                         }
1287                 }
1288                 R_Mesh_Draw(&m);
1289         }
1290 }
1291
1292 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1293 {
1294         int                             i;
1295         float                   diff[3], ifog;
1296         surfvertex_t    *v;
1297         surfvert_t              *sv;
1298         rmeshinfo_t             m;
1299
1300         memset(&m, 0, sizeof(m));
1301         if (currentrenderentity->effects & EF_ADDITIVE)
1302                 m.transparent = true;
1303         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1304                 m.transparent = true;
1305         else
1306                 m.transparent = false;
1307         m.blendfunc1 = GL_SRC_ALPHA;
1308         m.blendfunc2 = GL_ONE;
1309         m.numtriangles = s->mesh.numtriangles;
1310         m.numverts = s->mesh.numverts;
1311         m.index = s->mesh.index;
1312         m.cr = 1;
1313         m.cg = 1;
1314         m.cb = 1;
1315         m.ca = currentrenderentity->alpha;
1316         m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1317         m.texcoords[0] = &s->mesh.vertex->st[0];
1318         m.texcoordstep[0] = sizeof(surfvertex_t);
1319         if (fogenabled)
1320         {
1321                 m.color = &svert[0].c[0];
1322                 m.colorstep = sizeof(surfvert_t);
1323                 if (softwaretransform_complexity)
1324                 {
1325                         m.vertex = &svert[0].v[0];
1326                         m.vertexstep = sizeof(surfvert_t);
1327                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1328                         {
1329                                 softwaretransform(v->v, sv->v);
1330                                 VectorSubtract(sv->v, r_origin, diff);
1331                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1332                                 sv->c[0] = m.cr * ifog;
1333                                 sv->c[1] = m.cg * ifog;
1334                                 sv->c[2] = m.cb * ifog;
1335                                 sv->c[3] = m.ca;
1336                         }
1337                 }
1338                 else
1339                 {
1340                         m.vertex = &s->mesh.vertex->v[0];
1341                         m.vertexstep = sizeof(surfvertex_t);
1342                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1343                         {
1344                                 VectorSubtract(v->v, r_origin, diff);
1345                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1346                                 sv->c[0] = m.cr * ifog;
1347                                 sv->c[1] = m.cg * ifog;
1348                                 sv->c[2] = m.cb * ifog;
1349                                 sv->c[3] = m.ca;
1350                         }
1351                 }
1352         }
1353         else
1354         {
1355                 if (softwaretransform_complexity)
1356                 {
1357                         m.vertex = &svert[0].v[0];
1358                         m.vertexstep = sizeof(surfvert_t);
1359                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1360                                 softwaretransform(v->v, sv->v);
1361                 }
1362                 else
1363                 {
1364                         m.vertex = &s->mesh.vertex->v[0];
1365                         m.vertexstep = sizeof(surfvertex_t);
1366                 }
1367         }
1368         R_Mesh_Draw(&m);
1369 }
1370
1371 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1372 {
1373         int                             i;
1374         surfvertex_t    *v;
1375         surfvert_t              *sv;
1376         rmeshinfo_t             m;
1377         vec3_t                  diff;
1378
1379         memset(&m, 0, sizeof(m));
1380         if (currentrenderentity->effects & EF_ADDITIVE)
1381                 m.transparent = true;
1382         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1383                 m.transparent = true;
1384         else
1385                 m.transparent = false;
1386         m.blendfunc1 = GL_SRC_ALPHA;
1387         m.blendfunc2 = GL_ONE;
1388         m.numtriangles = s->mesh.numtriangles;
1389         m.numverts = s->mesh.numverts;
1390         m.index = s->mesh.index;
1391         m.color = &svert[0].c[0];
1392         m.colorstep = sizeof(surfvert_t);
1393         m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1394         m.texcoords[0] = &s->mesh.vertex->st[0];
1395         m.texcoordstep[0] = sizeof(surfvertex_t);
1396         if (softwaretransform_complexity)
1397         {
1398                 m.vertex = &svert[0].v[0];
1399                 m.vertexstep = sizeof(surfvert_t);
1400                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1401                 {
1402                         softwaretransform(v->v, sv->v);
1403                         VectorSubtract(sv->v, r_origin, diff);
1404                         sv->c[0] = fogcolor[0];
1405                         sv->c[1] = fogcolor[1];
1406                         sv->c[2] = fogcolor[2];
1407                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1408                 }
1409         }
1410         else
1411         {
1412                 m.vertex = &s->mesh.vertex->v[0];
1413                 m.vertexstep = sizeof(surfvertex_t);
1414                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1415                 {
1416                         VectorSubtract(v->v, r_origin, diff);
1417                         sv->c[0] = fogcolor[0];
1418                         sv->c[1] = fogcolor[1];
1419                         sv->c[2] = fogcolor[2];
1420                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1421                 }
1422         }
1423         R_Mesh_Draw(&m);
1424 }
1425
1426 static int RSurfShader_Wall_Fullbright(int stage, msurface_t *s)
1427 {
1428         switch(stage)
1429         {
1430         case 0:
1431                 RSurfShader_Wall_Pass_BaseFullbright(s);
1432                 return false;
1433         case 1:
1434                 if (s->currenttexture->glowtexture)
1435                         RSurfShader_Wall_Pass_Glow(s);
1436                 return false;
1437         default:
1438                 return true;
1439         }
1440 }
1441
1442 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1443 {
1444         switch(stage)
1445         {
1446         case 0:
1447                 RSurfShader_Wall_Pass_BaseVertex(s);
1448                 return false;
1449         case 1:
1450                 if (s->currenttexture->glowtexture)
1451                         RSurfShader_Wall_Pass_Glow(s);
1452                 return false;
1453         default:
1454                 return true;
1455         }
1456 }
1457
1458 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1459 {
1460         if (r_vertexsurfaces.integer)
1461         {
1462                 switch(stage)
1463                 {
1464                 case 0:
1465                         RSurfShader_Wall_Pass_BaseVertex(s);
1466                         return false;
1467                 case 1:
1468                         if (s->currenttexture->glowtexture)
1469                                 RSurfShader_Wall_Pass_Glow(s);
1470                         return false;
1471                 default:
1472                         return true;
1473                 }
1474         }
1475         else if (r_multitexture.integer)
1476         {
1477                 if (r_dlightmap.integer)
1478                 {
1479                         switch(stage)
1480                         {
1481                         case 0:
1482                                 RSurfShader_Wall_Pass_BaseMTex(s);
1483                                 return false;
1484                         case 1:
1485                                 if (s->currenttexture->glowtexture)
1486                                         RSurfShader_Wall_Pass_Glow(s);
1487                                 return false;
1488                         default:
1489                                 return true;
1490                         }
1491                 }
1492                 else
1493                 {
1494                         switch(stage)
1495                         {
1496                         case 0:
1497                                 RSurfShader_Wall_Pass_BaseMTex(s);
1498                                 return false;
1499                         case 1:
1500                                 if (s->dlightframe == r_framecount)
1501                                         RSurfShader_Wall_Pass_Light(s);
1502                                 return false;
1503                         case 2:
1504                                 if (s->currenttexture->glowtexture)
1505                                         RSurfShader_Wall_Pass_Glow(s);
1506                                 return false;
1507                         default:
1508                                 return true;
1509                         }
1510                 }
1511         }
1512         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1513         {
1514                 switch(stage)
1515                 {
1516                 case 0:
1517                         RSurfShader_Wall_Pass_BaseVertex(s);
1518                         return false;
1519                 case 1:
1520                         if (s->currenttexture->glowtexture)
1521                                 RSurfShader_Wall_Pass_Glow(s);
1522                         return false;
1523                 default:
1524                         return true;
1525                 }
1526         }
1527         else
1528         {
1529                 if (r_dlightmap.integer)
1530                 {
1531                         switch(stage)
1532                         {
1533                         case 0:
1534                                 RSurfShader_Wall_Pass_BaseTexture(s);
1535                                 return false;
1536                         case 1:
1537                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1538                                 return false;
1539                         case 2:
1540                                 if (s->currenttexture->glowtexture)
1541                                         RSurfShader_Wall_Pass_Glow(s);
1542                                 return false;
1543                         default:
1544                                 return true;
1545                         }
1546                 }
1547                 else
1548                 {
1549                         switch(stage)
1550                         {
1551                         case 0:
1552                                 RSurfShader_Wall_Pass_BaseTexture(s);
1553                                 return false;
1554                         case 1:
1555                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1556                                 return false;
1557                         case 2:
1558                                 if (s->dlightframe == r_framecount)
1559                                         RSurfShader_Wall_Pass_Light(s);
1560                                 return false;
1561                         case 3:
1562                                 if (s->currenttexture->glowtexture)
1563                                         RSurfShader_Wall_Pass_Glow(s);
1564                                 return false;
1565                         default:
1566                                 return true;
1567                         }
1568                 }
1569         }
1570 }
1571
1572 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1573 {
1574         if (stage == 0 && fogenabled)
1575         {
1576                 RSurfShader_Wall_Pass_Fog(s);
1577                 return false;
1578         }
1579         else
1580                 return true;
1581 }
1582
1583 /*
1584 =============================================================
1585
1586         WORLD MODEL
1587
1588 =============================================================
1589 */
1590
1591 static void RSurf_Callback(void *data, void *junk)
1592 {
1593         ((msurface_t *)data)->visframe = r_framecount;
1594 }
1595
1596 static void R_SolidWorldNode (void)
1597 {
1598         if (r_viewleaf->contents != CONTENTS_SOLID)
1599         {
1600                 int portalstack;
1601                 mportal_t *p, *pstack[8192];
1602                 msurface_t *surf, **mark, **endmark;
1603                 mleaf_t *leaf;
1604                 tinyplane_t plane;
1605                 // LordHavoc: portal-passage worldnode; follows portals leading
1606                 // outward from viewleaf, if a portal leads offscreen it is not
1607                 // followed, in indoor maps this can often cull a great deal of
1608                 // geometry away when pvs data is not present (useful with pvs as well)
1609
1610                 leaf = r_viewleaf;
1611                 leaf->worldnodeframe = r_framecount;
1612                 portalstack = 0;
1613         loc0:
1614                 c_leafs++;
1615
1616                 leaf->visframe = r_framecount;
1617
1618                 if (leaf->nummarksurfaces)
1619                 {
1620                         mark = leaf->firstmarksurface;
1621                         endmark = mark + leaf->nummarksurfaces;
1622                         if (r_ser.integer)
1623                         {
1624                                 do
1625                                 {
1626                                         surf = *mark++;
1627                                         // make sure surfaces are only processed once
1628                                         if (surf->worldnodeframe == r_framecount)
1629                                                 continue;
1630                                         surf->worldnodeframe = r_framecount;
1631                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1632                                         {
1633                                                 if (surf->flags & SURF_PLANEBACK)
1634                                                 {
1635                                                         VectorNegate(surf->plane->normal, plane.normal);
1636                                                         plane.dist = -surf->plane->dist;
1637                                                         R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1638                                                 }
1639                                         }
1640                                         else
1641                                         {
1642                                                 if (!(surf->flags & SURF_PLANEBACK))
1643                                                         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);
1644                                         }
1645                                 }
1646                                 while (mark < endmark);
1647                         }
1648                         else
1649                         {
1650                                 do
1651                                 {
1652                                         surf = *mark++;
1653                                         // make sure surfaces are only processed once
1654                                         if (surf->worldnodeframe == r_framecount)
1655                                                 continue;
1656                                         surf->worldnodeframe = r_framecount;
1657                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1658                                         {
1659                                                 if (surf->flags & SURF_PLANEBACK)
1660                                                         surf->visframe = r_framecount;
1661                                         }
1662                                         else
1663                                         {
1664                                                 if (!(surf->flags & SURF_PLANEBACK))
1665                                                         surf->visframe = r_framecount;
1666                                         }
1667                                 }
1668                                 while (mark < endmark);
1669                         }
1670                 }
1671
1672                 // follow portals into other leafs
1673                 p = leaf->portals;
1674                 for (;p;p = p->next)
1675                 {
1676                         if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1677                         {
1678                                 leaf = p->past;
1679                                 if (leaf->worldnodeframe != r_framecount)
1680                                 {
1681                                         leaf->worldnodeframe = r_framecount;
1682                                         if (leaf->contents != CONTENTS_SOLID)
1683                                         {
1684                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1685                                                 {
1686                                                         p->visframe = r_framecount;
1687                                                         pstack[portalstack++] = p;
1688                                                         goto loc0;
1689
1690         loc1:
1691                                                         p = pstack[--portalstack];
1692                                                 }
1693                                         }
1694                                 }
1695                         }
1696                 }
1697
1698                 if (portalstack)
1699                         goto loc1;
1700         }
1701         else
1702         {
1703                 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1704                 int nodestackpos = 0;
1705                 // LordHavoc: recursive descending worldnode; if portals are not
1706                 // available, this is a good last resort, can cull large amounts of
1707                 // geometry, but is more time consuming than portal-passage and renders
1708                 // things behind walls
1709
1710 loc2:
1711                 if (R_NotCulledBox(node->mins, node->maxs))
1712                 {
1713                         if (node->numsurfaces)
1714                         {
1715                                 if (r_ser.integer)
1716                                 {
1717                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1718                                         tinyplane_t plane;
1719                                         if (PlaneDiff (r_origin, node->plane) < 0)
1720                                         {
1721                                                 for (;surf < surfend;surf++)
1722                                                 {
1723                                                         if (surf->flags & SURF_PLANEBACK)
1724                                                         {
1725                                                                 VectorNegate(surf->plane->normal, plane.normal);
1726                                                                 plane.dist = -surf->plane->dist;
1727                                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1728                                                         }
1729                                                 }
1730                                         }
1731                                         else
1732                                         {
1733                                                 for (;surf < surfend;surf++)
1734                                                 {
1735                                                         if (!(surf->flags & SURF_PLANEBACK))
1736                                                                 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);
1737                                                 }
1738                                         }
1739                                 }
1740                                 else
1741                                 {
1742                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1743                                         if (PlaneDiff (r_origin, node->plane) < 0)
1744                                         {
1745                                                 for (;surf < surfend;surf++)
1746                                                 {
1747                                                         if (surf->flags & SURF_PLANEBACK)
1748                                                                 surf->visframe = r_framecount;
1749                                                 }
1750                                         }
1751                                         else
1752                                         {
1753                                                 for (;surf < surfend;surf++)
1754                                                 {
1755                                                         if (!(surf->flags & SURF_PLANEBACK))
1756                                                                 surf->visframe = r_framecount;
1757                                                 }
1758                                         }
1759                                 }
1760                         }
1761
1762                         // recurse down the children
1763                         if (node->children[0]->contents >= 0)
1764                         {
1765                                 if (node->children[1]->contents >= 0)
1766                                 {
1767                                         if (nodestackpos < 8192)
1768                                                 nodestack[nodestackpos++] = node->children[1];
1769                                         node = node->children[0];
1770                                         goto loc2;
1771                                 }
1772                                 else
1773                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1774                                 node = node->children[0];
1775                                 goto loc2;
1776                         }
1777                         else
1778                         {
1779                                 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1780                                 if (node->children[1]->contents >= 0)
1781                                 {
1782                                         node = node->children[1];
1783                                         goto loc2;
1784                                 }
1785                                 else if (nodestackpos > 0)
1786                                 {
1787                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1788                                         node = nodestack[--nodestackpos];
1789                                         goto loc2;
1790                                 }
1791                         }
1792                 }
1793                 else if (nodestackpos > 0)
1794                 {
1795                         node = nodestack[--nodestackpos];
1796                         goto loc2;
1797                 }
1798         }
1799 }
1800
1801 static int r_portalframecount = 0;
1802
1803 static void R_PVSWorldNode()
1804 {
1805         int portalstack, i;
1806         mportal_t *p, *pstack[8192];
1807         msurface_t *surf, **mark, **endmark;
1808         mleaf_t *leaf;
1809         tinyplane_t plane;
1810         byte *worldvis;
1811
1812         worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1813
1814         leaf = r_viewleaf;
1815         leaf->worldnodeframe = r_framecount;
1816         portalstack = 0;
1817 loc0:
1818         c_leafs++;
1819
1820         leaf->visframe = r_framecount;
1821
1822         if (leaf->nummarksurfaces)
1823         {
1824                 mark = leaf->firstmarksurface;
1825                 endmark = mark + leaf->nummarksurfaces;
1826                 if (r_ser.integer)
1827                 {
1828                         do
1829                         {
1830                                 surf = *mark++;
1831                                 // make sure surfaces are only processed once
1832                                 if (surf->worldnodeframe == r_framecount)
1833                                         continue;
1834                                 surf->worldnodeframe = r_framecount;
1835                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1836                                 {
1837                                         if (surf->flags & SURF_PLANEBACK)
1838                                         {
1839                                                 VectorNegate(surf->plane->normal, plane.normal);
1840                                                 plane.dist = -surf->plane->dist;
1841                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1842                                         }
1843                                 }
1844                                 else
1845                                 {
1846                                         if (!(surf->flags & SURF_PLANEBACK))
1847                                                 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);
1848                                 }
1849                         }
1850                         while (mark < endmark);
1851                 }
1852                 else
1853                 {
1854                         do
1855                         {
1856                                 surf = *mark++;
1857                                 // make sure surfaces are only processed once
1858                                 if (surf->worldnodeframe == r_framecount)
1859                                         continue;
1860                                 surf->worldnodeframe = r_framecount;
1861                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1862                                 {
1863                                         if (surf->flags & SURF_PLANEBACK)
1864                                                 surf->visframe = r_framecount;
1865                                 }
1866                                 else
1867                                 {
1868                                         if (!(surf->flags & SURF_PLANEBACK))
1869                                                 surf->visframe = r_framecount;
1870                                 }
1871                         }
1872                         while (mark < endmark);
1873                 }
1874         }
1875
1876         // follow portals into other leafs
1877         for (p = leaf->portals;p;p = p->next)
1878         {
1879                 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1880                 {
1881                         leaf = p->past;
1882                         if (leaf->worldnodeframe != r_framecount)
1883                         {
1884                                 leaf->worldnodeframe = r_framecount;
1885                                 if (leaf->contents != CONTENTS_SOLID)
1886                                 {
1887                                         i = (leaf - cl.worldmodel->leafs) - 1;
1888                                         if (worldvis[i>>3] & (1<<(i&7)))
1889                                         {
1890                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1891                                                 {
1892                                                         pstack[portalstack++] = p;
1893                                                         goto loc0;
1894
1895 loc1:
1896                                                         p = pstack[--portalstack];
1897                                                 }
1898                                         }
1899                                 }
1900                         }
1901                 }
1902         }
1903
1904         if (portalstack)
1905                 goto loc1;
1906 }
1907
1908 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1909 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1910 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
1911 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1912 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1913
1914 int Cshader_count = 5;
1915 Cshader_t *Cshaders[5] =
1916 {
1917         &Cshader_wall_vertex,
1918         &Cshader_wall_lightmap,
1919         &Cshader_wall_fullbright,
1920         &Cshader_water,
1921         &Cshader_sky
1922 };
1923
1924 void R_PrepareSurfaces(void)
1925 {
1926         int i;
1927         texture_t *t;
1928         model_t *model;
1929         msurface_t *surf;
1930
1931         for (i = 0;i < Cshader_count;i++)
1932                 Cshaders[i]->chain = NULL;
1933
1934         model = currentrenderentity->model;
1935
1936         for (i = 0;i < model->nummodelsurfaces;i++)
1937         {
1938                 surf = model->modelsortedsurfaces[i];
1939                 if (surf->visframe == r_framecount)
1940                 {
1941                         if (surf->insertframe != r_framecount)
1942                         {
1943                                 surf->insertframe = r_framecount;
1944                                 c_faces++;
1945                                 // manually inlined R_TextureAnimation
1946                                 //t = R_TextureAnimation(surf->texinfo->texture);
1947                                 t = surf->texinfo->texture;
1948                                 if (t->alternate_anims != NULL && currentrenderentity->frame)
1949                                         t = t->alternate_anims;
1950                                 if (t->anim_total >= 2)
1951                                         t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1952                                 surf->currenttexture = t;
1953                         }
1954
1955                         surf->chain = surf->shader->chain;
1956                         surf->shader->chain = surf;
1957                 }
1958         }
1959 }
1960
1961 void R_DrawSurfaces (int type)
1962 {
1963         int                     i, stage;
1964         msurface_t      *surf;
1965         Cshader_t       *shader;
1966
1967         for (i = 0;i < Cshader_count;i++)
1968         {
1969                 shader = Cshaders[i];
1970                 if (shader->chain && shader->shaderfunc[type])
1971                         for (stage = 0;stage < 1000;stage++)
1972                                 for (surf = shader->chain;surf;surf = surf->chain)
1973                                         if (shader->shaderfunc[type](stage, surf))
1974                                                 goto done;
1975 done:;
1976         }
1977 }
1978
1979 static float portalpointbuffer[256][3];
1980
1981 void R_DrawPortals(void)
1982 {
1983         int drawportals, i;
1984 //      mleaf_t *leaf, *endleaf;
1985         mportal_t *portal, *endportal;
1986         mvertex_t *point/*, *endpoint*/;
1987         rmeshinfo_t m;
1988         drawportals = r_drawportals.integer;
1989         if (drawportals < 1)
1990                 return;
1991         /*
1992         leaf = cl.worldmodel->leafs;
1993         endleaf = leaf + cl.worldmodel->numleafs;
1994         for (;leaf < endleaf;leaf++)
1995         {
1996                 if (leaf->visframe == r_framecount && leaf->portals)
1997                 {
1998                         i = leaf - cl.worldmodel->leafs;
1999                         r = (i & 0x0007) << 5;
2000                         g = (i & 0x0038) << 2;
2001                         b = (i & 0x01C0) >> 1;
2002                         portal = leaf->portals;
2003                         while (portal)
2004                         {
2005                                 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2006                                 point = portal->points + portal->numpoints - 1;
2007                                 endpoint = portal->points;
2008                                 for (;point >= endpoint;point--)
2009                                         transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2010                                 transpolyend();
2011                                 portal = portal->next;
2012                         }
2013                 }
2014         }
2015         */
2016         memset(&m, 0, sizeof(m));
2017         m.transparent = true;
2018         m.blendfunc1 = GL_SRC_ALPHA;
2019         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2020         m.vertex = &portalpointbuffer[0][0];
2021         m.vertexstep = sizeof(float[3]);
2022         m.ca = 0.125;
2023         for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2024         {
2025                 if (portal->visframe == r_portalframecount)
2026                 {
2027                         if (portal->numpoints <= 256)
2028                         {
2029                                 i = portal - cl.worldmodel->portals;
2030                                 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2031                                 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2032                                 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2033                                 point = portal->points;
2034                                 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2035                                 {
2036                                         for (i = portal->numpoints - 1;i >= 0;i--)
2037                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2038                                 }
2039                                 else
2040                                 {
2041                                         for (i = 0;i < portal->numpoints;i++)
2042                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2043                                 }
2044                                 R_Mesh_DrawPolygon(&m, portal->numpoints);
2045                         }
2046                 }
2047         }
2048 }
2049
2050 void R_SetupForBModelRendering(void)
2051 {
2052         int                     i;
2053         msurface_t      *s;
2054         model_t         *model;
2055         vec3_t          modelorg;
2056
2057         // because bmodels can be reused, we have to decide which things to render
2058         // from scratch every time
2059
2060         model = currentrenderentity->model;
2061
2062         softwaretransformforentity (currentrenderentity);
2063         softwareuntransform(r_origin, modelorg);
2064
2065         for (i = 0;i < model->nummodelsurfaces;i++)
2066         {
2067                 s = model->modelsortedsurfaces[i];
2068                 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
2069                         s->visframe = r_framecount;
2070                 else
2071                         s->visframe = -1;
2072                 s->worldnodeframe = -1;
2073                 s->lightframe = -1;
2074                 s->dlightframe = -1;
2075                 s->insertframe = -1;
2076         }
2077 }
2078
2079 void R_SetupForWorldRendering(void)
2080 {
2081         // there is only one instance of the world, but it can be rendered in
2082         // multiple stages
2083
2084         currentrenderentity = &cl_entities[0].render;
2085         softwaretransformidentity();
2086 }
2087
2088 static void R_SurfMarkLights (void)
2089 {
2090         int                     i;
2091         msurface_t      *s;
2092
2093         if (r_dynamic.integer)
2094                 R_MarkLights();
2095
2096         if (!r_vertexsurfaces.integer)
2097         {
2098                 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2099                 {
2100                         s = currentrenderentity->model->modelsortedsurfaces[i];
2101                         if (s->visframe == r_framecount && s->lightmaptexture != NULL)
2102                         {
2103                                 if (s->cached_dlight
2104                                  || s->cached_ambient != r_ambient.value
2105                                  || s->cached_lightscalebit != lightscalebit)
2106                                         R_BuildLightMap(s, false); // base lighting changed
2107                                 else if (r_dynamic.integer)
2108                                 {
2109                                         if  (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
2110                                          || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
2111                                          || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
2112                                          || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
2113                                         //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
2114                                         // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
2115                                         // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
2116                                         // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
2117                                                 R_BuildLightMap(s, false); // base lighting changed
2118                                         else if (s->dlightframe == r_framecount && r_dlightmap.integer)
2119                                                 R_BuildLightMap(s, true); // only dlights
2120                                 }
2121                         }
2122                 }
2123         }
2124 }
2125
2126 void R_MarkWorldLights(void)
2127 {
2128         R_SetupForWorldRendering();
2129         R_SurfMarkLights();
2130 }
2131
2132 /*
2133 =============
2134 R_DrawWorld
2135 =============
2136 */
2137 void R_DrawWorld (void)
2138 {
2139         R_SetupForWorldRendering();
2140
2141         if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2142                 R_SolidWorldNode ();
2143         else
2144                 R_PVSWorldNode ();
2145 }
2146
2147 /*
2148 =================
2149 R_DrawBrushModel
2150 =================
2151 */
2152 void R_DrawBrushModelSky (void)
2153 {
2154         R_SetupForBModelRendering();
2155
2156         R_PrepareSurfaces();
2157         R_DrawSurfaces(SHADERSTAGE_SKY);
2158 }
2159
2160 void R_DrawBrushModelNormal (void)
2161 {
2162         c_bmodels++;
2163
2164         // have to flush queue because of possible lightmap reuse
2165         R_Mesh_Render();
2166
2167         R_SetupForBModelRendering();
2168
2169         R_SurfMarkLights();
2170
2171         R_PrepareSurfaces();
2172
2173         if (!skyrendermasked)
2174                 R_DrawSurfaces(SHADERSTAGE_SKY);
2175         R_DrawSurfaces(SHADERSTAGE_NORMAL);
2176         R_DrawSurfaces(SHADERSTAGE_FOG);
2177 }