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