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 byte 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] = (byte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
266 bl[1] = (byte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
267 bl[2] = (byte) ((((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 byte *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 if (r_mergesky.integer)
563 m.tex[0] = R_GetTexture(mergeskytexture);
565 m.tex[0] = R_GetTexture(solidskytexture);
566 m.texcoords[0] = &svert[0].st[0];
567 m.texcoordstep[0] = sizeof(surfvert_t);
568 speedscale = cl.time * (8.0/128.0);
569 speedscale -= (int)speedscale;
570 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
572 softwaretransform(v->v, sv->v);
573 VectorSubtract (sv->v, r_origin, dir);
574 // flatten the sphere
577 number = DotProduct(dir, dir);
579 length = 3.0f / sqrt(number);
581 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
582 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
585 sv->st[0] = speedscale + dir[0] * length;
586 sv->st[1] = speedscale + dir[1] * length;
593 memset(&m, 0, sizeof(m));
594 m.transparent = false;
595 m.blendfunc1 = GL_ONE;
596 m.blendfunc2 = GL_ZERO;
597 m.numtriangles = s->mesh.numtriangles;
598 m.numverts = s->mesh.numverts;
599 m.index = s->mesh.index;
604 if (softwaretransform_complexity)
606 m.vertex = &svert[0].v[0];
607 m.vertexstep = sizeof(surfvert_t);
608 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
609 softwaretransform(v->v, sv->v);
613 m.vertex = &s->mesh.vertex[0].v[0];
614 m.vertexstep = sizeof(surfvertex_t);
622 if (skyrenderglquake && !r_mergesky.integer)
624 memset(&m, 0, sizeof(m));
625 m.transparent = false;
626 m.blendfunc1 = GL_SRC_ALPHA;
627 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
628 m.numtriangles = s->mesh.numtriangles;
629 m.numverts = s->mesh.numverts;
630 m.index = s->mesh.index;
631 m.vertex = &svert[0].v[0];
632 m.vertexstep = sizeof(surfvert_t);
637 m.tex[0] = R_GetTexture(alphaskytexture);
638 m.texcoords[0] = &svert[0].st[0];
639 m.texcoordstep[0] = sizeof(surfvert_t);
640 speedscale = cl.time * (16.0/128.0);
641 speedscale -= (int)speedscale;
642 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
644 softwaretransform(v->v, sv->v);
645 VectorSubtract (sv->v, r_origin, dir);
646 // flatten the sphere
649 number = DotProduct(dir, dir);
651 length = 3.0f / sqrt(number);
653 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
654 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
657 sv->st[0] = speedscale + dir[0] * length;
658 sv->st[1] = speedscale + dir[1] * length;
669 static int RSurf_Light(int *dlightbits, int numverts)
672 int i, l, lit = false;
676 for (l = 0;l < r_numdlights;l++)
678 if (dlightbits[l >> 5] & (1 << (l & 31)))
681 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
682 VectorCopy(rd->origin, lightorigin);
683 for (i = 0, sv = svert;i < numverts;i++, sv++)
685 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
686 if (f < rd->cullradius2)
688 f = (1.0f / f) - rd->lightsubtract;
689 sv->c[0] += rd->light[0] * f;
690 sv->c[1] += rd->light[1] * f;
691 sv->c[2] += rd->light[2] * f;
700 static void RSurfShader_Water_Pass_Base(msurface_t *s)
703 float diff[3], alpha, ifog;
707 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
709 memset(&m, 0, sizeof(m));
710 if (alpha != 1 || s->currenttexture->fogtexture != NULL)
712 m.transparent = true;
713 m.blendfunc1 = GL_SRC_ALPHA;
714 m.blendfunc2 = GL_ONE; //_MINUS_SRC_ALPHA;
718 m.transparent = false;
719 m.blendfunc1 = GL_ONE;
720 m.blendfunc2 = GL_ZERO;
722 m.numtriangles = s->mesh.numtriangles;
723 m.numverts = s->mesh.numverts;
724 m.index = s->mesh.index;
725 m.vertex = &svert[0].v[0];
726 m.vertexstep = sizeof(surfvert_t);
727 m.color = &svert[0].c[0];
728 m.colorstep = sizeof(surfvert_t);
729 m.tex[0] = R_GetTexture(s->currenttexture->texture);
730 m.texcoords[0] = &svert[0].st[0];
731 m.texcoordstep[0] = sizeof(surfvert_t);
732 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
734 softwaretransform(v->v, sv->v);
735 if (r_waterripple.value)
736 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];
737 if (s->flags & SURF_DRAWFULLBRIGHT)
751 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
752 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
754 if (s->dlightframe == r_framecount && !(s->flags & SURF_DRAWFULLBRIGHT))
755 RSurf_Light(s->dlightbits, m.numverts);
756 if (fogenabled/* && m.blendfunc2 == GL_ONE_MINUS_SRC_ALPHA*/)
758 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
760 VectorSubtract(sv->v, r_origin, diff);
761 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
770 static void RSurfShader_Water_Pass_Glow(msurface_t *s)
773 float diff[3], alpha, ifog;
777 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
779 memset(&m, 0, sizeof(m));
780 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
781 m.blendfunc1 = GL_SRC_ALPHA;
782 m.blendfunc2 = GL_ONE;
783 m.numtriangles = s->mesh.numtriangles;
784 m.numverts = s->mesh.numverts;
785 m.index = s->mesh.index;
786 m.vertex = &svert[0].v[0];
787 m.vertexstep = sizeof(surfvert_t);
792 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
793 m.texcoords[0] = &svert[0].st[0];
794 m.texcoordstep[0] = sizeof(surfvert_t);
797 m.color = &svert[0].c[0];
798 m.colorstep = sizeof(surfvert_t);
799 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
801 softwaretransform(v->v, sv->v);
802 if (r_waterripple.value)
803 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];
804 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
805 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
806 VectorSubtract(sv->v, r_origin, diff);
807 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
808 sv->c[0] = m.cr * ifog;
809 sv->c[1] = m.cg * ifog;
810 sv->c[2] = m.cb * ifog;
816 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
818 softwaretransform(v->v, sv->v);
819 if (r_waterripple.value)
820 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];
821 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
822 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
828 static void RSurfShader_Water_Pass_Fog(msurface_t *s)
836 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
838 memset(&m, 0, sizeof(m));
839 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
840 m.blendfunc1 = GL_SRC_ALPHA;
841 m.blendfunc2 = GL_ONE;
842 m.numtriangles = s->mesh.numtriangles;
843 m.numverts = s->mesh.numverts;
844 m.index = s->mesh.index;
845 m.vertex = &svert[0].v[0];
846 m.vertexstep = sizeof(surfvert_t);
847 m.color = &svert[0].c[0];
848 m.colorstep = sizeof(surfvert_t);
849 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
850 m.texcoords[0] = &svert[0].st[0];
851 m.texcoordstep[0] = sizeof(surfvert_t);
853 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
855 softwaretransform(v->v, sv->v);
856 if (r_waterripple.value)
857 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];
860 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
861 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
863 VectorSubtract(sv->v, r_origin, diff);
864 sv->c[0] = fogcolor[0];
865 sv->c[1] = fogcolor[1];
866 sv->c[2] = fogcolor[2];
867 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
872 static int RSurfShader_Water(int stage, msurface_t *s)
877 RSurfShader_Water_Pass_Base(s);
880 if (s->currenttexture->glowtexture)
881 RSurfShader_Water_Pass_Glow(s);
884 if (fogenabled && (s->flags & SURF_DRAWNOALPHA))
886 RSurfShader_Water_Pass_Fog(s);
896 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *s)
904 memset(&m, 0, sizeof(m));
905 if (currentrenderentity->effects & EF_ADDITIVE)
907 m.transparent = true;
908 m.blendfunc1 = GL_SRC_ALPHA;
909 m.blendfunc2 = GL_ONE;
911 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
913 m.transparent = true;
914 m.blendfunc1 = GL_SRC_ALPHA;
915 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
919 m.transparent = false;
920 m.blendfunc1 = GL_ONE;
921 m.blendfunc2 = GL_ZERO;
923 m.numtriangles = s->mesh.numtriangles;
924 m.numverts = s->mesh.numverts;
925 m.index = s->mesh.index;
929 if (gl_combine.integer)
933 m.ca = currentrenderentity->alpha;
934 m.tex[0] = R_GetTexture(s->currenttexture->texture);
935 m.tex[1] = R_GetTexture(s->lightmaptexture);
936 m.texcoords[0] = &s->mesh.vertex->st[0];
937 m.texcoords[1] = &s->mesh.vertex->uv[0];
938 m.texcoordstep[0] = sizeof(surfvertex_t);
939 m.texcoordstep[1] = sizeof(surfvertex_t);
942 m.color = &svert[0].c[0];
943 m.colorstep = sizeof(surfvert_t);
944 if (softwaretransform_complexity)
946 m.vertex = &svert[0].v[0];
947 m.vertexstep = sizeof(surfvert_t);
948 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
950 softwaretransform(v->v, sv->v);
951 VectorSubtract(sv->v, r_origin, diff);
952 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
953 sv->c[0] = m.cr * ifog;
954 sv->c[1] = m.cg * ifog;
955 sv->c[2] = m.cb * ifog;
961 m.vertex = &s->mesh.vertex->v[0];
962 m.vertexstep = sizeof(surfvertex_t);
963 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
965 VectorSubtract(v->v, r_origin, diff);
966 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
967 sv->c[0] = m.cr * ifog;
968 sv->c[1] = m.cg * ifog;
969 sv->c[2] = m.cb * ifog;
976 if (softwaretransform_complexity)
978 m.vertex = &svert[0].v[0];
979 m.vertexstep = sizeof(surfvert_t);
980 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
981 softwaretransform(v->v, sv->v);
985 m.vertex = &s->mesh.vertex->v[0];
986 m.vertexstep = sizeof(surfvertex_t);
992 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
999 memset(&m, 0, sizeof(m));
1000 m.transparent = false;
1001 m.blendfunc1 = GL_ONE;
1002 m.blendfunc2 = GL_ZERO;
1003 m.numtriangles = s->mesh.numtriangles;
1004 m.numverts = s->mesh.numverts;
1005 m.index = s->mesh.index;
1019 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1020 m.texcoords[0] = &s->mesh.vertex->st[0];
1021 m.texcoordstep[0] = sizeof(surfvertex_t);
1022 if (softwaretransform_complexity)
1024 m.vertex = &svert[0].v[0];
1025 m.vertexstep = sizeof(surfvert_t);
1026 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1027 softwaretransform(v->v, sv->v);
1031 m.vertex = &s->mesh.vertex->v[0];
1032 m.vertexstep = sizeof(surfvertex_t);
1037 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
1040 float diff[3], ifog;
1045 memset(&m, 0, sizeof(m));
1046 m.transparent = false;
1047 m.blendfunc1 = GL_ZERO;
1048 m.blendfunc2 = GL_SRC_COLOR;
1049 m.numtriangles = s->mesh.numtriangles;
1050 m.numverts = s->mesh.numverts;
1051 m.index = s->mesh.index;
1058 m.tex[0] = R_GetTexture(s->lightmaptexture);
1059 m.texcoords[0] = &s->mesh.vertex->uv[0];
1060 m.texcoordstep[0] = sizeof(surfvertex_t);
1063 m.color = &svert[0].c[0];
1064 m.colorstep = sizeof(surfvert_t);
1065 if (softwaretransform_complexity)
1067 m.vertex = &svert[0].v[0];
1068 m.vertexstep = sizeof(surfvert_t);
1069 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1071 softwaretransform(v->v, sv->v);
1072 VectorSubtract(sv->v, r_origin, diff);
1073 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1074 sv->c[0] = m.cr * ifog;
1075 sv->c[1] = m.cg * ifog;
1076 sv->c[2] = m.cb * ifog;
1082 m.vertex = &s->mesh.vertex->v[0];
1083 m.vertexstep = sizeof(surfvertex_t);
1084 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1086 VectorSubtract(v->v, r_origin, diff);
1087 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1088 sv->c[0] = m.cr * ifog;
1089 sv->c[1] = m.cg * ifog;
1090 sv->c[2] = m.cb * ifog;
1097 if (softwaretransform_complexity)
1099 m.vertex = &svert[0].v[0];
1100 m.vertexstep = sizeof(surfvert_t);
1101 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1102 softwaretransform(v->v, sv->v);
1106 m.vertex = &s->mesh.vertex->v[0];
1107 m.vertexstep = sizeof(surfvertex_t);
1113 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
1116 float c[3], base[3], scale, diff[3], ifog;
1122 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
1124 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1126 memset(&m, 0, sizeof(m));
1127 if (currentrenderentity->effects & EF_ADDITIVE)
1129 m.transparent = true;
1130 m.blendfunc1 = GL_SRC_ALPHA;
1131 m.blendfunc2 = GL_ONE;
1133 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1135 m.transparent = true;
1136 m.blendfunc1 = GL_SRC_ALPHA;
1137 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1141 m.transparent = false;
1142 m.blendfunc1 = GL_ONE;
1143 m.blendfunc2 = GL_ZERO;
1145 m.numtriangles = s->mesh.numtriangles;
1146 m.numverts = s->mesh.numverts;
1147 m.index = s->mesh.index;
1148 m.vertex = &svert[0].v[0];
1149 m.vertexstep = sizeof(surfvert_t);
1150 m.color = &svert[0].c[0];
1151 m.colorstep = sizeof(surfvert_t);
1152 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1153 m.texcoords[0] = &s->mesh.vertex->st[0];
1154 m.texcoordstep[0] = sizeof(surfvertex_t);
1155 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1157 softwaretransform(v->v, sv->v);
1158 VectorCopy(base, c);
1159 if (s->styles[0] != 255)
1161 lm = s->samples + v->lightmapoffset;
1162 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
1163 VectorMA(c, scale, lm, c);
1164 if (s->styles[1] != 255)
1167 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
1168 VectorMA(c, scale, lm, c);
1169 if (s->styles[2] != 255)
1172 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
1173 VectorMA(c, scale, lm, c);
1174 if (s->styles[3] != 255)
1177 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
1178 VectorMA(c, scale, lm, c);
1186 sv->c[3] = currentrenderentity->alpha;
1188 if (s->dlightframe == r_framecount)
1189 RSurf_Light(s->dlightbits, m.numverts);
1192 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1194 VectorSubtract(sv->v, r_origin, diff);
1195 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1204 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *s)
1207 float diff[3], ifog;
1212 memset(&m, 0, sizeof(m));
1213 if (currentrenderentity->effects & EF_ADDITIVE)
1215 m.transparent = true;
1216 m.blendfunc1 = GL_SRC_ALPHA;
1217 m.blendfunc2 = GL_ONE;
1219 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1221 m.transparent = true;
1222 m.blendfunc1 = GL_SRC_ALPHA;
1223 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1227 m.transparent = false;
1228 m.blendfunc1 = GL_ONE;
1229 m.blendfunc2 = GL_ZERO;
1231 m.numtriangles = s->mesh.numtriangles;
1232 m.numverts = s->mesh.numverts;
1233 m.index = s->mesh.index;
1234 m.vertex = &svert[0].v[0];
1235 m.vertexstep = sizeof(surfvert_t);
1236 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1237 m.texcoords[0] = &s->mesh.vertex->st[0];
1238 m.texcoordstep[0] = sizeof(surfvertex_t);
1241 m.color = &svert[0].c[0];
1242 m.colorstep = sizeof(surfvert_t);
1243 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1245 softwaretransform(v->v, sv->v);
1246 VectorSubtract(sv->v, r_origin, diff);
1247 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1251 sv->c[3] = currentrenderentity->alpha;
1256 m.cr = m.cg = m.cb = 1;
1257 m.ca = currentrenderentity->alpha;
1258 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1259 softwaretransform(v->v, sv->v);
1264 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1267 float diff[3], ifog;
1272 memset(&m, 0, sizeof(m));
1273 if (currentrenderentity->effects & EF_ADDITIVE)
1274 m.transparent = true;
1275 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1276 m.transparent = true;
1278 m.transparent = false;
1279 m.blendfunc1 = GL_SRC_ALPHA;
1280 m.blendfunc2 = GL_ONE;
1281 m.numtriangles = s->mesh.numtriangles;
1282 m.numverts = s->mesh.numverts;
1283 m.index = s->mesh.index;
1284 m.vertex = &svert[0].v[0];
1285 m.vertexstep = sizeof(surfvert_t);
1286 m.color = &svert[0].c[0];
1287 m.colorstep = sizeof(surfvert_t);
1288 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1289 m.texcoords[0] = &s->mesh.vertex->st[0];
1290 m.texcoordstep[0] = sizeof(surfvertex_t);
1291 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1293 softwaretransform(v->v, sv->v);
1297 sv->c[3] = currentrenderentity->alpha;
1299 if (RSurf_Light(s->dlightbits, m.numverts))
1303 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1305 VectorSubtract(sv->v, r_origin, diff);
1306 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1316 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1319 float diff[3], ifog;
1324 memset(&m, 0, sizeof(m));
1325 if (currentrenderentity->effects & EF_ADDITIVE)
1326 m.transparent = true;
1327 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1328 m.transparent = true;
1330 m.transparent = false;
1331 m.blendfunc1 = GL_SRC_ALPHA;
1332 m.blendfunc2 = GL_ONE;
1333 m.numtriangles = s->mesh.numtriangles;
1334 m.numverts = s->mesh.numverts;
1335 m.index = s->mesh.index;
1339 m.ca = currentrenderentity->alpha;
1340 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1341 m.texcoords[0] = &s->mesh.vertex->st[0];
1342 m.texcoordstep[0] = sizeof(surfvertex_t);
1345 m.color = &svert[0].c[0];
1346 m.colorstep = sizeof(surfvert_t);
1347 if (softwaretransform_complexity)
1349 m.vertex = &svert[0].v[0];
1350 m.vertexstep = sizeof(surfvert_t);
1351 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1353 softwaretransform(v->v, sv->v);
1354 VectorSubtract(sv->v, r_origin, diff);
1355 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1356 sv->c[0] = m.cr * ifog;
1357 sv->c[1] = m.cg * ifog;
1358 sv->c[2] = m.cb * ifog;
1364 m.vertex = &s->mesh.vertex->v[0];
1365 m.vertexstep = sizeof(surfvertex_t);
1366 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1368 VectorSubtract(v->v, r_origin, diff);
1369 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1370 sv->c[0] = m.cr * ifog;
1371 sv->c[1] = m.cg * ifog;
1372 sv->c[2] = m.cb * ifog;
1379 if (softwaretransform_complexity)
1381 m.vertex = &svert[0].v[0];
1382 m.vertexstep = sizeof(surfvert_t);
1383 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1384 softwaretransform(v->v, sv->v);
1388 m.vertex = &s->mesh.vertex->v[0];
1389 m.vertexstep = sizeof(surfvertex_t);
1395 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1403 memset(&m, 0, sizeof(m));
1404 if (currentrenderentity->effects & EF_ADDITIVE)
1405 m.transparent = true;
1406 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1407 m.transparent = true;
1409 m.transparent = false;
1410 m.blendfunc1 = GL_SRC_ALPHA;
1411 m.blendfunc2 = GL_ONE;
1412 m.numtriangles = s->mesh.numtriangles;
1413 m.numverts = s->mesh.numverts;
1414 m.index = s->mesh.index;
1415 m.color = &svert[0].c[0];
1416 m.colorstep = sizeof(surfvert_t);
1417 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1418 m.texcoords[0] = &s->mesh.vertex->st[0];
1419 m.texcoordstep[0] = sizeof(surfvertex_t);
1420 if (softwaretransform_complexity)
1422 m.vertex = &svert[0].v[0];
1423 m.vertexstep = sizeof(surfvert_t);
1424 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1426 softwaretransform(v->v, sv->v);
1427 VectorSubtract(sv->v, r_origin, diff);
1428 sv->c[0] = fogcolor[0];
1429 sv->c[1] = fogcolor[1];
1430 sv->c[2] = fogcolor[2];
1431 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1436 m.vertex = &s->mesh.vertex->v[0];
1437 m.vertexstep = sizeof(surfvertex_t);
1438 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1440 VectorSubtract(v->v, r_origin, diff);
1441 sv->c[0] = fogcolor[0];
1442 sv->c[1] = fogcolor[1];
1443 sv->c[2] = fogcolor[2];
1444 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1450 static int RSurfShader_Wall_Fullbright(int stage, msurface_t *s)
1455 RSurfShader_Wall_Pass_BaseFullbright(s);
1458 if (s->currenttexture->glowtexture)
1459 RSurfShader_Wall_Pass_Glow(s);
1466 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1471 RSurfShader_Wall_Pass_BaseVertex(s);
1474 if (s->currenttexture->glowtexture)
1475 RSurfShader_Wall_Pass_Glow(s);
1482 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1484 if (r_vertexsurfaces.integer)
1489 RSurfShader_Wall_Pass_BaseVertex(s);
1492 if (s->currenttexture->glowtexture)
1493 RSurfShader_Wall_Pass_Glow(s);
1499 else if (r_multitexture.integer)
1501 if (r_dlightmap.integer)
1506 RSurfShader_Wall_Pass_BaseMTex(s);
1509 if (s->currenttexture->glowtexture)
1510 RSurfShader_Wall_Pass_Glow(s);
1521 RSurfShader_Wall_Pass_BaseMTex(s);
1524 if (s->dlightframe == r_framecount)
1525 RSurfShader_Wall_Pass_Light(s);
1528 if (s->currenttexture->glowtexture)
1529 RSurfShader_Wall_Pass_Glow(s);
1536 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1541 RSurfShader_Wall_Pass_BaseVertex(s);
1544 if (s->currenttexture->glowtexture)
1545 RSurfShader_Wall_Pass_Glow(s);
1553 if (r_dlightmap.integer)
1558 RSurfShader_Wall_Pass_BaseTexture(s);
1561 RSurfShader_Wall_Pass_BaseLightmap(s);
1564 if (s->currenttexture->glowtexture)
1565 RSurfShader_Wall_Pass_Glow(s);
1576 RSurfShader_Wall_Pass_BaseTexture(s);
1579 RSurfShader_Wall_Pass_BaseLightmap(s);
1582 if (s->dlightframe == r_framecount)
1583 RSurfShader_Wall_Pass_Light(s);
1586 if (s->currenttexture->glowtexture)
1587 RSurfShader_Wall_Pass_Glow(s);
1596 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1598 if (stage == 0 && fogenabled)
1600 RSurfShader_Wall_Pass_Fog(s);
1608 =============================================================
1612 =============================================================
1615 static void RSurf_Callback(void *data, void *junk)
1617 ((msurface_t *)data)->visframe = r_framecount;
1620 static void R_SolidWorldNode (void)
1622 if (r_viewleaf->contents != CONTENTS_SOLID)
1625 mportal_t *p, *pstack[8192];
1626 msurface_t *surf, **mark, **endmark;
1629 // LordHavoc: portal-passage worldnode; follows portals leading
1630 // outward from viewleaf, if a portal leads offscreen it is not
1631 // followed, in indoor maps this can often cull a great deal of
1632 // geometry away when pvs data is not present (useful with pvs as well)
1635 leaf->worldnodeframe = r_framecount;
1640 leaf->visframe = r_framecount;
1642 if (leaf->nummarksurfaces)
1644 mark = leaf->firstmarksurface;
1645 endmark = mark + leaf->nummarksurfaces;
1651 // make sure surfaces are only processed once
1652 if (surf->worldnodeframe == r_framecount)
1654 surf->worldnodeframe = r_framecount;
1655 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1657 if (surf->flags & SURF_PLANEBACK)
1659 VectorNegate(surf->plane->normal, plane.normal);
1660 plane.dist = -surf->plane->dist;
1661 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1666 if (!(surf->flags & SURF_PLANEBACK))
1667 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);
1670 while (mark < endmark);
1677 // make sure surfaces are only processed once
1678 if (surf->worldnodeframe == r_framecount)
1680 surf->worldnodeframe = r_framecount;
1681 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1683 if (surf->flags & SURF_PLANEBACK)
1684 surf->visframe = r_framecount;
1688 if (!(surf->flags & SURF_PLANEBACK))
1689 surf->visframe = r_framecount;
1692 while (mark < endmark);
1696 // follow portals into other leafs
1698 for (;p;p = p->next)
1700 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1703 if (leaf->worldnodeframe != r_framecount)
1705 leaf->worldnodeframe = r_framecount;
1706 if (leaf->contents != CONTENTS_SOLID)
1708 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1710 p->visframe = r_framecount;
1711 pstack[portalstack++] = p;
1715 p = pstack[--portalstack];
1727 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1728 int nodestackpos = 0;
1729 // LordHavoc: recursive descending worldnode; if portals are not
1730 // available, this is a good last resort, can cull large amounts of
1731 // geometry, but is more time consuming than portal-passage and renders
1732 // things behind walls
1735 if (R_NotCulledBox(node->mins, node->maxs))
1737 if (node->numsurfaces)
1741 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1743 if (PlaneDiff (r_origin, node->plane) < 0)
1745 for (;surf < surfend;surf++)
1747 if (surf->flags & SURF_PLANEBACK)
1749 VectorNegate(surf->plane->normal, plane.normal);
1750 plane.dist = -surf->plane->dist;
1751 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1757 for (;surf < surfend;surf++)
1759 if (!(surf->flags & SURF_PLANEBACK))
1760 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);
1766 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1767 if (PlaneDiff (r_origin, node->plane) < 0)
1769 for (;surf < surfend;surf++)
1771 if (surf->flags & SURF_PLANEBACK)
1772 surf->visframe = r_framecount;
1777 for (;surf < surfend;surf++)
1779 if (!(surf->flags & SURF_PLANEBACK))
1780 surf->visframe = r_framecount;
1786 // recurse down the children
1787 if (node->children[0]->contents >= 0)
1789 if (node->children[1]->contents >= 0)
1791 if (nodestackpos < 8192)
1792 nodestack[nodestackpos++] = node->children[1];
1793 node = node->children[0];
1797 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1798 node = node->children[0];
1803 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1804 if (node->children[1]->contents >= 0)
1806 node = node->children[1];
1809 else if (nodestackpos > 0)
1811 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1812 node = nodestack[--nodestackpos];
1817 else if (nodestackpos > 0)
1819 node = nodestack[--nodestackpos];
1825 static int r_portalframecount = 0;
1827 static void R_PVSWorldNode()
1830 mportal_t *p, *pstack[8192];
1831 msurface_t *surf, **mark, **endmark;
1836 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1839 leaf->worldnodeframe = r_framecount;
1844 leaf->visframe = r_framecount;
1846 if (leaf->nummarksurfaces)
1848 mark = leaf->firstmarksurface;
1849 endmark = mark + leaf->nummarksurfaces;
1855 // make sure surfaces are only processed once
1856 if (surf->worldnodeframe == r_framecount)
1858 surf->worldnodeframe = r_framecount;
1859 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1861 if (surf->flags & SURF_PLANEBACK)
1863 VectorNegate(surf->plane->normal, plane.normal);
1864 plane.dist = -surf->plane->dist;
1865 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1870 if (!(surf->flags & SURF_PLANEBACK))
1871 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);
1874 while (mark < endmark);
1881 // make sure surfaces are only processed once
1882 if (surf->worldnodeframe == r_framecount)
1884 surf->worldnodeframe = r_framecount;
1885 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1887 if (surf->flags & SURF_PLANEBACK)
1888 surf->visframe = r_framecount;
1892 if (!(surf->flags & SURF_PLANEBACK))
1893 surf->visframe = r_framecount;
1896 while (mark < endmark);
1900 // follow portals into other leafs
1901 for (p = leaf->portals;p;p = p->next)
1903 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1906 if (leaf->worldnodeframe != r_framecount)
1908 leaf->worldnodeframe = r_framecount;
1909 if (leaf->contents != CONTENTS_SOLID)
1911 i = (leaf - cl.worldmodel->leafs) - 1;
1912 if (worldvis[i>>3] & (1<<(i&7)))
1914 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1916 pstack[portalstack++] = p;
1920 p = pstack[--portalstack];
1932 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1933 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1934 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
1935 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1936 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1938 int Cshader_count = 5;
1939 Cshader_t *Cshaders[5] =
1941 &Cshader_wall_vertex,
1942 &Cshader_wall_lightmap,
1943 &Cshader_wall_fullbright,
1948 void R_PrepareSurfaces(void)
1955 for (i = 0;i < Cshader_count;i++)
1956 Cshaders[i]->chain = NULL;
1958 model = currentrenderentity->model;
1960 for (i = 0;i < model->nummodelsurfaces;i++)
1962 surf = model->modelsortedsurfaces[i];
1963 if (surf->visframe == r_framecount)
1965 if (surf->insertframe != r_framecount)
1967 surf->insertframe = r_framecount;
1969 // manually inlined R_TextureAnimation
1970 //t = R_TextureAnimation(surf->texinfo->texture);
1971 t = surf->texinfo->texture;
1972 if (t->alternate_anims != NULL && currentrenderentity->frame)
1973 t = t->alternate_anims;
1974 if (t->anim_total >= 2)
1975 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1976 surf->currenttexture = t;
1979 surf->chain = surf->shader->chain;
1980 surf->shader->chain = surf;
1985 void R_DrawSurfaces (int type)
1991 for (i = 0;i < Cshader_count;i++)
1993 shader = Cshaders[i];
1994 if (shader->chain && shader->shaderfunc[type])
1995 for (stage = 0;stage < 1000;stage++)
1996 for (surf = shader->chain;surf;surf = surf->chain)
1997 if (shader->shaderfunc[type](stage, surf))
2003 static float portalpointbuffer[256][3];
2005 void R_DrawPortals(void)
2008 // mleaf_t *leaf, *endleaf;
2009 mportal_t *portal, *endportal;
2010 mvertex_t *point/*, *endpoint*/;
2012 drawportals = r_drawportals.integer;
2013 if (drawportals < 1)
2016 leaf = cl.worldmodel->leafs;
2017 endleaf = leaf + cl.worldmodel->numleafs;
2018 for (;leaf < endleaf;leaf++)
2020 if (leaf->visframe == r_framecount && leaf->portals)
2022 i = leaf - cl.worldmodel->leafs;
2023 r = (i & 0x0007) << 5;
2024 g = (i & 0x0038) << 2;
2025 b = (i & 0x01C0) >> 1;
2026 portal = leaf->portals;
2029 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2030 point = portal->points + portal->numpoints - 1;
2031 endpoint = portal->points;
2032 for (;point >= endpoint;point--)
2033 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2035 portal = portal->next;
2040 memset(&m, 0, sizeof(m));
2041 m.transparent = true;
2042 m.blendfunc1 = GL_SRC_ALPHA;
2043 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2044 m.vertex = &portalpointbuffer[0][0];
2045 m.vertexstep = sizeof(float[3]);
2047 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2049 if (portal->visframe == r_portalframecount)
2051 if (portal->numpoints <= 256)
2053 i = portal - cl.worldmodel->portals;
2054 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2055 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2056 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2057 point = portal->points;
2058 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2060 for (i = portal->numpoints - 1;i >= 0;i--)
2061 VectorCopy(point[i].position, portalpointbuffer[i]);
2065 for (i = 0;i < portal->numpoints;i++)
2066 VectorCopy(point[i].position, portalpointbuffer[i]);
2068 R_Mesh_DrawPolygon(&m, portal->numpoints);
2074 void R_SetupForBModelRendering(void)
2081 // because bmodels can be reused, we have to decide which things to render
2082 // from scratch every time
2084 model = currentrenderentity->model;
2086 softwaretransformforentity (currentrenderentity);
2087 softwareuntransform(r_origin, modelorg);
2089 for (i = 0;i < model->nummodelsurfaces;i++)
2091 s = model->modelsortedsurfaces[i];
2092 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
2093 s->visframe = r_framecount;
2096 s->worldnodeframe = -1;
2098 s->dlightframe = -1;
2099 s->insertframe = -1;
2103 void R_SetupForWorldRendering(void)
2105 // there is only one instance of the world, but it can be rendered in
2108 currentrenderentity = &cl_entities[0].render;
2109 softwaretransformidentity();
2112 static void R_SurfMarkLights (void)
2117 if (r_dynamic.integer)
2120 if (!r_vertexsurfaces.integer)
2122 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2124 s = currentrenderentity->model->modelsortedsurfaces[i];
2125 if (s->visframe == r_framecount && s->lightmaptexture != NULL)
2127 if (s->cached_dlight
2128 || s->cached_ambient != r_ambient.value
2129 || s->cached_lightscalebit != lightscalebit)
2130 R_BuildLightMap(s, false); // base lighting changed
2131 else if (r_dynamic.integer)
2133 if (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
2134 || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
2135 || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
2136 || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
2137 //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
2138 // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
2139 // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
2140 // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
2141 R_BuildLightMap(s, false); // base lighting changed
2142 else if (s->dlightframe == r_framecount && r_dlightmap.integer)
2143 R_BuildLightMap(s, true); // only dlights
2150 void R_MarkWorldLights(void)
2152 R_SetupForWorldRendering();
2161 void R_DrawWorld (void)
2163 R_SetupForWorldRendering();
2165 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2166 R_SolidWorldNode ();
2176 void R_DrawBrushModelSky (void)
2178 R_SetupForBModelRendering();
2180 R_PrepareSurfaces();
2181 R_DrawSurfaces(SHADERSTAGE_SKY);
2184 void R_DrawBrushModelNormal (void)
2188 // have to flush queue because of possible lightmap reuse
2191 R_SetupForBModelRendering();
2195 R_PrepareSurfaces();
2197 if (!skyrendermasked)
2198 R_DrawSurfaces(SHADERSTAGE_SKY);
2199 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2200 R_DrawSurfaces(SHADERSTAGE_FOG);