2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 #define MAX_LIGHTMAP_SIZE 256
26 static signed int blocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
30 cvar_t r_ambient = {0, "r_ambient", "0"};
31 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
32 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
36 static void gl_surf_start(void)
40 static void gl_surf_shutdown(void)
44 static void gl_surf_newmap(void)
48 static int dlightdivtable[32768];
50 void GL_Surf_Init(void)
53 dlightdivtable[0] = 4194304;
54 for (i = 1;i < 32768;i++)
55 dlightdivtable[i] = 4194304 / (i << 7);
57 Cvar_RegisterVariable(&r_ambient);
58 Cvar_RegisterVariable(&r_vertexsurfaces);
59 Cvar_RegisterVariable(&r_dlightmap);
60 Cvar_RegisterVariable(&r_drawportals);
61 Cvar_RegisterVariable(&r_testvis);
63 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
66 static int R_AddDynamicLights (msurface_t *surf)
68 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
73 // LordHavoc: use 64bit integer... shame it's not very standardized...
74 #if _MSC_VER || __BORLANDC__
82 smax = (surf->extents[0] >> 4) + 1;
83 tmax = (surf->extents[1] >> 4) + 1;
85 for (lnum = 0; lnum < r_numdlights; lnum++)
87 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
88 continue; // not lit by this light
90 softwareuntransform(r_dlight[lnum].origin, local);
91 // VectorSubtract (r_dlight[lnum].origin, currentrenderentity->origin, local);
92 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
94 // for comparisons to minimum acceptable light
95 // compensate for LIGHTOFFSET
96 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
98 // already clamped, skip this
99 // clamp radius to avoid exceeding 32768 entry division table
100 //if (maxdist > 4194304)
101 // maxdist = 4194304;
104 dist2 += LIGHTOFFSET;
105 if (dist2 >= maxdist)
108 impact[0] = local[0] - surf->plane->normal[0] * dist;
109 impact[1] = local[1] - surf->plane->normal[1] * dist;
110 impact[2] = local[2] - surf->plane->normal[2] * dist;
112 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
113 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
115 s = bound(0, impacts, smax * 16) - impacts;
116 t = bound(0, impactt, tmax * 16) - impactt;
117 i = s * s + t * t + dist2;
121 // reduce calculations
122 for (s = 0, i = impacts; s < smax; s++, i -= 16)
123 sdtable[s] = i * i + dist2;
125 maxdist3 = maxdist - dist2;
127 // convert to 8.8 blocklights format
128 red = r_dlight[lnum].light[0];
129 green = r_dlight[lnum].light[1];
130 blue = r_dlight[lnum].light[2];
131 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
136 for (t = 0;t < tmax;t++, i -= 16)
139 // make sure some part of it is visible on this line
142 maxdist2 = maxdist - td;
143 for (s = 0;s < smax;s++)
145 if (sdtable[s] < maxdist2)
147 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
150 bl[0] += (red * k) >> 8;
151 bl[1] += (green * k) >> 8;
152 bl[2] += (blue * k) >> 8;
166 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
169 msurface_t *surf, *endsurf;
170 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
173 // LordHavoc: use 64bit integer... shame it's not very standardized...
174 #if _MSC_VER || __BORLANDC__
181 // for comparisons to minimum acceptable light
182 // compensate for 4096 offset
183 maxdist = radius * radius + 4096;
185 // clamp radius to avoid exceeding 32768 entry division table
186 if (maxdist > 4194304)
189 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
192 if (node->contents < 0)
194 ndist = PlaneDiff(origin, node->plane);
197 node = node->children[0];
202 node = node->children[1];
206 dist2 = ndist * ndist;
210 maxdist3 = maxdist - dist2;
212 impact[0] = origin[0] - node->plane->normal[0] * ndist;
213 impact[1] = origin[1] - node->plane->normal[1] * ndist;
214 impact[2] = origin[2] - node->plane->normal[2] * ndist;
216 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
218 if (surf->stainsamples)
220 smax = (surf->extents[0] >> 4) + 1;
221 tmax = (surf->extents[1] >> 4) + 1;
223 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
224 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
226 s = bound(0, impacts, smax * 16) - impacts;
227 t = bound(0, impactt, tmax * 16) - impactt;
228 i = s * s + t * t + dist2;
232 // reduce calculations
233 for (s = 0, i = impacts; s < smax; s++, i -= 16)
234 sdtable[s] = i * i + dist2;
236 // convert to 8.8 blocklights format
237 bl = surf->stainsamples;
242 for (t = 0;t < tmax;t++, i -= 16)
245 // make sure some part of it is visible on this line
248 maxdist2 = maxdist - td;
249 for (s = 0;s < smax;s++)
251 if (sdtable[s] < maxdist2)
253 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
256 ratio = rand() & 255;
257 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
261 a = bound(0, a, 256);
262 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
263 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
264 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
265 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
266 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
267 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
278 // force lightmap upload
280 surf->cached_dlight = true;
285 if (node->children[0]->contents >= 0)
287 if (node->children[1]->contents >= 0)
289 R_StainNode(node->children[0], model, origin, radius, icolor);
290 node = node->children[1];
295 node = node->children[0];
299 else if (node->children[1]->contents >= 0)
301 node = node->children[1];
306 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
309 entity_render_t *ent;
321 model = cl.worldmodel;
322 softwaretransformidentity();
323 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
325 // look for embedded bmodels
326 for (n = 1;n < MAX_EDICTS;n++)
328 ent = &cl_entities[n].render;
330 if (model && model->name[0] == '*')
332 Mod_CheckLoaded(model);
333 if (model->type == mod_brush)
335 softwaretransformforentity(ent);
336 softwareuntransform(origin, org);
337 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
347 Combine and scale multiple lightmaps into the 8.8 format in blocklights
350 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
352 int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
353 qbyte *lightmap, *out, *stain;
355 // update cached lighting info
356 surf->cached_dlight = 0;
357 surf->cached_lightscalebit = lightscalebit;
358 surf->cached_ambient = r_ambient.value;
359 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
360 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
361 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
362 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
364 smax = (surf->extents[0]>>4)+1;
365 tmax = (surf->extents[1]>>4)+1;
368 lightmap = surf->samples;
370 // set to full bright if no light data
371 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
374 for (i = 0;i < size;i++)
384 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
388 for (i = 0;i < size3;i++)
392 memset(&blocklights[0], 0, size*3*sizeof(int));
394 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
396 surf->cached_dlight = R_AddDynamicLights(surf);
397 if (surf->cached_dlight)
399 else if (dlightchanged)
400 return; // don't upload if only updating dlights and none mattered
403 // add all the lightmaps
405 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
406 for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
407 *bl++ += *lightmap++ * scale;
410 stain = surf->stainsamples;
412 for (bl = blocklights, i = 0;i < size3;i++)
414 bl[i] = (bl[i] * stain[i]) >> 8;
418 // deal with lightmap brightness scale
419 shift = 7 + lightscalebit;
420 if (currentrenderentity->model->lightmaprgba)
422 stride = (surf->lightmaptexturestride - smax) * 4;
423 for (i = 0;i < tmax;i++, out += stride)
425 for (j = 0;j < smax;j++)
427 l = *bl++ >> shift;*out++ = min(l, 255);
428 l = *bl++ >> shift;*out++ = min(l, 255);
429 l = *bl++ >> shift;*out++ = min(l, 255);
436 stride = (surf->lightmaptexturestride - smax) * 3;
437 for (i = 0;i < tmax;i++, out += stride)
439 for (j = 0;j < smax;j++)
441 l = *bl++ >> shift;*out++ = min(l, 255);
442 l = *bl++ >> shift;*out++ = min(l, 255);
443 l = *bl++ >> shift;*out++ = min(l, 255);
448 R_UpdateTexture(surf->lightmaptexture, templight);
455 Returns the proper texture for a given time and base texture
459 // note: this was manually inlined in R_PrepareSurfaces
460 static texture_t *R_TextureAnimation (texture_t *base)
462 if (currentrenderentity->frame && base->alternate_anims != NULL)
463 base = base->alternate_anims;
465 if (base->anim_total < 2)
468 return base->anim_frames[(int)(cl.time * 5.0f) % base->anim_total];
474 =============================================================
478 =============================================================
482 static float turbsin[256] =
484 #include "gl_warp_sin.h"
486 #define TURBSCALE (256.0 / (2 * M_PI))
488 #define MAX_SURFVERTS 1024
497 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
499 static void RSurfShader_Sky(msurface_t *firstsurf)
503 float number, length, dir[3], speedscale;
508 // LordHavoc: HalfLife maps have freaky skypolys...
509 if (currentrenderentity->model->ishlbsp)
516 skyrendernow = false;
519 for (surf = firstsurf;surf;surf = surf->chain)
521 // draw depth-only polys
522 memset(&m, 0, sizeof(m));
523 m.transparent = false;
524 m.blendfunc1 = GL_ZERO;
525 m.blendfunc2 = GL_ONE;
527 m.numtriangles = surf->mesh.numtriangles;
528 m.numverts = surf->mesh.numverts;
529 m.index = surf->mesh.index;
534 if (softwaretransform_complexity)
536 m.vertex = &svert[0].v[0];
537 m.vertexstep = sizeof(surfvert_t);
538 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
539 softwaretransform(v->v, sv->v);
543 m.vertex = &surf->mesh.vertex[0].v[0];
544 m.vertexstep = sizeof(surfvertex_t);
549 else if (skyrenderglquake)
551 for (surf = firstsurf;surf;surf = surf->chain)
553 memset(&m, 0, sizeof(m));
554 m.transparent = false;
555 m.blendfunc1 = GL_ONE;
556 m.blendfunc2 = GL_ZERO;
557 m.numtriangles = surf->mesh.numtriangles;
558 m.numverts = surf->mesh.numverts;
559 m.index = surf->mesh.index;
560 m.vertex = &svert[0].v[0];
561 m.vertexstep = sizeof(surfvert_t);
566 m.tex[0] = R_GetTexture(solidskytexture);
567 m.texcoords[0] = &svert[0].st[0];
568 m.texcoordstep[0] = sizeof(surfvert_t);
569 speedscale = cl.time * (8.0/128.0);
570 speedscale -= (int)speedscale;
571 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
573 softwaretransform(v->v, sv->v);
574 VectorSubtract (sv->v, r_origin, dir);
575 // flatten the sphere
578 number = DotProduct(dir, dir);
580 length = 3.0f / sqrt(number);
582 *((long *)&length) = 0x5f3759df - ((* (long *) &number) >> 1);
583 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
586 sv->st[0] = speedscale + dir[0] * length;
587 sv->st[1] = speedscale + dir[1] * length;
594 for (surf = firstsurf;surf;surf = surf->chain)
597 memset(&m, 0, sizeof(m));
598 m.transparent = false;
599 m.blendfunc1 = GL_ONE;
600 m.blendfunc2 = GL_ZERO;
601 m.numtriangles = surf->mesh.numtriangles;
602 m.numverts = surf->mesh.numverts;
603 m.index = surf->mesh.index;
608 if (softwaretransform_complexity)
610 m.vertex = &svert[0].v[0];
611 m.vertexstep = sizeof(surfvert_t);
612 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
613 softwaretransform(v->v, sv->v);
617 m.vertex = &surf->mesh.vertex[0].v[0];
618 m.vertexstep = sizeof(surfvertex_t);
623 if (skyrenderglquake)
625 for (surf = firstsurf;surf;surf = surf->chain)
627 memset(&m, 0, sizeof(m));
628 m.transparent = false;
629 m.blendfunc1 = GL_SRC_ALPHA;
630 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
631 m.numtriangles = surf->mesh.numtriangles;
632 m.numverts = surf->mesh.numverts;
633 m.index = surf->mesh.index;
634 m.vertex = &svert[0].v[0];
635 m.vertexstep = sizeof(surfvert_t);
640 m.tex[0] = R_GetTexture(alphaskytexture);
641 m.texcoords[0] = &svert[0].st[0];
642 m.texcoordstep[0] = sizeof(surfvert_t);
643 speedscale = cl.time * (16.0/128.0);
644 speedscale -= (int)speedscale;
645 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
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;
668 static int RSurf_Light(int *dlightbits, int numverts)
671 int i, l, lit = false;
675 for (l = 0;l < r_numdlights;l++)
677 if (dlightbits[l >> 5] & (1 << (l & 31)))
680 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
681 VectorCopy(rd->origin, lightorigin);
682 for (i = 0, sv = svert;i < numverts;i++, sv++)
684 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
685 if (f < rd->cullradius2)
687 f = (1.0f / f) - rd->lightsubtract;
688 sv->c[0] += rd->light[0] * f;
689 sv->c[1] += rd->light[1] * f;
690 sv->c[2] += rd->light[2] * f;
699 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
702 float diff[3], alpha, ifog;
706 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
708 memset(&m, 0, sizeof(m));
709 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
711 m.transparent = true;
712 m.blendfunc1 = GL_SRC_ALPHA;
713 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
717 m.transparent = false;
718 m.blendfunc1 = GL_ONE;
719 m.blendfunc2 = GL_ZERO;
721 m.numtriangles = surf->mesh.numtriangles;
722 m.numverts = surf->mesh.numverts;
723 m.index = surf->mesh.index;
724 m.vertex = &svert[0].v[0];
725 m.vertexstep = sizeof(surfvert_t);
726 m.color = &svert[0].c[0];
727 m.colorstep = sizeof(surfvert_t);
728 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
729 m.texcoords[0] = &svert[0].st[0];
730 m.texcoordstep[0] = sizeof(surfvert_t);
731 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
733 softwaretransform(v->v, sv->v);
734 if (r_waterripple.value)
735 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
736 if (surf->flags & SURF_DRAWFULLBRIGHT)
750 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
751 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
753 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
754 RSurf_Light(surf->dlightbits, m.numverts);
755 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
757 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
759 VectorSubtract(sv->v, r_origin, diff);
760 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
769 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
772 float diff[3], alpha, ifog;
776 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
778 memset(&m, 0, sizeof(m));
779 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
780 m.blendfunc1 = GL_SRC_ALPHA;
781 m.blendfunc2 = GL_ONE;
782 m.numtriangles = surf->mesh.numtriangles;
783 m.numverts = surf->mesh.numverts;
784 m.index = surf->mesh.index;
785 m.vertex = &svert[0].v[0];
786 m.vertexstep = sizeof(surfvert_t);
791 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
792 m.texcoords[0] = &svert[0].st[0];
793 m.texcoordstep[0] = sizeof(surfvert_t);
796 m.color = &svert[0].c[0];
797 m.colorstep = sizeof(surfvert_t);
798 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
800 softwaretransform(v->v, sv->v);
801 if (r_waterripple.value)
802 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
803 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
804 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
805 VectorSubtract(sv->v, r_origin, diff);
806 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
807 sv->c[0] = m.cr * ifog;
808 sv->c[1] = m.cg * ifog;
809 sv->c[2] = m.cb * ifog;
815 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
817 softwaretransform(v->v, sv->v);
818 if (r_waterripple.value)
819 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
820 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
821 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
827 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
835 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
837 memset(&m, 0, sizeof(m));
838 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
839 m.blendfunc1 = GL_SRC_ALPHA;
840 m.blendfunc2 = GL_ONE;
841 m.numtriangles = surf->mesh.numtriangles;
842 m.numverts = surf->mesh.numverts;
843 m.index = surf->mesh.index;
844 m.vertex = &svert[0].v[0];
845 m.vertexstep = sizeof(surfvert_t);
846 m.color = &svert[0].c[0];
847 m.colorstep = sizeof(surfvert_t);
848 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
849 m.texcoords[0] = &svert[0].st[0];
850 m.texcoordstep[0] = sizeof(surfvert_t);
852 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
854 softwaretransform(v->v, sv->v);
855 if (r_waterripple.value)
856 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
859 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
860 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
862 VectorSubtract(sv->v, r_origin, diff);
863 sv->c[0] = fogcolor[0];
864 sv->c[1] = fogcolor[1];
865 sv->c[2] = fogcolor[2];
866 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
871 static void RSurfShader_Water(msurface_t *firstsurf)
874 for (surf = firstsurf;surf;surf = surf->chain)
875 RSurfShader_Water_Pass_Base(surf);
876 for (surf = firstsurf;surf;surf = surf->chain)
877 if (surf->currenttexture->glowtexture)
878 RSurfShader_Water_Pass_Glow(surf);
880 for (surf = firstsurf;surf;surf = surf->chain)
881 //if (currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
882 RSurfShader_Water_Pass_Fog(surf);
885 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
893 memset(&m, 0, sizeof(m));
894 if (currentrenderentity->effects & EF_ADDITIVE)
896 m.transparent = true;
897 m.blendfunc1 = GL_SRC_ALPHA;
898 m.blendfunc2 = GL_ONE;
900 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
902 m.transparent = true;
903 m.blendfunc1 = GL_SRC_ALPHA;
904 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
908 m.transparent = false;
909 m.blendfunc1 = GL_ONE;
910 m.blendfunc2 = GL_ZERO;
912 m.numtriangles = surf->mesh.numtriangles;
913 m.numverts = surf->mesh.numverts;
914 m.index = surf->mesh.index;
915 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
916 m.ca = currentrenderentity->alpha;
917 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
918 m.tex[1] = R_GetTexture(surf->lightmaptexture);
919 m.texcoords[0] = &surf->mesh.vertex->st[0];
920 m.texcoords[1] = &surf->mesh.vertex->uv[0];
921 m.texcoordstep[0] = sizeof(surfvertex_t);
922 m.texcoordstep[1] = sizeof(surfvertex_t);
925 m.color = &svert[0].c[0];
926 m.colorstep = sizeof(surfvert_t);
927 if (softwaretransform_complexity)
929 m.vertex = &svert[0].v[0];
930 m.vertexstep = sizeof(surfvert_t);
931 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
933 softwaretransform(v->v, sv->v);
934 VectorSubtract(sv->v, r_origin, diff);
935 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
936 sv->c[0] = m.cr * ifog;
937 sv->c[1] = m.cg * ifog;
938 sv->c[2] = m.cb * ifog;
944 m.vertex = &surf->mesh.vertex->v[0];
945 m.vertexstep = sizeof(surfvertex_t);
946 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
948 VectorSubtract(v->v, r_origin, diff);
949 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
950 sv->c[0] = m.cr * ifog;
951 sv->c[1] = m.cg * ifog;
952 sv->c[2] = m.cb * ifog;
959 if (softwaretransform_complexity)
961 m.vertex = &svert[0].v[0];
962 m.vertexstep = sizeof(surfvert_t);
963 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
964 softwaretransform(v->v, sv->v);
968 m.vertex = &surf->mesh.vertex->v[0];
969 m.vertexstep = sizeof(surfvertex_t);
975 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
982 memset(&m, 0, sizeof(m));
983 m.transparent = false;
984 m.blendfunc1 = GL_ONE;
985 m.blendfunc2 = GL_ZERO;
986 m.numtriangles = surf->mesh.numtriangles;
987 m.numverts = surf->mesh.numverts;
988 m.index = surf->mesh.index;
989 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
991 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
992 m.texcoords[0] = &surf->mesh.vertex->st[0];
993 m.texcoordstep[0] = sizeof(surfvertex_t);
994 if (softwaretransform_complexity)
996 m.vertex = &svert[0].v[0];
997 m.vertexstep = sizeof(surfvert_t);
998 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
999 softwaretransform(v->v, sv->v);
1003 m.vertex = &surf->mesh.vertex->v[0];
1004 m.vertexstep = sizeof(surfvertex_t);
1009 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1012 float diff[3], ifog;
1017 memset(&m, 0, sizeof(m));
1018 m.transparent = false;
1019 m.blendfunc1 = GL_ZERO;
1020 m.blendfunc2 = GL_SRC_COLOR;
1021 m.numtriangles = surf->mesh.numtriangles;
1022 m.numverts = surf->mesh.numverts;
1023 m.index = surf->mesh.index;
1024 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1026 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1027 m.texcoords[0] = &surf->mesh.vertex->uv[0];
1028 m.texcoordstep[0] = sizeof(surfvertex_t);
1031 m.color = &svert[0].c[0];
1032 m.colorstep = sizeof(surfvert_t);
1033 if (softwaretransform_complexity)
1035 m.vertex = &svert[0].v[0];
1036 m.vertexstep = sizeof(surfvert_t);
1037 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1039 softwaretransform(v->v, sv->v);
1040 VectorSubtract(sv->v, r_origin, diff);
1041 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1042 sv->c[0] = m.cr * ifog;
1043 sv->c[1] = m.cg * ifog;
1044 sv->c[2] = m.cb * ifog;
1050 m.vertex = &surf->mesh.vertex->v[0];
1051 m.vertexstep = sizeof(surfvertex_t);
1052 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1054 VectorSubtract(v->v, r_origin, diff);
1055 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1056 sv->c[0] = m.cr * ifog;
1057 sv->c[1] = m.cg * ifog;
1058 sv->c[2] = m.cb * ifog;
1065 if (softwaretransform_complexity)
1067 m.vertex = &svert[0].v[0];
1068 m.vertexstep = sizeof(surfvert_t);
1069 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1070 softwaretransform(v->v, sv->v);
1074 m.vertex = &surf->mesh.vertex->v[0];
1075 m.vertexstep = sizeof(surfvertex_t);
1081 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1084 float c[3], base[3], scale, diff[3], ifog;
1090 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1092 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1094 memset(&m, 0, sizeof(m));
1095 if (currentrenderentity->effects & EF_ADDITIVE)
1097 m.transparent = true;
1098 m.blendfunc1 = GL_SRC_ALPHA;
1099 m.blendfunc2 = GL_ONE;
1101 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1103 m.transparent = true;
1104 m.blendfunc1 = GL_SRC_ALPHA;
1105 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1109 m.transparent = false;
1110 m.blendfunc1 = GL_ONE;
1111 m.blendfunc2 = GL_ZERO;
1113 m.numtriangles = surf->mesh.numtriangles;
1114 m.numverts = surf->mesh.numverts;
1115 m.index = surf->mesh.index;
1116 m.vertex = &svert[0].v[0];
1117 m.vertexstep = sizeof(surfvert_t);
1118 m.color = &svert[0].c[0];
1119 m.colorstep = sizeof(surfvert_t);
1120 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1121 m.texcoords[0] = &surf->mesh.vertex->st[0];
1122 m.texcoordstep[0] = sizeof(surfvertex_t);
1123 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1125 softwaretransform(v->v, sv->v);
1126 VectorCopy(base, c);
1127 if (surf->styles[0] != 255)
1129 lm = surf->samples + v->lightmapoffset;
1130 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1131 VectorMA(c, scale, lm, c);
1132 if (surf->styles[1] != 255)
1135 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1136 VectorMA(c, scale, lm, c);
1137 if (surf->styles[2] != 255)
1140 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1141 VectorMA(c, scale, lm, c);
1142 if (surf->styles[3] != 255)
1145 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1146 VectorMA(c, scale, lm, c);
1154 sv->c[3] = currentrenderentity->alpha;
1156 if (surf->dlightframe == r_framecount)
1157 RSurf_Light(surf->dlightbits, m.numverts);
1160 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1162 VectorSubtract(sv->v, r_origin, diff);
1163 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1172 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1175 float diff[3], ifog;
1180 memset(&m, 0, sizeof(m));
1181 if (currentrenderentity->effects & EF_ADDITIVE)
1183 m.transparent = true;
1184 m.blendfunc1 = GL_SRC_ALPHA;
1185 m.blendfunc2 = GL_ONE;
1187 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1189 m.transparent = true;
1190 m.blendfunc1 = GL_SRC_ALPHA;
1191 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1195 m.transparent = false;
1196 m.blendfunc1 = GL_ONE;
1197 m.blendfunc2 = GL_ZERO;
1199 m.numtriangles = surf->mesh.numtriangles;
1200 m.numverts = surf->mesh.numverts;
1201 m.index = surf->mesh.index;
1202 m.vertex = &svert[0].v[0];
1203 m.vertexstep = sizeof(surfvert_t);
1204 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1205 m.texcoords[0] = &surf->mesh.vertex->st[0];
1206 m.texcoordstep[0] = sizeof(surfvertex_t);
1209 m.color = &svert[0].c[0];
1210 m.colorstep = sizeof(surfvert_t);
1211 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1213 softwaretransform(v->v, sv->v);
1214 VectorSubtract(sv->v, r_origin, diff);
1215 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1219 sv->c[3] = currentrenderentity->alpha;
1224 m.cr = m.cg = m.cb = 1;
1225 m.ca = currentrenderentity->alpha;
1226 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1227 softwaretransform(v->v, sv->v);
1232 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1235 float diff[3], ifog;
1240 memset(&m, 0, sizeof(m));
1241 if (currentrenderentity->effects & EF_ADDITIVE)
1242 m.transparent = true;
1243 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1244 m.transparent = true;
1246 m.transparent = false;
1247 m.blendfunc1 = GL_SRC_ALPHA;
1248 m.blendfunc2 = GL_ONE;
1249 m.numtriangles = surf->mesh.numtriangles;
1250 m.numverts = surf->mesh.numverts;
1251 m.index = surf->mesh.index;
1252 m.vertex = &svert[0].v[0];
1253 m.vertexstep = sizeof(surfvert_t);
1254 m.color = &svert[0].c[0];
1255 m.colorstep = sizeof(surfvert_t);
1256 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1257 m.texcoords[0] = &surf->mesh.vertex->st[0];
1258 m.texcoordstep[0] = sizeof(surfvertex_t);
1259 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1261 softwaretransform(v->v, sv->v);
1265 sv->c[3] = currentrenderentity->alpha;
1267 if (RSurf_Light(surf->dlightbits, m.numverts))
1271 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1273 VectorSubtract(sv->v, r_origin, diff);
1274 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1284 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1287 float diff[3], ifog;
1292 memset(&m, 0, sizeof(m));
1293 if (currentrenderentity->effects & EF_ADDITIVE)
1294 m.transparent = true;
1295 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1296 m.transparent = true;
1298 m.transparent = false;
1299 m.blendfunc1 = GL_SRC_ALPHA;
1300 m.blendfunc2 = GL_ONE;
1301 m.numtriangles = surf->mesh.numtriangles;
1302 m.numverts = surf->mesh.numverts;
1303 m.index = surf->mesh.index;
1307 m.ca = currentrenderentity->alpha;
1308 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1309 m.texcoords[0] = &surf->mesh.vertex->st[0];
1310 m.texcoordstep[0] = sizeof(surfvertex_t);
1313 m.color = &svert[0].c[0];
1314 m.colorstep = sizeof(surfvert_t);
1315 if (softwaretransform_complexity)
1317 m.vertex = &svert[0].v[0];
1318 m.vertexstep = sizeof(surfvert_t);
1319 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1321 softwaretransform(v->v, sv->v);
1322 VectorSubtract(sv->v, r_origin, diff);
1323 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1324 sv->c[0] = m.cr * ifog;
1325 sv->c[1] = m.cg * ifog;
1326 sv->c[2] = m.cb * ifog;
1332 m.vertex = &surf->mesh.vertex->v[0];
1333 m.vertexstep = sizeof(surfvertex_t);
1334 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1336 VectorSubtract(v->v, r_origin, diff);
1337 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1338 sv->c[0] = m.cr * ifog;
1339 sv->c[1] = m.cg * ifog;
1340 sv->c[2] = m.cb * ifog;
1347 if (softwaretransform_complexity)
1349 m.vertex = &svert[0].v[0];
1350 m.vertexstep = sizeof(surfvert_t);
1351 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1352 softwaretransform(v->v, sv->v);
1356 m.vertex = &surf->mesh.vertex->v[0];
1357 m.vertexstep = sizeof(surfvertex_t);
1363 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1371 memset(&m, 0, sizeof(m));
1372 if (currentrenderentity->effects & EF_ADDITIVE)
1373 m.transparent = true;
1374 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1375 m.transparent = true;
1377 m.transparent = false;
1378 m.blendfunc1 = GL_SRC_ALPHA;
1379 m.blendfunc2 = GL_ONE;
1380 m.numtriangles = surf->mesh.numtriangles;
1381 m.numverts = surf->mesh.numverts;
1382 m.index = surf->mesh.index;
1383 m.color = &svert[0].c[0];
1384 m.colorstep = sizeof(surfvert_t);
1385 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1386 m.texcoords[0] = &surf->mesh.vertex->st[0];
1387 m.texcoordstep[0] = sizeof(surfvertex_t);
1388 if (softwaretransform_complexity)
1390 m.vertex = &svert[0].v[0];
1391 m.vertexstep = sizeof(surfvert_t);
1392 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1394 softwaretransform(v->v, sv->v);
1395 VectorSubtract(sv->v, r_origin, diff);
1396 sv->c[0] = fogcolor[0];
1397 sv->c[1] = fogcolor[1];
1398 sv->c[2] = fogcolor[2];
1399 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1404 m.vertex = &surf->mesh.vertex->v[0];
1405 m.vertexstep = sizeof(surfvertex_t);
1406 for (i = 0, sv = svert, v = surf->mesh.vertex;i < m.numverts;i++, sv++, v++)
1408 VectorSubtract(v->v, r_origin, diff);
1409 sv->c[0] = fogcolor[0];
1410 sv->c[1] = fogcolor[1];
1411 sv->c[2] = fogcolor[2];
1412 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1418 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1421 for (surf = firstsurf;surf;surf = surf->chain)
1424 RSurfShader_Wall_Pass_BaseFullbright(surf);
1426 for (surf = firstsurf;surf;surf = surf->chain)
1427 if (surf->currenttexture->glowtexture)
1428 RSurfShader_Wall_Pass_Glow(surf);
1431 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1434 for (surf = firstsurf;surf;surf = surf->chain)
1437 RSurfShader_Wall_Pass_BaseVertex(surf);
1439 for (surf = firstsurf;surf;surf = surf->chain)
1440 if (surf->currenttexture->glowtexture)
1441 RSurfShader_Wall_Pass_Glow(surf);
1444 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1447 if (r_vertexsurfaces.integer)
1449 for (surf = firstsurf;surf;surf = surf->chain)
1452 RSurfShader_Wall_Pass_BaseVertex(surf);
1454 for (surf = firstsurf;surf;surf = surf->chain)
1455 if (surf->currenttexture->glowtexture)
1456 RSurfShader_Wall_Pass_Glow(surf);
1458 else if (r_multitexture.integer)
1460 if (r_dlightmap.integer)
1462 for (surf = firstsurf;surf;surf = surf->chain)
1465 RSurfShader_Wall_Pass_BaseMTex(surf);
1467 for (surf = firstsurf;surf;surf = surf->chain)
1468 if (surf->currenttexture->glowtexture)
1469 RSurfShader_Wall_Pass_Glow(surf);
1473 for (surf = firstsurf;surf;surf = surf->chain)
1476 RSurfShader_Wall_Pass_BaseMTex(surf);
1478 for (surf = firstsurf;surf;surf = surf->chain)
1479 if (surf->dlightframe == r_framecount)
1480 RSurfShader_Wall_Pass_Light(surf);
1481 for (surf = firstsurf;surf;surf = surf->chain)
1482 if (surf->currenttexture->glowtexture)
1483 RSurfShader_Wall_Pass_Glow(surf);
1486 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1488 for (surf = firstsurf;surf;surf = surf->chain)
1491 RSurfShader_Wall_Pass_BaseVertex(surf);
1493 for (surf = firstsurf;surf;surf = surf->chain)
1494 if (surf->currenttexture->glowtexture)
1495 RSurfShader_Wall_Pass_Glow(surf);
1499 if (r_dlightmap.integer)
1501 for (surf = firstsurf;surf;surf = surf->chain)
1504 RSurfShader_Wall_Pass_BaseTexture(surf);
1506 for (surf = firstsurf;surf;surf = surf->chain)
1507 RSurfShader_Wall_Pass_BaseLightmap(surf);
1508 for (surf = firstsurf;surf;surf = surf->chain)
1509 if (surf->currenttexture->glowtexture)
1510 RSurfShader_Wall_Pass_Glow(surf);
1514 for (surf = firstsurf;surf;surf = surf->chain)
1517 RSurfShader_Wall_Pass_BaseTexture(surf);
1519 for (surf = firstsurf;surf;surf = surf->chain)
1520 RSurfShader_Wall_Pass_BaseLightmap(surf);
1521 for (surf = firstsurf;surf;surf = surf->chain)
1522 if (surf->dlightframe == r_framecount)
1523 RSurfShader_Wall_Pass_Light(surf);
1524 for (surf = firstsurf;surf;surf = surf->chain)
1525 if (surf->currenttexture->glowtexture)
1526 RSurfShader_Wall_Pass_Glow(surf);
1531 static void RSurfShader_Wall_Fog(msurface_t *firstsurf)
1536 for (surf = firstsurf;surf;surf = surf->chain)
1537 RSurfShader_Wall_Pass_Fog(surf);
1541 =============================================================
1545 =============================================================
1548 static void RSurf_Callback(void *data, void *junk)
1550 ((msurface_t *)data)->visframe = r_framecount;
1553 static void R_SolidWorldNode (void)
1555 if (r_viewleaf->contents != CONTENTS_SOLID)
1558 mportal_t *p, *pstack[8192];
1559 msurface_t *surf, **mark, **endmark;
1562 // LordHavoc: portal-passage worldnode; follows portals leading
1563 // outward from viewleaf, if a portal leads offscreen it is not
1564 // followed, in indoor maps this can often cull a great deal of
1565 // geometry away when pvs data is not present (useful with pvs as well)
1568 leaf->worldnodeframe = r_framecount;
1573 leaf->visframe = r_framecount;
1575 if (leaf->nummarksurfaces)
1577 mark = leaf->firstmarksurface;
1578 endmark = mark + leaf->nummarksurfaces;
1584 // make sure surfaces are only processed once
1585 if (surf->worldnodeframe == r_framecount)
1587 surf->worldnodeframe = r_framecount;
1588 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1590 if (surf->flags & SURF_PLANEBACK)
1592 VectorNegate(surf->plane->normal, plane.normal);
1593 plane.dist = -surf->plane->dist;
1594 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1599 if (!(surf->flags & SURF_PLANEBACK))
1600 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1603 while (mark < endmark);
1610 // make sure surfaces are only processed once
1611 if (surf->worldnodeframe == r_framecount)
1613 surf->worldnodeframe = r_framecount;
1614 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1616 if (surf->flags & SURF_PLANEBACK)
1617 surf->visframe = r_framecount;
1621 if (!(surf->flags & SURF_PLANEBACK))
1622 surf->visframe = r_framecount;
1625 while (mark < endmark);
1629 // follow portals into other leafs
1631 for (;p;p = p->next)
1633 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1636 if (leaf->worldnodeframe != r_framecount)
1638 leaf->worldnodeframe = r_framecount;
1639 if (leaf->contents != CONTENTS_SOLID)
1641 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1643 p->visframe = r_framecount;
1644 pstack[portalstack++] = p;
1648 p = pstack[--portalstack];
1660 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1661 int nodestackpos = 0;
1662 // LordHavoc: recursive descending worldnode; if portals are not
1663 // available, this is a good last resort, can cull large amounts of
1664 // geometry, but is more time consuming than portal-passage and renders
1665 // things behind walls
1668 if (R_NotCulledBox(node->mins, node->maxs))
1670 if (node->numsurfaces)
1674 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1676 if (PlaneDiff (r_origin, node->plane) < 0)
1678 for (;surf < surfend;surf++)
1680 if (surf->flags & SURF_PLANEBACK)
1682 VectorNegate(surf->plane->normal, plane.normal);
1683 plane.dist = -surf->plane->dist;
1684 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1690 for (;surf < surfend;surf++)
1692 if (!(surf->flags & SURF_PLANEBACK))
1693 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1699 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1700 if (PlaneDiff (r_origin, node->plane) < 0)
1702 for (;surf < surfend;surf++)
1704 if (surf->flags & SURF_PLANEBACK)
1705 surf->visframe = r_framecount;
1710 for (;surf < surfend;surf++)
1712 if (!(surf->flags & SURF_PLANEBACK))
1713 surf->visframe = r_framecount;
1719 // recurse down the children
1720 if (node->children[0]->contents >= 0)
1722 if (node->children[1]->contents >= 0)
1724 if (nodestackpos < 8192)
1725 nodestack[nodestackpos++] = node->children[1];
1726 node = node->children[0];
1730 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1731 node = node->children[0];
1736 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1737 if (node->children[1]->contents >= 0)
1739 node = node->children[1];
1742 else if (nodestackpos > 0)
1744 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1745 node = nodestack[--nodestackpos];
1750 else if (nodestackpos > 0)
1752 node = nodestack[--nodestackpos];
1758 static int r_portalframecount = 0;
1760 static void R_PVSWorldNode()
1763 mportal_t *p, *pstack[8192];
1764 msurface_t *surf, **mark, **endmark;
1769 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1772 leaf->worldnodeframe = r_framecount;
1777 leaf->visframe = r_framecount;
1779 if (leaf->nummarksurfaces)
1781 mark = leaf->firstmarksurface;
1782 endmark = mark + leaf->nummarksurfaces;
1788 // make sure surfaces are only processed once
1789 if (surf->worldnodeframe == r_framecount)
1791 surf->worldnodeframe = r_framecount;
1792 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1794 if (surf->flags & SURF_PLANEBACK)
1796 VectorNegate(surf->plane->normal, plane.normal);
1797 plane.dist = -surf->plane->dist;
1798 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1803 if (!(surf->flags & SURF_PLANEBACK))
1804 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1807 while (mark < endmark);
1814 // make sure surfaces are only processed once
1815 if (surf->worldnodeframe == r_framecount)
1817 surf->worldnodeframe = r_framecount;
1818 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1820 if (surf->flags & SURF_PLANEBACK)
1821 surf->visframe = r_framecount;
1825 if (!(surf->flags & SURF_PLANEBACK))
1826 surf->visframe = r_framecount;
1829 while (mark < endmark);
1833 // follow portals into other leafs
1834 for (p = leaf->portals;p;p = p->next)
1836 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1839 if (leaf->worldnodeframe != r_framecount)
1841 leaf->worldnodeframe = r_framecount;
1842 if (leaf->contents != CONTENTS_SOLID)
1844 i = (leaf - cl.worldmodel->leafs) - 1;
1845 if (worldvis[i>>3] & (1<<(i&7)))
1847 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1849 pstack[portalstack++] = p;
1853 p = pstack[--portalstack];
1865 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex, RSurfShader_Wall_Fog}, NULL};
1866 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap, RSurfShader_Wall_Fog}, NULL};
1867 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright, RSurfShader_Wall_Fog}, NULL};
1868 Cshader_t Cshader_water = {{NULL, RSurfShader_Water, NULL}, NULL};
1869 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL, NULL}, NULL};
1871 int Cshader_count = 5;
1872 Cshader_t *Cshaders[5] =
1874 &Cshader_wall_vertex,
1875 &Cshader_wall_lightmap,
1876 &Cshader_wall_fullbright,
1881 void R_PrepareSurfaces(void)
1888 for (i = 0;i < Cshader_count;i++)
1889 Cshaders[i]->chain = NULL;
1891 model = currentrenderentity->model;
1893 for (i = 0;i < model->nummodelsurfaces;i++)
1895 surf = model->modelsortedsurfaces[i];
1896 if (surf->visframe == r_framecount)
1898 if (surf->insertframe != r_framecount)
1900 surf->insertframe = r_framecount;
1902 // manually inlined R_TextureAnimation
1903 //t = R_TextureAnimation(surf->texinfo->texture);
1904 t = surf->texinfo->texture;
1905 if (t->alternate_anims != NULL && currentrenderentity->frame)
1906 t = t->alternate_anims;
1907 if (t->anim_total >= 2)
1908 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1909 surf->currenttexture = t;
1912 surf->chain = surf->shader->chain;
1913 surf->shader->chain = surf;
1918 void R_DrawSurfaces (int type)
1923 for (i = 0;i < Cshader_count;i++)
1925 shader = Cshaders[i];
1926 if (shader->chain && shader->shaderfunc[type])
1927 shader->shaderfunc[type](shader->chain);
1931 static float portalpointbuffer[256][3];
1933 void R_DrawPortals(void)
1936 // mleaf_t *leaf, *endleaf;
1937 mportal_t *portal, *endportal;
1938 mvertex_t *point/*, *endpoint*/;
1940 drawportals = r_drawportals.integer;
1941 if (drawportals < 1)
1944 leaf = cl.worldmodel->leafs;
1945 endleaf = leaf + cl.worldmodel->numleafs;
1946 for (;leaf < endleaf;leaf++)
1948 if (leaf->visframe == r_framecount && leaf->portals)
1950 i = leaf - cl.worldmodel->leafs;
1951 r = (i & 0x0007) << 5;
1952 g = (i & 0x0038) << 2;
1953 b = (i & 0x01C0) >> 1;
1954 portal = leaf->portals;
1957 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1958 point = portal->points + portal->numpoints - 1;
1959 endpoint = portal->points;
1960 for (;point >= endpoint;point--)
1961 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1963 portal = portal->next;
1968 memset(&m, 0, sizeof(m));
1969 m.transparent = true;
1970 m.blendfunc1 = GL_SRC_ALPHA;
1971 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1972 m.vertex = &portalpointbuffer[0][0];
1973 m.vertexstep = sizeof(float[3]);
1975 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1977 if (portal->visframe == r_portalframecount)
1979 if (portal->numpoints <= 256)
1981 i = portal - cl.worldmodel->portals;
1982 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
1983 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
1984 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
1985 point = portal->points;
1986 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1988 for (i = portal->numpoints - 1;i >= 0;i--)
1989 VectorCopy(point[i].position, portalpointbuffer[i]);
1993 for (i = 0;i < portal->numpoints;i++)
1994 VectorCopy(point[i].position, portalpointbuffer[i]);
1996 R_Mesh_DrawPolygon(&m, portal->numpoints);
2002 void R_SetupForBModelRendering(void)
2009 // because bmodels can be reused, we have to decide which things to render
2010 // from scratch every time
2012 model = currentrenderentity->model;
2014 softwaretransformforentity (currentrenderentity);
2015 softwareuntransform(r_origin, modelorg);
2017 for (i = 0;i < model->nummodelsurfaces;i++)
2019 surf = model->modelsortedsurfaces[i];
2020 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2021 surf->visframe = r_framecount;
2023 surf->visframe = -1;
2024 surf->worldnodeframe = -1;
2025 surf->lightframe = -1;
2026 surf->dlightframe = -1;
2027 surf->insertframe = -1;
2031 void R_SetupForWorldRendering(void)
2033 // there is only one instance of the world, but it can be rendered in
2036 currentrenderentity = &cl_entities[0].render;
2037 softwaretransformidentity();
2040 static void R_SurfMarkLights (void)
2045 if (r_dynamic.integer)
2048 if (!r_vertexsurfaces.integer)
2050 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2052 surf = currentrenderentity->model->modelsortedsurfaces[i];
2053 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2055 if (surf->cached_dlight
2056 || surf->cached_ambient != r_ambient.value
2057 || surf->cached_lightscalebit != lightscalebit)
2058 R_BuildLightMap(surf, false); // base lighting changed
2059 else if (r_dynamic.integer)
2061 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2062 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2063 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2064 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2065 //if (surf->cached_light[0] != d_lightstylevalue[surf->styles[0]]
2066 // || surf->cached_light[1] != d_lightstylevalue[surf->styles[1]]
2067 // || surf->cached_light[2] != d_lightstylevalue[surf->styles[2]]
2068 // || surf->cached_light[3] != d_lightstylevalue[surf->styles[3]])
2069 R_BuildLightMap(surf, false); // base lighting changed
2070 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2071 R_BuildLightMap(surf, true); // only dlights
2078 void R_MarkWorldLights(void)
2080 R_SetupForWorldRendering();
2089 void R_DrawWorld (void)
2091 R_SetupForWorldRendering();
2093 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2094 R_SolidWorldNode ();
2104 void R_DrawBrushModelSky (void)
2106 R_SetupForBModelRendering();
2108 R_PrepareSurfaces();
2109 R_DrawSurfaces(SHADERSTAGE_SKY);
2112 void R_DrawBrushModelNormal (void)
2116 // have to flush queue because of possible lightmap reuse
2119 R_SetupForBModelRendering();
2123 R_PrepareSurfaces();
2125 if (!skyrendermasked)
2126 R_DrawSurfaces(SHADERSTAGE_SKY);
2127 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2128 R_DrawSurfaces(SHADERSTAGE_FOG);