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