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