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 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
93 // for comparisons to minimum acceptable light
94 // compensate for LIGHTOFFSET
95 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
102 impact[0] = local[0] - surf->plane->normal[0] * dist;
103 impact[1] = local[1] - surf->plane->normal[1] * dist;
104 impact[2] = local[2] - surf->plane->normal[2] * dist;
106 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
107 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
109 s = bound(0, impacts, smax * 16) - impacts;
110 t = bound(0, impactt, tmax * 16) - impactt;
111 i = s * s + t * t + dist2;
115 // reduce calculations
116 for (s = 0, i = impacts; s < smax; s++, i -= 16)
117 sdtable[s] = i * i + dist2;
119 maxdist3 = maxdist - dist2;
121 // convert to 8.8 blocklights format
122 red = r_dlight[lnum].light[0];
123 green = r_dlight[lnum].light[1];
124 blue = r_dlight[lnum].light[2];
125 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
130 for (t = 0;t < tmax;t++, i -= 16)
133 // make sure some part of it is visible on this line
136 maxdist2 = maxdist - td;
137 for (s = 0;s < smax;s++)
139 if (sdtable[s] < maxdist2)
141 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
144 bl[0] += (red * k) >> 8;
145 bl[1] += (green * k) >> 8;
146 bl[2] += (blue * k) >> 8;
160 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
163 msurface_t *surf, *endsurf;
164 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
167 // LordHavoc: use 64bit integer... shame it's not very standardized...
168 #if _MSC_VER || __BORLANDC__
175 // for comparisons to minimum acceptable light
176 // compensate for 4096 offset
177 maxdist = radius * radius + 4096;
179 // clamp radius to avoid exceeding 32768 entry division table
180 if (maxdist > 4194304)
183 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
186 if (node->contents < 0)
188 ndist = PlaneDiff(origin, node->plane);
191 node = node->children[0];
196 node = node->children[1];
200 dist2 = ndist * ndist;
204 maxdist3 = maxdist - dist2;
206 impact[0] = origin[0] - node->plane->normal[0] * ndist;
207 impact[1] = origin[1] - node->plane->normal[1] * ndist;
208 impact[2] = origin[2] - node->plane->normal[2] * ndist;
210 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
212 if (surf->stainsamples)
214 smax = (surf->extents[0] >> 4) + 1;
215 tmax = (surf->extents[1] >> 4) + 1;
217 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
218 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
220 s = bound(0, impacts, smax * 16) - impacts;
221 t = bound(0, impactt, tmax * 16) - impactt;
222 i = s * s + t * t + dist2;
226 // reduce calculations
227 for (s = 0, i = impacts; s < smax; s++, i -= 16)
228 sdtable[s] = i * i + dist2;
230 // convert to 8.8 blocklights format
231 bl = surf->stainsamples;
236 for (t = 0;t < tmax;t++, i -= 16)
239 // make sure some part of it is visible on this line
242 maxdist2 = maxdist - td;
243 for (s = 0;s < smax;s++)
245 if (sdtable[s] < maxdist2)
247 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
250 ratio = rand() & 255;
251 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
255 a = bound(0, a, 256);
256 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
257 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
258 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
259 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
260 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
261 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
272 // force lightmap upload
274 surf->cached_dlight = true;
279 if (node->children[0]->contents >= 0)
281 if (node->children[1]->contents >= 0)
283 R_StainNode(node->children[0], model, origin, radius, icolor);
284 node = node->children[1];
289 node = node->children[0];
293 else if (node->children[1]->contents >= 0)
295 node = node->children[1];
300 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
303 entity_render_t *ent;
315 model = cl.worldmodel;
316 softwaretransformidentity();
317 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
319 // look for embedded bmodels
320 for (n = 1;n < MAX_EDICTS;n++)
322 ent = &cl_entities[n].render;
324 if (model && model->name[0] == '*')
326 Mod_CheckLoaded(model);
327 if (model->type == mod_brush)
329 softwaretransformforentity(ent);
330 softwareuntransform(origin, org);
331 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
341 Combine and scale multiple lightmaps into the 8.8 format in blocklights
344 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
346 int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
347 qbyte *lightmap, *out, *stain;
349 // update cached lighting info
350 surf->cached_dlight = 0;
351 surf->cached_lightscalebit = lightscalebit;
352 surf->cached_ambient = r_ambient.value;
353 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
354 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
355 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
356 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
358 smax = (surf->extents[0]>>4)+1;
359 tmax = (surf->extents[1]>>4)+1;
362 lightmap = surf->samples;
364 // set to full bright if no light data
365 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !cl.worldmodel->lightdata)
368 for (i = 0;i < size;i++)
378 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
382 for (i = 0;i < size3;i++)
386 memset(&blocklights[0], 0, size*3*sizeof(int));
388 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
390 surf->cached_dlight = R_AddDynamicLights(surf);
391 if (surf->cached_dlight)
393 else if (dlightchanged)
394 return; // don't upload if only updating dlights and none mattered
397 // add all the lightmaps
399 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
400 for (scale = d_lightstylevalue[surf->styles[maps]], bl = blocklights, i = 0;i < size3;i++)
401 *bl++ += *lightmap++ * scale;
404 stain = surf->stainsamples;
406 for (bl = blocklights, i = 0;i < size3;i++)
408 bl[i] = (bl[i] * stain[i]) >> 8;
412 // deal with lightmap brightness scale
413 shift = 7 + lightscalebit;
414 if (currentrenderentity->model->lightmaprgba)
416 stride = (surf->lightmaptexturestride - smax) * 4;
417 for (i = 0;i < tmax;i++, out += stride)
419 for (j = 0;j < smax;j++)
421 l = *bl++ >> shift;*out++ = min(l, 255);
422 l = *bl++ >> shift;*out++ = min(l, 255);
423 l = *bl++ >> shift;*out++ = min(l, 255);
430 stride = (surf->lightmaptexturestride - smax) * 3;
431 for (i = 0;i < tmax;i++, out += stride)
433 for (j = 0;j < smax;j++)
435 l = *bl++ >> shift;*out++ = min(l, 255);
436 l = *bl++ >> shift;*out++ = min(l, 255);
437 l = *bl++ >> shift;*out++ = min(l, 255);
442 R_UpdateTexture(surf->lightmaptexture, templight);
447 =============================================================
451 =============================================================
455 static float turbsin[256] =
457 #include "gl_warp_sin.h"
459 #define TURBSCALE (256.0 / (2 * M_PI))
461 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
462 #define MAX_SURFVERTS 3072
471 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
473 static void RSurfShader_Sky(msurface_t *firstsurf)
477 float number, length, dir[3], speedscale;
483 // LordHavoc: HalfLife maps have freaky skypolys...
484 if (currentrenderentity->model->ishlbsp)
491 skyrendernow = false;
494 for (surf = firstsurf;surf;surf = surf->chain)
496 // draw depth-only polys
497 memset(&m, 0, sizeof(m));
498 m.transparent = false;
499 m.blendfunc1 = GL_ZERO;
500 m.blendfunc2 = GL_ONE;
502 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
504 m.numtriangles = mesh->numtriangles;
505 m.numverts = mesh->numverts;
506 m.index = mesh->index;
507 if (softwaretransform_complexity)
509 m.vertex = &svert[0].v[0];
510 m.vertexstep = sizeof(surfvert_t);
511 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
512 softwaretransform(v->v, sv->v);
516 m.vertex = &mesh->vertex[0].v[0];
517 m.vertexstep = sizeof(surfvertex_t);
523 else if (skyrenderglquake)
525 for (surf = firstsurf;surf;surf = surf->chain)
527 memset(&m, 0, sizeof(m));
528 m.transparent = false;
529 m.blendfunc1 = GL_ONE;
530 m.blendfunc2 = GL_ZERO;
531 m.vertex = &svert[0].v[0];
532 m.vertexstep = sizeof(surfvert_t);
537 m.tex[0] = R_GetTexture(solidskytexture);
538 m.texcoords[0] = &svert[0].st[0];
539 m.texcoordstep[0] = sizeof(surfvert_t);
540 speedscale = cl.time * (8.0/128.0);
541 speedscale -= (int)speedscale;
542 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
544 m.numtriangles = mesh->numtriangles;
545 m.numverts = mesh->numverts;
546 m.index = mesh->index;
547 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
549 softwaretransform(v->v, sv->v);
550 VectorSubtract (sv->v, r_origin, dir);
551 // flatten the sphere
554 number = DotProduct(dir, dir);
556 length = 3.0f / sqrt(number);
558 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
559 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
562 sv->st[0] = speedscale + dir[0] * length;
563 sv->st[1] = speedscale + dir[1] * length;
571 for (surf = firstsurf;surf;surf = surf->chain)
574 memset(&m, 0, sizeof(m));
575 m.transparent = false;
576 m.blendfunc1 = GL_ONE;
577 m.blendfunc2 = GL_ZERO;
582 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
584 m.numtriangles = mesh->numtriangles;
585 m.numverts = mesh->numverts;
586 m.index = mesh->index;
587 if (softwaretransform_complexity)
589 m.vertex = &svert[0].v[0];
590 m.vertexstep = sizeof(surfvert_t);
591 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
592 softwaretransform(v->v, sv->v);
596 m.vertex = &mesh->vertex[0].v[0];
597 m.vertexstep = sizeof(surfvertex_t);
603 if (skyrenderglquake)
605 for (surf = firstsurf;surf;surf = surf->chain)
607 memset(&m, 0, sizeof(m));
608 m.transparent = false;
609 m.blendfunc1 = GL_SRC_ALPHA;
610 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
611 m.vertex = &svert[0].v[0];
612 m.vertexstep = sizeof(surfvert_t);
617 m.tex[0] = R_GetTexture(alphaskytexture);
618 m.texcoords[0] = &svert[0].st[0];
619 m.texcoordstep[0] = sizeof(surfvert_t);
620 speedscale = cl.time * (16.0/128.0);
621 speedscale -= (int)speedscale;
622 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
624 m.numtriangles = mesh->numtriangles;
625 m.numverts = mesh->numverts;
626 m.index = mesh->index;
627 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
629 softwaretransform(v->v, sv->v);
630 VectorSubtract (sv->v, r_origin, dir);
631 // flatten the sphere
634 number = DotProduct(dir, dir);
636 length = 3.0f / sqrt(number);
638 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
639 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
642 sv->st[0] = speedscale + dir[0] * length;
643 sv->st[1] = speedscale + dir[1] * length;
651 static int RSurf_Light(int *dlightbits, int numverts)
654 int i, l, lit = false;
658 for (l = 0;l < r_numdlights;l++)
660 if (dlightbits[l >> 5] & (1 << (l & 31)))
663 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
664 VectorCopy(rd->origin, lightorigin);
665 for (i = 0, sv = svert;i < numverts;i++, sv++)
667 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
668 if (f < rd->cullradius2)
670 f = (1.0f / f) - rd->lightsubtract;
671 sv->c[0] += rd->light[0] * f;
672 sv->c[1] += rd->light[1] * f;
673 sv->c[2] += rd->light[2] * f;
682 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
685 float diff[3], alpha, ifog;
690 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
692 memset(&m, 0, sizeof(m));
693 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
695 m.transparent = true;
696 m.blendfunc1 = GL_SRC_ALPHA;
697 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
701 m.transparent = false;
702 m.blendfunc1 = GL_ONE;
703 m.blendfunc2 = GL_ZERO;
705 m.vertex = &svert[0].v[0];
706 m.vertexstep = sizeof(surfvert_t);
707 m.color = &svert[0].c[0];
708 m.colorstep = sizeof(surfvert_t);
709 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
710 m.texcoords[0] = &svert[0].st[0];
711 m.texcoordstep[0] = sizeof(surfvert_t);
712 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
714 m.numtriangles = mesh->numtriangles;
715 m.numverts = mesh->numverts;
716 m.index = mesh->index;
717 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
719 softwaretransform(v->v, sv->v);
720 if (r_waterripple.value)
721 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];
722 if (surf->flags & SURF_DRAWFULLBRIGHT)
736 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
737 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
739 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
740 RSurf_Light(surf->dlightbits, m.numverts);
741 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
743 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
745 VectorSubtract(sv->v, r_origin, diff);
746 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
756 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
759 float diff[3], alpha, ifog;
764 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
766 memset(&m, 0, sizeof(m));
767 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
768 m.blendfunc1 = GL_SRC_ALPHA;
769 m.blendfunc2 = GL_ONE;
770 m.vertex = &svert[0].v[0];
771 m.vertexstep = sizeof(surfvert_t);
776 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
777 m.texcoords[0] = &svert[0].st[0];
778 m.texcoordstep[0] = sizeof(surfvert_t);
779 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
781 m.numtriangles = mesh->numtriangles;
782 m.numverts = mesh->numverts;
783 m.index = mesh->index;
786 m.color = &svert[0].c[0];
787 m.colorstep = sizeof(surfvert_t);
788 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
790 softwaretransform(v->v, sv->v);
791 if (r_waterripple.value)
792 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];
793 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
794 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
795 VectorSubtract(sv->v, r_origin, diff);
796 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
797 sv->c[0] = m.cr * ifog;
798 sv->c[1] = m.cg * ifog;
799 sv->c[2] = m.cb * ifog;
805 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
807 softwaretransform(v->v, sv->v);
808 if (r_waterripple.value)
809 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];
810 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
811 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
818 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
827 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
829 memset(&m, 0, sizeof(m));
830 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
831 m.blendfunc1 = GL_SRC_ALPHA;
832 m.blendfunc2 = GL_ONE;
833 m.vertex = &svert[0].v[0];
834 m.vertexstep = sizeof(surfvert_t);
835 m.color = &svert[0].c[0];
836 m.colorstep = sizeof(surfvert_t);
837 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
838 m.texcoords[0] = &svert[0].st[0];
839 m.texcoordstep[0] = sizeof(surfvert_t);
841 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
843 m.numtriangles = mesh->numtriangles;
844 m.numverts = mesh->numverts;
845 m.index = mesh->index;
846 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
848 softwaretransform(v->v, sv->v);
849 if (r_waterripple.value)
850 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];
853 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
854 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
856 VectorSubtract(sv->v, r_origin, diff);
857 sv->c[0] = fogcolor[0];
858 sv->c[1] = fogcolor[1];
859 sv->c[2] = fogcolor[2];
860 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
866 static void RSurfShader_Water(msurface_t *firstsurf)
869 for (surf = firstsurf;surf;surf = surf->chain)
870 RSurfShader_Water_Pass_Base(surf);
871 for (surf = firstsurf;surf;surf = surf->chain)
872 if (surf->currenttexture->glowtexture)
873 RSurfShader_Water_Pass_Glow(surf);
875 for (surf = firstsurf;surf;surf = surf->chain)
876 RSurfShader_Water_Pass_Fog(surf);
879 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
888 memset(&m, 0, sizeof(m));
889 if (currentrenderentity->effects & EF_ADDITIVE)
891 m.transparent = true;
892 m.blendfunc1 = GL_SRC_ALPHA;
893 m.blendfunc2 = GL_ONE;
895 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
897 m.transparent = true;
898 m.blendfunc1 = GL_SRC_ALPHA;
899 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
903 m.transparent = false;
904 m.blendfunc1 = GL_ONE;
905 m.blendfunc2 = GL_ZERO;
907 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
908 m.ca = currentrenderentity->alpha;
909 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
910 m.tex[1] = R_GetTexture(surf->lightmaptexture);
911 m.texcoordstep[0] = sizeof(surfvertex_t);
912 m.texcoordstep[1] = sizeof(surfvertex_t);
913 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
915 m.numtriangles = mesh->numtriangles;
916 m.numverts = mesh->numverts;
917 m.index = mesh->index;
918 m.texcoords[0] = &mesh->vertex->st[0];
919 m.texcoords[1] = &mesh->vertex->uv[0];
922 m.color = &svert[0].c[0];
923 m.colorstep = sizeof(surfvert_t);
924 if (softwaretransform_complexity)
926 m.vertex = &svert[0].v[0];
927 m.vertexstep = sizeof(surfvert_t);
928 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
930 softwaretransform(v->v, sv->v);
931 VectorSubtract(sv->v, r_origin, diff);
932 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
933 sv->c[0] = m.cr * ifog;
934 sv->c[1] = m.cg * ifog;
935 sv->c[2] = m.cb * ifog;
941 m.vertex = &mesh->vertex->v[0];
942 m.vertexstep = sizeof(surfvertex_t);
943 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
945 VectorSubtract(v->v, r_origin, diff);
946 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
947 sv->c[0] = m.cr * ifog;
948 sv->c[1] = m.cg * ifog;
949 sv->c[2] = m.cb * ifog;
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++)
961 softwaretransform(v->v, sv->v);
965 m.vertex = &mesh->vertex->v[0];
966 m.vertexstep = sizeof(surfvertex_t);
973 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
981 memset(&m, 0, sizeof(m));
982 m.transparent = false;
983 m.blendfunc1 = GL_ONE;
984 m.blendfunc2 = GL_ZERO;
985 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
987 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
988 m.texcoordstep[0] = sizeof(surfvertex_t);
989 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
991 m.numtriangles = mesh->numtriangles;
992 m.numverts = mesh->numverts;
993 m.index = mesh->index;
994 m.texcoords[0] = &mesh->vertex->st[0];
995 if (softwaretransform_complexity)
997 m.vertex = &svert[0].v[0];
998 m.vertexstep = sizeof(surfvert_t);
999 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1000 softwaretransform(v->v, sv->v);
1004 m.vertex = &mesh->vertex->v[0];
1005 m.vertexstep = sizeof(surfvertex_t);
1011 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1014 float diff[3], ifog;
1020 memset(&m, 0, sizeof(m));
1021 m.transparent = false;
1022 m.blendfunc1 = GL_ZERO;
1023 m.blendfunc2 = GL_SRC_COLOR;
1024 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1026 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1027 m.texcoordstep[0] = sizeof(surfvertex_t);
1028 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1030 m.numtriangles = mesh->numtriangles;
1031 m.numverts = mesh->numverts;
1032 m.index = mesh->index;
1033 m.texcoords[0] = &mesh->vertex->uv[0];
1036 m.color = &svert[0].c[0];
1037 m.colorstep = sizeof(surfvert_t);
1038 if (softwaretransform_complexity)
1040 m.vertex = &svert[0].v[0];
1041 m.vertexstep = sizeof(surfvert_t);
1042 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1044 softwaretransform(v->v, sv->v);
1045 VectorSubtract(sv->v, r_origin, diff);
1046 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1047 sv->c[0] = m.cr * ifog;
1048 sv->c[1] = m.cg * ifog;
1049 sv->c[2] = m.cb * ifog;
1055 m.vertex = &mesh->vertex->v[0];
1056 m.vertexstep = sizeof(surfvertex_t);
1057 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1059 VectorSubtract(v->v, r_origin, diff);
1060 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1061 sv->c[0] = m.cr * ifog;
1062 sv->c[1] = m.cg * ifog;
1063 sv->c[2] = m.cb * ifog;
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++)
1075 softwaretransform(v->v, sv->v);
1079 m.vertex = &mesh->vertex->v[0];
1080 m.vertexstep = sizeof(surfvertex_t);
1087 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1090 float c[3], base[3], scale, diff[3], ifog;
1097 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1099 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1101 memset(&m, 0, sizeof(m));
1102 if (currentrenderentity->effects & EF_ADDITIVE)
1104 m.transparent = true;
1105 m.blendfunc1 = GL_SRC_ALPHA;
1106 m.blendfunc2 = GL_ONE;
1108 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1110 m.transparent = true;
1111 m.blendfunc1 = GL_SRC_ALPHA;
1112 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1116 m.transparent = false;
1117 m.blendfunc1 = GL_ONE;
1118 m.blendfunc2 = GL_ZERO;
1120 m.vertex = &svert[0].v[0];
1121 m.vertexstep = sizeof(surfvert_t);
1122 m.color = &svert[0].c[0];
1123 m.colorstep = sizeof(surfvert_t);
1124 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1125 m.texcoordstep[0] = sizeof(surfvertex_t);
1126 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1128 m.numtriangles = mesh->numtriangles;
1129 m.numverts = mesh->numverts;
1130 m.index = mesh->index;
1131 m.texcoords[0] = &mesh->vertex->st[0];
1132 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1134 softwaretransform(v->v, sv->v);
1135 VectorCopy(base, c);
1136 if (surf->styles[0] != 255)
1138 lm = surf->samples + v->lightmapoffset;
1139 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1140 VectorMA(c, scale, lm, c);
1141 if (surf->styles[1] != 255)
1144 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1145 VectorMA(c, scale, lm, c);
1146 if (surf->styles[2] != 255)
1149 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1150 VectorMA(c, scale, lm, c);
1151 if (surf->styles[3] != 255)
1154 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1155 VectorMA(c, scale, lm, c);
1163 sv->c[3] = currentrenderentity->alpha;
1165 if (surf->dlightframe == r_framecount)
1166 RSurf_Light(surf->dlightbits, m.numverts);
1169 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1171 VectorSubtract(sv->v, r_origin, diff);
1172 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1182 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1185 float diff[3], ifog;
1191 memset(&m, 0, sizeof(m));
1192 if (currentrenderentity->effects & EF_ADDITIVE)
1194 m.transparent = true;
1195 m.blendfunc1 = GL_SRC_ALPHA;
1196 m.blendfunc2 = GL_ONE;
1198 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1200 m.transparent = true;
1201 m.blendfunc1 = GL_SRC_ALPHA;
1202 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1206 m.transparent = false;
1207 m.blendfunc1 = GL_ONE;
1208 m.blendfunc2 = GL_ZERO;
1210 m.vertex = &svert[0].v[0];
1211 m.vertexstep = sizeof(surfvert_t);
1212 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1213 m.texcoordstep[0] = sizeof(surfvertex_t);
1214 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1216 m.numtriangles = mesh->numtriangles;
1217 m.numverts = mesh->numverts;
1218 m.index = mesh->index;
1219 m.texcoords[0] = &mesh->vertex->st[0];
1222 m.color = &svert[0].c[0];
1223 m.colorstep = sizeof(surfvert_t);
1224 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1226 softwaretransform(v->v, sv->v);
1227 VectorSubtract(sv->v, r_origin, diff);
1228 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1232 sv->c[3] = currentrenderentity->alpha;
1237 m.cr = m.cg = m.cb = 1;
1238 m.ca = currentrenderentity->alpha;
1239 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1240 softwaretransform(v->v, sv->v);
1246 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1249 float diff[3], ifog;
1255 memset(&m, 0, sizeof(m));
1256 if (currentrenderentity->effects & EF_ADDITIVE)
1257 m.transparent = true;
1258 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1259 m.transparent = true;
1261 m.transparent = false;
1262 m.blendfunc1 = GL_SRC_ALPHA;
1263 m.blendfunc2 = GL_ONE;
1264 m.vertex = &svert[0].v[0];
1265 m.vertexstep = sizeof(surfvert_t);
1266 m.color = &svert[0].c[0];
1267 m.colorstep = sizeof(surfvert_t);
1268 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1269 m.texcoordstep[0] = sizeof(surfvertex_t);
1270 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1272 m.numtriangles = mesh->numtriangles;
1273 m.numverts = mesh->numverts;
1274 m.index = mesh->index;
1275 m.texcoords[0] = &mesh->vertex->st[0];
1276 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1278 softwaretransform(v->v, sv->v);
1282 sv->c[3] = currentrenderentity->alpha;
1284 if (RSurf_Light(surf->dlightbits, m.numverts))
1288 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1290 VectorSubtract(sv->v, r_origin, diff);
1291 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1302 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1305 float diff[3], ifog;
1311 memset(&m, 0, sizeof(m));
1312 if (currentrenderentity->effects & EF_ADDITIVE)
1313 m.transparent = true;
1314 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1315 m.transparent = true;
1317 m.transparent = false;
1318 m.blendfunc1 = GL_SRC_ALPHA;
1319 m.blendfunc2 = GL_ONE;
1323 m.ca = currentrenderentity->alpha;
1324 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1325 m.texcoordstep[0] = sizeof(surfvertex_t);
1326 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1328 m.numtriangles = mesh->numtriangles;
1329 m.numverts = mesh->numverts;
1330 m.index = mesh->index;
1331 m.texcoords[0] = &mesh->vertex->st[0];
1334 m.color = &svert[0].c[0];
1335 m.colorstep = sizeof(surfvert_t);
1336 if (softwaretransform_complexity)
1338 m.vertex = &svert[0].v[0];
1339 m.vertexstep = sizeof(surfvert_t);
1340 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1342 softwaretransform(v->v, sv->v);
1343 VectorSubtract(sv->v, r_origin, diff);
1344 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1345 sv->c[0] = m.cr * ifog;
1346 sv->c[1] = m.cg * ifog;
1347 sv->c[2] = m.cb * ifog;
1353 m.vertex = &mesh->vertex->v[0];
1354 m.vertexstep = sizeof(surfvertex_t);
1355 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1357 VectorSubtract(v->v, r_origin, diff);
1358 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1359 sv->c[0] = m.cr * ifog;
1360 sv->c[1] = m.cg * ifog;
1361 sv->c[2] = m.cb * ifog;
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++)
1373 softwaretransform(v->v, sv->v);
1377 m.vertex = &mesh->vertex->v[0];
1378 m.vertexstep = sizeof(surfvertex_t);
1385 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1394 memset(&m, 0, sizeof(m));
1395 if (currentrenderentity->effects & EF_ADDITIVE)
1396 m.transparent = true;
1397 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1398 m.transparent = true;
1400 m.transparent = false;
1401 m.blendfunc1 = GL_SRC_ALPHA;
1402 m.blendfunc2 = GL_ONE;
1403 m.color = &svert[0].c[0];
1404 m.colorstep = sizeof(surfvert_t);
1405 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1406 m.texcoordstep[0] = sizeof(surfvertex_t);
1407 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1409 m.numtriangles = mesh->numtriangles;
1410 m.numverts = mesh->numverts;
1411 m.index = mesh->index;
1412 m.texcoords[0] = &mesh->vertex->st[0];
1413 if (softwaretransform_complexity)
1415 m.vertex = &svert[0].v[0];
1416 m.vertexstep = sizeof(surfvert_t);
1417 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1419 softwaretransform(v->v, sv->v);
1420 VectorSubtract(sv->v, r_origin, diff);
1421 sv->c[0] = fogcolor[0];
1422 sv->c[1] = fogcolor[1];
1423 sv->c[2] = fogcolor[2];
1424 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1429 m.vertex = &mesh->vertex->v[0];
1430 m.vertexstep = sizeof(surfvertex_t);
1431 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1433 VectorSubtract(v->v, r_origin, diff);
1434 sv->c[0] = fogcolor[0];
1435 sv->c[1] = fogcolor[1];
1436 sv->c[2] = fogcolor[2];
1437 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1444 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1447 for (surf = firstsurf;surf;surf = surf->chain)
1450 RSurfShader_Wall_Pass_BaseFullbright(surf);
1452 for (surf = firstsurf;surf;surf = surf->chain)
1453 if (surf->currenttexture->glowtexture)
1454 RSurfShader_Wall_Pass_Glow(surf);
1456 for (surf = firstsurf;surf;surf = surf->chain)
1457 RSurfShader_Wall_Pass_Fog(surf);
1460 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1463 for (surf = firstsurf;surf;surf = surf->chain)
1466 RSurfShader_Wall_Pass_BaseVertex(surf);
1468 for (surf = firstsurf;surf;surf = surf->chain)
1469 if (surf->currenttexture->glowtexture)
1470 RSurfShader_Wall_Pass_Glow(surf);
1472 for (surf = firstsurf;surf;surf = surf->chain)
1473 RSurfShader_Wall_Pass_Fog(surf);
1476 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1479 if (r_vertexsurfaces.integer)
1481 for (surf = firstsurf;surf;surf = surf->chain)
1484 RSurfShader_Wall_Pass_BaseVertex(surf);
1486 for (surf = firstsurf;surf;surf = surf->chain)
1487 if (surf->currenttexture->glowtexture)
1488 RSurfShader_Wall_Pass_Glow(surf);
1490 for (surf = firstsurf;surf;surf = surf->chain)
1491 RSurfShader_Wall_Pass_Fog(surf);
1493 else if (r_multitexture.integer)
1495 if (r_dlightmap.integer)
1497 for (surf = firstsurf;surf;surf = surf->chain)
1500 RSurfShader_Wall_Pass_BaseMTex(surf);
1502 for (surf = firstsurf;surf;surf = surf->chain)
1503 if (surf->currenttexture->glowtexture)
1504 RSurfShader_Wall_Pass_Glow(surf);
1506 for (surf = firstsurf;surf;surf = surf->chain)
1507 RSurfShader_Wall_Pass_Fog(surf);
1511 for (surf = firstsurf;surf;surf = surf->chain)
1514 RSurfShader_Wall_Pass_BaseMTex(surf);
1516 for (surf = firstsurf;surf;surf = surf->chain)
1517 if (surf->dlightframe == r_framecount)
1518 RSurfShader_Wall_Pass_Light(surf);
1519 for (surf = firstsurf;surf;surf = surf->chain)
1520 if (surf->currenttexture->glowtexture)
1521 RSurfShader_Wall_Pass_Glow(surf);
1523 for (surf = firstsurf;surf;surf = surf->chain)
1524 RSurfShader_Wall_Pass_Fog(surf);
1527 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1529 for (surf = firstsurf;surf;surf = surf->chain)
1532 RSurfShader_Wall_Pass_BaseVertex(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 if (r_dlightmap.integer)
1545 for (surf = firstsurf;surf;surf = surf->chain)
1548 RSurfShader_Wall_Pass_BaseTexture(surf);
1550 for (surf = firstsurf;surf;surf = surf->chain)
1551 RSurfShader_Wall_Pass_BaseLightmap(surf);
1552 for (surf = firstsurf;surf;surf = surf->chain)
1553 if (surf->currenttexture->glowtexture)
1554 RSurfShader_Wall_Pass_Glow(surf);
1556 for (surf = firstsurf;surf;surf = surf->chain)
1557 RSurfShader_Wall_Pass_Fog(surf);
1561 for (surf = firstsurf;surf;surf = surf->chain)
1564 RSurfShader_Wall_Pass_BaseTexture(surf);
1566 for (surf = firstsurf;surf;surf = surf->chain)
1567 RSurfShader_Wall_Pass_BaseLightmap(surf);
1568 for (surf = firstsurf;surf;surf = surf->chain)
1569 if (surf->dlightframe == r_framecount)
1570 RSurfShader_Wall_Pass_Light(surf);
1571 for (surf = firstsurf;surf;surf = surf->chain)
1572 if (surf->currenttexture->glowtexture)
1573 RSurfShader_Wall_Pass_Glow(surf);
1575 for (surf = firstsurf;surf;surf = surf->chain)
1576 RSurfShader_Wall_Pass_Fog(surf);
1582 =============================================================
1586 =============================================================
1589 static void RSurf_Callback(void *data, void *junk)
1591 ((msurface_t *)data)->visframe = r_framecount;
1594 static void R_SolidWorldNode (void)
1596 if (r_viewleaf->contents != CONTENTS_SOLID)
1599 mportal_t *p, *pstack[8192];
1600 msurface_t *surf, **mark, **endmark;
1603 // LordHavoc: portal-passage worldnode; follows portals leading
1604 // outward from viewleaf, if a portal leads offscreen it is not
1605 // followed, in indoor maps this can often cull a great deal of
1606 // geometry away when pvs data is not present (useful with pvs as well)
1609 leaf->worldnodeframe = r_framecount;
1614 leaf->visframe = r_framecount;
1616 if (leaf->nummarksurfaces)
1618 mark = leaf->firstmarksurface;
1619 endmark = mark + leaf->nummarksurfaces;
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)
1633 VectorNegate(surf->plane->normal, plane.normal);
1634 plane.dist = -surf->plane->dist;
1635 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1640 if (!(surf->flags & SURF_PLANEBACK))
1641 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);
1644 while (mark < endmark);
1651 // make sure surfaces are only processed once
1652 if (surf->worldnodeframe == r_framecount)
1654 surf->worldnodeframe = r_framecount;
1655 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1657 if (surf->flags & SURF_PLANEBACK)
1658 surf->visframe = r_framecount;
1662 if (!(surf->flags & SURF_PLANEBACK))
1663 surf->visframe = r_framecount;
1666 while (mark < endmark);
1670 // follow portals into other leafs
1672 for (;p;p = p->next)
1674 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1677 if (leaf->worldnodeframe != r_framecount)
1679 leaf->worldnodeframe = r_framecount;
1680 if (leaf->contents != CONTENTS_SOLID)
1682 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1684 p->visframe = r_framecount;
1685 pstack[portalstack++] = p;
1689 p = pstack[--portalstack];
1701 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1702 int nodestackpos = 0;
1703 // LordHavoc: recursive descending worldnode; if portals are not
1704 // available, this is a good last resort, can cull large amounts of
1705 // geometry, but is more time consuming than portal-passage and renders
1706 // things behind walls
1709 if (R_NotCulledBox(node->mins, node->maxs))
1711 if (node->numsurfaces)
1715 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1717 if (PlaneDiff (r_origin, node->plane) < 0)
1719 for (;surf < surfend;surf++)
1721 if (surf->flags & SURF_PLANEBACK)
1723 VectorNegate(surf->plane->normal, plane.normal);
1724 plane.dist = -surf->plane->dist;
1725 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1731 for (;surf < surfend;surf++)
1733 if (!(surf->flags & SURF_PLANEBACK))
1734 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);
1740 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1741 if (PlaneDiff (r_origin, node->plane) < 0)
1743 for (;surf < surfend;surf++)
1745 if (surf->flags & SURF_PLANEBACK)
1746 surf->visframe = r_framecount;
1751 for (;surf < surfend;surf++)
1753 if (!(surf->flags & SURF_PLANEBACK))
1754 surf->visframe = r_framecount;
1760 // recurse down the children
1761 if (node->children[0]->contents >= 0)
1763 if (node->children[1]->contents >= 0)
1765 if (nodestackpos < 8192)
1766 nodestack[nodestackpos++] = node->children[1];
1767 node = node->children[0];
1771 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1772 node = node->children[0];
1777 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1778 if (node->children[1]->contents >= 0)
1780 node = node->children[1];
1783 else if (nodestackpos > 0)
1785 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1786 node = nodestack[--nodestackpos];
1791 else if (nodestackpos > 0)
1793 node = nodestack[--nodestackpos];
1799 static int r_portalframecount = 0;
1801 static void R_PVSWorldNode()
1804 mportal_t *p, *pstack[8192];
1805 msurface_t *surf, **mark, **endmark;
1810 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1813 leaf->worldnodeframe = r_framecount;
1818 leaf->visframe = r_framecount;
1820 if (leaf->nummarksurfaces)
1822 mark = leaf->firstmarksurface;
1823 endmark = mark + leaf->nummarksurfaces;
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)
1837 VectorNegate(surf->plane->normal, plane.normal);
1838 plane.dist = -surf->plane->dist;
1839 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1844 if (!(surf->flags & SURF_PLANEBACK))
1845 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);
1848 while (mark < endmark);
1855 // make sure surfaces are only processed once
1856 if (surf->worldnodeframe == r_framecount)
1858 surf->worldnodeframe = r_framecount;
1859 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1861 if (surf->flags & SURF_PLANEBACK)
1862 surf->visframe = r_framecount;
1866 if (!(surf->flags & SURF_PLANEBACK))
1867 surf->visframe = r_framecount;
1870 while (mark < endmark);
1874 // follow portals into other leafs
1875 for (p = leaf->portals;p;p = p->next)
1877 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1880 if (leaf->worldnodeframe != r_framecount)
1882 leaf->worldnodeframe = r_framecount;
1883 if (leaf->contents != CONTENTS_SOLID)
1885 i = (leaf - cl.worldmodel->leafs) - 1;
1886 if (worldvis[i>>3] & (1<<(i&7)))
1888 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1890 pstack[portalstack++] = p;
1894 p = pstack[--portalstack];
1906 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1907 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1908 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1909 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1910 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1912 int Cshader_count = 5;
1913 Cshader_t *Cshaders[5] =
1915 &Cshader_wall_vertex,
1916 &Cshader_wall_lightmap,
1917 &Cshader_wall_fullbright,
1922 void R_PrepareSurfaces(void)
1929 for (i = 0;i < Cshader_count;i++)
1930 Cshaders[i]->chain = NULL;
1932 model = currentrenderentity->model;
1934 for (i = 0;i < model->nummodelsurfaces;i++)
1936 surf = model->modelsortedsurfaces[i];
1937 if (surf->visframe == r_framecount)
1939 if (surf->insertframe != r_framecount)
1941 surf->insertframe = r_framecount;
1943 t = surf->texinfo->texture;
1944 if (t->alternate_anims != NULL && currentrenderentity->frame)
1945 t = t->alternate_anims;
1946 if (t->anim_total >= 2)
1947 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1948 surf->currenttexture = t;
1951 surf->chain = surf->shader->chain;
1952 surf->shader->chain = surf;
1957 void R_DrawSurfaces (int type)
1962 for (i = 0;i < Cshader_count;i++)
1964 shader = Cshaders[i];
1965 if (shader->chain && shader->shaderfunc[type])
1966 shader->shaderfunc[type](shader->chain);
1970 static float portalpointbuffer[256][3];
1972 void R_DrawPortals(void)
1975 mportal_t *portal, *endportal;
1978 drawportals = r_drawportals.integer;
1980 if (drawportals < 1)
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 R_BuildLightMap(surf, false); // base lighting changed
2081 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2082 R_BuildLightMap(surf, true); // only dlights
2089 void R_MarkWorldLights(void)
2091 R_SetupForWorldRendering();
2100 void R_DrawWorld (void)
2102 R_SetupForWorldRendering();
2104 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2105 R_SolidWorldNode ();
2115 void R_DrawBrushModelSky (void)
2117 R_SetupForBModelRendering();
2119 R_PrepareSurfaces();
2120 R_DrawSurfaces(SHADERSTAGE_SKY);
2123 void R_DrawBrushModelNormal (void)
2127 // have to flush queue because of possible lightmap reuse
2130 R_SetupForBModelRendering();
2134 R_PrepareSurfaces();
2136 if (!skyrendermasked)
2137 R_DrawSurfaces(SHADERSTAGE_SKY);
2138 R_DrawSurfaces(SHADERSTAGE_NORMAL);