]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_rsurf.c
validate more when loading zymotic models, also reverse triangles so they don't rende...
[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 = 1;
927         if (lighthalf)
928                 m.cr *= 2;
929         if (gl_combine.integer)
930                 m.cr *= 4;
931         m.cg = m.cr;
932         m.cb = m.cr;
933         m.ca = currentrenderentity->alpha;
934         m.tex[0] = R_GetTexture(s->currenttexture->texture);
935         m.tex[1] = R_GetTexture(s->lightmaptexture);
936         m.texcoords[0] = &s->mesh.vertex->st[0];
937         m.texcoords[1] = &s->mesh.vertex->uv[0];
938         m.texcoordstep[0] = sizeof(surfvertex_t);
939         m.texcoordstep[1] = sizeof(surfvertex_t);
940         if (fogenabled)
941         {
942                 m.color = &svert[0].c[0];
943                 m.colorstep = sizeof(surfvert_t);
944                 if (softwaretransform_complexity)
945                 {
946                         m.vertex = &svert[0].v[0];
947                         m.vertexstep = sizeof(surfvert_t);
948                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
949                         {
950                                 softwaretransform(v->v, sv->v);
951                                 VectorSubtract(sv->v, r_origin, diff);
952                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
953                                 sv->c[0] = m.cr * ifog;
954                                 sv->c[1] = m.cg * ifog;
955                                 sv->c[2] = m.cb * ifog;
956                                 sv->c[3] = m.ca;
957                         }
958                 }
959                 else
960                 {
961                         m.vertex = &s->mesh.vertex->v[0];
962                         m.vertexstep = sizeof(surfvertex_t);
963                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
964                         {
965                                 VectorSubtract(v->v, r_origin, diff);
966                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
967                                 sv->c[0] = m.cr * ifog;
968                                 sv->c[1] = m.cg * ifog;
969                                 sv->c[2] = m.cb * ifog;
970                                 sv->c[3] = m.ca;
971                         }
972                 }
973         }
974         else
975         {
976                 if (softwaretransform_complexity)
977                 {
978                         m.vertex = &svert[0].v[0];
979                         m.vertexstep = sizeof(surfvert_t);
980                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
981                                 softwaretransform(v->v, sv->v);
982                 }
983                 else
984                 {
985                         m.vertex = &s->mesh.vertex->v[0];
986                         m.vertexstep = sizeof(surfvertex_t);
987                 }
988         }
989         R_Mesh_Draw(&m);
990 }
991
992 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
993 {
994         int                             i;
995         surfvertex_t    *v;
996         surfvert_t              *sv;
997         rmeshinfo_t             m;
998
999         memset(&m, 0, sizeof(m));
1000         m.transparent = false;
1001         m.blendfunc1 = GL_ONE;
1002         m.blendfunc2 = GL_ZERO;
1003         m.numtriangles = s->mesh.numtriangles;
1004         m.numverts = s->mesh.numverts;
1005         m.index = s->mesh.index;
1006         if (lighthalf)
1007         {
1008                 m.cr = 2;
1009                 m.cg = 2;
1010                 m.cb = 2;
1011         }
1012         else
1013         {
1014                 m.cr = 1;
1015                 m.cg = 1;
1016                 m.cb = 1;
1017         }
1018         m.ca = 1;
1019         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1020         m.texcoords[0] = &s->mesh.vertex->st[0];
1021         m.texcoordstep[0] = sizeof(surfvertex_t);
1022         if (softwaretransform_complexity)
1023         {
1024                 m.vertex = &svert[0].v[0];
1025                 m.vertexstep = sizeof(surfvert_t);
1026                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1027                         softwaretransform(v->v, sv->v);
1028         }
1029         else
1030         {
1031                 m.vertex = &s->mesh.vertex->v[0];
1032                 m.vertexstep = sizeof(surfvertex_t);
1033         }
1034         R_Mesh_Draw(&m);
1035 }
1036
1037 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
1038 {
1039         int                             i;
1040         float                   diff[3], ifog;
1041         surfvertex_t    *v;
1042         surfvert_t              *sv;
1043         rmeshinfo_t             m;
1044
1045         memset(&m, 0, sizeof(m));
1046         m.transparent = false;
1047         m.blendfunc1 = GL_ZERO;
1048         m.blendfunc2 = GL_SRC_COLOR;
1049         m.numtriangles = s->mesh.numtriangles;
1050         m.numverts = s->mesh.numverts;
1051         m.index = s->mesh.index;
1052         m.cr = 1;
1053         if (lighthalf)
1054                 m.cr *= 2.0f;
1055         m.cg = m.cr;
1056         m.cb = m.cr;
1057         m.ca = 1;
1058         m.tex[0] = R_GetTexture(s->lightmaptexture);
1059         m.texcoords[0] = &s->mesh.vertex->uv[0];
1060         m.texcoordstep[0] = sizeof(surfvertex_t);
1061         if (fogenabled)
1062         {
1063                 m.color = &svert[0].c[0];
1064                 m.colorstep = sizeof(surfvert_t);
1065                 if (softwaretransform_complexity)
1066                 {
1067                         m.vertex = &svert[0].v[0];
1068                         m.vertexstep = sizeof(surfvert_t);
1069                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1070                         {
1071                                 softwaretransform(v->v, sv->v);
1072                                 VectorSubtract(sv->v, r_origin, diff);
1073                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1074                                 sv->c[0] = m.cr * ifog;
1075                                 sv->c[1] = m.cg * ifog;
1076                                 sv->c[2] = m.cb * ifog;
1077                                 sv->c[3] = m.ca;
1078                         }
1079                 }
1080                 else
1081                 {
1082                         m.vertex = &s->mesh.vertex->v[0];
1083                         m.vertexstep = sizeof(surfvertex_t);
1084                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1085                         {
1086                                 VectorSubtract(v->v, r_origin, diff);
1087                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1088                                 sv->c[0] = m.cr * ifog;
1089                                 sv->c[1] = m.cg * ifog;
1090                                 sv->c[2] = m.cb * ifog;
1091                                 sv->c[3] = m.ca;
1092                         }
1093                 }
1094         }
1095         else
1096         {
1097                 if (softwaretransform_complexity)
1098                 {
1099                         m.vertex = &svert[0].v[0];
1100                         m.vertexstep = sizeof(surfvert_t);
1101                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1102                                 softwaretransform(v->v, sv->v);
1103                 }
1104                 else
1105                 {
1106                         m.vertex = &s->mesh.vertex->v[0];
1107                         m.vertexstep = sizeof(surfvertex_t);
1108                 }
1109         }
1110         R_Mesh_Draw(&m);
1111 }
1112
1113 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
1114 {
1115         int                             i, size3;
1116         float                   c[3], base[3], scale, diff[3], ifog;
1117         surfvertex_t    *v;
1118         surfvert_t              *sv;
1119         rmeshinfo_t             m;
1120         byte                    *lm;
1121
1122         size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
1123
1124         base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1125
1126         memset(&m, 0, sizeof(m));
1127         if (currentrenderentity->effects & EF_ADDITIVE)
1128         {
1129                 m.transparent = true;
1130                 m.blendfunc1 = GL_SRC_ALPHA;
1131                 m.blendfunc2 = GL_ONE;
1132         }
1133         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1134         {
1135                 m.transparent = true;
1136                 m.blendfunc1 = GL_SRC_ALPHA;
1137                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1138         }
1139         else
1140         {
1141                 m.transparent = false;
1142                 m.blendfunc1 = GL_ONE;
1143                 m.blendfunc2 = GL_ZERO;
1144         }
1145         m.numtriangles = s->mesh.numtriangles;
1146         m.numverts = s->mesh.numverts;
1147         m.index = s->mesh.index;
1148         m.vertex = &svert[0].v[0];
1149         m.vertexstep = sizeof(surfvert_t);
1150         m.color = &svert[0].c[0];
1151         m.colorstep = sizeof(surfvert_t);
1152         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1153         m.texcoords[0] = &s->mesh.vertex->st[0];
1154         m.texcoordstep[0] = sizeof(surfvertex_t);
1155         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1156         {
1157                 softwaretransform(v->v, sv->v);
1158                 VectorCopy(base, c);
1159                 if (s->styles[0] != 255)
1160                 {
1161                         lm = s->samples + v->lightmapoffset;
1162                         scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
1163                         VectorMA(c, scale, lm, c);
1164                         if (s->styles[1] != 255)
1165                         {
1166                                 lm += size3;
1167                                 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
1168                                 VectorMA(c, scale, lm, c);
1169                                 if (s->styles[2] != 255)
1170                                 {
1171                                         lm += size3;
1172                                         scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
1173                                         VectorMA(c, scale, lm, c);
1174                                         if (s->styles[3] != 255)
1175                                         {
1176                                                 lm += size3;
1177                                                 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
1178                                                 VectorMA(c, scale, lm, c);
1179                                         }
1180                                 }
1181                         }
1182                 }
1183                 sv->c[0] = c[0];
1184                 sv->c[1] = c[1];
1185                 sv->c[2] = c[2];
1186                 sv->c[3] = currentrenderentity->alpha;
1187         }
1188         if (s->dlightframe == r_framecount)
1189                 RSurf_Light(s->dlightbits, m.numverts);
1190         if (fogenabled)
1191         {
1192                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1193                 {
1194                         VectorSubtract(sv->v, r_origin, diff);
1195                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1196                         sv->c[0] *= ifog;
1197                         sv->c[1] *= ifog;
1198                         sv->c[2] *= ifog;
1199                 }
1200         }
1201         R_Mesh_Draw(&m);
1202 }
1203
1204 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *s)
1205 {
1206         int                             i;
1207         float                   diff[3], ifog;
1208         surfvertex_t    *v;
1209         surfvert_t              *sv;
1210         rmeshinfo_t             m;
1211
1212         memset(&m, 0, sizeof(m));
1213         if (currentrenderentity->effects & EF_ADDITIVE)
1214         {
1215                 m.transparent = true;
1216                 m.blendfunc1 = GL_SRC_ALPHA;
1217                 m.blendfunc2 = GL_ONE;
1218         }
1219         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1220         {
1221                 m.transparent = true;
1222                 m.blendfunc1 = GL_SRC_ALPHA;
1223                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1224         }
1225         else
1226         {
1227                 m.transparent = false;
1228                 m.blendfunc1 = GL_ONE;
1229                 m.blendfunc2 = GL_ZERO;
1230         }
1231         m.numtriangles = s->mesh.numtriangles;
1232         m.numverts = s->mesh.numverts;
1233         m.index = s->mesh.index;
1234         m.vertex = &svert[0].v[0];
1235         m.vertexstep = sizeof(surfvert_t);
1236         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1237         m.texcoords[0] = &s->mesh.vertex->st[0];
1238         m.texcoordstep[0] = sizeof(surfvertex_t);
1239         if (fogenabled)
1240         {
1241                 m.color = &svert[0].c[0];
1242                 m.colorstep = sizeof(surfvert_t);
1243                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1244                 {
1245                         softwaretransform(v->v, sv->v);
1246                         VectorSubtract(sv->v, r_origin, diff);
1247                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1248                         sv->c[0] = ifog;
1249                         sv->c[1] = ifog;
1250                         sv->c[2] = ifog;
1251                         sv->c[3] = currentrenderentity->alpha;
1252                 }
1253         }
1254         else
1255         {
1256                 m.cr = m.cg = m.cb = 1;
1257                 m.ca = currentrenderentity->alpha;
1258                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1259                         softwaretransform(v->v, sv->v);
1260         }
1261         R_Mesh_Draw(&m);
1262 }
1263
1264 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1265 {
1266         int                             i;
1267         float                   diff[3], ifog;
1268         surfvertex_t    *v;
1269         surfvert_t              *sv;
1270         rmeshinfo_t             m;
1271
1272         memset(&m, 0, sizeof(m));
1273         if (currentrenderentity->effects & EF_ADDITIVE)
1274                 m.transparent = true;
1275         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1276                 m.transparent = true;
1277         else
1278                 m.transparent = false;
1279         m.blendfunc1 = GL_SRC_ALPHA;
1280         m.blendfunc2 = GL_ONE;
1281         m.numtriangles = s->mesh.numtriangles;
1282         m.numverts = s->mesh.numverts;
1283         m.index = s->mesh.index;
1284         m.vertex = &svert[0].v[0];
1285         m.vertexstep = sizeof(surfvert_t);
1286         m.color = &svert[0].c[0];
1287         m.colorstep = sizeof(surfvert_t);
1288         m.tex[0] = R_GetTexture(s->currenttexture->texture);
1289         m.texcoords[0] = &s->mesh.vertex->st[0];
1290         m.texcoordstep[0] = sizeof(surfvertex_t);
1291         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1292         {
1293                 softwaretransform(v->v, sv->v);
1294                 sv->c[0] = 0;
1295                 sv->c[1] = 0;
1296                 sv->c[2] = 0;
1297                 sv->c[3] = currentrenderentity->alpha;
1298         }
1299         if (RSurf_Light(s->dlightbits, m.numverts))
1300         {
1301                 if (fogenabled)
1302                 {
1303                         for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1304                         {
1305                                 VectorSubtract(sv->v, r_origin, diff);
1306                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1307                                 sv->c[0] *= ifog;
1308                                 sv->c[1] *= ifog;
1309                                 sv->c[2] *= ifog;
1310                         }
1311                 }
1312                 R_Mesh_Draw(&m);
1313         }
1314 }
1315
1316 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1317 {
1318         int                             i;
1319         float                   diff[3], ifog;
1320         surfvertex_t    *v;
1321         surfvert_t              *sv;
1322         rmeshinfo_t             m;
1323
1324         memset(&m, 0, sizeof(m));
1325         if (currentrenderentity->effects & EF_ADDITIVE)
1326                 m.transparent = true;
1327         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1328                 m.transparent = true;
1329         else
1330                 m.transparent = false;
1331         m.blendfunc1 = GL_SRC_ALPHA;
1332         m.blendfunc2 = GL_ONE;
1333         m.numtriangles = s->mesh.numtriangles;
1334         m.numverts = s->mesh.numverts;
1335         m.index = s->mesh.index;
1336         m.cr = 1;
1337         m.cg = 1;
1338         m.cb = 1;
1339         m.ca = currentrenderentity->alpha;
1340         m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1341         m.texcoords[0] = &s->mesh.vertex->st[0];
1342         m.texcoordstep[0] = sizeof(surfvertex_t);
1343         if (fogenabled)
1344         {
1345                 m.color = &svert[0].c[0];
1346                 m.colorstep = sizeof(surfvert_t);
1347                 if (softwaretransform_complexity)
1348                 {
1349                         m.vertex = &svert[0].v[0];
1350                         m.vertexstep = sizeof(surfvert_t);
1351                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1352                         {
1353                                 softwaretransform(v->v, sv->v);
1354                                 VectorSubtract(sv->v, r_origin, diff);
1355                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1356                                 sv->c[0] = m.cr * ifog;
1357                                 sv->c[1] = m.cg * ifog;
1358                                 sv->c[2] = m.cb * ifog;
1359                                 sv->c[3] = m.ca;
1360                         }
1361                 }
1362                 else
1363                 {
1364                         m.vertex = &s->mesh.vertex->v[0];
1365                         m.vertexstep = sizeof(surfvertex_t);
1366                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1367                         {
1368                                 VectorSubtract(v->v, r_origin, diff);
1369                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1370                                 sv->c[0] = m.cr * ifog;
1371                                 sv->c[1] = m.cg * ifog;
1372                                 sv->c[2] = m.cb * ifog;
1373                                 sv->c[3] = m.ca;
1374                         }
1375                 }
1376         }
1377         else
1378         {
1379                 if (softwaretransform_complexity)
1380                 {
1381                         m.vertex = &svert[0].v[0];
1382                         m.vertexstep = sizeof(surfvert_t);
1383                         for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1384                                 softwaretransform(v->v, sv->v);
1385                 }
1386                 else
1387                 {
1388                         m.vertex = &s->mesh.vertex->v[0];
1389                         m.vertexstep = sizeof(surfvertex_t);
1390                 }
1391         }
1392         R_Mesh_Draw(&m);
1393 }
1394
1395 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1396 {
1397         int                             i;
1398         surfvertex_t    *v;
1399         surfvert_t              *sv;
1400         rmeshinfo_t             m;
1401         vec3_t                  diff;
1402
1403         memset(&m, 0, sizeof(m));
1404         if (currentrenderentity->effects & EF_ADDITIVE)
1405                 m.transparent = true;
1406         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1407                 m.transparent = true;
1408         else
1409                 m.transparent = false;
1410         m.blendfunc1 = GL_SRC_ALPHA;
1411         m.blendfunc2 = GL_ONE;
1412         m.numtriangles = s->mesh.numtriangles;
1413         m.numverts = s->mesh.numverts;
1414         m.index = s->mesh.index;
1415         m.color = &svert[0].c[0];
1416         m.colorstep = sizeof(surfvert_t);
1417         m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1418         m.texcoords[0] = &s->mesh.vertex->st[0];
1419         m.texcoordstep[0] = sizeof(surfvertex_t);
1420         if (softwaretransform_complexity)
1421         {
1422                 m.vertex = &svert[0].v[0];
1423                 m.vertexstep = sizeof(surfvert_t);
1424                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1425                 {
1426                         softwaretransform(v->v, sv->v);
1427                         VectorSubtract(sv->v, r_origin, diff);
1428                         sv->c[0] = fogcolor[0];
1429                         sv->c[1] = fogcolor[1];
1430                         sv->c[2] = fogcolor[2];
1431                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1432                 }
1433         }
1434         else
1435         {
1436                 m.vertex = &s->mesh.vertex->v[0];
1437                 m.vertexstep = sizeof(surfvertex_t);
1438                 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1439                 {
1440                         VectorSubtract(v->v, r_origin, diff);
1441                         sv->c[0] = fogcolor[0];
1442                         sv->c[1] = fogcolor[1];
1443                         sv->c[2] = fogcolor[2];
1444                         sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1445                 }
1446         }
1447         R_Mesh_Draw(&m);
1448 }
1449
1450 static int RSurfShader_Wall_Fullbright(int stage, msurface_t *s)
1451 {
1452         switch(stage)
1453         {
1454         case 0:
1455                 RSurfShader_Wall_Pass_BaseFullbright(s);
1456                 return false;
1457         case 1:
1458                 if (s->currenttexture->glowtexture)
1459                         RSurfShader_Wall_Pass_Glow(s);
1460                 return false;
1461         default:
1462                 return true;
1463         }
1464 }
1465
1466 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1467 {
1468         switch(stage)
1469         {
1470         case 0:
1471                 RSurfShader_Wall_Pass_BaseVertex(s);
1472                 return false;
1473         case 1:
1474                 if (s->currenttexture->glowtexture)
1475                         RSurfShader_Wall_Pass_Glow(s);
1476                 return false;
1477         default:
1478                 return true;
1479         }
1480 }
1481
1482 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1483 {
1484         if (r_vertexsurfaces.integer)
1485         {
1486                 switch(stage)
1487                 {
1488                 case 0:
1489                         RSurfShader_Wall_Pass_BaseVertex(s);
1490                         return false;
1491                 case 1:
1492                         if (s->currenttexture->glowtexture)
1493                                 RSurfShader_Wall_Pass_Glow(s);
1494                         return false;
1495                 default:
1496                         return true;
1497                 }
1498         }
1499         else if (r_multitexture.integer)
1500         {
1501                 if (r_dlightmap.integer)
1502                 {
1503                         switch(stage)
1504                         {
1505                         case 0:
1506                                 RSurfShader_Wall_Pass_BaseMTex(s);
1507                                 return false;
1508                         case 1:
1509                                 if (s->currenttexture->glowtexture)
1510                                         RSurfShader_Wall_Pass_Glow(s);
1511                                 return false;
1512                         default:
1513                                 return true;
1514                         }
1515                 }
1516                 else
1517                 {
1518                         switch(stage)
1519                         {
1520                         case 0:
1521                                 RSurfShader_Wall_Pass_BaseMTex(s);
1522                                 return false;
1523                         case 1:
1524                                 if (s->dlightframe == r_framecount)
1525                                         RSurfShader_Wall_Pass_Light(s);
1526                                 return false;
1527                         case 2:
1528                                 if (s->currenttexture->glowtexture)
1529                                         RSurfShader_Wall_Pass_Glow(s);
1530                                 return false;
1531                         default:
1532                                 return true;
1533                         }
1534                 }
1535         }
1536         else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1537         {
1538                 switch(stage)
1539                 {
1540                 case 0:
1541                         RSurfShader_Wall_Pass_BaseVertex(s);
1542                         return false;
1543                 case 1:
1544                         if (s->currenttexture->glowtexture)
1545                                 RSurfShader_Wall_Pass_Glow(s);
1546                         return false;
1547                 default:
1548                         return true;
1549                 }
1550         }
1551         else
1552         {
1553                 if (r_dlightmap.integer)
1554                 {
1555                         switch(stage)
1556                         {
1557                         case 0:
1558                                 RSurfShader_Wall_Pass_BaseTexture(s);
1559                                 return false;
1560                         case 1:
1561                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1562                                 return false;
1563                         case 2:
1564                                 if (s->currenttexture->glowtexture)
1565                                         RSurfShader_Wall_Pass_Glow(s);
1566                                 return false;
1567                         default:
1568                                 return true;
1569                         }
1570                 }
1571                 else
1572                 {
1573                         switch(stage)
1574                         {
1575                         case 0:
1576                                 RSurfShader_Wall_Pass_BaseTexture(s);
1577                                 return false;
1578                         case 1:
1579                                 RSurfShader_Wall_Pass_BaseLightmap(s);
1580                                 return false;
1581                         case 2:
1582                                 if (s->dlightframe == r_framecount)
1583                                         RSurfShader_Wall_Pass_Light(s);
1584                                 return false;
1585                         case 3:
1586                                 if (s->currenttexture->glowtexture)
1587                                         RSurfShader_Wall_Pass_Glow(s);
1588                                 return false;
1589                         default:
1590                                 return true;
1591                         }
1592                 }
1593         }
1594 }
1595
1596 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1597 {
1598         if (stage == 0 && fogenabled)
1599         {
1600                 RSurfShader_Wall_Pass_Fog(s);
1601                 return false;
1602         }
1603         else
1604                 return true;
1605 }
1606
1607 /*
1608 =============================================================
1609
1610         WORLD MODEL
1611
1612 =============================================================
1613 */
1614
1615 static void RSurf_Callback(void *data, void *junk)
1616 {
1617         ((msurface_t *)data)->visframe = r_framecount;
1618 }
1619
1620 static void R_SolidWorldNode (void)
1621 {
1622         if (r_viewleaf->contents != CONTENTS_SOLID)
1623         {
1624                 int portalstack;
1625                 mportal_t *p, *pstack[8192];
1626                 msurface_t *surf, **mark, **endmark;
1627                 mleaf_t *leaf;
1628                 tinyplane_t plane;
1629                 // LordHavoc: portal-passage worldnode; follows portals leading
1630                 // outward from viewleaf, if a portal leads offscreen it is not
1631                 // followed, in indoor maps this can often cull a great deal of
1632                 // geometry away when pvs data is not present (useful with pvs as well)
1633
1634                 leaf = r_viewleaf;
1635                 leaf->worldnodeframe = r_framecount;
1636                 portalstack = 0;
1637         loc0:
1638                 c_leafs++;
1639
1640                 leaf->visframe = r_framecount;
1641
1642                 if (leaf->nummarksurfaces)
1643                 {
1644                         mark = leaf->firstmarksurface;
1645                         endmark = mark + leaf->nummarksurfaces;
1646                         if (r_ser.integer)
1647                         {
1648                                 do
1649                                 {
1650                                         surf = *mark++;
1651                                         // make sure surfaces are only processed once
1652                                         if (surf->worldnodeframe == r_framecount)
1653                                                 continue;
1654                                         surf->worldnodeframe = r_framecount;
1655                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1656                                         {
1657                                                 if (surf->flags & SURF_PLANEBACK)
1658                                                 {
1659                                                         VectorNegate(surf->plane->normal, plane.normal);
1660                                                         plane.dist = -surf->plane->dist;
1661                                                         R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1662                                                 }
1663                                         }
1664                                         else
1665                                         {
1666                                                 if (!(surf->flags & SURF_PLANEBACK))
1667                                                         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);
1668                                         }
1669                                 }
1670                                 while (mark < endmark);
1671                         }
1672                         else
1673                         {
1674                                 do
1675                                 {
1676                                         surf = *mark++;
1677                                         // make sure surfaces are only processed once
1678                                         if (surf->worldnodeframe == r_framecount)
1679                                                 continue;
1680                                         surf->worldnodeframe = r_framecount;
1681                                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1682                                         {
1683                                                 if (surf->flags & SURF_PLANEBACK)
1684                                                         surf->visframe = r_framecount;
1685                                         }
1686                                         else
1687                                         {
1688                                                 if (!(surf->flags & SURF_PLANEBACK))
1689                                                         surf->visframe = r_framecount;
1690                                         }
1691                                 }
1692                                 while (mark < endmark);
1693                         }
1694                 }
1695
1696                 // follow portals into other leafs
1697                 p = leaf->portals;
1698                 for (;p;p = p->next)
1699                 {
1700                         if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1701                         {
1702                                 leaf = p->past;
1703                                 if (leaf->worldnodeframe != r_framecount)
1704                                 {
1705                                         leaf->worldnodeframe = r_framecount;
1706                                         if (leaf->contents != CONTENTS_SOLID)
1707                                         {
1708                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1709                                                 {
1710                                                         p->visframe = r_framecount;
1711                                                         pstack[portalstack++] = p;
1712                                                         goto loc0;
1713
1714         loc1:
1715                                                         p = pstack[--portalstack];
1716                                                 }
1717                                         }
1718                                 }
1719                         }
1720                 }
1721
1722                 if (portalstack)
1723                         goto loc1;
1724         }
1725         else
1726         {
1727                 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1728                 int nodestackpos = 0;
1729                 // LordHavoc: recursive descending worldnode; if portals are not
1730                 // available, this is a good last resort, can cull large amounts of
1731                 // geometry, but is more time consuming than portal-passage and renders
1732                 // things behind walls
1733
1734 loc2:
1735                 if (R_NotCulledBox(node->mins, node->maxs))
1736                 {
1737                         if (node->numsurfaces)
1738                         {
1739                                 if (r_ser.integer)
1740                                 {
1741                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1742                                         tinyplane_t plane;
1743                                         if (PlaneDiff (r_origin, node->plane) < 0)
1744                                         {
1745                                                 for (;surf < surfend;surf++)
1746                                                 {
1747                                                         if (surf->flags & SURF_PLANEBACK)
1748                                                         {
1749                                                                 VectorNegate(surf->plane->normal, plane.normal);
1750                                                                 plane.dist = -surf->plane->dist;
1751                                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1752                                                         }
1753                                                 }
1754                                         }
1755                                         else
1756                                         {
1757                                                 for (;surf < surfend;surf++)
1758                                                 {
1759                                                         if (!(surf->flags & SURF_PLANEBACK))
1760                                                                 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);
1761                                                 }
1762                                         }
1763                                 }
1764                                 else
1765                                 {
1766                                         msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1767                                         if (PlaneDiff (r_origin, node->plane) < 0)
1768                                         {
1769                                                 for (;surf < surfend;surf++)
1770                                                 {
1771                                                         if (surf->flags & SURF_PLANEBACK)
1772                                                                 surf->visframe = r_framecount;
1773                                                 }
1774                                         }
1775                                         else
1776                                         {
1777                                                 for (;surf < surfend;surf++)
1778                                                 {
1779                                                         if (!(surf->flags & SURF_PLANEBACK))
1780                                                                 surf->visframe = r_framecount;
1781                                                 }
1782                                         }
1783                                 }
1784                         }
1785
1786                         // recurse down the children
1787                         if (node->children[0]->contents >= 0)
1788                         {
1789                                 if (node->children[1]->contents >= 0)
1790                                 {
1791                                         if (nodestackpos < 8192)
1792                                                 nodestack[nodestackpos++] = node->children[1];
1793                                         node = node->children[0];
1794                                         goto loc2;
1795                                 }
1796                                 else
1797                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1798                                 node = node->children[0];
1799                                 goto loc2;
1800                         }
1801                         else
1802                         {
1803                                 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1804                                 if (node->children[1]->contents >= 0)
1805                                 {
1806                                         node = node->children[1];
1807                                         goto loc2;
1808                                 }
1809                                 else if (nodestackpos > 0)
1810                                 {
1811                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1812                                         node = nodestack[--nodestackpos];
1813                                         goto loc2;
1814                                 }
1815                         }
1816                 }
1817                 else if (nodestackpos > 0)
1818                 {
1819                         node = nodestack[--nodestackpos];
1820                         goto loc2;
1821                 }
1822         }
1823 }
1824
1825 static int r_portalframecount = 0;
1826
1827 static void R_PVSWorldNode()
1828 {
1829         int portalstack, i;
1830         mportal_t *p, *pstack[8192];
1831         msurface_t *surf, **mark, **endmark;
1832         mleaf_t *leaf;
1833         tinyplane_t plane;
1834         byte *worldvis;
1835
1836         worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1837
1838         leaf = r_viewleaf;
1839         leaf->worldnodeframe = r_framecount;
1840         portalstack = 0;
1841 loc0:
1842         c_leafs++;
1843
1844         leaf->visframe = r_framecount;
1845
1846         if (leaf->nummarksurfaces)
1847         {
1848                 mark = leaf->firstmarksurface;
1849                 endmark = mark + leaf->nummarksurfaces;
1850                 if (r_ser.integer)
1851                 {
1852                         do
1853                         {
1854                                 surf = *mark++;
1855                                 // make sure surfaces are only processed once
1856                                 if (surf->worldnodeframe == r_framecount)
1857                                         continue;
1858                                 surf->worldnodeframe = r_framecount;
1859                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1860                                 {
1861                                         if (surf->flags & SURF_PLANEBACK)
1862                                         {
1863                                                 VectorNegate(surf->plane->normal, plane.normal);
1864                                                 plane.dist = -surf->plane->dist;
1865                                                 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1866                                         }
1867                                 }
1868                                 else
1869                                 {
1870                                         if (!(surf->flags & SURF_PLANEBACK))
1871                                                 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);
1872                                 }
1873                         }
1874                         while (mark < endmark);
1875                 }
1876                 else
1877                 {
1878                         do
1879                         {
1880                                 surf = *mark++;
1881                                 // make sure surfaces are only processed once
1882                                 if (surf->worldnodeframe == r_framecount)
1883                                         continue;
1884                                 surf->worldnodeframe = r_framecount;
1885                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1886                                 {
1887                                         if (surf->flags & SURF_PLANEBACK)
1888                                                 surf->visframe = r_framecount;
1889                                 }
1890                                 else
1891                                 {
1892                                         if (!(surf->flags & SURF_PLANEBACK))
1893                                                 surf->visframe = r_framecount;
1894                                 }
1895                         }
1896                         while (mark < endmark);
1897                 }
1898         }
1899
1900         // follow portals into other leafs
1901         for (p = leaf->portals;p;p = p->next)
1902         {
1903                 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1904                 {
1905                         leaf = p->past;
1906                         if (leaf->worldnodeframe != r_framecount)
1907                         {
1908                                 leaf->worldnodeframe = r_framecount;
1909                                 if (leaf->contents != CONTENTS_SOLID)
1910                                 {
1911                                         i = (leaf - cl.worldmodel->leafs) - 1;
1912                                         if (worldvis[i>>3] & (1<<(i&7)))
1913                                         {
1914                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1915                                                 {
1916                                                         pstack[portalstack++] = p;
1917                                                         goto loc0;
1918
1919 loc1:
1920                                                         p = pstack[--portalstack];
1921                                                 }
1922                                         }
1923                                 }
1924                         }
1925                 }
1926         }
1927
1928         if (portalstack)
1929                 goto loc1;
1930 }
1931
1932 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1933 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1934 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
1935 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1936 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1937
1938 int Cshader_count = 5;
1939 Cshader_t *Cshaders[5] =
1940 {
1941         &Cshader_wall_vertex,
1942         &Cshader_wall_lightmap,
1943         &Cshader_wall_fullbright,
1944         &Cshader_water,
1945         &Cshader_sky
1946 };
1947
1948 void R_PrepareSurfaces(void)
1949 {
1950         int i;
1951         texture_t *t;
1952         model_t *model;
1953         msurface_t *surf;
1954
1955         for (i = 0;i < Cshader_count;i++)
1956                 Cshaders[i]->chain = NULL;
1957
1958         model = currentrenderentity->model;
1959
1960         for (i = 0;i < model->nummodelsurfaces;i++)
1961         {
1962                 surf = model->modelsortedsurfaces[i];
1963                 if (surf->visframe == r_framecount)
1964                 {
1965                         if (surf->insertframe != r_framecount)
1966                         {
1967                                 surf->insertframe = r_framecount;
1968                                 c_faces++;
1969                                 // manually inlined R_TextureAnimation
1970                                 //t = R_TextureAnimation(surf->texinfo->texture);
1971                                 t = surf->texinfo->texture;
1972                                 if (t->alternate_anims != NULL && currentrenderentity->frame)
1973                                         t = t->alternate_anims;
1974                                 if (t->anim_total >= 2)
1975                                         t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1976                                 surf->currenttexture = t;
1977                         }
1978
1979                         surf->chain = surf->shader->chain;
1980                         surf->shader->chain = surf;
1981                 }
1982         }
1983 }
1984
1985 void R_DrawSurfaces (int type)
1986 {
1987         int                     i, stage;
1988         msurface_t      *surf;
1989         Cshader_t       *shader;
1990
1991         for (i = 0;i < Cshader_count;i++)
1992         {
1993                 shader = Cshaders[i];
1994                 if (shader->chain && shader->shaderfunc[type])
1995                         for (stage = 0;stage < 1000;stage++)
1996                                 for (surf = shader->chain;surf;surf = surf->chain)
1997                                         if (shader->shaderfunc[type](stage, surf))
1998                                                 goto done;
1999 done:;
2000         }
2001 }
2002
2003 static float portalpointbuffer[256][3];
2004
2005 void R_DrawPortals(void)
2006 {
2007         int drawportals, i;
2008 //      mleaf_t *leaf, *endleaf;
2009         mportal_t *portal, *endportal;
2010         mvertex_t *point/*, *endpoint*/;
2011         rmeshinfo_t m;
2012         drawportals = r_drawportals.integer;
2013         if (drawportals < 1)
2014                 return;
2015         /*
2016         leaf = cl.worldmodel->leafs;
2017         endleaf = leaf + cl.worldmodel->numleafs;
2018         for (;leaf < endleaf;leaf++)
2019         {
2020                 if (leaf->visframe == r_framecount && leaf->portals)
2021                 {
2022                         i = leaf - cl.worldmodel->leafs;
2023                         r = (i & 0x0007) << 5;
2024                         g = (i & 0x0038) << 2;
2025                         b = (i & 0x01C0) >> 1;
2026                         portal = leaf->portals;
2027                         while (portal)
2028                         {
2029                                 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2030                                 point = portal->points + portal->numpoints - 1;
2031                                 endpoint = portal->points;
2032                                 for (;point >= endpoint;point--)
2033                                         transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2034                                 transpolyend();
2035                                 portal = portal->next;
2036                         }
2037                 }
2038         }
2039         */
2040         memset(&m, 0, sizeof(m));
2041         m.transparent = true;
2042         m.blendfunc1 = GL_SRC_ALPHA;
2043         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2044         m.vertex = &portalpointbuffer[0][0];
2045         m.vertexstep = sizeof(float[3]);
2046         m.ca = 0.125;
2047         for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2048         {
2049                 if (portal->visframe == r_portalframecount)
2050                 {
2051                         if (portal->numpoints <= 256)
2052                         {
2053                                 i = portal - cl.worldmodel->portals;
2054                                 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2055                                 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2056                                 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2057                                 point = portal->points;
2058                                 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2059                                 {
2060                                         for (i = portal->numpoints - 1;i >= 0;i--)
2061                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2062                                 }
2063                                 else
2064                                 {
2065                                         for (i = 0;i < portal->numpoints;i++)
2066                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2067                                 }
2068                                 R_Mesh_DrawPolygon(&m, portal->numpoints);
2069                         }
2070                 }
2071         }
2072 }
2073
2074 void R_SetupForBModelRendering(void)
2075 {
2076         int                     i;
2077         msurface_t      *s;
2078         model_t         *model;
2079         vec3_t          modelorg;
2080
2081         // because bmodels can be reused, we have to decide which things to render
2082         // from scratch every time
2083
2084         model = currentrenderentity->model;
2085
2086         softwaretransformforentity (currentrenderentity);
2087         softwareuntransform(r_origin, modelorg);
2088
2089         for (i = 0;i < model->nummodelsurfaces;i++)
2090         {
2091                 s = model->modelsortedsurfaces[i];
2092                 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
2093                         s->visframe = r_framecount;
2094                 else
2095                         s->visframe = -1;
2096                 s->worldnodeframe = -1;
2097                 s->lightframe = -1;
2098                 s->dlightframe = -1;
2099                 s->insertframe = -1;
2100         }
2101 }
2102
2103 void R_SetupForWorldRendering(void)
2104 {
2105         // there is only one instance of the world, but it can be rendered in
2106         // multiple stages
2107
2108         currentrenderentity = &cl_entities[0].render;
2109         softwaretransformidentity();
2110 }
2111
2112 static void R_SurfMarkLights (void)
2113 {
2114         int                     i;
2115         msurface_t      *s;
2116
2117         if (r_dynamic.integer)
2118                 R_MarkLights();
2119
2120         if (!r_vertexsurfaces.integer)
2121         {
2122                 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2123                 {
2124                         s = currentrenderentity->model->modelsortedsurfaces[i];
2125                         if (s->visframe == r_framecount && s->lightmaptexture != NULL)
2126                         {
2127                                 if (s->cached_dlight
2128                                  || s->cached_ambient != r_ambient.value
2129                                  || s->cached_lightscalebit != lightscalebit)
2130                                         R_BuildLightMap(s, false); // base lighting changed
2131                                 else if (r_dynamic.integer)
2132                                 {
2133                                         if  (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
2134                                          || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
2135                                          || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
2136                                          || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
2137                                         //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
2138                                         // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
2139                                         // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
2140                                         // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
2141                                                 R_BuildLightMap(s, false); // base lighting changed
2142                                         else if (s->dlightframe == r_framecount && r_dlightmap.integer)
2143                                                 R_BuildLightMap(s, true); // only dlights
2144                                 }
2145                         }
2146                 }
2147         }
2148 }
2149
2150 void R_MarkWorldLights(void)
2151 {
2152         R_SetupForWorldRendering();
2153         R_SurfMarkLights();
2154 }
2155
2156 /*
2157 =============
2158 R_DrawWorld
2159 =============
2160 */
2161 void R_DrawWorld (void)
2162 {
2163         R_SetupForWorldRendering();
2164
2165         if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2166                 R_SolidWorldNode ();
2167         else
2168                 R_PVSWorldNode ();
2169 }
2170
2171 /*
2172 =================
2173 R_DrawBrushModel
2174 =================
2175 */
2176 void R_DrawBrushModelSky (void)
2177 {
2178         R_SetupForBModelRendering();
2179
2180         R_PrepareSurfaces();
2181         R_DrawSurfaces(SHADERSTAGE_SKY);
2182 }
2183
2184 void R_DrawBrushModelNormal (void)
2185 {
2186         c_bmodels++;
2187
2188         // have to flush queue because of possible lightmap reuse
2189         R_Mesh_Render();
2190
2191         R_SetupForBModelRendering();
2192
2193         R_SurfMarkLights();
2194
2195         R_PrepareSurfaces();
2196
2197         if (!skyrendermasked)
2198                 R_DrawSurfaces(SHADERSTAGE_SKY);
2199         R_DrawSurfaces(SHADERSTAGE_NORMAL);
2200         R_DrawSurfaces(SHADERSTAGE_FOG);
2201 }