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
25 #define USEMESHBUFFER 1
27 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
30 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
32 cvar_t r_ambient = {0, "r_ambient", "0"};
33 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
34 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
35 cvar_t r_drawportals = {0, "r_drawportals", "0"};
36 cvar_t r_testvis = {0, "r_testvis", "0"};
37 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
39 static int dlightdivtable[32768];
41 static int R_IntAddDynamicLights (msurface_t *surf)
43 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
45 float dist, impact[3], local[3];
47 // LordHavoc: use 64bit integer... shame it's not very standardized...
48 #if _MSC_VER || __BORLANDC__
56 smax = (surf->extents[0] >> 4) + 1;
57 tmax = (surf->extents[1] >> 4) + 1;
60 for (lnum = 0; lnum < r_numdlights; lnum++)
62 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
63 continue; // not lit by this light
65 softwareuntransform(r_dlight[lnum].origin, local);
66 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
68 // for comparisons to minimum acceptable light
69 // compensate for LIGHTOFFSET
70 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
77 if (surf->plane->type < 3)
79 VectorCopy(local, impact);
80 impact[surf->plane->type] -= dist;
84 impact[0] = local[0] - surf->plane->normal[0] * dist;
85 impact[1] = local[1] - surf->plane->normal[1] * dist;
86 impact[2] = local[2] - surf->plane->normal[2] * dist;
89 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
90 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
92 s = bound(0, impacts, smax * 16) - impacts;
93 t = bound(0, impactt, tmax * 16) - impactt;
94 i = s * s + t * t + dist2;
98 // reduce calculations
99 for (s = 0, i = impacts; s < smax; s++, i -= 16)
100 sdtable[s] = i * i + dist2;
102 maxdist3 = maxdist - dist2;
104 // convert to 8.8 blocklights format
105 red = r_dlight[lnum].light[0];
106 green = r_dlight[lnum].light[1];
107 blue = r_dlight[lnum].light[2];
108 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
112 for (t = 0;t < tmax;t++, i -= 16)
115 // make sure some part of it is visible on this line
118 maxdist2 = maxdist - td;
119 for (s = 0;s < smax;s++)
121 if (sdtable[s] < maxdist2)
123 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
126 bl[0] += (red * k) >> 7;
127 bl[1] += (green * k) >> 7;
128 bl[2] += (blue * k) >> 7;
142 static int R_FloatAddDynamicLights (msurface_t *surf)
144 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
145 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
149 smax = (surf->extents[0] >> 4) + 1;
150 tmax = (surf->extents[1] >> 4) + 1;
153 for (lnum = 0; lnum < r_numdlights; lnum++)
155 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
156 continue; // not lit by this light
158 softwareuntransform(r_dlight[lnum].origin, local);
159 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
161 // for comparisons to minimum acceptable light
162 // compensate for LIGHTOFFSET
163 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
166 dist2 += LIGHTOFFSET;
167 if (dist2 >= maxdist)
170 if (surf->plane->type < 3)
172 VectorCopy(local, impact);
173 impact[surf->plane->type] -= dist;
177 impact[0] = local[0] - surf->plane->normal[0] * dist;
178 impact[1] = local[1] - surf->plane->normal[1] * dist;
179 impact[2] = local[2] - surf->plane->normal[2] * dist;
182 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
183 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
185 td = bound(0, impacts, smax * 16) - impacts;
186 td1 = bound(0, impactt, tmax * 16) - impactt;
187 td = td * td + td1 * td1 + dist2;
191 // reduce calculations
192 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
193 sdtable[s] = td1 * td1 + dist2;
195 maxdist3 = maxdist - dist2;
197 // convert to 8.8 blocklights format
198 red = r_dlight[lnum].light[0];
199 green = r_dlight[lnum].light[1];
200 blue = r_dlight[lnum].light[2];
201 subtract = r_dlight[lnum].subtract * 32768.0f;
202 bl = floatblocklights;
205 for (t = 0;t < tmax;t++, td1 -= 16.0f)
208 // make sure some part of it is visible on this line
211 maxdist2 = maxdist - td;
212 for (s = 0;s < smax;s++)
214 if (sdtable[s] < maxdist2)
216 k = (32768.0f / (sdtable[s] + td)) - subtract;
236 Combine and scale multiple lightmaps into the 8.8 format in blocklights
239 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
241 if (!r_floatbuildlightmap.integer)
243 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
244 unsigned int *bl, scale;
245 qbyte *lightmap, *out, *stain;
247 // update cached lighting info
248 surf->cached_dlight = 0;
249 surf->cached_lightscalebit = lightscalebit;
250 surf->cached_ambient = r_ambient.value;
251 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
252 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
253 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
254 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
256 smax = (surf->extents[0]>>4)+1;
257 tmax = (surf->extents[1]>>4)+1;
260 lightmap = surf->samples;
262 // set to full bright if no light data
264 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
266 for (i = 0;i < size3;i++)
272 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
275 for (i = 0;i < size3;i++)
279 memset(bl, 0, size*3*sizeof(unsigned int));
281 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
283 surf->cached_dlight = R_IntAddDynamicLights(surf);
284 if (surf->cached_dlight)
286 else if (dlightchanged)
287 return; // don't upload if only updating dlights and none mattered
290 // add all the lightmaps
294 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
295 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
296 bl[i] += lightmap[i] * scale;
300 stain = surf->stainsamples;
303 // deal with lightmap brightness scale
304 shift = 7 + lightscalebit + 8;
305 if (currentrenderentity->model->lightmaprgba)
307 stride = (surf->lightmaptexturestride - smax) * 4;
308 for (i = 0;i < tmax;i++, out += stride)
310 for (j = 0;j < smax;j++)
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
321 stride = (surf->lightmaptexturestride - smax) * 3;
322 for (i = 0;i < tmax;i++, out += stride)
324 for (j = 0;j < smax;j++)
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
328 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
333 R_UpdateTexture(surf->lightmaptexture, templight);
337 int smax, tmax, i, j, size, size3, maps, stride, l;
339 qbyte *lightmap, *out, *stain;
341 // update cached lighting info
342 surf->cached_dlight = 0;
343 surf->cached_lightscalebit = lightscalebit;
344 surf->cached_ambient = r_ambient.value;
345 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
346 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
347 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
348 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
350 smax = (surf->extents[0]>>4)+1;
351 tmax = (surf->extents[1]>>4)+1;
354 lightmap = surf->samples;
356 // set to full bright if no light data
357 bl = floatblocklights;
358 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
361 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
366 for (i = 0;i < size3;i++)
370 memset(bl, 0, size*3*sizeof(float));
372 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
374 surf->cached_dlight = R_FloatAddDynamicLights(surf);
375 if (surf->cached_dlight)
377 else if (dlightchanged)
378 return; // don't upload if only updating dlights and none mattered
381 // add all the lightmaps
384 bl = floatblocklights;
385 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
386 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
387 bl[i] += lightmap[i] * scale;
390 stain = surf->stainsamples;
391 bl = floatblocklights;
393 // deal with lightmap brightness scale
394 scale = 1.0f / (1 << (7 + lightscalebit + 8));
395 if (currentrenderentity->model->lightmaprgba)
397 stride = (surf->lightmaptexturestride - smax) * 4;
398 for (i = 0;i < tmax;i++, out += stride)
400 for (j = 0;j < smax;j++)
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
404 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411 stride = (surf->lightmaptexturestride - smax) * 3;
412 for (i = 0;i < tmax;i++, out += stride)
414 for (j = 0;j < smax;j++)
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
423 R_UpdateTexture(surf->lightmaptexture, templight);
427 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
430 msurface_t *surf, *endsurf;
431 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
434 // LordHavoc: use 64bit integer... shame it's not very standardized...
435 #if _MSC_VER || __BORLANDC__
442 // for comparisons to minimum acceptable light
443 // compensate for 4096 offset
444 maxdist = radius * radius + 4096;
446 // clamp radius to avoid exceeding 32768 entry division table
447 if (maxdist > 4194304)
450 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
453 if (node->contents < 0)
455 ndist = PlaneDiff(origin, node->plane);
458 node = node->children[0];
463 node = node->children[1];
467 dist2 = ndist * ndist;
471 maxdist3 = maxdist - dist2;
473 if (node->plane->type < 3)
475 VectorCopy(origin, impact);
476 impact[node->plane->type] -= ndist;
480 impact[0] = origin[0] - node->plane->normal[0] * ndist;
481 impact[1] = origin[1] - node->plane->normal[1] * ndist;
482 impact[2] = origin[2] - node->plane->normal[2] * ndist;
485 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
487 if (surf->stainsamples)
489 smax = (surf->extents[0] >> 4) + 1;
490 tmax = (surf->extents[1] >> 4) + 1;
492 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
493 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
495 s = bound(0, impacts, smax * 16) - impacts;
496 t = bound(0, impactt, tmax * 16) - impactt;
497 i = s * s + t * t + dist2;
501 // reduce calculations
502 for (s = 0, i = impacts; s < smax; s++, i -= 16)
503 sdtable[s] = i * i + dist2;
505 // convert to 8.8 blocklights format
506 bl = surf->stainsamples;
511 for (t = 0;t < tmax;t++, i -= 16)
514 // make sure some part of it is visible on this line
517 maxdist2 = maxdist - td;
518 for (s = 0;s < smax;s++)
520 if (sdtable[s] < maxdist2)
522 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
525 ratio = rand() & 255;
526 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
530 a = bound(0, a, 256);
531 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
532 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
533 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
534 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
535 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
536 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
547 // force lightmap upload
549 surf->cached_dlight = true;
554 if (node->children[0]->contents >= 0)
556 if (node->children[1]->contents >= 0)
558 R_StainNode(node->children[0], model, origin, radius, icolor);
559 node = node->children[1];
564 node = node->children[0];
568 else if (node->children[1]->contents >= 0)
570 node = node->children[1];
575 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
578 entity_render_t *ent;
590 model = cl.worldmodel;
591 softwaretransformidentity();
592 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
594 // look for embedded bmodels
595 for (n = 0;n < cl_num_brushmodel_entities;n++)
597 ent = cl_brushmodel_entities[n];
599 if (model && model->name[0] == '*')
601 Mod_CheckLoaded(model);
602 if (model->type == mod_brush)
604 softwaretransformforentity(ent);
605 softwareuntransform(origin, org);
606 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
614 =============================================================
618 =============================================================
622 static float turbsin[256] =
624 #include "gl_warp_sin.h"
626 #define TURBSCALE (256.0 / (2 * M_PI))
628 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
629 #define MAX_SURFVERTS 3072
638 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
640 static void RSurfShader_Sky(msurface_t *firstsurf)
644 float number, length, dir[3], speedscale;
650 // LordHavoc: HalfLife maps have freaky skypolys...
651 if (currentrenderentity->model->ishlbsp)
658 skyrendernow = false;
661 for (surf = firstsurf;surf;surf = surf->chain)
663 // draw depth-only polys
664 memset(&m, 0, sizeof(m));
665 m.transparent = false;
666 m.blendfunc1 = GL_ZERO;
667 m.blendfunc2 = GL_ONE;
669 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
671 m.numtriangles = mesh->numtriangles;
672 m.numverts = mesh->numverts;
673 m.index = mesh->index;
674 if (softwaretransform_complexity)
676 m.vertex = &svert[0].v[0];
677 m.vertexstep = sizeof(surfvert_t);
678 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
679 softwaretransform(v->v, sv->v);
683 m.vertex = &mesh->vertex[0].v[0];
684 m.vertexstep = sizeof(surfvertex_t);
690 else if (skyrenderglquake)
692 for (surf = firstsurf;surf;surf = surf->chain)
694 memset(&m, 0, sizeof(m));
695 m.transparent = false;
696 m.blendfunc1 = GL_ONE;
697 m.blendfunc2 = GL_ZERO;
698 m.vertex = &svert[0].v[0];
699 m.vertexstep = sizeof(surfvert_t);
704 m.tex[0] = R_GetTexture(solidskytexture);
705 m.texcoords[0] = &svert[0].st[0];
706 m.texcoordstep[0] = sizeof(surfvert_t);
707 speedscale = cl.time * (8.0/128.0);
708 speedscale -= (int)speedscale;
709 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
711 m.numtriangles = mesh->numtriangles;
712 m.numverts = mesh->numverts;
713 m.index = mesh->index;
714 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
716 softwaretransform(v->v, sv->v);
717 VectorSubtract (sv->v, r_origin, dir);
718 // flatten the sphere
721 number = DotProduct(dir, dir);
723 length = 3.0f / sqrt(number);
725 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
726 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
729 sv->st[0] = speedscale + dir[0] * length;
730 sv->st[1] = speedscale + dir[1] * length;
738 for (surf = firstsurf;surf;surf = surf->chain)
741 memset(&m, 0, sizeof(m));
742 m.transparent = false;
743 m.blendfunc1 = GL_ONE;
744 m.blendfunc2 = GL_ZERO;
749 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
751 m.numtriangles = mesh->numtriangles;
752 m.numverts = mesh->numverts;
753 m.index = mesh->index;
754 if (softwaretransform_complexity)
756 m.vertex = &svert[0].v[0];
757 m.vertexstep = sizeof(surfvert_t);
758 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
759 softwaretransform(v->v, sv->v);
763 m.vertex = &mesh->vertex[0].v[0];
764 m.vertexstep = sizeof(surfvertex_t);
770 if (skyrenderglquake)
772 for (surf = firstsurf;surf;surf = surf->chain)
774 memset(&m, 0, sizeof(m));
775 m.transparent = false;
776 m.blendfunc1 = GL_SRC_ALPHA;
777 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
778 m.vertex = &svert[0].v[0];
779 m.vertexstep = sizeof(surfvert_t);
784 m.tex[0] = R_GetTexture(alphaskytexture);
785 m.texcoords[0] = &svert[0].st[0];
786 m.texcoordstep[0] = sizeof(surfvert_t);
787 speedscale = cl.time * (16.0/128.0);
788 speedscale -= (int)speedscale;
789 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
791 m.numtriangles = mesh->numtriangles;
792 m.numverts = mesh->numverts;
793 m.index = mesh->index;
794 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
796 softwaretransform(v->v, sv->v);
797 VectorSubtract (sv->v, r_origin, dir);
798 // flatten the sphere
801 number = DotProduct(dir, dir);
803 length = 3.0f / sqrt(number);
805 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
806 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
809 sv->st[0] = speedscale + dir[0] * length;
810 sv->st[1] = speedscale + dir[1] * length;
818 static int RSurf_Light(int *dlightbits, int numverts)
821 int i, l, lit = false;
825 for (l = 0;l < r_numdlights;l++)
827 if (dlightbits[l >> 5] & (1 << (l & 31)))
830 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
831 VectorCopy(rd->origin, lightorigin);
832 for (i = 0, sv = svert;i < numverts;i++, sv++)
834 f = VectorDistance2(sv->v, lightorigin);
835 if (f < rd->cullradius2)
837 f = (1.0f / f) - rd->subtract;
838 sv->c[0] += rd->light[0] * f;
839 sv->c[1] += rd->light[1] * f;
840 sv->c[2] += rd->light[2] * f;
850 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
853 int i, l, lit = false;
856 for (l = 0;l < r_numdlights;l++)
858 if (dlightbits[l >> 5] & (1 << (l & 31)))
861 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
862 VectorCopy(rd->origin, lightorigin);
863 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
865 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
866 if (f < rd->cullradius2)
868 f = (1.0f / f) - rd->subtract;
869 VectorMA(c, f, rd->light, c);
878 // note: this untransforms lights to do the checking,
879 // and takes surf->mesh->vertex data
880 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
886 for (l = 0;l < r_numdlights;l++)
888 if (dlightbits[l >> 5] & (1 << (l & 31)))
891 softwareuntransform(rd->origin, lightorigin);
892 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
893 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
901 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
906 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
907 float base[3], scale, f;
911 memset(&m, 0, sizeof(m));
912 if (currentrenderentity->effects & EF_ADDITIVE)
914 m.transparent = true;
915 m.blendfunc1 = GL_SRC_ALPHA;
916 m.blendfunc2 = GL_ONE;
918 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
920 m.transparent = true;
921 m.blendfunc1 = GL_SRC_ALPHA;
922 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
926 m.transparent = false;
927 m.blendfunc1 = GL_ONE;
928 m.blendfunc2 = GL_ZERO;
930 m.depthwrite = false;
931 m.depthdisable = false;
932 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
934 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
936 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f) + surf->flags & SURF_LIGHTMAP ? 0 : ;
938 ca = currentrenderentity->alpha;
939 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
941 m.numtriangles = mesh->numtriangles;
942 m.numverts = mesh->numverts;
944 if (R_Mesh_Draw_GetBuffer(&m))
947 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
949 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)
951 softwaretransform(v->v, outv);
953 VectorCopy(base, outc);
959 if (surf->dlightframe == r_framecount)
960 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
962 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
964 if (surf->flags & SURF_LIGHTMAP)
965 if (surf->styles[0] != 255)
967 lm = surf->samples + v->lightmapoffset;
968 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
969 VectorMA(outc, scale, lm, outc);
970 if (surf->styles[1] != 255)
973 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
974 VectorMA(outc, scale, lm, outc);
975 if (surf->styles[2] != 255)
978 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
979 VectorMA(outc, scale, lm, outc);
980 if (surf->styles[3] != 255)
983 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
984 VectorMA(outc, scale, lm, outc);
991 VectorSubtract(outv, r_origin, diff);
992 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
993 VectorScale(outc, f, outc);
996 VectorScale(outc, cl, outc);
1002 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1004 rmeshbufferinfo_t m;
1005 memset(&m, 0, sizeof(m));
1006 if (currentrenderentity->effects & EF_ADDITIVE)
1008 m.transparent = true;
1009 m.blendfunc1 = GL_SRC_ALPHA;
1010 m.blendfunc2 = GL_ONE;
1012 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1014 m.transparent = true;
1015 m.blendfunc1 = GL_SRC_ALPHA;
1016 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1020 m.transparent = false;
1021 m.blendfunc1 = GL_ONE;
1022 m.blendfunc2 = GL_ZERO;
1024 m.depthwrite = false;
1025 m.depthdisable = false;
1026 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1027 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1028 ca = currentrenderentity->alpha;
1029 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1031 m.numtriangles = mesh->numtriangles;
1032 m.numverts = mesh->numverts;
1034 if (R_Mesh_Draw_GetBuffer(&m))
1036 cl = (float) (1 << lightscalebit) * m.colorscale;
1037 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1038 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)
1040 softwaretransform(v->v, outv);
1041 if (r_waterripple.value)
1042 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];
1046 VectorSubtract(outv, r_origin, diff);
1047 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1050 outc[0] = outc[1] = outc[2] = cl;
1052 outst[0] = v->st[0];
1053 outst[1] = v->st[1];
1054 outuv[0] = v->uv[0];
1055 outuv[1] = v->uv[1];
1061 float diff[3], alpha, ifog;
1066 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1068 memset(&m, 0, sizeof(m));
1069 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
1071 m.transparent = true;
1072 m.blendfunc1 = GL_SRC_ALPHA;
1073 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1077 m.transparent = false;
1078 m.blendfunc1 = GL_ONE;
1079 m.blendfunc2 = GL_ZERO;
1081 m.vertex = &svert[0].v[0];
1082 m.vertexstep = sizeof(surfvert_t);
1083 m.color = &svert[0].c[0];
1084 m.colorstep = sizeof(surfvert_t);
1085 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1086 m.texcoords[0] = &svert[0].st[0];
1087 m.texcoordstep[0] = sizeof(surfvert_t);
1088 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1090 m.numtriangles = mesh->numtriangles;
1091 m.numverts = mesh->numverts;
1092 m.index = mesh->index;
1093 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1095 softwaretransform(v->v, sv->v);
1096 if (r_waterripple.value)
1097 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];
1098 if (surf->flags & SURF_DRAWFULLBRIGHT)
1112 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1113 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1115 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
1116 RSurf_Light(surf->dlightbits, m.numverts);
1117 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
1119 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1121 VectorSubtract(sv->v, r_origin, diff);
1122 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1133 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
1144 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
1146 memset(&m, 0, sizeof(m));
1147 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
1148 m.blendfunc1 = GL_SRC_ALPHA;
1149 m.blendfunc2 = GL_ONE;
1150 m.vertex = &svert[0].v[0];
1151 m.vertexstep = sizeof(surfvert_t);
1152 m.color = &svert[0].c[0];
1153 m.colorstep = sizeof(surfvert_t);
1154 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1155 m.texcoords[0] = &svert[0].st[0];
1156 m.texcoordstep[0] = sizeof(surfvert_t);
1158 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1160 m.numtriangles = mesh->numtriangles;
1161 m.numverts = mesh->numverts;
1162 m.index = mesh->index;
1163 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1165 softwaretransform(v->v, sv->v);
1166 if (r_waterripple.value)
1167 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];
1170 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1171 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
1173 VectorSubtract(sv->v, r_origin, diff);
1174 sv->c[0] = fogcolor[0];
1175 sv->c[1] = fogcolor[1];
1176 sv->c[2] = fogcolor[2];
1177 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
1184 static void RSurfShader_Water(msurface_t *firstsurf)
1187 for (surf = firstsurf;surf;surf = surf->chain)
1188 RSurfShader_Water_Pass_Base(surf);
1190 for (surf = firstsurf;surf;surf = surf->chain)
1191 RSurfShader_Water_Pass_Fog(surf);
1194 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
1199 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1201 rmeshbufferinfo_t m;
1202 memset(&m, 0, sizeof(m));
1203 if (currentrenderentity->effects & EF_ADDITIVE)
1205 m.transparent = true;
1206 m.blendfunc1 = GL_SRC_ALPHA;
1207 m.blendfunc2 = GL_ONE;
1209 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1211 m.transparent = true;
1212 m.blendfunc1 = GL_SRC_ALPHA;
1213 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1217 m.transparent = false;
1218 m.blendfunc1 = GL_ONE;
1219 m.blendfunc2 = GL_ZERO;
1221 m.depthwrite = false;
1222 m.depthdisable = false;
1223 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1224 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1225 ca = currentrenderentity->alpha;
1226 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1228 m.numtriangles = mesh->numtriangles;
1229 m.numverts = mesh->numverts;
1231 if (R_Mesh_Draw_GetBuffer(&m))
1233 cl = (float) (1 << lightscalebit) * m.colorscale;
1234 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1237 if (softwaretransform_complexity)
1239 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)
1241 softwaretransform(v->v, outv);
1243 VectorSubtract(outv, r_origin, diff);
1244 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1246 outst[0] = v->st[0];
1247 outst[1] = v->st[1];
1248 outuv[0] = v->uv[0];
1249 outuv[1] = v->uv[1];
1254 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)
1256 VectorCopy(v->v, outv);
1258 VectorSubtract(outv, r_origin, diff);
1259 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1261 outst[0] = v->st[0];
1262 outst[1] = v->st[1];
1263 outuv[0] = v->uv[0];
1264 outuv[1] = v->uv[1];
1270 if (softwaretransform_complexity)
1272 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)
1274 softwaretransform(v->v, outv);
1276 outc[0] = outc[1] = outc[2] = cl;
1278 outst[0] = v->st[0];
1279 outst[1] = v->st[1];
1280 outuv[0] = v->uv[0];
1281 outuv[1] = v->uv[1];
1286 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)
1288 VectorCopy(v->v, outv);
1290 outc[0] = outc[1] = outc[2] = cl;
1292 outst[0] = v->st[0];
1293 outst[1] = v->st[1];
1294 outuv[0] = v->uv[0];
1295 outuv[1] = v->uv[1];
1303 float diff[3], ifog;
1309 memset(&m, 0, sizeof(m));
1310 if (currentrenderentity->effects & EF_ADDITIVE)
1312 m.transparent = true;
1313 m.blendfunc1 = GL_SRC_ALPHA;
1314 m.blendfunc2 = GL_ONE;
1316 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1318 m.transparent = true;
1319 m.blendfunc1 = GL_SRC_ALPHA;
1320 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1324 m.transparent = false;
1325 m.blendfunc1 = GL_ONE;
1326 m.blendfunc2 = GL_ZERO;
1328 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
1329 m.ca = currentrenderentity->alpha;
1330 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1331 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1332 m.texcoordstep[0] = sizeof(surfvertex_t);
1333 m.texcoordstep[1] = sizeof(surfvertex_t);
1334 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1336 m.numtriangles = mesh->numtriangles;
1337 m.numverts = mesh->numverts;
1338 m.index = mesh->index;
1339 m.texcoords[0] = &mesh->vertex->st[0];
1340 m.texcoords[1] = &mesh->vertex->uv[0];
1343 m.color = &svert[0].c[0];
1344 m.colorstep = sizeof(surfvert_t);
1345 if (softwaretransform_complexity)
1347 m.vertex = &svert[0].v[0];
1348 m.vertexstep = sizeof(surfvert_t);
1349 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1351 softwaretransform(v->v, sv->v);
1352 VectorSubtract(sv->v, r_origin, diff);
1353 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1354 sv->c[0] = m.cr * ifog;
1355 sv->c[1] = m.cg * ifog;
1356 sv->c[2] = m.cb * ifog;
1362 m.vertex = &mesh->vertex->v[0];
1363 m.vertexstep = sizeof(surfvertex_t);
1364 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1366 VectorSubtract(v->v, r_origin, diff);
1367 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1368 sv->c[0] = m.cr * ifog;
1369 sv->c[1] = m.cg * ifog;
1370 sv->c[2] = m.cb * ifog;
1377 if (softwaretransform_complexity)
1379 m.vertex = &svert[0].v[0];
1380 m.vertexstep = sizeof(surfvert_t);
1381 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1382 softwaretransform(v->v, sv->v);
1386 m.vertex = &mesh->vertex->v[0];
1387 m.vertexstep = sizeof(surfvertex_t);
1395 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1400 float *outv, *outc, *outst, cl, ca;
1402 rmeshbufferinfo_t m;
1403 memset(&m, 0, sizeof(m));
1404 m.transparent = false;
1405 m.blendfunc1 = GL_ONE;
1406 m.blendfunc2 = GL_ZERO;
1407 m.depthwrite = false;
1408 m.depthdisable = false;
1409 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1410 ca = currentrenderentity->alpha;
1411 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1413 m.numtriangles = mesh->numtriangles;
1414 m.numverts = mesh->numverts;
1416 if (R_Mesh_Draw_GetBuffer(&m))
1418 cl = (float) (1 << lightscalebit) * m.colorscale;
1419 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1420 if (softwaretransform_complexity)
1422 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)
1424 softwaretransform(v->v, outv);
1426 outc[0] = outc[1] = outc[2] = cl;
1428 outst[0] = v->st[0];
1429 outst[1] = v->st[1];
1434 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)
1436 VectorCopy(v->v, outv);
1438 outc[0] = outc[1] = outc[2] = cl;
1440 outst[0] = v->st[0];
1441 outst[1] = v->st[1];
1453 memset(&m, 0, sizeof(m));
1454 m.transparent = false;
1455 m.blendfunc1 = GL_ONE;
1456 m.blendfunc2 = GL_ZERO;
1457 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1459 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1460 m.texcoordstep[0] = sizeof(surfvertex_t);
1461 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1463 m.numtriangles = mesh->numtriangles;
1464 m.numverts = mesh->numverts;
1465 m.index = mesh->index;
1466 m.texcoords[0] = &mesh->vertex->st[0];
1467 if (softwaretransform_complexity)
1469 m.vertex = &svert[0].v[0];
1470 m.vertexstep = sizeof(surfvert_t);
1471 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1472 softwaretransform(v->v, sv->v);
1476 m.vertex = &mesh->vertex->v[0];
1477 m.vertexstep = sizeof(surfvertex_t);
1484 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1489 float *outv, *outc, *outuv, cl, ca, diff[3];
1491 rmeshbufferinfo_t m;
1492 memset(&m, 0, sizeof(m));
1493 m.transparent = false;
1494 m.blendfunc1 = GL_ZERO;
1495 m.blendfunc2 = GL_SRC_COLOR;
1496 m.depthwrite = false;
1497 m.depthdisable = false;
1498 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1499 ca = currentrenderentity->alpha;
1500 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1502 m.numtriangles = mesh->numtriangles;
1503 m.numverts = mesh->numverts;
1505 if (R_Mesh_Draw_GetBuffer(&m))
1507 cl = (float) (1 << lightscalebit) * m.colorscale;
1508 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1511 if (softwaretransform_complexity)
1513 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)
1515 softwaretransform(v->v, outv);
1517 VectorSubtract(outv, r_origin, diff);
1518 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1520 outuv[0] = v->uv[0];
1521 outuv[1] = v->uv[1];
1526 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)
1528 VectorCopy(v->v, outv);
1530 VectorSubtract(outv, r_origin, diff);
1531 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1533 outuv[0] = v->uv[0];
1534 outuv[1] = v->uv[1];
1540 if (softwaretransform_complexity)
1542 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)
1544 softwaretransform(v->v, outv);
1546 outc[0] = outc[1] = outc[2] = cl;
1548 outuv[0] = v->uv[0];
1549 outuv[1] = v->uv[1];
1554 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)
1556 VectorCopy(v->v, outv);
1558 outc[0] = outc[1] = outc[2] = cl;
1560 outuv[0] = v->uv[0];
1561 outuv[1] = v->uv[1];
1569 float diff[3], ifog;
1575 memset(&m, 0, sizeof(m));
1576 m.transparent = false;
1577 m.blendfunc1 = GL_ZERO;
1578 m.blendfunc2 = GL_SRC_COLOR;
1579 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1581 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1582 m.texcoordstep[0] = sizeof(surfvertex_t);
1583 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1585 m.numtriangles = mesh->numtriangles;
1586 m.numverts = mesh->numverts;
1587 m.index = mesh->index;
1588 m.texcoords[0] = &mesh->vertex->uv[0];
1591 m.color = &svert[0].c[0];
1592 m.colorstep = sizeof(surfvert_t);
1593 if (softwaretransform_complexity)
1595 m.vertex = &svert[0].v[0];
1596 m.vertexstep = sizeof(surfvert_t);
1597 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1599 softwaretransform(v->v, sv->v);
1600 VectorSubtract(sv->v, r_origin, diff);
1601 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1602 sv->c[0] = m.cr * ifog;
1603 sv->c[1] = m.cg * ifog;
1604 sv->c[2] = m.cb * ifog;
1610 m.vertex = &mesh->vertex->v[0];
1611 m.vertexstep = sizeof(surfvertex_t);
1612 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1614 VectorSubtract(v->v, r_origin, diff);
1615 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1616 sv->c[0] = m.cr * ifog;
1617 sv->c[1] = m.cg * ifog;
1618 sv->c[2] = m.cb * ifog;
1625 if (softwaretransform_complexity)
1627 m.vertex = &svert[0].v[0];
1628 m.vertexstep = sizeof(surfvert_t);
1629 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1630 softwaretransform(v->v, sv->v);
1634 m.vertex = &mesh->vertex->v[0];
1635 m.vertexstep = sizeof(surfvertex_t);
1643 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1648 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
1649 float base[3], scale, f;
1652 rmeshbufferinfo_t m;
1653 memset(&m, 0, sizeof(m));
1654 if (currentrenderentity->effects & EF_ADDITIVE)
1656 m.transparent = true;
1657 m.blendfunc1 = GL_SRC_ALPHA;
1658 m.blendfunc2 = GL_ONE;
1660 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1662 m.transparent = true;
1663 m.blendfunc1 = GL_SRC_ALPHA;
1664 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1668 m.transparent = false;
1669 m.blendfunc1 = GL_ONE;
1670 m.blendfunc2 = GL_ZERO;
1672 m.depthwrite = false;
1673 m.depthdisable = false;
1674 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1676 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1678 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1680 ca = currentrenderentity->alpha;
1681 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1683 m.numtriangles = mesh->numtriangles;
1684 m.numverts = mesh->numverts;
1686 if (R_Mesh_Draw_GetBuffer(&m))
1689 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1691 if (currentrenderentity->effects & EF_FULLBRIGHT)
1693 if (softwaretransform_complexity)
1695 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)
1697 softwaretransform(v->v, outv);
1699 VectorSubtract(outv, r_origin, diff);
1700 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1702 outst[0] = v->st[0];
1703 outst[1] = v->st[1];
1708 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)
1710 VectorCopy(v->v, outv);
1712 VectorSubtract(outv, r_origin, diff);
1713 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1715 outst[0] = v->st[0];
1716 outst[1] = v->st[1];
1722 if (softwaretransform_complexity)
1724 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)
1726 softwaretransform(v->v, outv);
1728 VectorCopy(base, outc);
1730 outst[0] = v->st[0];
1731 outst[1] = v->st[1];
1736 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)
1738 VectorCopy(v->v, outv);
1740 VectorCopy(base, outc);
1742 outst[0] = v->st[0];
1743 outst[1] = v->st[1];
1747 if (surf->dlightframe == r_framecount)
1748 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1750 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1752 if (surf->styles[0] != 255)
1754 lm = surf->samples + v->lightmapoffset;
1755 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1756 VectorMA(outc, scale, lm, outc);
1757 if (surf->styles[1] != 255)
1760 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1761 VectorMA(outc, scale, lm, outc);
1762 if (surf->styles[2] != 255)
1765 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1766 VectorMA(outc, scale, lm, outc);
1767 if (surf->styles[3] != 255)
1770 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1771 VectorMA(outc, scale, lm, outc);
1778 VectorSubtract(outv, r_origin, diff);
1779 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1780 VectorScale(outc, f, outc);
1783 VectorScale(outc, cl, outc);
1790 float c[3], base[3], scale, diff[3], ifog;
1797 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1799 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 128.0f);
1801 memset(&m, 0, sizeof(m));
1802 if (currentrenderentity->effects & EF_ADDITIVE)
1804 m.transparent = true;
1805 m.blendfunc1 = GL_SRC_ALPHA;
1806 m.blendfunc2 = GL_ONE;
1808 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1810 m.transparent = true;
1811 m.blendfunc1 = GL_SRC_ALPHA;
1812 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1816 m.transparent = false;
1817 m.blendfunc1 = GL_ONE;
1818 m.blendfunc2 = GL_ZERO;
1820 m.vertex = &svert[0].v[0];
1821 m.vertexstep = sizeof(surfvert_t);
1822 m.color = &svert[0].c[0];
1823 m.colorstep = sizeof(surfvert_t);
1824 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1825 m.texcoordstep[0] = sizeof(surfvertex_t);
1826 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1828 m.numtriangles = mesh->numtriangles;
1829 m.numverts = mesh->numverts;
1830 m.index = mesh->index;
1831 m.texcoords[0] = &mesh->vertex->st[0];
1832 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1834 softwaretransform(v->v, sv->v);
1835 VectorCopy(base, c);
1836 if (surf->styles[0] != 255)
1838 lm = surf->samples + v->lightmapoffset;
1839 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1840 VectorMA(c, scale, lm, c);
1841 if (surf->styles[1] != 255)
1844 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1845 VectorMA(c, scale, lm, c);
1846 if (surf->styles[2] != 255)
1849 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1850 VectorMA(c, scale, lm, c);
1851 if (surf->styles[3] != 255)
1854 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1855 VectorMA(c, scale, lm, c);
1863 sv->c[3] = currentrenderentity->alpha;
1865 if (surf->dlightframe == r_framecount)
1866 RSurf_Light(surf->dlightbits, m.numverts);
1869 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1871 VectorSubtract(sv->v, r_origin, diff);
1872 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1883 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1888 float *outv, *outc, *outst, cl, ca, diff[3];
1890 rmeshbufferinfo_t m;
1891 memset(&m, 0, sizeof(m));
1892 if (currentrenderentity->effects & EF_ADDITIVE)
1894 m.transparent = true;
1895 m.blendfunc1 = GL_SRC_ALPHA;
1896 m.blendfunc2 = GL_ONE;
1898 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1900 m.transparent = true;
1901 m.blendfunc1 = GL_SRC_ALPHA;
1902 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1906 m.transparent = false;
1907 m.blendfunc1 = GL_ONE;
1908 m.blendfunc2 = GL_ZERO;
1910 m.depthwrite = false;
1911 m.depthdisable = false;
1912 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1913 ca = currentrenderentity->alpha;
1914 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1916 m.numtriangles = mesh->numtriangles;
1917 m.numverts = mesh->numverts;
1919 if (R_Mesh_Draw_GetBuffer(&m))
1922 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1925 if (softwaretransform_complexity)
1927 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)
1929 softwaretransform(v->v, outv);
1931 VectorSubtract(outv, r_origin, diff);
1932 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1934 outst[0] = v->st[0];
1935 outst[1] = v->st[1];
1940 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)
1942 VectorCopy(v->v, outv);
1944 VectorSubtract(outv, r_origin, diff);
1945 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1947 outst[0] = v->st[0];
1948 outst[1] = v->st[1];
1954 if (softwaretransform_complexity)
1956 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)
1958 softwaretransform(v->v, outv);
1960 outc[0] = outc[1] = outc[2] = cl;
1962 outst[0] = v->st[0];
1963 outst[1] = v->st[1];
1968 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)
1970 VectorCopy(v->v, outv);
1972 outc[0] = outc[1] = outc[2] = cl;
1974 outst[0] = v->st[0];
1975 outst[1] = v->st[1];
1983 float diff[3], ifog;
1989 memset(&m, 0, sizeof(m));
1990 if (currentrenderentity->effects & EF_ADDITIVE)
1992 m.transparent = true;
1993 m.blendfunc1 = GL_SRC_ALPHA;
1994 m.blendfunc2 = GL_ONE;
1996 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1998 m.transparent = true;
1999 m.blendfunc1 = GL_SRC_ALPHA;
2000 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2004 m.transparent = false;
2005 m.blendfunc1 = GL_ONE;
2006 m.blendfunc2 = GL_ZERO;
2008 m.vertex = &svert[0].v[0];
2009 m.vertexstep = sizeof(surfvert_t);
2010 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2011 m.texcoordstep[0] = sizeof(surfvertex_t);
2012 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2014 m.numtriangles = mesh->numtriangles;
2015 m.numverts = mesh->numverts;
2016 m.index = mesh->index;
2017 m.texcoords[0] = &mesh->vertex->st[0];
2020 m.color = &svert[0].c[0];
2021 m.colorstep = sizeof(surfvert_t);
2022 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2024 softwaretransform(v->v, sv->v);
2025 VectorSubtract(sv->v, r_origin, diff);
2026 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2030 sv->c[3] = currentrenderentity->alpha;
2035 m.cr = m.cg = m.cb = 1;
2036 m.ca = currentrenderentity->alpha;
2037 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2038 softwaretransform(v->v, sv->v);
2045 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
2050 float *outv, *outc, *outst, *outuv, cl, ca, diff[3], f;
2052 rmeshbufferinfo_t m;
2054 if (surf->dlightframe != r_framecount)
2056 if (currentrenderentity->effects & EF_FULLBRIGHT)
2059 memset(&m, 0, sizeof(m));
2060 if (currentrenderentity->effects & EF_ADDITIVE)
2062 m.transparent = true;
2063 m.blendfunc1 = GL_SRC_ALPHA;
2064 m.blendfunc2 = GL_ONE;
2066 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2068 m.transparent = true;
2069 m.blendfunc1 = GL_SRC_ALPHA;
2070 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2074 m.transparent = false;
2075 m.blendfunc1 = GL_ONE;
2076 m.blendfunc2 = GL_ZERO;
2078 m.depthwrite = false;
2079 m.depthdisable = false;
2080 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2081 ca = currentrenderentity->alpha;
2082 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2084 if (RSurf_LightCheck(surf->dlightbits, mesh))
2086 m.numtriangles = mesh->numtriangles;
2087 m.numverts = mesh->numverts;
2089 if (R_Mesh_Draw_GetBuffer(&m))
2092 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2093 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)
2095 softwaretransform(v->v, outv);
2099 outst[0] = v->st[0];
2100 outst[1] = v->st[1];
2102 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
2105 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
2107 VectorSubtract(outv, r_origin, diff);
2108 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2109 VectorScale(outc, f, outc);
2113 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
2114 VectorScale(outc, cl, outc);
2120 float diff[3], ifog;
2126 memset(&m, 0, sizeof(m));
2127 if (currentrenderentity->effects & EF_ADDITIVE)
2128 m.transparent = true;
2129 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
2130 m.transparent = true;
2132 m.transparent = false;
2133 m.blendfunc1 = GL_SRC_ALPHA;
2134 m.blendfunc2 = GL_ONE;
2135 m.vertex = &svert[0].v[0];
2136 m.vertexstep = sizeof(surfvert_t);
2137 m.color = &svert[0].c[0];
2138 m.colorstep = sizeof(surfvert_t);
2139 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
2140 m.texcoordstep[0] = sizeof(surfvertex_t);
2141 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2143 m.numtriangles = mesh->numtriangles;
2144 m.numverts = mesh->numverts;
2145 m.index = mesh->index;
2146 m.texcoords[0] = &mesh->vertex->st[0];
2147 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2149 softwaretransform(v->v, sv->v);
2153 sv->c[3] = currentrenderentity->alpha;
2155 if (RSurf_Light(surf->dlightbits, m.numverts))
2159 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
2161 VectorSubtract(sv->v, r_origin, diff);
2162 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2174 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
2179 float *outv, *outc, *outst, cl, ca, diff[3];
2181 rmeshbufferinfo_t m;
2182 memset(&m, 0, sizeof(m));
2183 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2184 m.blendfunc1 = GL_SRC_ALPHA;
2185 m.blendfunc2 = GL_ONE;
2186 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2187 ca = currentrenderentity->alpha;
2188 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2190 m.numtriangles = mesh->numtriangles;
2191 m.numverts = mesh->numverts;
2193 if (R_Mesh_Draw_GetBuffer(&m))
2196 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2199 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)
2201 softwaretransform(v->v, outv);
2203 VectorSubtract(outv, r_origin, diff);
2204 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
2206 outst[0] = v->st[0];
2207 outst[1] = v->st[1];
2212 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)
2214 softwaretransform(v->v, outv);
2216 outc[0] = outc[1] = outc[2] = cl;
2218 outst[0] = v->st[0];
2219 outst[1] = v->st[1];
2226 float diff[3], ifog;
2232 memset(&m, 0, sizeof(m));
2233 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2234 m.blendfunc1 = GL_SRC_ALPHA;
2235 m.blendfunc2 = GL_ONE;
2239 m.ca = currentrenderentity->alpha;
2240 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
2241 m.texcoordstep[0] = sizeof(surfvertex_t);
2242 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2244 m.numtriangles = mesh->numtriangles;
2245 m.numverts = mesh->numverts;
2246 m.index = mesh->index;
2247 m.texcoords[0] = &mesh->vertex->st[0];
2250 m.color = &svert[0].c[0];
2251 m.colorstep = sizeof(surfvert_t);
2252 if (softwaretransform_complexity)
2254 m.vertex = &svert[0].v[0];
2255 m.vertexstep = sizeof(surfvert_t);
2256 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2258 softwaretransform(v->v, sv->v);
2259 VectorSubtract(sv->v, r_origin, diff);
2260 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2261 sv->c[0] = m.cr * ifog;
2262 sv->c[1] = m.cg * ifog;
2263 sv->c[2] = m.cb * ifog;
2269 m.vertex = &mesh->vertex->v[0];
2270 m.vertexstep = sizeof(surfvertex_t);
2271 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2273 VectorSubtract(v->v, r_origin, diff);
2274 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
2275 sv->c[0] = m.cr * ifog;
2276 sv->c[1] = m.cg * ifog;
2277 sv->c[2] = m.cb * ifog;
2284 if (softwaretransform_complexity)
2286 m.vertex = &svert[0].v[0];
2287 m.vertexstep = sizeof(surfvert_t);
2288 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2289 softwaretransform(v->v, sv->v);
2293 m.vertex = &mesh->vertex->v[0];
2294 m.vertexstep = sizeof(surfvertex_t);
2302 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
2307 float *outv, *outc, *outst, cl, ca, diff[3], f;
2309 rmeshbufferinfo_t m;
2310 memset(&m, 0, sizeof(m));
2311 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2312 m.blendfunc1 = GL_SRC_ALPHA;
2313 m.blendfunc2 = GL_ONE;
2314 ca = currentrenderentity->alpha;
2315 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2317 m.numtriangles = mesh->numtriangles;
2318 m.numverts = mesh->numverts;
2320 if (R_Mesh_Draw_GetBuffer(&m))
2323 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
2324 if (softwaretransform_complexity)
2326 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)
2328 softwaretransform(v->v, outv);
2330 VectorSubtract(outv, r_origin, diff);
2331 f = cl * exp(fogdensity/DotProduct(diff, diff));
2332 VectorScale(fogcolor, f, outc);
2334 outst[0] = v->st[0];
2335 outst[1] = v->st[1];
2340 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)
2342 VectorCopy(v->v, outv);
2344 VectorSubtract(outv, r_origin, diff);
2345 VectorSubtract(outv, r_origin, diff);
2346 f = cl * exp(fogdensity/DotProduct(diff, diff));
2347 VectorScale(fogcolor, f, outc);
2349 outst[0] = v->st[0];
2350 outst[1] = v->st[1];
2363 memset(&m, 0, sizeof(m));
2364 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
2365 m.blendfunc1 = GL_SRC_ALPHA;
2366 m.blendfunc2 = GL_ONE;
2367 m.color = &svert[0].c[0];
2368 m.colorstep = sizeof(surfvert_t);
2369 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
2370 m.texcoordstep[0] = sizeof(surfvertex_t);
2371 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
2373 m.numtriangles = mesh->numtriangles;
2374 m.numverts = mesh->numverts;
2375 m.index = mesh->index;
2376 m.texcoords[0] = &mesh->vertex->st[0];
2377 if (softwaretransform_complexity)
2379 m.vertex = &svert[0].v[0];
2380 m.vertexstep = sizeof(surfvert_t);
2381 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2383 softwaretransform(v->v, sv->v);
2384 VectorSubtract(sv->v, r_origin, diff);
2385 sv->c[0] = fogcolor[0];
2386 sv->c[1] = fogcolor[1];
2387 sv->c[2] = fogcolor[2];
2388 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2393 m.vertex = &mesh->vertex->v[0];
2394 m.vertexstep = sizeof(surfvertex_t);
2395 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
2397 VectorSubtract(v->v, r_origin, diff);
2398 sv->c[0] = fogcolor[0];
2399 sv->c[1] = fogcolor[1];
2400 sv->c[2] = fogcolor[2];
2401 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
2409 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
2412 for (surf = firstsurf;surf;surf = surf->chain)
2415 RSurfShader_Wall_Pass_BaseFullbright(surf);
2417 for (surf = firstsurf;surf;surf = surf->chain)
2418 if (surf->currenttexture->glowtexture)
2419 RSurfShader_Wall_Pass_Glow(surf);
2421 for (surf = firstsurf;surf;surf = surf->chain)
2422 RSurfShader_Wall_Pass_Fog(surf);
2425 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
2428 for (surf = firstsurf;surf;surf = surf->chain)
2431 RSurfShader_Wall_Pass_BaseVertex(surf);
2433 for (surf = firstsurf;surf;surf = surf->chain)
2434 if (surf->currenttexture->glowtexture)
2435 RSurfShader_Wall_Pass_Glow(surf);
2437 for (surf = firstsurf;surf;surf = surf->chain)
2438 RSurfShader_Wall_Pass_Fog(surf);
2441 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
2444 if (r_vertexsurfaces.integer)
2446 for (surf = firstsurf;surf;surf = surf->chain)
2449 RSurfShader_Wall_Pass_BaseVertex(surf);
2451 for (surf = firstsurf;surf;surf = surf->chain)
2452 if (surf->currenttexture->glowtexture)
2453 RSurfShader_Wall_Pass_Glow(surf);
2455 for (surf = firstsurf;surf;surf = surf->chain)
2456 RSurfShader_Wall_Pass_Fog(surf);
2458 else if (r_multitexture.integer)
2460 if (r_dlightmap.integer)
2462 for (surf = firstsurf;surf;surf = surf->chain)
2465 RSurfShader_Wall_Pass_BaseMTex(surf);
2467 for (surf = firstsurf;surf;surf = surf->chain)
2468 if (surf->currenttexture->glowtexture)
2469 RSurfShader_Wall_Pass_Glow(surf);
2471 for (surf = firstsurf;surf;surf = surf->chain)
2472 RSurfShader_Wall_Pass_Fog(surf);
2476 for (surf = firstsurf;surf;surf = surf->chain)
2479 RSurfShader_Wall_Pass_BaseMTex(surf);
2481 for (surf = firstsurf;surf;surf = surf->chain)
2482 if (surf->dlightframe == r_framecount)
2483 RSurfShader_Wall_Pass_Light(surf);
2484 for (surf = firstsurf;surf;surf = surf->chain)
2485 if (surf->currenttexture->glowtexture)
2486 RSurfShader_Wall_Pass_Glow(surf);
2488 for (surf = firstsurf;surf;surf = surf->chain)
2489 RSurfShader_Wall_Pass_Fog(surf);
2492 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
2494 for (surf = firstsurf;surf;surf = surf->chain)
2497 RSurfShader_Wall_Pass_BaseVertex(surf);
2499 for (surf = firstsurf;surf;surf = surf->chain)
2500 if (surf->currenttexture->glowtexture)
2501 RSurfShader_Wall_Pass_Glow(surf);
2503 for (surf = firstsurf;surf;surf = surf->chain)
2504 RSurfShader_Wall_Pass_Fog(surf);
2508 if (r_dlightmap.integer)
2510 for (surf = firstsurf;surf;surf = surf->chain)
2513 RSurfShader_Wall_Pass_BaseTexture(surf);
2515 for (surf = firstsurf;surf;surf = surf->chain)
2516 RSurfShader_Wall_Pass_BaseLightmap(surf);
2517 for (surf = firstsurf;surf;surf = surf->chain)
2518 if (surf->currenttexture->glowtexture)
2519 RSurfShader_Wall_Pass_Glow(surf);
2521 for (surf = firstsurf;surf;surf = surf->chain)
2522 RSurfShader_Wall_Pass_Fog(surf);
2526 for (surf = firstsurf;surf;surf = surf->chain)
2529 RSurfShader_Wall_Pass_BaseTexture(surf);
2531 for (surf = firstsurf;surf;surf = surf->chain)
2532 RSurfShader_Wall_Pass_BaseLightmap(surf);
2533 for (surf = firstsurf;surf;surf = surf->chain)
2534 if (surf->dlightframe == r_framecount)
2535 RSurfShader_Wall_Pass_Light(surf);
2536 for (surf = firstsurf;surf;surf = surf->chain)
2537 if (surf->currenttexture->glowtexture)
2538 RSurfShader_Wall_Pass_Glow(surf);
2540 for (surf = firstsurf;surf;surf = surf->chain)
2541 RSurfShader_Wall_Pass_Fog(surf);
2547 =============================================================
2551 =============================================================
2554 static void R_SolidWorldNode (void)
2556 if (r_viewleaf->contents != CONTENTS_SOLID)
2559 mportal_t *p, *pstack[8192];
2560 msurface_t *surf, **mark, **endmark;
2562 // LordHavoc: portal-passage worldnode; follows portals leading
2563 // outward from viewleaf, if a portal leads offscreen it is not
2564 // followed, in indoor maps this can often cull a great deal of
2565 // geometry away when pvs data is not present (useful with pvs as well)
2568 leaf->worldnodeframe = r_framecount;
2573 leaf->visframe = r_framecount;
2575 if (leaf->nummarksurfaces)
2577 mark = leaf->firstmarksurface;
2578 endmark = mark + leaf->nummarksurfaces;
2582 // make sure surfaces are only processed once
2583 if (surf->worldnodeframe == r_framecount)
2585 surf->worldnodeframe = r_framecount;
2586 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2588 if (surf->flags & SURF_PLANEBACK)
2589 surf->visframe = r_framecount;
2593 if (!(surf->flags & SURF_PLANEBACK))
2594 surf->visframe = r_framecount;
2597 while (mark < endmark);
2600 // follow portals into other leafs
2602 for (;p;p = p->next)
2604 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2607 if (leaf->worldnodeframe != r_framecount)
2609 leaf->worldnodeframe = r_framecount;
2610 if (leaf->contents != CONTENTS_SOLID)
2612 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2614 p->visframe = r_framecount;
2615 pstack[portalstack++] = p;
2619 p = pstack[--portalstack];
2631 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
2632 int nodestackpos = 0;
2633 // LordHavoc: recursive descending worldnode; if portals are not
2634 // available, this is a good last resort, can cull large amounts of
2635 // geometry, but is more time consuming than portal-passage and renders
2636 // things behind walls
2639 if (R_NotCulledBox(node->mins, node->maxs))
2641 if (node->numsurfaces)
2643 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
2644 if (PlaneDiff (r_origin, node->plane) < 0)
2646 for (;surf < surfend;surf++)
2648 if (surf->flags & SURF_PLANEBACK)
2649 surf->visframe = r_framecount;
2654 for (;surf < surfend;surf++)
2656 if (!(surf->flags & SURF_PLANEBACK))
2657 surf->visframe = r_framecount;
2662 // recurse down the children
2663 if (node->children[0]->contents >= 0)
2665 if (node->children[1]->contents >= 0)
2667 if (nodestackpos < 8192)
2668 nodestack[nodestackpos++] = node->children[1];
2669 node = node->children[0];
2673 ((mleaf_t *)node->children[1])->visframe = r_framecount;
2674 node = node->children[0];
2679 ((mleaf_t *)node->children[0])->visframe = r_framecount;
2680 if (node->children[1]->contents >= 0)
2682 node = node->children[1];
2685 else if (nodestackpos > 0)
2687 ((mleaf_t *)node->children[1])->visframe = r_framecount;
2688 node = nodestack[--nodestackpos];
2693 else if (nodestackpos > 0)
2695 node = nodestack[--nodestackpos];
2701 static int r_portalframecount = 0;
2703 static void R_PVSWorldNode()
2706 mportal_t *p, *pstack[8192];
2707 msurface_t *surf, **mark, **endmark;
2711 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
2714 leaf->worldnodeframe = r_framecount;
2719 leaf->visframe = r_framecount;
2721 if (leaf->nummarksurfaces)
2723 mark = leaf->firstmarksurface;
2724 endmark = mark + leaf->nummarksurfaces;
2728 // make sure surfaces are only processed once
2729 if (surf->worldnodeframe == r_framecount)
2731 surf->worldnodeframe = r_framecount;
2732 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
2734 if (surf->flags & SURF_PLANEBACK)
2735 surf->visframe = r_framecount;
2739 if (!(surf->flags & SURF_PLANEBACK))
2740 surf->visframe = r_framecount;
2743 while (mark < endmark);
2746 // follow portals into other leafs
2747 for (p = leaf->portals;p;p = p->next)
2749 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
2752 if (leaf->worldnodeframe != r_framecount)
2754 leaf->worldnodeframe = r_framecount;
2755 if (leaf->contents != CONTENTS_SOLID)
2757 i = (leaf - cl.worldmodel->leafs) - 1;
2758 if (worldvis[i>>3] & (1<<(i&7)))
2760 if (R_NotCulledBox(leaf->mins, leaf->maxs))
2762 pstack[portalstack++] = p;
2766 p = pstack[--portalstack];
2778 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
2779 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
2780 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
2781 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
2782 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
2784 int Cshader_count = 5;
2785 Cshader_t *Cshaders[5] =
2787 &Cshader_wall_vertex,
2788 &Cshader_wall_lightmap,
2789 &Cshader_wall_fullbright,
2794 void R_PrepareSurfaces(void)
2796 int i, alttextures, texframe, framecount;
2801 for (i = 0;i < Cshader_count;i++)
2802 Cshaders[i]->chain = NULL;
2804 model = currentrenderentity->model;
2805 alttextures = currentrenderentity->frame != 0;
2806 texframe = (int)(cl.time * 5.0f);
2808 for (i = 0;i < model->nummodelsurfaces;i++)
2810 surf = model->modelsortedsurfaces[i];
2811 if (surf->visframe == r_framecount)
2813 if (surf->insertframe != r_framecount)
2815 surf->insertframe = r_framecount;
2817 t = surf->texinfo->texture;
2820 framecount = t->anim_total[alttextures];
2821 if (framecount >= 2)
2822 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
2824 surf->currenttexture = t->anim_frames[alttextures][0];
2827 surf->currenttexture = t;
2830 surf->chain = surf->shader->chain;
2831 surf->shader->chain = surf;
2836 void R_DrawSurfaces (int type)
2841 for (i = 0;i < Cshader_count;i++)
2843 shader = Cshaders[i];
2844 if (shader->chain && shader->shaderfunc[type])
2845 shader->shaderfunc[type](shader->chain);
2849 static float portalpointbuffer[256][3];
2851 void R_DrawPortals(void)
2854 mportal_t *portal, *endportal;
2857 drawportals = r_drawportals.integer;
2859 if (drawportals < 1)
2862 memset(&m, 0, sizeof(m));
2863 m.transparent = true;
2864 m.blendfunc1 = GL_SRC_ALPHA;
2865 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2866 m.vertex = &portalpointbuffer[0][0];
2867 m.vertexstep = sizeof(float[3]);
2869 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2871 if (portal->visframe == r_portalframecount)
2873 if (portal->numpoints <= 256)
2875 i = portal - cl.worldmodel->portals;
2876 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2877 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2878 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2879 point = portal->points;
2880 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2882 for (i = portal->numpoints - 1;i >= 0;i--)
2883 VectorCopy(point[i].position, portalpointbuffer[i]);
2887 for (i = 0;i < portal->numpoints;i++)
2888 VectorCopy(point[i].position, portalpointbuffer[i]);
2890 R_Mesh_DrawPolygon(&m, portal->numpoints);
2896 void R_SetupForBModelRendering(void)
2903 // because bmodels can be reused, we have to decide which things to render
2904 // from scratch every time
2906 model = currentrenderentity->model;
2908 softwaretransformforentity (currentrenderentity);
2909 softwareuntransform(r_origin, modelorg);
2911 for (i = 0;i < model->nummodelsurfaces;i++)
2913 surf = model->modelsortedsurfaces[i];
2914 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2915 surf->visframe = r_framecount;
2917 surf->visframe = -1;
2918 surf->worldnodeframe = -1;
2919 surf->lightframe = -1;
2920 surf->dlightframe = -1;
2921 surf->insertframe = -1;
2925 void R_SetupForWorldRendering(void)
2927 // there is only one instance of the world, but it can be rendered in
2930 currentrenderentity = &cl_entities[0].render;
2931 softwaretransformidentity();
2934 static void R_SurfMarkLights (void)
2939 if (r_dynamic.integer)
2942 if (!r_vertexsurfaces.integer)
2944 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2946 surf = currentrenderentity->model->modelsortedsurfaces[i];
2947 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2949 if (surf->cached_dlight
2950 || surf->cached_ambient != r_ambient.value
2951 || surf->cached_lightscalebit != lightscalebit)
2952 R_BuildLightMap(surf, false); // base lighting changed
2953 else if (r_dynamic.integer)
2955 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2956 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2957 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2958 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2959 R_BuildLightMap(surf, false); // base lighting changed
2960 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2961 R_BuildLightMap(surf, true); // only dlights
2968 void R_MarkWorldLights(void)
2970 R_SetupForWorldRendering();
2979 void R_DrawWorld (void)
2981 R_SetupForWorldRendering();
2983 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2984 R_SolidWorldNode ();
2994 void R_DrawBrushModelSky (void)
2996 R_SetupForBModelRendering();
2998 R_PrepareSurfaces();
2999 R_DrawSurfaces(SHADERSTAGE_SKY);
3002 void R_DrawBrushModelNormal (void)
3006 // have to flush queue because of possible lightmap reuse
3009 R_SetupForBModelRendering();
3013 R_PrepareSurfaces();
3015 if (!skyrendermasked)
3016 R_DrawSurfaces(SHADERSTAGE_SKY);
3017 R_DrawSurfaces(SHADERSTAGE_NORMAL);
3020 static void gl_surf_start(void)
3024 static void gl_surf_shutdown(void)
3028 static void gl_surf_newmap(void)
3032 void GL_Surf_Init(void)
3035 dlightdivtable[0] = 4194304;
3036 for (i = 1;i < 32768;i++)
3037 dlightdivtable[i] = 4194304 / (i << 7);
3039 Cvar_RegisterVariable(&r_ambient);
3040 Cvar_RegisterVariable(&r_vertexsurfaces);
3041 Cvar_RegisterVariable(&r_dlightmap);
3042 Cvar_RegisterVariable(&r_drawportals);
3043 Cvar_RegisterVariable(&r_testvis);
3044 Cvar_RegisterVariable(&r_floatbuildlightmap);
3046 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);