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