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 if (!dlightdivtable[1])
55 dlightdivtable[0] = 4194304;
56 for (i = 1;i < 32768;i++)
57 dlightdivtable[i] = 4194304 / (i << 7);
60 Cvar_RegisterVariable(&r_ambient);
61 Cvar_RegisterVariable(&r_vertexsurfaces);
62 Cvar_RegisterVariable(&r_dlightmap);
63 Cvar_RegisterVariable(&r_drawportals);
64 Cvar_RegisterVariable(&r_testvis);
66 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
69 static int R_AddDynamicLights (msurface_t *surf)
71 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
76 // LordHavoc: use 64bit integer... shame it's not very standardized...
77 #if _MSC_VER || __BORLANDC__
85 smax = (surf->extents[0] >> 4) + 1;
86 tmax = (surf->extents[1] >> 4) + 1;
88 for (lnum = 0; lnum < r_numdlights; lnum++)
90 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
91 continue; // not lit by this light
93 softwareuntransform(r_dlight[lnum].origin, local);
94 // VectorSubtract (r_dlight[lnum].origin, currentrenderentity->origin, local);
95 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
97 // for comparisons to minimum acceptable light
98 // compensate for LIGHTOFFSET
99 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
101 // already clamped, skip this
102 // clamp radius to avoid exceeding 32768 entry division table
103 //if (maxdist > 4194304)
104 // maxdist = 4194304;
107 dist2 += LIGHTOFFSET;
108 if (dist2 >= maxdist)
111 impact[0] = local[0] - surf->plane->normal[0] * dist;
112 impact[1] = local[1] - surf->plane->normal[1] * dist;
113 impact[2] = local[2] - surf->plane->normal[2] * dist;
115 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
116 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
118 s = bound(0, impacts, smax * 16) - impacts;
119 t = bound(0, impactt, tmax * 16) - impactt;
120 i = s * s + t * t + dist2;
124 // reduce calculations
125 for (s = 0, i = impacts; s < smax; s++, i -= 16)
126 sdtable[s] = i * i + dist2;
128 maxdist3 = maxdist - dist2;
130 // convert to 8.8 blocklights format
131 red = r_dlight[lnum].light[0];
132 green = r_dlight[lnum].light[1];
133 blue = r_dlight[lnum].light[2];
134 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
139 for (t = 0;t < tmax;t++, i -= 16)
142 // make sure some part of it is visible on this line
145 maxdist2 = maxdist - td;
146 for (s = 0;s < smax;s++)
148 if (sdtable[s] < maxdist2)
150 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
153 bl[0] += (red * k) >> 8;
154 bl[1] += (green * k) >> 8;
155 bl[2] += (blue * k) >> 8;
169 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
172 msurface_t *surf, *endsurf;
173 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
176 // LordHavoc: use 64bit integer... shame it's not very standardized...
177 #if _MSC_VER || __BORLANDC__
184 // for comparisons to minimum acceptable light
185 // compensate for 4096 offset
186 maxdist = radius * radius + 4096;
188 // clamp radius to avoid exceeding 32768 entry division table
189 if (maxdist > 4194304)
192 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
195 if (node->contents < 0)
197 ndist = PlaneDiff(origin, node->plane);
200 node = node->children[0];
205 node = node->children[1];
209 dist2 = ndist * ndist;
213 maxdist3 = maxdist - dist2;
215 impact[0] = origin[0] - node->plane->normal[0] * ndist;
216 impact[1] = origin[1] - node->plane->normal[1] * ndist;
217 impact[2] = origin[2] - node->plane->normal[2] * ndist;
219 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
221 if (surf->stainsamples)
223 smax = (surf->extents[0] >> 4) + 1;
224 tmax = (surf->extents[1] >> 4) + 1;
226 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
227 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
229 s = bound(0, impacts, smax * 16) - impacts;
230 t = bound(0, impactt, tmax * 16) - impactt;
231 i = s * s + t * t + dist2;
235 // reduce calculations
236 for (s = 0, i = impacts; s < smax; s++, i -= 16)
237 sdtable[s] = i * i + dist2;
239 // convert to 8.8 blocklights format
240 bl = surf->stainsamples;
245 for (t = 0;t < tmax;t++, i -= 16)
248 // make sure some part of it is visible on this line
251 maxdist2 = maxdist - td;
252 for (s = 0;s < smax;s++)
254 if (sdtable[s] < maxdist2)
256 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
259 ratio = rand() & 255;
260 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
264 a = bound(0, a, 256);
265 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
266 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
267 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
268 bl[0] = (byte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
269 bl[1] = (byte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
270 bl[2] = (byte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
281 // force lightmap upload
283 surf->cached_dlight = true;
288 if (node->children[0]->contents >= 0)
290 if (node->children[1]->contents >= 0)
292 R_StainNode(node->children[0], model, origin, radius, icolor);
293 node = node->children[1];
298 node = node->children[0];
302 else if (node->children[1]->contents >= 0)
304 node = node->children[1];
309 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
312 entity_render_t *ent;
324 model = cl.worldmodel;
325 softwaretransformidentity();
326 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
328 // look for embedded bmodels
329 for (n = 1;n < MAX_EDICTS;n++)
331 ent = &cl_entities[n].render;
333 if (model && model->name[0] == '*')
335 Mod_CheckLoaded(model);
336 if (model->type == mod_brush)
338 softwaretransformforentity(ent);
339 softwareuntransform(origin, org);
340 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
350 Combine and scale multiple lightmaps into the 8.8 format in blocklights
353 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
355 int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
356 byte *lightmap, *out, *stain;
358 // update cached lighting info
359 surf->cached_dlight = 0;
360 surf->cached_lightscalebit = lightscalebit;
361 surf->cached_ambient = r_ambient.value;
362 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
363 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
364 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
365 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
367 smax = (surf->extents[0]>>4)+1;
368 tmax = (surf->extents[1]>>4)+1;
371 lightmap = surf->samples;
373 // set to full bright if no light data
374 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
377 for (i = 0;i < size;i++)
387 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
391 for (i = 0;i < size3;i++)
395 memset(&blocklights[0], 0, size*3*sizeof(int));
397 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
399 surf->cached_dlight = R_AddDynamicLights(surf);
400 if (surf->cached_dlight)
402 else if (dlightchanged)
403 return; // don't upload if only updating dlights and none mattered
406 // add all the lightmaps
408 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
409 for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
410 *bl++ += *lightmap++ * scale;
413 stain = surf->stainsamples;
415 for (bl = blocklights, i = 0;i < size3;i++)
417 bl[i] = (bl[i] * stain[i]) >> 8;
421 // deal with lightmap brightness scale
422 shift = 7 + lightscalebit;
423 if (currentrenderentity->model->lightmaprgba)
425 stride = (surf->lightmaptexturestride - smax) * 4;
426 for (i = 0;i < tmax;i++, out += stride)
428 for (j = 0;j < smax;j++)
430 l = *bl++ >> shift;*out++ = min(l, 255);
431 l = *bl++ >> shift;*out++ = min(l, 255);
432 l = *bl++ >> shift;*out++ = min(l, 255);
439 stride = (surf->lightmaptexturestride - smax) * 3;
440 for (i = 0;i < tmax;i++, out += stride)
442 for (j = 0;j < smax;j++)
444 l = *bl++ >> shift;*out++ = min(l, 255);
445 l = *bl++ >> shift;*out++ = min(l, 255);
446 l = *bl++ >> shift;*out++ = min(l, 255);
451 R_UpdateTexture(surf->lightmaptexture, templight);
458 Returns the proper texture for a given time and base texture
462 // note: this was manually inlined in R_PrepareSurfaces
463 static texture_t *R_TextureAnimation (texture_t *base)
465 if (currentrenderentity->frame && base->alternate_anims != NULL)
466 base = base->alternate_anims;
468 if (base->anim_total < 2)
471 return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
477 =============================================================
481 =============================================================
485 static float turbsin[256] =
487 #include "gl_warp_sin.h"
489 #define TURBSCALE (256.0 / (2 * M_PI))
491 #define MAX_SURFVERTS 1024
500 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
502 static int RSurfShader_Sky(int stage, msurface_t *s)
505 float number, length, dir[3], speedscale;
510 // LordHavoc: HalfLife maps have freaky skypolys...
511 if (currentrenderentity->model->ishlbsp)
520 skyrendernow = false;
523 // draw depth-only polys
524 memset(&m, 0, sizeof(m));
525 m.transparent = false;
526 m.blendfunc1 = GL_ZERO;
527 m.blendfunc2 = GL_ONE;
529 m.numtriangles = s->mesh.numtriangles;
530 m.numverts = s->mesh.numverts;
531 m.index = s->mesh.index;
536 if (softwaretransform_complexity)
538 m.vertex = &svert[0].v[0];
539 m.vertexstep = sizeof(surfvert_t);
540 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
541 softwaretransform(v->v, sv->v);
545 m.vertex = &s->mesh.vertex[0].v[0];
546 m.vertexstep = sizeof(surfvertex_t);
550 else if (skyrenderglquake)
552 memset(&m, 0, sizeof(m));
553 m.transparent = false;
554 m.blendfunc1 = GL_ONE;
555 m.blendfunc2 = GL_ZERO;
556 m.numtriangles = s->mesh.numtriangles;
557 m.numverts = s->mesh.numverts;
558 m.index = s->mesh.index;
559 m.vertex = &svert[0].v[0];
560 m.vertexstep = sizeof(surfvert_t);
565 if (r_mergesky.integer)
566 m.tex[0] = R_GetTexture(mergeskytexture);
568 m.tex[0] = R_GetTexture(solidskytexture);
569 m.texcoords[0] = &svert[0].st[0];
570 m.texcoordstep[0] = sizeof(surfvert_t);
571 speedscale = cl.time * (8.0/128.0);
572 speedscale -= (int)speedscale;
573 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
575 softwaretransform(v->v, sv->v);
576 VectorSubtract (sv->v, r_origin, dir);
577 // flatten the sphere
580 number = DotProduct(dir, dir);
582 length = 3.0f / sqrt(number);
584 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
585 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
588 sv->st[0] = speedscale + dir[0] * length;
589 sv->st[1] = speedscale + dir[1] * length;
596 memset(&m, 0, sizeof(m));
597 m.transparent = false;
598 m.blendfunc1 = GL_ONE;
599 m.blendfunc2 = GL_ZERO;
600 m.numtriangles = s->mesh.numtriangles;
601 m.numverts = s->mesh.numverts;
602 m.index = s->mesh.index;
607 if (softwaretransform_complexity)
609 m.vertex = &svert[0].v[0];
610 m.vertexstep = sizeof(surfvert_t);
611 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
612 softwaretransform(v->v, sv->v);
616 m.vertex = &s->mesh.vertex[0].v[0];
617 m.vertexstep = sizeof(surfvertex_t);
625 if (skyrenderglquake && !r_mergesky.integer)
627 memset(&m, 0, sizeof(m));
628 m.transparent = false;
629 m.blendfunc1 = GL_SRC_ALPHA;
630 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
631 m.numtriangles = s->mesh.numtriangles;
632 m.numverts = s->mesh.numverts;
633 m.index = s->mesh.index;
634 m.vertex = &svert[0].v[0];
635 m.vertexstep = sizeof(surfvert_t);
640 m.tex[0] = R_GetTexture(alphaskytexture);
641 m.texcoords[0] = &svert[0].st[0];
642 m.texcoordstep[0] = sizeof(surfvert_t);
643 speedscale = cl.time * (16.0/128.0);
644 speedscale -= (int)speedscale;
645 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
647 softwaretransform(v->v, sv->v);
648 VectorSubtract (sv->v, r_origin, dir);
649 // flatten the sphere
652 number = DotProduct(dir, dir);
654 length = 3.0f / sqrt(number);
656 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
657 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
660 sv->st[0] = speedscale + dir[0] * length;
661 sv->st[1] = speedscale + dir[1] * length;
672 static int RSurf_Light(int *dlightbits, int numverts)
675 int i, l, lit = false;
679 for (l = 0;l < r_numdlights;l++)
681 if (dlightbits[l >> 5] & (1 << (l & 31)))
684 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
685 VectorCopy(rd->origin, lightorigin);
686 for (i = 0, sv = svert;i < numverts;i++, sv++)
688 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
689 if (f < rd->cullradius2)
691 f = (1.0f / f) - rd->lightsubtract;
692 sv->c[0] += rd->light[0] * f;
693 sv->c[1] += rd->light[1] * f;
694 sv->c[2] += rd->light[2] * f;
703 static void RSurfShader_Water_Pass_Base(msurface_t *s)
706 float diff[3], alpha, ifog;
710 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
712 memset(&m, 0, sizeof(m));
713 if (alpha != 1 || s->currenttexture->fogtexture != NULL)
715 m.transparent = true;
716 m.blendfunc1 = GL_SRC_ALPHA;
717 m.blendfunc2 = GL_ONE; //_MINUS_SRC_ALPHA;
721 m.transparent = false;
722 m.blendfunc1 = GL_ONE;
723 m.blendfunc2 = GL_ZERO;
725 m.numtriangles = s->mesh.numtriangles;
726 m.numverts = s->mesh.numverts;
727 m.index = s->mesh.index;
728 m.vertex = &svert[0].v[0];
729 m.vertexstep = sizeof(surfvert_t);
730 m.color = &svert[0].c[0];
731 m.colorstep = sizeof(surfvert_t);
732 m.tex[0] = R_GetTexture(s->currenttexture->texture);
733 m.texcoords[0] = &svert[0].st[0];
734 m.texcoordstep[0] = sizeof(surfvert_t);
735 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
737 softwaretransform(v->v, sv->v);
738 if (r_waterripple.value)
739 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];
740 if (s->flags & SURF_DRAWFULLBRIGHT)
754 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
755 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
757 if (s->dlightframe == r_framecount && !(s->flags & SURF_DRAWFULLBRIGHT))
758 RSurf_Light(s->dlightbits, m.numverts);
759 if (fogenabled/* && m.blendfunc2 == GL_ONE_MINUS_SRC_ALPHA*/)
761 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
763 VectorSubtract(sv->v, r_origin, diff);
764 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
773 static void RSurfShader_Water_Pass_Glow(msurface_t *s)
776 float diff[3], alpha, ifog;
780 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
782 memset(&m, 0, sizeof(m));
783 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
784 m.blendfunc1 = GL_SRC_ALPHA;
785 m.blendfunc2 = GL_ONE;
786 m.numtriangles = s->mesh.numtriangles;
787 m.numverts = s->mesh.numverts;
788 m.index = s->mesh.index;
789 m.vertex = &svert[0].v[0];
790 m.vertexstep = sizeof(surfvert_t);
795 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
796 m.texcoords[0] = &svert[0].st[0];
797 m.texcoordstep[0] = sizeof(surfvert_t);
800 m.color = &svert[0].c[0];
801 m.colorstep = sizeof(surfvert_t);
802 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
804 softwaretransform(v->v, sv->v);
805 if (r_waterripple.value)
806 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];
807 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
808 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
809 VectorSubtract(sv->v, r_origin, diff);
810 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
811 sv->c[0] = m.cr * ifog;
812 sv->c[1] = m.cg * ifog;
813 sv->c[2] = m.cb * ifog;
819 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
821 softwaretransform(v->v, sv->v);
822 if (r_waterripple.value)
823 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];
824 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
825 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
831 static void RSurfShader_Water_Pass_Fog(msurface_t *s)
839 alpha = currentrenderentity->alpha * (s->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
841 memset(&m, 0, sizeof(m));
842 m.transparent = alpha != 1 || s->currenttexture->fogtexture != NULL;
843 m.blendfunc1 = GL_SRC_ALPHA;
844 m.blendfunc2 = GL_ONE;
845 m.numtriangles = s->mesh.numtriangles;
846 m.numverts = s->mesh.numverts;
847 m.index = s->mesh.index;
848 m.vertex = &svert[0].v[0];
849 m.vertexstep = sizeof(surfvert_t);
850 m.color = &svert[0].c[0];
851 m.colorstep = sizeof(surfvert_t);
852 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
853 m.texcoords[0] = &svert[0].st[0];
854 m.texcoordstep[0] = sizeof(surfvert_t);
856 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
858 softwaretransform(v->v, sv->v);
859 if (r_waterripple.value)
860 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];
863 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
864 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
866 VectorSubtract(sv->v, r_origin, diff);
867 sv->c[0] = fogcolor[0];
868 sv->c[1] = fogcolor[1];
869 sv->c[2] = fogcolor[2];
870 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
875 static int RSurfShader_Water(int stage, msurface_t *s)
880 RSurfShader_Water_Pass_Base(s);
883 if (s->currenttexture->glowtexture)
884 RSurfShader_Water_Pass_Glow(s);
887 if (fogenabled && (s->flags & SURF_DRAWNOALPHA))
889 RSurfShader_Water_Pass_Fog(s);
899 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *s)
907 memset(&m, 0, sizeof(m));
908 if (currentrenderentity->effects & EF_ADDITIVE)
910 m.transparent = true;
911 m.blendfunc1 = GL_SRC_ALPHA;
912 m.blendfunc2 = GL_ONE;
914 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
916 m.transparent = true;
917 m.blendfunc1 = GL_SRC_ALPHA;
918 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
922 m.transparent = false;
923 m.blendfunc1 = GL_ONE;
924 m.blendfunc2 = GL_ZERO;
926 m.numtriangles = s->mesh.numtriangles;
927 m.numverts = s->mesh.numverts;
928 m.index = s->mesh.index;
932 if (gl_combine.integer)
936 m.ca = currentrenderentity->alpha;
937 m.tex[0] = R_GetTexture(s->currenttexture->texture);
938 m.tex[1] = R_GetTexture(s->lightmaptexture);
939 m.texcoords[0] = &s->mesh.vertex->st[0];
940 m.texcoords[1] = &s->mesh.vertex->uv[0];
941 m.texcoordstep[0] = sizeof(surfvertex_t);
942 m.texcoordstep[1] = sizeof(surfvertex_t);
945 m.color = &svert[0].c[0];
946 m.colorstep = sizeof(surfvert_t);
947 if (softwaretransform_complexity)
949 m.vertex = &svert[0].v[0];
950 m.vertexstep = sizeof(surfvert_t);
951 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
953 softwaretransform(v->v, sv->v);
954 VectorSubtract(sv->v, r_origin, diff);
955 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
956 sv->c[0] = m.cr * ifog;
957 sv->c[1] = m.cg * ifog;
958 sv->c[2] = m.cb * ifog;
964 m.vertex = &s->mesh.vertex->v[0];
965 m.vertexstep = sizeof(surfvertex_t);
966 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
968 VectorSubtract(v->v, r_origin, diff);
969 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
970 sv->c[0] = m.cr * ifog;
971 sv->c[1] = m.cg * ifog;
972 sv->c[2] = m.cb * ifog;
979 if (softwaretransform_complexity)
981 m.vertex = &svert[0].v[0];
982 m.vertexstep = sizeof(surfvert_t);
983 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
984 softwaretransform(v->v, sv->v);
988 m.vertex = &s->mesh.vertex->v[0];
989 m.vertexstep = sizeof(surfvertex_t);
995 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *s)
1002 memset(&m, 0, sizeof(m));
1003 m.transparent = false;
1004 m.blendfunc1 = GL_ONE;
1005 m.blendfunc2 = GL_ZERO;
1006 m.numtriangles = s->mesh.numtriangles;
1007 m.numverts = s->mesh.numverts;
1008 m.index = s->mesh.index;
1022 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1023 m.texcoords[0] = &s->mesh.vertex->st[0];
1024 m.texcoordstep[0] = sizeof(surfvertex_t);
1025 if (softwaretransform_complexity)
1027 m.vertex = &svert[0].v[0];
1028 m.vertexstep = sizeof(surfvert_t);
1029 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1030 softwaretransform(v->v, sv->v);
1034 m.vertex = &s->mesh.vertex->v[0];
1035 m.vertexstep = sizeof(surfvertex_t);
1040 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *s)
1043 float diff[3], ifog;
1048 memset(&m, 0, sizeof(m));
1049 m.transparent = false;
1050 m.blendfunc1 = GL_ZERO;
1051 m.blendfunc2 = GL_SRC_COLOR;
1052 m.numtriangles = s->mesh.numtriangles;
1053 m.numverts = s->mesh.numverts;
1054 m.index = s->mesh.index;
1061 m.tex[0] = R_GetTexture(s->lightmaptexture);
1062 m.texcoords[0] = &s->mesh.vertex->uv[0];
1063 m.texcoordstep[0] = sizeof(surfvertex_t);
1066 m.color = &svert[0].c[0];
1067 m.colorstep = sizeof(surfvert_t);
1068 if (softwaretransform_complexity)
1070 m.vertex = &svert[0].v[0];
1071 m.vertexstep = sizeof(surfvert_t);
1072 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1074 softwaretransform(v->v, sv->v);
1075 VectorSubtract(sv->v, r_origin, diff);
1076 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1077 sv->c[0] = m.cr * ifog;
1078 sv->c[1] = m.cg * ifog;
1079 sv->c[2] = m.cb * ifog;
1085 m.vertex = &s->mesh.vertex->v[0];
1086 m.vertexstep = sizeof(surfvertex_t);
1087 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1089 VectorSubtract(v->v, r_origin, diff);
1090 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1091 sv->c[0] = m.cr * ifog;
1092 sv->c[1] = m.cg * ifog;
1093 sv->c[2] = m.cb * ifog;
1100 if (softwaretransform_complexity)
1102 m.vertex = &svert[0].v[0];
1103 m.vertexstep = sizeof(surfvert_t);
1104 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1105 softwaretransform(v->v, sv->v);
1109 m.vertex = &s->mesh.vertex->v[0];
1110 m.vertexstep = sizeof(surfvertex_t);
1116 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *s)
1119 float c[3], base[3], scale, diff[3], ifog;
1125 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3;
1127 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1129 memset(&m, 0, sizeof(m));
1130 if (currentrenderentity->effects & EF_ADDITIVE)
1132 m.transparent = true;
1133 m.blendfunc1 = GL_SRC_ALPHA;
1134 m.blendfunc2 = GL_ONE;
1136 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1138 m.transparent = true;
1139 m.blendfunc1 = GL_SRC_ALPHA;
1140 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1144 m.transparent = false;
1145 m.blendfunc1 = GL_ONE;
1146 m.blendfunc2 = GL_ZERO;
1148 m.numtriangles = s->mesh.numtriangles;
1149 m.numverts = s->mesh.numverts;
1150 m.index = s->mesh.index;
1151 m.vertex = &svert[0].v[0];
1152 m.vertexstep = sizeof(surfvert_t);
1153 m.color = &svert[0].c[0];
1154 m.colorstep = sizeof(surfvert_t);
1155 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1156 m.texcoords[0] = &s->mesh.vertex->st[0];
1157 m.texcoordstep[0] = sizeof(surfvertex_t);
1158 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1160 softwaretransform(v->v, sv->v);
1161 VectorCopy(base, c);
1162 if (s->styles[0] != 255)
1164 lm = s->samples + v->lightmapoffset;
1165 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 32768.0f);
1166 VectorMA(c, scale, lm, c);
1167 if (s->styles[1] != 255)
1170 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 32768.0f);
1171 VectorMA(c, scale, lm, c);
1172 if (s->styles[2] != 255)
1175 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 32768.0f);
1176 VectorMA(c, scale, lm, c);
1177 if (s->styles[3] != 255)
1180 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 32768.0f);
1181 VectorMA(c, scale, lm, c);
1189 sv->c[3] = currentrenderentity->alpha;
1191 if (s->dlightframe == r_framecount)
1192 RSurf_Light(s->dlightbits, m.numverts);
1195 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1197 VectorSubtract(sv->v, r_origin, diff);
1198 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1207 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *s)
1210 float diff[3], ifog;
1215 memset(&m, 0, sizeof(m));
1216 if (currentrenderentity->effects & EF_ADDITIVE)
1218 m.transparent = true;
1219 m.blendfunc1 = GL_SRC_ALPHA;
1220 m.blendfunc2 = GL_ONE;
1222 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1224 m.transparent = true;
1225 m.blendfunc1 = GL_SRC_ALPHA;
1226 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1230 m.transparent = false;
1231 m.blendfunc1 = GL_ONE;
1232 m.blendfunc2 = GL_ZERO;
1234 m.numtriangles = s->mesh.numtriangles;
1235 m.numverts = s->mesh.numverts;
1236 m.index = s->mesh.index;
1237 m.vertex = &svert[0].v[0];
1238 m.vertexstep = sizeof(surfvert_t);
1239 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1240 m.texcoords[0] = &s->mesh.vertex->st[0];
1241 m.texcoordstep[0] = sizeof(surfvertex_t);
1244 m.color = &svert[0].c[0];
1245 m.colorstep = sizeof(surfvert_t);
1246 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1248 softwaretransform(v->v, sv->v);
1249 VectorSubtract(sv->v, r_origin, diff);
1250 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1254 sv->c[3] = currentrenderentity->alpha;
1259 m.cr = m.cg = m.cb = 1;
1260 m.ca = currentrenderentity->alpha;
1261 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1262 softwaretransform(v->v, sv->v);
1267 static void RSurfShader_Wall_Pass_Light(msurface_t *s)
1270 float diff[3], ifog;
1275 memset(&m, 0, sizeof(m));
1276 if (currentrenderentity->effects & EF_ADDITIVE)
1277 m.transparent = true;
1278 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1279 m.transparent = true;
1281 m.transparent = false;
1282 m.blendfunc1 = GL_SRC_ALPHA;
1283 m.blendfunc2 = GL_ONE;
1284 m.numtriangles = s->mesh.numtriangles;
1285 m.numverts = s->mesh.numverts;
1286 m.index = s->mesh.index;
1287 m.vertex = &svert[0].v[0];
1288 m.vertexstep = sizeof(surfvert_t);
1289 m.color = &svert[0].c[0];
1290 m.colorstep = sizeof(surfvert_t);
1291 m.tex[0] = R_GetTexture(s->currenttexture->texture);
1292 m.texcoords[0] = &s->mesh.vertex->st[0];
1293 m.texcoordstep[0] = sizeof(surfvertex_t);
1294 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1296 softwaretransform(v->v, sv->v);
1300 sv->c[3] = currentrenderentity->alpha;
1302 if (RSurf_Light(s->dlightbits, m.numverts))
1306 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1308 VectorSubtract(sv->v, r_origin, diff);
1309 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1319 static void RSurfShader_Wall_Pass_Glow(msurface_t *s)
1322 float diff[3], ifog;
1327 memset(&m, 0, sizeof(m));
1328 if (currentrenderentity->effects & EF_ADDITIVE)
1329 m.transparent = true;
1330 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1331 m.transparent = true;
1333 m.transparent = false;
1334 m.blendfunc1 = GL_SRC_ALPHA;
1335 m.blendfunc2 = GL_ONE;
1336 m.numtriangles = s->mesh.numtriangles;
1337 m.numverts = s->mesh.numverts;
1338 m.index = s->mesh.index;
1342 m.ca = currentrenderentity->alpha;
1343 m.tex[0] = R_GetTexture(s->currenttexture->glowtexture);
1344 m.texcoords[0] = &s->mesh.vertex->st[0];
1345 m.texcoordstep[0] = sizeof(surfvertex_t);
1348 m.color = &svert[0].c[0];
1349 m.colorstep = sizeof(surfvert_t);
1350 if (softwaretransform_complexity)
1352 m.vertex = &svert[0].v[0];
1353 m.vertexstep = sizeof(surfvert_t);
1354 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1356 softwaretransform(v->v, sv->v);
1357 VectorSubtract(sv->v, r_origin, diff);
1358 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1359 sv->c[0] = m.cr * ifog;
1360 sv->c[1] = m.cg * ifog;
1361 sv->c[2] = m.cb * ifog;
1367 m.vertex = &s->mesh.vertex->v[0];
1368 m.vertexstep = sizeof(surfvertex_t);
1369 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1371 VectorSubtract(v->v, r_origin, diff);
1372 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1373 sv->c[0] = m.cr * ifog;
1374 sv->c[1] = m.cg * ifog;
1375 sv->c[2] = m.cb * ifog;
1382 if (softwaretransform_complexity)
1384 m.vertex = &svert[0].v[0];
1385 m.vertexstep = sizeof(surfvert_t);
1386 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1387 softwaretransform(v->v, sv->v);
1391 m.vertex = &s->mesh.vertex->v[0];
1392 m.vertexstep = sizeof(surfvertex_t);
1398 static void RSurfShader_Wall_Pass_Fog(msurface_t *s)
1406 memset(&m, 0, sizeof(m));
1407 if (currentrenderentity->effects & EF_ADDITIVE)
1408 m.transparent = true;
1409 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1410 m.transparent = true;
1412 m.transparent = false;
1413 m.blendfunc1 = GL_SRC_ALPHA;
1414 m.blendfunc2 = GL_ONE;
1415 m.numtriangles = s->mesh.numtriangles;
1416 m.numverts = s->mesh.numverts;
1417 m.index = s->mesh.index;
1418 m.color = &svert[0].c[0];
1419 m.colorstep = sizeof(surfvert_t);
1420 m.tex[0] = R_GetTexture(s->currenttexture->fogtexture);
1421 m.texcoords[0] = &s->mesh.vertex->st[0];
1422 m.texcoordstep[0] = sizeof(surfvertex_t);
1423 if (softwaretransform_complexity)
1425 m.vertex = &svert[0].v[0];
1426 m.vertexstep = sizeof(surfvert_t);
1427 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1429 softwaretransform(v->v, sv->v);
1430 VectorSubtract(sv->v, r_origin, diff);
1431 sv->c[0] = fogcolor[0];
1432 sv->c[1] = fogcolor[1];
1433 sv->c[2] = fogcolor[2];
1434 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1439 m.vertex = &s->mesh.vertex->v[0];
1440 m.vertexstep = sizeof(surfvertex_t);
1441 for (i = 0, sv = svert, v = s->mesh.vertex;i < m.numverts;i++, sv++, v++)
1443 VectorSubtract(v->v, r_origin, diff);
1444 sv->c[0] = fogcolor[0];
1445 sv->c[1] = fogcolor[1];
1446 sv->c[2] = fogcolor[2];
1447 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1453 static int RSurfShader_Wall_Fullbright(int stage, msurface_t *s)
1458 RSurfShader_Wall_Pass_BaseFullbright(s);
1461 if (s->currenttexture->glowtexture)
1462 RSurfShader_Wall_Pass_Glow(s);
1469 static int RSurfShader_Wall_Vertex(int stage, msurface_t *s)
1474 RSurfShader_Wall_Pass_BaseVertex(s);
1477 if (s->currenttexture->glowtexture)
1478 RSurfShader_Wall_Pass_Glow(s);
1485 static int RSurfShader_Wall_Lightmap(int stage, msurface_t *s)
1487 if (r_vertexsurfaces.integer)
1492 RSurfShader_Wall_Pass_BaseVertex(s);
1495 if (s->currenttexture->glowtexture)
1496 RSurfShader_Wall_Pass_Glow(s);
1502 else if (r_multitexture.integer)
1504 if (r_dlightmap.integer)
1509 RSurfShader_Wall_Pass_BaseMTex(s);
1512 if (s->currenttexture->glowtexture)
1513 RSurfShader_Wall_Pass_Glow(s);
1524 RSurfShader_Wall_Pass_BaseMTex(s);
1527 if (s->dlightframe == r_framecount)
1528 RSurfShader_Wall_Pass_Light(s);
1531 if (s->currenttexture->glowtexture)
1532 RSurfShader_Wall_Pass_Glow(s);
1539 else if (s->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1544 RSurfShader_Wall_Pass_BaseVertex(s);
1547 if (s->currenttexture->glowtexture)
1548 RSurfShader_Wall_Pass_Glow(s);
1556 if (r_dlightmap.integer)
1561 RSurfShader_Wall_Pass_BaseTexture(s);
1564 RSurfShader_Wall_Pass_BaseLightmap(s);
1567 if (s->currenttexture->glowtexture)
1568 RSurfShader_Wall_Pass_Glow(s);
1579 RSurfShader_Wall_Pass_BaseTexture(s);
1582 RSurfShader_Wall_Pass_BaseLightmap(s);
1585 if (s->dlightframe == r_framecount)
1586 RSurfShader_Wall_Pass_Light(s);
1589 if (s->currenttexture->glowtexture)
1590 RSurfShader_Wall_Pass_Glow(s);
1599 static int RSurfShader_Wall_Fog(int stage, msurface_t *s)
1601 if (stage == 0 && fogenabled)
1603 RSurfShader_Wall_Pass_Fog(s);
1611 =============================================================
1615 =============================================================
1618 static void RSurf_Callback(void *data, void *junk)
1620 ((msurface_t *)data)->visframe = r_framecount;
1623 static void R_SolidWorldNode (void)
1625 if (r_viewleaf->contents != CONTENTS_SOLID)
1628 mportal_t *p, *pstack[8192];
1629 msurface_t *surf, **mark, **endmark;
1632 // LordHavoc: portal-passage worldnode; follows portals leading
1633 // outward from viewleaf, if a portal leads offscreen it is not
1634 // followed, in indoor maps this can often cull a great deal of
1635 // geometry away when pvs data is not present (useful with pvs as well)
1638 leaf->worldnodeframe = r_framecount;
1643 leaf->visframe = r_framecount;
1645 if (leaf->nummarksurfaces)
1647 mark = leaf->firstmarksurface;
1648 endmark = mark + leaf->nummarksurfaces;
1654 // make sure surfaces are only processed once
1655 if (surf->worldnodeframe == r_framecount)
1657 surf->worldnodeframe = r_framecount;
1658 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1660 if (surf->flags & SURF_PLANEBACK)
1662 VectorNegate(surf->plane->normal, plane.normal);
1663 plane.dist = -surf->plane->dist;
1664 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1669 if (!(surf->flags & SURF_PLANEBACK))
1670 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);
1673 while (mark < endmark);
1680 // make sure surfaces are only processed once
1681 if (surf->worldnodeframe == r_framecount)
1683 surf->worldnodeframe = r_framecount;
1684 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1686 if (surf->flags & SURF_PLANEBACK)
1687 surf->visframe = r_framecount;
1691 if (!(surf->flags & SURF_PLANEBACK))
1692 surf->visframe = r_framecount;
1695 while (mark < endmark);
1699 // follow portals into other leafs
1701 for (;p;p = p->next)
1703 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1706 if (leaf->worldnodeframe != r_framecount)
1708 leaf->worldnodeframe = r_framecount;
1709 if (leaf->contents != CONTENTS_SOLID)
1711 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1713 p->visframe = r_framecount;
1714 pstack[portalstack++] = p;
1718 p = pstack[--portalstack];
1730 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1731 int nodestackpos = 0;
1732 // LordHavoc: recursive descending worldnode; if portals are not
1733 // available, this is a good last resort, can cull large amounts of
1734 // geometry, but is more time consuming than portal-passage and renders
1735 // things behind walls
1738 if (R_NotCulledBox(node->mins, node->maxs))
1740 if (node->numsurfaces)
1744 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1746 if (PlaneDiff (r_origin, node->plane) < 0)
1748 for (;surf < surfend;surf++)
1750 if (surf->flags & SURF_PLANEBACK)
1752 VectorNegate(surf->plane->normal, plane.normal);
1753 plane.dist = -surf->plane->dist;
1754 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1760 for (;surf < surfend;surf++)
1762 if (!(surf->flags & SURF_PLANEBACK))
1763 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);
1769 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1770 if (PlaneDiff (r_origin, node->plane) < 0)
1772 for (;surf < surfend;surf++)
1774 if (surf->flags & SURF_PLANEBACK)
1775 surf->visframe = r_framecount;
1780 for (;surf < surfend;surf++)
1782 if (!(surf->flags & SURF_PLANEBACK))
1783 surf->visframe = r_framecount;
1789 // recurse down the children
1790 if (node->children[0]->contents >= 0)
1792 if (node->children[1]->contents >= 0)
1794 if (nodestackpos < 8192)
1795 nodestack[nodestackpos++] = node->children[1];
1796 node = node->children[0];
1800 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1801 node = node->children[0];
1806 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1807 if (node->children[1]->contents >= 0)
1809 node = node->children[1];
1812 else if (nodestackpos > 0)
1814 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1815 node = nodestack[--nodestackpos];
1820 else if (nodestackpos > 0)
1822 node = nodestack[--nodestackpos];
1828 static int r_portalframecount = 0;
1830 static void R_PVSWorldNode()
1833 mportal_t *p, *pstack[8192];
1834 msurface_t *surf, **mark, **endmark;
1839 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1842 leaf->worldnodeframe = r_framecount;
1847 leaf->visframe = r_framecount;
1849 if (leaf->nummarksurfaces)
1851 mark = leaf->firstmarksurface;
1852 endmark = mark + leaf->nummarksurfaces;
1858 // make sure surfaces are only processed once
1859 if (surf->worldnodeframe == r_framecount)
1861 surf->worldnodeframe = r_framecount;
1862 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1864 if (surf->flags & SURF_PLANEBACK)
1866 VectorNegate(surf->plane->normal, plane.normal);
1867 plane.dist = -surf->plane->dist;
1868 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1873 if (!(surf->flags & SURF_PLANEBACK))
1874 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);
1877 while (mark < endmark);
1884 // make sure surfaces are only processed once
1885 if (surf->worldnodeframe == r_framecount)
1887 surf->worldnodeframe = r_framecount;
1888 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1890 if (surf->flags & SURF_PLANEBACK)
1891 surf->visframe = r_framecount;
1895 if (!(surf->flags & SURF_PLANEBACK))
1896 surf->visframe = r_framecount;
1899 while (mark < endmark);
1903 // follow portals into other leafs
1904 for (p = leaf->portals;p;p = p->next)
1906 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1909 if (leaf->worldnodeframe != r_framecount)
1911 leaf->worldnodeframe = r_framecount;
1912 if (leaf->contents != CONTENTS_SOLID)
1914 i = (leaf - cl.worldmodel->leafs) - 1;
1915 if (worldvis[i>>3] & (1<<(i&7)))
1917 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1919 pstack[portalstack++] = p;
1923 p = pstack[--portalstack];
1935 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1936 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1937 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
1938 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1939 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1941 int Cshader_count = 5;
1942 Cshader_t *Cshaders[5] =
1944 &Cshader_wall_vertex,
1945 &Cshader_wall_lightmap,
1946 &Cshader_wall_fullbright,
1951 void R_PrepareSurfaces(void)
1958 for (i = 0;i < Cshader_count;i++)
1959 Cshaders[i]->chain = NULL;
1961 model = currentrenderentity->model;
1963 for (i = 0;i < model->nummodelsurfaces;i++)
1965 surf = model->modelsortedsurfaces[i];
1966 if (surf->visframe == r_framecount)
1968 if (surf->insertframe != r_framecount)
1970 surf->insertframe = r_framecount;
1972 // manually inlined R_TextureAnimation
1973 //t = R_TextureAnimation(surf->texinfo->texture);
1974 t = surf->texinfo->texture;
1975 if (t->alternate_anims != NULL && currentrenderentity->frame)
1976 t = t->alternate_anims;
1977 if (t->anim_total >= 2)
1978 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1979 surf->currenttexture = t;
1982 surf->chain = surf->shader->chain;
1983 surf->shader->chain = surf;
1988 void R_DrawSurfaces (int type)
1994 for (i = 0;i < Cshader_count;i++)
1996 shader = Cshaders[i];
1997 if (shader->chain && shader->shaderfunc[type])
1998 for (stage = 0;stage < 1000;stage++)
1999 for (surf = shader->chain;surf;surf = surf->chain)
2000 if (shader->shaderfunc[type](stage, surf))
2006 void R_DrawSurfacesAll (void)
2008 R_DrawSurfaces(SHADERSTAGE_SKY);
2009 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2010 R_DrawSurfaces(SHADERSTAGE_FOG);
2013 static float portalpointbuffer[256][3];
2015 void R_DrawPortals(void)
2018 // mleaf_t *leaf, *endleaf;
2019 mportal_t *portal, *endportal;
2020 mvertex_t *point/*, *endpoint*/;
2022 drawportals = r_drawportals.integer;
2023 if (drawportals < 1)
2026 leaf = cl.worldmodel->leafs;
2027 endleaf = leaf + cl.worldmodel->numleafs;
2028 for (;leaf < endleaf;leaf++)
2030 if (leaf->visframe == r_framecount && leaf->portals)
2032 i = leaf - cl.worldmodel->leafs;
2033 r = (i & 0x0007) << 5;
2034 g = (i & 0x0038) << 2;
2035 b = (i & 0x01C0) >> 1;
2036 portal = leaf->portals;
2039 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2040 point = portal->points + portal->numpoints - 1;
2041 endpoint = portal->points;
2042 for (;point >= endpoint;point--)
2043 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2045 portal = portal->next;
2050 memset(&m, 0, sizeof(m));
2051 m.transparent = true;
2052 m.blendfunc1 = GL_SRC_ALPHA;
2053 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2054 m.vertex = &portalpointbuffer[0][0];
2055 m.vertexstep = sizeof(float[3]);
2057 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2059 if (portal->visframe == r_portalframecount)
2061 if (portal->numpoints <= 256)
2063 i = portal - cl.worldmodel->portals;
2064 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2065 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2066 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2067 point = portal->points;
2068 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2070 for (i = portal->numpoints - 1;i >= 0;i--)
2071 VectorCopy(point[i].position, portalpointbuffer[i]);
2075 for (i = 0;i < portal->numpoints;i++)
2076 VectorCopy(point[i].position, portalpointbuffer[i]);
2078 R_Mesh_DrawPolygon(&m, portal->numpoints);
2084 void R_SetupForBModelRendering(void)
2091 // because bmodels can be reused, we have to decide which things to render
2092 // from scratch every time
2094 model = currentrenderentity->model;
2096 softwaretransformforentity (currentrenderentity);
2097 softwareuntransform(r_origin, modelorg);
2099 for (i = 0;i < model->nummodelsurfaces;i++)
2101 s = model->modelsortedsurfaces[i];
2102 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
2103 s->visframe = r_framecount;
2106 s->worldnodeframe = -1;
2108 s->dlightframe = -1;
2109 s->insertframe = -1;
2113 void R_SetupForWorldRendering(void)
2115 // there is only one instance of the world, but it can be rendered in
2118 currentrenderentity = &cl_entities[0].render;
2119 softwaretransformidentity();
2122 static void R_SurfMarkLights (void)
2127 if (r_dynamic.integer)
2130 if (!r_vertexsurfaces.integer)
2132 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2134 s = currentrenderentity->model->modelsortedsurfaces[i];
2135 if (s->visframe == r_framecount && s->lightmaptexture != NULL)
2137 if (s->cached_dlight
2138 || s->cached_ambient != r_ambient.value
2139 || s->cached_lightscalebit != lightscalebit)
2140 R_BuildLightMap(s, false); // base lighting changed
2141 else if (r_dynamic.integer)
2143 if (s->styles[0] != 255 && (d_lightstylevalue[s->styles[0]] != s->cached_light[0]
2144 || (s->styles[1] != 255 && (d_lightstylevalue[s->styles[1]] != s->cached_light[1]
2145 || (s->styles[2] != 255 && (d_lightstylevalue[s->styles[2]] != s->cached_light[2]
2146 || (s->styles[3] != 255 && (d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))))))
2147 //if (s->cached_light[0] != d_lightstylevalue[s->styles[0]]
2148 // || s->cached_light[1] != d_lightstylevalue[s->styles[1]]
2149 // || s->cached_light[2] != d_lightstylevalue[s->styles[2]]
2150 // || s->cached_light[3] != d_lightstylevalue[s->styles[3]])
2151 R_BuildLightMap(s, false); // base lighting changed
2152 else if (s->dlightframe == r_framecount && r_dlightmap.integer)
2153 R_BuildLightMap(s, true); // only dlights
2160 void R_MarkWorldLights(void)
2162 R_SetupForWorldRendering();
2171 void R_DrawWorld (void)
2173 R_SetupForWorldRendering();
2175 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2176 R_SolidWorldNode ();
2186 void R_DrawBrushModelSky (void)
2188 R_SetupForBModelRendering();
2190 R_PrepareSurfaces();
2191 R_DrawSurfaces(SHADERSTAGE_SKY);
2194 void R_DrawBrushModelNormal (void)
2198 // have to flush queue because of possible lightmap reuse
2201 R_SetupForBModelRendering();
2205 R_PrepareSurfaces();
2207 if (!skyrendermasked)
2208 R_DrawSurfaces(SHADERSTAGE_SKY);
2209 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2210 R_DrawSurfaces(SHADERSTAGE_FOG);