]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - gl_rsurf.c
rewrote most of the RSurfShader_ functions to use R_Mesh_Draw_GetBuffer instead of...
[xonotic/darkplaces.git] / gl_rsurf.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // r_surf.c: surface-related refresh code
21
22 #include "quakedef.h"
23
24 #define MAX_LIGHTMAP_SIZE 256
25
26 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28
29 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
30
31 cvar_t r_ambient = {0, "r_ambient", "0"};
32 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
33 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
37
38 static int dlightdivtable[32768];
39
40 static int R_IntAddDynamicLights (msurface_t *surf)
41 {
42         int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
43         unsigned int *bl;
44         float dist, impact[3], local[3];
45
46         // LordHavoc: use 64bit integer...  shame it's not very standardized...
47 #if _MSC_VER || __BORLANDC__
48         __int64     k;
49 #else
50         long long   k;
51 #endif
52
53         lit = false;
54
55         smax = (surf->extents[0] >> 4) + 1;
56         tmax = (surf->extents[1] >> 4) + 1;
57         smax3 = smax * 3;
58
59         for (lnum = 0; lnum < r_numdlights; lnum++)
60         {
61                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
62                         continue;                                       // not lit by this light
63
64                 softwareuntransform(r_dlight[lnum].origin, local);
65                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
66
67                 // for comparisons to minimum acceptable light
68                 // compensate for LIGHTOFFSET
69                 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
70
71                 dist2 = dist * dist;
72                 dist2 += LIGHTOFFSET;
73                 if (dist2 >= maxdist)
74                         continue;
75
76                 if (surf->plane->type < 3)
77                 {
78                         VectorCopy(local, impact);
79                         impact[surf->plane->type] -= dist;
80                 }
81                 else
82                 {
83                         impact[0] = local[0] - surf->plane->normal[0] * dist;
84                         impact[1] = local[1] - surf->plane->normal[1] * dist;
85                         impact[2] = local[2] - surf->plane->normal[2] * dist;
86                 }
87
88                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
89                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
90
91                 s = bound(0, impacts, smax * 16) - impacts;
92                 t = bound(0, impactt, tmax * 16) - impactt;
93                 i = s * s + t * t + dist2;
94                 if (i > maxdist)
95                         continue;
96
97                 // reduce calculations
98                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
99                         sdtable[s] = i * i + dist2;
100
101                 maxdist3 = maxdist - dist2;
102
103                 // convert to 8.8 blocklights format
104                 red = r_dlight[lnum].light[0];
105                 green = r_dlight[lnum].light[1];
106                 blue = r_dlight[lnum].light[2];
107                 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
108                 bl = intblocklights;
109
110                 i = impactt;
111                 for (t = 0;t < tmax;t++, i -= 16)
112                 {
113                         td = i * i;
114                         // make sure some part of it is visible on this line
115                         if (td < maxdist3)
116                         {
117                                 maxdist2 = maxdist - td;
118                                 for (s = 0;s < smax;s++)
119                                 {
120                                         if (sdtable[s] < maxdist2)
121                                         {
122                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
123                                                 if (k > 0)
124                                                 {
125                                                         bl[0] += (red   * k) >> 7;
126                                                         bl[1] += (green * k) >> 7;
127                                                         bl[2] += (blue  * k) >> 7;
128                                                         lit = true;
129                                                 }
130                                         }
131                                         bl += 3;
132                                 }
133                         }
134                         else // skip line
135                                 bl += smax3;
136                 }
137         }
138         return lit;
139 }
140
141 static int R_FloatAddDynamicLights (msurface_t *surf)
142 {
143         int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
144         float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
145
146         lit = false;
147
148         smax = (surf->extents[0] >> 4) + 1;
149         tmax = (surf->extents[1] >> 4) + 1;
150         smax3 = smax * 3;
151
152         for (lnum = 0; lnum < r_numdlights; lnum++)
153         {
154                 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155                         continue;                                       // not lit by this light
156
157                 softwareuntransform(r_dlight[lnum].origin, local);
158                 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
159
160                 // for comparisons to minimum acceptable light
161                 // compensate for LIGHTOFFSET
162                 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
163
164                 dist2 = dist * dist;
165                 dist2 += LIGHTOFFSET;
166                 if (dist2 >= maxdist)
167                         continue;
168
169                 if (surf->plane->type < 3)
170                 {
171                         VectorCopy(local, impact);
172                         impact[surf->plane->type] -= dist;
173                 }
174                 else
175                 {
176                         impact[0] = local[0] - surf->plane->normal[0] * dist;
177                         impact[1] = local[1] - surf->plane->normal[1] * dist;
178                         impact[2] = local[2] - surf->plane->normal[2] * dist;
179                 }
180
181                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
183
184                 td = bound(0, impacts, smax * 16) - impacts;
185                 td1 = bound(0, impactt, tmax * 16) - impactt;
186                 td = td * td + td1 * td1 + dist2;
187                 if (td > maxdist)
188                         continue;
189
190                 // reduce calculations
191                 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192                         sdtable[s] = td1 * td1 + dist2;
193
194                 maxdist3 = maxdist - dist2;
195
196                 // convert to 8.8 blocklights format
197                 red = r_dlight[lnum].light[0];
198                 green = r_dlight[lnum].light[1];
199                 blue = r_dlight[lnum].light[2];
200                 subtract = r_dlight[lnum].subtract * 32768.0f;
201                 bl = floatblocklights;
202
203                 td1 = impactt;
204                 for (t = 0;t < tmax;t++, td1 -= 16.0f)
205                 {
206                         td = td1 * td1;
207                         // make sure some part of it is visible on this line
208                         if (td < maxdist3)
209                         {
210                                 maxdist2 = maxdist - td;
211                                 for (s = 0;s < smax;s++)
212                                 {
213                                         if (sdtable[s] < maxdist2)
214                                         {
215                                                 k = (32768.0f / (sdtable[s] + td)) - subtract;
216                                                 bl[0] += red   * k;
217                                                 bl[1] += green * k;
218                                                 bl[2] += blue  * k;
219                                                 lit = true;
220                                         }
221                                         bl += 3;
222                                 }
223                         }
224                         else // skip line
225                                 bl += smax3;
226                 }
227         }
228         return lit;
229 }
230
231 /*
232 ===============
233 R_BuildLightMap
234
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
236 ===============
237 */
238 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
239 {
240         if (!r_floatbuildlightmap.integer)
241         {
242                 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
243                 unsigned int *bl, scale;
244                 qbyte *lightmap, *out, *stain;
245
246                 // update cached lighting info
247                 surf->cached_dlight = 0;
248                 surf->cached_lightscalebit = lightscalebit;
249                 surf->cached_ambient = r_ambient.value;
250                 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
251                 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
252                 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
253                 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
254
255                 smax = (surf->extents[0]>>4)+1;
256                 tmax = (surf->extents[1]>>4)+1;
257                 size = smax*tmax;
258                 size3 = size*3;
259                 lightmap = surf->samples;
260
261         // set to full bright if no light data
262                 bl = intblocklights;
263                 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
264                 {
265                         for (i = 0;i < size3;i++)
266                                 bl[i] = 255*256;
267                 }
268                 else
269                 {
270         // clear to no light
271                         j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
272                         if (j)
273                         {
274                                 for (i = 0;i < size3;i++)
275                                         *bl++ = j;
276                         }
277                         else
278                                 memset(bl, 0, size*3*sizeof(unsigned int));
279
280                         if (surf->dlightframe == r_framecount && r_dlightmap.integer)
281                         {
282                                 surf->cached_dlight = R_IntAddDynamicLights(surf);
283                                 if (surf->cached_dlight)
284                                         c_light_polys++;
285                                 else if (dlightchanged)
286                                         return; // don't upload if only updating dlights and none mattered
287                         }
288
289         // add all the lightmaps
290                         if (lightmap)
291                         {
292                                 bl = intblocklights;
293                                 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
294                                         for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
295                                                 bl[i] += lightmap[i] * scale;
296                         }
297                 }
298
299                 stain = surf->stainsamples;
300                 bl = intblocklights;
301                 out = templight;
302                 // deal with lightmap brightness scale
303                 shift = 7 + lightscalebit + 8;
304                 if (currentrenderentity->model->lightmaprgba)
305                 {
306                         stride = (surf->lightmaptexturestride - smax) * 4;
307                         for (i = 0;i < tmax;i++, out += stride)
308                         {
309                                 for (j = 0;j < smax;j++)
310                                 {
311                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
312                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314                                         *out++ = 255;
315                                 }
316                         }
317                 }
318                 else
319                 {
320                         stride = (surf->lightmaptexturestride - smax) * 3;
321                         for (i = 0;i < tmax;i++, out += stride)
322                         {
323                                 for (j = 0;j < smax;j++)
324                                 {
325                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
326                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327                                         l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
328                                 }
329                         }
330                 }
331
332                 R_UpdateTexture(surf->lightmaptexture, templight);
333         }
334         else
335         {
336                 int smax, tmax, i, j, size, size3, maps, stride, l;
337                 float *bl, scale;
338                 qbyte *lightmap, *out, *stain;
339
340                 // update cached lighting info
341                 surf->cached_dlight = 0;
342                 surf->cached_lightscalebit = lightscalebit;
343                 surf->cached_ambient = r_ambient.value;
344                 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
345                 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
346                 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
347                 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
348
349                 smax = (surf->extents[0]>>4)+1;
350                 tmax = (surf->extents[1]>>4)+1;
351                 size = smax*tmax;
352                 size3 = size*3;
353                 lightmap = surf->samples;
354
355         // set to full bright if no light data
356                 bl = floatblocklights;
357                 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
358                         j = 255*256;
359                 else
360                         j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
361
362                 // clear to no light
363                 if (j)
364                 {
365                         for (i = 0;i < size3;i++)
366                                 *bl++ = j;
367                 }
368                 else
369                         memset(bl, 0, size*3*sizeof(float));
370
371                 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
372                 {
373                         surf->cached_dlight = R_FloatAddDynamicLights(surf);
374                         if (surf->cached_dlight)
375                                 c_light_polys++;
376                         else if (dlightchanged)
377                                 return; // don't upload if only updating dlights and none mattered
378                 }
379
380                 // add all the lightmaps
381                 if (lightmap)
382                 {
383                         bl = floatblocklights;
384                         for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
385                                 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
386                                         bl[i] += lightmap[i] * scale;
387                 }
388
389                 stain = surf->stainsamples;
390                 bl = floatblocklights;
391                 out = templight;
392                 // deal with lightmap brightness scale
393                 scale = 1.0f / (1 << (7 + lightscalebit + 8));
394                 if (currentrenderentity->model->lightmaprgba)
395                 {
396                         stride = (surf->lightmaptexturestride - smax) * 4;
397                         for (i = 0;i < tmax;i++, out += stride)
398                         {
399                                 for (j = 0;j < smax;j++)
400                                 {
401                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
404                                         *out++ = 255;
405                                 }
406                         }
407                 }
408                 else
409                 {
410                         stride = (surf->lightmaptexturestride - smax) * 3;
411                         for (i = 0;i < tmax;i++, out += stride)
412                         {
413                                 for (j = 0;j < smax;j++)
414                                 {
415                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417                                         l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418                                 }
419                         }
420                 }
421
422                 R_UpdateTexture(surf->lightmaptexture, templight);
423         }
424 }
425
426 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
427 {
428         float ndist;
429         msurface_t *surf, *endsurf;
430         int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
431         qbyte *bl;
432         vec3_t impact;
433         // LordHavoc: use 64bit integer...  shame it's not very standardized...
434 #if _MSC_VER || __BORLANDC__
435         __int64     k;
436 #else
437         long long   k;
438 #endif
439
440
441         // for comparisons to minimum acceptable light
442         // compensate for 4096 offset
443         maxdist = radius * radius + 4096;
444
445         // clamp radius to avoid exceeding 32768 entry division table
446         if (maxdist > 4194304)
447                 maxdist = 4194304;
448
449         subtract = (int) ((1.0f / maxdist) * 4194304.0f);
450
451 loc0:
452         if (node->contents < 0)
453                 return;
454         ndist = PlaneDiff(origin, node->plane);
455         if (ndist > radius)
456         {
457                 node = node->children[0];
458                 goto loc0;
459         }
460         if (ndist < -radius)
461         {
462                 node = node->children[1];
463                 goto loc0;
464         }
465
466         dist2 = ndist * ndist;
467         dist2 += 4096.0f;
468         if (dist2 < maxdist)
469         {
470                 maxdist3 = maxdist - dist2;
471
472                 if (node->plane->type < 3)
473                 {
474                         VectorCopy(origin, impact);
475                         impact[node->plane->type] -= ndist;
476                 }
477                 else
478                 {
479                         impact[0] = origin[0] - node->plane->normal[0] * ndist;
480                         impact[1] = origin[1] - node->plane->normal[1] * ndist;
481                         impact[2] = origin[2] - node->plane->normal[2] * ndist;
482                 }
483
484                 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
485                 {
486                         if (surf->stainsamples)
487                         {
488                                 smax = (surf->extents[0] >> 4) + 1;
489                                 tmax = (surf->extents[1] >> 4) + 1;
490
491                                 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
492                                 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
493
494                                 s = bound(0, impacts, smax * 16) - impacts;
495                                 t = bound(0, impactt, tmax * 16) - impactt;
496                                 i = s * s + t * t + dist2;
497                                 if (i > maxdist)
498                                         continue;
499
500                                 // reduce calculations
501                                 for (s = 0, i = impacts; s < smax; s++, i -= 16)
502                                         sdtable[s] = i * i + dist2;
503
504                                 // convert to 8.8 blocklights format
505                                 bl = surf->stainsamples;
506                                 smax3 = smax * 3;
507                                 stained = false;
508
509                                 i = impactt;
510                                 for (t = 0;t < tmax;t++, i -= 16)
511                                 {
512                                         td = i * i;
513                                         // make sure some part of it is visible on this line
514                                         if (td < maxdist3)
515                                         {
516                                                 maxdist2 = maxdist - td;
517                                                 for (s = 0;s < smax;s++)
518                                                 {
519                                                         if (sdtable[s] < maxdist2)
520                                                         {
521                                                                 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
522                                                                 if (k > 0)
523                                                                 {
524                                                                         ratio = rand() & 255;
525                                                                         ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
526                                                                         a = (ca * k) >> 8;
527                                                                         if (a > 0)
528                                                                         {
529                                                                                 a = bound(0, a, 256);
530                                                                                 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
531                                                                                 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
532                                                                                 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
533                                                                                 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
534                                                                                 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
535                                                                                 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
536                                                                                 stained = true;
537                                                                         }
538                                                                 }
539                                                         }
540                                                         bl += 3;
541                                                 }
542                                         }
543                                         else // skip line
544                                                 bl += smax3;
545                                 }
546                                 // force lightmap upload
547                                 if (stained)
548                                         surf->cached_dlight = true;
549                         }
550                 }
551         }
552
553         if (node->children[0]->contents >= 0)
554         {
555                 if (node->children[1]->contents >= 0)
556                 {
557                         R_StainNode(node->children[0], model, origin, radius, icolor);
558                         node = node->children[1];
559                         goto loc0;
560                 }
561                 else
562                 {
563                         node = node->children[0];
564                         goto loc0;
565                 }
566         }
567         else if (node->children[1]->contents >= 0)
568         {
569                 node = node->children[1];
570                 goto loc0;
571         }
572 }
573
574 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
575 {
576         int n, icolor[8];
577         entity_render_t *ent;
578         model_t *model;
579         vec3_t org;
580         icolor[0] = cr1;
581         icolor[1] = cg1;
582         icolor[2] = cb1;
583         icolor[3] = ca1;
584         icolor[4] = cr2;
585         icolor[5] = cg2;
586         icolor[6] = cb2;
587         icolor[7] = ca2;
588
589         model = cl.worldmodel;
590         softwaretransformidentity();
591         R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
592
593         // look for embedded bmodels
594         for (n = 0;n < cl_num_brushmodel_entities;n++)
595         {
596                 ent = cl_brushmodel_entities[n];
597                 model = ent->model;
598                 if (model && model->name[0] == '*')
599                 {
600                         Mod_CheckLoaded(model);
601                         if (model->type == mod_brush)
602                         {
603                                 softwaretransformforentity(ent);
604                                 softwareuntransform(origin, org);
605                                 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
606                         }
607                 }
608         }
609 }
610
611
612 /*
613 =============================================================
614
615         BRUSH MODELS
616
617 =============================================================
618 */
619
620
621 static float turbsin[256] =
622 {
623         #include "gl_warp_sin.h"
624 };
625 #define TURBSCALE (256.0 / (2 * M_PI))
626
627 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
628 #define MAX_SURFVERTS 3072
629 typedef struct
630 {
631         float v[4];
632         float st[2];
633         float uv[2];
634         float c[4];
635 }
636 surfvert_t;
637 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
638
639 static void RSurfShader_Sky(msurface_t *firstsurf)
640 {
641         msurface_t *surf;
642         int i;
643         float number, length, dir[3], speedscale;
644         surfvertex_t *v;
645         surfvert_t *sv;
646         surfmesh_t *mesh;
647         rmeshinfo_t m;
648
649         // LordHavoc: HalfLife maps have freaky skypolys...
650         if (currentrenderentity->model->ishlbsp)
651                 return;
652
653         if (skyrendermasked)
654         {
655                 if (skyrendernow)
656                 {
657                         skyrendernow = false;
658                         R_Sky();
659                 }
660                 for (surf = firstsurf;surf;surf = surf->chain)
661                 {
662                         // draw depth-only polys
663                         memset(&m, 0, sizeof(m));
664                         m.transparent = false;
665                         m.blendfunc1 = GL_ZERO;
666                         m.blendfunc2 = GL_ONE;
667                         m.depthwrite = true;
668                         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
669                         {
670                                 m.numtriangles = mesh->numtriangles;
671                                 m.numverts = mesh->numverts;
672                                 m.index = mesh->index;
673                                 if (softwaretransform_complexity)
674                                 {
675                                         m.vertex = &svert[0].v[0];
676                                         m.vertexstep = sizeof(surfvert_t);
677                                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
678                                                 softwaretransform(v->v, sv->v);
679                                 }
680                                 else
681                                 {
682                                         m.vertex = &mesh->vertex[0].v[0];
683                                         m.vertexstep = sizeof(surfvertex_t);
684                                 }
685                                 R_Mesh_Draw(&m);
686                         }
687                 }
688         }
689         else if (skyrenderglquake)
690         {
691                 for (surf = firstsurf;surf;surf = surf->chain)
692                 {
693                         memset(&m, 0, sizeof(m));
694                         m.transparent = false;
695                         m.blendfunc1 = GL_ONE;
696                         m.blendfunc2 = GL_ZERO;
697                         m.vertex = &svert[0].v[0];
698                         m.vertexstep = sizeof(surfvert_t);
699                         m.cr = 1;
700                         m.cg = 1;
701                         m.cb = 1;
702                         m.ca = 1;
703                         m.tex[0] = R_GetTexture(solidskytexture);
704                         m.texcoords[0] = &svert[0].st[0];
705                         m.texcoordstep[0] = sizeof(surfvert_t);
706                         speedscale = cl.time * (8.0/128.0);
707                         speedscale -= (int)speedscale;
708                         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
709                         {
710                                 m.numtriangles = mesh->numtriangles;
711                                 m.numverts = mesh->numverts;
712                                 m.index = mesh->index;
713                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
714                                 {
715                                         softwaretransform(v->v, sv->v);
716                                         VectorSubtract (sv->v, r_origin, dir);
717                                         // flatten the sphere
718                                         dir[2] *= 3;
719
720                                         number = DotProduct(dir, dir);
721                                         #if SLOWMATH
722                                         length = 3.0f / sqrt(number);
723                                         #else
724                                         *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
725                                         length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
726                                         #endif
727
728                                         sv->st[0] = speedscale + dir[0] * length;
729                                         sv->st[1] = speedscale + dir[1] * length;
730                                 }
731                                 R_Mesh_Draw(&m);
732                         }
733                 }
734         }
735         else
736         {
737                 for (surf = firstsurf;surf;surf = surf->chain)
738                 {
739                         // flat color
740                         memset(&m, 0, sizeof(m));
741                         m.transparent = false;
742                         m.blendfunc1 = GL_ONE;
743                         m.blendfunc2 = GL_ZERO;
744                         m.cr = fogcolor[0];
745                         m.cg = fogcolor[1];
746                         m.cb = fogcolor[2];
747                         m.ca = 1;
748                         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
749                         {
750                                 m.numtriangles = mesh->numtriangles;
751                                 m.numverts = mesh->numverts;
752                                 m.index = mesh->index;
753                                 if (softwaretransform_complexity)
754                                 {
755                                         m.vertex = &svert[0].v[0];
756                                         m.vertexstep = sizeof(surfvert_t);
757                                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
758                                                 softwaretransform(v->v, sv->v);
759                                 }
760                                 else
761                                 {
762                                         m.vertex = &mesh->vertex[0].v[0];
763                                         m.vertexstep = sizeof(surfvertex_t);
764                                 }
765                                 R_Mesh_Draw(&m);
766                         }
767                 }
768         }
769         if (skyrenderglquake)
770         {
771                 for (surf = firstsurf;surf;surf = surf->chain)
772                 {
773                         memset(&m, 0, sizeof(m));
774                         m.transparent = false;
775                         m.blendfunc1 = GL_SRC_ALPHA;
776                         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
777                         m.vertex = &svert[0].v[0];
778                         m.vertexstep = sizeof(surfvert_t);
779                         m.cr = 1;
780                         m.cg = 1;
781                         m.cb = 1;
782                         m.ca = 1;
783                         m.tex[0] = R_GetTexture(alphaskytexture);
784                         m.texcoords[0] = &svert[0].st[0];
785                         m.texcoordstep[0] = sizeof(surfvert_t);
786                         speedscale = cl.time * (16.0/128.0);
787                         speedscale -= (int)speedscale;
788                         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
789                         {
790                                 m.numtriangles = mesh->numtriangles;
791                                 m.numverts = mesh->numverts;
792                                 m.index = mesh->index;
793                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
794                                 {
795                                         softwaretransform(v->v, sv->v);
796                                         VectorSubtract (sv->v, r_origin, dir);
797                                         // flatten the sphere
798                                         dir[2] *= 3;
799
800                                         number = DotProduct(dir, dir);
801                                         #if SLOWMATH
802                                         length = 3.0f / sqrt(number);
803                                         #else
804                                         *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
805                                         length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
806                                         #endif
807
808                                         sv->st[0] = speedscale + dir[0] * length;
809                                         sv->st[1] = speedscale + dir[1] * length;
810                                 }
811                                 R_Mesh_Draw(&m);
812                         }
813                 }
814         }
815 }
816
817 static int RSurf_Light(int *dlightbits, int numverts)
818 {
819         float f;
820         int i, l, lit = false;
821         rdlight_t *rd;
822         vec3_t lightorigin;
823         surfvert_t *sv;
824         for (l = 0;l < r_numdlights;l++)
825         {
826                 if (dlightbits[l >> 5] & (1 << (l & 31)))
827                 {
828                         rd = &r_dlight[l];
829                         // FIXME: support softwareuntransform here and make bmodels use hardware transform?
830                         VectorCopy(rd->origin, lightorigin);
831                         for (i = 0, sv = svert;i < numverts;i++, sv++)
832                         {
833                                 f = VectorDistance2(sv->v, lightorigin);
834                                 if (f < rd->cullradius2)
835                                 {
836                                         f = (1.0f / f) - rd->subtract;
837                                         sv->c[0] += rd->light[0] * f;
838                                         sv->c[1] += rd->light[1] * f;
839                                         sv->c[2] += rd->light[2] * f;
840                                         lit = true;
841                                 }
842                         }
843                 }
844         }
845         return lit;
846 }
847
848 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
849 {
850         float f, *v, *c;
851         int i, l, lit = false;
852         rdlight_t *rd;
853         vec3_t lightorigin;
854         for (l = 0;l < r_numdlights;l++)
855         {
856                 if (dlightbits[l >> 5] & (1 << (l & 31)))
857                 {
858                         rd = &r_dlight[l];
859                         // FIXME: support softwareuntransform here and make bmodels use hardware transform?
860                         VectorCopy(rd->origin, lightorigin);
861                         for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
862                         {
863                                 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
864                                 if (f < rd->cullradius2)
865                                 {
866                                         f = (1.0f / f) - rd->subtract;
867                                         VectorMA(c, f, rd->light, c);
868                                         lit = true;
869                                 }
870                         }
871                 }
872         }
873         return lit;
874 }
875
876 // note: this untransforms lights to do the checking,
877 // and takes surf->mesh->vertex data
878 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
879 {
880         int i, l;
881         rdlight_t *rd;
882         vec3_t lightorigin;
883         surfvertex_t *sv;
884         for (l = 0;l < r_numdlights;l++)
885         {
886                 if (dlightbits[l >> 5] & (1 << (l & 31)))
887                 {
888                         rd = &r_dlight[l];
889                         softwareuntransform(rd->origin, lightorigin);
890                         for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
891                                 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
892                                         return true;
893                 }
894         }
895         return false;
896 }
897
898 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
899 {
900 #if 0
901         int i, size3;
902         surfvertex_t *v;
903         float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
904         float base[3], scale, f;
905         qbyte *lm;
906         surfmesh_t *mesh;
907         rmeshbufferinfo_t m;
908         memset(&m, 0, sizeof(m));
909         if (currentrenderentity->effects & EF_ADDITIVE)
910         {
911                 m.transparent = true;
912                 m.blendfunc1 = GL_SRC_ALPHA;
913                 m.blendfunc2 = GL_ONE;
914         }
915         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
916         {
917                 m.transparent = true;
918                 m.blendfunc1 = GL_SRC_ALPHA;
919                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
920         }
921         else
922         {
923                 m.transparent = false;
924                 m.blendfunc1 = GL_ONE;
925                 m.blendfunc2 = GL_ZERO;
926         }
927         m.depthwrite = false;
928         m.depthdisable = false;
929         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
930
931         size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
932
933         base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f) + surf->flags & SURF_LIGHTMAP ? 0 : ;
934
935         ca = currentrenderentity->alpha;
936         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
937         {
938                 m.numtriangles = mesh->numtriangles;
939                 m.numverts = mesh->numverts;
940
941                 if (R_Mesh_Draw_GetBuffer(&m))
942                 {
943                         cl = m.colorscale;
944                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
945
946                         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)
947                         {
948                                 softwaretransform(v->v, outv);
949                                 outv[3] = 1;
950                                 VectorCopy(base, outc);
951                                 outc[3] = ca;
952                                 outst[0] = v->st[0];
953                                 outst[1] = v->st[1];
954                         }
955
956                         if (surf->dlightframe == r_framecount)
957                                 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
958
959                         for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
960                         {
961                                 if (surf->flags & SURF_LIGHTMAP)
962                                 if (surf->styles[0] != 255)
963                                 {
964                                         lm = surf->samples + v->lightmapoffset;
965                                         scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
966                                         VectorMA(outc, scale, lm, outc);
967                                         if (surf->styles[1] != 255)
968                                         {
969                                                 lm += size3;
970                                                 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
971                                                 VectorMA(outc, scale, lm, outc);
972                                                 if (surf->styles[2] != 255)
973                                                 {
974                                                         lm += size3;
975                                                         scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
976                                                         VectorMA(outc, scale, lm, outc);
977                                                         if (surf->styles[3] != 255)
978                                                         {
979                                                                 lm += size3;
980                                                                 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
981                                                                 VectorMA(outc, scale, lm, outc);
982                                                         }
983                                                 }
984                                         }
985                                 }
986                                 if (fogenabled)
987                                 {
988                                         VectorSubtract(outv, r_origin, diff);
989                                         f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
990                                         VectorScale(outc, f, outc);
991                                 }
992                                 else
993                                         VectorScale(outc, cl, outc);
994                         }
995                 }
996         }
997         int i;
998         surfvertex_t *v;
999         float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1000         surfmesh_t *mesh;
1001         rmeshbufferinfo_t m;
1002         memset(&m, 0, sizeof(m));
1003         if (currentrenderentity->effects & EF_ADDITIVE)
1004         {
1005                 m.transparent = true;
1006                 m.blendfunc1 = GL_SRC_ALPHA;
1007                 m.blendfunc2 = GL_ONE;
1008         }
1009         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1010         {
1011                 m.transparent = true;
1012                 m.blendfunc1 = GL_SRC_ALPHA;
1013                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1014         }
1015         else
1016         {
1017                 m.transparent = false;
1018                 m.blendfunc1 = GL_ONE;
1019                 m.blendfunc2 = GL_ZERO;
1020         }
1021         m.depthwrite = false;
1022         m.depthdisable = false;
1023         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1024         m.tex[1] = R_GetTexture(surf->lightmaptexture);
1025         ca = currentrenderentity->alpha;
1026         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1027         {
1028                 m.numtriangles = mesh->numtriangles;
1029                 m.numverts = mesh->numverts;
1030
1031                 if (R_Mesh_Draw_GetBuffer(&m))
1032                 {
1033                         cl = (float) (1 << lightscalebit) * m.colorscale;
1034                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1035                         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)
1036                         {
1037                                 softwaretransform(v->v, outv);
1038                                 if (r_waterripple.value)
1039                                         outv[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
1040                                 outv[3] = 1;
1041                                 if (fogenabled)
1042                                 {
1043                                         VectorSubtract(outv, r_origin, diff);
1044                                         outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1045                                 }
1046                                 else
1047                                         outc[0] = outc[1] = outc[2] = cl;
1048                                 outc[3] = ca;
1049                                 outst[0] = v->st[0];
1050                                 outst[1] = v->st[1];
1051                                 outuv[0] = v->uv[0];
1052                                 outuv[1] = v->uv[1];
1053                         }
1054                 }
1055         }
1056 #else
1057         int i;
1058         float diff[3], alpha, ifog;
1059         surfvertex_t *v;
1060         surfvert_t *sv;
1061         surfmesh_t *mesh;
1062         rmeshinfo_t m;
1063         alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1064
1065         memset(&m, 0, sizeof(m));
1066         if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
1067         {
1068                 m.transparent = true;
1069                 m.blendfunc1 = GL_SRC_ALPHA;
1070                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1071         }
1072         else
1073         {
1074                 m.transparent = false;
1075                 m.blendfunc1 = GL_ONE;
1076                 m.blendfunc2 = GL_ZERO;
1077         }
1078         m.vertex = &svert[0].v[0];
1079         m.vertexstep = sizeof(surfvert_t);
1080         m.color = &svert[0].c[0];
1081         m.colorstep = sizeof(surfvert_t);
1082         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1083         m.texcoords[0] = &svert[0].st[0];
1084         m.texcoordstep[0] = sizeof(surfvert_t);
1085         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1086         {
1087                 m.numtriangles = mesh->numtriangles;
1088                 m.numverts = mesh->numverts;
1089                 m.index = mesh->index;
1090                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1091                 {
1092                         softwaretransform(v->v, sv->v);
1093                         if (r_waterripple.value)
1094                                 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
1095                         if (surf->flags & SURF_DRAWFULLBRIGHT)
1096                         {
1097                                 sv->c[0] = 1;
1098                                 sv->c[1] = 1;
1099                                 sv->c[2] = 1;
1100                                 sv->c[3] = alpha;
1101                         }
1102                         else
1103                         {
1104                                 sv->c[0] = 0.5f;
1105                                 sv->c[1] = 0.5f;
1106                                 sv->c[2] = 0.5f;
1107                                 sv->c[3] = alpha;
1108                         }
1109                         sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1110                         sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1111                 }
1112                 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
1113                         RSurf_Light(surf->dlightbits, m.numverts);
1114                 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
1115                 {
1116                         for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1117                         {
1118                                 VectorSubtract(sv->v, r_origin, diff);
1119                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1120                                 sv->c[0] *= ifog;
1121                                 sv->c[1] *= ifog;
1122                                 sv->c[2] *= ifog;
1123                         }
1124                 }
1125                 R_Mesh_Draw(&m);
1126         }
1127 #endif
1128 }
1129
1130 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
1131 {
1132 #if 0
1133 #else
1134         int i;
1135         float alpha;
1136         surfvertex_t *v;
1137         surfvert_t *sv;
1138         surfmesh_t *mesh;
1139         rmeshinfo_t m;
1140         vec3_t diff;
1141         alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1142
1143         memset(&m, 0, sizeof(m));
1144         m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
1145         m.blendfunc1 = GL_SRC_ALPHA;
1146         m.blendfunc2 = GL_ONE;
1147         m.vertex = &svert[0].v[0];
1148         m.vertexstep = sizeof(surfvert_t);
1149         m.color = &svert[0].c[0];
1150         m.colorstep = sizeof(surfvert_t);
1151         m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1152         m.texcoords[0] = &svert[0].st[0];
1153         m.texcoordstep[0] = sizeof(surfvert_t);
1154
1155         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1156         {
1157                 m.numtriangles = mesh->numtriangles;
1158                 m.numverts = mesh->numverts;
1159                 m.index = mesh->index;
1160                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1161                 {
1162                         softwaretransform(v->v, sv->v);
1163                         if (r_waterripple.value)
1164                                 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
1165                         if (m.tex[0])
1166                         {
1167                                 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1168                                 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1169                         }
1170                         VectorSubtract(sv->v, r_origin, diff);
1171                         sv->c[0] = fogcolor[0];
1172                         sv->c[1] = fogcolor[1];
1173                         sv->c[2] = fogcolor[2];
1174                         sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
1175                 }
1176                 R_Mesh_Draw(&m);
1177         }
1178 #endif
1179 }
1180
1181 static void RSurfShader_Water(msurface_t *firstsurf)
1182 {
1183         msurface_t *surf;
1184         for (surf = firstsurf;surf;surf = surf->chain)
1185                 RSurfShader_Water_Pass_Base(surf);
1186         if (fogenabled)
1187                 for (surf = firstsurf;surf;surf = surf->chain)
1188                         RSurfShader_Water_Pass_Fog(surf);
1189 }
1190
1191 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
1192 {
1193 #if 1
1194         int i;
1195         surfvertex_t *v;
1196         float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1197         surfmesh_t *mesh;
1198         rmeshbufferinfo_t m;
1199         memset(&m, 0, sizeof(m));
1200         if (currentrenderentity->effects & EF_ADDITIVE)
1201         {
1202                 m.transparent = true;
1203                 m.blendfunc1 = GL_SRC_ALPHA;
1204                 m.blendfunc2 = GL_ONE;
1205         }
1206         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1207         {
1208                 m.transparent = true;
1209                 m.blendfunc1 = GL_SRC_ALPHA;
1210                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1211         }
1212         else
1213         {
1214                 m.transparent = false;
1215                 m.blendfunc1 = GL_ONE;
1216                 m.blendfunc2 = GL_ZERO;
1217         }
1218         m.depthwrite = false;
1219         m.depthdisable = false;
1220         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1221         m.tex[1] = R_GetTexture(surf->lightmaptexture);
1222         ca = currentrenderentity->alpha;
1223         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1224         {
1225                 m.numtriangles = mesh->numtriangles;
1226                 m.numverts = mesh->numverts;
1227
1228                 if (R_Mesh_Draw_GetBuffer(&m))
1229                 {
1230                         cl = (float) (1 << lightscalebit) * m.colorscale;
1231                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1232                         if (fogenabled)
1233                         {
1234                                 if (softwaretransform_complexity)
1235                                 {
1236                                         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)
1237                                         {
1238                                                 softwaretransform(v->v, outv);
1239                                                 outv[3] = 1;
1240                                                 VectorSubtract(outv, r_origin, diff);
1241                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1242                                                 outc[3] = ca;
1243                                                 outst[0] = v->st[0];
1244                                                 outst[1] = v->st[1];
1245                                                 outuv[0] = v->uv[0];
1246                                                 outuv[1] = v->uv[1];
1247                                         }
1248                                 }
1249                                 else
1250                                 {
1251                                         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)
1252                                         {
1253                                                 VectorCopy(v->v, outv);
1254                                                 outv[3] = 1;
1255                                                 VectorSubtract(outv, r_origin, diff);
1256                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1257                                                 outc[3] = ca;
1258                                                 outst[0] = v->st[0];
1259                                                 outst[1] = v->st[1];
1260                                                 outuv[0] = v->uv[0];
1261                                                 outuv[1] = v->uv[1];
1262                                         }
1263                                 }
1264                         }
1265                         else
1266                         {
1267                                 if (softwaretransform_complexity)
1268                                 {
1269                                         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)
1270                                         {
1271                                                 softwaretransform(v->v, outv);
1272                                                 outv[3] = 1;
1273                                                 outc[0] = outc[1] = outc[2] = cl;
1274                                                 outc[3] = ca;
1275                                                 outst[0] = v->st[0];
1276                                                 outst[1] = v->st[1];
1277                                                 outuv[0] = v->uv[0];
1278                                                 outuv[1] = v->uv[1];
1279                                         }
1280                                 }
1281                                 else
1282                                 {
1283                                         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)
1284                                         {
1285                                                 VectorCopy(v->v, outv);
1286                                                 outv[3] = 1;
1287                                                 outc[0] = outc[1] = outc[2] = cl;
1288                                                 outc[3] = ca;
1289                                                 outst[0] = v->st[0];
1290                                                 outst[1] = v->st[1];
1291                                                 outuv[0] = v->uv[0];
1292                                                 outuv[1] = v->uv[1];
1293                                         }
1294                                 }
1295                         }
1296                 }
1297         }
1298 #else
1299         int i;
1300         float diff[3], ifog;
1301         surfvertex_t *v;
1302         surfvert_t *sv;
1303         surfmesh_t *mesh;
1304         rmeshinfo_t m;
1305
1306         memset(&m, 0, sizeof(m));
1307         if (currentrenderentity->effects & EF_ADDITIVE)
1308         {
1309                 m.transparent = true;
1310                 m.blendfunc1 = GL_SRC_ALPHA;
1311                 m.blendfunc2 = GL_ONE;
1312         }
1313         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1314         {
1315                 m.transparent = true;
1316                 m.blendfunc1 = GL_SRC_ALPHA;
1317                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1318         }
1319         else
1320         {
1321                 m.transparent = false;
1322                 m.blendfunc1 = GL_ONE;
1323                 m.blendfunc2 = GL_ZERO;
1324         }
1325         m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
1326         m.ca = currentrenderentity->alpha;
1327         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1328         m.tex[1] = R_GetTexture(surf->lightmaptexture);
1329         m.texcoordstep[0] = sizeof(surfvertex_t);
1330         m.texcoordstep[1] = sizeof(surfvertex_t);
1331         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1332         {
1333                 m.numtriangles = mesh->numtriangles;
1334                 m.numverts = mesh->numverts;
1335                 m.index = mesh->index;
1336                 m.texcoords[0] = &mesh->vertex->st[0];
1337                 m.texcoords[1] = &mesh->vertex->uv[0];
1338                 if (fogenabled)
1339                 {
1340                         m.color = &svert[0].c[0];
1341                         m.colorstep = sizeof(surfvert_t);
1342                         if (softwaretransform_complexity)
1343                         {
1344                                 m.vertex = &svert[0].v[0];
1345                                 m.vertexstep = sizeof(surfvert_t);
1346                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1347                                 {
1348                                         softwaretransform(v->v, sv->v);
1349                                         VectorSubtract(sv->v, r_origin, diff);
1350                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1351                                         sv->c[0] = m.cr * ifog;
1352                                         sv->c[1] = m.cg * ifog;
1353                                         sv->c[2] = m.cb * ifog;
1354                                         sv->c[3] = m.ca;
1355                                 }
1356                         }
1357                         else
1358                         {
1359                                 m.vertex = &mesh->vertex->v[0];
1360                                 m.vertexstep = sizeof(surfvertex_t);
1361                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1362                                 {
1363                                         VectorSubtract(v->v, r_origin, diff);
1364                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1365                                         sv->c[0] = m.cr * ifog;
1366                                         sv->c[1] = m.cg * ifog;
1367                                         sv->c[2] = m.cb * ifog;
1368                                         sv->c[3] = m.ca;
1369                                 }
1370                         }
1371                 }
1372                 else
1373                 {
1374                         if (softwaretransform_complexity)
1375                         {
1376                                 m.vertex = &svert[0].v[0];
1377                                 m.vertexstep = sizeof(surfvert_t);
1378                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1379                                         softwaretransform(v->v, sv->v);
1380                         }
1381                         else
1382                         {
1383                                 m.vertex = &mesh->vertex->v[0];
1384                                 m.vertexstep = sizeof(surfvertex_t);
1385                         }
1386                 }
1387                 R_Mesh_Draw(&m);
1388         }
1389 #endif
1390 }
1391
1392 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1393 {
1394 #if 1
1395         int i;
1396         surfvertex_t *v;
1397         float *outv, *outc, *outst, cl, ca;
1398         surfmesh_t *mesh;
1399         rmeshbufferinfo_t m;
1400         memset(&m, 0, sizeof(m));
1401         m.transparent = false;
1402         m.blendfunc1 = GL_ONE;
1403         m.blendfunc2 = GL_ZERO;
1404         m.depthwrite = false;
1405         m.depthdisable = false;
1406         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1407         ca = currentrenderentity->alpha;
1408         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1409         {
1410                 m.numtriangles = mesh->numtriangles;
1411                 m.numverts = mesh->numverts;
1412
1413                 if (R_Mesh_Draw_GetBuffer(&m))
1414                 {
1415                         cl = (float) (1 << lightscalebit) * m.colorscale;
1416                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1417                         if (softwaretransform_complexity)
1418                         {
1419                                 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)
1420                                 {
1421                                         softwaretransform(v->v, outv);
1422                                         outv[3] = 1;
1423                                         outc[0] = outc[1] = outc[2] = cl;
1424                                         outc[3] = ca;
1425                                         outst[0] = v->st[0];
1426                                         outst[1] = v->st[1];
1427                                 }
1428                         }
1429                         else
1430                         {
1431                                 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)
1432                                 {
1433                                         VectorCopy(v->v, outv);
1434                                         outv[3] = 1;
1435                                         outc[0] = outc[1] = outc[2] = cl;
1436                                         outc[3] = ca;
1437                                         outst[0] = v->st[0];
1438                                         outst[1] = v->st[1];
1439                                 }
1440                         }
1441                 }
1442         }
1443 #else
1444         int i;
1445         surfvertex_t *v;
1446         surfvert_t *sv;
1447         surfmesh_t *mesh;
1448         rmeshinfo_t m;
1449
1450         memset(&m, 0, sizeof(m));
1451         m.transparent = false;
1452         m.blendfunc1 = GL_ONE;
1453         m.blendfunc2 = GL_ZERO;
1454         m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1455         m.ca = 1;
1456         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1457         m.texcoordstep[0] = sizeof(surfvertex_t);
1458         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1459         {
1460                 m.numtriangles = mesh->numtriangles;
1461                 m.numverts = mesh->numverts;
1462                 m.index = mesh->index;
1463                 m.texcoords[0] = &mesh->vertex->st[0];
1464                 if (softwaretransform_complexity)
1465                 {
1466                         m.vertex = &svert[0].v[0];
1467                         m.vertexstep = sizeof(surfvert_t);
1468                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1469                                 softwaretransform(v->v, sv->v);
1470                 }
1471                 else
1472                 {
1473                         m.vertex = &mesh->vertex->v[0];
1474                         m.vertexstep = sizeof(surfvertex_t);
1475                 }
1476                 R_Mesh_Draw(&m);
1477         }
1478 #endif
1479 }
1480
1481 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1482 {
1483 #if 1
1484         int i;
1485         surfvertex_t *v;
1486         float *outv, *outc, *outuv, cl, ca, diff[3];
1487         surfmesh_t *mesh;
1488         rmeshbufferinfo_t m;
1489         memset(&m, 0, sizeof(m));
1490         m.transparent = false;
1491         m.blendfunc1 = GL_ZERO;
1492         m.blendfunc2 = GL_SRC_COLOR;
1493         m.depthwrite = false;
1494         m.depthdisable = false;
1495         m.tex[0] = R_GetTexture(surf->lightmaptexture);
1496         ca = currentrenderentity->alpha;
1497         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1498         {
1499                 m.numtriangles = mesh->numtriangles;
1500                 m.numverts = mesh->numverts;
1501
1502                 if (R_Mesh_Draw_GetBuffer(&m))
1503                 {
1504                         cl = (float) (1 << lightscalebit) * m.colorscale;
1505                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1506                         if (fogenabled)
1507                         {
1508                                 if (softwaretransform_complexity)
1509                                 {
1510                                         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)
1511                                         {
1512                                                 softwaretransform(v->v, outv);
1513                                                 outv[3] = 1;
1514                                                 VectorSubtract(outv, r_origin, diff);
1515                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1516                                                 outc[3] = ca;
1517                                                 outuv[0] = v->uv[0];
1518                                                 outuv[1] = v->uv[1];
1519                                         }
1520                                 }
1521                                 else
1522                                 {
1523                                         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)
1524                                         {
1525                                                 VectorCopy(v->v, outv);
1526                                                 outv[3] = 1;
1527                                                 VectorSubtract(outv, r_origin, diff);
1528                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1529                                                 outc[3] = ca;
1530                                                 outuv[0] = v->uv[0];
1531                                                 outuv[1] = v->uv[1];
1532                                         }
1533                                 }
1534                         }
1535                         else
1536                         {
1537                                 if (softwaretransform_complexity)
1538                                 {
1539                                         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)
1540                                         {
1541                                                 softwaretransform(v->v, outv);
1542                                                 outv[3] = 1;
1543                                                 outc[0] = outc[1] = outc[2] = cl;
1544                                                 outc[3] = ca;
1545                                                 outuv[0] = v->uv[0];
1546                                                 outuv[1] = v->uv[1];
1547                                         }
1548                                 }
1549                                 else
1550                                 {
1551                                         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)
1552                                         {
1553                                                 VectorCopy(v->v, outv);
1554                                                 outv[3] = 1;
1555                                                 outc[0] = outc[1] = outc[2] = cl;
1556                                                 outc[3] = ca;
1557                                                 outuv[0] = v->uv[0];
1558                                                 outuv[1] = v->uv[1];
1559                                         }
1560                                 }
1561                         }
1562                 }
1563         }
1564 #else
1565         int i;
1566         float diff[3], ifog;
1567         surfvertex_t *v;
1568         surfvert_t *sv;
1569         surfmesh_t *mesh;
1570         rmeshinfo_t m;
1571
1572         memset(&m, 0, sizeof(m));
1573         m.transparent = false;
1574         m.blendfunc1 = GL_ZERO;
1575         m.blendfunc2 = GL_SRC_COLOR;
1576         m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1577         m.ca = 1;
1578         m.tex[0] = R_GetTexture(surf->lightmaptexture);
1579         m.texcoordstep[0] = sizeof(surfvertex_t);
1580         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1581         {
1582                 m.numtriangles = mesh->numtriangles;
1583                 m.numverts = mesh->numverts;
1584                 m.index = mesh->index;
1585                 m.texcoords[0] = &mesh->vertex->uv[0];
1586                 if (fogenabled)
1587                 {
1588                         m.color = &svert[0].c[0];
1589                         m.colorstep = sizeof(surfvert_t);
1590                         if (softwaretransform_complexity)
1591                         {
1592                                 m.vertex = &svert[0].v[0];
1593                                 m.vertexstep = sizeof(surfvert_t);
1594                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1595                                 {
1596                                         softwaretransform(v->v, sv->v);
1597                                         VectorSubtract(sv->v, r_origin, diff);
1598                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1599                                         sv->c[0] = m.cr * ifog;
1600                                         sv->c[1] = m.cg * ifog;
1601                                         sv->c[2] = m.cb * ifog;
1602                                         sv->c[3] = m.ca;
1603                                 }
1604                         }
1605                         else
1606                         {
1607                                 m.vertex = &mesh->vertex->v[0];
1608                                 m.vertexstep = sizeof(surfvertex_t);
1609                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1610                                 {
1611                                         VectorSubtract(v->v, r_origin, diff);
1612                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1613                                         sv->c[0] = m.cr * ifog;
1614                                         sv->c[1] = m.cg * ifog;
1615                                         sv->c[2] = m.cb * ifog;
1616                                         sv->c[3] = m.ca;
1617                                 }
1618                         }
1619                 }
1620                 else
1621                 {
1622                         if (softwaretransform_complexity)
1623                         {
1624                                 m.vertex = &svert[0].v[0];
1625                                 m.vertexstep = sizeof(surfvert_t);
1626                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1627                                         softwaretransform(v->v, sv->v);
1628                         }
1629                         else
1630                         {
1631                                 m.vertex = &mesh->vertex->v[0];
1632                                 m.vertexstep = sizeof(surfvertex_t);
1633                         }
1634                 }
1635                 R_Mesh_Draw(&m);
1636         }
1637 #endif
1638 }
1639
1640 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1641 {
1642 #if 1
1643         int i, size3;
1644         surfvertex_t *v;
1645         float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1646         float base[3], scale, f;
1647         qbyte *lm;
1648         surfmesh_t *mesh;
1649         rmeshbufferinfo_t m;
1650         memset(&m, 0, sizeof(m));
1651         if (currentrenderentity->effects & EF_ADDITIVE)
1652         {
1653                 m.transparent = true;
1654                 m.blendfunc1 = GL_SRC_ALPHA;
1655                 m.blendfunc2 = GL_ONE;
1656         }
1657         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1658         {
1659                 m.transparent = true;
1660                 m.blendfunc1 = GL_SRC_ALPHA;
1661                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1662         }
1663         else
1664         {
1665                 m.transparent = false;
1666                 m.blendfunc1 = GL_ONE;
1667                 m.blendfunc2 = GL_ZERO;
1668         }
1669         m.depthwrite = false;
1670         m.depthdisable = false;
1671         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1672
1673         size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1674
1675         base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1676
1677         ca = currentrenderentity->alpha;
1678         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1679         {
1680                 m.numtriangles = mesh->numtriangles;
1681                 m.numverts = mesh->numverts;
1682
1683                 if (R_Mesh_Draw_GetBuffer(&m))
1684                 {
1685                         cl = m.colorscale;
1686                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1687
1688                         if (currentrenderentity->effects & EF_FULLBRIGHT)
1689                         {
1690                                 if (softwaretransform_complexity)
1691                                 {
1692                                         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)
1693                                         {
1694                                                 softwaretransform(v->v, outv);
1695                                                 outv[3] = 1;
1696                                                 VectorSubtract(outv, r_origin, diff);
1697                                                 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1698                                                 outc[3] = ca;
1699                                                 outst[0] = v->st[0];
1700                                                 outst[1] = v->st[1];
1701                                         }
1702                                 }
1703                                 else
1704                                 {
1705                                         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)
1706                                         {
1707                                                 VectorCopy(v->v, outv);
1708                                                 outv[3] = 1;
1709                                                 VectorSubtract(outv, r_origin, diff);
1710                                                 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1711                                                 outc[3] = ca;
1712                                                 outst[0] = v->st[0];
1713                                                 outst[1] = v->st[1];
1714                                         }
1715                                 }
1716                         }
1717                         else
1718                         {
1719                                 if (softwaretransform_complexity)
1720                                 {
1721                                         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)
1722                                         {
1723                                                 softwaretransform(v->v, outv);
1724                                                 outv[3] = 1;
1725                                                 VectorCopy(base, outc);
1726                                                 outc[3] = ca;
1727                                                 outst[0] = v->st[0];
1728                                                 outst[1] = v->st[1];
1729                                         }
1730                                 }
1731                                 else
1732                                 {
1733                                         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)
1734                                         {
1735                                                 VectorCopy(v->v, outv);
1736                                                 outv[3] = 1;
1737                                                 VectorCopy(base, outc);
1738                                                 outc[3] = ca;
1739                                                 outst[0] = v->st[0];
1740                                                 outst[1] = v->st[1];
1741                                         }
1742                                 }
1743
1744                                 if (surf->dlightframe == r_framecount)
1745                                         RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1746
1747                                 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1748                                 {
1749                                         if (surf->styles[0] != 255)
1750                                         {
1751                                                 lm = surf->samples + v->lightmapoffset;
1752                                                 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1753                                                 VectorMA(outc, scale, lm, outc);
1754                                                 if (surf->styles[1] != 255)
1755                                                 {
1756                                                         lm += size3;
1757                                                         scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1758                                                         VectorMA(outc, scale, lm, outc);
1759                                                         if (surf->styles[2] != 255)
1760                                                         {
1761                                                                 lm += size3;
1762                                                                 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1763                                                                 VectorMA(outc, scale, lm, outc);
1764                                                                 if (surf->styles[3] != 255)
1765                                                                 {
1766                                                                         lm += size3;
1767                                                                         scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1768                                                                         VectorMA(outc, scale, lm, outc);
1769                                                                 }
1770                                                         }
1771                                                 }
1772                                         }
1773                                         if (fogenabled)
1774                                         {
1775                                                 VectorSubtract(outv, r_origin, diff);
1776                                                 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1777                                                 VectorScale(outc, f, outc);
1778                                         }
1779                                         else
1780                                                 VectorScale(outc, cl, outc);
1781                                 }
1782                         }
1783                 }
1784         }
1785 #else
1786         int i, size3;
1787         float c[3], base[3], scale, diff[3], ifog;
1788         surfvertex_t *v;
1789         surfvert_t *sv;
1790         surfmesh_t *mesh;
1791         rmeshinfo_t m;
1792         qbyte *lm;
1793
1794         size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1795
1796         base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 128.0f);
1797
1798         memset(&m, 0, sizeof(m));
1799         if (currentrenderentity->effects & EF_ADDITIVE)
1800         {
1801                 m.transparent = true;
1802                 m.blendfunc1 = GL_SRC_ALPHA;
1803                 m.blendfunc2 = GL_ONE;
1804         }
1805         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1806         {
1807                 m.transparent = true;
1808                 m.blendfunc1 = GL_SRC_ALPHA;
1809                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1810         }
1811         else
1812         {
1813                 m.transparent = false;
1814                 m.blendfunc1 = GL_ONE;
1815                 m.blendfunc2 = GL_ZERO;
1816         }
1817         m.vertex = &svert[0].v[0];
1818         m.vertexstep = sizeof(surfvert_t);
1819         m.color = &svert[0].c[0];
1820         m.colorstep = sizeof(surfvert_t);
1821         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1822         m.texcoordstep[0] = sizeof(surfvertex_t);
1823         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1824         {
1825                 m.numtriangles = mesh->numtriangles;
1826                 m.numverts = mesh->numverts;
1827                 m.index = mesh->index;
1828                 m.texcoords[0] = &mesh->vertex->st[0];
1829                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1830                 {
1831                         softwaretransform(v->v, sv->v);
1832                         VectorCopy(base, c);
1833                         if (surf->styles[0] != 255)
1834                         {
1835                                 lm = surf->samples + v->lightmapoffset;
1836                                 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1837                                 VectorMA(c, scale, lm, c);
1838                                 if (surf->styles[1] != 255)
1839                                 {
1840                                         lm += size3;
1841                                         scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1842                                         VectorMA(c, scale, lm, c);
1843                                         if (surf->styles[2] != 255)
1844                                         {
1845                                                 lm += size3;
1846                                                 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1847                                                 VectorMA(c, scale, lm, c);
1848                                                 if (surf->styles[3] != 255)
1849                                                 {
1850                                                         lm += size3;
1851                                                         scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1852                                                         VectorMA(c, scale, lm, c);
1853                                                 }
1854                                         }
1855                                 }
1856                         }
1857                         sv->c[0] = c[0];
1858                         sv->c[1] = c[1];
1859                         sv->c[2] = c[2];
1860                         sv->c[3] = currentrenderentity->alpha;
1861                 }
1862                 if (surf->dlightframe == r_framecount)
1863                         RSurf_Light(surf->dlightbits, m.numverts);
1864                 if (fogenabled)
1865                 {
1866                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1867                         {
1868                                 VectorSubtract(sv->v, r_origin, diff);
1869                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1870                                 sv->c[0] *= ifog;
1871                                 sv->c[1] *= ifog;
1872                                 sv->c[2] *= ifog;
1873                         }
1874                 }
1875                 R_Mesh_Draw(&m);
1876         }
1877 #endif
1878 }
1879
1880 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1881 {
1882 #if 1
1883         int i;
1884         surfvertex_t *v;
1885         float *outv, *outc, *outst, cl, ca, diff[3];
1886         surfmesh_t *mesh;
1887         rmeshbufferinfo_t m;
1888         memset(&m, 0, sizeof(m));
1889         if (currentrenderentity->effects & EF_ADDITIVE)
1890         {
1891                 m.transparent = true;
1892                 m.blendfunc1 = GL_SRC_ALPHA;
1893                 m.blendfunc2 = GL_ONE;
1894         }
1895         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1896         {
1897                 m.transparent = true;
1898                 m.blendfunc1 = GL_SRC_ALPHA;
1899                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1900         }
1901         else
1902         {
1903                 m.transparent = false;
1904                 m.blendfunc1 = GL_ONE;
1905                 m.blendfunc2 = GL_ZERO;
1906         }
1907         m.depthwrite = false;
1908         m.depthdisable = false;
1909         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1910         ca = currentrenderentity->alpha;
1911         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1912         {
1913                 m.numtriangles = mesh->numtriangles;
1914                 m.numverts = mesh->numverts;
1915
1916                 if (R_Mesh_Draw_GetBuffer(&m))
1917                 {
1918                         cl = m.colorscale;
1919                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1920                         if (fogenabled)
1921                         {
1922                                 if (softwaretransform_complexity)
1923                                 {
1924                                         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)
1925                                         {
1926                                                 softwaretransform(v->v, outv);
1927                                                 outv[3] = 1;
1928                                                 VectorSubtract(outv, r_origin, diff);
1929                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1930                                                 outc[3] = ca;
1931                                                 outst[0] = v->st[0];
1932                                                 outst[1] = v->st[1];
1933                                         }
1934                                 }
1935                                 else
1936                                 {
1937                                         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)
1938                                         {
1939                                                 VectorCopy(v->v, outv);
1940                                                 outv[3] = 1;
1941                                                 VectorSubtract(outv, r_origin, diff);
1942                                                 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1943                                                 outc[3] = ca;
1944                                                 outst[0] = v->st[0];
1945                                                 outst[1] = v->st[1];
1946                                         }
1947                                 }
1948                         }
1949                         else
1950                         {
1951                                 if (softwaretransform_complexity)
1952                                 {
1953                                         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)
1954                                         {
1955                                                 softwaretransform(v->v, outv);
1956                                                 outv[3] = 1;
1957                                                 outc[0] = outc[1] = outc[2] = cl;
1958                                                 outc[3] = ca;
1959                                                 outst[0] = v->st[0];
1960                                                 outst[1] = v->st[1];
1961                                         }
1962                                 }
1963                                 else
1964                                 {
1965                                         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)
1966                                         {
1967                                                 VectorCopy(v->v, outv);
1968                                                 outv[3] = 1;
1969                                                 outc[0] = outc[1] = outc[2] = cl;
1970                                                 outc[3] = ca;
1971                                                 outst[0] = v->st[0];
1972                                                 outst[1] = v->st[1];
1973                                         }
1974                                 }
1975                         }
1976                 }
1977         }
1978 #else
1979         int i;
1980         float diff[3], ifog;
1981         surfvertex_t *v;
1982         surfvert_t *sv;
1983         surfmesh_t *mesh;
1984         rmeshinfo_t m;
1985
1986         memset(&m, 0, sizeof(m));
1987         if (currentrenderentity->effects & EF_ADDITIVE)
1988         {
1989                 m.transparent = true;
1990                 m.blendfunc1 = GL_SRC_ALPHA;
1991                 m.blendfunc2 = GL_ONE;
1992         }
1993         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1994         {
1995                 m.transparent = true;
1996                 m.blendfunc1 = GL_SRC_ALPHA;
1997                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1998         }
1999         else
2000         {
2001                 m.transparent = false;
2002                 m.blendfunc1 = GL_ONE;
2003                 m.blendfunc2 = GL_ZERO;
2004         }
2005         m.vertex = &svert[0].v[0];
2006         m.vertexstep = sizeof(surfvert_t);
2007         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2008         m.texcoordstep[0] = sizeof(surfvertex_t);
2009         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2010         {
2011                 m.numtriangles = mesh->numtriangles;
2012                 m.numverts = mesh->numverts;
2013                 m.index = mesh->index;
2014                 m.texcoords[0] = &mesh->vertex->st[0];
2015                 if (fogenabled)
2016                 {
2017                         m.color = &svert[0].c[0];
2018                         m.colorstep = sizeof(surfvert_t);
2019                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2020                         {
2021                                 softwaretransform(v->v, sv->v);
2022                                 VectorSubtract(sv->v, r_origin, diff);
2023                                 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2024                                 sv->c[0] = ifog;
2025                                 sv->c[1] = ifog;
2026                                 sv->c[2] = ifog;
2027                                 sv->c[3] = currentrenderentity->alpha;
2028                         }
2029                 }
2030                 else
2031                 {
2032                         m.cr = m.cg = m.cb = 1;
2033                         m.ca = currentrenderentity->alpha;
2034                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2035                                 softwaretransform(v->v, sv->v);
2036                 }
2037                 R_Mesh_Draw(&m);
2038         }
2039 #endif
2040 }
2041
2042 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
2043 {
2044 #if 1
2045         int i;
2046         surfvertex_t *v;
2047         float *outv, *outc, *outst, *outuv, cl, ca, diff[3], f;
2048         surfmesh_t *mesh;
2049         rmeshbufferinfo_t m;
2050
2051         if (surf->dlightframe != r_framecount)
2052                 return;
2053         if (currentrenderentity->effects & EF_FULLBRIGHT)
2054                 return;
2055
2056         memset(&m, 0, sizeof(m));
2057         if (currentrenderentity->effects & EF_ADDITIVE)
2058         {
2059                 m.transparent = true;
2060                 m.blendfunc1 = GL_SRC_ALPHA;
2061                 m.blendfunc2 = GL_ONE;
2062         }
2063         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2064         {
2065                 m.transparent = true;
2066                 m.blendfunc1 = GL_SRC_ALPHA;
2067                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2068         }
2069         else
2070         {
2071                 m.transparent = false;
2072                 m.blendfunc1 = GL_ONE;
2073                 m.blendfunc2 = GL_ZERO;
2074         }
2075         m.depthwrite = false;
2076         m.depthdisable = false;
2077         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2078         ca = currentrenderentity->alpha;
2079         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2080         {
2081                 if (RSurf_LightCheck(surf->dlightbits, mesh))
2082                 {
2083                         m.numtriangles = mesh->numtriangles;
2084                         m.numverts = mesh->numverts;
2085
2086                         if (R_Mesh_Draw_GetBuffer(&m))
2087                         {
2088                                 cl = m.colorscale;
2089                                 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2090                                 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)
2091                                 {
2092                                         softwaretransform(v->v, outv);
2093                                         outv[3] = 1;
2094                                         VectorClear(outc);
2095                                         outc[3] = ca;
2096                                         outst[0] = v->st[0];
2097                                         outst[1] = v->st[1];
2098                                 }
2099                                 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
2100                                 if (fogenabled)
2101                                 {
2102                                         for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
2103                                         {
2104                                                 VectorSubtract(outv, r_origin, diff);
2105                                                 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2106                                                 VectorScale(outc, f, outc);
2107                                         }
2108                                 }
2109                                 else if (cl != 1)
2110                                         for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
2111                                                 VectorScale(outc, cl, outc);
2112                         }
2113                 }
2114         }
2115 #else
2116         int i;
2117         float diff[3], ifog;
2118         surfvertex_t *v;
2119         surfvert_t *sv;
2120         surfmesh_t *mesh;
2121         rmeshinfo_t m;
2122
2123         memset(&m, 0, sizeof(m));
2124         if (currentrenderentity->effects & EF_ADDITIVE)
2125                 m.transparent = true;
2126         else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2127                 m.transparent = true;
2128         else
2129                 m.transparent = false;
2130         m.blendfunc1 = GL_SRC_ALPHA;
2131         m.blendfunc2 = GL_ONE;
2132         m.vertex = &svert[0].v[0];
2133         m.vertexstep = sizeof(surfvert_t);
2134         m.color = &svert[0].c[0];
2135         m.colorstep = sizeof(surfvert_t);
2136         m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2137         m.texcoordstep[0] = sizeof(surfvertex_t);
2138         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2139         {
2140                 m.numtriangles = mesh->numtriangles;
2141                 m.numverts = mesh->numverts;
2142                 m.index = mesh->index;
2143                 m.texcoords[0] = &mesh->vertex->st[0];
2144                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2145                 {
2146                         softwaretransform(v->v, sv->v);
2147                         sv->c[0] = 0;
2148                         sv->c[1] = 0;
2149                         sv->c[2] = 0;
2150                         sv->c[3] = currentrenderentity->alpha;
2151                 }
2152                 if (RSurf_Light(surf->dlightbits, m.numverts))
2153                 {
2154                         if (fogenabled)
2155                         {
2156                                 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
2157                                 {
2158                                         VectorSubtract(sv->v, r_origin, diff);
2159                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2160                                         sv->c[0] *= ifog;
2161                                         sv->c[1] *= ifog;
2162                                         sv->c[2] *= ifog;
2163                                 }
2164                         }
2165                         R_Mesh_Draw(&m);
2166                 }
2167         }
2168 #endif
2169 }
2170
2171 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
2172 {
2173 #if 1
2174         int i;
2175         surfvertex_t *v;
2176         float *outv, *outc, *outst, cl, ca, diff[3];
2177         surfmesh_t *mesh;
2178         rmeshbufferinfo_t m;
2179         memset(&m, 0, sizeof(m));
2180         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2181         m.blendfunc1 = GL_SRC_ALPHA;
2182         m.blendfunc2 = GL_ONE;
2183         m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2184         ca = currentrenderentity->alpha;
2185         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2186         {
2187                 m.numtriangles = mesh->numtriangles;
2188                 m.numverts = mesh->numverts;
2189
2190                 if (R_Mesh_Draw_GetBuffer(&m))
2191                 {
2192                         cl = m.colorscale;
2193                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2194                         if (fogenabled)
2195                         {
2196                                 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)
2197                                 {
2198                                         softwaretransform(v->v, outv);
2199                                         outv[3] = 1;
2200                                         VectorSubtract(outv, r_origin, diff);
2201                                         outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2202                                         outc[3] = ca;
2203                                         outst[0] = v->st[0];
2204                                         outst[1] = v->st[1];
2205                                 }
2206                         }
2207                         else
2208                         {
2209                                 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)
2210                                 {
2211                                         softwaretransform(v->v, outv);
2212                                         outv[3] = 1;
2213                                         outc[0] = outc[1] = outc[2] = cl;
2214                                         outc[3] = ca;
2215                                         outst[0] = v->st[0];
2216                                         outst[1] = v->st[1];
2217                                 }
2218                         }
2219                 }
2220         }
2221 #else
2222         int i;
2223         float diff[3], ifog;
2224         surfvertex_t *v;
2225         surfvert_t *sv;
2226         surfmesh_t *mesh;
2227         rmeshinfo_t m;
2228
2229         memset(&m, 0, sizeof(m));
2230         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2231         m.blendfunc1 = GL_SRC_ALPHA;
2232         m.blendfunc2 = GL_ONE;
2233         m.cr = 1;
2234         m.cg = 1;
2235         m.cb = 1;
2236         m.ca = currentrenderentity->alpha;
2237         m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2238         m.texcoordstep[0] = sizeof(surfvertex_t);
2239         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2240         {
2241                 m.numtriangles = mesh->numtriangles;
2242                 m.numverts = mesh->numverts;
2243                 m.index = mesh->index;
2244                 m.texcoords[0] = &mesh->vertex->st[0];
2245                 if (fogenabled)
2246                 {
2247                         m.color = &svert[0].c[0];
2248                         m.colorstep = sizeof(surfvert_t);
2249                         if (softwaretransform_complexity)
2250                         {
2251                                 m.vertex = &svert[0].v[0];
2252                                 m.vertexstep = sizeof(surfvert_t);
2253                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2254                                 {
2255                                         softwaretransform(v->v, sv->v);
2256                                         VectorSubtract(sv->v, r_origin, diff);
2257                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2258                                         sv->c[0] = m.cr * ifog;
2259                                         sv->c[1] = m.cg * ifog;
2260                                         sv->c[2] = m.cb * ifog;
2261                                         sv->c[3] = m.ca;
2262                                 }
2263                         }
2264                         else
2265                         {
2266                                 m.vertex = &mesh->vertex->v[0];
2267                                 m.vertexstep = sizeof(surfvertex_t);
2268                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2269                                 {
2270                                         VectorSubtract(v->v, r_origin, diff);
2271                                         ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2272                                         sv->c[0] = m.cr * ifog;
2273                                         sv->c[1] = m.cg * ifog;
2274                                         sv->c[2] = m.cb * ifog;
2275                                         sv->c[3] = m.ca;
2276                                 }
2277                         }
2278                 }
2279                 else
2280                 {
2281                         if (softwaretransform_complexity)
2282                         {
2283                                 m.vertex = &svert[0].v[0];
2284                                 m.vertexstep = sizeof(surfvert_t);
2285                                 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2286                                         softwaretransform(v->v, sv->v);
2287                         }
2288                         else
2289                         {
2290                                 m.vertex = &mesh->vertex->v[0];
2291                                 m.vertexstep = sizeof(surfvertex_t);
2292                         }
2293                 }
2294                 R_Mesh_Draw(&m);
2295         }
2296 #endif
2297 }
2298
2299 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
2300 {
2301 #if 1
2302         int i;
2303         surfvertex_t *v;
2304         float *outv, *outc, *outst, cl, ca, diff[3], f;
2305         surfmesh_t *mesh;
2306         rmeshbufferinfo_t m;
2307         memset(&m, 0, sizeof(m));
2308         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2309         m.blendfunc1 = GL_SRC_ALPHA;
2310         m.blendfunc2 = GL_ONE;
2311         ca = currentrenderentity->alpha;
2312         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2313         {
2314                 m.numtriangles = mesh->numtriangles;
2315                 m.numverts = mesh->numverts;
2316
2317                 if (R_Mesh_Draw_GetBuffer(&m))
2318                 {
2319                         cl = m.colorscale;
2320                         memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2321                         if (softwaretransform_complexity)
2322                         {
2323                                 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)
2324                                 {
2325                                         softwaretransform(v->v, outv);
2326                                         outv[3] = 1;
2327                                         VectorSubtract(outv, r_origin, diff);
2328                                         f = cl * exp(fogdensity/DotProduct(diff, diff));
2329                                         VectorScale(fogcolor, f, outc);
2330                                         outc[3] = ca;
2331                                         outst[0] = v->st[0];
2332                                         outst[1] = v->st[1];
2333                                 }
2334                         }
2335                         else
2336                         {
2337                                 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)
2338                                 {
2339                                         VectorCopy(v->v, outv);
2340                                         outv[3] = 1;
2341                                         VectorSubtract(outv, r_origin, diff);
2342                                         VectorSubtract(outv, r_origin, diff);
2343                                         f = cl * exp(fogdensity/DotProduct(diff, diff));
2344                                         VectorScale(fogcolor, f, outc);
2345                                         outc[3] = ca;
2346                                         outst[0] = v->st[0];
2347                                         outst[1] = v->st[1];
2348                                 }
2349                         }
2350                 }
2351         }
2352 #else
2353         int i;
2354         surfvertex_t *v;
2355         surfvert_t *sv;
2356         rmeshinfo_t m;
2357         surfmesh_t *mesh;
2358         vec3_t diff;
2359
2360         memset(&m, 0, sizeof(m));
2361         m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2362         m.blendfunc1 = GL_SRC_ALPHA;
2363         m.blendfunc2 = GL_ONE;
2364         m.color = &svert[0].c[0];
2365         m.colorstep = sizeof(surfvert_t);
2366         m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
2367         m.texcoordstep[0] = sizeof(surfvertex_t);
2368         for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2369         {
2370                 m.numtriangles = mesh->numtriangles;
2371                 m.numverts = mesh->numverts;
2372                 m.index = mesh->index;
2373                 m.texcoords[0] = &mesh->vertex->st[0];
2374                 if (softwaretransform_complexity)
2375                 {
2376                         m.vertex = &svert[0].v[0];
2377                         m.vertexstep = sizeof(surfvert_t);
2378                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2379                         {
2380                                 softwaretransform(v->v, sv->v);
2381                                 VectorSubtract(sv->v, r_origin, diff);
2382                                 sv->c[0] = fogcolor[0];
2383                                 sv->c[1] = fogcolor[1];
2384                                 sv->c[2] = fogcolor[2];
2385                                 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2386                         }
2387                 }
2388                 else
2389                 {
2390                         m.vertex = &mesh->vertex->v[0];
2391                         m.vertexstep = sizeof(surfvertex_t);
2392                         for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2393                         {
2394                                 VectorSubtract(v->v, r_origin, diff);
2395                                 sv->c[0] = fogcolor[0];
2396                                 sv->c[1] = fogcolor[1];
2397                                 sv->c[2] = fogcolor[2];
2398                                 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2399                         }
2400                 }
2401                 R_Mesh_Draw(&m);
2402         }
2403 #endif
2404 }
2405
2406 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
2407 {
2408         msurface_t *surf;
2409         for (surf = firstsurf;surf;surf = surf->chain)
2410         {
2411                 c_brush_polys++;
2412                 RSurfShader_Wall_Pass_BaseFullbright(surf);
2413         }
2414         for (surf = firstsurf;surf;surf = surf->chain)
2415                 if (surf->currenttexture->glowtexture)
2416                         RSurfShader_Wall_Pass_Glow(surf);
2417         if (fogenabled)
2418                 for (surf = firstsurf;surf;surf = surf->chain)
2419                         RSurfShader_Wall_Pass_Fog(surf);
2420 }
2421
2422 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
2423 {
2424         msurface_t *surf;
2425         for (surf = firstsurf;surf;surf = surf->chain)
2426         {
2427                 c_brush_polys++;
2428                 RSurfShader_Wall_Pass_BaseVertex(surf);
2429         }
2430         for (surf = firstsurf;surf;surf = surf->chain)
2431                 if (surf->currenttexture->glowtexture)
2432                         RSurfShader_Wall_Pass_Glow(surf);
2433         if (fogenabled)
2434                 for (surf = firstsurf;surf;surf = surf->chain)
2435                         RSurfShader_Wall_Pass_Fog(surf);
2436 }
2437
2438 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
2439 {
2440         msurface_t *surf;
2441         if (r_vertexsurfaces.integer)
2442         {
2443                 for (surf = firstsurf;surf;surf = surf->chain)
2444                 {
2445                         c_brush_polys++;
2446                         RSurfShader_Wall_Pass_BaseVertex(surf);
2447                 }
2448                 for (surf = firstsurf;surf;surf = surf->chain)
2449                         if (surf->currenttexture->glowtexture)
2450                                 RSurfShader_Wall_Pass_Glow(surf);
2451                 if (fogenabled)
2452                         for (surf = firstsurf;surf;surf = surf->chain)
2453                                 RSurfShader_Wall_Pass_Fog(surf);
2454         }
2455         else if (r_multitexture.integer)
2456         {
2457                 if (r_dlightmap.integer)
2458                 {
2459                         for (surf = firstsurf;surf;surf = surf->chain)
2460                         {
2461                                 c_brush_polys++;
2462                                 RSurfShader_Wall_Pass_BaseMTex(surf);
2463                         }
2464                         for (surf = firstsurf;surf;surf = surf->chain)
2465                                 if (surf->currenttexture->glowtexture)
2466                                         RSurfShader_Wall_Pass_Glow(surf);
2467                         if (fogenabled)
2468                                 for (surf = firstsurf;surf;surf = surf->chain)
2469                                         RSurfShader_Wall_Pass_Fog(surf);
2470                 }
2471                 else
2472                 {
2473                         for (surf = firstsurf;surf;surf = surf->chain)
2474                         {
2475                                 c_brush_polys++;
2476                                 RSurfShader_Wall_Pass_BaseMTex(surf);
2477                         }
2478                         for (surf = firstsurf;surf;surf = surf->chain)
2479                                 if (surf->dlightframe == r_framecount)
2480                                         RSurfShader_Wall_Pass_Light(surf);
2481                         for (surf = firstsurf;surf;surf = surf->chain)
2482                                 if (surf->currenttexture->glowtexture)
2483                                         RSurfShader_Wall_Pass_Glow(surf);
2484                         if (fogenabled)
2485                                 for (surf = firstsurf;surf;surf = surf->chain)
2486                                         RSurfShader_Wall_Pass_Fog(surf);
2487                 }
2488         }
2489         else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
2490         {
2491                 for (surf = firstsurf;surf;surf = surf->chain)
2492                 {
2493                         c_brush_polys++;
2494                         RSurfShader_Wall_Pass_BaseVertex(surf);
2495                 }
2496                 for (surf = firstsurf;surf;surf = surf->chain)
2497                         if (surf->currenttexture->glowtexture)
2498                                 RSurfShader_Wall_Pass_Glow(surf);
2499                 if (fogenabled)
2500                         for (surf = firstsurf;surf;surf = surf->chain)
2501                                 RSurfShader_Wall_Pass_Fog(surf);
2502         }
2503         else
2504         {
2505                 if (r_dlightmap.integer)
2506                 {
2507                         for (surf = firstsurf;surf;surf = surf->chain)
2508                         {
2509                                 c_brush_polys++;
2510                                 RSurfShader_Wall_Pass_BaseTexture(surf);
2511                         }
2512                         for (surf = firstsurf;surf;surf = surf->chain)
2513                                 RSurfShader_Wall_Pass_BaseLightmap(surf);
2514                         for (surf = firstsurf;surf;surf = surf->chain)
2515                                 if (surf->currenttexture->glowtexture)
2516                                         RSurfShader_Wall_Pass_Glow(surf);
2517                         if (fogenabled)
2518                                 for (surf = firstsurf;surf;surf = surf->chain)
2519                                         RSurfShader_Wall_Pass_Fog(surf);
2520                 }
2521                 else
2522                 {
2523                         for (surf = firstsurf;surf;surf = surf->chain)
2524                         {
2525                                 c_brush_polys++;
2526                                 RSurfShader_Wall_Pass_BaseTexture(surf);
2527                         }
2528                         for (surf = firstsurf;surf;surf = surf->chain)
2529                                 RSurfShader_Wall_Pass_BaseLightmap(surf);
2530                         for (surf = firstsurf;surf;surf = surf->chain)
2531                                 if (surf->dlightframe == r_framecount)
2532                                         RSurfShader_Wall_Pass_Light(surf);
2533                         for (surf = firstsurf;surf;surf = surf->chain)
2534                                 if (surf->currenttexture->glowtexture)
2535                                         RSurfShader_Wall_Pass_Glow(surf);
2536                         if (fogenabled)
2537                                 for (surf = firstsurf;surf;surf = surf->chain)
2538                                         RSurfShader_Wall_Pass_Fog(surf);
2539                 }
2540         }
2541 }
2542
2543 /*
2544 =============================================================
2545
2546         WORLD MODEL
2547
2548 =============================================================
2549 */
2550
2551 static void R_SolidWorldNode (void)
2552 {
2553         if (r_viewleaf->contents != CONTENTS_SOLID)
2554         {
2555                 int portalstack;
2556                 mportal_t *p, *pstack[8192];
2557                 msurface_t *surf, **mark, **endmark;
2558                 mleaf_t *leaf;
2559                 // LordHavoc: portal-passage worldnode; follows portals leading
2560                 // outward from viewleaf, if a portal leads offscreen it is not
2561                 // followed, in indoor maps this can often cull a great deal of
2562                 // geometry away when pvs data is not present (useful with pvs as well)
2563
2564                 leaf = r_viewleaf;
2565                 leaf->worldnodeframe = r_framecount;
2566                 portalstack = 0;
2567         loc0:
2568                 c_leafs++;
2569
2570                 leaf->visframe = r_framecount;
2571
2572                 if (leaf->nummarksurfaces)
2573                 {
2574                         mark = leaf->firstmarksurface;
2575                         endmark = mark + leaf->nummarksurfaces;
2576                         do
2577                         {
2578                                 surf = *mark++;
2579                                 // make sure surfaces are only processed once
2580                                 if (surf->worldnodeframe == r_framecount)
2581                                         continue;
2582                                 surf->worldnodeframe = r_framecount;
2583                                 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2584                                 {
2585                                         if (surf->flags & SURF_PLANEBACK)
2586                                                 surf->visframe = r_framecount;
2587                                 }
2588                                 else
2589                                 {
2590                                         if (!(surf->flags & SURF_PLANEBACK))
2591                                                 surf->visframe = r_framecount;
2592                                 }
2593                         }
2594                         while (mark < endmark);
2595                 }
2596
2597                 // follow portals into other leafs
2598                 p = leaf->portals;
2599                 for (;p;p = p->next)
2600                 {
2601                         if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2602                         {
2603                                 leaf = p->past;
2604                                 if (leaf->worldnodeframe != r_framecount)
2605                                 {
2606                                         leaf->worldnodeframe = r_framecount;
2607                                         if (leaf->contents != CONTENTS_SOLID)
2608                                         {
2609                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2610                                                 {
2611                                                         p->visframe = r_framecount;
2612                                                         pstack[portalstack++] = p;
2613                                                         goto loc0;
2614
2615         loc1:
2616                                                         p = pstack[--portalstack];
2617                                                 }
2618                                         }
2619                                 }
2620                         }
2621                 }
2622
2623                 if (portalstack)
2624                         goto loc1;
2625         }
2626         else
2627         {
2628                 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
2629                 int nodestackpos = 0;
2630                 // LordHavoc: recursive descending worldnode; if portals are not
2631                 // available, this is a good last resort, can cull large amounts of
2632                 // geometry, but is more time consuming than portal-passage and renders
2633                 // things behind walls
2634
2635 loc2:
2636                 if (R_NotCulledBox(node->mins, node->maxs))
2637                 {
2638                         if (node->numsurfaces)
2639                         {
2640                                 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
2641                                 if (PlaneDiff (r_origin, node->plane) < 0)
2642                                 {
2643                                         for (;surf < surfend;surf++)
2644                                         {
2645                                                 if (surf->flags & SURF_PLANEBACK)
2646                                                         surf->visframe = r_framecount;
2647                                         }
2648                                 }
2649                                 else
2650                                 {
2651                                         for (;surf < surfend;surf++)
2652                                         {
2653                                                 if (!(surf->flags & SURF_PLANEBACK))
2654                                                         surf->visframe = r_framecount;
2655                                         }
2656                                 }
2657                         }
2658
2659                         // recurse down the children
2660                         if (node->children[0]->contents >= 0)
2661                         {
2662                                 if (node->children[1]->contents >= 0)
2663                                 {
2664                                         if (nodestackpos < 8192)
2665                                                 nodestack[nodestackpos++] = node->children[1];
2666                                         node = node->children[0];
2667                                         goto loc2;
2668                                 }
2669                                 else
2670                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
2671                                 node = node->children[0];
2672                                 goto loc2;
2673                         }
2674                         else
2675                         {
2676                                 ((mleaf_t *)node->children[0])->visframe = r_framecount;
2677                                 if (node->children[1]->contents >= 0)
2678                                 {
2679                                         node = node->children[1];
2680                                         goto loc2;
2681                                 }
2682                                 else if (nodestackpos > 0)
2683                                 {
2684                                         ((mleaf_t *)node->children[1])->visframe = r_framecount;
2685                                         node = nodestack[--nodestackpos];
2686                                         goto loc2;
2687                                 }
2688                         }
2689                 }
2690                 else if (nodestackpos > 0)
2691                 {
2692                         node = nodestack[--nodestackpos];
2693                         goto loc2;
2694                 }
2695         }
2696 }
2697
2698 static int r_portalframecount = 0;
2699
2700 static void R_PVSWorldNode()
2701 {
2702         int portalstack, i;
2703         mportal_t *p, *pstack[8192];
2704         msurface_t *surf, **mark, **endmark;
2705         mleaf_t *leaf;
2706         qbyte *worldvis;
2707
2708         worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
2709
2710         leaf = r_viewleaf;
2711         leaf->worldnodeframe = r_framecount;
2712         portalstack = 0;
2713 loc0:
2714         c_leafs++;
2715
2716         leaf->visframe = r_framecount;
2717
2718         if (leaf->nummarksurfaces)
2719         {
2720                 mark = leaf->firstmarksurface;
2721                 endmark = mark + leaf->nummarksurfaces;
2722                 do
2723                 {
2724                         surf = *mark++;
2725                         // make sure surfaces are only processed once
2726                         if (surf->worldnodeframe == r_framecount)
2727                                 continue;
2728                         surf->worldnodeframe = r_framecount;
2729                         if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2730                         {
2731                                 if (surf->flags & SURF_PLANEBACK)
2732                                         surf->visframe = r_framecount;
2733                         }
2734                         else
2735                         {
2736                                 if (!(surf->flags & SURF_PLANEBACK))
2737                                         surf->visframe = r_framecount;
2738                         }
2739                 }
2740                 while (mark < endmark);
2741         }
2742
2743         // follow portals into other leafs
2744         for (p = leaf->portals;p;p = p->next)
2745         {
2746                 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2747                 {
2748                         leaf = p->past;
2749                         if (leaf->worldnodeframe != r_framecount)
2750                         {
2751                                 leaf->worldnodeframe = r_framecount;
2752                                 if (leaf->contents != CONTENTS_SOLID)
2753                                 {
2754                                         i = (leaf - cl.worldmodel->leafs) - 1;
2755                                         if (worldvis[i>>3] & (1<<(i&7)))
2756                                         {
2757                                                 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2758                                                 {
2759                                                         pstack[portalstack++] = p;
2760                                                         goto loc0;
2761
2762 loc1:
2763                                                         p = pstack[--portalstack];
2764                                                 }
2765                                         }
2766                                 }
2767                         }
2768                 }
2769         }
2770
2771         if (portalstack)
2772                 goto loc1;
2773 }
2774
2775 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
2776 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
2777 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
2778 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
2779 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
2780
2781 int Cshader_count = 5;
2782 Cshader_t *Cshaders[5] =
2783 {
2784         &Cshader_wall_vertex,
2785         &Cshader_wall_lightmap,
2786         &Cshader_wall_fullbright,
2787         &Cshader_water,
2788         &Cshader_sky
2789 };
2790
2791 void R_PrepareSurfaces(void)
2792 {
2793         int i, alttextures, texframe, framecount;
2794         texture_t *t;
2795         model_t *model;
2796         msurface_t *surf;
2797
2798         for (i = 0;i < Cshader_count;i++)
2799                 Cshaders[i]->chain = NULL;
2800
2801         model = currentrenderentity->model;
2802         alttextures = currentrenderentity->frame != 0;
2803         texframe = (int)(cl.time * 5.0f);
2804
2805         for (i = 0;i < model->nummodelsurfaces;i++)
2806         {
2807                 surf = model->modelsortedsurfaces[i];
2808                 if (surf->visframe == r_framecount)
2809                 {
2810                         if (surf->insertframe != r_framecount)
2811                         {
2812                                 surf->insertframe = r_framecount;
2813                                 c_faces++;
2814                                 t = surf->texinfo->texture;
2815                                 if (t->animated)
2816                                 {
2817                                         framecount = t->anim_total[alttextures];
2818                                         if (framecount >= 2)
2819                                                 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
2820                                         else
2821                                                 surf->currenttexture = t->anim_frames[alttextures][0];
2822                                 }
2823                                 else
2824                                         surf->currenttexture = t;
2825                         }
2826
2827                         surf->chain = surf->shader->chain;
2828                         surf->shader->chain = surf;
2829                 }
2830         }
2831 }
2832
2833 void R_DrawSurfaces (int type)
2834 {
2835         int                     i;
2836         Cshader_t       *shader;
2837
2838         for (i = 0;i < Cshader_count;i++)
2839         {
2840                 shader = Cshaders[i];
2841                 if (shader->chain && shader->shaderfunc[type])
2842                         shader->shaderfunc[type](shader->chain);
2843         }
2844 }
2845
2846 static float portalpointbuffer[256][3];
2847
2848 void R_DrawPortals(void)
2849 {
2850         int drawportals, i;
2851         mportal_t *portal, *endportal;
2852         mvertex_t *point;
2853         rmeshinfo_t m;
2854         drawportals = r_drawportals.integer;
2855
2856         if (drawportals < 1)
2857                 return;
2858
2859         memset(&m, 0, sizeof(m));
2860         m.transparent = true;
2861         m.blendfunc1 = GL_SRC_ALPHA;
2862         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2863         m.vertex = &portalpointbuffer[0][0];
2864         m.vertexstep = sizeof(float[3]);
2865         m.ca = 0.125;
2866         for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2867         {
2868                 if (portal->visframe == r_portalframecount)
2869                 {
2870                         if (portal->numpoints <= 256)
2871                         {
2872                                 i = portal - cl.worldmodel->portals;
2873                                 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2874                                 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2875                                 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2876                                 point = portal->points;
2877                                 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2878                                 {
2879                                         for (i = portal->numpoints - 1;i >= 0;i--)
2880                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2881                                 }
2882                                 else
2883                                 {
2884                                         for (i = 0;i < portal->numpoints;i++)
2885                                                 VectorCopy(point[i].position, portalpointbuffer[i]);
2886                                 }
2887                                 R_Mesh_DrawPolygon(&m, portal->numpoints);
2888                         }
2889                 }
2890         }
2891 }
2892
2893 void R_SetupForBModelRendering(void)
2894 {
2895         int                     i;
2896         msurface_t      *surf;
2897         model_t         *model;
2898         vec3_t          modelorg;
2899
2900         // because bmodels can be reused, we have to decide which things to render
2901         // from scratch every time
2902
2903         model = currentrenderentity->model;
2904
2905         softwaretransformforentity (currentrenderentity);
2906         softwareuntransform(r_origin, modelorg);
2907
2908         for (i = 0;i < model->nummodelsurfaces;i++)
2909         {
2910                 surf = model->modelsortedsurfaces[i];
2911                 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2912                         surf->visframe = r_framecount;
2913                 else
2914                         surf->visframe = -1;
2915                 surf->worldnodeframe = -1;
2916                 surf->lightframe = -1;
2917                 surf->dlightframe = -1;
2918                 surf->insertframe = -1;
2919         }
2920 }
2921
2922 void R_SetupForWorldRendering(void)
2923 {
2924         // there is only one instance of the world, but it can be rendered in
2925         // multiple stages
2926
2927         currentrenderentity = &cl_entities[0].render;
2928         softwaretransformidentity();
2929 }
2930
2931 static void R_SurfMarkLights (void)
2932 {
2933         int                     i;
2934         msurface_t      *surf;
2935
2936         if (r_dynamic.integer)
2937                 R_MarkLights();
2938
2939         if (!r_vertexsurfaces.integer)
2940         {
2941                 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2942                 {
2943                         surf = currentrenderentity->model->modelsortedsurfaces[i];
2944                         if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2945                         {
2946                                 if (surf->cached_dlight
2947                                  || surf->cached_ambient != r_ambient.value
2948                                  || surf->cached_lightscalebit != lightscalebit)
2949                                         R_BuildLightMap(surf, false); // base lighting changed
2950                                 else if (r_dynamic.integer)
2951                                 {
2952                                         if  (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2953                                          || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2954                                          || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2955                                          || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2956                                                 R_BuildLightMap(surf, false); // base lighting changed
2957                                         else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2958                                                 R_BuildLightMap(surf, true); // only dlights
2959                                 }
2960                         }
2961                 }
2962         }
2963 }
2964
2965 void R_MarkWorldLights(void)
2966 {
2967         R_SetupForWorldRendering();
2968         R_SurfMarkLights();
2969 }
2970
2971 /*
2972 =============
2973 R_DrawWorld
2974 =============
2975 */
2976 void R_DrawWorld (void)
2977 {
2978         R_SetupForWorldRendering();
2979
2980         if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2981                 R_SolidWorldNode ();
2982         else
2983                 R_PVSWorldNode ();
2984 }
2985
2986 /*
2987 =================
2988 R_DrawBrushModel
2989 =================
2990 */
2991 void R_DrawBrushModelSky (void)
2992 {
2993         R_SetupForBModelRendering();
2994
2995         R_PrepareSurfaces();
2996         R_DrawSurfaces(SHADERSTAGE_SKY);
2997 }
2998
2999 void R_DrawBrushModelNormal (void)
3000 {
3001         c_bmodels++;
3002
3003         // have to flush queue because of possible lightmap reuse
3004         R_Mesh_Render();
3005
3006         R_SetupForBModelRendering();
3007
3008         R_SurfMarkLights();
3009
3010         R_PrepareSurfaces();
3011
3012         if (!skyrendermasked)
3013                 R_DrawSurfaces(SHADERSTAGE_SKY);
3014         R_DrawSurfaces(SHADERSTAGE_NORMAL);
3015 }
3016
3017 static void gl_surf_start(void)
3018 {
3019 }
3020
3021 static void gl_surf_shutdown(void)
3022 {
3023 }
3024
3025 static void gl_surf_newmap(void)
3026 {
3027 }
3028
3029 void GL_Surf_Init(void)
3030 {
3031         int i;
3032         dlightdivtable[0] = 4194304;
3033         for (i = 1;i < 32768;i++)
3034                 dlightdivtable[i] = 4194304 / (i << 7);
3035
3036         Cvar_RegisterVariable(&r_ambient);
3037         Cvar_RegisterVariable(&r_vertexsurfaces);
3038         Cvar_RegisterVariable(&r_dlightmap);
3039         Cvar_RegisterVariable(&r_drawportals);
3040         Cvar_RegisterVariable(&r_testvis);
3041         Cvar_RegisterVariable(&r_floatbuildlightmap);
3042
3043         R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
3044 }
3045