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