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;
509 // LordHavoc: HalfLife maps have freaky skypolys...
510 if (currentrenderentity->model->ishlbsp)
517 skyrendernow = false;
520 for (surf = firstsurf;surf;surf = surf->chain)
522 // draw depth-only polys
523 memset(&m, 0, sizeof(m));
524 m.transparent = false;
525 m.blendfunc1 = GL_ZERO;
526 m.blendfunc2 = GL_ONE;
528 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
530 m.numtriangles = mesh->numtriangles;
531 m.numverts = mesh->numverts;
532 m.index = mesh->index;
537 if (softwaretransform_complexity)
539 m.vertex = &svert[0].v[0];
540 m.vertexstep = sizeof(surfvert_t);
541 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
542 softwaretransform(v->v, sv->v);
546 m.vertex = &mesh->vertex[0].v[0];
547 m.vertexstep = sizeof(surfvertex_t);
553 else if (skyrenderglquake)
555 for (surf = firstsurf;surf;surf = surf->chain)
557 memset(&m, 0, sizeof(m));
558 m.transparent = false;
559 m.blendfunc1 = GL_ONE;
560 m.blendfunc2 = GL_ZERO;
561 m.vertex = &svert[0].v[0];
562 m.vertexstep = sizeof(surfvert_t);
567 m.tex[0] = R_GetTexture(solidskytexture);
568 m.texcoords[0] = &svert[0].st[0];
569 m.texcoordstep[0] = sizeof(surfvert_t);
570 speedscale = cl.time * (8.0/128.0);
571 speedscale -= (int)speedscale;
572 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
574 m.numtriangles = mesh->numtriangles;
575 m.numverts = mesh->numverts;
576 m.index = mesh->index;
577 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
579 softwaretransform(v->v, sv->v);
580 VectorSubtract (sv->v, r_origin, dir);
581 // flatten the sphere
584 number = DotProduct(dir, dir);
586 length = 3.0f / sqrt(number);
588 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
589 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
592 sv->st[0] = speedscale + dir[0] * length;
593 sv->st[1] = speedscale + dir[1] * length;
601 for (surf = firstsurf;surf;surf = surf->chain)
604 memset(&m, 0, sizeof(m));
605 m.transparent = false;
606 m.blendfunc1 = GL_ONE;
607 m.blendfunc2 = GL_ZERO;
612 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
614 m.numtriangles = mesh->numtriangles;
615 m.numverts = mesh->numverts;
616 m.index = mesh->index;
617 if (softwaretransform_complexity)
619 m.vertex = &svert[0].v[0];
620 m.vertexstep = sizeof(surfvert_t);
621 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
622 softwaretransform(v->v, sv->v);
626 m.vertex = &mesh->vertex[0].v[0];
627 m.vertexstep = sizeof(surfvertex_t);
633 if (skyrenderglquake)
635 for (surf = firstsurf;surf;surf = surf->chain)
637 memset(&m, 0, sizeof(m));
638 m.transparent = false;
639 m.blendfunc1 = GL_SRC_ALPHA;
640 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
641 m.vertex = &svert[0].v[0];
642 m.vertexstep = sizeof(surfvert_t);
647 m.tex[0] = R_GetTexture(alphaskytexture);
648 m.texcoords[0] = &svert[0].st[0];
649 m.texcoordstep[0] = sizeof(surfvert_t);
650 speedscale = cl.time * (16.0/128.0);
651 speedscale -= (int)speedscale;
652 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
654 m.numtriangles = mesh->numtriangles;
655 m.numverts = mesh->numverts;
656 m.index = mesh->index;
657 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
659 softwaretransform(v->v, sv->v);
660 VectorSubtract (sv->v, r_origin, dir);
661 // flatten the sphere
664 number = DotProduct(dir, dir);
666 length = 3.0f / sqrt(number);
668 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
669 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
672 sv->st[0] = speedscale + dir[0] * length;
673 sv->st[1] = speedscale + dir[1] * length;
681 static int RSurf_Light(int *dlightbits, int numverts)
684 int i, l, lit = false;
688 for (l = 0;l < r_numdlights;l++)
690 if (dlightbits[l >> 5] & (1 << (l & 31)))
693 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
694 VectorCopy(rd->origin, lightorigin);
695 for (i = 0, sv = svert;i < numverts;i++, sv++)
697 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
698 if (f < rd->cullradius2)
700 f = (1.0f / f) - rd->lightsubtract;
701 sv->c[0] += rd->light[0] * f;
702 sv->c[1] += rd->light[1] * f;
703 sv->c[2] += rd->light[2] * f;
712 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
715 float diff[3], alpha, ifog;
720 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
722 memset(&m, 0, sizeof(m));
723 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
725 m.transparent = true;
726 m.blendfunc1 = GL_SRC_ALPHA;
727 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
731 m.transparent = false;
732 m.blendfunc1 = GL_ONE;
733 m.blendfunc2 = GL_ZERO;
735 m.vertex = &svert[0].v[0];
736 m.vertexstep = sizeof(surfvert_t);
737 m.color = &svert[0].c[0];
738 m.colorstep = sizeof(surfvert_t);
739 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
740 m.texcoords[0] = &svert[0].st[0];
741 m.texcoordstep[0] = sizeof(surfvert_t);
742 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
744 m.numtriangles = mesh->numtriangles;
745 m.numverts = mesh->numverts;
746 m.index = mesh->index;
747 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
749 softwaretransform(v->v, sv->v);
750 if (r_waterripple.value)
751 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];
752 if (surf->flags & SURF_DRAWFULLBRIGHT)
766 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
767 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
769 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
770 RSurf_Light(surf->dlightbits, m.numverts);
771 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
773 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
775 VectorSubtract(sv->v, r_origin, diff);
776 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
786 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
789 float diff[3], alpha, ifog;
794 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
796 memset(&m, 0, sizeof(m));
797 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
798 m.blendfunc1 = GL_SRC_ALPHA;
799 m.blendfunc2 = GL_ONE;
800 m.vertex = &svert[0].v[0];
801 m.vertexstep = sizeof(surfvert_t);
806 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
807 m.texcoords[0] = &svert[0].st[0];
808 m.texcoordstep[0] = sizeof(surfvert_t);
809 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
811 m.numtriangles = mesh->numtriangles;
812 m.numverts = mesh->numverts;
813 m.index = mesh->index;
816 m.color = &svert[0].c[0];
817 m.colorstep = sizeof(surfvert_t);
818 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
820 softwaretransform(v->v, sv->v);
821 if (r_waterripple.value)
822 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];
823 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
824 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
825 VectorSubtract(sv->v, r_origin, diff);
826 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
827 sv->c[0] = m.cr * ifog;
828 sv->c[1] = m.cg * ifog;
829 sv->c[2] = m.cb * ifog;
835 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
837 softwaretransform(v->v, sv->v);
838 if (r_waterripple.value)
839 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];
840 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
841 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
848 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
857 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
859 memset(&m, 0, sizeof(m));
860 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
861 m.blendfunc1 = GL_SRC_ALPHA;
862 m.blendfunc2 = GL_ONE;
863 m.vertex = &svert[0].v[0];
864 m.vertexstep = sizeof(surfvert_t);
865 m.color = &svert[0].c[0];
866 m.colorstep = sizeof(surfvert_t);
867 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
868 m.texcoords[0] = &svert[0].st[0];
869 m.texcoordstep[0] = sizeof(surfvert_t);
871 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
873 m.numtriangles = mesh->numtriangles;
874 m.numverts = mesh->numverts;
875 m.index = mesh->index;
876 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
878 softwaretransform(v->v, sv->v);
879 if (r_waterripple.value)
880 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];
883 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
884 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
886 VectorSubtract(sv->v, r_origin, diff);
887 sv->c[0] = fogcolor[0];
888 sv->c[1] = fogcolor[1];
889 sv->c[2] = fogcolor[2];
890 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
896 static void RSurfShader_Water(msurface_t *firstsurf)
899 for (surf = firstsurf;surf;surf = surf->chain)
900 RSurfShader_Water_Pass_Base(surf);
901 for (surf = firstsurf;surf;surf = surf->chain)
902 if (surf->currenttexture->glowtexture)
903 RSurfShader_Water_Pass_Glow(surf);
905 for (surf = firstsurf;surf;surf = surf->chain)
906 //if (currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
907 RSurfShader_Water_Pass_Fog(surf);
910 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
919 memset(&m, 0, sizeof(m));
920 if (currentrenderentity->effects & EF_ADDITIVE)
922 m.transparent = true;
923 m.blendfunc1 = GL_SRC_ALPHA;
924 m.blendfunc2 = GL_ONE;
926 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
928 m.transparent = true;
929 m.blendfunc1 = GL_SRC_ALPHA;
930 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
934 m.transparent = false;
935 m.blendfunc1 = GL_ONE;
936 m.blendfunc2 = GL_ZERO;
938 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
939 m.ca = currentrenderentity->alpha;
940 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
941 m.tex[1] = R_GetTexture(surf->lightmaptexture);
942 m.texcoordstep[0] = sizeof(surfvertex_t);
943 m.texcoordstep[1] = sizeof(surfvertex_t);
944 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
946 m.numtriangles = mesh->numtriangles;
947 m.numverts = mesh->numverts;
948 m.index = mesh->index;
949 m.texcoords[0] = &mesh->vertex->st[0];
950 m.texcoords[1] = &mesh->vertex->uv[0];
953 m.color = &svert[0].c[0];
954 m.colorstep = sizeof(surfvert_t);
955 if (softwaretransform_complexity)
957 m.vertex = &svert[0].v[0];
958 m.vertexstep = sizeof(surfvert_t);
959 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
961 softwaretransform(v->v, sv->v);
962 VectorSubtract(sv->v, r_origin, diff);
963 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
964 sv->c[0] = m.cr * ifog;
965 sv->c[1] = m.cg * ifog;
966 sv->c[2] = m.cb * ifog;
972 m.vertex = &mesh->vertex->v[0];
973 m.vertexstep = sizeof(surfvertex_t);
974 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
976 VectorSubtract(v->v, r_origin, diff);
977 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
978 sv->c[0] = m.cr * ifog;
979 sv->c[1] = m.cg * ifog;
980 sv->c[2] = m.cb * ifog;
987 if (softwaretransform_complexity)
989 m.vertex = &svert[0].v[0];
990 m.vertexstep = sizeof(surfvert_t);
991 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
992 softwaretransform(v->v, sv->v);
996 m.vertex = &mesh->vertex->v[0];
997 m.vertexstep = sizeof(surfvertex_t);
1004 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1012 memset(&m, 0, sizeof(m));
1013 m.transparent = false;
1014 m.blendfunc1 = GL_ONE;
1015 m.blendfunc2 = GL_ZERO;
1016 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1018 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1019 m.texcoordstep[0] = sizeof(surfvertex_t);
1020 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1022 m.numtriangles = mesh->numtriangles;
1023 m.numverts = mesh->numverts;
1024 m.index = mesh->index;
1025 m.texcoords[0] = &mesh->vertex->st[0];
1026 if (softwaretransform_complexity)
1028 m.vertex = &svert[0].v[0];
1029 m.vertexstep = sizeof(surfvert_t);
1030 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1031 softwaretransform(v->v, sv->v);
1035 m.vertex = &mesh->vertex->v[0];
1036 m.vertexstep = sizeof(surfvertex_t);
1042 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1045 float diff[3], ifog;
1051 memset(&m, 0, sizeof(m));
1052 m.transparent = false;
1053 m.blendfunc1 = GL_ZERO;
1054 m.blendfunc2 = GL_SRC_COLOR;
1055 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1057 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1058 m.texcoordstep[0] = sizeof(surfvertex_t);
1059 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1061 m.numtriangles = mesh->numtriangles;
1062 m.numverts = mesh->numverts;
1063 m.index = mesh->index;
1064 m.texcoords[0] = &mesh->vertex->uv[0];
1067 m.color = &svert[0].c[0];
1068 m.colorstep = sizeof(surfvert_t);
1069 if (softwaretransform_complexity)
1071 m.vertex = &svert[0].v[0];
1072 m.vertexstep = sizeof(surfvert_t);
1073 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1075 softwaretransform(v->v, sv->v);
1076 VectorSubtract(sv->v, r_origin, diff);
1077 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1078 sv->c[0] = m.cr * ifog;
1079 sv->c[1] = m.cg * ifog;
1080 sv->c[2] = m.cb * ifog;
1086 m.vertex = &mesh->vertex->v[0];
1087 m.vertexstep = sizeof(surfvertex_t);
1088 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1090 VectorSubtract(v->v, r_origin, diff);
1091 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1092 sv->c[0] = m.cr * ifog;
1093 sv->c[1] = m.cg * ifog;
1094 sv->c[2] = m.cb * ifog;
1101 if (softwaretransform_complexity)
1103 m.vertex = &svert[0].v[0];
1104 m.vertexstep = sizeof(surfvert_t);
1105 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1106 softwaretransform(v->v, sv->v);
1110 m.vertex = &mesh->vertex->v[0];
1111 m.vertexstep = sizeof(surfvertex_t);
1118 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1121 float c[3], base[3], scale, diff[3], ifog;
1128 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1130 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1132 memset(&m, 0, sizeof(m));
1133 if (currentrenderentity->effects & EF_ADDITIVE)
1135 m.transparent = true;
1136 m.blendfunc1 = GL_SRC_ALPHA;
1137 m.blendfunc2 = GL_ONE;
1139 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1141 m.transparent = true;
1142 m.blendfunc1 = GL_SRC_ALPHA;
1143 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1147 m.transparent = false;
1148 m.blendfunc1 = GL_ONE;
1149 m.blendfunc2 = GL_ZERO;
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(surf->currenttexture->texture);
1156 m.texcoordstep[0] = sizeof(surfvertex_t);
1157 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1159 m.numtriangles = mesh->numtriangles;
1160 m.numverts = mesh->numverts;
1161 m.index = mesh->index;
1162 m.texcoords[0] = &mesh->vertex->st[0];
1163 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1165 softwaretransform(v->v, sv->v);
1166 VectorCopy(base, c);
1167 if (surf->styles[0] != 255)
1169 lm = surf->samples + v->lightmapoffset;
1170 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1171 VectorMA(c, scale, lm, c);
1172 if (surf->styles[1] != 255)
1175 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1176 VectorMA(c, scale, lm, c);
1177 if (surf->styles[2] != 255)
1180 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1181 VectorMA(c, scale, lm, c);
1182 if (surf->styles[3] != 255)
1185 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1186 VectorMA(c, scale, lm, c);
1194 sv->c[3] = currentrenderentity->alpha;
1196 if (surf->dlightframe == r_framecount)
1197 RSurf_Light(surf->dlightbits, m.numverts);
1200 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1202 VectorSubtract(sv->v, r_origin, diff);
1203 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1213 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1216 float diff[3], ifog;
1222 memset(&m, 0, sizeof(m));
1223 if (currentrenderentity->effects & EF_ADDITIVE)
1225 m.transparent = true;
1226 m.blendfunc1 = GL_SRC_ALPHA;
1227 m.blendfunc2 = GL_ONE;
1229 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1231 m.transparent = true;
1232 m.blendfunc1 = GL_SRC_ALPHA;
1233 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1237 m.transparent = false;
1238 m.blendfunc1 = GL_ONE;
1239 m.blendfunc2 = GL_ZERO;
1241 m.vertex = &svert[0].v[0];
1242 m.vertexstep = sizeof(surfvert_t);
1243 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1244 m.texcoordstep[0] = sizeof(surfvertex_t);
1245 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1247 m.numtriangles = mesh->numtriangles;
1248 m.numverts = mesh->numverts;
1249 m.index = mesh->index;
1250 m.texcoords[0] = &mesh->vertex->st[0];
1253 m.color = &svert[0].c[0];
1254 m.colorstep = sizeof(surfvert_t);
1255 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1257 softwaretransform(v->v, sv->v);
1258 VectorSubtract(sv->v, r_origin, diff);
1259 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1263 sv->c[3] = currentrenderentity->alpha;
1268 m.cr = m.cg = m.cb = 1;
1269 m.ca = currentrenderentity->alpha;
1270 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1271 softwaretransform(v->v, sv->v);
1277 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1280 float diff[3], ifog;
1286 memset(&m, 0, sizeof(m));
1287 if (currentrenderentity->effects & EF_ADDITIVE)
1288 m.transparent = true;
1289 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1290 m.transparent = true;
1292 m.transparent = false;
1293 m.blendfunc1 = GL_SRC_ALPHA;
1294 m.blendfunc2 = GL_ONE;
1295 m.vertex = &svert[0].v[0];
1296 m.vertexstep = sizeof(surfvert_t);
1297 m.color = &svert[0].c[0];
1298 m.colorstep = sizeof(surfvert_t);
1299 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1300 m.texcoordstep[0] = sizeof(surfvertex_t);
1301 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1303 m.numtriangles = mesh->numtriangles;
1304 m.numverts = mesh->numverts;
1305 m.index = mesh->index;
1306 m.texcoords[0] = &mesh->vertex->st[0];
1307 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1309 softwaretransform(v->v, sv->v);
1313 sv->c[3] = currentrenderentity->alpha;
1315 if (RSurf_Light(surf->dlightbits, m.numverts))
1319 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1321 VectorSubtract(sv->v, r_origin, diff);
1322 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1333 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1336 float diff[3], ifog;
1342 memset(&m, 0, sizeof(m));
1343 if (currentrenderentity->effects & EF_ADDITIVE)
1344 m.transparent = true;
1345 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1346 m.transparent = true;
1348 m.transparent = false;
1349 m.blendfunc1 = GL_SRC_ALPHA;
1350 m.blendfunc2 = GL_ONE;
1354 m.ca = currentrenderentity->alpha;
1355 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1356 m.texcoordstep[0] = sizeof(surfvertex_t);
1357 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1359 m.numtriangles = mesh->numtriangles;
1360 m.numverts = mesh->numverts;
1361 m.index = mesh->index;
1362 m.texcoords[0] = &mesh->vertex->st[0];
1365 m.color = &svert[0].c[0];
1366 m.colorstep = sizeof(surfvert_t);
1367 if (softwaretransform_complexity)
1369 m.vertex = &svert[0].v[0];
1370 m.vertexstep = sizeof(surfvert_t);
1371 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1373 softwaretransform(v->v, sv->v);
1374 VectorSubtract(sv->v, r_origin, diff);
1375 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1376 sv->c[0] = m.cr * ifog;
1377 sv->c[1] = m.cg * ifog;
1378 sv->c[2] = m.cb * ifog;
1384 m.vertex = &mesh->vertex->v[0];
1385 m.vertexstep = sizeof(surfvertex_t);
1386 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1388 VectorSubtract(v->v, r_origin, diff);
1389 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1390 sv->c[0] = m.cr * ifog;
1391 sv->c[1] = m.cg * ifog;
1392 sv->c[2] = m.cb * ifog;
1399 if (softwaretransform_complexity)
1401 m.vertex = &svert[0].v[0];
1402 m.vertexstep = sizeof(surfvert_t);
1403 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1404 softwaretransform(v->v, sv->v);
1408 m.vertex = &mesh->vertex->v[0];
1409 m.vertexstep = sizeof(surfvertex_t);
1416 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1425 memset(&m, 0, sizeof(m));
1426 if (currentrenderentity->effects & EF_ADDITIVE)
1427 m.transparent = true;
1428 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1429 m.transparent = true;
1431 m.transparent = false;
1432 m.blendfunc1 = GL_SRC_ALPHA;
1433 m.blendfunc2 = GL_ONE;
1434 m.color = &svert[0].c[0];
1435 m.colorstep = sizeof(surfvert_t);
1436 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1437 m.texcoordstep[0] = sizeof(surfvertex_t);
1438 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1440 m.numtriangles = mesh->numtriangles;
1441 m.numverts = mesh->numverts;
1442 m.index = mesh->index;
1443 m.texcoords[0] = &mesh->vertex->st[0];
1444 if (softwaretransform_complexity)
1446 m.vertex = &svert[0].v[0];
1447 m.vertexstep = sizeof(surfvert_t);
1448 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1450 softwaretransform(v->v, sv->v);
1451 VectorSubtract(sv->v, r_origin, diff);
1452 sv->c[0] = fogcolor[0];
1453 sv->c[1] = fogcolor[1];
1454 sv->c[2] = fogcolor[2];
1455 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1460 m.vertex = &mesh->vertex->v[0];
1461 m.vertexstep = sizeof(surfvertex_t);
1462 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1464 VectorSubtract(v->v, r_origin, diff);
1465 sv->c[0] = fogcolor[0];
1466 sv->c[1] = fogcolor[1];
1467 sv->c[2] = fogcolor[2];
1468 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1475 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1478 for (surf = firstsurf;surf;surf = surf->chain)
1481 RSurfShader_Wall_Pass_BaseFullbright(surf);
1483 for (surf = firstsurf;surf;surf = surf->chain)
1484 if (surf->currenttexture->glowtexture)
1485 RSurfShader_Wall_Pass_Glow(surf);
1487 for (surf = firstsurf;surf;surf = surf->chain)
1488 RSurfShader_Wall_Pass_Fog(surf);
1491 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1494 for (surf = firstsurf;surf;surf = surf->chain)
1497 RSurfShader_Wall_Pass_BaseVertex(surf);
1499 for (surf = firstsurf;surf;surf = surf->chain)
1500 if (surf->currenttexture->glowtexture)
1501 RSurfShader_Wall_Pass_Glow(surf);
1503 for (surf = firstsurf;surf;surf = surf->chain)
1504 RSurfShader_Wall_Pass_Fog(surf);
1507 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1510 if (r_vertexsurfaces.integer)
1512 for (surf = firstsurf;surf;surf = surf->chain)
1515 RSurfShader_Wall_Pass_BaseVertex(surf);
1517 for (surf = firstsurf;surf;surf = surf->chain)
1518 if (surf->currenttexture->glowtexture)
1519 RSurfShader_Wall_Pass_Glow(surf);
1521 for (surf = firstsurf;surf;surf = surf->chain)
1522 RSurfShader_Wall_Pass_Fog(surf);
1524 else if (r_multitexture.integer)
1526 if (r_dlightmap.integer)
1528 for (surf = firstsurf;surf;surf = surf->chain)
1531 RSurfShader_Wall_Pass_BaseMTex(surf);
1533 for (surf = firstsurf;surf;surf = surf->chain)
1534 if (surf->currenttexture->glowtexture)
1535 RSurfShader_Wall_Pass_Glow(surf);
1537 for (surf = firstsurf;surf;surf = surf->chain)
1538 RSurfShader_Wall_Pass_Fog(surf);
1542 for (surf = firstsurf;surf;surf = surf->chain)
1545 RSurfShader_Wall_Pass_BaseMTex(surf);
1547 for (surf = firstsurf;surf;surf = surf->chain)
1548 if (surf->dlightframe == r_framecount)
1549 RSurfShader_Wall_Pass_Light(surf);
1550 for (surf = firstsurf;surf;surf = surf->chain)
1551 if (surf->currenttexture->glowtexture)
1552 RSurfShader_Wall_Pass_Glow(surf);
1554 for (surf = firstsurf;surf;surf = surf->chain)
1555 RSurfShader_Wall_Pass_Fog(surf);
1558 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1560 for (surf = firstsurf;surf;surf = surf->chain)
1563 RSurfShader_Wall_Pass_BaseVertex(surf);
1565 for (surf = firstsurf;surf;surf = surf->chain)
1566 if (surf->currenttexture->glowtexture)
1567 RSurfShader_Wall_Pass_Glow(surf);
1569 for (surf = firstsurf;surf;surf = surf->chain)
1570 RSurfShader_Wall_Pass_Fog(surf);
1574 if (r_dlightmap.integer)
1576 for (surf = firstsurf;surf;surf = surf->chain)
1579 RSurfShader_Wall_Pass_BaseTexture(surf);
1581 for (surf = firstsurf;surf;surf = surf->chain)
1582 RSurfShader_Wall_Pass_BaseLightmap(surf);
1583 for (surf = firstsurf;surf;surf = surf->chain)
1584 if (surf->currenttexture->glowtexture)
1585 RSurfShader_Wall_Pass_Glow(surf);
1587 for (surf = firstsurf;surf;surf = surf->chain)
1588 RSurfShader_Wall_Pass_Fog(surf);
1592 for (surf = firstsurf;surf;surf = surf->chain)
1595 RSurfShader_Wall_Pass_BaseTexture(surf);
1597 for (surf = firstsurf;surf;surf = surf->chain)
1598 RSurfShader_Wall_Pass_BaseLightmap(surf);
1599 for (surf = firstsurf;surf;surf = surf->chain)
1600 if (surf->dlightframe == r_framecount)
1601 RSurfShader_Wall_Pass_Light(surf);
1602 for (surf = firstsurf;surf;surf = surf->chain)
1603 if (surf->currenttexture->glowtexture)
1604 RSurfShader_Wall_Pass_Glow(surf);
1606 for (surf = firstsurf;surf;surf = surf->chain)
1607 RSurfShader_Wall_Pass_Fog(surf);
1613 =============================================================
1617 =============================================================
1620 static void RSurf_Callback(void *data, void *junk)
1622 ((msurface_t *)data)->visframe = r_framecount;
1625 static void R_SolidWorldNode (void)
1627 if (r_viewleaf->contents != CONTENTS_SOLID)
1630 mportal_t *p, *pstack[8192];
1631 msurface_t *surf, **mark, **endmark;
1634 // LordHavoc: portal-passage worldnode; follows portals leading
1635 // outward from viewleaf, if a portal leads offscreen it is not
1636 // followed, in indoor maps this can often cull a great deal of
1637 // geometry away when pvs data is not present (useful with pvs as well)
1640 leaf->worldnodeframe = r_framecount;
1645 leaf->visframe = r_framecount;
1647 if (leaf->nummarksurfaces)
1649 mark = leaf->firstmarksurface;
1650 endmark = mark + leaf->nummarksurfaces;
1656 // make sure surfaces are only processed once
1657 if (surf->worldnodeframe == r_framecount)
1659 surf->worldnodeframe = r_framecount;
1660 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1662 if (surf->flags & SURF_PLANEBACK)
1664 VectorNegate(surf->plane->normal, plane.normal);
1665 plane.dist = -surf->plane->dist;
1666 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1671 if (!(surf->flags & SURF_PLANEBACK))
1672 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);
1675 while (mark < endmark);
1682 // make sure surfaces are only processed once
1683 if (surf->worldnodeframe == r_framecount)
1685 surf->worldnodeframe = r_framecount;
1686 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1688 if (surf->flags & SURF_PLANEBACK)
1689 surf->visframe = r_framecount;
1693 if (!(surf->flags & SURF_PLANEBACK))
1694 surf->visframe = r_framecount;
1697 while (mark < endmark);
1701 // follow portals into other leafs
1703 for (;p;p = p->next)
1705 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1708 if (leaf->worldnodeframe != r_framecount)
1710 leaf->worldnodeframe = r_framecount;
1711 if (leaf->contents != CONTENTS_SOLID)
1713 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1715 p->visframe = r_framecount;
1716 pstack[portalstack++] = p;
1720 p = pstack[--portalstack];
1732 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1733 int nodestackpos = 0;
1734 // LordHavoc: recursive descending worldnode; if portals are not
1735 // available, this is a good last resort, can cull large amounts of
1736 // geometry, but is more time consuming than portal-passage and renders
1737 // things behind walls
1740 if (R_NotCulledBox(node->mins, node->maxs))
1742 if (node->numsurfaces)
1746 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1748 if (PlaneDiff (r_origin, node->plane) < 0)
1750 for (;surf < surfend;surf++)
1752 if (surf->flags & SURF_PLANEBACK)
1754 VectorNegate(surf->plane->normal, plane.normal);
1755 plane.dist = -surf->plane->dist;
1756 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1762 for (;surf < surfend;surf++)
1764 if (!(surf->flags & SURF_PLANEBACK))
1765 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);
1771 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1772 if (PlaneDiff (r_origin, node->plane) < 0)
1774 for (;surf < surfend;surf++)
1776 if (surf->flags & SURF_PLANEBACK)
1777 surf->visframe = r_framecount;
1782 for (;surf < surfend;surf++)
1784 if (!(surf->flags & SURF_PLANEBACK))
1785 surf->visframe = r_framecount;
1791 // recurse down the children
1792 if (node->children[0]->contents >= 0)
1794 if (node->children[1]->contents >= 0)
1796 if (nodestackpos < 8192)
1797 nodestack[nodestackpos++] = node->children[1];
1798 node = node->children[0];
1802 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1803 node = node->children[0];
1808 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1809 if (node->children[1]->contents >= 0)
1811 node = node->children[1];
1814 else if (nodestackpos > 0)
1816 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1817 node = nodestack[--nodestackpos];
1822 else if (nodestackpos > 0)
1824 node = nodestack[--nodestackpos];
1830 static int r_portalframecount = 0;
1832 static void R_PVSWorldNode()
1835 mportal_t *p, *pstack[8192];
1836 msurface_t *surf, **mark, **endmark;
1841 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1844 leaf->worldnodeframe = r_framecount;
1849 leaf->visframe = r_framecount;
1851 if (leaf->nummarksurfaces)
1853 mark = leaf->firstmarksurface;
1854 endmark = mark + leaf->nummarksurfaces;
1860 // make sure surfaces are only processed once
1861 if (surf->worldnodeframe == r_framecount)
1863 surf->worldnodeframe = r_framecount;
1864 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1866 if (surf->flags & SURF_PLANEBACK)
1868 VectorNegate(surf->plane->normal, plane.normal);
1869 plane.dist = -surf->plane->dist;
1870 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1875 if (!(surf->flags & SURF_PLANEBACK))
1876 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);
1879 while (mark < endmark);
1886 // make sure surfaces are only processed once
1887 if (surf->worldnodeframe == r_framecount)
1889 surf->worldnodeframe = r_framecount;
1890 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1892 if (surf->flags & SURF_PLANEBACK)
1893 surf->visframe = r_framecount;
1897 if (!(surf->flags & SURF_PLANEBACK))
1898 surf->visframe = r_framecount;
1901 while (mark < endmark);
1905 // follow portals into other leafs
1906 for (p = leaf->portals;p;p = p->next)
1908 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1911 if (leaf->worldnodeframe != r_framecount)
1913 leaf->worldnodeframe = r_framecount;
1914 if (leaf->contents != CONTENTS_SOLID)
1916 i = (leaf - cl.worldmodel->leafs) - 1;
1917 if (worldvis[i>>3] & (1<<(i&7)))
1919 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1921 pstack[portalstack++] = p;
1925 p = pstack[--portalstack];
1937 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1938 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1939 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1940 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1941 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1943 int Cshader_count = 5;
1944 Cshader_t *Cshaders[5] =
1946 &Cshader_wall_vertex,
1947 &Cshader_wall_lightmap,
1948 &Cshader_wall_fullbright,
1953 void R_PrepareSurfaces(void)
1960 for (i = 0;i < Cshader_count;i++)
1961 Cshaders[i]->chain = NULL;
1963 model = currentrenderentity->model;
1965 for (i = 0;i < model->nummodelsurfaces;i++)
1967 surf = model->modelsortedsurfaces[i];
1968 if (surf->visframe == r_framecount)
1970 if (surf->insertframe != r_framecount)
1972 surf->insertframe = r_framecount;
1974 // manually inlined R_TextureAnimation
1975 //t = R_TextureAnimation(surf->texinfo->texture);
1976 t = surf->texinfo->texture;
1977 if (t->alternate_anims != NULL && currentrenderentity->frame)
1978 t = t->alternate_anims;
1979 if (t->anim_total >= 2)
1980 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1981 surf->currenttexture = t;
1984 surf->chain = surf->shader->chain;
1985 surf->shader->chain = surf;
1990 void R_DrawSurfaces (int type)
1995 for (i = 0;i < Cshader_count;i++)
1997 shader = Cshaders[i];
1998 if (shader->chain && shader->shaderfunc[type])
1999 shader->shaderfunc[type](shader->chain);
2003 static float portalpointbuffer[256][3];
2005 void R_DrawPortals(void)
2008 // mleaf_t *leaf, *endleaf;
2009 mportal_t *portal, *endportal;
2010 mvertex_t *point/*, *endpoint*/;
2012 drawportals = r_drawportals.integer;
2013 if (drawportals < 1)
2016 leaf = cl.worldmodel->leafs;
2017 endleaf = leaf + cl.worldmodel->numleafs;
2018 for (;leaf < endleaf;leaf++)
2020 if (leaf->visframe == r_framecount && leaf->portals)
2022 i = leaf - cl.worldmodel->leafs;
2023 r = (i & 0x0007) << 5;
2024 g = (i & 0x0038) << 2;
2025 b = (i & 0x01C0) >> 1;
2026 portal = leaf->portals;
2029 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2030 point = portal->points + portal->numpoints - 1;
2031 endpoint = portal->points;
2032 for (;point >= endpoint;point--)
2033 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2035 portal = portal->next;
2040 memset(&m, 0, sizeof(m));
2041 m.transparent = true;
2042 m.blendfunc1 = GL_SRC_ALPHA;
2043 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2044 m.vertex = &portalpointbuffer[0][0];
2045 m.vertexstep = sizeof(float[3]);
2047 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2049 if (portal->visframe == r_portalframecount)
2051 if (portal->numpoints <= 256)
2053 i = portal - cl.worldmodel->portals;
2054 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2055 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2056 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2057 point = portal->points;
2058 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2060 for (i = portal->numpoints - 1;i >= 0;i--)
2061 VectorCopy(point[i].position, portalpointbuffer[i]);
2065 for (i = 0;i < portal->numpoints;i++)
2066 VectorCopy(point[i].position, portalpointbuffer[i]);
2068 R_Mesh_DrawPolygon(&m, portal->numpoints);
2074 void R_SetupForBModelRendering(void)
2081 // because bmodels can be reused, we have to decide which things to render
2082 // from scratch every time
2084 model = currentrenderentity->model;
2086 softwaretransformforentity (currentrenderentity);
2087 softwareuntransform(r_origin, modelorg);
2089 for (i = 0;i < model->nummodelsurfaces;i++)
2091 surf = model->modelsortedsurfaces[i];
2092 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2093 surf->visframe = r_framecount;
2095 surf->visframe = -1;
2096 surf->worldnodeframe = -1;
2097 surf->lightframe = -1;
2098 surf->dlightframe = -1;
2099 surf->insertframe = -1;
2103 void R_SetupForWorldRendering(void)
2105 // there is only one instance of the world, but it can be rendered in
2108 currentrenderentity = &cl_entities[0].render;
2109 softwaretransformidentity();
2112 static void R_SurfMarkLights (void)
2117 if (r_dynamic.integer)
2120 if (!r_vertexsurfaces.integer)
2122 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2124 surf = currentrenderentity->model->modelsortedsurfaces[i];
2125 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2127 if (surf->cached_dlight
2128 || surf->cached_ambient != r_ambient.value
2129 || surf->cached_lightscalebit != lightscalebit)
2130 R_BuildLightMap(surf, false); // base lighting changed
2131 else if (r_dynamic.integer)
2133 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2134 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2135 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2136 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2137 //if (surf->cached_light[0] != d_lightstylevalue[surf->styles[0]]
2138 // || surf->cached_light[1] != d_lightstylevalue[surf->styles[1]]
2139 // || surf->cached_light[2] != d_lightstylevalue[surf->styles[2]]
2140 // || surf->cached_light[3] != d_lightstylevalue[surf->styles[3]])
2141 R_BuildLightMap(surf, false); // base lighting changed
2142 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2143 R_BuildLightMap(surf, true); // only dlights
2150 void R_MarkWorldLights(void)
2152 R_SetupForWorldRendering();
2161 void R_DrawWorld (void)
2163 R_SetupForWorldRendering();
2165 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2166 R_SolidWorldNode ();
2176 void R_DrawBrushModelSky (void)
2178 R_SetupForBModelRendering();
2180 R_PrepareSurfaces();
2181 R_DrawSurfaces(SHADERSTAGE_SKY);
2184 void R_DrawBrushModelNormal (void)
2188 // have to flush queue because of possible lightmap reuse
2191 R_SetupForBModelRendering();
2195 R_PrepareSurfaces();
2197 if (!skyrendermasked)
2198 R_DrawSurfaces(SHADERSTAGE_SKY);
2199 R_DrawSurfaces(SHADERSTAGE_NORMAL);