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 unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31 cvar_t r_ambient = {0, "r_ambient", "0"};
32 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
33 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
38 static int dlightdivtable[32768];
40 static int R_IntAddDynamicLights (msurface_t *surf)
42 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
44 float dist, impact[3], local[3];
46 // LordHavoc: use 64bit integer... shame it's not very standardized...
47 #if _MSC_VER || __BORLANDC__
55 smax = (surf->extents[0] >> 4) + 1;
56 tmax = (surf->extents[1] >> 4) + 1;
59 for (lnum = 0; lnum < r_numdlights; lnum++)
61 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
62 continue; // not lit by this light
64 softwareuntransform(r_dlight[lnum].origin, local);
65 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
67 // for comparisons to minimum acceptable light
68 // compensate for LIGHTOFFSET
69 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
76 if (surf->plane->type < 3)
78 VectorCopy(local, impact);
79 impact[surf->plane->type] -= dist;
83 impact[0] = local[0] - surf->plane->normal[0] * dist;
84 impact[1] = local[1] - surf->plane->normal[1] * dist;
85 impact[2] = local[2] - surf->plane->normal[2] * dist;
88 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
89 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
91 s = bound(0, impacts, smax * 16) - impacts;
92 t = bound(0, impactt, tmax * 16) - impactt;
93 i = s * s + t * t + dist2;
97 // reduce calculations
98 for (s = 0, i = impacts; s < smax; s++, i -= 16)
99 sdtable[s] = i * i + dist2;
101 maxdist3 = maxdist - dist2;
103 // convert to 8.8 blocklights format
104 red = r_dlight[lnum].light[0];
105 green = r_dlight[lnum].light[1];
106 blue = r_dlight[lnum].light[2];
107 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
111 for (t = 0;t < tmax;t++, i -= 16)
114 // make sure some part of it is visible on this line
117 maxdist2 = maxdist - td;
118 for (s = 0;s < smax;s++)
120 if (sdtable[s] < maxdist2)
122 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
125 bl[0] += (red * k) >> 8;
126 bl[1] += (green * k) >> 8;
127 bl[2] += (blue * k) >> 8;
141 static int R_FloatAddDynamicLights (msurface_t *surf)
143 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
144 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
148 smax = (surf->extents[0] >> 4) + 1;
149 tmax = (surf->extents[1] >> 4) + 1;
152 for (lnum = 0; lnum < r_numdlights; lnum++)
154 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155 continue; // not lit by this light
157 softwareuntransform(r_dlight[lnum].origin, local);
158 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
160 // for comparisons to minimum acceptable light
161 // compensate for LIGHTOFFSET
162 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
165 dist2 += LIGHTOFFSET;
166 if (dist2 >= maxdist)
169 if (surf->plane->type < 3)
171 VectorCopy(local, impact);
172 impact[surf->plane->type] -= dist;
176 impact[0] = local[0] - surf->plane->normal[0] * dist;
177 impact[1] = local[1] - surf->plane->normal[1] * dist;
178 impact[2] = local[2] - surf->plane->normal[2] * dist;
181 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
184 td = bound(0, impacts, smax * 16) - impacts;
185 td1 = bound(0, impactt, tmax * 16) - impactt;
186 td = td * td + td1 * td1 + dist2;
190 // reduce calculations
191 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192 sdtable[s] = td1 * td1 + dist2;
194 maxdist3 = maxdist - dist2;
196 // convert to 8.8 blocklights format
197 red = r_dlight[lnum].light[0];
198 green = r_dlight[lnum].light[1];
199 blue = r_dlight[lnum].light[2];
200 subtract = r_dlight[lnum].lightsubtract * 16384.0f;
201 bl = floatblocklights;
204 for (t = 0;t < tmax;t++, td1 -= 16.0f)
207 // make sure some part of it is visible on this line
210 maxdist2 = maxdist - td;
211 for (s = 0;s < smax;s++)
213 if (sdtable[s] < maxdist2)
215 k = (16384.0f / (sdtable[s] + td)) - subtract;
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
238 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
240 if (!r_floatbuildlightmap.integer)
242 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
243 unsigned int *bl, scale;
244 qbyte *lightmap, *out, *stain;
246 // update cached lighting info
247 surf->cached_dlight = 0;
248 surf->cached_lightscalebit = lightscalebit;
249 surf->cached_ambient = r_ambient.value;
250 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
251 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
252 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
253 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
255 smax = (surf->extents[0]>>4)+1;
256 tmax = (surf->extents[1]>>4)+1;
259 lightmap = surf->samples;
261 // set to full bright if no light data
263 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
265 for (i = 0;i < size3;i++)
271 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
274 for (i = 0;i < size3;i++)
278 memset(bl, 0, size*3*sizeof(unsigned int));
280 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
282 surf->cached_dlight = R_IntAddDynamicLights(surf);
283 if (surf->cached_dlight)
285 else if (dlightchanged)
286 return; // don't upload if only updating dlights and none mattered
289 // add all the lightmaps
293 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
294 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
295 bl[i] += lightmap[i] * scale;
299 stain = surf->stainsamples;
302 // deal with lightmap brightness scale
303 shift = 7 + lightscalebit + 8;
304 if (currentrenderentity->model->lightmaprgba)
306 stride = (surf->lightmaptexturestride - smax) * 4;
307 for (i = 0;i < tmax;i++, out += stride)
309 for (j = 0;j < smax;j++)
311 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 stride = (surf->lightmaptexturestride - smax) * 3;
321 for (i = 0;i < tmax;i++, out += stride)
323 for (j = 0;j < smax;j++)
325 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
332 R_UpdateTexture(surf->lightmaptexture, templight);
336 int smax, tmax, i, j, size, size3, maps, stride, l;
338 qbyte *lightmap, *out, *stain;
340 // update cached lighting info
341 surf->cached_dlight = 0;
342 surf->cached_lightscalebit = lightscalebit;
343 surf->cached_ambient = r_ambient.value;
344 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
345 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
346 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
347 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
349 smax = (surf->extents[0]>>4)+1;
350 tmax = (surf->extents[1]>>4)+1;
353 lightmap = surf->samples;
355 // set to full bright if no light data
356 bl = floatblocklights;
357 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
360 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
365 for (i = 0;i < size3;i++)
369 memset(bl, 0, size*3*sizeof(float));
371 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
373 surf->cached_dlight = R_FloatAddDynamicLights(surf);
374 if (surf->cached_dlight)
376 else if (dlightchanged)
377 return; // don't upload if only updating dlights and none mattered
380 // add all the lightmaps
383 bl = floatblocklights;
384 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
385 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
386 bl[i] += lightmap[i] * scale;
389 stain = surf->stainsamples;
390 bl = floatblocklights;
392 // deal with lightmap brightness scale
393 scale = 1.0f / (1 << (7 + lightscalebit + 8));
394 if (currentrenderentity->model->lightmaprgba)
396 stride = (surf->lightmaptexturestride - smax) * 4;
397 for (i = 0;i < tmax;i++, out += stride)
399 for (j = 0;j < smax;j++)
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 stride = (surf->lightmaptexturestride - smax) * 3;
411 for (i = 0;i < tmax;i++, out += stride)
413 for (j = 0;j < smax;j++)
415 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
422 R_UpdateTexture(surf->lightmaptexture, templight);
426 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
429 msurface_t *surf, *endsurf;
430 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
433 // LordHavoc: use 64bit integer... shame it's not very standardized...
434 #if _MSC_VER || __BORLANDC__
441 // for comparisons to minimum acceptable light
442 // compensate for 4096 offset
443 maxdist = radius * radius + 4096;
445 // clamp radius to avoid exceeding 32768 entry division table
446 if (maxdist > 4194304)
449 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
452 if (node->contents < 0)
454 ndist = PlaneDiff(origin, node->plane);
457 node = node->children[0];
462 node = node->children[1];
466 dist2 = ndist * ndist;
470 maxdist3 = maxdist - dist2;
472 if (node->plane->type < 3)
474 VectorCopy(origin, impact);
475 impact[node->plane->type] -= ndist;
479 impact[0] = origin[0] - node->plane->normal[0] * ndist;
480 impact[1] = origin[1] - node->plane->normal[1] * ndist;
481 impact[2] = origin[2] - node->plane->normal[2] * ndist;
484 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
486 if (surf->stainsamples)
488 smax = (surf->extents[0] >> 4) + 1;
489 tmax = (surf->extents[1] >> 4) + 1;
491 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
492 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
494 s = bound(0, impacts, smax * 16) - impacts;
495 t = bound(0, impactt, tmax * 16) - impactt;
496 i = s * s + t * t + dist2;
500 // reduce calculations
501 for (s = 0, i = impacts; s < smax; s++, i -= 16)
502 sdtable[s] = i * i + dist2;
504 // convert to 8.8 blocklights format
505 bl = surf->stainsamples;
510 for (t = 0;t < tmax;t++, i -= 16)
513 // make sure some part of it is visible on this line
516 maxdist2 = maxdist - td;
517 for (s = 0;s < smax;s++)
519 if (sdtable[s] < maxdist2)
521 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
524 ratio = rand() & 255;
525 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
529 a = bound(0, a, 256);
530 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
531 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
532 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
533 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
534 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
535 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
546 // force lightmap upload
548 surf->cached_dlight = true;
553 if (node->children[0]->contents >= 0)
555 if (node->children[1]->contents >= 0)
557 R_StainNode(node->children[0], model, origin, radius, icolor);
558 node = node->children[1];
563 node = node->children[0];
567 else if (node->children[1]->contents >= 0)
569 node = node->children[1];
574 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
577 entity_render_t *ent;
589 model = cl.worldmodel;
590 softwaretransformidentity();
591 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
593 // look for embedded bmodels
594 for (n = 0;n < cl_num_brushmodel_entities;n++)
596 ent = cl_brushmodel_entities[n];
598 if (model && model->name[0] == '*')
600 Mod_CheckLoaded(model);
601 if (model->type == mod_brush)
603 softwaretransformforentity(ent);
604 softwareuntransform(origin, org);
605 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
613 =============================================================
617 =============================================================
621 static float turbsin[256] =
623 #include "gl_warp_sin.h"
625 #define TURBSCALE (256.0 / (2 * M_PI))
627 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
628 #define MAX_SURFVERTS 3072
637 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
639 static void RSurfShader_Sky(msurface_t *firstsurf)
643 float number, length, dir[3], speedscale;
649 // LordHavoc: HalfLife maps have freaky skypolys...
650 if (currentrenderentity->model->ishlbsp)
657 skyrendernow = false;
660 for (surf = firstsurf;surf;surf = surf->chain)
662 // draw depth-only polys
663 memset(&m, 0, sizeof(m));
664 m.transparent = false;
665 m.blendfunc1 = GL_ZERO;
666 m.blendfunc2 = GL_ONE;
668 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
670 m.numtriangles = mesh->numtriangles;
671 m.numverts = mesh->numverts;
672 m.index = mesh->index;
673 if (softwaretransform_complexity)
675 m.vertex = &svert[0].v[0];
676 m.vertexstep = sizeof(surfvert_t);
677 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
678 softwaretransform(v->v, sv->v);
682 m.vertex = &mesh->vertex[0].v[0];
683 m.vertexstep = sizeof(surfvertex_t);
689 else if (skyrenderglquake)
691 for (surf = firstsurf;surf;surf = surf->chain)
693 memset(&m, 0, sizeof(m));
694 m.transparent = false;
695 m.blendfunc1 = GL_ONE;
696 m.blendfunc2 = GL_ZERO;
697 m.vertex = &svert[0].v[0];
698 m.vertexstep = sizeof(surfvert_t);
703 m.tex[0] = R_GetTexture(solidskytexture);
704 m.texcoords[0] = &svert[0].st[0];
705 m.texcoordstep[0] = sizeof(surfvert_t);
706 speedscale = cl.time * (8.0/128.0);
707 speedscale -= (int)speedscale;
708 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
710 m.numtriangles = mesh->numtriangles;
711 m.numverts = mesh->numverts;
712 m.index = mesh->index;
713 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
715 softwaretransform(v->v, sv->v);
716 VectorSubtract (sv->v, r_origin, dir);
717 // flatten the sphere
720 number = DotProduct(dir, dir);
722 length = 3.0f / sqrt(number);
724 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
725 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
728 sv->st[0] = speedscale + dir[0] * length;
729 sv->st[1] = speedscale + dir[1] * length;
737 for (surf = firstsurf;surf;surf = surf->chain)
740 memset(&m, 0, sizeof(m));
741 m.transparent = false;
742 m.blendfunc1 = GL_ONE;
743 m.blendfunc2 = GL_ZERO;
748 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
750 m.numtriangles = mesh->numtriangles;
751 m.numverts = mesh->numverts;
752 m.index = mesh->index;
753 if (softwaretransform_complexity)
755 m.vertex = &svert[0].v[0];
756 m.vertexstep = sizeof(surfvert_t);
757 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
758 softwaretransform(v->v, sv->v);
762 m.vertex = &mesh->vertex[0].v[0];
763 m.vertexstep = sizeof(surfvertex_t);
769 if (skyrenderglquake)
771 for (surf = firstsurf;surf;surf = surf->chain)
773 memset(&m, 0, sizeof(m));
774 m.transparent = false;
775 m.blendfunc1 = GL_SRC_ALPHA;
776 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
777 m.vertex = &svert[0].v[0];
778 m.vertexstep = sizeof(surfvert_t);
783 m.tex[0] = R_GetTexture(alphaskytexture);
784 m.texcoords[0] = &svert[0].st[0];
785 m.texcoordstep[0] = sizeof(surfvert_t);
786 speedscale = cl.time * (16.0/128.0);
787 speedscale -= (int)speedscale;
788 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
790 m.numtriangles = mesh->numtriangles;
791 m.numverts = mesh->numverts;
792 m.index = mesh->index;
793 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
795 softwaretransform(v->v, sv->v);
796 VectorSubtract (sv->v, r_origin, dir);
797 // flatten the sphere
800 number = DotProduct(dir, dir);
802 length = 3.0f / sqrt(number);
804 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
805 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
808 sv->st[0] = speedscale + dir[0] * length;
809 sv->st[1] = speedscale + dir[1] * length;
817 static int RSurf_Light(int *dlightbits, int numverts)
820 int i, l, lit = false;
824 for (l = 0;l < r_numdlights;l++)
826 if (dlightbits[l >> 5] & (1 << (l & 31)))
829 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
830 VectorCopy(rd->origin, lightorigin);
831 for (i = 0, sv = svert;i < numverts;i++, sv++)
833 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
834 if (f < rd->cullradius2)
836 f = (1.0f / f) - rd->lightsubtract;
837 sv->c[0] += rd->light[0] * f;
838 sv->c[1] += rd->light[1] * f;
839 sv->c[2] += rd->light[2] * f;
848 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
851 float diff[3], alpha, ifog;
856 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
858 memset(&m, 0, sizeof(m));
859 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
861 m.transparent = true;
862 m.blendfunc1 = GL_SRC_ALPHA;
863 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
867 m.transparent = false;
868 m.blendfunc1 = GL_ONE;
869 m.blendfunc2 = GL_ZERO;
871 m.vertex = &svert[0].v[0];
872 m.vertexstep = sizeof(surfvert_t);
873 m.color = &svert[0].c[0];
874 m.colorstep = sizeof(surfvert_t);
875 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
876 m.texcoords[0] = &svert[0].st[0];
877 m.texcoordstep[0] = sizeof(surfvert_t);
878 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
880 m.numtriangles = mesh->numtriangles;
881 m.numverts = mesh->numverts;
882 m.index = mesh->index;
883 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
885 softwaretransform(v->v, sv->v);
886 if (r_waterripple.value)
887 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];
888 if (surf->flags & SURF_DRAWFULLBRIGHT)
902 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
903 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
905 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
906 RSurf_Light(surf->dlightbits, m.numverts);
907 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
909 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
911 VectorSubtract(sv->v, r_origin, diff);
912 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
922 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
925 float diff[3], alpha, ifog;
930 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
932 memset(&m, 0, sizeof(m));
933 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
934 m.blendfunc1 = GL_SRC_ALPHA;
935 m.blendfunc2 = GL_ONE;
936 m.vertex = &svert[0].v[0];
937 m.vertexstep = sizeof(surfvert_t);
942 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
943 m.texcoords[0] = &svert[0].st[0];
944 m.texcoordstep[0] = sizeof(surfvert_t);
945 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
947 m.numtriangles = mesh->numtriangles;
948 m.numverts = mesh->numverts;
949 m.index = mesh->index;
952 m.color = &svert[0].c[0];
953 m.colorstep = sizeof(surfvert_t);
954 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
956 softwaretransform(v->v, sv->v);
957 if (r_waterripple.value)
958 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];
959 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
960 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
961 VectorSubtract(sv->v, r_origin, diff);
962 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
963 sv->c[0] = m.cr * ifog;
964 sv->c[1] = m.cg * ifog;
965 sv->c[2] = m.cb * ifog;
971 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
973 softwaretransform(v->v, sv->v);
974 if (r_waterripple.value)
975 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];
976 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
977 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
984 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
993 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
995 memset(&m, 0, sizeof(m));
996 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
997 m.blendfunc1 = GL_SRC_ALPHA;
998 m.blendfunc2 = GL_ONE;
999 m.vertex = &svert[0].v[0];
1000 m.vertexstep = sizeof(surfvert_t);
1001 m.color = &svert[0].c[0];
1002 m.colorstep = sizeof(surfvert_t);
1003 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1004 m.texcoords[0] = &svert[0].st[0];
1005 m.texcoordstep[0] = sizeof(surfvert_t);
1007 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1009 m.numtriangles = mesh->numtriangles;
1010 m.numverts = mesh->numverts;
1011 m.index = mesh->index;
1012 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1014 softwaretransform(v->v, sv->v);
1015 if (r_waterripple.value)
1016 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];
1019 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1020 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1022 VectorSubtract(sv->v, r_origin, diff);
1023 sv->c[0] = fogcolor[0];
1024 sv->c[1] = fogcolor[1];
1025 sv->c[2] = fogcolor[2];
1026 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
1032 static void RSurfShader_Water(msurface_t *firstsurf)
1035 for (surf = firstsurf;surf;surf = surf->chain)
1036 RSurfShader_Water_Pass_Base(surf);
1037 for (surf = firstsurf;surf;surf = surf->chain)
1038 if (surf->currenttexture->glowtexture)
1039 RSurfShader_Water_Pass_Glow(surf);
1041 for (surf = firstsurf;surf;surf = surf->chain)
1042 RSurfShader_Water_Pass_Fog(surf);
1045 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
1048 float diff[3], ifog;
1054 memset(&m, 0, sizeof(m));
1055 if (currentrenderentity->effects & EF_ADDITIVE)
1057 m.transparent = true;
1058 m.blendfunc1 = GL_SRC_ALPHA;
1059 m.blendfunc2 = GL_ONE;
1061 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1063 m.transparent = true;
1064 m.blendfunc1 = GL_SRC_ALPHA;
1065 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1069 m.transparent = false;
1070 m.blendfunc1 = GL_ONE;
1071 m.blendfunc2 = GL_ZERO;
1073 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
1074 m.ca = currentrenderentity->alpha;
1075 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1076 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1077 m.texcoordstep[0] = sizeof(surfvertex_t);
1078 m.texcoordstep[1] = sizeof(surfvertex_t);
1079 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1081 m.numtriangles = mesh->numtriangles;
1082 m.numverts = mesh->numverts;
1083 m.index = mesh->index;
1084 m.texcoords[0] = &mesh->vertex->st[0];
1085 m.texcoords[1] = &mesh->vertex->uv[0];
1088 m.color = &svert[0].c[0];
1089 m.colorstep = sizeof(surfvert_t);
1090 if (softwaretransform_complexity)
1092 m.vertex = &svert[0].v[0];
1093 m.vertexstep = sizeof(surfvert_t);
1094 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1096 softwaretransform(v->v, sv->v);
1097 VectorSubtract(sv->v, r_origin, diff);
1098 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1099 sv->c[0] = m.cr * ifog;
1100 sv->c[1] = m.cg * ifog;
1101 sv->c[2] = m.cb * ifog;
1107 m.vertex = &mesh->vertex->v[0];
1108 m.vertexstep = sizeof(surfvertex_t);
1109 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1111 VectorSubtract(v->v, r_origin, diff);
1112 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1113 sv->c[0] = m.cr * ifog;
1114 sv->c[1] = m.cg * ifog;
1115 sv->c[2] = m.cb * ifog;
1122 if (softwaretransform_complexity)
1124 m.vertex = &svert[0].v[0];
1125 m.vertexstep = sizeof(surfvert_t);
1126 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1127 softwaretransform(v->v, sv->v);
1131 m.vertex = &mesh->vertex->v[0];
1132 m.vertexstep = sizeof(surfvertex_t);
1139 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1147 memset(&m, 0, sizeof(m));
1148 m.transparent = false;
1149 m.blendfunc1 = GL_ONE;
1150 m.blendfunc2 = GL_ZERO;
1151 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1153 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1154 m.texcoordstep[0] = sizeof(surfvertex_t);
1155 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1157 m.numtriangles = mesh->numtriangles;
1158 m.numverts = mesh->numverts;
1159 m.index = mesh->index;
1160 m.texcoords[0] = &mesh->vertex->st[0];
1161 if (softwaretransform_complexity)
1163 m.vertex = &svert[0].v[0];
1164 m.vertexstep = sizeof(surfvert_t);
1165 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1166 softwaretransform(v->v, sv->v);
1170 m.vertex = &mesh->vertex->v[0];
1171 m.vertexstep = sizeof(surfvertex_t);
1177 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1180 float diff[3], ifog;
1186 memset(&m, 0, sizeof(m));
1187 m.transparent = false;
1188 m.blendfunc1 = GL_ZERO;
1189 m.blendfunc2 = GL_SRC_COLOR;
1190 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1192 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1193 m.texcoordstep[0] = sizeof(surfvertex_t);
1194 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1196 m.numtriangles = mesh->numtriangles;
1197 m.numverts = mesh->numverts;
1198 m.index = mesh->index;
1199 m.texcoords[0] = &mesh->vertex->uv[0];
1202 m.color = &svert[0].c[0];
1203 m.colorstep = sizeof(surfvert_t);
1204 if (softwaretransform_complexity)
1206 m.vertex = &svert[0].v[0];
1207 m.vertexstep = sizeof(surfvert_t);
1208 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1210 softwaretransform(v->v, sv->v);
1211 VectorSubtract(sv->v, r_origin, diff);
1212 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1213 sv->c[0] = m.cr * ifog;
1214 sv->c[1] = m.cg * ifog;
1215 sv->c[2] = m.cb * ifog;
1221 m.vertex = &mesh->vertex->v[0];
1222 m.vertexstep = sizeof(surfvertex_t);
1223 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1225 VectorSubtract(v->v, r_origin, diff);
1226 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1227 sv->c[0] = m.cr * ifog;
1228 sv->c[1] = m.cg * ifog;
1229 sv->c[2] = m.cb * ifog;
1236 if (softwaretransform_complexity)
1238 m.vertex = &svert[0].v[0];
1239 m.vertexstep = sizeof(surfvert_t);
1240 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1241 softwaretransform(v->v, sv->v);
1245 m.vertex = &mesh->vertex->v[0];
1246 m.vertexstep = sizeof(surfvertex_t);
1253 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1256 float c[3], base[3], scale, diff[3], ifog;
1263 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1265 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1267 memset(&m, 0, sizeof(m));
1268 if (currentrenderentity->effects & EF_ADDITIVE)
1270 m.transparent = true;
1271 m.blendfunc1 = GL_SRC_ALPHA;
1272 m.blendfunc2 = GL_ONE;
1274 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1276 m.transparent = true;
1277 m.blendfunc1 = GL_SRC_ALPHA;
1278 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1282 m.transparent = false;
1283 m.blendfunc1 = GL_ONE;
1284 m.blendfunc2 = GL_ZERO;
1286 m.vertex = &svert[0].v[0];
1287 m.vertexstep = sizeof(surfvert_t);
1288 m.color = &svert[0].c[0];
1289 m.colorstep = sizeof(surfvert_t);
1290 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1291 m.texcoordstep[0] = sizeof(surfvertex_t);
1292 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1294 m.numtriangles = mesh->numtriangles;
1295 m.numverts = mesh->numverts;
1296 m.index = mesh->index;
1297 m.texcoords[0] = &mesh->vertex->st[0];
1298 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1300 softwaretransform(v->v, sv->v);
1301 VectorCopy(base, c);
1302 if (surf->styles[0] != 255)
1304 lm = surf->samples + v->lightmapoffset;
1305 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1306 VectorMA(c, scale, lm, c);
1307 if (surf->styles[1] != 255)
1310 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1311 VectorMA(c, scale, lm, c);
1312 if (surf->styles[2] != 255)
1315 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1316 VectorMA(c, scale, lm, c);
1317 if (surf->styles[3] != 255)
1320 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1321 VectorMA(c, scale, lm, c);
1329 sv->c[3] = currentrenderentity->alpha;
1331 if (surf->dlightframe == r_framecount)
1332 RSurf_Light(surf->dlightbits, m.numverts);
1335 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1337 VectorSubtract(sv->v, r_origin, diff);
1338 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1348 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1351 float diff[3], ifog;
1357 memset(&m, 0, sizeof(m));
1358 if (currentrenderentity->effects & EF_ADDITIVE)
1360 m.transparent = true;
1361 m.blendfunc1 = GL_SRC_ALPHA;
1362 m.blendfunc2 = GL_ONE;
1364 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1366 m.transparent = true;
1367 m.blendfunc1 = GL_SRC_ALPHA;
1368 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1372 m.transparent = false;
1373 m.blendfunc1 = GL_ONE;
1374 m.blendfunc2 = GL_ZERO;
1376 m.vertex = &svert[0].v[0];
1377 m.vertexstep = sizeof(surfvert_t);
1378 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1379 m.texcoordstep[0] = sizeof(surfvertex_t);
1380 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1382 m.numtriangles = mesh->numtriangles;
1383 m.numverts = mesh->numverts;
1384 m.index = mesh->index;
1385 m.texcoords[0] = &mesh->vertex->st[0];
1388 m.color = &svert[0].c[0];
1389 m.colorstep = sizeof(surfvert_t);
1390 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1392 softwaretransform(v->v, sv->v);
1393 VectorSubtract(sv->v, r_origin, diff);
1394 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1398 sv->c[3] = currentrenderentity->alpha;
1403 m.cr = m.cg = m.cb = 1;
1404 m.ca = currentrenderentity->alpha;
1405 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1406 softwaretransform(v->v, sv->v);
1412 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1415 float diff[3], ifog;
1421 memset(&m, 0, sizeof(m));
1422 if (currentrenderentity->effects & EF_ADDITIVE)
1423 m.transparent = true;
1424 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1425 m.transparent = true;
1427 m.transparent = false;
1428 m.blendfunc1 = GL_SRC_ALPHA;
1429 m.blendfunc2 = GL_ONE;
1430 m.vertex = &svert[0].v[0];
1431 m.vertexstep = sizeof(surfvert_t);
1432 m.color = &svert[0].c[0];
1433 m.colorstep = sizeof(surfvert_t);
1434 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1435 m.texcoordstep[0] = sizeof(surfvertex_t);
1436 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1438 m.numtriangles = mesh->numtriangles;
1439 m.numverts = mesh->numverts;
1440 m.index = mesh->index;
1441 m.texcoords[0] = &mesh->vertex->st[0];
1442 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1444 softwaretransform(v->v, sv->v);
1448 sv->c[3] = currentrenderentity->alpha;
1450 if (RSurf_Light(surf->dlightbits, m.numverts))
1454 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1456 VectorSubtract(sv->v, r_origin, diff);
1457 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1468 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1471 float diff[3], ifog;
1477 memset(&m, 0, sizeof(m));
1478 if (currentrenderentity->effects & EF_ADDITIVE)
1479 m.transparent = true;
1480 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1481 m.transparent = true;
1483 m.transparent = false;
1484 m.blendfunc1 = GL_SRC_ALPHA;
1485 m.blendfunc2 = GL_ONE;
1489 m.ca = currentrenderentity->alpha;
1490 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1491 m.texcoordstep[0] = sizeof(surfvertex_t);
1492 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1494 m.numtriangles = mesh->numtriangles;
1495 m.numverts = mesh->numverts;
1496 m.index = mesh->index;
1497 m.texcoords[0] = &mesh->vertex->st[0];
1500 m.color = &svert[0].c[0];
1501 m.colorstep = sizeof(surfvert_t);
1502 if (softwaretransform_complexity)
1504 m.vertex = &svert[0].v[0];
1505 m.vertexstep = sizeof(surfvert_t);
1506 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1508 softwaretransform(v->v, sv->v);
1509 VectorSubtract(sv->v, r_origin, diff);
1510 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1511 sv->c[0] = m.cr * ifog;
1512 sv->c[1] = m.cg * ifog;
1513 sv->c[2] = m.cb * ifog;
1519 m.vertex = &mesh->vertex->v[0];
1520 m.vertexstep = sizeof(surfvertex_t);
1521 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1523 VectorSubtract(v->v, r_origin, diff);
1524 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1525 sv->c[0] = m.cr * ifog;
1526 sv->c[1] = m.cg * ifog;
1527 sv->c[2] = m.cb * ifog;
1534 if (softwaretransform_complexity)
1536 m.vertex = &svert[0].v[0];
1537 m.vertexstep = sizeof(surfvert_t);
1538 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1539 softwaretransform(v->v, sv->v);
1543 m.vertex = &mesh->vertex->v[0];
1544 m.vertexstep = sizeof(surfvertex_t);
1551 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1560 memset(&m, 0, sizeof(m));
1561 if (currentrenderentity->effects & EF_ADDITIVE)
1562 m.transparent = true;
1563 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1564 m.transparent = true;
1566 m.transparent = false;
1567 m.blendfunc1 = GL_SRC_ALPHA;
1568 m.blendfunc2 = GL_ONE;
1569 m.color = &svert[0].c[0];
1570 m.colorstep = sizeof(surfvert_t);
1571 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1572 m.texcoordstep[0] = sizeof(surfvertex_t);
1573 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1575 m.numtriangles = mesh->numtriangles;
1576 m.numverts = mesh->numverts;
1577 m.index = mesh->index;
1578 m.texcoords[0] = &mesh->vertex->st[0];
1579 if (softwaretransform_complexity)
1581 m.vertex = &svert[0].v[0];
1582 m.vertexstep = sizeof(surfvert_t);
1583 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1585 softwaretransform(v->v, sv->v);
1586 VectorSubtract(sv->v, r_origin, diff);
1587 sv->c[0] = fogcolor[0];
1588 sv->c[1] = fogcolor[1];
1589 sv->c[2] = fogcolor[2];
1590 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1595 m.vertex = &mesh->vertex->v[0];
1596 m.vertexstep = sizeof(surfvertex_t);
1597 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1599 VectorSubtract(v->v, r_origin, diff);
1600 sv->c[0] = fogcolor[0];
1601 sv->c[1] = fogcolor[1];
1602 sv->c[2] = fogcolor[2];
1603 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1610 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1613 for (surf = firstsurf;surf;surf = surf->chain)
1616 RSurfShader_Wall_Pass_BaseFullbright(surf);
1618 for (surf = firstsurf;surf;surf = surf->chain)
1619 if (surf->currenttexture->glowtexture)
1620 RSurfShader_Wall_Pass_Glow(surf);
1622 for (surf = firstsurf;surf;surf = surf->chain)
1623 RSurfShader_Wall_Pass_Fog(surf);
1626 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1629 for (surf = firstsurf;surf;surf = surf->chain)
1632 RSurfShader_Wall_Pass_BaseVertex(surf);
1634 for (surf = firstsurf;surf;surf = surf->chain)
1635 if (surf->currenttexture->glowtexture)
1636 RSurfShader_Wall_Pass_Glow(surf);
1638 for (surf = firstsurf;surf;surf = surf->chain)
1639 RSurfShader_Wall_Pass_Fog(surf);
1642 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1645 if (r_vertexsurfaces.integer)
1647 for (surf = firstsurf;surf;surf = surf->chain)
1650 RSurfShader_Wall_Pass_BaseVertex(surf);
1652 for (surf = firstsurf;surf;surf = surf->chain)
1653 if (surf->currenttexture->glowtexture)
1654 RSurfShader_Wall_Pass_Glow(surf);
1656 for (surf = firstsurf;surf;surf = surf->chain)
1657 RSurfShader_Wall_Pass_Fog(surf);
1659 else if (r_multitexture.integer)
1661 if (r_dlightmap.integer)
1663 for (surf = firstsurf;surf;surf = surf->chain)
1666 RSurfShader_Wall_Pass_BaseMTex(surf);
1668 for (surf = firstsurf;surf;surf = surf->chain)
1669 if (surf->currenttexture->glowtexture)
1670 RSurfShader_Wall_Pass_Glow(surf);
1672 for (surf = firstsurf;surf;surf = surf->chain)
1673 RSurfShader_Wall_Pass_Fog(surf);
1677 for (surf = firstsurf;surf;surf = surf->chain)
1680 RSurfShader_Wall_Pass_BaseMTex(surf);
1682 for (surf = firstsurf;surf;surf = surf->chain)
1683 if (surf->dlightframe == r_framecount)
1684 RSurfShader_Wall_Pass_Light(surf);
1685 for (surf = firstsurf;surf;surf = surf->chain)
1686 if (surf->currenttexture->glowtexture)
1687 RSurfShader_Wall_Pass_Glow(surf);
1689 for (surf = firstsurf;surf;surf = surf->chain)
1690 RSurfShader_Wall_Pass_Fog(surf);
1693 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1695 for (surf = firstsurf;surf;surf = surf->chain)
1698 RSurfShader_Wall_Pass_BaseVertex(surf);
1700 for (surf = firstsurf;surf;surf = surf->chain)
1701 if (surf->currenttexture->glowtexture)
1702 RSurfShader_Wall_Pass_Glow(surf);
1704 for (surf = firstsurf;surf;surf = surf->chain)
1705 RSurfShader_Wall_Pass_Fog(surf);
1709 if (r_dlightmap.integer)
1711 for (surf = firstsurf;surf;surf = surf->chain)
1714 RSurfShader_Wall_Pass_BaseTexture(surf);
1716 for (surf = firstsurf;surf;surf = surf->chain)
1717 RSurfShader_Wall_Pass_BaseLightmap(surf);
1718 for (surf = firstsurf;surf;surf = surf->chain)
1719 if (surf->currenttexture->glowtexture)
1720 RSurfShader_Wall_Pass_Glow(surf);
1722 for (surf = firstsurf;surf;surf = surf->chain)
1723 RSurfShader_Wall_Pass_Fog(surf);
1727 for (surf = firstsurf;surf;surf = surf->chain)
1730 RSurfShader_Wall_Pass_BaseTexture(surf);
1732 for (surf = firstsurf;surf;surf = surf->chain)
1733 RSurfShader_Wall_Pass_BaseLightmap(surf);
1734 for (surf = firstsurf;surf;surf = surf->chain)
1735 if (surf->dlightframe == r_framecount)
1736 RSurfShader_Wall_Pass_Light(surf);
1737 for (surf = firstsurf;surf;surf = surf->chain)
1738 if (surf->currenttexture->glowtexture)
1739 RSurfShader_Wall_Pass_Glow(surf);
1741 for (surf = firstsurf;surf;surf = surf->chain)
1742 RSurfShader_Wall_Pass_Fog(surf);
1748 =============================================================
1752 =============================================================
1755 static void RSurf_Callback(void *data, void *junk)
1757 ((msurface_t *)data)->visframe = r_framecount;
1760 static void R_SolidWorldNode (void)
1762 if (r_viewleaf->contents != CONTENTS_SOLID)
1765 mportal_t *p, *pstack[8192];
1766 msurface_t *surf, **mark, **endmark;
1769 // LordHavoc: portal-passage worldnode; follows portals leading
1770 // outward from viewleaf, if a portal leads offscreen it is not
1771 // followed, in indoor maps this can often cull a great deal of
1772 // geometry away when pvs data is not present (useful with pvs as well)
1775 leaf->worldnodeframe = r_framecount;
1780 leaf->visframe = r_framecount;
1782 if (leaf->nummarksurfaces)
1784 mark = leaf->firstmarksurface;
1785 endmark = mark + leaf->nummarksurfaces;
1791 // make sure surfaces are only processed once
1792 if (surf->worldnodeframe == r_framecount)
1794 surf->worldnodeframe = r_framecount;
1795 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1797 if (surf->flags & SURF_PLANEBACK)
1799 VectorNegate(surf->plane->normal, plane.normal);
1800 plane.dist = -surf->plane->dist;
1801 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1806 if (!(surf->flags & SURF_PLANEBACK))
1807 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);
1810 while (mark < endmark);
1817 // make sure surfaces are only processed once
1818 if (surf->worldnodeframe == r_framecount)
1820 surf->worldnodeframe = r_framecount;
1821 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1823 if (surf->flags & SURF_PLANEBACK)
1824 surf->visframe = r_framecount;
1828 if (!(surf->flags & SURF_PLANEBACK))
1829 surf->visframe = r_framecount;
1832 while (mark < endmark);
1836 // follow portals into other leafs
1838 for (;p;p = p->next)
1840 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1843 if (leaf->worldnodeframe != r_framecount)
1845 leaf->worldnodeframe = r_framecount;
1846 if (leaf->contents != CONTENTS_SOLID)
1848 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1850 p->visframe = r_framecount;
1851 pstack[portalstack++] = p;
1855 p = pstack[--portalstack];
1867 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1868 int nodestackpos = 0;
1869 // LordHavoc: recursive descending worldnode; if portals are not
1870 // available, this is a good last resort, can cull large amounts of
1871 // geometry, but is more time consuming than portal-passage and renders
1872 // things behind walls
1875 if (R_NotCulledBox(node->mins, node->maxs))
1877 if (node->numsurfaces)
1881 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1883 if (PlaneDiff (r_origin, node->plane) < 0)
1885 for (;surf < surfend;surf++)
1887 if (surf->flags & SURF_PLANEBACK)
1889 VectorNegate(surf->plane->normal, plane.normal);
1890 plane.dist = -surf->plane->dist;
1891 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1897 for (;surf < surfend;surf++)
1899 if (!(surf->flags & SURF_PLANEBACK))
1900 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);
1906 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1907 if (PlaneDiff (r_origin, node->plane) < 0)
1909 for (;surf < surfend;surf++)
1911 if (surf->flags & SURF_PLANEBACK)
1912 surf->visframe = r_framecount;
1917 for (;surf < surfend;surf++)
1919 if (!(surf->flags & SURF_PLANEBACK))
1920 surf->visframe = r_framecount;
1926 // recurse down the children
1927 if (node->children[0]->contents >= 0)
1929 if (node->children[1]->contents >= 0)
1931 if (nodestackpos < 8192)
1932 nodestack[nodestackpos++] = node->children[1];
1933 node = node->children[0];
1937 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1938 node = node->children[0];
1943 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1944 if (node->children[1]->contents >= 0)
1946 node = node->children[1];
1949 else if (nodestackpos > 0)
1951 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1952 node = nodestack[--nodestackpos];
1957 else if (nodestackpos > 0)
1959 node = nodestack[--nodestackpos];
1965 static int r_portalframecount = 0;
1967 static void R_PVSWorldNode()
1970 mportal_t *p, *pstack[8192];
1971 msurface_t *surf, **mark, **endmark;
1976 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1979 leaf->worldnodeframe = r_framecount;
1984 leaf->visframe = r_framecount;
1986 if (leaf->nummarksurfaces)
1988 mark = leaf->firstmarksurface;
1989 endmark = mark + leaf->nummarksurfaces;
1995 // make sure surfaces are only processed once
1996 if (surf->worldnodeframe == r_framecount)
1998 surf->worldnodeframe = r_framecount;
1999 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2001 if (surf->flags & SURF_PLANEBACK)
2003 VectorNegate(surf->plane->normal, plane.normal);
2004 plane.dist = -surf->plane->dist;
2005 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
2010 if (!(surf->flags & SURF_PLANEBACK))
2011 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);
2014 while (mark < endmark);
2021 // make sure surfaces are only processed once
2022 if (surf->worldnodeframe == r_framecount)
2024 surf->worldnodeframe = r_framecount;
2025 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2027 if (surf->flags & SURF_PLANEBACK)
2028 surf->visframe = r_framecount;
2032 if (!(surf->flags & SURF_PLANEBACK))
2033 surf->visframe = r_framecount;
2036 while (mark < endmark);
2040 // follow portals into other leafs
2041 for (p = leaf->portals;p;p = p->next)
2043 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2046 if (leaf->worldnodeframe != r_framecount)
2048 leaf->worldnodeframe = r_framecount;
2049 if (leaf->contents != CONTENTS_SOLID)
2051 i = (leaf - cl.worldmodel->leafs) - 1;
2052 if (worldvis[i>>3] & (1<<(i&7)))
2054 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2056 pstack[portalstack++] = p;
2060 p = pstack[--portalstack];
2072 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
2073 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
2074 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
2075 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
2076 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
2078 int Cshader_count = 5;
2079 Cshader_t *Cshaders[5] =
2081 &Cshader_wall_vertex,
2082 &Cshader_wall_lightmap,
2083 &Cshader_wall_fullbright,
2088 void R_PrepareSurfaces(void)
2090 int i, alttextures, texframe, framecount;
2095 for (i = 0;i < Cshader_count;i++)
2096 Cshaders[i]->chain = NULL;
2098 model = currentrenderentity->model;
2099 alttextures = currentrenderentity->frame != 0;
2100 texframe = (int)(cl.time * 5.0f);
2102 for (i = 0;i < model->nummodelsurfaces;i++)
2104 surf = model->modelsortedsurfaces[i];
2105 if (surf->visframe == r_framecount)
2107 if (surf->insertframe != r_framecount)
2109 surf->insertframe = r_framecount;
2111 t = surf->texinfo->texture;
2114 framecount = t->anim_total[alttextures];
2115 if (framecount >= 2)
2116 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
2118 surf->currenttexture = t->anim_frames[alttextures][0];
2121 surf->currenttexture = t;
2124 surf->chain = surf->shader->chain;
2125 surf->shader->chain = surf;
2130 void R_DrawSurfaces (int type)
2135 for (i = 0;i < Cshader_count;i++)
2137 shader = Cshaders[i];
2138 if (shader->chain && shader->shaderfunc[type])
2139 shader->shaderfunc[type](shader->chain);
2143 static float portalpointbuffer[256][3];
2145 void R_DrawPortals(void)
2148 mportal_t *portal, *endportal;
2151 drawportals = r_drawportals.integer;
2153 if (drawportals < 1)
2156 memset(&m, 0, sizeof(m));
2157 m.transparent = true;
2158 m.blendfunc1 = GL_SRC_ALPHA;
2159 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2160 m.vertex = &portalpointbuffer[0][0];
2161 m.vertexstep = sizeof(float[3]);
2163 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2165 if (portal->visframe == r_portalframecount)
2167 if (portal->numpoints <= 256)
2169 i = portal - cl.worldmodel->portals;
2170 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2171 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2172 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2173 point = portal->points;
2174 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2176 for (i = portal->numpoints - 1;i >= 0;i--)
2177 VectorCopy(point[i].position, portalpointbuffer[i]);
2181 for (i = 0;i < portal->numpoints;i++)
2182 VectorCopy(point[i].position, portalpointbuffer[i]);
2184 R_Mesh_DrawPolygon(&m, portal->numpoints);
2190 void R_SetupForBModelRendering(void)
2197 // because bmodels can be reused, we have to decide which things to render
2198 // from scratch every time
2200 model = currentrenderentity->model;
2202 softwaretransformforentity (currentrenderentity);
2203 softwareuntransform(r_origin, modelorg);
2205 for (i = 0;i < model->nummodelsurfaces;i++)
2207 surf = model->modelsortedsurfaces[i];
2208 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2209 surf->visframe = r_framecount;
2211 surf->visframe = -1;
2212 surf->worldnodeframe = -1;
2213 surf->lightframe = -1;
2214 surf->dlightframe = -1;
2215 surf->insertframe = -1;
2219 void R_SetupForWorldRendering(void)
2221 // there is only one instance of the world, but it can be rendered in
2224 currentrenderentity = &cl_entities[0].render;
2225 softwaretransformidentity();
2228 static void R_SurfMarkLights (void)
2233 if (r_dynamic.integer)
2236 if (!r_vertexsurfaces.integer)
2238 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2240 surf = currentrenderentity->model->modelsortedsurfaces[i];
2241 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2243 if (surf->cached_dlight
2244 || surf->cached_ambient != r_ambient.value
2245 || surf->cached_lightscalebit != lightscalebit)
2246 R_BuildLightMap(surf, false); // base lighting changed
2247 else if (r_dynamic.integer)
2249 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2250 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2251 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2252 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2253 R_BuildLightMap(surf, false); // base lighting changed
2254 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2255 R_BuildLightMap(surf, true); // only dlights
2262 void R_MarkWorldLights(void)
2264 R_SetupForWorldRendering();
2273 void R_DrawWorld (void)
2275 R_SetupForWorldRendering();
2277 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2278 R_SolidWorldNode ();
2288 void R_DrawBrushModelSky (void)
2290 R_SetupForBModelRendering();
2292 R_PrepareSurfaces();
2293 R_DrawSurfaces(SHADERSTAGE_SKY);
2296 void R_DrawBrushModelNormal (void)
2300 // have to flush queue because of possible lightmap reuse
2303 R_SetupForBModelRendering();
2307 R_PrepareSurfaces();
2309 if (!skyrendermasked)
2310 R_DrawSurfaces(SHADERSTAGE_SKY);
2311 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2314 static void gl_surf_start(void)
2318 static void gl_surf_shutdown(void)
2322 static void gl_surf_newmap(void)
2326 void GL_Surf_Init(void)
2329 dlightdivtable[0] = 4194304;
2330 for (i = 1;i < 32768;i++)
2331 dlightdivtable[i] = 4194304 / (i << 7);
2333 Cvar_RegisterVariable(&r_ambient);
2334 Cvar_RegisterVariable(&r_vertexsurfaces);
2335 Cvar_RegisterVariable(&r_dlightmap);
2336 Cvar_RegisterVariable(&r_drawportals);
2337 Cvar_RegisterVariable(&r_testvis);
2338 Cvar_RegisterVariable(&r_floatbuildlightmap);
2340 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);