no more warping meshs of any kind
[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].subtract * 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) >> 7;
126                                                         bl[1] += (green * k) >> 7;
127                                                         bl[2] += (blue  * k) >> 7;
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].subtract * 32768.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 = (32768.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 static void RSurfShader_Sky(msurface_t *firstsurf)
621 {
622         msurface_t *surf;
623         int i;
624         surfvertex_t *v;
625         surfmesh_t *mesh;
626         rmeshbufferinfo_t m;
627         float cr, cg, cb, ca;
628         float *outv, *outc;
629
630         // LordHavoc: HalfLife maps have freaky skypolys...
631         if (currentrenderentity->model->ishlbsp)
632                 return;
633
634         if (skyrendernow)
635         {
636                 skyrendernow = false;
637                 if (skyrendermasked)
638                         R_Sky();
639         }
640         for (surf = firstsurf;surf;surf = surf->chain)
641         {
642                 // draw depth-only polys
643                 memset(&m, 0, sizeof(m));
644                 m.transparent = false;
645                 if (skyrendermasked)
646                 {
647                         m.blendfunc1 = GL_ZERO;
648                         m.blendfunc2 = GL_ONE;
649                         m.depthwrite = true;
650                 }
651                 else
652                 {
653                         m.blendfunc1 = GL_ONE;
654                         m.blendfunc2 = GL_ZERO;
655                         m.depthwrite = false;
656                 }
657                 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
658                 {
659                         m.numtriangles = mesh->numtriangles;
660                         m.numverts = mesh->numverts;
661                         if (R_Mesh_Draw_GetBuffer(&m))
662                         {
663                                 cr = fogcolor[0] * m.colorscale;
664                                 cg = fogcolor[1] * m.colorscale;
665                                 cb = fogcolor[2] * m.colorscale;
666                                 ca = 1;
667                                 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
668                                 for (i = 0, v = mesh->vertex, outv = m.vertex;i < m.numverts;i++, v++, outv += 4, outc += 4)
669                                 {
670                                         softwaretransform(v->v, outv);
671                                 }
672                                 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
673                                 {
674                                         outc[0] = cr;
675                                         outc[1] = cg;
676                                         outc[2] = cb;
677                                         outc[3] = ca;
678                                 }
679                         }
680                 }
681         }
682 }
683
684 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
685 {
686         float f, *v, *c;
687         int i, l, lit = false;
688         rdlight_t *rd;
689         vec3_t lightorigin;
690         for (l = 0;l < r_numdlights;l++)
691         {
692                 if (dlightbits[l >> 5] & (1 << (l & 31)))
693                 {
694                         rd = &r_dlight[l];
695                         // FIXME: support softwareuntransform here and make bmodels use hardware transform?
696                         VectorCopy(rd->origin, lightorigin);
697                         for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
698                         {
699                                 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
700                                 if (f < rd->cullradius2)
701                                 {
702                                         f = (1.0f / f) - rd->subtract;
703                                         VectorMA(c, f, rd->light, c);
704                                         lit = true;
705                                 }
706                         }
707                 }
708         }
709         return lit;
710 }
711
712 // note: this untransforms lights to do the checking,
713 // and takes surf->mesh->vertex data
714 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
715 {
716         int i, l;
717         rdlight_t *rd;
718         vec3_t lightorigin;
719         surfvertex_t *sv;
720         for (l = 0;l < r_numdlights;l++)
721         {
722                 if (dlightbits[l >> 5] & (1 << (l & 31)))
723                 {
724                         rd = &r_dlight[l];
725                         softwareuntransform(rd->origin, lightorigin);
726                         for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
727                                 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
728                                         return true;
729                 }
730         }
731         return false;
732 }
733
734 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
735 {
736         int i, size3;
737         surfvertex_t *v;
738         float *outv, *outc, *outst, cl, diff[3];
739         float base[3], scale, f;
740         qbyte *lm;
741         surfmesh_t *mesh;
742         rmeshbufferinfo_t m;
743         float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
744         memset(&m, 0, sizeof(m));
745         if (currentrenderentity->effects & EF_ADDITIVE)
746         {
747                 m.transparent = true;
748                 m.blendfunc1 = GL_SRC_ALPHA;
749                 m.blendfunc2 = GL_ONE;
750         }
751         else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
752         {
753                 m.transparent = true;
754                 m.blendfunc1 = GL_SRC_ALPHA;
755                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
756         }
757         else
758         {
759                 m.transparent = false;
760                 m.blendfunc1 = GL_ONE;
761                 m.blendfunc2 = GL_ZERO;
762         }
763         m.depthwrite = false;
764         m.depthdisable = false;
765         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
766         if (surf->flags & SURF_DRAWFULLBRIGHT || currentrenderentity->effects & EF_FULLBRIGHT)
767         {
768                 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
769                 {
770                         m.numtriangles = mesh->numtriangles;
771                         m.numverts = mesh->numverts;
772                         if (R_Mesh_Draw_GetBuffer(&m))
773                         {
774                                 base[0] = base[1] = base[2] = 1.0f * m.colorscale;
775                                 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
776                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
777                                 {
778                                         softwaretransform(v->v, outv);
779                                         outv[3] = 1;
780                                         VectorCopy(base, outc);
781                                         outc[3] = alpha;
782                                         outst[0] = v->st[0];
783                                         outst[1] = v->st[1];
784                                         if (fogenabled)
785                                         {
786                                                 VectorSubtract(outv, r_origin, diff);
787                                                 f = 1 - exp(fogdensity/DotProduct(diff, diff));
788                                                 VectorScale(outc, f, outc);
789                                         }
790                                 }
791                         }
792                 }
793         }
794         else
795         {
796                 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
797                 base[0] = base[1] = base[2] = (r_ambient.value * (1.0f / 64.0f) + ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f));
798                 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
799                 {
800                         m.numtriangles = mesh->numtriangles;
801                         m.numverts = mesh->numverts;
802                         if (R_Mesh_Draw_GetBuffer(&m))
803                         {
804                                 cl = m.colorscale;
805                                 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
806                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
807                                 {
808                                         softwaretransform(v->v, outv);
809                                         outv[3] = 1;
810                                         VectorCopy(base, outc);
811                                         outc[3] = alpha;
812                                         outst[0] = v->st[0];
813                                         outst[1] = v->st[1];
814                                 }
815                                 if (surf->dlightframe == r_framecount)
816                                         RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
817                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
818                                 {
819                                         if (surf->flags & SURF_LIGHTMAP)
820                                         if (surf->styles[0] != 255)
821                                         {
822                                                 lm = surf->samples + v->lightmapoffset;
823                                                 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
824                                                 VectorMA(outc, scale, lm, outc);
825                                                 if (surf->styles[1] != 255)
826                                                 {
827                                                         lm += size3;
828                                                         scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
829                                                         VectorMA(outc, scale, lm, outc);
830                                                         if (surf->styles[2] != 255)
831                                                         {
832                                                                 lm += size3;
833                                                                 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
834                                                                 VectorMA(outc, scale, lm, outc);
835                                                                 if (surf->styles[3] != 255)
836                                                                 {
837                                                                         lm += size3;
838                                                                         scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
839                                                                         VectorMA(outc, scale, lm, outc);
840                                                                 }
841                                                         }
842                                                 }
843                                         }
844                                         if (fogenabled)
845                                         {
846                                                 VectorSubtract(outv, r_origin, diff);
847                                                 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
848                                                 VectorScale(outc, f, outc);
849                                         }
850                                         else
851                                                 VectorScale(outc, cl, outc);
852                                 }
853                         }
854                 }
855         }
856 }
857
858 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
859 {
860         int i;
861         surfvertex_t *v;
862         float *outv, *outc, *outst, diff[3];
863         float base[3], f;
864         surfmesh_t *mesh;
865         rmeshbufferinfo_t m;
866         float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
867         memset(&m, 0, sizeof(m));
868         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || alpha < 1;
869         m.blendfunc1 = GL_SRC_ALPHA;
870         m.blendfunc2 = GL_ONE;
871         m.depthwrite = false;
872         m.depthdisable = false;
873         m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
874         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
875         {
876                 m.numtriangles = mesh->numtriangles;
877                 m.numverts = mesh->numverts;
878                 if (R_Mesh_Draw_GetBuffer(&m))
879                 {
880                         VectorScale(fogcolor, m.colorscale, base);
881                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
882                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
883                         {
884                                 softwaretransform(v->v, outv);
885                                 outv[3] = 1;
886                                 VectorSubtract(outv, r_origin, diff);
887                                 f = exp(fogdensity/DotProduct(diff, diff));
888                                 VectorScale(base, f, outc);
889                                 outc[3] = alpha;
890                         }
891                         if (m.tex[0])
892                         {
893                                 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
894                                 {
895                                         outst[0] = v->st[0];
896                                         outst[1] = v->st[1];
897                                 }
898                         }
899                 }
900         }
901 }
902
903 static void RSurfShader_Water(msurface_t *firstsurf)
904 {
905         msurface_t *surf;
906         for (surf = firstsurf;surf;surf = surf->chain)
907                 RSurfShader_Water_Pass_Base(surf);
908         if (fogenabled)
909                 for (surf = firstsurf;surf;surf = surf->chain)
910                         RSurfShader_Water_Pass_Fog(surf);
911 }
912
913 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
914 {
915         int i;
916         surfvertex_t *v;
917         float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
918         surfmesh_t *mesh;
919         rmeshbufferinfo_t m;
920         memset(&m, 0, sizeof(m));
921         if (currentrenderentity->effects & EF_ADDITIVE)
922         {
923                 m.transparent = true;
924                 m.blendfunc1 = GL_SRC_ALPHA;
925                 m.blendfunc2 = GL_ONE;
926         }
927         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
928         {
929                 m.transparent = true;
930                 m.blendfunc1 = GL_SRC_ALPHA;
931                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
932         }
933         else
934         {
935                 m.transparent = false;
936                 m.blendfunc1 = GL_ONE;
937                 m.blendfunc2 = GL_ZERO;
938         }
939         m.depthwrite = false;
940         m.depthdisable = false;
941         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
942         m.tex[1] = R_GetTexture(surf->lightmaptexture);
943         ca = currentrenderentity->alpha;
944         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
945         {
946                 m.numtriangles = mesh->numtriangles;
947                 m.numverts = mesh->numverts;
948
949                 if (R_Mesh_Draw_GetBuffer(&m))
950                 {
951                         cl = (float) (1 << lightscalebit) * m.colorscale;
952                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
953                         if (fogenabled)
954                         {
955                                 if (softwaretransform_complexity)
956                                 {
957                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
958                                         {
959                                                 softwaretransform(v->v, outv);
960                                                 outv[3] = 1;
961                                                 VectorSubtract(outv, r_origin, diff);
962                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
963                                                 outc[3] = ca;
964                                                 outst[0] = v->st[0];
965                                                 outst[1] = v->st[1];
966                                                 outuv[0] = v->uv[0];
967                                                 outuv[1] = v->uv[1];
968                                         }
969                                 }
970                                 else
971                                 {
972                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
973                                         {
974                                                 VectorCopy(v->v, outv);
975                                                 outv[3] = 1;
976                                                 VectorSubtract(outv, r_origin, diff);
977                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
978                                                 outc[3] = ca;
979                                                 outst[0] = v->st[0];
980                                                 outst[1] = v->st[1];
981                                                 outuv[0] = v->uv[0];
982                                                 outuv[1] = v->uv[1];
983                                         }
984                                 }
985                         }
986                         else
987                         {
988                                 if (softwaretransform_complexity)
989                                 {
990                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
991                                         {
992                                                 softwaretransform(v->v, outv);
993                                                 outv[3] = 1;
994                                                 outc[0] = outc[1] = outc[2] = cl;
995                                                 outc[3] = ca;
996                                                 outst[0] = v->st[0];
997                                                 outst[1] = v->st[1];
998                                                 outuv[0] = v->uv[0];
999                                                 outuv[1] = v->uv[1];
1000                                         }
1001                                 }
1002                                 else
1003                                 {
1004                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1005                                         {
1006                                                 VectorCopy(v->v, outv);
1007                                                 outv[3] = 1;
1008                                                 outc[0] = outc[1] = outc[2] = cl;
1009                                                 outc[3] = ca;
1010                                                 outst[0] = v->st[0];
1011                                                 outst[1] = v->st[1];
1012                                                 outuv[0] = v->uv[0];
1013                                                 outuv[1] = v->uv[1];
1014                                         }
1015                                 }
1016                         }
1017                 }
1018         }
1019 }
1020
1021 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1022 {
1023         int i;
1024         surfvertex_t *v;
1025         float *outv, *outc, *outst, cl, ca;
1026         surfmesh_t *mesh;
1027         rmeshbufferinfo_t m;
1028         memset(&m, 0, sizeof(m));
1029         m.transparent = false;
1030         m.blendfunc1 = GL_ONE;
1031         m.blendfunc2 = GL_ZERO;
1032         m.depthwrite = false;
1033         m.depthdisable = false;
1034         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1035         ca = currentrenderentity->alpha;
1036         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1037         {
1038                 m.numtriangles = mesh->numtriangles;
1039                 m.numverts = mesh->numverts;
1040
1041                 if (R_Mesh_Draw_GetBuffer(&m))
1042                 {
1043                         cl = (float) (1 << lightscalebit) * m.colorscale;
1044                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1045                         if (softwaretransform_complexity)
1046                         {
1047                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1048                                 {
1049                                         softwaretransform(v->v, outv);
1050                                         outv[3] = 1;
1051                                         outc[0] = outc[1] = outc[2] = cl;
1052                                         outc[3] = ca;
1053                                         outst[0] = v->st[0];
1054                                         outst[1] = v->st[1];
1055                                 }
1056                         }
1057                         else
1058                         {
1059                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1060                                 {
1061                                         VectorCopy(v->v, outv);
1062                                         outv[3] = 1;
1063                                         outc[0] = outc[1] = outc[2] = cl;
1064                                         outc[3] = ca;
1065                                         outst[0] = v->st[0];
1066                                         outst[1] = v->st[1];
1067                                 }
1068                         }
1069                 }
1070         }
1071 }
1072
1073 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1074 {
1075         int i;
1076         surfvertex_t *v;
1077         float *outv, *outc, *outuv, cl, ca, diff[3];
1078         surfmesh_t *mesh;
1079         rmeshbufferinfo_t m;
1080         memset(&m, 0, sizeof(m));
1081         m.transparent = false;
1082         m.blendfunc1 = GL_ZERO;
1083         m.blendfunc2 = GL_SRC_COLOR;
1084         m.depthwrite = false;
1085         m.depthdisable = false;
1086         m.tex[0] = R_GetTexture(surf->lightmaptexture);
1087         ca = currentrenderentity->alpha;
1088         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1089         {
1090                 m.numtriangles = mesh->numtriangles;
1091                 m.numverts = mesh->numverts;
1092
1093                 if (R_Mesh_Draw_GetBuffer(&m))
1094                 {
1095                         cl = (float) (1 << lightscalebit) * m.colorscale;
1096                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1097                         if (fogenabled)
1098                         {
1099                                 if (softwaretransform_complexity)
1100                                 {
1101                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1102                                         {
1103                                                 softwaretransform(v->v, outv);
1104                                                 outv[3] = 1;
1105                                                 VectorSubtract(outv, r_origin, diff);
1106                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1107                                                 outc[3] = ca;
1108                                                 outuv[0] = v->uv[0];
1109                                                 outuv[1] = v->uv[1];
1110                                         }
1111                                 }
1112                                 else
1113                                 {
1114                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1115                                         {
1116                                                 VectorCopy(v->v, outv);
1117                                                 outv[3] = 1;
1118                                                 VectorSubtract(outv, r_origin, diff);
1119                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1120                                                 outc[3] = ca;
1121                                                 outuv[0] = v->uv[0];
1122                                                 outuv[1] = v->uv[1];
1123                                         }
1124                                 }
1125                         }
1126                         else
1127                         {
1128                                 if (softwaretransform_complexity)
1129                                 {
1130                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1131                                         {
1132                                                 softwaretransform(v->v, outv);
1133                                                 outv[3] = 1;
1134                                                 outc[0] = outc[1] = outc[2] = cl;
1135                                                 outc[3] = ca;
1136                                                 outuv[0] = v->uv[0];
1137                                                 outuv[1] = v->uv[1];
1138                                         }
1139                                 }
1140                                 else
1141                                 {
1142                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1143                                         {
1144                                                 VectorCopy(v->v, outv);
1145                                                 outv[3] = 1;
1146                                                 outc[0] = outc[1] = outc[2] = cl;
1147                                                 outc[3] = ca;
1148                                                 outuv[0] = v->uv[0];
1149                                                 outuv[1] = v->uv[1];
1150                                         }
1151                                 }
1152                         }
1153                 }
1154         }
1155 }
1156
1157 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1158 {
1159         int i, size3;
1160         surfvertex_t *v;
1161         float *outv, *outc, *outst, cl, ca, diff[3];
1162         float base[3], scale, f;
1163         qbyte *lm;
1164         surfmesh_t *mesh;
1165         rmeshbufferinfo_t m;
1166         memset(&m, 0, sizeof(m));
1167         if (currentrenderentity->effects & EF_ADDITIVE)
1168         {
1169                 m.transparent = true;
1170                 m.blendfunc1 = GL_SRC_ALPHA;
1171                 m.blendfunc2 = GL_ONE;
1172         }
1173         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1174         {
1175                 m.transparent = true;
1176                 m.blendfunc1 = GL_SRC_ALPHA;
1177                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1178         }
1179         else
1180         {
1181                 m.transparent = false;
1182                 m.blendfunc1 = GL_ONE;
1183                 m.blendfunc2 = GL_ZERO;
1184         }
1185         m.depthwrite = false;
1186         m.depthdisable = false;
1187         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1188
1189         size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1190
1191         base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1192
1193         ca = currentrenderentity->alpha;
1194         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1195         {
1196                 m.numtriangles = mesh->numtriangles;
1197                 m.numverts = mesh->numverts;
1198
1199                 if (R_Mesh_Draw_GetBuffer(&m))
1200                 {
1201                         cl = m.colorscale;
1202                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1203
1204                         if (currentrenderentity->effects & EF_FULLBRIGHT)
1205                         {
1206                                 if (softwaretransform_complexity)
1207                                 {
1208                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1209                                         {
1210                                                 softwaretransform(v->v, outv);
1211                                                 outv[3] = 1;
1212                                                 VectorSubtract(outv, r_origin, diff);
1213                                                 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1214                                                 outc[3] = ca;
1215                                                 outst[0] = v->st[0];
1216                                                 outst[1] = v->st[1];
1217                                         }
1218                                 }
1219                                 else
1220                                 {
1221                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1222                                         {
1223                                                 VectorCopy(v->v, outv);
1224                                                 outv[3] = 1;
1225                                                 VectorSubtract(outv, r_origin, diff);
1226                                                 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1227                                                 outc[3] = ca;
1228                                                 outst[0] = v->st[0];
1229                                                 outst[1] = v->st[1];
1230                                         }
1231                                 }
1232                         }
1233                         else
1234                         {
1235                                 if (softwaretransform_complexity)
1236                                 {
1237                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1238                                         {
1239                                                 softwaretransform(v->v, outv);
1240                                                 outv[3] = 1;
1241                                                 VectorCopy(base, outc);
1242                                                 outc[3] = ca;
1243                                                 outst[0] = v->st[0];
1244                                                 outst[1] = v->st[1];
1245                                         }
1246                                 }
1247                                 else
1248                                 {
1249                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1250                                         {
1251                                                 VectorCopy(v->v, outv);
1252                                                 outv[3] = 1;
1253                                                 VectorCopy(base, outc);
1254                                                 outc[3] = ca;
1255                                                 outst[0] = v->st[0];
1256                                                 outst[1] = v->st[1];
1257                                         }
1258                                 }
1259
1260                                 if (surf->dlightframe == r_framecount)
1261                                         RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1262
1263                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1264                                 {
1265                                         if (surf->styles[0] != 255)
1266                                         {
1267                                                 lm = surf->samples + v->lightmapoffset;
1268                                                 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1269                                                 VectorMA(outc, scale, lm, outc);
1270                                                 if (surf->styles[1] != 255)
1271                                                 {
1272                                                         lm += size3;
1273                                                         scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1274                                                         VectorMA(outc, scale, lm, outc);
1275                                                         if (surf->styles[2] != 255)
1276                                                         {
1277                                                                 lm += size3;
1278                                                                 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1279                                                                 VectorMA(outc, scale, lm, outc);
1280                                                                 if (surf->styles[3] != 255)
1281                                                                 {
1282                                                                         lm += size3;
1283                                                                         scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1284                                                                         VectorMA(outc, scale, lm, outc);
1285                                                                 }
1286                                                         }
1287                                                 }
1288                                         }
1289                                         if (fogenabled)
1290                                         {
1291                                                 VectorSubtract(outv, r_origin, diff);
1292                                                 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1293                                                 VectorScale(outc, f, outc);
1294                                         }
1295                                         else
1296                                                 VectorScale(outc, cl, outc);
1297                                 }
1298                         }
1299                 }
1300         }
1301 }
1302
1303 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1304 {
1305         int i;
1306         surfvertex_t *v;
1307         float *outv, *outc, *outst, cl, ca, diff[3];
1308         surfmesh_t *mesh;
1309         rmeshbufferinfo_t m;
1310         memset(&m, 0, sizeof(m));
1311         if (currentrenderentity->effects & EF_ADDITIVE)
1312         {
1313                 m.transparent = true;
1314                 m.blendfunc1 = GL_SRC_ALPHA;
1315                 m.blendfunc2 = GL_ONE;
1316         }
1317         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1318         {
1319                 m.transparent = true;
1320                 m.blendfunc1 = GL_SRC_ALPHA;
1321                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1322         }
1323         else
1324         {
1325                 m.transparent = false;
1326                 m.blendfunc1 = GL_ONE;
1327                 m.blendfunc2 = GL_ZERO;
1328         }
1329         m.depthwrite = false;
1330         m.depthdisable = false;
1331         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1332         ca = currentrenderentity->alpha;
1333         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1334         {
1335                 m.numtriangles = mesh->numtriangles;
1336                 m.numverts = mesh->numverts;
1337
1338                 if (R_Mesh_Draw_GetBuffer(&m))
1339                 {
1340                         cl = m.colorscale;
1341                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1342                         if (fogenabled)
1343                         {
1344                                 if (softwaretransform_complexity)
1345                                 {
1346                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1347                                         {
1348                                                 softwaretransform(v->v, outv);
1349                                                 outv[3] = 1;
1350                                                 VectorSubtract(outv, r_origin, diff);
1351                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1352                                                 outc[3] = ca;
1353                                                 outst[0] = v->st[0];
1354                                                 outst[1] = v->st[1];
1355                                         }
1356                                 }
1357                                 else
1358                                 {
1359                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1360                                         {
1361                                                 VectorCopy(v->v, outv);
1362                                                 outv[3] = 1;
1363                                                 VectorSubtract(outv, r_origin, diff);
1364                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1365                                                 outc[3] = ca;
1366                                                 outst[0] = v->st[0];
1367                                                 outst[1] = v->st[1];
1368                                         }
1369                                 }
1370                         }
1371                         else
1372                         {
1373                                 if (softwaretransform_complexity)
1374                                 {
1375                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1376                                         {
1377                                                 softwaretransform(v->v, outv);
1378                                                 outv[3] = 1;
1379                                                 outc[0] = outc[1] = outc[2] = cl;
1380                                                 outc[3] = ca;
1381                                                 outst[0] = v->st[0];
1382                                                 outst[1] = v->st[1];
1383                                         }
1384                                 }
1385                                 else
1386                                 {
1387                                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1388                                         {
1389                                                 VectorCopy(v->v, outv);
1390                                                 outv[3] = 1;
1391                                                 outc[0] = outc[1] = outc[2] = cl;
1392                                                 outc[3] = ca;
1393                                                 outst[0] = v->st[0];
1394                                                 outst[1] = v->st[1];
1395                                         }
1396                                 }
1397                         }
1398                 }
1399         }
1400 }
1401
1402 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1403 {
1404         int i;
1405         surfvertex_t *v;
1406         float *outv, *outc, *outst, cl, ca, diff[3], f;
1407         surfmesh_t *mesh;
1408         rmeshbufferinfo_t m;
1409
1410         if (surf->dlightframe != r_framecount)
1411                 return;
1412         if (currentrenderentity->effects & EF_FULLBRIGHT)
1413                 return;
1414
1415         memset(&m, 0, sizeof(m));
1416         if (currentrenderentity->effects & EF_ADDITIVE)
1417         {
1418                 m.transparent = true;
1419                 m.blendfunc1 = GL_SRC_ALPHA;
1420                 m.blendfunc2 = GL_ONE;
1421         }
1422         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1423         {
1424                 m.transparent = true;
1425                 m.blendfunc1 = GL_SRC_ALPHA;
1426                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1427         }
1428         else
1429         {
1430                 m.transparent = false;
1431                 m.blendfunc1 = GL_ONE;
1432                 m.blendfunc2 = GL_ZERO;
1433         }
1434         m.depthwrite = false;
1435         m.depthdisable = false;
1436         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1437         ca = currentrenderentity->alpha;
1438         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1439         {
1440                 if (RSurf_LightCheck(surf->dlightbits, mesh))
1441                 {
1442                         m.numtriangles = mesh->numtriangles;
1443                         m.numverts = mesh->numverts;
1444
1445                         if (R_Mesh_Draw_GetBuffer(&m))
1446                         {
1447                                 cl = m.colorscale;
1448                                 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1449                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1450                                 {
1451                                         softwaretransform(v->v, outv);
1452                                         outv[3] = 1;
1453                                         VectorClear(outc);
1454                                         outc[3] = ca;
1455                                         outst[0] = v->st[0];
1456                                         outst[1] = v->st[1];
1457                                 }
1458                                 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1459                                 if (fogenabled)
1460                                 {
1461                                         for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
1462                                         {
1463                                                 VectorSubtract(outv, r_origin, diff);
1464                                                 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1465                                                 VectorScale(outc, f, outc);
1466                                         }
1467                                 }
1468                                 else if (cl != 1)
1469                                         for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1470                                                 VectorScale(outc, cl, outc);
1471                         }
1472                 }
1473         }
1474 }
1475
1476 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1477 {
1478         int i;
1479         surfvertex_t *v;
1480         float *outv, *outc, *outst, cl, ca, diff[3];
1481         surfmesh_t *mesh;
1482         rmeshbufferinfo_t m;
1483         memset(&m, 0, sizeof(m));
1484         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1485         m.blendfunc1 = GL_SRC_ALPHA;
1486         m.blendfunc2 = GL_ONE;
1487         m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1488         ca = currentrenderentity->alpha;
1489         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1490         {
1491                 m.numtriangles = mesh->numtriangles;
1492                 m.numverts = mesh->numverts;
1493
1494                 if (R_Mesh_Draw_GetBuffer(&m))
1495                 {
1496                         cl = m.colorscale;
1497                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1498                         if (fogenabled)
1499                         {
1500                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1501                                 {
1502                                         softwaretransform(v->v, outv);
1503                                         outv[3] = 1;
1504                                         VectorSubtract(outv, r_origin, diff);
1505                                         outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1506                                         outc[3] = ca;
1507                                         outst[0] = v->st[0];
1508                                         outst[1] = v->st[1];
1509                                 }
1510                         }
1511                         else
1512                         {
1513                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1514                                 {
1515                                         softwaretransform(v->v, outv);
1516                                         outv[3] = 1;
1517                                         outc[0] = outc[1] = outc[2] = cl;
1518                                         outc[3] = ca;
1519                                         outst[0] = v->st[0];
1520                                         outst[1] = v->st[1];
1521                                 }
1522                         }
1523                 }
1524         }
1525 }
1526
1527 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1528 {
1529         int i;
1530         surfvertex_t *v;
1531         float *outv, *outc, *outst, cl, ca, diff[3], f;
1532         surfmesh_t *mesh;
1533         rmeshbufferinfo_t m;
1534         memset(&m, 0, sizeof(m));
1535         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1536         m.blendfunc1 = GL_SRC_ALPHA;
1537         m.blendfunc2 = GL_ONE;
1538         ca = currentrenderentity->alpha;
1539         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1540         {
1541                 m.numtriangles = mesh->numtriangles;
1542                 m.numverts = mesh->numverts;
1543
1544                 if (R_Mesh_Draw_GetBuffer(&m))
1545                 {
1546                         cl = m.colorscale;
1547                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1548                         if (softwaretransform_complexity)
1549                         {
1550                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1551                                 {
1552                                         softwaretransform(v->v, outv);
1553                                         outv[3] = 1;
1554                                         VectorSubtract(outv, r_origin, diff);
1555                                         f = cl * exp(fogdensity/DotProduct(diff, diff));
1556                                         VectorScale(fogcolor, f, outc);
1557                                         outc[3] = ca;
1558                                         outst[0] = v->st[0];
1559                                         outst[1] = v->st[1];
1560                                 }
1561                         }
1562                         else
1563                         {
1564                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1565                                 {
1566                                         VectorCopy(v->v, outv);
1567                                         outv[3] = 1;
1568                                         VectorSubtract(outv, r_origin, diff);
1569                                         VectorSubtract(outv, r_origin, diff);
1570                                         f = cl * exp(fogdensity/DotProduct(diff, diff));
1571                                         VectorScale(fogcolor, f, outc);
1572                                         outc[3] = ca;
1573                                         outst[0] = v->st[0];
1574                                         outst[1] = v->st[1];
1575                                 }
1576                         }
1577                 }
1578         }
1579 }
1580
1581 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1582 {
1583         msurface_t *surf;
1584         for (surf = firstsurf;surf;surf = surf->chain)
1585         {
1586                 c_brush_polys++;
1587                 RSurfShader_Wall_Pass_BaseFullbright(surf);
1588         }
1589         for (surf = firstsurf;surf;surf = surf->chain)
1590                 if (surf->currenttexture->glowtexture)
1591                         RSurfShader_Wall_Pass_Glow(surf);
1592         if (fogenabled)
1593                 for (surf = firstsurf;surf;surf = surf->chain)
1594                         RSurfShader_Wall_Pass_Fog(surf);
1595 }
1596
1597 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1598 {
1599         msurface_t *surf;
1600         for (surf = firstsurf;surf;surf = surf->chain)
1601         {
1602                 c_brush_polys++;
1603                 RSurfShader_Wall_Pass_BaseVertex(surf);
1604         }
1605         for (surf = firstsurf;surf;surf = surf->chain)
1606                 if (surf->currenttexture->glowtexture)
1607                         RSurfShader_Wall_Pass_Glow(surf);
1608         if (fogenabled)
1609                 for (surf = firstsurf;surf;surf = surf->chain)
1610                         RSurfShader_Wall_Pass_Fog(surf);
1611 }
1612
1613 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1614 {
1615         msurface_t *surf;
1616         if (r_vertexsurfaces.integer)
1617         {
1618                 for (surf = firstsurf;surf;surf = surf->chain)
1619                 {
1620                         c_brush_polys++;
1621                         RSurfShader_Wall_Pass_BaseVertex(surf);
1622                 }
1623                 for (surf = firstsurf;surf;surf = surf->chain)
1624                         if (surf->currenttexture->glowtexture)
1625                                 RSurfShader_Wall_Pass_Glow(surf);
1626                 if (fogenabled)
1627                         for (surf = firstsurf;surf;surf = surf->chain)
1628                                 RSurfShader_Wall_Pass_Fog(surf);
1629         }
1630         else if (r_multitexture.integer)
1631         {
1632                 if (r_dlightmap.integer)
1633                 {
1634                         for (surf = firstsurf;surf;surf = surf->chain)
1635                         {
1636                                 c_brush_polys++;
1637                                 RSurfShader_Wall_Pass_BaseMTex(surf);
1638                         }
1639                         for (surf = firstsurf;surf;surf = surf->chain)
1640                                 if (surf->currenttexture->glowtexture)
1641                                         RSurfShader_Wall_Pass_Glow(surf);
1642                         if (fogenabled)
1643                                 for (surf = firstsurf;surf;surf = surf->chain)
1644                                         RSurfShader_Wall_Pass_Fog(surf);
1645                 }
1646                 else
1647                 {
1648                         for (surf = firstsurf;surf;surf = surf->chain)
1649                         {
1650                                 c_brush_polys++;
1651                                 RSurfShader_Wall_Pass_BaseMTex(surf);
1652                         }
1653                         for (surf = firstsurf;surf;surf = surf->chain)
1654                                 if (surf->dlightframe == r_framecount)
1655                                         RSurfShader_Wall_Pass_Light(surf);
1656                         for (surf = firstsurf;surf;surf = surf->chain)
1657                                 if (surf->currenttexture->glowtexture)
1658                                         RSurfShader_Wall_Pass_Glow(surf);
1659                         if (fogenabled)
1660                                 for (surf = firstsurf;surf;surf = surf->chain)
1661                                         RSurfShader_Wall_Pass_Fog(surf);
1662                 }
1663         }
1664         else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1665         {
1666                 for (surf = firstsurf;surf;surf = surf->chain)
1667                 {
1668                         c_brush_polys++;
1669                         RSurfShader_Wall_Pass_BaseVertex(surf);
1670                 }
1671                 for (surf = firstsurf;surf;surf = surf->chain)
1672                         if (surf->currenttexture->glowtexture)
1673                                 RSurfShader_Wall_Pass_Glow(surf);
1674                 if (fogenabled)
1675                         for (surf = firstsurf;surf;surf = surf->chain)
1676                                 RSurfShader_Wall_Pass_Fog(surf);
1677         }
1678         else
1679         {
1680                 if (r_dlightmap.integer)
1681                 {
1682                         for (surf = firstsurf;surf;surf = surf->chain)
1683                         {
1684                                 c_brush_polys++;
1685                                 RSurfShader_Wall_Pass_BaseTexture(surf);
1686                         }
1687                         for (surf = firstsurf;surf;surf = surf->chain)
1688                                 RSurfShader_Wall_Pass_BaseLightmap(surf);
1689                         for (surf = firstsurf;surf;surf = surf->chain)
1690                                 if (surf->currenttexture->glowtexture)
1691                                         RSurfShader_Wall_Pass_Glow(surf);
1692                         if (fogenabled)
1693                                 for (surf = firstsurf;surf;surf = surf->chain)
1694                                         RSurfShader_Wall_Pass_Fog(surf);
1695                 }
1696                 else
1697                 {
1698                         for (surf = firstsurf;surf;surf = surf->chain)
1699                         {
1700                                 c_brush_polys++;
1701                                 RSurfShader_Wall_Pass_BaseTexture(surf);
1702                         }
1703                         for (surf = firstsurf;surf;surf = surf->chain)
1704                                 RSurfShader_Wall_Pass_BaseLightmap(surf);
1705                         for (surf = firstsurf;surf;surf = surf->chain)
1706                                 if (surf->dlightframe == r_framecount)
1707                                         RSurfShader_Wall_Pass_Light(surf);
1708                         for (surf = firstsurf;surf;surf = surf->chain)
1709                                 if (surf->currenttexture->glowtexture)
1710                                         RSurfShader_Wall_Pass_Glow(surf);
1711                         if (fogenabled)
1712                                 for (surf = firstsurf;surf;surf = surf->chain)
1713                                         RSurfShader_Wall_Pass_Fog(surf);
1714                 }
1715         }
1716 }
1717
1718 /*
1719 =============================================================
1720
1721         WORLD MODEL
1722
1723 =============================================================
1724 */
1725
1726 static void R_SolidWorldNode (void)
1727 {
1728         if (r_viewleaf->contents != CONTENTS_SOLID)
1729         {
1730                 int portalstack;
1731                 mportal_t *p, *pstack[8192];
1732                 msurface_t *surf, **mark, **endmark;
1733                 mleaf_t *leaf;
1734                 // LordHavoc: portal-passage worldnode; follows portals leading
1735                 // outward from viewleaf, if a portal leads offscreen it is not
1736                 // followed, in indoor maps this can often cull a great deal of
1737                 // geometry away when pvs data is not present (useful with pvs as well)
1738
1739                 leaf = r_viewleaf;
1740                 leaf->worldnodeframe = r_framecount;
1741                 portalstack = 0;
1742         loc0:
1743                 c_leafs++;
1744
1745                 leaf->visframe = r_framecount;
1746
1747                 if (leaf->nummarksurfaces)
1748                 {
1749                         mark = leaf->firstmarksurface;
1750                         endmark = mark + leaf->nummarksurfaces;
1751                         do
1752                         {
1753                                 surf = *mark++;
1754                                 // make sure surfaces are only processed once
1755                                 if (surf->worldnodeframe == r_framecount)
1756                                         continue;
1757                                 surf->worldnodeframe = r_framecount;
1758                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1759                                 {
1760                                         if (surf->flags & SURF_PLANEBACK)
1761                                                 surf->visframe = r_framecount;
1762                                 }
1763                                 else
1764                                 {
1765                                         if (!(surf->flags & SURF_PLANEBACK))
1766                                                 surf->visframe = r_framecount;
1767                                 }
1768                         }
1769                         while (mark < endmark);
1770                 }
1771
1772                 // follow portals into other leafs
1773                 p = leaf->portals;
1774                 for (;p;p = p->next)
1775                 {
1776                         if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1777                         {
1778                                 leaf = p->past;
1779                                 if (leaf->worldnodeframe != r_framecount)
1780                                 {
1781                                         leaf->worldnodeframe = r_framecount;
1782                                         if (leaf->contents != CONTENTS_SOLID)
1783                                         {
1784                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1785                                                 {
1786                                                         p->visframe = r_framecount;
1787                                                         pstack[portalstack++] = p;
1788                                                         goto loc0;
1789
1790         loc1:
1791                                                         p = pstack[--portalstack];
1792                                                 }
1793                                         }
1794                                 }
1795                         }
1796                 }
1797
1798                 if (portalstack)
1799                         goto loc1;
1800         }
1801         else
1802         {
1803                 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1804                 int nodestackpos = 0;
1805                 // LordHavoc: recursive descending worldnode; if portals are not
1806                 // available, this is a good last resort, can cull large amounts of
1807                 // geometry, but is more time consuming than portal-passage and renders
1808                 // things behind walls
1809
1810 loc2:
1811                 if (R_NotCulledBox(node->mins, node->maxs))
1812                 {
1813                         if (node->numsurfaces)
1814                         {
1815                                 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1816                                 if (PlaneDiff (r_origin, node->plane) < 0)
1817                                 {
1818                                         for (;surf < surfend;surf++)
1819                                         {
1820                                                 if (surf->flags & SURF_PLANEBACK)
1821                                                         surf->visframe = r_framecount;
1822                                         }
1823                                 }
1824                                 else
1825                                 {
1826                                         for (;surf < surfend;surf++)
1827                                         {
1828                                                 if (!(surf->flags & SURF_PLANEBACK))
1829                                                         surf->visframe = r_framecount;
1830                                         }
1831                                 }
1832                         }
1833
1834                         // recurse down the children
1835                         if (node->children[0]->contents >= 0)
1836                         {
1837                                 if (node->children[1]->contents >= 0)
1838                                 {
1839                                         if (nodestackpos < 8192)
1840                                                 nodestack[nodestackpos++] = node->children[1];
1841                                         node = node->children[0];
1842                                         goto loc2;
1843                                 }
1844                                 else
1845                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1846                                 node = node->children[0];
1847                                 goto loc2;
1848                         }
1849                         else
1850                         {
1851                                 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1852                                 if (node->children[1]->contents >= 0)
1853                                 {
1854                                         node = node->children[1];
1855                                         goto loc2;
1856                                 }
1857                                 else if (nodestackpos > 0)
1858                                 {
1859                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
1860                                         node = nodestack[--nodestackpos];
1861                                         goto loc2;
1862                                 }
1863                         }
1864                 }
1865                 else if (nodestackpos > 0)
1866                 {
1867                         node = nodestack[--nodestackpos];
1868                         goto loc2;
1869                 }
1870         }
1871 }
1872
1873 static int r_portalframecount = 0;
1874
1875 static void R_PVSWorldNode()
1876 {
1877         int portalstack, i;
1878         mportal_t *p, *pstack[8192];
1879         msurface_t *surf, **mark, **endmark;
1880         mleaf_t *leaf;
1881         qbyte *worldvis;
1882
1883         worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1884
1885         leaf = r_viewleaf;
1886         leaf->worldnodeframe = r_framecount;
1887         portalstack = 0;
1888 loc0:
1889         c_leafs++;
1890
1891         leaf->visframe = r_framecount;
1892
1893         if (leaf->nummarksurfaces)
1894         {
1895                 mark = leaf->firstmarksurface;
1896                 endmark = mark + leaf->nummarksurfaces;
1897                 do
1898                 {
1899                         surf = *mark++;
1900                         // make sure surfaces are only processed once
1901                         if (surf->worldnodeframe == r_framecount)
1902                                 continue;
1903                         surf->worldnodeframe = r_framecount;
1904                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1905                         {
1906                                 if (surf->flags & SURF_PLANEBACK)
1907                                         surf->visframe = r_framecount;
1908                         }
1909                         else
1910                         {
1911                                 if (!(surf->flags & SURF_PLANEBACK))
1912                                         surf->visframe = r_framecount;
1913                         }
1914                 }
1915                 while (mark < endmark);
1916         }
1917
1918         // follow portals into other leafs
1919         for (p = leaf->portals;p;p = p->next)
1920         {
1921                 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1922                 {
1923                         leaf = p->past;
1924                         if (leaf->worldnodeframe != r_framecount)
1925                         {
1926                                 leaf->worldnodeframe = r_framecount;
1927                                 if (leaf->contents != CONTENTS_SOLID)
1928                                 {
1929                                         i = (leaf - cl.worldmodel->leafs) - 1;
1930                                         if (worldvis[i>>3] & (1<<(i&7)))
1931                                         {
1932                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1933                                                 {
1934                                                         pstack[portalstack++] = p;
1935                                                         goto loc0;
1936
1937 loc1:
1938                                                         p = pstack[--portalstack];
1939                                                 }
1940                                         }
1941                                 }
1942                         }
1943                 }
1944         }
1945
1946         if (portalstack)
1947                 goto loc1;
1948 }
1949
1950 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1951 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1952 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1953 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1954 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1955
1956 int Cshader_count = 5;
1957 Cshader_t *Cshaders[5] =
1958 {
1959         &Cshader_wall_vertex,
1960         &Cshader_wall_lightmap,
1961         &Cshader_wall_fullbright,
1962         &Cshader_water,
1963         &Cshader_sky
1964 };
1965
1966 void R_PrepareSurfaces(void)
1967 {
1968         int i, alttextures, texframe, framecount;
1969         texture_t *t;
1970         model_t *model;
1971         msurface_t *surf;
1972
1973         for (i = 0;i < Cshader_count;i++)
1974                 Cshaders[i]->chain = NULL;
1975
1976         model = currentrenderentity->model;
1977         alttextures = currentrenderentity->frame != 0;
1978         texframe = (int)(cl.time * 5.0f);
1979
1980         for (i = 0;i < model->nummodelsurfaces;i++)
1981         {
1982                 surf = model->modelsortedsurfaces[i];
1983                 if (surf->visframe == r_framecount)
1984                 {
1985                         if (surf->insertframe != r_framecount)
1986                         {
1987                                 surf->insertframe = r_framecount;
1988                                 c_faces++;
1989                                 t = surf->texinfo->texture;
1990                                 if (t->animated)
1991                                 {
1992                                         framecount = t->anim_total[alttextures];
1993                                         if (framecount >= 2)
1994                                                 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1995                                         else
1996                                                 surf->currenttexture = t->anim_frames[alttextures][0];
1997                                 }
1998                                 else
1999                                         surf->currenttexture = t;
2000                         }
2001
2002                         surf->chain = surf->shader->chain;
2003                         surf->shader->chain = surf;
2004                 }
2005         }
2006 }
2007
2008 void R_DrawSurfaces (int type)
2009 {
2010         int                     i;
2011         Cshader_t       *shader;
2012
2013         for (i = 0;i < Cshader_count;i++)
2014         {
2015                 shader = Cshaders[i];
2016                 if (shader->chain && shader->shaderfunc[type])
2017                         shader->shaderfunc[type](shader->chain);
2018         }
2019 }
2020
2021 static float portalpointbuffer[256][3];
2022
2023 void R_DrawPortals(void)
2024 {
2025         int drawportals, i;
2026         mportal_t *portal, *endportal;
2027         mvertex_t *point;
2028         rmeshinfo_t m;
2029         drawportals = r_drawportals.integer;
2030
2031         if (drawportals < 1)
2032                 return;
2033
2034         memset(&m, 0, sizeof(m));
2035         m.transparent = true;
2036         m.blendfunc1 = GL_SRC_ALPHA;
2037         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2038         m.vertex = &portalpointbuffer[0][0];
2039         m.vertexstep = sizeof(float[3]);
2040         m.ca = 0.125;
2041         for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2042         {
2043                 if (portal->visframe == r_portalframecount)
2044                 {
2045                         if (portal->numpoints <= 256)
2046                         {
2047                                 i = portal - cl.worldmodel->portals;
2048                                 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2049                                 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2050                                 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2051                                 point = portal->points;
2052                                 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2053                                 {
2054                                         for (i = portal->numpoints - 1;i >= 0;i--)
2055                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2056                                 }
2057                                 else
2058                                 {
2059                                         for (i = 0;i < portal->numpoints;i++)
2060                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2061                                 }
2062                                 R_Mesh_DrawPolygon(&m, portal->numpoints);
2063                         }
2064                 }
2065         }
2066 }
2067
2068 void R_SetupForBModelRendering(void)
2069 {
2070         int                     i;
2071         msurface_t      *surf;
2072         model_t         *model;
2073         vec3_t          modelorg;
2074
2075         // because bmodels can be reused, we have to decide which things to render
2076         // from scratch every time
2077
2078         model = currentrenderentity->model;
2079
2080         softwaretransformforentity (currentrenderentity);
2081         softwareuntransform(r_origin, modelorg);
2082
2083         for (i = 0;i < model->nummodelsurfaces;i++)
2084         {
2085                 surf = model->modelsortedsurfaces[i];
2086                 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2087                         surf->visframe = r_framecount;
2088                 else
2089                         surf->visframe = -1;
2090                 surf->worldnodeframe = -1;
2091                 surf->lightframe = -1;
2092                 surf->dlightframe = -1;
2093                 surf->insertframe = -1;
2094         }
2095 }
2096
2097 void R_SetupForWorldRendering(void)
2098 {
2099         // there is only one instance of the world, but it can be rendered in
2100         // multiple stages
2101
2102         currentrenderentity = &cl_entities[0].render;
2103         softwaretransformidentity();
2104 }
2105
2106 static void R_SurfMarkLights (void)
2107 {
2108         int                     i;
2109         msurface_t      *surf;
2110
2111         if (r_dynamic.integer)
2112                 R_MarkLights();
2113
2114         if (!r_vertexsurfaces.integer)
2115         {
2116                 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2117                 {
2118                         surf = currentrenderentity->model->modelsortedsurfaces[i];
2119                         if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2120                         {
2121                                 if (surf->cached_dlight
2122                                  || surf->cached_ambient != r_ambient.value
2123                                  || surf->cached_lightscalebit != lightscalebit)
2124                                         R_BuildLightMap(surf, false); // base lighting changed
2125                                 else if (r_dynamic.integer)
2126                                 {
2127                                         if  (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2128                                          || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2129                                          || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2130                                          || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2131                                                 R_BuildLightMap(surf, false); // base lighting changed
2132                                         else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2133                                                 R_BuildLightMap(surf, true); // only dlights
2134                                 }
2135                         }
2136                 }
2137         }
2138 }
2139
2140 void R_MarkWorldLights(void)
2141 {
2142         R_SetupForWorldRendering();
2143         R_SurfMarkLights();
2144 }
2145
2146 /*
2147 =============
2148 R_DrawWorld
2149 =============
2150 */
2151 void R_DrawWorld (void)
2152 {
2153         R_SetupForWorldRendering();
2154
2155         if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2156                 R_SolidWorldNode ();
2157         else
2158                 R_PVSWorldNode ();
2159 }
2160
2161 /*
2162 =================
2163 R_DrawBrushModel
2164 =================
2165 */
2166 void R_DrawBrushModelSky (void)
2167 {
2168         R_SetupForBModelRendering();
2169
2170         R_PrepareSurfaces();
2171         R_DrawSurfaces(SHADERSTAGE_SKY);
2172 }
2173
2174 void R_DrawBrushModelNormal (void)
2175 {
2176         c_bmodels++;
2177
2178         // have to flush queue because of possible lightmap reuse
2179         R_Mesh_Render();
2180
2181         R_SetupForBModelRendering();
2182
2183         R_SurfMarkLights();
2184
2185         R_PrepareSurfaces();
2186
2187         if (!skyrendermasked)
2188                 R_DrawSurfaces(SHADERSTAGE_SKY);
2189         R_DrawSurfaces(SHADERSTAGE_NORMAL);
2190 }
2191
2192 static void gl_surf_start(void)
2193 {
2194 }
2195
2196 static void gl_surf_shutdown(void)
2197 {
2198 }
2199
2200 static void gl_surf_newmap(void)
2201 {
2202 }
2203
2204 void GL_Surf_Init(void)
2205 {
2206         int i;
2207         dlightdivtable[0] = 4194304;
2208         for (i = 1;i < 32768;i++)
2209                 dlightdivtable[i] = 4194304 / (i << 7);
2210
2211         Cvar_RegisterVariable(&r_ambient);
2212         Cvar_RegisterVariable(&r_vertexsurfaces);
2213         Cvar_RegisterVariable(&r_dlightmap);
2214         Cvar_RegisterVariable(&r_drawportals);
2215         Cvar_RegisterVariable(&r_testvis);
2216         Cvar_RegisterVariable(&r_floatbuildlightmap);
2217
2218         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
2219 }
2220