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