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