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