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].subtract * 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) >> 7;
126 bl[1] += (green * k) >> 7;
127 bl[2] += (blue * k) >> 7;
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].subtract * 32768.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 = (32768.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);
834 if (f < rd->cullradius2)
836 f = (1.0f / f) - rd->subtract;
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 int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
851 int i, l, lit = false;
854 for (l = 0;l < r_numdlights;l++)
856 if (dlightbits[l >> 5] & (1 << (l & 31)))
859 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
860 VectorCopy(rd->origin, lightorigin);
861 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
863 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
864 if (f < rd->cullradius2)
866 f = (1.0f / f) - rd->subtract;
867 VectorMA(c, f, rd->light, c);
876 // note: this untransforms lights to do the checking,
877 // and takes surf->mesh->vertex data
878 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
884 for (l = 0;l < r_numdlights;l++)
886 if (dlightbits[l >> 5] & (1 << (l & 31)))
889 softwareuntransform(rd->origin, lightorigin);
890 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
891 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
898 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
903 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
904 float base[3], scale, f;
908 memset(&m, 0, sizeof(m));
909 if (currentrenderentity->effects & EF_ADDITIVE)
911 m.transparent = true;
912 m.blendfunc1 = GL_SRC_ALPHA;
913 m.blendfunc2 = GL_ONE;
915 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
917 m.transparent = true;
918 m.blendfunc1 = GL_SRC_ALPHA;
919 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
923 m.transparent = false;
924 m.blendfunc1 = GL_ONE;
925 m.blendfunc2 = GL_ZERO;
927 m.depthwrite = false;
928 m.depthdisable = false;
929 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
931 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
933 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f) + surf->flags & SURF_LIGHTMAP ? 0 : ;
935 ca = currentrenderentity->alpha;
936 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
938 m.numtriangles = mesh->numtriangles;
939 m.numverts = mesh->numverts;
941 if (R_Mesh_Draw_GetBuffer(&m))
944 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
946 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
948 softwaretransform(v->v, outv);
950 VectorCopy(base, outc);
956 if (surf->dlightframe == r_framecount)
957 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
959 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
961 if (surf->flags & SURF_LIGHTMAP)
962 if (surf->styles[0] != 255)
964 lm = surf->samples + v->lightmapoffset;
965 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
966 VectorMA(outc, scale, lm, outc);
967 if (surf->styles[1] != 255)
970 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
971 VectorMA(outc, scale, lm, outc);
972 if (surf->styles[2] != 255)
975 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
976 VectorMA(outc, scale, lm, outc);
977 if (surf->styles[3] != 255)
980 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
981 VectorMA(outc, scale, lm, outc);
988 VectorSubtract(outv, r_origin, diff);
989 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
990 VectorScale(outc, f, outc);
993 VectorScale(outc, cl, outc);
999 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1001 rmeshbufferinfo_t m;
1002 memset(&m, 0, sizeof(m));
1003 if (currentrenderentity->effects & EF_ADDITIVE)
1005 m.transparent = true;
1006 m.blendfunc1 = GL_SRC_ALPHA;
1007 m.blendfunc2 = GL_ONE;
1009 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1011 m.transparent = true;
1012 m.blendfunc1 = GL_SRC_ALPHA;
1013 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1017 m.transparent = false;
1018 m.blendfunc1 = GL_ONE;
1019 m.blendfunc2 = GL_ZERO;
1021 m.depthwrite = false;
1022 m.depthdisable = false;
1023 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1024 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1025 ca = currentrenderentity->alpha;
1026 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1028 m.numtriangles = mesh->numtriangles;
1029 m.numverts = mesh->numverts;
1031 if (R_Mesh_Draw_GetBuffer(&m))
1033 cl = (float) (1 << lightscalebit) * m.colorscale;
1034 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1035 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1037 softwaretransform(v->v, outv);
1038 if (r_waterripple.value)
1039 outv[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];
1043 VectorSubtract(outv, r_origin, diff);
1044 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1047 outc[0] = outc[1] = outc[2] = cl;
1049 outst[0] = v->st[0];
1050 outst[1] = v->st[1];
1051 outuv[0] = v->uv[0];
1052 outuv[1] = v->uv[1];
1058 float diff[3], alpha, ifog;
1063 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1065 memset(&m, 0, sizeof(m));
1066 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
1068 m.transparent = true;
1069 m.blendfunc1 = GL_SRC_ALPHA;
1070 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1074 m.transparent = false;
1075 m.blendfunc1 = GL_ONE;
1076 m.blendfunc2 = GL_ZERO;
1078 m.vertex = &svert[0].v[0];
1079 m.vertexstep = sizeof(surfvert_t);
1080 m.color = &svert[0].c[0];
1081 m.colorstep = sizeof(surfvert_t);
1082 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1083 m.texcoords[0] = &svert[0].st[0];
1084 m.texcoordstep[0] = sizeof(surfvert_t);
1085 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1087 m.numtriangles = mesh->numtriangles;
1088 m.numverts = mesh->numverts;
1089 m.index = mesh->index;
1090 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1092 softwaretransform(v->v, sv->v);
1093 if (r_waterripple.value)
1094 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];
1095 if (surf->flags & SURF_DRAWFULLBRIGHT)
1109 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1110 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1112 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
1113 RSurf_Light(surf->dlightbits, m.numverts);
1114 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
1116 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1118 VectorSubtract(sv->v, r_origin, diff);
1119 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1130 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
1141 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1143 memset(&m, 0, sizeof(m));
1144 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
1145 m.blendfunc1 = GL_SRC_ALPHA;
1146 m.blendfunc2 = GL_ONE;
1147 m.vertex = &svert[0].v[0];
1148 m.vertexstep = sizeof(surfvert_t);
1149 m.color = &svert[0].c[0];
1150 m.colorstep = sizeof(surfvert_t);
1151 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1152 m.texcoords[0] = &svert[0].st[0];
1153 m.texcoordstep[0] = sizeof(surfvert_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 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1162 softwaretransform(v->v, sv->v);
1163 if (r_waterripple.value)
1164 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];
1167 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1168 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1170 VectorSubtract(sv->v, r_origin, diff);
1171 sv->c[0] = fogcolor[0];
1172 sv->c[1] = fogcolor[1];
1173 sv->c[2] = fogcolor[2];
1174 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
1181 static void RSurfShader_Water(msurface_t *firstsurf)
1184 for (surf = firstsurf;surf;surf = surf->chain)
1185 RSurfShader_Water_Pass_Base(surf);
1187 for (surf = firstsurf;surf;surf = surf->chain)
1188 RSurfShader_Water_Pass_Fog(surf);
1191 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
1196 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1198 rmeshbufferinfo_t m;
1199 memset(&m, 0, sizeof(m));
1200 if (currentrenderentity->effects & EF_ADDITIVE)
1202 m.transparent = true;
1203 m.blendfunc1 = GL_SRC_ALPHA;
1204 m.blendfunc2 = GL_ONE;
1206 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1208 m.transparent = true;
1209 m.blendfunc1 = GL_SRC_ALPHA;
1210 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1214 m.transparent = false;
1215 m.blendfunc1 = GL_ONE;
1216 m.blendfunc2 = GL_ZERO;
1218 m.depthwrite = false;
1219 m.depthdisable = false;
1220 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1221 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1222 ca = currentrenderentity->alpha;
1223 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1225 m.numtriangles = mesh->numtriangles;
1226 m.numverts = mesh->numverts;
1228 if (R_Mesh_Draw_GetBuffer(&m))
1230 cl = (float) (1 << lightscalebit) * m.colorscale;
1231 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1234 if (softwaretransform_complexity)
1236 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1238 softwaretransform(v->v, outv);
1240 VectorSubtract(outv, r_origin, diff);
1241 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1243 outst[0] = v->st[0];
1244 outst[1] = v->st[1];
1245 outuv[0] = v->uv[0];
1246 outuv[1] = v->uv[1];
1251 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1253 VectorCopy(v->v, outv);
1255 VectorSubtract(outv, r_origin, diff);
1256 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1258 outst[0] = v->st[0];
1259 outst[1] = v->st[1];
1260 outuv[0] = v->uv[0];
1261 outuv[1] = v->uv[1];
1267 if (softwaretransform_complexity)
1269 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1271 softwaretransform(v->v, outv);
1273 outc[0] = outc[1] = outc[2] = cl;
1275 outst[0] = v->st[0];
1276 outst[1] = v->st[1];
1277 outuv[0] = v->uv[0];
1278 outuv[1] = v->uv[1];
1283 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1285 VectorCopy(v->v, outv);
1287 outc[0] = outc[1] = outc[2] = cl;
1289 outst[0] = v->st[0];
1290 outst[1] = v->st[1];
1291 outuv[0] = v->uv[0];
1292 outuv[1] = v->uv[1];
1300 float diff[3], ifog;
1306 memset(&m, 0, sizeof(m));
1307 if (currentrenderentity->effects & EF_ADDITIVE)
1309 m.transparent = true;
1310 m.blendfunc1 = GL_SRC_ALPHA;
1311 m.blendfunc2 = GL_ONE;
1313 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1315 m.transparent = true;
1316 m.blendfunc1 = GL_SRC_ALPHA;
1317 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1321 m.transparent = false;
1322 m.blendfunc1 = GL_ONE;
1323 m.blendfunc2 = GL_ZERO;
1325 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
1326 m.ca = currentrenderentity->alpha;
1327 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1328 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1329 m.texcoordstep[0] = sizeof(surfvertex_t);
1330 m.texcoordstep[1] = sizeof(surfvertex_t);
1331 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1333 m.numtriangles = mesh->numtriangles;
1334 m.numverts = mesh->numverts;
1335 m.index = mesh->index;
1336 m.texcoords[0] = &mesh->vertex->st[0];
1337 m.texcoords[1] = &mesh->vertex->uv[0];
1340 m.color = &svert[0].c[0];
1341 m.colorstep = sizeof(surfvert_t);
1342 if (softwaretransform_complexity)
1344 m.vertex = &svert[0].v[0];
1345 m.vertexstep = sizeof(surfvert_t);
1346 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1348 softwaretransform(v->v, sv->v);
1349 VectorSubtract(sv->v, r_origin, diff);
1350 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1351 sv->c[0] = m.cr * ifog;
1352 sv->c[1] = m.cg * ifog;
1353 sv->c[2] = m.cb * ifog;
1359 m.vertex = &mesh->vertex->v[0];
1360 m.vertexstep = sizeof(surfvertex_t);
1361 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1363 VectorSubtract(v->v, r_origin, diff);
1364 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1365 sv->c[0] = m.cr * ifog;
1366 sv->c[1] = m.cg * ifog;
1367 sv->c[2] = m.cb * ifog;
1374 if (softwaretransform_complexity)
1376 m.vertex = &svert[0].v[0];
1377 m.vertexstep = sizeof(surfvert_t);
1378 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1379 softwaretransform(v->v, sv->v);
1383 m.vertex = &mesh->vertex->v[0];
1384 m.vertexstep = sizeof(surfvertex_t);
1392 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1397 float *outv, *outc, *outst, cl, ca;
1399 rmeshbufferinfo_t m;
1400 memset(&m, 0, sizeof(m));
1401 m.transparent = false;
1402 m.blendfunc1 = GL_ONE;
1403 m.blendfunc2 = GL_ZERO;
1404 m.depthwrite = false;
1405 m.depthdisable = false;
1406 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1407 ca = currentrenderentity->alpha;
1408 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1410 m.numtriangles = mesh->numtriangles;
1411 m.numverts = mesh->numverts;
1413 if (R_Mesh_Draw_GetBuffer(&m))
1415 cl = (float) (1 << lightscalebit) * m.colorscale;
1416 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1417 if (softwaretransform_complexity)
1419 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1421 softwaretransform(v->v, outv);
1423 outc[0] = outc[1] = outc[2] = cl;
1425 outst[0] = v->st[0];
1426 outst[1] = v->st[1];
1431 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1433 VectorCopy(v->v, outv);
1435 outc[0] = outc[1] = outc[2] = cl;
1437 outst[0] = v->st[0];
1438 outst[1] = v->st[1];
1450 memset(&m, 0, sizeof(m));
1451 m.transparent = false;
1452 m.blendfunc1 = GL_ONE;
1453 m.blendfunc2 = GL_ZERO;
1454 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1456 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1457 m.texcoordstep[0] = sizeof(surfvertex_t);
1458 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1460 m.numtriangles = mesh->numtriangles;
1461 m.numverts = mesh->numverts;
1462 m.index = mesh->index;
1463 m.texcoords[0] = &mesh->vertex->st[0];
1464 if (softwaretransform_complexity)
1466 m.vertex = &svert[0].v[0];
1467 m.vertexstep = sizeof(surfvert_t);
1468 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1469 softwaretransform(v->v, sv->v);
1473 m.vertex = &mesh->vertex->v[0];
1474 m.vertexstep = sizeof(surfvertex_t);
1481 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1486 float *outv, *outc, *outuv, cl, ca, diff[3];
1488 rmeshbufferinfo_t m;
1489 memset(&m, 0, sizeof(m));
1490 m.transparent = false;
1491 m.blendfunc1 = GL_ZERO;
1492 m.blendfunc2 = GL_SRC_COLOR;
1493 m.depthwrite = false;
1494 m.depthdisable = false;
1495 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1496 ca = currentrenderentity->alpha;
1497 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1499 m.numtriangles = mesh->numtriangles;
1500 m.numverts = mesh->numverts;
1502 if (R_Mesh_Draw_GetBuffer(&m))
1504 cl = (float) (1 << lightscalebit) * m.colorscale;
1505 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1508 if (softwaretransform_complexity)
1510 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1512 softwaretransform(v->v, outv);
1514 VectorSubtract(outv, r_origin, diff);
1515 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1517 outuv[0] = v->uv[0];
1518 outuv[1] = v->uv[1];
1523 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1525 VectorCopy(v->v, outv);
1527 VectorSubtract(outv, r_origin, diff);
1528 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1530 outuv[0] = v->uv[0];
1531 outuv[1] = v->uv[1];
1537 if (softwaretransform_complexity)
1539 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1541 softwaretransform(v->v, outv);
1543 outc[0] = outc[1] = outc[2] = cl;
1545 outuv[0] = v->uv[0];
1546 outuv[1] = v->uv[1];
1551 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outuv = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outuv += 2)
1553 VectorCopy(v->v, outv);
1555 outc[0] = outc[1] = outc[2] = cl;
1557 outuv[0] = v->uv[0];
1558 outuv[1] = v->uv[1];
1566 float diff[3], ifog;
1572 memset(&m, 0, sizeof(m));
1573 m.transparent = false;
1574 m.blendfunc1 = GL_ZERO;
1575 m.blendfunc2 = GL_SRC_COLOR;
1576 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1578 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1579 m.texcoordstep[0] = sizeof(surfvertex_t);
1580 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1582 m.numtriangles = mesh->numtriangles;
1583 m.numverts = mesh->numverts;
1584 m.index = mesh->index;
1585 m.texcoords[0] = &mesh->vertex->uv[0];
1588 m.color = &svert[0].c[0];
1589 m.colorstep = sizeof(surfvert_t);
1590 if (softwaretransform_complexity)
1592 m.vertex = &svert[0].v[0];
1593 m.vertexstep = sizeof(surfvert_t);
1594 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1596 softwaretransform(v->v, sv->v);
1597 VectorSubtract(sv->v, r_origin, diff);
1598 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1599 sv->c[0] = m.cr * ifog;
1600 sv->c[1] = m.cg * ifog;
1601 sv->c[2] = m.cb * ifog;
1607 m.vertex = &mesh->vertex->v[0];
1608 m.vertexstep = sizeof(surfvertex_t);
1609 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1611 VectorSubtract(v->v, r_origin, diff);
1612 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1613 sv->c[0] = m.cr * ifog;
1614 sv->c[1] = m.cg * ifog;
1615 sv->c[2] = m.cb * ifog;
1622 if (softwaretransform_complexity)
1624 m.vertex = &svert[0].v[0];
1625 m.vertexstep = sizeof(surfvert_t);
1626 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1627 softwaretransform(v->v, sv->v);
1631 m.vertex = &mesh->vertex->v[0];
1632 m.vertexstep = sizeof(surfvertex_t);
1640 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1645 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1646 float base[3], scale, f;
1649 rmeshbufferinfo_t m;
1650 memset(&m, 0, sizeof(m));
1651 if (currentrenderentity->effects & EF_ADDITIVE)
1653 m.transparent = true;
1654 m.blendfunc1 = GL_SRC_ALPHA;
1655 m.blendfunc2 = GL_ONE;
1657 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1659 m.transparent = true;
1660 m.blendfunc1 = GL_SRC_ALPHA;
1661 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1665 m.transparent = false;
1666 m.blendfunc1 = GL_ONE;
1667 m.blendfunc2 = GL_ZERO;
1669 m.depthwrite = false;
1670 m.depthdisable = false;
1671 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1673 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1675 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1677 ca = currentrenderentity->alpha;
1678 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1680 m.numtriangles = mesh->numtriangles;
1681 m.numverts = mesh->numverts;
1683 if (R_Mesh_Draw_GetBuffer(&m))
1686 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1688 if (currentrenderentity->effects & EF_FULLBRIGHT)
1690 if (softwaretransform_complexity)
1692 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1694 softwaretransform(v->v, outv);
1696 VectorSubtract(outv, r_origin, diff);
1697 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1699 outst[0] = v->st[0];
1700 outst[1] = v->st[1];
1705 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1707 VectorCopy(v->v, outv);
1709 VectorSubtract(outv, r_origin, diff);
1710 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1712 outst[0] = v->st[0];
1713 outst[1] = v->st[1];
1719 if (softwaretransform_complexity)
1721 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1723 softwaretransform(v->v, outv);
1725 VectorCopy(base, outc);
1727 outst[0] = v->st[0];
1728 outst[1] = v->st[1];
1733 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
1735 VectorCopy(v->v, outv);
1737 VectorCopy(base, outc);
1739 outst[0] = v->st[0];
1740 outst[1] = v->st[1];
1744 if (surf->dlightframe == r_framecount)
1745 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1747 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1749 if (surf->styles[0] != 255)
1751 lm = surf->samples + v->lightmapoffset;
1752 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1753 VectorMA(outc, scale, lm, outc);
1754 if (surf->styles[1] != 255)
1757 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1758 VectorMA(outc, scale, lm, outc);
1759 if (surf->styles[2] != 255)
1762 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1763 VectorMA(outc, scale, lm, outc);
1764 if (surf->styles[3] != 255)
1767 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1768 VectorMA(outc, scale, lm, outc);
1775 VectorSubtract(outv, r_origin, diff);
1776 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1777 VectorScale(outc, f, outc);
1780 VectorScale(outc, cl, outc);
1787 float c[3], base[3], scale, diff[3], ifog;
1794 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1796 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 128.0f);
1798 memset(&m, 0, sizeof(m));
1799 if (currentrenderentity->effects & EF_ADDITIVE)
1801 m.transparent = true;
1802 m.blendfunc1 = GL_SRC_ALPHA;
1803 m.blendfunc2 = GL_ONE;
1805 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1807 m.transparent = true;
1808 m.blendfunc1 = GL_SRC_ALPHA;
1809 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1813 m.transparent = false;
1814 m.blendfunc1 = GL_ONE;
1815 m.blendfunc2 = GL_ZERO;
1817 m.vertex = &svert[0].v[0];
1818 m.vertexstep = sizeof(surfvert_t);
1819 m.color = &svert[0].c[0];
1820 m.colorstep = sizeof(surfvert_t);
1821 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1822 m.texcoordstep[0] = sizeof(surfvertex_t);
1823 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1825 m.numtriangles = mesh->numtriangles;
1826 m.numverts = mesh->numverts;
1827 m.index = mesh->index;
1828 m.texcoords[0] = &mesh->vertex->st[0];
1829 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1831 softwaretransform(v->v, sv->v);
1832 VectorCopy(base, c);
1833 if (surf->styles[0] != 255)
1835 lm = surf->samples + v->lightmapoffset;
1836 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1837 VectorMA(c, scale, lm, c);
1838 if (surf->styles[1] != 255)
1841 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1842 VectorMA(c, scale, lm, c);
1843 if (surf->styles[2] != 255)
1846 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1847 VectorMA(c, scale, lm, c);
1848 if (surf->styles[3] != 255)
1851 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1852 VectorMA(c, scale, lm, c);
1860 sv->c[3] = currentrenderentity->alpha;
1862 if (surf->dlightframe == r_framecount)
1863 RSurf_Light(surf->dlightbits, m.numverts);
1866 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1868 VectorSubtract(sv->v, r_origin, diff);
1869 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1880 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1885 float *outv, *outc, *outst, cl, ca, diff[3];
1887 rmeshbufferinfo_t m;
1888 memset(&m, 0, sizeof(m));
1889 if (currentrenderentity->effects & EF_ADDITIVE)
1891 m.transparent = true;
1892 m.blendfunc1 = GL_SRC_ALPHA;
1893 m.blendfunc2 = GL_ONE;
1895 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1897 m.transparent = true;
1898 m.blendfunc1 = GL_SRC_ALPHA;
1899 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1903 m.transparent = false;
1904 m.blendfunc1 = GL_ONE;
1905 m.blendfunc2 = GL_ZERO;
1907 m.depthwrite = false;
1908 m.depthdisable = false;
1909 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1910 ca = currentrenderentity->alpha;
1911 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1913 m.numtriangles = mesh->numtriangles;
1914 m.numverts = mesh->numverts;
1916 if (R_Mesh_Draw_GetBuffer(&m))
1919 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1922 if (softwaretransform_complexity)
1924 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1926 softwaretransform(v->v, outv);
1928 VectorSubtract(outv, r_origin, diff);
1929 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1931 outst[0] = v->st[0];
1932 outst[1] = v->st[1];
1937 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1939 VectorCopy(v->v, outv);
1941 VectorSubtract(outv, r_origin, diff);
1942 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1944 outst[0] = v->st[0];
1945 outst[1] = v->st[1];
1951 if (softwaretransform_complexity)
1953 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1955 softwaretransform(v->v, outv);
1957 outc[0] = outc[1] = outc[2] = cl;
1959 outst[0] = v->st[0];
1960 outst[1] = v->st[1];
1965 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
1967 VectorCopy(v->v, outv);
1969 outc[0] = outc[1] = outc[2] = cl;
1971 outst[0] = v->st[0];
1972 outst[1] = v->st[1];
1980 float diff[3], ifog;
1986 memset(&m, 0, sizeof(m));
1987 if (currentrenderentity->effects & EF_ADDITIVE)
1989 m.transparent = true;
1990 m.blendfunc1 = GL_SRC_ALPHA;
1991 m.blendfunc2 = GL_ONE;
1993 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1995 m.transparent = true;
1996 m.blendfunc1 = GL_SRC_ALPHA;
1997 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2001 m.transparent = false;
2002 m.blendfunc1 = GL_ONE;
2003 m.blendfunc2 = GL_ZERO;
2005 m.vertex = &svert[0].v[0];
2006 m.vertexstep = sizeof(surfvert_t);
2007 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2008 m.texcoordstep[0] = sizeof(surfvertex_t);
2009 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2011 m.numtriangles = mesh->numtriangles;
2012 m.numverts = mesh->numverts;
2013 m.index = mesh->index;
2014 m.texcoords[0] = &mesh->vertex->st[0];
2017 m.color = &svert[0].c[0];
2018 m.colorstep = sizeof(surfvert_t);
2019 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2021 softwaretransform(v->v, sv->v);
2022 VectorSubtract(sv->v, r_origin, diff);
2023 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2027 sv->c[3] = currentrenderentity->alpha;
2032 m.cr = m.cg = m.cb = 1;
2033 m.ca = currentrenderentity->alpha;
2034 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2035 softwaretransform(v->v, sv->v);
2042 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
2047 float *outv, *outc, *outst, *outuv, cl, ca, diff[3], f;
2049 rmeshbufferinfo_t m;
2051 if (surf->dlightframe != r_framecount)
2053 if (currentrenderentity->effects & EF_FULLBRIGHT)
2056 memset(&m, 0, sizeof(m));
2057 if (currentrenderentity->effects & EF_ADDITIVE)
2059 m.transparent = true;
2060 m.blendfunc1 = GL_SRC_ALPHA;
2061 m.blendfunc2 = GL_ONE;
2063 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2065 m.transparent = true;
2066 m.blendfunc1 = GL_SRC_ALPHA;
2067 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2071 m.transparent = false;
2072 m.blendfunc1 = GL_ONE;
2073 m.blendfunc2 = GL_ZERO;
2075 m.depthwrite = false;
2076 m.depthdisable = false;
2077 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2078 ca = currentrenderentity->alpha;
2079 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2081 if (RSurf_LightCheck(surf->dlightbits, mesh))
2083 m.numtriangles = mesh->numtriangles;
2084 m.numverts = mesh->numverts;
2086 if (R_Mesh_Draw_GetBuffer(&m))
2089 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2090 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2)
2092 softwaretransform(v->v, outv);
2096 outst[0] = v->st[0];
2097 outst[1] = v->st[1];
2099 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
2102 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
2104 VectorSubtract(outv, r_origin, diff);
2105 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2106 VectorScale(outc, f, outc);
2110 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
2111 VectorScale(outc, cl, outc);
2117 float diff[3], ifog;
2123 memset(&m, 0, sizeof(m));
2124 if (currentrenderentity->effects & EF_ADDITIVE)
2125 m.transparent = true;
2126 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2127 m.transparent = true;
2129 m.transparent = false;
2130 m.blendfunc1 = GL_SRC_ALPHA;
2131 m.blendfunc2 = GL_ONE;
2132 m.vertex = &svert[0].v[0];
2133 m.vertexstep = sizeof(surfvert_t);
2134 m.color = &svert[0].c[0];
2135 m.colorstep = sizeof(surfvert_t);
2136 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2137 m.texcoordstep[0] = sizeof(surfvertex_t);
2138 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2140 m.numtriangles = mesh->numtriangles;
2141 m.numverts = mesh->numverts;
2142 m.index = mesh->index;
2143 m.texcoords[0] = &mesh->vertex->st[0];
2144 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2146 softwaretransform(v->v, sv->v);
2150 sv->c[3] = currentrenderentity->alpha;
2152 if (RSurf_Light(surf->dlightbits, m.numverts))
2156 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
2158 VectorSubtract(sv->v, r_origin, diff);
2159 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2171 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
2176 float *outv, *outc, *outst, cl, ca, diff[3];
2178 rmeshbufferinfo_t m;
2179 memset(&m, 0, sizeof(m));
2180 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2181 m.blendfunc1 = GL_SRC_ALPHA;
2182 m.blendfunc2 = GL_ONE;
2183 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2184 ca = currentrenderentity->alpha;
2185 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2187 m.numtriangles = mesh->numtriangles;
2188 m.numverts = mesh->numverts;
2190 if (R_Mesh_Draw_GetBuffer(&m))
2193 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2196 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
2198 softwaretransform(v->v, outv);
2200 VectorSubtract(outv, r_origin, diff);
2201 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2203 outst[0] = v->st[0];
2204 outst[1] = v->st[1];
2209 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
2211 softwaretransform(v->v, outv);
2213 outc[0] = outc[1] = outc[2] = cl;
2215 outst[0] = v->st[0];
2216 outst[1] = v->st[1];
2223 float diff[3], ifog;
2229 memset(&m, 0, sizeof(m));
2230 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2231 m.blendfunc1 = GL_SRC_ALPHA;
2232 m.blendfunc2 = GL_ONE;
2236 m.ca = currentrenderentity->alpha;
2237 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2238 m.texcoordstep[0] = sizeof(surfvertex_t);
2239 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2241 m.numtriangles = mesh->numtriangles;
2242 m.numverts = mesh->numverts;
2243 m.index = mesh->index;
2244 m.texcoords[0] = &mesh->vertex->st[0];
2247 m.color = &svert[0].c[0];
2248 m.colorstep = sizeof(surfvert_t);
2249 if (softwaretransform_complexity)
2251 m.vertex = &svert[0].v[0];
2252 m.vertexstep = sizeof(surfvert_t);
2253 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2255 softwaretransform(v->v, sv->v);
2256 VectorSubtract(sv->v, r_origin, diff);
2257 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2258 sv->c[0] = m.cr * ifog;
2259 sv->c[1] = m.cg * ifog;
2260 sv->c[2] = m.cb * ifog;
2266 m.vertex = &mesh->vertex->v[0];
2267 m.vertexstep = sizeof(surfvertex_t);
2268 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2270 VectorSubtract(v->v, r_origin, diff);
2271 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2272 sv->c[0] = m.cr * ifog;
2273 sv->c[1] = m.cg * ifog;
2274 sv->c[2] = m.cb * ifog;
2281 if (softwaretransform_complexity)
2283 m.vertex = &svert[0].v[0];
2284 m.vertexstep = sizeof(surfvert_t);
2285 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2286 softwaretransform(v->v, sv->v);
2290 m.vertex = &mesh->vertex->v[0];
2291 m.vertexstep = sizeof(surfvertex_t);
2299 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
2304 float *outv, *outc, *outst, cl, ca, diff[3], f;
2306 rmeshbufferinfo_t m;
2307 memset(&m, 0, sizeof(m));
2308 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2309 m.blendfunc1 = GL_SRC_ALPHA;
2310 m.blendfunc2 = GL_ONE;
2311 ca = currentrenderentity->alpha;
2312 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2314 m.numtriangles = mesh->numtriangles;
2315 m.numverts = mesh->numverts;
2317 if (R_Mesh_Draw_GetBuffer(&m))
2320 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2321 if (softwaretransform_complexity)
2323 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
2325 softwaretransform(v->v, outv);
2327 VectorSubtract(outv, r_origin, diff);
2328 f = cl * exp(fogdensity/DotProduct(diff, diff));
2329 VectorScale(fogcolor, f, outc);
2331 outst[0] = v->st[0];
2332 outst[1] = v->st[1];
2337 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2)
2339 VectorCopy(v->v, outv);
2341 VectorSubtract(outv, r_origin, diff);
2342 VectorSubtract(outv, r_origin, diff);
2343 f = cl * exp(fogdensity/DotProduct(diff, diff));
2344 VectorScale(fogcolor, f, outc);
2346 outst[0] = v->st[0];
2347 outst[1] = v->st[1];
2360 memset(&m, 0, sizeof(m));
2361 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2362 m.blendfunc1 = GL_SRC_ALPHA;
2363 m.blendfunc2 = GL_ONE;
2364 m.color = &svert[0].c[0];
2365 m.colorstep = sizeof(surfvert_t);
2366 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
2367 m.texcoordstep[0] = sizeof(surfvertex_t);
2368 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2370 m.numtriangles = mesh->numtriangles;
2371 m.numverts = mesh->numverts;
2372 m.index = mesh->index;
2373 m.texcoords[0] = &mesh->vertex->st[0];
2374 if (softwaretransform_complexity)
2376 m.vertex = &svert[0].v[0];
2377 m.vertexstep = sizeof(surfvert_t);
2378 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2380 softwaretransform(v->v, sv->v);
2381 VectorSubtract(sv->v, r_origin, diff);
2382 sv->c[0] = fogcolor[0];
2383 sv->c[1] = fogcolor[1];
2384 sv->c[2] = fogcolor[2];
2385 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2390 m.vertex = &mesh->vertex->v[0];
2391 m.vertexstep = sizeof(surfvertex_t);
2392 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2394 VectorSubtract(v->v, r_origin, diff);
2395 sv->c[0] = fogcolor[0];
2396 sv->c[1] = fogcolor[1];
2397 sv->c[2] = fogcolor[2];
2398 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2406 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
2409 for (surf = firstsurf;surf;surf = surf->chain)
2412 RSurfShader_Wall_Pass_BaseFullbright(surf);
2414 for (surf = firstsurf;surf;surf = surf->chain)
2415 if (surf->currenttexture->glowtexture)
2416 RSurfShader_Wall_Pass_Glow(surf);
2418 for (surf = firstsurf;surf;surf = surf->chain)
2419 RSurfShader_Wall_Pass_Fog(surf);
2422 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
2425 for (surf = firstsurf;surf;surf = surf->chain)
2428 RSurfShader_Wall_Pass_BaseVertex(surf);
2430 for (surf = firstsurf;surf;surf = surf->chain)
2431 if (surf->currenttexture->glowtexture)
2432 RSurfShader_Wall_Pass_Glow(surf);
2434 for (surf = firstsurf;surf;surf = surf->chain)
2435 RSurfShader_Wall_Pass_Fog(surf);
2438 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
2441 if (r_vertexsurfaces.integer)
2443 for (surf = firstsurf;surf;surf = surf->chain)
2446 RSurfShader_Wall_Pass_BaseVertex(surf);
2448 for (surf = firstsurf;surf;surf = surf->chain)
2449 if (surf->currenttexture->glowtexture)
2450 RSurfShader_Wall_Pass_Glow(surf);
2452 for (surf = firstsurf;surf;surf = surf->chain)
2453 RSurfShader_Wall_Pass_Fog(surf);
2455 else if (r_multitexture.integer)
2457 if (r_dlightmap.integer)
2459 for (surf = firstsurf;surf;surf = surf->chain)
2462 RSurfShader_Wall_Pass_BaseMTex(surf);
2464 for (surf = firstsurf;surf;surf = surf->chain)
2465 if (surf->currenttexture->glowtexture)
2466 RSurfShader_Wall_Pass_Glow(surf);
2468 for (surf = firstsurf;surf;surf = surf->chain)
2469 RSurfShader_Wall_Pass_Fog(surf);
2473 for (surf = firstsurf;surf;surf = surf->chain)
2476 RSurfShader_Wall_Pass_BaseMTex(surf);
2478 for (surf = firstsurf;surf;surf = surf->chain)
2479 if (surf->dlightframe == r_framecount)
2480 RSurfShader_Wall_Pass_Light(surf);
2481 for (surf = firstsurf;surf;surf = surf->chain)
2482 if (surf->currenttexture->glowtexture)
2483 RSurfShader_Wall_Pass_Glow(surf);
2485 for (surf = firstsurf;surf;surf = surf->chain)
2486 RSurfShader_Wall_Pass_Fog(surf);
2489 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
2491 for (surf = firstsurf;surf;surf = surf->chain)
2494 RSurfShader_Wall_Pass_BaseVertex(surf);
2496 for (surf = firstsurf;surf;surf = surf->chain)
2497 if (surf->currenttexture->glowtexture)
2498 RSurfShader_Wall_Pass_Glow(surf);
2500 for (surf = firstsurf;surf;surf = surf->chain)
2501 RSurfShader_Wall_Pass_Fog(surf);
2505 if (r_dlightmap.integer)
2507 for (surf = firstsurf;surf;surf = surf->chain)
2510 RSurfShader_Wall_Pass_BaseTexture(surf);
2512 for (surf = firstsurf;surf;surf = surf->chain)
2513 RSurfShader_Wall_Pass_BaseLightmap(surf);
2514 for (surf = firstsurf;surf;surf = surf->chain)
2515 if (surf->currenttexture->glowtexture)
2516 RSurfShader_Wall_Pass_Glow(surf);
2518 for (surf = firstsurf;surf;surf = surf->chain)
2519 RSurfShader_Wall_Pass_Fog(surf);
2523 for (surf = firstsurf;surf;surf = surf->chain)
2526 RSurfShader_Wall_Pass_BaseTexture(surf);
2528 for (surf = firstsurf;surf;surf = surf->chain)
2529 RSurfShader_Wall_Pass_BaseLightmap(surf);
2530 for (surf = firstsurf;surf;surf = surf->chain)
2531 if (surf->dlightframe == r_framecount)
2532 RSurfShader_Wall_Pass_Light(surf);
2533 for (surf = firstsurf;surf;surf = surf->chain)
2534 if (surf->currenttexture->glowtexture)
2535 RSurfShader_Wall_Pass_Glow(surf);
2537 for (surf = firstsurf;surf;surf = surf->chain)
2538 RSurfShader_Wall_Pass_Fog(surf);
2544 =============================================================
2548 =============================================================
2551 static void R_SolidWorldNode (void)
2553 if (r_viewleaf->contents != CONTENTS_SOLID)
2556 mportal_t *p, *pstack[8192];
2557 msurface_t *surf, **mark, **endmark;
2559 // LordHavoc: portal-passage worldnode; follows portals leading
2560 // outward from viewleaf, if a portal leads offscreen it is not
2561 // followed, in indoor maps this can often cull a great deal of
2562 // geometry away when pvs data is not present (useful with pvs as well)
2565 leaf->worldnodeframe = r_framecount;
2570 leaf->visframe = r_framecount;
2572 if (leaf->nummarksurfaces)
2574 mark = leaf->firstmarksurface;
2575 endmark = mark + leaf->nummarksurfaces;
2579 // make sure surfaces are only processed once
2580 if (surf->worldnodeframe == r_framecount)
2582 surf->worldnodeframe = r_framecount;
2583 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2585 if (surf->flags & SURF_PLANEBACK)
2586 surf->visframe = r_framecount;
2590 if (!(surf->flags & SURF_PLANEBACK))
2591 surf->visframe = r_framecount;
2594 while (mark < endmark);
2597 // follow portals into other leafs
2599 for (;p;p = p->next)
2601 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2604 if (leaf->worldnodeframe != r_framecount)
2606 leaf->worldnodeframe = r_framecount;
2607 if (leaf->contents != CONTENTS_SOLID)
2609 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2611 p->visframe = r_framecount;
2612 pstack[portalstack++] = p;
2616 p = pstack[--portalstack];
2628 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
2629 int nodestackpos = 0;
2630 // LordHavoc: recursive descending worldnode; if portals are not
2631 // available, this is a good last resort, can cull large amounts of
2632 // geometry, but is more time consuming than portal-passage and renders
2633 // things behind walls
2636 if (R_NotCulledBox(node->mins, node->maxs))
2638 if (node->numsurfaces)
2640 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
2641 if (PlaneDiff (r_origin, node->plane) < 0)
2643 for (;surf < surfend;surf++)
2645 if (surf->flags & SURF_PLANEBACK)
2646 surf->visframe = r_framecount;
2651 for (;surf < surfend;surf++)
2653 if (!(surf->flags & SURF_PLANEBACK))
2654 surf->visframe = r_framecount;
2659 // recurse down the children
2660 if (node->children[0]->contents >= 0)
2662 if (node->children[1]->contents >= 0)
2664 if (nodestackpos < 8192)
2665 nodestack[nodestackpos++] = node->children[1];
2666 node = node->children[0];
2670 ((mleaf_t *)node->children[1])->visframe = r_framecount;
2671 node = node->children[0];
2676 ((mleaf_t *)node->children[0])->visframe = r_framecount;
2677 if (node->children[1]->contents >= 0)
2679 node = node->children[1];
2682 else if (nodestackpos > 0)
2684 ((mleaf_t *)node->children[1])->visframe = r_framecount;
2685 node = nodestack[--nodestackpos];
2690 else if (nodestackpos > 0)
2692 node = nodestack[--nodestackpos];
2698 static int r_portalframecount = 0;
2700 static void R_PVSWorldNode()
2703 mportal_t *p, *pstack[8192];
2704 msurface_t *surf, **mark, **endmark;
2708 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
2711 leaf->worldnodeframe = r_framecount;
2716 leaf->visframe = r_framecount;
2718 if (leaf->nummarksurfaces)
2720 mark = leaf->firstmarksurface;
2721 endmark = mark + leaf->nummarksurfaces;
2725 // make sure surfaces are only processed once
2726 if (surf->worldnodeframe == r_framecount)
2728 surf->worldnodeframe = r_framecount;
2729 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2731 if (surf->flags & SURF_PLANEBACK)
2732 surf->visframe = r_framecount;
2736 if (!(surf->flags & SURF_PLANEBACK))
2737 surf->visframe = r_framecount;
2740 while (mark < endmark);
2743 // follow portals into other leafs
2744 for (p = leaf->portals;p;p = p->next)
2746 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2749 if (leaf->worldnodeframe != r_framecount)
2751 leaf->worldnodeframe = r_framecount;
2752 if (leaf->contents != CONTENTS_SOLID)
2754 i = (leaf - cl.worldmodel->leafs) - 1;
2755 if (worldvis[i>>3] & (1<<(i&7)))
2757 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2759 pstack[portalstack++] = p;
2763 p = pstack[--portalstack];
2775 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
2776 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
2777 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
2778 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
2779 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
2781 int Cshader_count = 5;
2782 Cshader_t *Cshaders[5] =
2784 &Cshader_wall_vertex,
2785 &Cshader_wall_lightmap,
2786 &Cshader_wall_fullbright,
2791 void R_PrepareSurfaces(void)
2793 int i, alttextures, texframe, framecount;
2798 for (i = 0;i < Cshader_count;i++)
2799 Cshaders[i]->chain = NULL;
2801 model = currentrenderentity->model;
2802 alttextures = currentrenderentity->frame != 0;
2803 texframe = (int)(cl.time * 5.0f);
2805 for (i = 0;i < model->nummodelsurfaces;i++)
2807 surf = model->modelsortedsurfaces[i];
2808 if (surf->visframe == r_framecount)
2810 if (surf->insertframe != r_framecount)
2812 surf->insertframe = r_framecount;
2814 t = surf->texinfo->texture;
2817 framecount = t->anim_total[alttextures];
2818 if (framecount >= 2)
2819 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
2821 surf->currenttexture = t->anim_frames[alttextures][0];
2824 surf->currenttexture = t;
2827 surf->chain = surf->shader->chain;
2828 surf->shader->chain = surf;
2833 void R_DrawSurfaces (int type)
2838 for (i = 0;i < Cshader_count;i++)
2840 shader = Cshaders[i];
2841 if (shader->chain && shader->shaderfunc[type])
2842 shader->shaderfunc[type](shader->chain);
2846 static float portalpointbuffer[256][3];
2848 void R_DrawPortals(void)
2851 mportal_t *portal, *endportal;
2854 drawportals = r_drawportals.integer;
2856 if (drawportals < 1)
2859 memset(&m, 0, sizeof(m));
2860 m.transparent = true;
2861 m.blendfunc1 = GL_SRC_ALPHA;
2862 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2863 m.vertex = &portalpointbuffer[0][0];
2864 m.vertexstep = sizeof(float[3]);
2866 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2868 if (portal->visframe == r_portalframecount)
2870 if (portal->numpoints <= 256)
2872 i = portal - cl.worldmodel->portals;
2873 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2874 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2875 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2876 point = portal->points;
2877 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2879 for (i = portal->numpoints - 1;i >= 0;i--)
2880 VectorCopy(point[i].position, portalpointbuffer[i]);
2884 for (i = 0;i < portal->numpoints;i++)
2885 VectorCopy(point[i].position, portalpointbuffer[i]);
2887 R_Mesh_DrawPolygon(&m, portal->numpoints);
2893 void R_SetupForBModelRendering(void)
2900 // because bmodels can be reused, we have to decide which things to render
2901 // from scratch every time
2903 model = currentrenderentity->model;
2905 softwaretransformforentity (currentrenderentity);
2906 softwareuntransform(r_origin, modelorg);
2908 for (i = 0;i < model->nummodelsurfaces;i++)
2910 surf = model->modelsortedsurfaces[i];
2911 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2912 surf->visframe = r_framecount;
2914 surf->visframe = -1;
2915 surf->worldnodeframe = -1;
2916 surf->lightframe = -1;
2917 surf->dlightframe = -1;
2918 surf->insertframe = -1;
2922 void R_SetupForWorldRendering(void)
2924 // there is only one instance of the world, but it can be rendered in
2927 currentrenderentity = &cl_entities[0].render;
2928 softwaretransformidentity();
2931 static void R_SurfMarkLights (void)
2936 if (r_dynamic.integer)
2939 if (!r_vertexsurfaces.integer)
2941 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2943 surf = currentrenderentity->model->modelsortedsurfaces[i];
2944 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2946 if (surf->cached_dlight
2947 || surf->cached_ambient != r_ambient.value
2948 || surf->cached_lightscalebit != lightscalebit)
2949 R_BuildLightMap(surf, false); // base lighting changed
2950 else if (r_dynamic.integer)
2952 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2953 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2954 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2955 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2956 R_BuildLightMap(surf, false); // base lighting changed
2957 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2958 R_BuildLightMap(surf, true); // only dlights
2965 void R_MarkWorldLights(void)
2967 R_SetupForWorldRendering();
2976 void R_DrawWorld (void)
2978 R_SetupForWorldRendering();
2980 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2981 R_SolidWorldNode ();
2991 void R_DrawBrushModelSky (void)
2993 R_SetupForBModelRendering();
2995 R_PrepareSurfaces();
2996 R_DrawSurfaces(SHADERSTAGE_SKY);
2999 void R_DrawBrushModelNormal (void)
3003 // have to flush queue because of possible lightmap reuse
3006 R_SetupForBModelRendering();
3010 R_PrepareSurfaces();
3012 if (!skyrendermasked)
3013 R_DrawSurfaces(SHADERSTAGE_SKY);
3014 R_DrawSurfaces(SHADERSTAGE_NORMAL);
3017 static void gl_surf_start(void)
3021 static void gl_surf_shutdown(void)
3025 static void gl_surf_newmap(void)
3029 void GL_Surf_Init(void)
3032 dlightdivtable[0] = 4194304;
3033 for (i = 1;i < 32768;i++)
3034 dlightdivtable[i] = 4194304 / (i << 7);
3036 Cvar_RegisterVariable(&r_ambient);
3037 Cvar_RegisterVariable(&r_vertexsurfaces);
3038 Cvar_RegisterVariable(&r_dlightmap);
3039 Cvar_RegisterVariable(&r_drawportals);
3040 Cvar_RegisterVariable(&r_testvis);
3041 Cvar_RegisterVariable(&r_floatbuildlightmap);
3043 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);