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 < size3;i++)
374 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
378 for (i = 0;i < size3;i++)
382 memset(&blocklights[0], 0, size*3*sizeof(int));
384 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
386 surf->cached_dlight = R_AddDynamicLights(surf);
387 if (surf->cached_dlight)
389 else if (dlightchanged)
390 return; // don't upload if only updating dlights and none mattered
393 // add all the lightmaps
397 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
398 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
399 bl[i] += lightmap[i] * scale;
403 stain = surf->stainsamples;
406 // deal with lightmap brightness scale
407 shift = 15 + lightscalebit;
408 if (currentrenderentity->model->lightmaprgba)
410 stride = (surf->lightmaptexturestride - smax) * 4;
411 for (i = 0;i < tmax;i++, out += stride)
413 for (j = 0;j < smax;j++)
415 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
416 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
417 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
424 stride = (surf->lightmaptexturestride - smax) * 3;
425 for (i = 0;i < tmax;i++, out += stride)
427 for (j = 0;j < smax;j++)
429 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
430 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
431 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
436 R_UpdateTexture(surf->lightmaptexture, templight);
441 =============================================================
445 =============================================================
449 static float turbsin[256] =
451 #include "gl_warp_sin.h"
453 #define TURBSCALE (256.0 / (2 * M_PI))
455 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
456 #define MAX_SURFVERTS 3072
465 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
467 static void RSurfShader_Sky(msurface_t *firstsurf)
471 float number, length, dir[3], speedscale;
477 // LordHavoc: HalfLife maps have freaky skypolys...
478 if (currentrenderentity->model->ishlbsp)
485 skyrendernow = false;
488 for (surf = firstsurf;surf;surf = surf->chain)
490 // draw depth-only polys
491 memset(&m, 0, sizeof(m));
492 m.transparent = false;
493 m.blendfunc1 = GL_ZERO;
494 m.blendfunc2 = GL_ONE;
496 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
498 m.numtriangles = mesh->numtriangles;
499 m.numverts = mesh->numverts;
500 m.index = mesh->index;
501 if (softwaretransform_complexity)
503 m.vertex = &svert[0].v[0];
504 m.vertexstep = sizeof(surfvert_t);
505 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
506 softwaretransform(v->v, sv->v);
510 m.vertex = &mesh->vertex[0].v[0];
511 m.vertexstep = sizeof(surfvertex_t);
517 else if (skyrenderglquake)
519 for (surf = firstsurf;surf;surf = surf->chain)
521 memset(&m, 0, sizeof(m));
522 m.transparent = false;
523 m.blendfunc1 = GL_ONE;
524 m.blendfunc2 = GL_ZERO;
525 m.vertex = &svert[0].v[0];
526 m.vertexstep = sizeof(surfvert_t);
531 m.tex[0] = R_GetTexture(solidskytexture);
532 m.texcoords[0] = &svert[0].st[0];
533 m.texcoordstep[0] = sizeof(surfvert_t);
534 speedscale = cl.time * (8.0/128.0);
535 speedscale -= (int)speedscale;
536 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
538 m.numtriangles = mesh->numtriangles;
539 m.numverts = mesh->numverts;
540 m.index = mesh->index;
541 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
543 softwaretransform(v->v, sv->v);
544 VectorSubtract (sv->v, r_origin, dir);
545 // flatten the sphere
548 number = DotProduct(dir, dir);
550 length = 3.0f / sqrt(number);
552 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
553 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
556 sv->st[0] = speedscale + dir[0] * length;
557 sv->st[1] = speedscale + dir[1] * length;
565 for (surf = firstsurf;surf;surf = surf->chain)
568 memset(&m, 0, sizeof(m));
569 m.transparent = false;
570 m.blendfunc1 = GL_ONE;
571 m.blendfunc2 = GL_ZERO;
576 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
578 m.numtriangles = mesh->numtriangles;
579 m.numverts = mesh->numverts;
580 m.index = mesh->index;
581 if (softwaretransform_complexity)
583 m.vertex = &svert[0].v[0];
584 m.vertexstep = sizeof(surfvert_t);
585 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
586 softwaretransform(v->v, sv->v);
590 m.vertex = &mesh->vertex[0].v[0];
591 m.vertexstep = sizeof(surfvertex_t);
597 if (skyrenderglquake)
599 for (surf = firstsurf;surf;surf = surf->chain)
601 memset(&m, 0, sizeof(m));
602 m.transparent = false;
603 m.blendfunc1 = GL_SRC_ALPHA;
604 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
605 m.vertex = &svert[0].v[0];
606 m.vertexstep = sizeof(surfvert_t);
611 m.tex[0] = R_GetTexture(alphaskytexture);
612 m.texcoords[0] = &svert[0].st[0];
613 m.texcoordstep[0] = sizeof(surfvert_t);
614 speedscale = cl.time * (16.0/128.0);
615 speedscale -= (int)speedscale;
616 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
618 m.numtriangles = mesh->numtriangles;
619 m.numverts = mesh->numverts;
620 m.index = mesh->index;
621 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
623 softwaretransform(v->v, sv->v);
624 VectorSubtract (sv->v, r_origin, dir);
625 // flatten the sphere
628 number = DotProduct(dir, dir);
630 length = 3.0f / sqrt(number);
632 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
633 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
636 sv->st[0] = speedscale + dir[0] * length;
637 sv->st[1] = speedscale + dir[1] * length;
645 static int RSurf_Light(int *dlightbits, int numverts)
648 int i, l, lit = false;
652 for (l = 0;l < r_numdlights;l++)
654 if (dlightbits[l >> 5] & (1 << (l & 31)))
657 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
658 VectorCopy(rd->origin, lightorigin);
659 for (i = 0, sv = svert;i < numverts;i++, sv++)
661 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
662 if (f < rd->cullradius2)
664 f = (1.0f / f) - rd->lightsubtract;
665 sv->c[0] += rd->light[0] * f;
666 sv->c[1] += rd->light[1] * f;
667 sv->c[2] += rd->light[2] * f;
676 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
679 float diff[3], alpha, ifog;
684 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
686 memset(&m, 0, sizeof(m));
687 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
689 m.transparent = true;
690 m.blendfunc1 = GL_SRC_ALPHA;
691 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
695 m.transparent = false;
696 m.blendfunc1 = GL_ONE;
697 m.blendfunc2 = GL_ZERO;
699 m.vertex = &svert[0].v[0];
700 m.vertexstep = sizeof(surfvert_t);
701 m.color = &svert[0].c[0];
702 m.colorstep = sizeof(surfvert_t);
703 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
704 m.texcoords[0] = &svert[0].st[0];
705 m.texcoordstep[0] = sizeof(surfvert_t);
706 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
708 m.numtriangles = mesh->numtriangles;
709 m.numverts = mesh->numverts;
710 m.index = mesh->index;
711 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
713 softwaretransform(v->v, sv->v);
714 if (r_waterripple.value)
715 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];
716 if (surf->flags & SURF_DRAWFULLBRIGHT)
730 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
731 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
733 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
734 RSurf_Light(surf->dlightbits, m.numverts);
735 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
737 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
739 VectorSubtract(sv->v, r_origin, diff);
740 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
750 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
753 float diff[3], alpha, ifog;
758 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
760 memset(&m, 0, sizeof(m));
761 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
762 m.blendfunc1 = GL_SRC_ALPHA;
763 m.blendfunc2 = GL_ONE;
764 m.vertex = &svert[0].v[0];
765 m.vertexstep = sizeof(surfvert_t);
770 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
771 m.texcoords[0] = &svert[0].st[0];
772 m.texcoordstep[0] = sizeof(surfvert_t);
773 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
775 m.numtriangles = mesh->numtriangles;
776 m.numverts = mesh->numverts;
777 m.index = mesh->index;
780 m.color = &svert[0].c[0];
781 m.colorstep = sizeof(surfvert_t);
782 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
784 softwaretransform(v->v, sv->v);
785 if (r_waterripple.value)
786 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];
787 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
788 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
789 VectorSubtract(sv->v, r_origin, diff);
790 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
791 sv->c[0] = m.cr * ifog;
792 sv->c[1] = m.cg * ifog;
793 sv->c[2] = m.cb * ifog;
799 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
801 softwaretransform(v->v, sv->v);
802 if (r_waterripple.value)
803 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];
804 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
805 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
812 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
821 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
823 memset(&m, 0, sizeof(m));
824 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
825 m.blendfunc1 = GL_SRC_ALPHA;
826 m.blendfunc2 = GL_ONE;
827 m.vertex = &svert[0].v[0];
828 m.vertexstep = sizeof(surfvert_t);
829 m.color = &svert[0].c[0];
830 m.colorstep = sizeof(surfvert_t);
831 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
832 m.texcoords[0] = &svert[0].st[0];
833 m.texcoordstep[0] = sizeof(surfvert_t);
835 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
837 m.numtriangles = mesh->numtriangles;
838 m.numverts = mesh->numverts;
839 m.index = mesh->index;
840 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
842 softwaretransform(v->v, sv->v);
843 if (r_waterripple.value)
844 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];
847 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
848 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
850 VectorSubtract(sv->v, r_origin, diff);
851 sv->c[0] = fogcolor[0];
852 sv->c[1] = fogcolor[1];
853 sv->c[2] = fogcolor[2];
854 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
860 static void RSurfShader_Water(msurface_t *firstsurf)
863 for (surf = firstsurf;surf;surf = surf->chain)
864 RSurfShader_Water_Pass_Base(surf);
865 for (surf = firstsurf;surf;surf = surf->chain)
866 if (surf->currenttexture->glowtexture)
867 RSurfShader_Water_Pass_Glow(surf);
869 for (surf = firstsurf;surf;surf = surf->chain)
870 RSurfShader_Water_Pass_Fog(surf);
873 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
882 memset(&m, 0, sizeof(m));
883 if (currentrenderentity->effects & EF_ADDITIVE)
885 m.transparent = true;
886 m.blendfunc1 = GL_SRC_ALPHA;
887 m.blendfunc2 = GL_ONE;
889 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
891 m.transparent = true;
892 m.blendfunc1 = GL_SRC_ALPHA;
893 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
897 m.transparent = false;
898 m.blendfunc1 = GL_ONE;
899 m.blendfunc2 = GL_ZERO;
901 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
902 m.ca = currentrenderentity->alpha;
903 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
904 m.tex[1] = R_GetTexture(surf->lightmaptexture);
905 m.texcoordstep[0] = sizeof(surfvertex_t);
906 m.texcoordstep[1] = sizeof(surfvertex_t);
907 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
909 m.numtriangles = mesh->numtriangles;
910 m.numverts = mesh->numverts;
911 m.index = mesh->index;
912 m.texcoords[0] = &mesh->vertex->st[0];
913 m.texcoords[1] = &mesh->vertex->uv[0];
916 m.color = &svert[0].c[0];
917 m.colorstep = sizeof(surfvert_t);
918 if (softwaretransform_complexity)
920 m.vertex = &svert[0].v[0];
921 m.vertexstep = sizeof(surfvert_t);
922 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
924 softwaretransform(v->v, sv->v);
925 VectorSubtract(sv->v, r_origin, diff);
926 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
927 sv->c[0] = m.cr * ifog;
928 sv->c[1] = m.cg * ifog;
929 sv->c[2] = m.cb * ifog;
935 m.vertex = &mesh->vertex->v[0];
936 m.vertexstep = sizeof(surfvertex_t);
937 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
939 VectorSubtract(v->v, r_origin, diff);
940 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
941 sv->c[0] = m.cr * ifog;
942 sv->c[1] = m.cg * ifog;
943 sv->c[2] = m.cb * ifog;
950 if (softwaretransform_complexity)
952 m.vertex = &svert[0].v[0];
953 m.vertexstep = sizeof(surfvert_t);
954 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
955 softwaretransform(v->v, sv->v);
959 m.vertex = &mesh->vertex->v[0];
960 m.vertexstep = sizeof(surfvertex_t);
967 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
975 memset(&m, 0, sizeof(m));
976 m.transparent = false;
977 m.blendfunc1 = GL_ONE;
978 m.blendfunc2 = GL_ZERO;
979 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
981 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
982 m.texcoordstep[0] = sizeof(surfvertex_t);
983 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
985 m.numtriangles = mesh->numtriangles;
986 m.numverts = mesh->numverts;
987 m.index = mesh->index;
988 m.texcoords[0] = &mesh->vertex->st[0];
989 if (softwaretransform_complexity)
991 m.vertex = &svert[0].v[0];
992 m.vertexstep = sizeof(surfvert_t);
993 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
994 softwaretransform(v->v, sv->v);
998 m.vertex = &mesh->vertex->v[0];
999 m.vertexstep = sizeof(surfvertex_t);
1005 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1008 float diff[3], ifog;
1014 memset(&m, 0, sizeof(m));
1015 m.transparent = false;
1016 m.blendfunc1 = GL_ZERO;
1017 m.blendfunc2 = GL_SRC_COLOR;
1018 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1020 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1021 m.texcoordstep[0] = sizeof(surfvertex_t);
1022 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1024 m.numtriangles = mesh->numtriangles;
1025 m.numverts = mesh->numverts;
1026 m.index = mesh->index;
1027 m.texcoords[0] = &mesh->vertex->uv[0];
1030 m.color = &svert[0].c[0];
1031 m.colorstep = sizeof(surfvert_t);
1032 if (softwaretransform_complexity)
1034 m.vertex = &svert[0].v[0];
1035 m.vertexstep = sizeof(surfvert_t);
1036 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1038 softwaretransform(v->v, sv->v);
1039 VectorSubtract(sv->v, r_origin, diff);
1040 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1041 sv->c[0] = m.cr * ifog;
1042 sv->c[1] = m.cg * ifog;
1043 sv->c[2] = m.cb * ifog;
1049 m.vertex = &mesh->vertex->v[0];
1050 m.vertexstep = sizeof(surfvertex_t);
1051 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1053 VectorSubtract(v->v, r_origin, diff);
1054 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1055 sv->c[0] = m.cr * ifog;
1056 sv->c[1] = m.cg * ifog;
1057 sv->c[2] = m.cb * ifog;
1064 if (softwaretransform_complexity)
1066 m.vertex = &svert[0].v[0];
1067 m.vertexstep = sizeof(surfvert_t);
1068 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1069 softwaretransform(v->v, sv->v);
1073 m.vertex = &mesh->vertex->v[0];
1074 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;
1091 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1093 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1095 memset(&m, 0, sizeof(m));
1096 if (currentrenderentity->effects & EF_ADDITIVE)
1098 m.transparent = true;
1099 m.blendfunc1 = GL_SRC_ALPHA;
1100 m.blendfunc2 = GL_ONE;
1102 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1104 m.transparent = true;
1105 m.blendfunc1 = GL_SRC_ALPHA;
1106 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1110 m.transparent = false;
1111 m.blendfunc1 = GL_ONE;
1112 m.blendfunc2 = GL_ZERO;
1114 m.vertex = &svert[0].v[0];
1115 m.vertexstep = sizeof(surfvert_t);
1116 m.color = &svert[0].c[0];
1117 m.colorstep = sizeof(surfvert_t);
1118 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1119 m.texcoordstep[0] = sizeof(surfvertex_t);
1120 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1122 m.numtriangles = mesh->numtriangles;
1123 m.numverts = mesh->numverts;
1124 m.index = mesh->index;
1125 m.texcoords[0] = &mesh->vertex->st[0];
1126 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1128 softwaretransform(v->v, sv->v);
1129 VectorCopy(base, c);
1130 if (surf->styles[0] != 255)
1132 lm = surf->samples + v->lightmapoffset;
1133 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1134 VectorMA(c, scale, lm, c);
1135 if (surf->styles[1] != 255)
1138 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1139 VectorMA(c, scale, lm, c);
1140 if (surf->styles[2] != 255)
1143 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1144 VectorMA(c, scale, lm, c);
1145 if (surf->styles[3] != 255)
1148 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1149 VectorMA(c, scale, lm, c);
1157 sv->c[3] = currentrenderentity->alpha;
1159 if (surf->dlightframe == r_framecount)
1160 RSurf_Light(surf->dlightbits, m.numverts);
1163 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1165 VectorSubtract(sv->v, r_origin, diff);
1166 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1176 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1179 float diff[3], ifog;
1185 memset(&m, 0, sizeof(m));
1186 if (currentrenderentity->effects & EF_ADDITIVE)
1188 m.transparent = true;
1189 m.blendfunc1 = GL_SRC_ALPHA;
1190 m.blendfunc2 = GL_ONE;
1192 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1194 m.transparent = true;
1195 m.blendfunc1 = GL_SRC_ALPHA;
1196 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1200 m.transparent = false;
1201 m.blendfunc1 = GL_ONE;
1202 m.blendfunc2 = GL_ZERO;
1204 m.vertex = &svert[0].v[0];
1205 m.vertexstep = sizeof(surfvert_t);
1206 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1207 m.texcoordstep[0] = sizeof(surfvertex_t);
1208 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1210 m.numtriangles = mesh->numtriangles;
1211 m.numverts = mesh->numverts;
1212 m.index = mesh->index;
1213 m.texcoords[0] = &mesh->vertex->st[0];
1216 m.color = &svert[0].c[0];
1217 m.colorstep = sizeof(surfvert_t);
1218 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1220 softwaretransform(v->v, sv->v);
1221 VectorSubtract(sv->v, r_origin, diff);
1222 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1226 sv->c[3] = currentrenderentity->alpha;
1231 m.cr = m.cg = m.cb = 1;
1232 m.ca = currentrenderentity->alpha;
1233 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1234 softwaretransform(v->v, sv->v);
1240 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1243 float diff[3], ifog;
1249 memset(&m, 0, sizeof(m));
1250 if (currentrenderentity->effects & EF_ADDITIVE)
1251 m.transparent = true;
1252 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1253 m.transparent = true;
1255 m.transparent = false;
1256 m.blendfunc1 = GL_SRC_ALPHA;
1257 m.blendfunc2 = GL_ONE;
1258 m.vertex = &svert[0].v[0];
1259 m.vertexstep = sizeof(surfvert_t);
1260 m.color = &svert[0].c[0];
1261 m.colorstep = sizeof(surfvert_t);
1262 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1263 m.texcoordstep[0] = sizeof(surfvertex_t);
1264 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1266 m.numtriangles = mesh->numtriangles;
1267 m.numverts = mesh->numverts;
1268 m.index = mesh->index;
1269 m.texcoords[0] = &mesh->vertex->st[0];
1270 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1272 softwaretransform(v->v, sv->v);
1276 sv->c[3] = currentrenderentity->alpha;
1278 if (RSurf_Light(surf->dlightbits, m.numverts))
1282 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1284 VectorSubtract(sv->v, r_origin, diff);
1285 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1296 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1299 float diff[3], ifog;
1305 memset(&m, 0, sizeof(m));
1306 if (currentrenderentity->effects & EF_ADDITIVE)
1307 m.transparent = true;
1308 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1309 m.transparent = true;
1311 m.transparent = false;
1312 m.blendfunc1 = GL_SRC_ALPHA;
1313 m.blendfunc2 = GL_ONE;
1317 m.ca = currentrenderentity->alpha;
1318 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1319 m.texcoordstep[0] = sizeof(surfvertex_t);
1320 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1322 m.numtriangles = mesh->numtriangles;
1323 m.numverts = mesh->numverts;
1324 m.index = mesh->index;
1325 m.texcoords[0] = &mesh->vertex->st[0];
1328 m.color = &svert[0].c[0];
1329 m.colorstep = sizeof(surfvert_t);
1330 if (softwaretransform_complexity)
1332 m.vertex = &svert[0].v[0];
1333 m.vertexstep = sizeof(surfvert_t);
1334 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1336 softwaretransform(v->v, sv->v);
1337 VectorSubtract(sv->v, r_origin, diff);
1338 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1339 sv->c[0] = m.cr * ifog;
1340 sv->c[1] = m.cg * ifog;
1341 sv->c[2] = m.cb * ifog;
1347 m.vertex = &mesh->vertex->v[0];
1348 m.vertexstep = sizeof(surfvertex_t);
1349 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1351 VectorSubtract(v->v, r_origin, diff);
1352 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1353 sv->c[0] = m.cr * ifog;
1354 sv->c[1] = m.cg * ifog;
1355 sv->c[2] = m.cb * ifog;
1362 if (softwaretransform_complexity)
1364 m.vertex = &svert[0].v[0];
1365 m.vertexstep = sizeof(surfvert_t);
1366 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1367 softwaretransform(v->v, sv->v);
1371 m.vertex = &mesh->vertex->v[0];
1372 m.vertexstep = sizeof(surfvertex_t);
1379 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1388 memset(&m, 0, sizeof(m));
1389 if (currentrenderentity->effects & EF_ADDITIVE)
1390 m.transparent = true;
1391 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1392 m.transparent = true;
1394 m.transparent = false;
1395 m.blendfunc1 = GL_SRC_ALPHA;
1396 m.blendfunc2 = GL_ONE;
1397 m.color = &svert[0].c[0];
1398 m.colorstep = sizeof(surfvert_t);
1399 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1400 m.texcoordstep[0] = sizeof(surfvertex_t);
1401 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1403 m.numtriangles = mesh->numtriangles;
1404 m.numverts = mesh->numverts;
1405 m.index = mesh->index;
1406 m.texcoords[0] = &mesh->vertex->st[0];
1407 if (softwaretransform_complexity)
1409 m.vertex = &svert[0].v[0];
1410 m.vertexstep = sizeof(surfvert_t);
1411 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1413 softwaretransform(v->v, sv->v);
1414 VectorSubtract(sv->v, r_origin, diff);
1415 sv->c[0] = fogcolor[0];
1416 sv->c[1] = fogcolor[1];
1417 sv->c[2] = fogcolor[2];
1418 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1423 m.vertex = &mesh->vertex->v[0];
1424 m.vertexstep = sizeof(surfvertex_t);
1425 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1427 VectorSubtract(v->v, r_origin, diff);
1428 sv->c[0] = fogcolor[0];
1429 sv->c[1] = fogcolor[1];
1430 sv->c[2] = fogcolor[2];
1431 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1438 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1441 for (surf = firstsurf;surf;surf = surf->chain)
1444 RSurfShader_Wall_Pass_BaseFullbright(surf);
1446 for (surf = firstsurf;surf;surf = surf->chain)
1447 if (surf->currenttexture->glowtexture)
1448 RSurfShader_Wall_Pass_Glow(surf);
1450 for (surf = firstsurf;surf;surf = surf->chain)
1451 RSurfShader_Wall_Pass_Fog(surf);
1454 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1457 for (surf = firstsurf;surf;surf = surf->chain)
1460 RSurfShader_Wall_Pass_BaseVertex(surf);
1462 for (surf = firstsurf;surf;surf = surf->chain)
1463 if (surf->currenttexture->glowtexture)
1464 RSurfShader_Wall_Pass_Glow(surf);
1466 for (surf = firstsurf;surf;surf = surf->chain)
1467 RSurfShader_Wall_Pass_Fog(surf);
1470 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1473 if (r_vertexsurfaces.integer)
1475 for (surf = firstsurf;surf;surf = surf->chain)
1478 RSurfShader_Wall_Pass_BaseVertex(surf);
1480 for (surf = firstsurf;surf;surf = surf->chain)
1481 if (surf->currenttexture->glowtexture)
1482 RSurfShader_Wall_Pass_Glow(surf);
1484 for (surf = firstsurf;surf;surf = surf->chain)
1485 RSurfShader_Wall_Pass_Fog(surf);
1487 else if (r_multitexture.integer)
1489 if (r_dlightmap.integer)
1491 for (surf = firstsurf;surf;surf = surf->chain)
1494 RSurfShader_Wall_Pass_BaseMTex(surf);
1496 for (surf = firstsurf;surf;surf = surf->chain)
1497 if (surf->currenttexture->glowtexture)
1498 RSurfShader_Wall_Pass_Glow(surf);
1500 for (surf = firstsurf;surf;surf = surf->chain)
1501 RSurfShader_Wall_Pass_Fog(surf);
1505 for (surf = firstsurf;surf;surf = surf->chain)
1508 RSurfShader_Wall_Pass_BaseMTex(surf);
1510 for (surf = firstsurf;surf;surf = surf->chain)
1511 if (surf->dlightframe == r_framecount)
1512 RSurfShader_Wall_Pass_Light(surf);
1513 for (surf = firstsurf;surf;surf = surf->chain)
1514 if (surf->currenttexture->glowtexture)
1515 RSurfShader_Wall_Pass_Glow(surf);
1517 for (surf = firstsurf;surf;surf = surf->chain)
1518 RSurfShader_Wall_Pass_Fog(surf);
1521 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1523 for (surf = firstsurf;surf;surf = surf->chain)
1526 RSurfShader_Wall_Pass_BaseVertex(surf);
1528 for (surf = firstsurf;surf;surf = surf->chain)
1529 if (surf->currenttexture->glowtexture)
1530 RSurfShader_Wall_Pass_Glow(surf);
1532 for (surf = firstsurf;surf;surf = surf->chain)
1533 RSurfShader_Wall_Pass_Fog(surf);
1537 if (r_dlightmap.integer)
1539 for (surf = firstsurf;surf;surf = surf->chain)
1542 RSurfShader_Wall_Pass_BaseTexture(surf);
1544 for (surf = firstsurf;surf;surf = surf->chain)
1545 RSurfShader_Wall_Pass_BaseLightmap(surf);
1546 for (surf = firstsurf;surf;surf = surf->chain)
1547 if (surf->currenttexture->glowtexture)
1548 RSurfShader_Wall_Pass_Glow(surf);
1550 for (surf = firstsurf;surf;surf = surf->chain)
1551 RSurfShader_Wall_Pass_Fog(surf);
1555 for (surf = firstsurf;surf;surf = surf->chain)
1558 RSurfShader_Wall_Pass_BaseTexture(surf);
1560 for (surf = firstsurf;surf;surf = surf->chain)
1561 RSurfShader_Wall_Pass_BaseLightmap(surf);
1562 for (surf = firstsurf;surf;surf = surf->chain)
1563 if (surf->dlightframe == r_framecount)
1564 RSurfShader_Wall_Pass_Light(surf);
1565 for (surf = firstsurf;surf;surf = surf->chain)
1566 if (surf->currenttexture->glowtexture)
1567 RSurfShader_Wall_Pass_Glow(surf);
1569 for (surf = firstsurf;surf;surf = surf->chain)
1570 RSurfShader_Wall_Pass_Fog(surf);
1576 =============================================================
1580 =============================================================
1583 static void RSurf_Callback(void *data, void *junk)
1585 ((msurface_t *)data)->visframe = r_framecount;
1588 static void R_SolidWorldNode (void)
1590 if (r_viewleaf->contents != CONTENTS_SOLID)
1593 mportal_t *p, *pstack[8192];
1594 msurface_t *surf, **mark, **endmark;
1597 // LordHavoc: portal-passage worldnode; follows portals leading
1598 // outward from viewleaf, if a portal leads offscreen it is not
1599 // followed, in indoor maps this can often cull a great deal of
1600 // geometry away when pvs data is not present (useful with pvs as well)
1603 leaf->worldnodeframe = r_framecount;
1608 leaf->visframe = r_framecount;
1610 if (leaf->nummarksurfaces)
1612 mark = leaf->firstmarksurface;
1613 endmark = mark + leaf->nummarksurfaces;
1619 // make sure surfaces are only processed once
1620 if (surf->worldnodeframe == r_framecount)
1622 surf->worldnodeframe = r_framecount;
1623 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1625 if (surf->flags & SURF_PLANEBACK)
1627 VectorNegate(surf->plane->normal, plane.normal);
1628 plane.dist = -surf->plane->dist;
1629 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1634 if (!(surf->flags & SURF_PLANEBACK))
1635 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);
1638 while (mark < endmark);
1645 // make sure surfaces are only processed once
1646 if (surf->worldnodeframe == r_framecount)
1648 surf->worldnodeframe = r_framecount;
1649 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1651 if (surf->flags & SURF_PLANEBACK)
1652 surf->visframe = r_framecount;
1656 if (!(surf->flags & SURF_PLANEBACK))
1657 surf->visframe = r_framecount;
1660 while (mark < endmark);
1664 // follow portals into other leafs
1666 for (;p;p = p->next)
1668 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1671 if (leaf->worldnodeframe != r_framecount)
1673 leaf->worldnodeframe = r_framecount;
1674 if (leaf->contents != CONTENTS_SOLID)
1676 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1678 p->visframe = r_framecount;
1679 pstack[portalstack++] = p;
1683 p = pstack[--portalstack];
1695 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1696 int nodestackpos = 0;
1697 // LordHavoc: recursive descending worldnode; if portals are not
1698 // available, this is a good last resort, can cull large amounts of
1699 // geometry, but is more time consuming than portal-passage and renders
1700 // things behind walls
1703 if (R_NotCulledBox(node->mins, node->maxs))
1705 if (node->numsurfaces)
1709 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1711 if (PlaneDiff (r_origin, node->plane) < 0)
1713 for (;surf < surfend;surf++)
1715 if (surf->flags & SURF_PLANEBACK)
1717 VectorNegate(surf->plane->normal, plane.normal);
1718 plane.dist = -surf->plane->dist;
1719 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1725 for (;surf < surfend;surf++)
1727 if (!(surf->flags & SURF_PLANEBACK))
1728 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);
1734 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1735 if (PlaneDiff (r_origin, node->plane) < 0)
1737 for (;surf < surfend;surf++)
1739 if (surf->flags & SURF_PLANEBACK)
1740 surf->visframe = r_framecount;
1745 for (;surf < surfend;surf++)
1747 if (!(surf->flags & SURF_PLANEBACK))
1748 surf->visframe = r_framecount;
1754 // recurse down the children
1755 if (node->children[0]->contents >= 0)
1757 if (node->children[1]->contents >= 0)
1759 if (nodestackpos < 8192)
1760 nodestack[nodestackpos++] = node->children[1];
1761 node = node->children[0];
1765 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1766 node = node->children[0];
1771 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1772 if (node->children[1]->contents >= 0)
1774 node = node->children[1];
1777 else if (nodestackpos > 0)
1779 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1780 node = nodestack[--nodestackpos];
1785 else if (nodestackpos > 0)
1787 node = nodestack[--nodestackpos];
1793 static int r_portalframecount = 0;
1795 static void R_PVSWorldNode()
1798 mportal_t *p, *pstack[8192];
1799 msurface_t *surf, **mark, **endmark;
1804 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1807 leaf->worldnodeframe = r_framecount;
1812 leaf->visframe = r_framecount;
1814 if (leaf->nummarksurfaces)
1816 mark = leaf->firstmarksurface;
1817 endmark = mark + leaf->nummarksurfaces;
1823 // make sure surfaces are only processed once
1824 if (surf->worldnodeframe == r_framecount)
1826 surf->worldnodeframe = r_framecount;
1827 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1829 if (surf->flags & SURF_PLANEBACK)
1831 VectorNegate(surf->plane->normal, plane.normal);
1832 plane.dist = -surf->plane->dist;
1833 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1838 if (!(surf->flags & SURF_PLANEBACK))
1839 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);
1842 while (mark < endmark);
1849 // make sure surfaces are only processed once
1850 if (surf->worldnodeframe == r_framecount)
1852 surf->worldnodeframe = r_framecount;
1853 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1855 if (surf->flags & SURF_PLANEBACK)
1856 surf->visframe = r_framecount;
1860 if (!(surf->flags & SURF_PLANEBACK))
1861 surf->visframe = r_framecount;
1864 while (mark < endmark);
1868 // follow portals into other leafs
1869 for (p = leaf->portals;p;p = p->next)
1871 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1874 if (leaf->worldnodeframe != r_framecount)
1876 leaf->worldnodeframe = r_framecount;
1877 if (leaf->contents != CONTENTS_SOLID)
1879 i = (leaf - cl.worldmodel->leafs) - 1;
1880 if (worldvis[i>>3] & (1<<(i&7)))
1882 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1884 pstack[portalstack++] = p;
1888 p = pstack[--portalstack];
1900 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1901 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1902 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1903 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1904 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1906 int Cshader_count = 5;
1907 Cshader_t *Cshaders[5] =
1909 &Cshader_wall_vertex,
1910 &Cshader_wall_lightmap,
1911 &Cshader_wall_fullbright,
1916 void R_PrepareSurfaces(void)
1923 for (i = 0;i < Cshader_count;i++)
1924 Cshaders[i]->chain = NULL;
1926 model = currentrenderentity->model;
1928 for (i = 0;i < model->nummodelsurfaces;i++)
1930 surf = model->modelsortedsurfaces[i];
1931 if (surf->visframe == r_framecount)
1933 if (surf->insertframe != r_framecount)
1935 surf->insertframe = r_framecount;
1937 t = surf->texinfo->texture;
1938 if (t->alternate_anims != NULL && currentrenderentity->frame)
1939 t = t->alternate_anims;
1940 if (t->anim_total >= 2)
1941 t = t->anim_frames[(int)(cl.time * 5.0f) % t->anim_total];
1942 surf->currenttexture = t;
1945 surf->chain = surf->shader->chain;
1946 surf->shader->chain = surf;
1951 void R_DrawSurfaces (int type)
1956 for (i = 0;i < Cshader_count;i++)
1958 shader = Cshaders[i];
1959 if (shader->chain && shader->shaderfunc[type])
1960 shader->shaderfunc[type](shader->chain);
1964 static float portalpointbuffer[256][3];
1966 void R_DrawPortals(void)
1969 mportal_t *portal, *endportal;
1972 drawportals = r_drawportals.integer;
1974 if (drawportals < 1)
1977 memset(&m, 0, sizeof(m));
1978 m.transparent = true;
1979 m.blendfunc1 = GL_SRC_ALPHA;
1980 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1981 m.vertex = &portalpointbuffer[0][0];
1982 m.vertexstep = sizeof(float[3]);
1984 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1986 if (portal->visframe == r_portalframecount)
1988 if (portal->numpoints <= 256)
1990 i = portal - cl.worldmodel->portals;
1991 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
1992 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
1993 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
1994 point = portal->points;
1995 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1997 for (i = portal->numpoints - 1;i >= 0;i--)
1998 VectorCopy(point[i].position, portalpointbuffer[i]);
2002 for (i = 0;i < portal->numpoints;i++)
2003 VectorCopy(point[i].position, portalpointbuffer[i]);
2005 R_Mesh_DrawPolygon(&m, portal->numpoints);
2011 void R_SetupForBModelRendering(void)
2018 // because bmodels can be reused, we have to decide which things to render
2019 // from scratch every time
2021 model = currentrenderentity->model;
2023 softwaretransformforentity (currentrenderentity);
2024 softwareuntransform(r_origin, modelorg);
2026 for (i = 0;i < model->nummodelsurfaces;i++)
2028 surf = model->modelsortedsurfaces[i];
2029 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2030 surf->visframe = r_framecount;
2032 surf->visframe = -1;
2033 surf->worldnodeframe = -1;
2034 surf->lightframe = -1;
2035 surf->dlightframe = -1;
2036 surf->insertframe = -1;
2040 void R_SetupForWorldRendering(void)
2042 // there is only one instance of the world, but it can be rendered in
2045 currentrenderentity = &cl_entities[0].render;
2046 softwaretransformidentity();
2049 static void R_SurfMarkLights (void)
2054 if (r_dynamic.integer)
2057 if (!r_vertexsurfaces.integer)
2059 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2061 surf = currentrenderentity->model->modelsortedsurfaces[i];
2062 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2064 if (surf->cached_dlight
2065 || surf->cached_ambient != r_ambient.value
2066 || surf->cached_lightscalebit != lightscalebit)
2067 R_BuildLightMap(surf, false); // base lighting changed
2068 else if (r_dynamic.integer)
2070 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2071 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2072 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2073 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2074 R_BuildLightMap(surf, false); // base lighting changed
2075 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2076 R_BuildLightMap(surf, true); // only dlights
2083 void R_MarkWorldLights(void)
2085 R_SetupForWorldRendering();
2094 void R_DrawWorld (void)
2096 R_SetupForWorldRendering();
2098 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2099 R_SolidWorldNode ();
2109 void R_DrawBrushModelSky (void)
2111 R_SetupForBModelRendering();
2113 R_PrepareSurfaces();
2114 R_DrawSurfaces(SHADERSTAGE_SKY);
2117 void R_DrawBrushModelNormal (void)
2121 // have to flush queue because of possible lightmap reuse
2124 R_SetupForBModelRendering();
2128 R_PrepareSurfaces();
2130 if (!skyrendermasked)
2131 R_DrawSurfaces(SHADERSTAGE_SKY);
2132 R_DrawSurfaces(SHADERSTAGE_NORMAL);