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