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