2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // r_surf.c: surface-related refresh code
24 #define MAX_LIGHTMAP_SIZE 256
26 static signed int blocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
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"};
36 static void gl_surf_start(void)
40 static void gl_surf_shutdown(void)
44 static void gl_surf_newmap(void)
48 static int dlightdivtable[32768];
50 void GL_Surf_Init(void)
53 dlightdivtable[0] = 4194304;
54 for (i = 1;i < 32768;i++)
55 dlightdivtable[i] = 4194304 / (i << 7);
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);
63 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
66 static int R_AddDynamicLights (msurface_t *surf)
68 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
73 // LordHavoc: use 64bit integer... shame it's not very standardized...
74 #if _MSC_VER || __BORLANDC__
82 smax = (surf->extents[0] >> 4) + 1;
83 tmax = (surf->extents[1] >> 4) + 1;
85 for (lnum = 0; lnum < r_numdlights; lnum++)
87 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
88 continue; // not lit by this light
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;
94 // for comparisons to minimum acceptable light
95 // compensate for LIGHTOFFSET
96 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
98 // already clamped, skip this
99 // clamp radius to avoid exceeding 32768 entry division table
100 //if (maxdist > 4194304)
101 // maxdist = 4194304;
104 dist2 += LIGHTOFFSET;
105 if (dist2 >= maxdist)
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;
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];
115 s = bound(0, impacts, smax * 16) - impacts;
116 t = bound(0, impactt, tmax * 16) - impactt;
117 i = s * s + t * t + dist2;
121 // reduce calculations
122 for (s = 0, i = impacts; s < smax; s++, i -= 16)
123 sdtable[s] = i * i + dist2;
125 maxdist3 = maxdist - dist2;
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);
136 for (t = 0;t < tmax;t++, i -= 16)
139 // make sure some part of it is visible on this line
142 maxdist2 = maxdist - td;
143 for (s = 0;s < smax;s++)
145 if (sdtable[s] < maxdist2)
147 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
150 bl[0] += (red * k) >> 8;
151 bl[1] += (green * k) >> 8;
152 bl[2] += (blue * k) >> 8;
166 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
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;
173 // LordHavoc: use 64bit integer... shame it's not very standardized...
174 #if _MSC_VER || __BORLANDC__
181 // for comparisons to minimum acceptable light
182 // compensate for 4096 offset
183 maxdist = radius * radius + 4096;
185 // clamp radius to avoid exceeding 32768 entry division table
186 if (maxdist > 4194304)
189 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
192 if (node->contents < 0)
194 ndist = PlaneDiff(origin, node->plane);
197 node = node->children[0];
202 node = node->children[1];
206 dist2 = ndist * ndist;
210 maxdist3 = maxdist - dist2;
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;
216 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
218 if (surf->stainsamples)
220 smax = (surf->extents[0] >> 4) + 1;
221 tmax = (surf->extents[1] >> 4) + 1;
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];
226 s = bound(0, impacts, smax * 16) - impacts;
227 t = bound(0, impactt, tmax * 16) - impactt;
228 i = s * s + t * t + dist2;
232 // reduce calculations
233 for (s = 0, i = impacts; s < smax; s++, i -= 16)
234 sdtable[s] = i * i + dist2;
236 // convert to 8.8 blocklights format
237 bl = surf->stainsamples;
242 for (t = 0;t < tmax;t++, i -= 16)
245 // make sure some part of it is visible on this line
248 maxdist2 = maxdist - td;
249 for (s = 0;s < smax;s++)
251 if (sdtable[s] < maxdist2)
253 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
256 ratio = rand() & 255;
257 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
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]);
278 // force lightmap upload
280 surf->cached_dlight = true;
285 if (node->children[0]->contents >= 0)
287 if (node->children[1]->contents >= 0)
289 R_StainNode(node->children[0], model, origin, radius, icolor);
290 node = node->children[1];
295 node = node->children[0];
299 else if (node->children[1]->contents >= 0)
301 node = node->children[1];
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)
309 entity_render_t *ent;
321 model = cl.worldmodel;
322 softwaretransformidentity();
323 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
325 // look for embedded bmodels
326 for (n = 1;n < MAX_EDICTS;n++)
328 ent = &cl_entities[n].render;
330 if (model && model->name[0] == '*')
332 Mod_CheckLoaded(model);
333 if (model->type == mod_brush)
335 softwaretransformforentity(ent);
336 softwareuntransform(origin, org);
337 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
347 Combine and scale multiple lightmaps into the 8.8 format in blocklights
350 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
352 int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
353 qbyte *lightmap, *out, *stain;
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]];
364 smax = (surf->extents[0]>>4)+1;
365 tmax = (surf->extents[1]>>4)+1;
368 lightmap = surf->samples;
370 // set to full bright if no light data
371 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
374 for (i = 0;i < size;i++)
384 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
388 for (i = 0;i < size3;i++)
392 memset(&blocklights[0], 0, size*3*sizeof(int));
394 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
396 surf->cached_dlight = R_AddDynamicLights(surf);
397 if (surf->cached_dlight)
399 else if (dlightchanged)
400 return; // don't upload if only updating dlights and none mattered
403 // add all the lightmaps
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;
410 stain = surf->stainsamples;
412 for (bl = blocklights, i = 0;i < size3;i++)
414 bl[i] = (bl[i] * stain[i]) >> 8;
418 // deal with lightmap brightness scale
419 shift = 7 + lightscalebit;
420 if (currentrenderentity->model->lightmaprgba)
422 stride = (surf->lightmaptexturestride - smax) * 4;
423 for (i = 0;i < tmax;i++, out += stride)
425 for (j = 0;j < smax;j++)
427 l = *bl++ >> shift;*out++ = min(l, 255);
428 l = *bl++ >> shift;*out++ = min(l, 255);
429 l = *bl++ >> shift;*out++ = min(l, 255);
436 stride = (surf->lightmaptexturestride - smax) * 3;
437 for (i = 0;i < tmax;i++, out += stride)
439 for (j = 0;j < smax;j++)
441 l = *bl++ >> shift;*out++ = min(l, 255);
442 l = *bl++ >> shift;*out++ = min(l, 255);
443 l = *bl++ >> shift;*out++ = min(l, 255);
448 R_UpdateTexture(surf->lightmaptexture, templight);
455 Returns the proper texture for a given time and base texture
459 // note: this was manually inlined in R_PrepareSurfaces
460 static texture_t *R_TextureAnimation (texture_t *base)
462 if (currentrenderentity->frame && base->alternate_anims != NULL)
463 base = base->alternate_anims;
465 if (base->anim_total < 2)
468 return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
474 =============================================================
478 =============================================================
482 static float turbsin[256] =
484 #include "gl_warp_sin.h"
486 #define TURBSCALE (256.0 / (2 * M_PI))
488 #define MAX_SURFVERTS 1024
497 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
499 static int RSurfShader_Sky(int stage, msurface_t *s)
502 float number, length, dir[3], speedscale;
507 // LordHavoc: HalfLife maps have freaky skypolys...
508 if (currentrenderentity->model->ishlbsp)
517 skyrendernow = false;
520 // draw depth-only polys
521 memset(&m, 0, sizeof(m));
522 m.transparent = false;
523 m.blendfunc1 = GL_ZERO;
524 m.blendfunc2 = GL_ONE;
526 m.numtriangles = s->mesh.numtriangles;
527 m.numverts = s->mesh.numverts;
528 m.index = s->mesh.index;
533 if (softwaretransform_complexity)
535 m.vertex = &svert[0].v[0];
536 m.vertexstep = sizeof(surfvert_t);
537 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
538 softwaretransform(v->v, sv->v);
542 m.vertex = &s->mesh.vertex[0].v[0];
543 m.vertexstep = sizeof(surfvertex_t);
547 else if (skyrenderglquake)
549 memset(&m, 0, sizeof(m));
550 m.transparent = false;
551 m.blendfunc1 = GL_ONE;
552 m.blendfunc2 = GL_ZERO;
553 m.numtriangles = s->mesh.numtriangles;
554 m.numverts = s->mesh.numverts;
555 m.index = s->mesh.index;
556 m.vertex = &svert[0].v[0];
557 m.vertexstep = sizeof(surfvert_t);
562 m.tex[0] = R_GetTexture(solidskytexture);
563 m.texcoords[0] = &svert[0].st[0];
564 m.texcoordstep[0] = sizeof(surfvert_t);
565 speedscale = cl.time * (8.0/128.0);
566 speedscale -= (int)speedscale;
567 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
569 softwaretransform(v->v, sv->v);
570 VectorSubtract (sv->v, r_origin, dir);
571 // flatten the sphere
574 number = DotProduct(dir, dir);
576 length = 3.0f / sqrt(number);
578 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
579 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
582 sv->st[0] = speedscale + dir[0] * length;
583 sv->st[1] = speedscale + dir[1] * length;
590 memset(&m, 0, sizeof(m));
591 m.transparent = false;
592 m.blendfunc1 = GL_ONE;
593 m.blendfunc2 = GL_ZERO;
594 m.numtriangles = s->mesh.numtriangles;
595 m.numverts = s->mesh.numverts;
596 m.index = s->mesh.index;
601 if (softwaretransform_complexity)
603 m.vertex = &svert[0].v[0];
604 m.vertexstep = sizeof(surfvert_t);
605 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
606 softwaretransform(v->v, sv->v);
610 m.vertex = &s->mesh.vertex[0].v[0];
611 m.vertexstep = sizeof(surfvertex_t);
619 if (skyrenderglquake)
621 memset(&m, 0, sizeof(m));
622 m.transparent = false;
623 m.blendfunc1 = GL_SRC_ALPHA;
624 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
625 m.numtriangles = s->mesh.numtriangles;
626 m.numverts = s->mesh.numverts;
627 m.index = s->mesh.index;
628 m.vertex = &svert[0].v[0];
629 m.vertexstep = sizeof(surfvert_t);
634 m.tex[0] = R_GetTexture(alphaskytexture);
635 m.texcoords[0] = &svert[0].st[0];
636 m.texcoordstep[0] = sizeof(surfvert_t);
637 speedscale = cl.time * (16.0/128.0);
638 speedscale -= (int)speedscale;
639 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
641 softwaretransform(v->v, sv->v);
642 VectorSubtract (sv->v, r_origin, dir);
643 // flatten the sphere
646 number = DotProduct(dir, dir);
648 length = 3.0f / sqrt(number);
650 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
651 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
654 sv->st[0] = speedscale + dir[0] * length;
655 sv->st[1] = speedscale + dir[1] * length;
666 static int RSurf_Light(int *dlightbits, int numverts)
669 int i, l, lit = false;
673 for (l = 0;l < r_numdlights;l++)
675 if (dlightbits[l >> 5] & (1 << (l & 31)))
678 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
679 VectorCopy(rd->origin, lightorigin);
680 for (i = 0, sv = svert;i < numverts;i++, sv++)
682 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
683 if (f < rd->cullradius2)
685 f = (1.0f / f) - rd->lightsubtract;
686 sv->c[0] += rd->light[0] * f;
687 sv->c[1] += rd->light[1] * f;
688 sv->c[2] += rd->light[2] * f;
697 static void RSurfShader_Water_Pass_Base(msurface_t *s)
700 float diff[3], alpha, ifog;
704 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
706 memset(&m, 0, sizeof(m));
707 if (alpha != 1 || s->currenttexture->fogtexture != NULL)
709 m.transparent = true;
710 m.blendfunc1 = GL_SRC_ALPHA;
711 m.blendfunc2 = GL_ONE; //_MINUS_SRC_ALPHA;
715 m.transparent = false;
716 m.blendfunc1 = GL_ONE;
717 m.blendfunc2 = GL_ZERO;
719 m.numtriangles = s->mesh.numtriangles;
720 m.numverts = s->mesh.numverts;
721 m.index = s->mesh.index;
722 m.vertex = &svert[0].v[0];
723 m.vertexstep = sizeof(surfvert_t);
724 m.color = &svert[0].c[0];
725 m.colorstep = sizeof(surfvert_t);
726 m.tex[0] = R_GetTexture(s->currenttexture->texture);
727 m.texcoords[0] = &svert[0].st[0];
728 m.texcoordstep[0] = sizeof(surfvert_t);
729 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
731 softwaretransform(v->v, sv->v);
732 if (r_waterripple.value)
733 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];
734 if (s->flags & SURF_DRAWFULLBRIGHT)
748 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
749 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
751 if (s->dlightframe == r_framecount && !(s->flags & SURF_DRAWFULLBRIGHT))
752 RSurf_Light(s->dlightbits, m.numverts);
753 if (fogenabled && (s->flags & SURF_DRAWNOALPHA))
755 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
757 VectorSubtract(sv->v, r_origin, diff);
758 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
767 static void RSurfShader_Water_Pass_Glow(msurface_t *s)
770 float diff[3], alpha, ifog;
774 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
776 memset(&m, 0, sizeof(m));
777 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
778 m.blendfunc1 = GL_SRC_ALPHA;
779 m.blendfunc2 = GL_ONE;
780 m.numtriangles = s->mesh.numtriangles;
781 m.numverts = s->mesh.numverts;
782 m.index = s->mesh.index;
783 m.vertex = &svert[0].v[0];
784 m.vertexstep = sizeof(surfvert_t);
789 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
790 m.texcoords[0] = &svert[0].st[0];
791 m.texcoordstep[0] = sizeof(surfvert_t);
794 m.color = &svert[0].c[0];
795 m.colorstep = sizeof(surfvert_t);
796 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
798 softwaretransform(v->v, sv->v);
799 if (r_waterripple.value)
800 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];
801 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
802 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
803 VectorSubtract(sv->v, r_origin, diff);
804 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
805 sv->c[0] = m.cr * ifog;
806 sv->c[1] = m.cg * ifog;
807 sv->c[2] = m.cb * ifog;
813 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
815 softwaretransform(v->v, sv->v);
816 if (r_waterripple.value)
817 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];
818 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
819 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
825 static void RSurfShader_Water_Pass_Fog(msurface_t *s)
833 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
835 memset(&m, 0, sizeof(m));
836 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
837 m.blendfunc1 = GL_SRC_ALPHA;
838 m.blendfunc2 = GL_ONE;
839 m.numtriangles = s->mesh.numtriangles;
840 m.numverts = s->mesh.numverts;
841 m.index = s->mesh.index;
842 m.vertex = &svert[0].v[0];
843 m.vertexstep = sizeof(surfvert_t);
844 m.color = &svert[0].c[0];
845 m.colorstep = sizeof(surfvert_t);
846 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
847 m.texcoords[0] = &svert[0].st[0];
848 m.texcoordstep[0] = sizeof(surfvert_t);
850 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
852 softwaretransform(v->v, sv->v);
853 if (r_waterripple.value)
854 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 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
858 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
860 VectorSubtract(sv->v, r_origin, diff);
861 sv->c[0] = fogcolor[0];
862 sv->c[1] = fogcolor[1];
863 sv->c[2] = fogcolor[2];
864 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
869 static int RSurfShader_Water(int stage, msurface_t *s)
874 RSurfShader_Water_Pass_Base(s);
879 if (currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
880 RSurfShader_Water_Pass_Fog(s);
890 static int RSurfShader_Water_Glow(int stage, msurface_t *s)
895 RSurfShader_Water_Pass_Base(s);
898 RSurfShader_Water_Pass_Glow(s);
903 if (currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
904 RSurfShader_Water_Pass_Fog(s);
914 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *s)
922 memset(&m, 0, sizeof(m));
923 if (currentrenderentity->effects & EF_ADDITIVE)
925 m.transparent = true;
926 m.blendfunc1 = GL_SRC_ALPHA;
927 m.blendfunc2 = GL_ONE;
929 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
931 m.transparent = true;
932 m.blendfunc1 = GL_SRC_ALPHA;
933 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
937 m.transparent = false;
938 m.blendfunc1 = GL_ONE;
939 m.blendfunc2 = GL_ZERO;
941 m.numtriangles = s->mesh.numtriangles;
942 m.numverts = s->mesh.numverts;
943 m.index = s->mesh.index;
944 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
945 m.ca = currentrenderentity->alpha;
946 m.tex[0] = R_GetTexture(s->currenttexture->texture);
947 m.tex[1] = R_GetTexture(s->lightmaptexture);
948 m.texcoords[0] = &s->mesh.vertex->st[0];
949 m.texcoords[1] = &s->mesh.vertex->uv[0];
950 m.texcoordstep[0] = sizeof(surfvertex_t);
951 m.texcoordstep[1] = sizeof(surfvertex_t);
954 m.color = &svert[0].c[0];
955 m.colorstep = sizeof(surfvert_t);
956 if (softwaretransform_complexity)
958 m.vertex = &svert[0].v[0];
959 m.vertexstep = sizeof(surfvert_t);
960 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
962 softwaretransform(v->v, sv->v);
963 VectorSubtract(sv->v, r_origin, diff);
964 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
965 sv->c[0] = m.cr * ifog;
966 sv->c[1] = m.cg * ifog;
967 sv->c[2] = m.cb * ifog;
973 m.vertex = &s->mesh.vertex->v[0];
974 m.vertexstep = sizeof(surfvertex_t);
975 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
977 VectorSubtract(v->v, r_origin, diff);
978 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
979 sv->c[0] = m.cr * ifog;
980 sv->c[1] = m.cg * ifog;
981 sv->c[2] = m.cb * ifog;
988 if (softwaretransform_complexity)
990 m.vertex = &svert[0].v[0];
991 m.vertexstep = sizeof(surfvert_t);
992 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
993 softwaretransform(v->v, sv->v);
997 m.vertex = &s->mesh.vertex->v[0];
998 m.vertexstep = sizeof(surfvertex_t);
1004 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
1011 memset(&m, 0, sizeof(m));
1012 m.transparent = false;
1013 m.blendfunc1 = GL_ONE;
1014 m.blendfunc2 = GL_ZERO;
1015 m.numtriangles = s->mesh.numtriangles;
1016 m.numverts = s->mesh.numverts;
1017 m.index = s->mesh.index;
1018 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1020 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1021 m.texcoords[0] = &s->mesh.vertex->st[0];
1022 m.texcoordstep[0] = sizeof(surfvertex_t);
1023 if (softwaretransform_complexity)
1025 m.vertex = &svert[0].v[0];
1026 m.vertexstep = sizeof(surfvert_t);
1027 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1028 softwaretransform(v->v, sv->v);
1032 m.vertex = &s->mesh.vertex->v[0];
1033 m.vertexstep = sizeof(surfvertex_t);
1038 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
1041 float diff[3], ifog;
1046 memset(&m, 0, sizeof(m));
1047 m.transparent = false;
1048 m.blendfunc1 = GL_ZERO;
1049 m.blendfunc2 = GL_SRC_COLOR;
1050 m.numtriangles = s->mesh.numtriangles;
1051 m.numverts = s->mesh.numverts;
1052 m.index = s->mesh.index;
1053 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1055 m.tex[0] = R_GetTexture(s->lightmaptexture);
1056 m.texcoords[0] = &s->mesh.vertex->uv[0];
1057 m.texcoordstep[0] = sizeof(surfvertex_t);
1060 m.color = &svert[0].c[0];
1061 m.colorstep = sizeof(surfvert_t);
1062 if (softwaretransform_complexity)
1064 m.vertex = &svert[0].v[0];
1065 m.vertexstep = sizeof(surfvert_t);
1066 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1068 softwaretransform(v->v, sv->v);
1069 VectorSubtract(sv->v, r_origin, diff);
1070 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1071 sv->c[0] = m.cr * ifog;
1072 sv->c[1] = m.cg * ifog;
1073 sv->c[2] = m.cb * ifog;
1079 m.vertex = &s->mesh.vertex->v[0];
1080 m.vertexstep = sizeof(surfvertex_t);
1081 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1083 VectorSubtract(v->v, r_origin, diff);
1084 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1085 sv->c[0] = m.cr * ifog;
1086 sv->c[1] = m.cg * ifog;
1087 sv->c[2] = m.cb * ifog;
1094 if (softwaretransform_complexity)
1096 m.vertex = &svert[0].v[0];
1097 m.vertexstep = sizeof(surfvert_t);
1098 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1099 softwaretransform(v->v, sv->v);
1103 m.vertex = &s->mesh.vertex->v[0];
1104 m.vertexstep = sizeof(surfvertex_t);
1110 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
1113 float c[3], base[3], scale, diff[3], ifog;
1119 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
1121 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1123 memset(&m, 0, sizeof(m));
1124 if (currentrenderentity->effects & EF_ADDITIVE)
1126 m.transparent = true;
1127 m.blendfunc1 = GL_SRC_ALPHA;
1128 m.blendfunc2 = GL_ONE;
1130 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1132 m.transparent = true;
1133 m.blendfunc1 = GL_SRC_ALPHA;
1134 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1138 m.transparent = false;
1139 m.blendfunc1 = GL_ONE;
1140 m.blendfunc2 = GL_ZERO;
1142 m.numtriangles = s->mesh.numtriangles;
1143 m.numverts = s->mesh.numverts;
1144 m.index = s->mesh.index;
1145 m.vertex = &svert[0].v[0];
1146 m.vertexstep = sizeof(surfvert_t);
1147 m.color = &svert[0].c[0];
1148 m.colorstep = sizeof(surfvert_t);
1149 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1150 m.texcoords[0] = &s->mesh.vertex->st[0];
1151 m.texcoordstep[0] = sizeof(surfvertex_t);
1152 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1154 softwaretransform(v->v, sv->v);
1155 VectorCopy(base, c);
1156 if (s->styles[0] != 255)
1158 lm = s->samples + v->lightmapoffset;
1159 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
1160 VectorMA(c, scale, lm, c);
1161 if (s->styles[1] != 255)
1164 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
1165 VectorMA(c, scale, lm, c);
1166 if (s->styles[2] != 255)
1169 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
1170 VectorMA(c, scale, lm, c);
1171 if (s->styles[3] != 255)
1174 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
1175 VectorMA(c, scale, lm, c);
1183 sv->c[3] = currentrenderentity->alpha;
1185 if (s->dlightframe == r_framecount)
1186 RSurf_Light(s->dlightbits, m.numverts);
1189 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1191 VectorSubtract(sv->v, r_origin, diff);
1192 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1201 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *s)
1204 float diff[3], ifog;
1209 memset(&m, 0, sizeof(m));
1210 if (currentrenderentity->effects & EF_ADDITIVE)
1212 m.transparent = true;
1213 m.blendfunc1 = GL_SRC_ALPHA;
1214 m.blendfunc2 = GL_ONE;
1216 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1218 m.transparent = true;
1219 m.blendfunc1 = GL_SRC_ALPHA;
1220 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1224 m.transparent = false;
1225 m.blendfunc1 = GL_ONE;
1226 m.blendfunc2 = GL_ZERO;
1228 m.numtriangles = s->mesh.numtriangles;
1229 m.numverts = s->mesh.numverts;
1230 m.index = s->mesh.index;
1231 m.vertex = &svert[0].v[0];
1232 m.vertexstep = sizeof(surfvert_t);
1233 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1234 m.texcoords[0] = &s->mesh.vertex->st[0];
1235 m.texcoordstep[0] = sizeof(surfvertex_t);
1238 m.color = &svert[0].c[0];
1239 m.colorstep = sizeof(surfvert_t);
1240 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1242 softwaretransform(v->v, sv->v);
1243 VectorSubtract(sv->v, r_origin, diff);
1244 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1248 sv->c[3] = currentrenderentity->alpha;
1253 m.cr = m.cg = m.cb = 1;
1254 m.ca = currentrenderentity->alpha;
1255 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1256 softwaretransform(v->v, sv->v);
1261 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1264 float diff[3], ifog;
1269 memset(&m, 0, sizeof(m));
1270 if (currentrenderentity->effects & EF_ADDITIVE)
1271 m.transparent = true;
1272 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1273 m.transparent = true;
1275 m.transparent = false;
1276 m.blendfunc1 = GL_SRC_ALPHA;
1277 m.blendfunc2 = GL_ONE;
1278 m.numtriangles = s->mesh.numtriangles;
1279 m.numverts = s->mesh.numverts;
1280 m.index = s->mesh.index;
1281 m.vertex = &svert[0].v[0];
1282 m.vertexstep = sizeof(surfvert_t);
1283 m.color = &svert[0].c[0];
1284 m.colorstep = sizeof(surfvert_t);
1285 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1286 m.texcoords[0] = &s->mesh.vertex->st[0];
1287 m.texcoordstep[0] = sizeof(surfvertex_t);
1288 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1290 softwaretransform(v->v, sv->v);
1294 sv->c[3] = currentrenderentity->alpha;
1296 if (RSurf_Light(s->dlightbits, m.numverts))
1300 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1302 VectorSubtract(sv->v, r_origin, diff);
1303 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1313 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1316 float diff[3], ifog;
1321 memset(&m, 0, sizeof(m));
1322 if (currentrenderentity->effects & EF_ADDITIVE)
1323 m.transparent = true;
1324 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1325 m.transparent = true;
1327 m.transparent = false;
1328 m.blendfunc1 = GL_SRC_ALPHA;
1329 m.blendfunc2 = GL_ONE;
1330 m.numtriangles = s->mesh.numtriangles;
1331 m.numverts = s->mesh.numverts;
1332 m.index = s->mesh.index;
1336 m.ca = currentrenderentity->alpha;
1337 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1338 m.texcoords[0] = &s->mesh.vertex->st[0];
1339 m.texcoordstep[0] = sizeof(surfvertex_t);
1342 m.color = &svert[0].c[0];
1343 m.colorstep = sizeof(surfvert_t);
1344 if (softwaretransform_complexity)
1346 m.vertex = &svert[0].v[0];
1347 m.vertexstep = sizeof(surfvert_t);
1348 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1350 softwaretransform(v->v, sv->v);
1351 VectorSubtract(sv->v, r_origin, diff);
1352 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1353 sv->c[0] = m.cr * ifog;
1354 sv->c[1] = m.cg * ifog;
1355 sv->c[2] = m.cb * ifog;
1361 m.vertex = &s->mesh.vertex->v[0];
1362 m.vertexstep = sizeof(surfvertex_t);
1363 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1365 VectorSubtract(v->v, r_origin, diff);
1366 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1367 sv->c[0] = m.cr * ifog;
1368 sv->c[1] = m.cg * ifog;
1369 sv->c[2] = m.cb * ifog;
1376 if (softwaretransform_complexity)
1378 m.vertex = &svert[0].v[0];
1379 m.vertexstep = sizeof(surfvert_t);
1380 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1381 softwaretransform(v->v, sv->v);
1385 m.vertex = &s->mesh.vertex->v[0];
1386 m.vertexstep = sizeof(surfvertex_t);
1392 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1400 memset(&m, 0, sizeof(m));
1401 if (currentrenderentity->effects & EF_ADDITIVE)
1402 m.transparent = true;
1403 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1404 m.transparent = true;
1406 m.transparent = false;
1407 m.blendfunc1 = GL_SRC_ALPHA;
1408 m.blendfunc2 = GL_ONE;
1409 m.numtriangles = s->mesh.numtriangles;
1410 m.numverts = s->mesh.numverts;
1411 m.index = s->mesh.index;
1412 m.color = &svert[0].c[0];
1413 m.colorstep = sizeof(surfvert_t);
1414 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1415 m.texcoords[0] = &s->mesh.vertex->st[0];
1416 m.texcoordstep[0] = sizeof(surfvertex_t);
1417 if (softwaretransform_complexity)
1419 m.vertex = &svert[0].v[0];
1420 m.vertexstep = sizeof(surfvert_t);
1421 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1423 softwaretransform(v->v, sv->v);
1424 VectorSubtract(sv->v, r_origin, diff);
1425 sv->c[0] = fogcolor[0];
1426 sv->c[1] = fogcolor[1];
1427 sv->c[2] = fogcolor[2];
1428 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1433 m.vertex = &s->mesh.vertex->v[0];
1434 m.vertexstep = sizeof(surfvertex_t);
1435 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1437 VectorSubtract(v->v, r_origin, diff);
1438 sv->c[0] = fogcolor[0];
1439 sv->c[1] = fogcolor[1];
1440 sv->c[2] = fogcolor[2];
1441 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1447 static int RSurfShader_Wall_Fullbright(int stage, msurface_t *s)
1452 RSurfShader_Wall_Pass_BaseFullbright(s);
1458 static int RSurfShader_Wall_Fullbright_Glow(int stage, msurface_t *s)
1464 RSurfShader_Wall_Pass_BaseFullbright(s);
1467 RSurfShader_Wall_Pass_Glow(s);
1474 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1479 RSurfShader_Wall_Pass_BaseVertex(s);
1485 static int RSurfShader_Wall_Vertex_Glow(int stage, msurface_t *s)
1491 RSurfShader_Wall_Pass_BaseVertex(s);
1494 RSurfShader_Wall_Pass_Glow(s);
1501 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1503 if (r_vertexsurfaces.integer)
1508 RSurfShader_Wall_Pass_BaseVertex(s);
1513 else if (r_multitexture.integer)
1515 if (r_dlightmap.integer)
1520 RSurfShader_Wall_Pass_BaseMTex(s);
1531 RSurfShader_Wall_Pass_BaseMTex(s);
1534 if (s->dlightframe == r_framecount)
1535 RSurfShader_Wall_Pass_Light(s);
1542 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1547 RSurfShader_Wall_Pass_BaseVertex(s);
1554 if (r_dlightmap.integer)
1560 RSurfShader_Wall_Pass_BaseTexture(s);
1563 RSurfShader_Wall_Pass_BaseLightmap(s);
1575 RSurfShader_Wall_Pass_BaseTexture(s);
1578 RSurfShader_Wall_Pass_BaseLightmap(s);
1581 if (s->dlightframe == r_framecount)
1582 RSurfShader_Wall_Pass_Light(s);
1591 static int RSurfShader_Wall_Lightmap_Glow(int stage, msurface_t *s)
1595 if (r_vertexsurfaces.integer)
1600 RSurfShader_Wall_Pass_BaseVertex(s);
1603 RSurfShader_Wall_Pass_Glow(s);
1609 else if (r_multitexture.integer)
1611 if (r_dlightmap.integer)
1616 RSurfShader_Wall_Pass_BaseMTex(s);
1619 RSurfShader_Wall_Pass_Glow(s);
1630 RSurfShader_Wall_Pass_BaseMTex(s);
1633 if (s->dlightframe == r_framecount)
1634 RSurfShader_Wall_Pass_Light(s);
1637 RSurfShader_Wall_Pass_Glow(s);
1644 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1649 RSurfShader_Wall_Pass_BaseVertex(s);
1652 RSurfShader_Wall_Pass_Glow(s);
1660 if (r_dlightmap.integer)
1665 RSurfShader_Wall_Pass_BaseTexture(s);
1668 RSurfShader_Wall_Pass_BaseLightmap(s);
1671 RSurfShader_Wall_Pass_Glow(s);
1682 RSurfShader_Wall_Pass_BaseTexture(s);
1685 RSurfShader_Wall_Pass_BaseLightmap(s);
1688 if (s->dlightframe == r_framecount)
1689 RSurfShader_Wall_Pass_Light(s);
1692 RSurfShader_Wall_Pass_Glow(s);
1701 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1703 if (stage == 0 && fogenabled)
1705 RSurfShader_Wall_Pass_Fog(s);
1713 =============================================================
1717 =============================================================
1720 static void RSurf_Callback(void *data, void *junk)
1722 ((msurface_t *)data)->visframe = r_framecount;
1725 static void R_SolidWorldNode (void)
1727 if (r_viewleaf->contents != CONTENTS_SOLID)
1730 mportal_t *p, *pstack[8192];
1731 msurface_t *surf, **mark, **endmark;
1734 // LordHavoc: portal-passage worldnode; follows portals leading
1735 // outward from viewleaf, if a portal leads offscreen it is not
1736 // followed, in indoor maps this can often cull a great deal of
1737 // geometry away when pvs data is not present (useful with pvs as well)
1740 leaf->worldnodeframe = r_framecount;
1745 leaf->visframe = r_framecount;
1747 if (leaf->nummarksurfaces)
1749 mark = leaf->firstmarksurface;
1750 endmark = mark + leaf->nummarksurfaces;
1756 // make sure surfaces are only processed once
1757 if (surf->worldnodeframe == r_framecount)
1759 surf->worldnodeframe = r_framecount;
1760 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1762 if (surf->flags & SURF_PLANEBACK)
1764 VectorNegate(surf->plane->normal, plane.normal);
1765 plane.dist = -surf->plane->dist;
1766 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1771 if (!(surf->flags & SURF_PLANEBACK))
1772 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);
1775 while (mark < endmark);
1782 // make sure surfaces are only processed once
1783 if (surf->worldnodeframe == r_framecount)
1785 surf->worldnodeframe = r_framecount;
1786 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1788 if (surf->flags & SURF_PLANEBACK)
1789 surf->visframe = r_framecount;
1793 if (!(surf->flags & SURF_PLANEBACK))
1794 surf->visframe = r_framecount;
1797 while (mark < endmark);
1801 // follow portals into other leafs
1803 for (;p;p = p->next)
1805 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1808 if (leaf->worldnodeframe != r_framecount)
1810 leaf->worldnodeframe = r_framecount;
1811 if (leaf->contents != CONTENTS_SOLID)
1813 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1815 p->visframe = r_framecount;
1816 pstack[portalstack++] = p;
1820 p = pstack[--portalstack];
1832 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1833 int nodestackpos = 0;
1834 // LordHavoc: recursive descending worldnode; if portals are not
1835 // available, this is a good last resort, can cull large amounts of
1836 // geometry, but is more time consuming than portal-passage and renders
1837 // things behind walls
1840 if (R_NotCulledBox(node->mins, node->maxs))
1842 if (node->numsurfaces)
1846 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1848 if (PlaneDiff (r_origin, node->plane) < 0)
1850 for (;surf < surfend;surf++)
1852 if (surf->flags & SURF_PLANEBACK)
1854 VectorNegate(surf->plane->normal, plane.normal);
1855 plane.dist = -surf->plane->dist;
1856 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1862 for (;surf < surfend;surf++)
1864 if (!(surf->flags & SURF_PLANEBACK))
1865 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);
1871 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1872 if (PlaneDiff (r_origin, node->plane) < 0)
1874 for (;surf < surfend;surf++)
1876 if (surf->flags & SURF_PLANEBACK)
1877 surf->visframe = r_framecount;
1882 for (;surf < surfend;surf++)
1884 if (!(surf->flags & SURF_PLANEBACK))
1885 surf->visframe = r_framecount;
1891 // recurse down the children
1892 if (node->children[0]->contents >= 0)
1894 if (node->children[1]->contents >= 0)
1896 if (nodestackpos < 8192)
1897 nodestack[nodestackpos++] = node->children[1];
1898 node = node->children[0];
1902 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1903 node = node->children[0];
1908 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1909 if (node->children[1]->contents >= 0)
1911 node = node->children[1];
1914 else if (nodestackpos > 0)
1916 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1917 node = nodestack[--nodestackpos];
1922 else if (nodestackpos > 0)
1924 node = nodestack[--nodestackpos];
1930 static int r_portalframecount = 0;
1932 static void R_PVSWorldNode()
1935 mportal_t *p, *pstack[8192];
1936 msurface_t *surf, **mark, **endmark;
1941 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1944 leaf->worldnodeframe = r_framecount;
1949 leaf->visframe = r_framecount;
1951 if (leaf->nummarksurfaces)
1953 mark = leaf->firstmarksurface;
1954 endmark = mark + leaf->nummarksurfaces;
1960 // make sure surfaces are only processed once
1961 if (surf->worldnodeframe == r_framecount)
1963 surf->worldnodeframe = r_framecount;
1964 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1966 if (surf->flags & SURF_PLANEBACK)
1968 VectorNegate(surf->plane->normal, plane.normal);
1969 plane.dist = -surf->plane->dist;
1970 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1975 if (!(surf->flags & SURF_PLANEBACK))
1976 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);
1979 while (mark < endmark);
1986 // make sure surfaces are only processed once
1987 if (surf->worldnodeframe == r_framecount)
1989 surf->worldnodeframe = r_framecount;
1990 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1992 if (surf->flags & SURF_PLANEBACK)
1993 surf->visframe = r_framecount;
1997 if (!(surf->flags & SURF_PLANEBACK))
1998 surf->visframe = r_framecount;
2001 while (mark < endmark);
2005 // follow portals into other leafs
2006 for (p = leaf->portals;p;p = p->next)
2008 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2011 if (leaf->worldnodeframe != r_framecount)
2013 leaf->worldnodeframe = r_framecount;
2014 if (leaf->contents != CONTENTS_SOLID)
2016 i = (leaf - cl.worldmodel->leafs) - 1;
2017 if (worldvis[i>>3] & (1<<(i&7)))
2019 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2021 pstack[portalstack++] = p;
2025 p = pstack[--portalstack];
2037 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
2038 Cshader_t Cshader_wall_vertex_glow = {{NULL, RSurfShader_Wall_Vertex_Glow, RSurfShader_Wall_Fog}, NULL};
2039 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
2040 Cshader_t Cshader_wall_lightmap_glow = {{NULL, RSurfShader_Wall_Lightmap_Glow, RSurfShader_Wall_Fog}, NULL};
2041 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
2042 Cshader_t Cshader_wall_fullbright_glow = {{NULL, RSurfShader_Wall_Fullbright_Glow, RSurfShader_Wall_Fog}, NULL};
2043 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
2044 Cshader_t Cshader_water_glow = {{NULL, RSurfShader_Water_Glow, NULL}, NULL};
2045 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
2047 int Cshader_count = 5;
2048 Cshader_t *Cshaders[5] =
2050 &Cshader_wall_vertex,
2051 &Cshader_wall_lightmap,
2052 &Cshader_wall_fullbright,
2057 void R_PrepareSurfaces(void)
2064 for (i = 0;i < Cshader_count;i++)
2065 Cshaders[i]->chain = NULL;
2067 model = currentrenderentity->model;
2069 for (i = 0;i < model->nummodelsurfaces;i++)
2071 surf = model->modelsortedsurfaces[i];
2072 if (surf->visframe == r_framecount)
2074 if (surf->insertframe != r_framecount)
2076 surf->insertframe = r_framecount;
2078 // manually inlined R_TextureAnimation
2079 //t = R_TextureAnimation(surf->texinfo->texture);
2080 t = surf->texinfo->texture;
2081 if (t->alternate_anims != NULL && currentrenderentity->frame)
2082 t = t->alternate_anims;
2083 if (t->anim_total >= 2)
2084 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
2085 surf->currenttexture = t;
2088 surf->chain = surf->shader->chain;
2089 surf->shader->chain = surf;
2094 void R_DrawSurfaces (int type)
2100 for (i = 0;i < Cshader_count;i++)
2102 shader = Cshaders[i];
2103 if (shader->chain && shader->shaderfunc[type])
2104 // shader->shaderfunc[type](shader->chain);
2105 for (stage = 0;stage < 1000;stage++)
2106 for (surf = shader->chain;surf;surf = surf->chain)
2107 if (shader->shaderfunc[type](stage, surf))
2113 static float portalpointbuffer[256][3];
2115 void R_DrawPortals(void)
2118 // mleaf_t *leaf, *endleaf;
2119 mportal_t *portal, *endportal;
2120 mvertex_t *point/*, *endpoint*/;
2122 drawportals = r_drawportals.integer;
2123 if (drawportals < 1)
2126 leaf = cl.worldmodel->leafs;
2127 endleaf = leaf + cl.worldmodel->numleafs;
2128 for (;leaf < endleaf;leaf++)
2130 if (leaf->visframe == r_framecount && leaf->portals)
2132 i = leaf - cl.worldmodel->leafs;
2133 r = (i & 0x0007) << 5;
2134 g = (i & 0x0038) << 2;
2135 b = (i & 0x01C0) >> 1;
2136 portal = leaf->portals;
2139 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2140 point = portal->points + portal->numpoints - 1;
2141 endpoint = portal->points;
2142 for (;point >= endpoint;point--)
2143 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2145 portal = portal->next;
2150 memset(&m, 0, sizeof(m));
2151 m.transparent = true;
2152 m.blendfunc1 = GL_SRC_ALPHA;
2153 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2154 m.vertex = &portalpointbuffer[0][0];
2155 m.vertexstep = sizeof(float[3]);
2157 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2159 if (portal->visframe == r_portalframecount)
2161 if (portal->numpoints <= 256)
2163 i = portal - cl.worldmodel->portals;
2164 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2165 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2166 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2167 point = portal->points;
2168 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2170 for (i = portal->numpoints - 1;i >= 0;i--)
2171 VectorCopy(point[i].position, portalpointbuffer[i]);
2175 for (i = 0;i < portal->numpoints;i++)
2176 VectorCopy(point[i].position, portalpointbuffer[i]);
2178 R_Mesh_DrawPolygon(&m, portal->numpoints);
2184 void R_SetupForBModelRendering(void)
2191 // because bmodels can be reused, we have to decide which things to render
2192 // from scratch every time
2194 model = currentrenderentity->model;
2196 softwaretransformforentity (currentrenderentity);
2197 softwareuntransform(r_origin, modelorg);
2199 for (i = 0;i < model->nummodelsurfaces;i++)
2201 s = model->modelsortedsurfaces[i];
2202 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
2203 s->visframe = r_framecount;
2206 s->worldnodeframe = -1;
2208 s->dlightframe = -1;
2209 s->insertframe = -1;
2213 void R_SetupForWorldRendering(void)
2215 // there is only one instance of the world, but it can be rendered in
2218 currentrenderentity = &cl_entities[0].render;
2219 softwaretransformidentity();
2222 static void R_SurfMarkLights (void)
2227 if (r_dynamic.integer)
2230 if (!r_vertexsurfaces.integer)
2232 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2234 s = currentrenderentity->model->modelsortedsurfaces[i];
2235 if (s->visframe == r_framecount && s->lightmaptexture != NULL)
2237 if (s->cached_dlight
2238 || s->cached_ambient != r_ambient.value
2239 || s->cached_lightscalebit != lightscalebit)
2240 R_BuildLightMap(s, false); // base lighting changed
2241 else if (r_dynamic.integer)
2243 if (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
2244 || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
2245 || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
2246 || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
2247 //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
2248 // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
2249 // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
2250 // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
2251 R_BuildLightMap(s, false); // base lighting changed
2252 else if (s->dlightframe == r_framecount && r_dlightmap.integer)
2253 R_BuildLightMap(s, true); // only dlights
2260 void R_MarkWorldLights(void)
2262 R_SetupForWorldRendering();
2271 void R_DrawWorld (void)
2273 R_SetupForWorldRendering();
2275 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2276 R_SolidWorldNode ();
2286 void R_DrawBrushModelSky (void)
2288 R_SetupForBModelRendering();
2290 R_PrepareSurfaces();
2291 R_DrawSurfaces(SHADERSTAGE_SKY);
2294 void R_DrawBrushModelNormal (void)
2298 // have to flush queue because of possible lightmap reuse
2301 R_SetupForBModelRendering();
2305 R_PrepareSurfaces();
2307 if (!skyrendermasked)
2308 R_DrawSurfaces(SHADERSTAGE_SKY);
2309 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2310 R_DrawSurfaces(SHADERSTAGE_FOG);