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