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