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