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