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 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
489 #define MAX_SURFVERTS 3072
498 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
500 static void RSurfShader_Sky(msurface_t *firstsurf)
504 float number, length, dir[3], speedscale;
510 // LordHavoc: HalfLife maps have freaky skypolys...
511 if (currentrenderentity->model->ishlbsp)
518 skyrendernow = false;
521 for (surf = firstsurf;surf;surf = surf->chain)
523 // draw depth-only polys
524 memset(&m, 0, sizeof(m));
525 m.transparent = false;
526 m.blendfunc1 = GL_ZERO;
527 m.blendfunc2 = GL_ONE;
529 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
531 m.numtriangles = mesh->numtriangles;
532 m.numverts = mesh->numverts;
533 m.index = mesh->index;
538 if (softwaretransform_complexity)
540 m.vertex = &svert[0].v[0];
541 m.vertexstep = sizeof(surfvert_t);
542 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
543 softwaretransform(v->v, sv->v);
547 m.vertex = &mesh->vertex[0].v[0];
548 m.vertexstep = sizeof(surfvertex_t);
554 else if (skyrenderglquake)
556 for (surf = firstsurf;surf;surf = surf->chain)
558 memset(&m, 0, sizeof(m));
559 m.transparent = false;
560 m.blendfunc1 = GL_ONE;
561 m.blendfunc2 = GL_ZERO;
562 m.vertex = &svert[0].v[0];
563 m.vertexstep = sizeof(surfvert_t);
568 m.tex[0] = R_GetTexture(solidskytexture);
569 m.texcoords[0] = &svert[0].st[0];
570 m.texcoordstep[0] = sizeof(surfvert_t);
571 speedscale = cl.time * (8.0/128.0);
572 speedscale -= (int)speedscale;
573 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
575 m.numtriangles = mesh->numtriangles;
576 m.numverts = mesh->numverts;
577 m.index = mesh->index;
578 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
580 softwaretransform(v->v, sv->v);
581 VectorSubtract (sv->v, r_origin, dir);
582 // flatten the sphere
585 number = DotProduct(dir, dir);
587 length = 3.0f / sqrt(number);
589 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
590 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
593 sv->st[0] = speedscale + dir[0] * length;
594 sv->st[1] = speedscale + dir[1] * length;
602 for (surf = firstsurf;surf;surf = surf->chain)
605 memset(&m, 0, sizeof(m));
606 m.transparent = false;
607 m.blendfunc1 = GL_ONE;
608 m.blendfunc2 = GL_ZERO;
613 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
615 m.numtriangles = mesh->numtriangles;
616 m.numverts = mesh->numverts;
617 m.index = mesh->index;
618 if (softwaretransform_complexity)
620 m.vertex = &svert[0].v[0];
621 m.vertexstep = sizeof(surfvert_t);
622 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
623 softwaretransform(v->v, sv->v);
627 m.vertex = &mesh->vertex[0].v[0];
628 m.vertexstep = sizeof(surfvertex_t);
634 if (skyrenderglquake)
636 for (surf = firstsurf;surf;surf = surf->chain)
638 memset(&m, 0, sizeof(m));
639 m.transparent = false;
640 m.blendfunc1 = GL_SRC_ALPHA;
641 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
642 m.vertex = &svert[0].v[0];
643 m.vertexstep = sizeof(surfvert_t);
648 m.tex[0] = R_GetTexture(alphaskytexture);
649 m.texcoords[0] = &svert[0].st[0];
650 m.texcoordstep[0] = sizeof(surfvert_t);
651 speedscale = cl.time * (16.0/128.0);
652 speedscale -= (int)speedscale;
653 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
655 m.numtriangles = mesh->numtriangles;
656 m.numverts = mesh->numverts;
657 m.index = mesh->index;
658 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
660 softwaretransform(v->v, sv->v);
661 VectorSubtract (sv->v, r_origin, dir);
662 // flatten the sphere
665 number = DotProduct(dir, dir);
667 length = 3.0f / sqrt(number);
669 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
670 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
673 sv->st[0] = speedscale + dir[0] * length;
674 sv->st[1] = speedscale + dir[1] * length;
682 static int RSurf_Light(int *dlightbits, int numverts)
685 int i, l, lit = false;
689 for (l = 0;l < r_numdlights;l++)
691 if (dlightbits[l >> 5] & (1 << (l & 31)))
694 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
695 VectorCopy(rd->origin, lightorigin);
696 for (i = 0, sv = svert;i < numverts;i++, sv++)
698 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
699 if (f < rd->cullradius2)
701 f = (1.0f / f) - rd->lightsubtract;
702 sv->c[0] += rd->light[0] * f;
703 sv->c[1] += rd->light[1] * f;
704 sv->c[2] += rd->light[2] * f;
713 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
716 float diff[3], alpha, ifog;
721 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
723 memset(&m, 0, sizeof(m));
724 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
726 m.transparent = true;
727 m.blendfunc1 = GL_SRC_ALPHA;
728 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
732 m.transparent = false;
733 m.blendfunc1 = GL_ONE;
734 m.blendfunc2 = GL_ZERO;
736 m.vertex = &svert[0].v[0];
737 m.vertexstep = sizeof(surfvert_t);
738 m.color = &svert[0].c[0];
739 m.colorstep = sizeof(surfvert_t);
740 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
741 m.texcoords[0] = &svert[0].st[0];
742 m.texcoordstep[0] = sizeof(surfvert_t);
743 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
745 m.numtriangles = mesh->numtriangles;
746 m.numverts = mesh->numverts;
747 m.index = mesh->index;
748 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
750 softwaretransform(v->v, sv->v);
751 if (r_waterripple.value)
752 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];
753 if (surf->flags & SURF_DRAWFULLBRIGHT)
767 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
768 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
770 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
771 RSurf_Light(surf->dlightbits, m.numverts);
772 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
774 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
776 VectorSubtract(sv->v, r_origin, diff);
777 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
787 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
790 float diff[3], alpha, ifog;
795 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
797 memset(&m, 0, sizeof(m));
798 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
799 m.blendfunc1 = GL_SRC_ALPHA;
800 m.blendfunc2 = GL_ONE;
801 m.vertex = &svert[0].v[0];
802 m.vertexstep = sizeof(surfvert_t);
807 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
808 m.texcoords[0] = &svert[0].st[0];
809 m.texcoordstep[0] = sizeof(surfvert_t);
810 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
812 m.numtriangles = mesh->numtriangles;
813 m.numverts = mesh->numverts;
814 m.index = mesh->index;
817 m.color = &svert[0].c[0];
818 m.colorstep = sizeof(surfvert_t);
819 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
821 softwaretransform(v->v, sv->v);
822 if (r_waterripple.value)
823 sv->v[2] += r_waterripple.value * (1.0f / 64.0f) * turbsin[(int)((v->v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v->v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255];
824 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
825 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
826 VectorSubtract(sv->v, r_origin, diff);
827 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
828 sv->c[0] = m.cr * ifog;
829 sv->c[1] = m.cg * ifog;
830 sv->c[2] = m.cb * ifog;
836 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
838 softwaretransform(v->v, sv->v);
839 if (r_waterripple.value)
840 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];
841 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
842 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
849 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
858 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
860 memset(&m, 0, sizeof(m));
861 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
862 m.blendfunc1 = GL_SRC_ALPHA;
863 m.blendfunc2 = GL_ONE;
864 m.vertex = &svert[0].v[0];
865 m.vertexstep = sizeof(surfvert_t);
866 m.color = &svert[0].c[0];
867 m.colorstep = sizeof(surfvert_t);
868 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
869 m.texcoords[0] = &svert[0].st[0];
870 m.texcoordstep[0] = sizeof(surfvert_t);
872 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
874 m.numtriangles = mesh->numtriangles;
875 m.numverts = mesh->numverts;
876 m.index = mesh->index;
877 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
879 softwaretransform(v->v, sv->v);
880 if (r_waterripple.value)
881 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];
884 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
885 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
887 VectorSubtract(sv->v, r_origin, diff);
888 sv->c[0] = fogcolor[0];
889 sv->c[1] = fogcolor[1];
890 sv->c[2] = fogcolor[2];
891 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
897 static void RSurfShader_Water(msurface_t *firstsurf)
900 for (surf = firstsurf;surf;surf = surf->chain)
901 RSurfShader_Water_Pass_Base(surf);
902 for (surf = firstsurf;surf;surf = surf->chain)
903 if (surf->currenttexture->glowtexture)
904 RSurfShader_Water_Pass_Glow(surf);
906 for (surf = firstsurf;surf;surf = surf->chain)
907 //if (currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value) >= 1.0f)
908 RSurfShader_Water_Pass_Fog(surf);
911 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
920 memset(&m, 0, sizeof(m));
921 if (currentrenderentity->effects & EF_ADDITIVE)
923 m.transparent = true;
924 m.blendfunc1 = GL_SRC_ALPHA;
925 m.blendfunc2 = GL_ONE;
927 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
929 m.transparent = true;
930 m.blendfunc1 = GL_SRC_ALPHA;
931 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
935 m.transparent = false;
936 m.blendfunc1 = GL_ONE;
937 m.blendfunc2 = GL_ZERO;
939 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
940 m.ca = currentrenderentity->alpha;
941 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
942 m.tex[1] = R_GetTexture(surf->lightmaptexture);
943 m.texcoordstep[0] = sizeof(surfvertex_t);
944 m.texcoordstep[1] = sizeof(surfvertex_t);
945 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
947 m.numtriangles = mesh->numtriangles;
948 m.numverts = mesh->numverts;
949 m.index = mesh->index;
950 m.texcoords[0] = &mesh->vertex->st[0];
951 m.texcoords[1] = &mesh->vertex->uv[0];
954 m.color = &svert[0].c[0];
955 m.colorstep = sizeof(surfvert_t);
956 if (softwaretransform_complexity)
958 m.vertex = &svert[0].v[0];
959 m.vertexstep = sizeof(surfvert_t);
960 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
962 softwaretransform(v->v, sv->v);
963 VectorSubtract(sv->v, r_origin, diff);
964 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
965 sv->c[0] = m.cr * ifog;
966 sv->c[1] = m.cg * ifog;
967 sv->c[2] = m.cb * ifog;
973 m.vertex = &mesh->vertex->v[0];
974 m.vertexstep = sizeof(surfvertex_t);
975 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
977 VectorSubtract(v->v, r_origin, diff);
978 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
979 sv->c[0] = m.cr * ifog;
980 sv->c[1] = m.cg * ifog;
981 sv->c[2] = m.cb * ifog;
988 if (softwaretransform_complexity)
990 m.vertex = &svert[0].v[0];
991 m.vertexstep = sizeof(surfvert_t);
992 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
993 softwaretransform(v->v, sv->v);
997 m.vertex = &mesh->vertex->v[0];
998 m.vertexstep = sizeof(surfvertex_t);
1005 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1013 memset(&m, 0, sizeof(m));
1014 m.transparent = false;
1015 m.blendfunc1 = GL_ONE;
1016 m.blendfunc2 = GL_ZERO;
1017 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1019 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1020 m.texcoordstep[0] = sizeof(surfvertex_t);
1021 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1023 m.numtriangles = mesh->numtriangles;
1024 m.numverts = mesh->numverts;
1025 m.index = mesh->index;
1026 m.texcoords[0] = &mesh->vertex->st[0];
1027 if (softwaretransform_complexity)
1029 m.vertex = &svert[0].v[0];
1030 m.vertexstep = sizeof(surfvert_t);
1031 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1032 softwaretransform(v->v, sv->v);
1036 m.vertex = &mesh->vertex->v[0];
1037 m.vertexstep = sizeof(surfvertex_t);
1043 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1046 float diff[3], ifog;
1052 memset(&m, 0, sizeof(m));
1053 m.transparent = false;
1054 m.blendfunc1 = GL_ZERO;
1055 m.blendfunc2 = GL_SRC_COLOR;
1056 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1058 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1059 m.texcoordstep[0] = sizeof(surfvertex_t);
1060 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1062 m.numtriangles = mesh->numtriangles;
1063 m.numverts = mesh->numverts;
1064 m.index = mesh->index;
1065 m.texcoords[0] = &mesh->vertex->uv[0];
1068 m.color = &svert[0].c[0];
1069 m.colorstep = sizeof(surfvert_t);
1070 if (softwaretransform_complexity)
1072 m.vertex = &svert[0].v[0];
1073 m.vertexstep = sizeof(surfvert_t);
1074 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1076 softwaretransform(v->v, sv->v);
1077 VectorSubtract(sv->v, r_origin, diff);
1078 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1079 sv->c[0] = m.cr * ifog;
1080 sv->c[1] = m.cg * ifog;
1081 sv->c[2] = m.cb * ifog;
1087 m.vertex = &mesh->vertex->v[0];
1088 m.vertexstep = sizeof(surfvertex_t);
1089 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1091 VectorSubtract(v->v, r_origin, diff);
1092 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1093 sv->c[0] = m.cr * ifog;
1094 sv->c[1] = m.cg * ifog;
1095 sv->c[2] = m.cb * ifog;
1102 if (softwaretransform_complexity)
1104 m.vertex = &svert[0].v[0];
1105 m.vertexstep = sizeof(surfvert_t);
1106 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1107 softwaretransform(v->v, sv->v);
1111 m.vertex = &mesh->vertex->v[0];
1112 m.vertexstep = sizeof(surfvertex_t);
1119 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1122 float c[3], base[3], scale, diff[3], ifog;
1129 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1131 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1133 memset(&m, 0, sizeof(m));
1134 if (currentrenderentity->effects & EF_ADDITIVE)
1136 m.transparent = true;
1137 m.blendfunc1 = GL_SRC_ALPHA;
1138 m.blendfunc2 = GL_ONE;
1140 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1142 m.transparent = true;
1143 m.blendfunc1 = GL_SRC_ALPHA;
1144 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1148 m.transparent = false;
1149 m.blendfunc1 = GL_ONE;
1150 m.blendfunc2 = GL_ZERO;
1152 m.vertex = &svert[0].v[0];
1153 m.vertexstep = sizeof(surfvert_t);
1154 m.color = &svert[0].c[0];
1155 m.colorstep = sizeof(surfvert_t);
1156 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1157 m.texcoordstep[0] = sizeof(surfvertex_t);
1158 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1160 m.numtriangles = mesh->numtriangles;
1161 m.numverts = mesh->numverts;
1162 m.index = mesh->index;
1163 m.texcoords[0] = &mesh->vertex->st[0];
1164 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1166 softwaretransform(v->v, sv->v);
1167 VectorCopy(base, c);
1168 if (surf->styles[0] != 255)
1170 lm = surf->samples + v->lightmapoffset;
1171 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1172 VectorMA(c, scale, lm, c);
1173 if (surf->styles[1] != 255)
1176 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1177 VectorMA(c, scale, lm, c);
1178 if (surf->styles[2] != 255)
1181 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1182 VectorMA(c, scale, lm, c);
1183 if (surf->styles[3] != 255)
1186 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1187 VectorMA(c, scale, lm, c);
1195 sv->c[3] = currentrenderentity->alpha;
1197 if (surf->dlightframe == r_framecount)
1198 RSurf_Light(surf->dlightbits, m.numverts);
1201 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1203 VectorSubtract(sv->v, r_origin, diff);
1204 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1214 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1217 float diff[3], ifog;
1223 memset(&m, 0, sizeof(m));
1224 if (currentrenderentity->effects & EF_ADDITIVE)
1226 m.transparent = true;
1227 m.blendfunc1 = GL_SRC_ALPHA;
1228 m.blendfunc2 = GL_ONE;
1230 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1232 m.transparent = true;
1233 m.blendfunc1 = GL_SRC_ALPHA;
1234 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1238 m.transparent = false;
1239 m.blendfunc1 = GL_ONE;
1240 m.blendfunc2 = GL_ZERO;
1242 m.vertex = &svert[0].v[0];
1243 m.vertexstep = sizeof(surfvert_t);
1244 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1245 m.texcoordstep[0] = sizeof(surfvertex_t);
1246 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1248 m.numtriangles = mesh->numtriangles;
1249 m.numverts = mesh->numverts;
1250 m.index = mesh->index;
1251 m.texcoords[0] = &mesh->vertex->st[0];
1254 m.color = &svert[0].c[0];
1255 m.colorstep = sizeof(surfvert_t);
1256 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1258 softwaretransform(v->v, sv->v);
1259 VectorSubtract(sv->v, r_origin, diff);
1260 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1264 sv->c[3] = currentrenderentity->alpha;
1269 m.cr = m.cg = m.cb = 1;
1270 m.ca = currentrenderentity->alpha;
1271 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1272 softwaretransform(v->v, sv->v);
1278 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1281 float diff[3], ifog;
1287 memset(&m, 0, sizeof(m));
1288 if (currentrenderentity->effects & EF_ADDITIVE)
1289 m.transparent = true;
1290 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1291 m.transparent = true;
1293 m.transparent = false;
1294 m.blendfunc1 = GL_SRC_ALPHA;
1295 m.blendfunc2 = GL_ONE;
1296 m.vertex = &svert[0].v[0];
1297 m.vertexstep = sizeof(surfvert_t);
1298 m.color = &svert[0].c[0];
1299 m.colorstep = sizeof(surfvert_t);
1300 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1301 m.texcoordstep[0] = sizeof(surfvertex_t);
1302 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1304 m.numtriangles = mesh->numtriangles;
1305 m.numverts = mesh->numverts;
1306 m.index = mesh->index;
1307 m.texcoords[0] = &mesh->vertex->st[0];
1308 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1310 softwaretransform(v->v, sv->v);
1314 sv->c[3] = currentrenderentity->alpha;
1316 if (RSurf_Light(surf->dlightbits, m.numverts))
1320 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1322 VectorSubtract(sv->v, r_origin, diff);
1323 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1334 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1337 float diff[3], ifog;
1343 memset(&m, 0, sizeof(m));
1344 if (currentrenderentity->effects & EF_ADDITIVE)
1345 m.transparent = true;
1346 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1347 m.transparent = true;
1349 m.transparent = false;
1350 m.blendfunc1 = GL_SRC_ALPHA;
1351 m.blendfunc2 = GL_ONE;
1355 m.ca = currentrenderentity->alpha;
1356 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1357 m.texcoordstep[0] = sizeof(surfvertex_t);
1358 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1360 m.numtriangles = mesh->numtriangles;
1361 m.numverts = mesh->numverts;
1362 m.index = mesh->index;
1363 m.texcoords[0] = &mesh->vertex->st[0];
1366 m.color = &svert[0].c[0];
1367 m.colorstep = sizeof(surfvert_t);
1368 if (softwaretransform_complexity)
1370 m.vertex = &svert[0].v[0];
1371 m.vertexstep = sizeof(surfvert_t);
1372 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1374 softwaretransform(v->v, sv->v);
1375 VectorSubtract(sv->v, r_origin, diff);
1376 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1377 sv->c[0] = m.cr * ifog;
1378 sv->c[1] = m.cg * ifog;
1379 sv->c[2] = m.cb * ifog;
1385 m.vertex = &mesh->vertex->v[0];
1386 m.vertexstep = sizeof(surfvertex_t);
1387 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1389 VectorSubtract(v->v, r_origin, diff);
1390 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1391 sv->c[0] = m.cr * ifog;
1392 sv->c[1] = m.cg * ifog;
1393 sv->c[2] = m.cb * ifog;
1400 if (softwaretransform_complexity)
1402 m.vertex = &svert[0].v[0];
1403 m.vertexstep = sizeof(surfvert_t);
1404 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1405 softwaretransform(v->v, sv->v);
1409 m.vertex = &mesh->vertex->v[0];
1410 m.vertexstep = sizeof(surfvertex_t);
1417 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1426 memset(&m, 0, sizeof(m));
1427 if (currentrenderentity->effects & EF_ADDITIVE)
1428 m.transparent = true;
1429 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1430 m.transparent = true;
1432 m.transparent = false;
1433 m.blendfunc1 = GL_SRC_ALPHA;
1434 m.blendfunc2 = GL_ONE;
1435 m.color = &svert[0].c[0];
1436 m.colorstep = sizeof(surfvert_t);
1437 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1438 m.texcoordstep[0] = sizeof(surfvertex_t);
1439 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1441 m.numtriangles = mesh->numtriangles;
1442 m.numverts = mesh->numverts;
1443 m.index = mesh->index;
1444 m.texcoords[0] = &mesh->vertex->st[0];
1445 if (softwaretransform_complexity)
1447 m.vertex = &svert[0].v[0];
1448 m.vertexstep = sizeof(surfvert_t);
1449 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1451 softwaretransform(v->v, sv->v);
1452 VectorSubtract(sv->v, r_origin, diff);
1453 sv->c[0] = fogcolor[0];
1454 sv->c[1] = fogcolor[1];
1455 sv->c[2] = fogcolor[2];
1456 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1461 m.vertex = &mesh->vertex->v[0];
1462 m.vertexstep = sizeof(surfvertex_t);
1463 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1465 VectorSubtract(v->v, r_origin, diff);
1466 sv->c[0] = fogcolor[0];
1467 sv->c[1] = fogcolor[1];
1468 sv->c[2] = fogcolor[2];
1469 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1476 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1479 for (surf = firstsurf;surf;surf = surf->chain)
1482 RSurfShader_Wall_Pass_BaseFullbright(surf);
1484 for (surf = firstsurf;surf;surf = surf->chain)
1485 if (surf->currenttexture->glowtexture)
1486 RSurfShader_Wall_Pass_Glow(surf);
1488 for (surf = firstsurf;surf;surf = surf->chain)
1489 RSurfShader_Wall_Pass_Fog(surf);
1492 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1495 for (surf = firstsurf;surf;surf = surf->chain)
1498 RSurfShader_Wall_Pass_BaseVertex(surf);
1500 for (surf = firstsurf;surf;surf = surf->chain)
1501 if (surf->currenttexture->glowtexture)
1502 RSurfShader_Wall_Pass_Glow(surf);
1504 for (surf = firstsurf;surf;surf = surf->chain)
1505 RSurfShader_Wall_Pass_Fog(surf);
1508 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1511 if (r_vertexsurfaces.integer)
1513 for (surf = firstsurf;surf;surf = surf->chain)
1516 RSurfShader_Wall_Pass_BaseVertex(surf);
1518 for (surf = firstsurf;surf;surf = surf->chain)
1519 if (surf->currenttexture->glowtexture)
1520 RSurfShader_Wall_Pass_Glow(surf);
1522 for (surf = firstsurf;surf;surf = surf->chain)
1523 RSurfShader_Wall_Pass_Fog(surf);
1525 else if (r_multitexture.integer)
1527 if (r_dlightmap.integer)
1529 for (surf = firstsurf;surf;surf = surf->chain)
1532 RSurfShader_Wall_Pass_BaseMTex(surf);
1534 for (surf = firstsurf;surf;surf = surf->chain)
1535 if (surf->currenttexture->glowtexture)
1536 RSurfShader_Wall_Pass_Glow(surf);
1538 for (surf = firstsurf;surf;surf = surf->chain)
1539 RSurfShader_Wall_Pass_Fog(surf);
1543 for (surf = firstsurf;surf;surf = surf->chain)
1546 RSurfShader_Wall_Pass_BaseMTex(surf);
1548 for (surf = firstsurf;surf;surf = surf->chain)
1549 if (surf->dlightframe == r_framecount)
1550 RSurfShader_Wall_Pass_Light(surf);
1551 for (surf = firstsurf;surf;surf = surf->chain)
1552 if (surf->currenttexture->glowtexture)
1553 RSurfShader_Wall_Pass_Glow(surf);
1555 for (surf = firstsurf;surf;surf = surf->chain)
1556 RSurfShader_Wall_Pass_Fog(surf);
1559 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1561 for (surf = firstsurf;surf;surf = surf->chain)
1564 RSurfShader_Wall_Pass_BaseVertex(surf);
1566 for (surf = firstsurf;surf;surf = surf->chain)
1567 if (surf->currenttexture->glowtexture)
1568 RSurfShader_Wall_Pass_Glow(surf);
1570 for (surf = firstsurf;surf;surf = surf->chain)
1571 RSurfShader_Wall_Pass_Fog(surf);
1575 if (r_dlightmap.integer)
1577 for (surf = firstsurf;surf;surf = surf->chain)
1580 RSurfShader_Wall_Pass_BaseTexture(surf);
1582 for (surf = firstsurf;surf;surf = surf->chain)
1583 RSurfShader_Wall_Pass_BaseLightmap(surf);
1584 for (surf = firstsurf;surf;surf = surf->chain)
1585 if (surf->currenttexture->glowtexture)
1586 RSurfShader_Wall_Pass_Glow(surf);
1588 for (surf = firstsurf;surf;surf = surf->chain)
1589 RSurfShader_Wall_Pass_Fog(surf);
1593 for (surf = firstsurf;surf;surf = surf->chain)
1596 RSurfShader_Wall_Pass_BaseTexture(surf);
1598 for (surf = firstsurf;surf;surf = surf->chain)
1599 RSurfShader_Wall_Pass_BaseLightmap(surf);
1600 for (surf = firstsurf;surf;surf = surf->chain)
1601 if (surf->dlightframe == r_framecount)
1602 RSurfShader_Wall_Pass_Light(surf);
1603 for (surf = firstsurf;surf;surf = surf->chain)
1604 if (surf->currenttexture->glowtexture)
1605 RSurfShader_Wall_Pass_Glow(surf);
1607 for (surf = firstsurf;surf;surf = surf->chain)
1608 RSurfShader_Wall_Pass_Fog(surf);
1614 =============================================================
1618 =============================================================
1621 static void RSurf_Callback(void *data, void *junk)
1623 ((msurface_t *)data)->visframe = r_framecount;
1626 static void R_SolidWorldNode (void)
1628 if (r_viewleaf->contents != CONTENTS_SOLID)
1631 mportal_t *p, *pstack[8192];
1632 msurface_t *surf, **mark, **endmark;
1635 // LordHavoc: portal-passage worldnode; follows portals leading
1636 // outward from viewleaf, if a portal leads offscreen it is not
1637 // followed, in indoor maps this can often cull a great deal of
1638 // geometry away when pvs data is not present (useful with pvs as well)
1641 leaf->worldnodeframe = r_framecount;
1646 leaf->visframe = r_framecount;
1648 if (leaf->nummarksurfaces)
1650 mark = leaf->firstmarksurface;
1651 endmark = mark + leaf->nummarksurfaces;
1657 // make sure surfaces are only processed once
1658 if (surf->worldnodeframe == r_framecount)
1660 surf->worldnodeframe = r_framecount;
1661 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1663 if (surf->flags & SURF_PLANEBACK)
1665 VectorNegate(surf->plane->normal, plane.normal);
1666 plane.dist = -surf->plane->dist;
1667 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1672 if (!(surf->flags & SURF_PLANEBACK))
1673 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);
1676 while (mark < endmark);
1683 // make sure surfaces are only processed once
1684 if (surf->worldnodeframe == r_framecount)
1686 surf->worldnodeframe = r_framecount;
1687 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1689 if (surf->flags & SURF_PLANEBACK)
1690 surf->visframe = r_framecount;
1694 if (!(surf->flags & SURF_PLANEBACK))
1695 surf->visframe = r_framecount;
1698 while (mark < endmark);
1702 // follow portals into other leafs
1704 for (;p;p = p->next)
1706 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1709 if (leaf->worldnodeframe != r_framecount)
1711 leaf->worldnodeframe = r_framecount;
1712 if (leaf->contents != CONTENTS_SOLID)
1714 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1716 p->visframe = r_framecount;
1717 pstack[portalstack++] = p;
1721 p = pstack[--portalstack];
1733 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1734 int nodestackpos = 0;
1735 // LordHavoc: recursive descending worldnode; if portals are not
1736 // available, this is a good last resort, can cull large amounts of
1737 // geometry, but is more time consuming than portal-passage and renders
1738 // things behind walls
1741 if (R_NotCulledBox(node->mins, node->maxs))
1743 if (node->numsurfaces)
1747 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1749 if (PlaneDiff (r_origin, node->plane) < 0)
1751 for (;surf < surfend;surf++)
1753 if (surf->flags & SURF_PLANEBACK)
1755 VectorNegate(surf->plane->normal, plane.normal);
1756 plane.dist = -surf->plane->dist;
1757 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1763 for (;surf < surfend;surf++)
1765 if (!(surf->flags & SURF_PLANEBACK))
1766 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);
1772 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1773 if (PlaneDiff (r_origin, node->plane) < 0)
1775 for (;surf < surfend;surf++)
1777 if (surf->flags & SURF_PLANEBACK)
1778 surf->visframe = r_framecount;
1783 for (;surf < surfend;surf++)
1785 if (!(surf->flags & SURF_PLANEBACK))
1786 surf->visframe = r_framecount;
1792 // recurse down the children
1793 if (node->children[0]->contents >= 0)
1795 if (node->children[1]->contents >= 0)
1797 if (nodestackpos < 8192)
1798 nodestack[nodestackpos++] = node->children[1];
1799 node = node->children[0];
1803 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1804 node = node->children[0];
1809 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1810 if (node->children[1]->contents >= 0)
1812 node = node->children[1];
1815 else if (nodestackpos > 0)
1817 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1818 node = nodestack[--nodestackpos];
1823 else if (nodestackpos > 0)
1825 node = nodestack[--nodestackpos];
1831 static int r_portalframecount = 0;
1833 static void R_PVSWorldNode()
1836 mportal_t *p, *pstack[8192];
1837 msurface_t *surf, **mark, **endmark;
1842 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1845 leaf->worldnodeframe = r_framecount;
1850 leaf->visframe = r_framecount;
1852 if (leaf->nummarksurfaces)
1854 mark = leaf->firstmarksurface;
1855 endmark = mark + leaf->nummarksurfaces;
1861 // make sure surfaces are only processed once
1862 if (surf->worldnodeframe == r_framecount)
1864 surf->worldnodeframe = r_framecount;
1865 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1867 if (surf->flags & SURF_PLANEBACK)
1869 VectorNegate(surf->plane->normal, plane.normal);
1870 plane.dist = -surf->plane->dist;
1871 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1876 if (!(surf->flags & SURF_PLANEBACK))
1877 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);
1880 while (mark < endmark);
1887 // make sure surfaces are only processed once
1888 if (surf->worldnodeframe == r_framecount)
1890 surf->worldnodeframe = r_framecount;
1891 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1893 if (surf->flags & SURF_PLANEBACK)
1894 surf->visframe = r_framecount;
1898 if (!(surf->flags & SURF_PLANEBACK))
1899 surf->visframe = r_framecount;
1902 while (mark < endmark);
1906 // follow portals into other leafs
1907 for (p = leaf->portals;p;p = p->next)
1909 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1912 if (leaf->worldnodeframe != r_framecount)
1914 leaf->worldnodeframe = r_framecount;
1915 if (leaf->contents != CONTENTS_SOLID)
1917 i = (leaf - cl.worldmodel->leafs) - 1;
1918 if (worldvis[i>>3] & (1<<(i&7)))
1920 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1922 pstack[portalstack++] = p;
1926 p = pstack[--portalstack];
1938 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1939 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1940 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1941 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1942 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1944 int Cshader_count = 5;
1945 Cshader_t *Cshaders[5] =
1947 &Cshader_wall_vertex,
1948 &Cshader_wall_lightmap,
1949 &Cshader_wall_fullbright,
1954 void R_PrepareSurfaces(void)
1961 for (i = 0;i < Cshader_count;i++)
1962 Cshaders[i]->chain = NULL;
1964 model = currentrenderentity->model;
1966 for (i = 0;i < model->nummodelsurfaces;i++)
1968 surf = model->modelsortedsurfaces[i];
1969 if (surf->visframe == r_framecount)
1971 if (surf->insertframe != r_framecount)
1973 surf->insertframe = r_framecount;
1975 // manually inlined R_TextureAnimation
1976 //t = R_TextureAnimation(surf->texinfo->texture);
1977 t = surf->texinfo->texture;
1978 if (t->alternate_anims != NULL && currentrenderentity->frame)
1979 t = t->alternate_anims;
1980 if (t->anim_total >= 2)
1981 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1982 surf->currenttexture = t;
1985 surf->chain = surf->shader->chain;
1986 surf->shader->chain = surf;
1991 void R_DrawSurfaces (int type)
1996 for (i = 0;i < Cshader_count;i++)
1998 shader = Cshaders[i];
1999 if (shader->chain && shader->shaderfunc[type])
2000 shader->shaderfunc[type](shader->chain);
2004 static float portalpointbuffer[256][3];
2006 void R_DrawPortals(void)
2009 // mleaf_t *leaf, *endleaf;
2010 mportal_t *portal, *endportal;
2011 mvertex_t *point/*, *endpoint*/;
2013 drawportals = r_drawportals.integer;
2014 if (drawportals < 1)
2017 leaf = cl.worldmodel->leafs;
2018 endleaf = leaf + cl.worldmodel->numleafs;
2019 for (;leaf < endleaf;leaf++)
2021 if (leaf->visframe == r_framecount && leaf->portals)
2023 i = leaf - cl.worldmodel->leafs;
2024 r = (i & 0x0007) << 5;
2025 g = (i & 0x0038) << 2;
2026 b = (i & 0x01C0) >> 1;
2027 portal = leaf->portals;
2030 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
2031 point = portal->points + portal->numpoints - 1;
2032 endpoint = portal->points;
2033 for (;point >= endpoint;point--)
2034 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
2036 portal = portal->next;
2041 memset(&m, 0, sizeof(m));
2042 m.transparent = true;
2043 m.blendfunc1 = GL_SRC_ALPHA;
2044 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2045 m.vertex = &portalpointbuffer[0][0];
2046 m.vertexstep = sizeof(float[3]);
2048 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2050 if (portal->visframe == r_portalframecount)
2052 if (portal->numpoints <= 256)
2054 i = portal - cl.worldmodel->portals;
2055 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2056 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2057 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2058 point = portal->points;
2059 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2061 for (i = portal->numpoints - 1;i >= 0;i--)
2062 VectorCopy(point[i].position, portalpointbuffer[i]);
2066 for (i = 0;i < portal->numpoints;i++)
2067 VectorCopy(point[i].position, portalpointbuffer[i]);
2069 R_Mesh_DrawPolygon(&m, portal->numpoints);
2075 void R_SetupForBModelRendering(void)
2082 // because bmodels can be reused, we have to decide which things to render
2083 // from scratch every time
2085 model = currentrenderentity->model;
2087 softwaretransformforentity (currentrenderentity);
2088 softwareuntransform(r_origin, modelorg);
2090 for (i = 0;i < model->nummodelsurfaces;i++)
2092 surf = model->modelsortedsurfaces[i];
2093 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2094 surf->visframe = r_framecount;
2096 surf->visframe = -1;
2097 surf->worldnodeframe = -1;
2098 surf->lightframe = -1;
2099 surf->dlightframe = -1;
2100 surf->insertframe = -1;
2104 void R_SetupForWorldRendering(void)
2106 // there is only one instance of the world, but it can be rendered in
2109 currentrenderentity = &cl_entities[0].render;
2110 softwaretransformidentity();
2113 static void R_SurfMarkLights (void)
2118 if (r_dynamic.integer)
2121 if (!r_vertexsurfaces.integer)
2123 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2125 surf = currentrenderentity->model->modelsortedsurfaces[i];
2126 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2128 if (surf->cached_dlight
2129 || surf->cached_ambient != r_ambient.value
2130 || surf->cached_lightscalebit != lightscalebit)
2131 R_BuildLightMap(surf, false); // base lighting changed
2132 else if (r_dynamic.integer)
2134 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2135 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2136 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2137 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2138 //if (surf->cached_light[0] != d_lightstylevalue[surf->styles[0]]
2139 // || surf->cached_light[1] != d_lightstylevalue[surf->styles[1]]
2140 // || surf->cached_light[2] != d_lightstylevalue[surf->styles[2]]
2141 // || surf->cached_light[3] != d_lightstylevalue[surf->styles[3]])
2142 R_BuildLightMap(surf, false); // base lighting changed
2143 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2144 R_BuildLightMap(surf, true); // only dlights
2151 void R_MarkWorldLights(void)
2153 R_SetupForWorldRendering();
2162 void R_DrawWorld (void)
2164 R_SetupForWorldRendering();
2166 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2167 R_SolidWorldNode ();
2177 void R_DrawBrushModelSky (void)
2179 R_SetupForBModelRendering();
2181 R_PrepareSurfaces();
2182 R_DrawSurfaces(SHADERSTAGE_SKY);
2185 void R_DrawBrushModelNormal (void)
2189 // have to flush queue because of possible lightmap reuse
2192 R_SetupForBModelRendering();
2196 R_PrepareSurfaces();
2198 if (!skyrendermasked)
2199 R_DrawSurfaces(SHADERSTAGE_SKY);
2200 R_DrawSurfaces(SHADERSTAGE_NORMAL);