2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 #define MAX_LIGHTMAP_SIZE 256
26 static signed int blocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
28 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
30 cvar_t r_ambient = {0, "r_ambient", "0"};
31 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
32 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
33 cvar_t r_drawportals = {0, "r_drawportals", "0"};
34 cvar_t r_testvis = {0, "r_testvis", "0"};
36 static void gl_surf_start(void)
40 static void gl_surf_shutdown(void)
44 static void gl_surf_newmap(void)
48 static int dlightdivtable[32768];
50 void GL_Surf_Init(void)
53 dlightdivtable[0] = 4194304;
54 for (i = 1;i < 32768;i++)
55 dlightdivtable[i] = 4194304 / (i << 7);
57 Cvar_RegisterVariable(&r_ambient);
58 Cvar_RegisterVariable(&r_vertexsurfaces);
59 Cvar_RegisterVariable(&r_dlightmap);
60 Cvar_RegisterVariable(&r_drawportals);
61 Cvar_RegisterVariable(&r_testvis);
63 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
66 static int R_AddDynamicLights (msurface_t *surf)
68 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
73 // LordHavoc: use 64bit integer... shame it's not very standardized...
74 #if _MSC_VER || __BORLANDC__
82 smax = (surf->extents[0] >> 4) + 1;
83 tmax = (surf->extents[1] >> 4) + 1;
85 for (lnum = 0; lnum < r_numdlights; lnum++)
87 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
88 continue; // not lit by this light
90 softwareuntransform(r_dlight[lnum].origin, local);
91 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
93 // for comparisons to minimum acceptable light
94 // compensate for LIGHTOFFSET
95 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
102 if (surf->plane->type < 3)
104 VectorCopy(local, impact);
105 impact[surf->plane->type] -= dist;
109 impact[0] = local[0] - surf->plane->normal[0] * dist;
110 impact[1] = local[1] - surf->plane->normal[1] * dist;
111 impact[2] = local[2] - surf->plane->normal[2] * dist;
114 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
115 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
117 s = bound(0, impacts, smax * 16) - impacts;
118 t = bound(0, impactt, tmax * 16) - impactt;
119 i = s * s + t * t + dist2;
123 // reduce calculations
124 for (s = 0, i = impacts; s < smax; s++, i -= 16)
125 sdtable[s] = i * i + dist2;
127 maxdist3 = maxdist - dist2;
129 // convert to 8.8 blocklights format
130 red = r_dlight[lnum].light[0];
131 green = r_dlight[lnum].light[1];
132 blue = r_dlight[lnum].light[2];
133 subtract = (int) (r_dlight[lnum].lightsubtract * 4194304.0f);
138 for (t = 0;t < tmax;t++, i -= 16)
141 // make sure some part of it is visible on this line
144 maxdist2 = maxdist - td;
145 for (s = 0;s < smax;s++)
147 if (sdtable[s] < maxdist2)
149 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
152 bl[0] += (red * k) >> 8;
153 bl[1] += (green * k) >> 8;
154 bl[2] += (blue * k) >> 8;
168 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
171 msurface_t *surf, *endsurf;
172 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
175 // LordHavoc: use 64bit integer... shame it's not very standardized...
176 #if _MSC_VER || __BORLANDC__
183 // for comparisons to minimum acceptable light
184 // compensate for 4096 offset
185 maxdist = radius * radius + 4096;
187 // clamp radius to avoid exceeding 32768 entry division table
188 if (maxdist > 4194304)
191 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
194 if (node->contents < 0)
196 ndist = PlaneDiff(origin, node->plane);
199 node = node->children[0];
204 node = node->children[1];
208 dist2 = ndist * ndist;
212 maxdist3 = maxdist - dist2;
214 if (node->plane->type < 3)
216 VectorCopy(origin, impact);
217 impact[node->plane->type] -= ndist;
221 impact[0] = origin[0] - node->plane->normal[0] * ndist;
222 impact[1] = origin[1] - node->plane->normal[1] * ndist;
223 impact[2] = origin[2] - node->plane->normal[2] * ndist;
226 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
228 if (surf->stainsamples)
230 smax = (surf->extents[0] >> 4) + 1;
231 tmax = (surf->extents[1] >> 4) + 1;
233 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
234 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
236 s = bound(0, impacts, smax * 16) - impacts;
237 t = bound(0, impactt, tmax * 16) - impactt;
238 i = s * s + t * t + dist2;
242 // reduce calculations
243 for (s = 0, i = impacts; s < smax; s++, i -= 16)
244 sdtable[s] = i * i + dist2;
246 // convert to 8.8 blocklights format
247 bl = surf->stainsamples;
252 for (t = 0;t < tmax;t++, i -= 16)
255 // make sure some part of it is visible on this line
258 maxdist2 = maxdist - td;
259 for (s = 0;s < smax;s++)
261 if (sdtable[s] < maxdist2)
263 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
266 ratio = rand() & 255;
267 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
271 a = bound(0, a, 256);
272 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
273 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
274 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
275 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
276 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
277 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
288 // force lightmap upload
290 surf->cached_dlight = true;
295 if (node->children[0]->contents >= 0)
297 if (node->children[1]->contents >= 0)
299 R_StainNode(node->children[0], model, origin, radius, icolor);
300 node = node->children[1];
305 node = node->children[0];
309 else if (node->children[1]->contents >= 0)
311 node = node->children[1];
316 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
319 entity_render_t *ent;
331 model = cl.worldmodel;
332 softwaretransformidentity();
333 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
335 // look for embedded bmodels
336 for (n = 0;n < cl_num_brushmodel_entities;n++)
338 ent = cl_brushmodel_entities[n];
340 if (model && model->name[0] == '*')
342 Mod_CheckLoaded(model);
343 if (model->type == mod_brush)
345 softwaretransformforentity(ent);
346 softwareuntransform(origin, org);
347 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
357 Combine and scale multiple lightmaps into the 8.8 format in blocklights
360 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
362 int smax, tmax, i, j, size, size3, shift, scale, maps, *bl, stride, l;
363 qbyte *lightmap, *out, *stain;
365 // update cached lighting info
366 surf->cached_dlight = 0;
367 surf->cached_lightscalebit = lightscalebit;
368 surf->cached_ambient = r_ambient.value;
369 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
370 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
371 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
372 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
374 smax = (surf->extents[0]>>4)+1;
375 tmax = (surf->extents[1]>>4)+1;
378 lightmap = surf->samples;
380 // set to full bright if no light data
381 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
384 for (i = 0;i < size3;i++)
390 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
394 for (i = 0;i < size3;i++)
398 memset(&blocklights[0], 0, size*3*sizeof(int));
400 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
402 surf->cached_dlight = R_AddDynamicLights(surf);
403 if (surf->cached_dlight)
405 else if (dlightchanged)
406 return; // don't upload if only updating dlights and none mattered
409 // add all the lightmaps
413 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
414 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
415 bl[i] += lightmap[i] * scale;
419 stain = surf->stainsamples;
422 // deal with lightmap brightness scale
423 shift = 15 + lightscalebit;
424 if (currentrenderentity->model->lightmaprgba)
426 stride = (surf->lightmaptexturestride - smax) * 4;
427 for (i = 0;i < tmax;i++, out += stride)
429 for (j = 0;j < smax;j++)
431 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
432 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
433 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
440 stride = (surf->lightmaptexturestride - smax) * 3;
441 for (i = 0;i < tmax;i++, out += stride)
443 for (j = 0;j < smax;j++)
445 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
446 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
447 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
452 R_UpdateTexture(surf->lightmaptexture, templight);
457 =============================================================
461 =============================================================
465 static float turbsin[256] =
467 #include "gl_warp_sin.h"
469 #define TURBSCALE (256.0 / (2 * M_PI))
471 // only need to hold as many verts as the mesh splitter will allow in model_brush.c
472 #define MAX_SURFVERTS 3072
481 static surfvert_t svert[MAX_SURFVERTS]; // used by the following functions
483 static void RSurfShader_Sky(msurface_t *firstsurf)
487 float number, length, dir[3], speedscale;
493 // LordHavoc: HalfLife maps have freaky skypolys...
494 if (currentrenderentity->model->ishlbsp)
501 skyrendernow = false;
504 for (surf = firstsurf;surf;surf = surf->chain)
506 // draw depth-only polys
507 memset(&m, 0, sizeof(m));
508 m.transparent = false;
509 m.blendfunc1 = GL_ZERO;
510 m.blendfunc2 = GL_ONE;
512 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
514 m.numtriangles = mesh->numtriangles;
515 m.numverts = mesh->numverts;
516 m.index = mesh->index;
517 if (softwaretransform_complexity)
519 m.vertex = &svert[0].v[0];
520 m.vertexstep = sizeof(surfvert_t);
521 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
522 softwaretransform(v->v, sv->v);
526 m.vertex = &mesh->vertex[0].v[0];
527 m.vertexstep = sizeof(surfvertex_t);
533 else if (skyrenderglquake)
535 for (surf = firstsurf;surf;surf = surf->chain)
537 memset(&m, 0, sizeof(m));
538 m.transparent = false;
539 m.blendfunc1 = GL_ONE;
540 m.blendfunc2 = GL_ZERO;
541 m.vertex = &svert[0].v[0];
542 m.vertexstep = sizeof(surfvert_t);
547 m.tex[0] = R_GetTexture(solidskytexture);
548 m.texcoords[0] = &svert[0].st[0];
549 m.texcoordstep[0] = sizeof(surfvert_t);
550 speedscale = cl.time * (8.0/128.0);
551 speedscale -= (int)speedscale;
552 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
554 m.numtriangles = mesh->numtriangles;
555 m.numverts = mesh->numverts;
556 m.index = mesh->index;
557 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
559 softwaretransform(v->v, sv->v);
560 VectorSubtract (sv->v, r_origin, dir);
561 // flatten the sphere
564 number = DotProduct(dir, dir);
566 length = 3.0f / sqrt(number);
568 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
569 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
572 sv->st[0] = speedscale + dir[0] * length;
573 sv->st[1] = speedscale + dir[1] * length;
581 for (surf = firstsurf;surf;surf = surf->chain)
584 memset(&m, 0, sizeof(m));
585 m.transparent = false;
586 m.blendfunc1 = GL_ONE;
587 m.blendfunc2 = GL_ZERO;
592 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
594 m.numtriangles = mesh->numtriangles;
595 m.numverts = mesh->numverts;
596 m.index = mesh->index;
597 if (softwaretransform_complexity)
599 m.vertex = &svert[0].v[0];
600 m.vertexstep = sizeof(surfvert_t);
601 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
602 softwaretransform(v->v, sv->v);
606 m.vertex = &mesh->vertex[0].v[0];
607 m.vertexstep = sizeof(surfvertex_t);
613 if (skyrenderglquake)
615 for (surf = firstsurf;surf;surf = surf->chain)
617 memset(&m, 0, sizeof(m));
618 m.transparent = false;
619 m.blendfunc1 = GL_SRC_ALPHA;
620 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
621 m.vertex = &svert[0].v[0];
622 m.vertexstep = sizeof(surfvert_t);
627 m.tex[0] = R_GetTexture(alphaskytexture);
628 m.texcoords[0] = &svert[0].st[0];
629 m.texcoordstep[0] = sizeof(surfvert_t);
630 speedscale = cl.time * (16.0/128.0);
631 speedscale -= (int)speedscale;
632 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
634 m.numtriangles = mesh->numtriangles;
635 m.numverts = mesh->numverts;
636 m.index = mesh->index;
637 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
639 softwaretransform(v->v, sv->v);
640 VectorSubtract (sv->v, r_origin, dir);
641 // flatten the sphere
644 number = DotProduct(dir, dir);
646 length = 3.0f / sqrt(number);
648 *((int *)&length) = 0x5f3759df - ((* (int *) &number) >> 1);
649 length = 3.0f * (length * (1.5f - (number * 0.5f * length * length)));
652 sv->st[0] = speedscale + dir[0] * length;
653 sv->st[1] = speedscale + dir[1] * length;
661 static int RSurf_Light(int *dlightbits, int numverts)
664 int i, l, lit = false;
668 for (l = 0;l < r_numdlights;l++)
670 if (dlightbits[l >> 5] & (1 << (l & 31)))
673 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
674 VectorCopy(rd->origin, lightorigin);
675 for (i = 0, sv = svert;i < numverts;i++, sv++)
677 f = VectorDistance2(sv->v, lightorigin) + LIGHTOFFSET;
678 if (f < rd->cullradius2)
680 f = (1.0f / f) - rd->lightsubtract;
681 sv->c[0] += rd->light[0] * f;
682 sv->c[1] += rd->light[1] * f;
683 sv->c[2] += rd->light[2] * f;
692 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
695 float diff[3], alpha, ifog;
700 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
702 memset(&m, 0, sizeof(m));
703 if (alpha != 1 || surf->currenttexture->fogtexture != NULL)
705 m.transparent = true;
706 m.blendfunc1 = GL_SRC_ALPHA;
707 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
711 m.transparent = false;
712 m.blendfunc1 = GL_ONE;
713 m.blendfunc2 = GL_ZERO;
715 m.vertex = &svert[0].v[0];
716 m.vertexstep = sizeof(surfvert_t);
717 m.color = &svert[0].c[0];
718 m.colorstep = sizeof(surfvert_t);
719 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
720 m.texcoords[0] = &svert[0].st[0];
721 m.texcoordstep[0] = sizeof(surfvert_t);
722 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
724 m.numtriangles = mesh->numtriangles;
725 m.numverts = mesh->numverts;
726 m.index = mesh->index;
727 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
729 softwaretransform(v->v, sv->v);
730 if (r_waterripple.value)
731 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];
732 if (surf->flags & SURF_DRAWFULLBRIGHT)
746 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
747 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
749 if (surf->dlightframe == r_framecount && !(surf->flags & SURF_DRAWFULLBRIGHT))
750 RSurf_Light(surf->dlightbits, m.numverts);
751 if (fogenabled && (surf->flags & SURF_DRAWNOALPHA))
753 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
755 VectorSubtract(sv->v, r_origin, diff);
756 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
766 static void RSurfShader_Water_Pass_Glow(msurface_t *surf)
769 float diff[3], alpha, ifog;
774 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
776 memset(&m, 0, sizeof(m));
777 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
778 m.blendfunc1 = GL_SRC_ALPHA;
779 m.blendfunc2 = GL_ONE;
780 m.vertex = &svert[0].v[0];
781 m.vertexstep = sizeof(surfvert_t);
786 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
787 m.texcoords[0] = &svert[0].st[0];
788 m.texcoordstep[0] = sizeof(surfvert_t);
789 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
791 m.numtriangles = mesh->numtriangles;
792 m.numverts = mesh->numverts;
793 m.index = mesh->index;
796 m.color = &svert[0].c[0];
797 m.colorstep = sizeof(surfvert_t);
798 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
800 softwaretransform(v->v, sv->v);
801 if (r_waterripple.value)
802 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];
803 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
804 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
805 VectorSubtract(sv->v, r_origin, diff);
806 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
807 sv->c[0] = m.cr * ifog;
808 sv->c[1] = m.cg * ifog;
809 sv->c[2] = m.cb * ifog;
815 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
817 softwaretransform(v->v, sv->v);
818 if (r_waterripple.value)
819 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];
820 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
821 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
828 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
837 alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
839 memset(&m, 0, sizeof(m));
840 m.transparent = alpha != 1 || surf->currenttexture->fogtexture != NULL;
841 m.blendfunc1 = GL_SRC_ALPHA;
842 m.blendfunc2 = GL_ONE;
843 m.vertex = &svert[0].v[0];
844 m.vertexstep = sizeof(surfvert_t);
845 m.color = &svert[0].c[0];
846 m.colorstep = sizeof(surfvert_t);
847 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
848 m.texcoords[0] = &svert[0].st[0];
849 m.texcoordstep[0] = sizeof(surfvert_t);
851 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
853 m.numtriangles = mesh->numtriangles;
854 m.numverts = mesh->numverts;
855 m.index = mesh->index;
856 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
858 softwaretransform(v->v, sv->v);
859 if (r_waterripple.value)
860 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];
863 sv->st[0] = (v->st[0] + turbsin[(int)((v->st[1]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
864 sv->st[1] = (v->st[1] + turbsin[(int)((v->st[0]*0.125f+cl.time) * TURBSCALE) & 255]) * (1.0f / 64.0f);
866 VectorSubtract(sv->v, r_origin, diff);
867 sv->c[0] = fogcolor[0];
868 sv->c[1] = fogcolor[1];
869 sv->c[2] = fogcolor[2];
870 sv->c[3] = alpha * exp(fogdensity/DotProduct(diff, diff));
876 static void RSurfShader_Water(msurface_t *firstsurf)
879 for (surf = firstsurf;surf;surf = surf->chain)
880 RSurfShader_Water_Pass_Base(surf);
881 for (surf = firstsurf;surf;surf = surf->chain)
882 if (surf->currenttexture->glowtexture)
883 RSurfShader_Water_Pass_Glow(surf);
885 for (surf = firstsurf;surf;surf = surf->chain)
886 RSurfShader_Water_Pass_Fog(surf);
889 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
898 memset(&m, 0, sizeof(m));
899 if (currentrenderentity->effects & EF_ADDITIVE)
901 m.transparent = true;
902 m.blendfunc1 = GL_SRC_ALPHA;
903 m.blendfunc2 = GL_ONE;
905 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
907 m.transparent = true;
908 m.blendfunc1 = GL_SRC_ALPHA;
909 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
913 m.transparent = false;
914 m.blendfunc1 = GL_ONE;
915 m.blendfunc2 = GL_ZERO;
917 m.cr = m.cg = m.cb = (float) (1 << lightscalebit);
918 m.ca = currentrenderentity->alpha;
919 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
920 m.tex[1] = R_GetTexture(surf->lightmaptexture);
921 m.texcoordstep[0] = sizeof(surfvertex_t);
922 m.texcoordstep[1] = sizeof(surfvertex_t);
923 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
925 m.numtriangles = mesh->numtriangles;
926 m.numverts = mesh->numverts;
927 m.index = mesh->index;
928 m.texcoords[0] = &mesh->vertex->st[0];
929 m.texcoords[1] = &mesh->vertex->uv[0];
932 m.color = &svert[0].c[0];
933 m.colorstep = sizeof(surfvert_t);
934 if (softwaretransform_complexity)
936 m.vertex = &svert[0].v[0];
937 m.vertexstep = sizeof(surfvert_t);
938 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
940 softwaretransform(v->v, sv->v);
941 VectorSubtract(sv->v, r_origin, diff);
942 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
943 sv->c[0] = m.cr * ifog;
944 sv->c[1] = m.cg * ifog;
945 sv->c[2] = m.cb * ifog;
951 m.vertex = &mesh->vertex->v[0];
952 m.vertexstep = sizeof(surfvertex_t);
953 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
955 VectorSubtract(v->v, r_origin, diff);
956 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
957 sv->c[0] = m.cr * ifog;
958 sv->c[1] = m.cg * ifog;
959 sv->c[2] = m.cb * ifog;
966 if (softwaretransform_complexity)
968 m.vertex = &svert[0].v[0];
969 m.vertexstep = sizeof(surfvert_t);
970 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
971 softwaretransform(v->v, sv->v);
975 m.vertex = &mesh->vertex->v[0];
976 m.vertexstep = sizeof(surfvertex_t);
983 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
991 memset(&m, 0, sizeof(m));
992 m.transparent = false;
993 m.blendfunc1 = GL_ONE;
994 m.blendfunc2 = GL_ZERO;
995 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
997 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
998 m.texcoordstep[0] = sizeof(surfvertex_t);
999 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1001 m.numtriangles = mesh->numtriangles;
1002 m.numverts = mesh->numverts;
1003 m.index = mesh->index;
1004 m.texcoords[0] = &mesh->vertex->st[0];
1005 if (softwaretransform_complexity)
1007 m.vertex = &svert[0].v[0];
1008 m.vertexstep = sizeof(surfvert_t);
1009 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1010 softwaretransform(v->v, sv->v);
1014 m.vertex = &mesh->vertex->v[0];
1015 m.vertexstep = sizeof(surfvertex_t);
1021 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1024 float diff[3], ifog;
1030 memset(&m, 0, sizeof(m));
1031 m.transparent = false;
1032 m.blendfunc1 = GL_ZERO;
1033 m.blendfunc2 = GL_SRC_COLOR;
1034 m.cr = m.cg = m.cb = (float) (1 << v_overbrightbits.integer);
1036 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1037 m.texcoordstep[0] = sizeof(surfvertex_t);
1038 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1040 m.numtriangles = mesh->numtriangles;
1041 m.numverts = mesh->numverts;
1042 m.index = mesh->index;
1043 m.texcoords[0] = &mesh->vertex->uv[0];
1046 m.color = &svert[0].c[0];
1047 m.colorstep = sizeof(surfvert_t);
1048 if (softwaretransform_complexity)
1050 m.vertex = &svert[0].v[0];
1051 m.vertexstep = sizeof(surfvert_t);
1052 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1054 softwaretransform(v->v, sv->v);
1055 VectorSubtract(sv->v, r_origin, diff);
1056 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1057 sv->c[0] = m.cr * ifog;
1058 sv->c[1] = m.cg * ifog;
1059 sv->c[2] = m.cb * ifog;
1065 m.vertex = &mesh->vertex->v[0];
1066 m.vertexstep = sizeof(surfvertex_t);
1067 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1069 VectorSubtract(v->v, r_origin, diff);
1070 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1071 sv->c[0] = m.cr * ifog;
1072 sv->c[1] = m.cg * ifog;
1073 sv->c[2] = m.cb * ifog;
1080 if (softwaretransform_complexity)
1082 m.vertex = &svert[0].v[0];
1083 m.vertexstep = sizeof(surfvert_t);
1084 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1085 softwaretransform(v->v, sv->v);
1089 m.vertex = &mesh->vertex->v[0];
1090 m.vertexstep = sizeof(surfvertex_t);
1097 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1100 float c[3], base[3], scale, diff[3], ifog;
1107 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1109 base[0] = base[1] = base[2] = r_ambient.value * (1.0f / 128.0f);
1111 memset(&m, 0, sizeof(m));
1112 if (currentrenderentity->effects & EF_ADDITIVE)
1114 m.transparent = true;
1115 m.blendfunc1 = GL_SRC_ALPHA;
1116 m.blendfunc2 = GL_ONE;
1118 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1120 m.transparent = true;
1121 m.blendfunc1 = GL_SRC_ALPHA;
1122 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1126 m.transparent = false;
1127 m.blendfunc1 = GL_ONE;
1128 m.blendfunc2 = GL_ZERO;
1130 m.vertex = &svert[0].v[0];
1131 m.vertexstep = sizeof(surfvert_t);
1132 m.color = &svert[0].c[0];
1133 m.colorstep = sizeof(surfvert_t);
1134 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1135 m.texcoordstep[0] = sizeof(surfvertex_t);
1136 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1138 m.numtriangles = mesh->numtriangles;
1139 m.numverts = mesh->numverts;
1140 m.index = mesh->index;
1141 m.texcoords[0] = &mesh->vertex->st[0];
1142 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1144 softwaretransform(v->v, sv->v);
1145 VectorCopy(base, c);
1146 if (surf->styles[0] != 255)
1148 lm = surf->samples + v->lightmapoffset;
1149 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1150 VectorMA(c, scale, lm, c);
1151 if (surf->styles[1] != 255)
1154 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1155 VectorMA(c, scale, lm, c);
1156 if (surf->styles[2] != 255)
1159 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1160 VectorMA(c, scale, lm, c);
1161 if (surf->styles[3] != 255)
1164 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1165 VectorMA(c, scale, lm, c);
1173 sv->c[3] = currentrenderentity->alpha;
1175 if (surf->dlightframe == r_framecount)
1176 RSurf_Light(surf->dlightbits, m.numverts);
1179 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1181 VectorSubtract(sv->v, r_origin, diff);
1182 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1192 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1195 float diff[3], ifog;
1201 memset(&m, 0, sizeof(m));
1202 if (currentrenderentity->effects & EF_ADDITIVE)
1204 m.transparent = true;
1205 m.blendfunc1 = GL_SRC_ALPHA;
1206 m.blendfunc2 = GL_ONE;
1208 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1210 m.transparent = true;
1211 m.blendfunc1 = GL_SRC_ALPHA;
1212 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1216 m.transparent = false;
1217 m.blendfunc1 = GL_ONE;
1218 m.blendfunc2 = GL_ZERO;
1220 m.vertex = &svert[0].v[0];
1221 m.vertexstep = sizeof(surfvert_t);
1222 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1223 m.texcoordstep[0] = sizeof(surfvertex_t);
1224 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1226 m.numtriangles = mesh->numtriangles;
1227 m.numverts = mesh->numverts;
1228 m.index = mesh->index;
1229 m.texcoords[0] = &mesh->vertex->st[0];
1232 m.color = &svert[0].c[0];
1233 m.colorstep = sizeof(surfvert_t);
1234 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1236 softwaretransform(v->v, sv->v);
1237 VectorSubtract(sv->v, r_origin, diff);
1238 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1242 sv->c[3] = currentrenderentity->alpha;
1247 m.cr = m.cg = m.cb = 1;
1248 m.ca = currentrenderentity->alpha;
1249 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1250 softwaretransform(v->v, sv->v);
1256 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1259 float diff[3], ifog;
1265 memset(&m, 0, sizeof(m));
1266 if (currentrenderentity->effects & EF_ADDITIVE)
1267 m.transparent = true;
1268 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1269 m.transparent = true;
1271 m.transparent = false;
1272 m.blendfunc1 = GL_SRC_ALPHA;
1273 m.blendfunc2 = GL_ONE;
1274 m.vertex = &svert[0].v[0];
1275 m.vertexstep = sizeof(surfvert_t);
1276 m.color = &svert[0].c[0];
1277 m.colorstep = sizeof(surfvert_t);
1278 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1279 m.texcoordstep[0] = sizeof(surfvertex_t);
1280 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1282 m.numtriangles = mesh->numtriangles;
1283 m.numverts = mesh->numverts;
1284 m.index = mesh->index;
1285 m.texcoords[0] = &mesh->vertex->st[0];
1286 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1288 softwaretransform(v->v, sv->v);
1292 sv->c[3] = currentrenderentity->alpha;
1294 if (RSurf_Light(surf->dlightbits, m.numverts))
1298 for (i = 0, sv = svert;i < m.numverts;i++, sv++)
1300 VectorSubtract(sv->v, r_origin, diff);
1301 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1312 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1315 float diff[3], ifog;
1321 memset(&m, 0, sizeof(m));
1322 if (currentrenderentity->effects & EF_ADDITIVE)
1323 m.transparent = true;
1324 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1325 m.transparent = true;
1327 m.transparent = false;
1328 m.blendfunc1 = GL_SRC_ALPHA;
1329 m.blendfunc2 = GL_ONE;
1333 m.ca = currentrenderentity->alpha;
1334 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1335 m.texcoordstep[0] = sizeof(surfvertex_t);
1336 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1338 m.numtriangles = mesh->numtriangles;
1339 m.numverts = mesh->numverts;
1340 m.index = mesh->index;
1341 m.texcoords[0] = &mesh->vertex->st[0];
1344 m.color = &svert[0].c[0];
1345 m.colorstep = sizeof(surfvert_t);
1346 if (softwaretransform_complexity)
1348 m.vertex = &svert[0].v[0];
1349 m.vertexstep = sizeof(surfvert_t);
1350 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1352 softwaretransform(v->v, sv->v);
1353 VectorSubtract(sv->v, r_origin, diff);
1354 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1355 sv->c[0] = m.cr * ifog;
1356 sv->c[1] = m.cg * ifog;
1357 sv->c[2] = m.cb * ifog;
1363 m.vertex = &mesh->vertex->v[0];
1364 m.vertexstep = sizeof(surfvertex_t);
1365 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1367 VectorSubtract(v->v, r_origin, diff);
1368 ifog = 1 - exp(fogdensity/DotProduct(diff, diff));
1369 sv->c[0] = m.cr * ifog;
1370 sv->c[1] = m.cg * ifog;
1371 sv->c[2] = m.cb * ifog;
1378 if (softwaretransform_complexity)
1380 m.vertex = &svert[0].v[0];
1381 m.vertexstep = sizeof(surfvert_t);
1382 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1383 softwaretransform(v->v, sv->v);
1387 m.vertex = &mesh->vertex->v[0];
1388 m.vertexstep = sizeof(surfvertex_t);
1395 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1404 memset(&m, 0, sizeof(m));
1405 if (currentrenderentity->effects & EF_ADDITIVE)
1406 m.transparent = true;
1407 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1408 m.transparent = true;
1410 m.transparent = false;
1411 m.blendfunc1 = GL_SRC_ALPHA;
1412 m.blendfunc2 = GL_ONE;
1413 m.color = &svert[0].c[0];
1414 m.colorstep = sizeof(surfvert_t);
1415 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
1416 m.texcoordstep[0] = sizeof(surfvertex_t);
1417 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1419 m.numtriangles = mesh->numtriangles;
1420 m.numverts = mesh->numverts;
1421 m.index = mesh->index;
1422 m.texcoords[0] = &mesh->vertex->st[0];
1423 if (softwaretransform_complexity)
1425 m.vertex = &svert[0].v[0];
1426 m.vertexstep = sizeof(surfvert_t);
1427 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1429 softwaretransform(v->v, sv->v);
1430 VectorSubtract(sv->v, r_origin, diff);
1431 sv->c[0] = fogcolor[0];
1432 sv->c[1] = fogcolor[1];
1433 sv->c[2] = fogcolor[2];
1434 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1439 m.vertex = &mesh->vertex->v[0];
1440 m.vertexstep = sizeof(surfvertex_t);
1441 for (i = 0, sv = svert, v = mesh->vertex;i < m.numverts;i++, sv++, v++)
1443 VectorSubtract(v->v, r_origin, diff);
1444 sv->c[0] = fogcolor[0];
1445 sv->c[1] = fogcolor[1];
1446 sv->c[2] = fogcolor[2];
1447 sv->c[3] = currentrenderentity->alpha * exp(fogdensity/DotProduct(diff,diff));
1454 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1457 for (surf = firstsurf;surf;surf = surf->chain)
1460 RSurfShader_Wall_Pass_BaseFullbright(surf);
1462 for (surf = firstsurf;surf;surf = surf->chain)
1463 if (surf->currenttexture->glowtexture)
1464 RSurfShader_Wall_Pass_Glow(surf);
1466 for (surf = firstsurf;surf;surf = surf->chain)
1467 RSurfShader_Wall_Pass_Fog(surf);
1470 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1473 for (surf = firstsurf;surf;surf = surf->chain)
1476 RSurfShader_Wall_Pass_BaseVertex(surf);
1478 for (surf = firstsurf;surf;surf = surf->chain)
1479 if (surf->currenttexture->glowtexture)
1480 RSurfShader_Wall_Pass_Glow(surf);
1482 for (surf = firstsurf;surf;surf = surf->chain)
1483 RSurfShader_Wall_Pass_Fog(surf);
1486 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1489 if (r_vertexsurfaces.integer)
1491 for (surf = firstsurf;surf;surf = surf->chain)
1494 RSurfShader_Wall_Pass_BaseVertex(surf);
1496 for (surf = firstsurf;surf;surf = surf->chain)
1497 if (surf->currenttexture->glowtexture)
1498 RSurfShader_Wall_Pass_Glow(surf);
1500 for (surf = firstsurf;surf;surf = surf->chain)
1501 RSurfShader_Wall_Pass_Fog(surf);
1503 else if (r_multitexture.integer)
1505 if (r_dlightmap.integer)
1507 for (surf = firstsurf;surf;surf = surf->chain)
1510 RSurfShader_Wall_Pass_BaseMTex(surf);
1512 for (surf = firstsurf;surf;surf = surf->chain)
1513 if (surf->currenttexture->glowtexture)
1514 RSurfShader_Wall_Pass_Glow(surf);
1516 for (surf = firstsurf;surf;surf = surf->chain)
1517 RSurfShader_Wall_Pass_Fog(surf);
1521 for (surf = firstsurf;surf;surf = surf->chain)
1524 RSurfShader_Wall_Pass_BaseMTex(surf);
1526 for (surf = firstsurf;surf;surf = surf->chain)
1527 if (surf->dlightframe == r_framecount)
1528 RSurfShader_Wall_Pass_Light(surf);
1529 for (surf = firstsurf;surf;surf = surf->chain)
1530 if (surf->currenttexture->glowtexture)
1531 RSurfShader_Wall_Pass_Glow(surf);
1533 for (surf = firstsurf;surf;surf = surf->chain)
1534 RSurfShader_Wall_Pass_Fog(surf);
1537 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1539 for (surf = firstsurf;surf;surf = surf->chain)
1542 RSurfShader_Wall_Pass_BaseVertex(surf);
1544 for (surf = firstsurf;surf;surf = surf->chain)
1545 if (surf->currenttexture->glowtexture)
1546 RSurfShader_Wall_Pass_Glow(surf);
1548 for (surf = firstsurf;surf;surf = surf->chain)
1549 RSurfShader_Wall_Pass_Fog(surf);
1553 if (r_dlightmap.integer)
1555 for (surf = firstsurf;surf;surf = surf->chain)
1558 RSurfShader_Wall_Pass_BaseTexture(surf);
1560 for (surf = firstsurf;surf;surf = surf->chain)
1561 RSurfShader_Wall_Pass_BaseLightmap(surf);
1562 for (surf = firstsurf;surf;surf = surf->chain)
1563 if (surf->currenttexture->glowtexture)
1564 RSurfShader_Wall_Pass_Glow(surf);
1566 for (surf = firstsurf;surf;surf = surf->chain)
1567 RSurfShader_Wall_Pass_Fog(surf);
1571 for (surf = firstsurf;surf;surf = surf->chain)
1574 RSurfShader_Wall_Pass_BaseTexture(surf);
1576 for (surf = firstsurf;surf;surf = surf->chain)
1577 RSurfShader_Wall_Pass_BaseLightmap(surf);
1578 for (surf = firstsurf;surf;surf = surf->chain)
1579 if (surf->dlightframe == r_framecount)
1580 RSurfShader_Wall_Pass_Light(surf);
1581 for (surf = firstsurf;surf;surf = surf->chain)
1582 if (surf->currenttexture->glowtexture)
1583 RSurfShader_Wall_Pass_Glow(surf);
1585 for (surf = firstsurf;surf;surf = surf->chain)
1586 RSurfShader_Wall_Pass_Fog(surf);
1592 =============================================================
1596 =============================================================
1599 static void RSurf_Callback(void *data, void *junk)
1601 ((msurface_t *)data)->visframe = r_framecount;
1604 static void R_SolidWorldNode (void)
1606 if (r_viewleaf->contents != CONTENTS_SOLID)
1609 mportal_t *p, *pstack[8192];
1610 msurface_t *surf, **mark, **endmark;
1613 // LordHavoc: portal-passage worldnode; follows portals leading
1614 // outward from viewleaf, if a portal leads offscreen it is not
1615 // followed, in indoor maps this can often cull a great deal of
1616 // geometry away when pvs data is not present (useful with pvs as well)
1619 leaf->worldnodeframe = r_framecount;
1624 leaf->visframe = r_framecount;
1626 if (leaf->nummarksurfaces)
1628 mark = leaf->firstmarksurface;
1629 endmark = mark + leaf->nummarksurfaces;
1635 // make sure surfaces are only processed once
1636 if (surf->worldnodeframe == r_framecount)
1638 surf->worldnodeframe = r_framecount;
1639 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1641 if (surf->flags & SURF_PLANEBACK)
1643 VectorNegate(surf->plane->normal, plane.normal);
1644 plane.dist = -surf->plane->dist;
1645 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1650 if (!(surf->flags & SURF_PLANEBACK))
1651 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1654 while (mark < endmark);
1661 // make sure surfaces are only processed once
1662 if (surf->worldnodeframe == r_framecount)
1664 surf->worldnodeframe = r_framecount;
1665 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1667 if (surf->flags & SURF_PLANEBACK)
1668 surf->visframe = r_framecount;
1672 if (!(surf->flags & SURF_PLANEBACK))
1673 surf->visframe = r_framecount;
1676 while (mark < endmark);
1680 // follow portals into other leafs
1682 for (;p;p = p->next)
1684 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1687 if (leaf->worldnodeframe != r_framecount)
1689 leaf->worldnodeframe = r_framecount;
1690 if (leaf->contents != CONTENTS_SOLID)
1692 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1694 p->visframe = r_framecount;
1695 pstack[portalstack++] = p;
1699 p = pstack[--portalstack];
1711 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1712 int nodestackpos = 0;
1713 // LordHavoc: recursive descending worldnode; if portals are not
1714 // available, this is a good last resort, can cull large amounts of
1715 // geometry, but is more time consuming than portal-passage and renders
1716 // things behind walls
1719 if (R_NotCulledBox(node->mins, node->maxs))
1721 if (node->numsurfaces)
1725 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1727 if (PlaneDiff (r_origin, node->plane) < 0)
1729 for (;surf < surfend;surf++)
1731 if (surf->flags & SURF_PLANEBACK)
1733 VectorNegate(surf->plane->normal, plane.normal);
1734 plane.dist = -surf->plane->dist;
1735 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, &plane);
1741 for (;surf < surfend;surf++)
1743 if (!(surf->flags & SURF_PLANEBACK))
1744 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), surf->flags & SURF_CLIPSOLID, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1750 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1751 if (PlaneDiff (r_origin, node->plane) < 0)
1753 for (;surf < surfend;surf++)
1755 if (surf->flags & SURF_PLANEBACK)
1756 surf->visframe = r_framecount;
1761 for (;surf < surfend;surf++)
1763 if (!(surf->flags & SURF_PLANEBACK))
1764 surf->visframe = r_framecount;
1770 // recurse down the children
1771 if (node->children[0]->contents >= 0)
1773 if (node->children[1]->contents >= 0)
1775 if (nodestackpos < 8192)
1776 nodestack[nodestackpos++] = node->children[1];
1777 node = node->children[0];
1781 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1782 node = node->children[0];
1787 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1788 if (node->children[1]->contents >= 0)
1790 node = node->children[1];
1793 else if (nodestackpos > 0)
1795 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1796 node = nodestack[--nodestackpos];
1801 else if (nodestackpos > 0)
1803 node = nodestack[--nodestackpos];
1809 static int r_portalframecount = 0;
1811 static void R_PVSWorldNode()
1814 mportal_t *p, *pstack[8192];
1815 msurface_t *surf, **mark, **endmark;
1820 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1823 leaf->worldnodeframe = r_framecount;
1828 leaf->visframe = r_framecount;
1830 if (leaf->nummarksurfaces)
1832 mark = leaf->firstmarksurface;
1833 endmark = mark + leaf->nummarksurfaces;
1839 // make sure surfaces are only processed once
1840 if (surf->worldnodeframe == r_framecount)
1842 surf->worldnodeframe = r_framecount;
1843 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1845 if (surf->flags & SURF_PLANEBACK)
1847 VectorNegate(surf->plane->normal, plane.normal);
1848 plane.dist = -surf->plane->dist;
1849 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, &plane);
1854 if (!(surf->flags & SURF_PLANEBACK))
1855 R_Clip_AddPolygon((float *)surf->poly_verts, surf->poly_numverts, sizeof(float[3]), (surf->flags & SURF_CLIPSOLID) != 0, RSurf_Callback, surf, NULL, (tinyplane_t *)surf->plane);
1858 while (mark < endmark);
1865 // make sure surfaces are only processed once
1866 if (surf->worldnodeframe == r_framecount)
1868 surf->worldnodeframe = r_framecount;
1869 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1871 if (surf->flags & SURF_PLANEBACK)
1872 surf->visframe = r_framecount;
1876 if (!(surf->flags & SURF_PLANEBACK))
1877 surf->visframe = r_framecount;
1880 while (mark < endmark);
1884 // follow portals into other leafs
1885 for (p = leaf->portals;p;p = p->next)
1887 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1890 if (leaf->worldnodeframe != r_framecount)
1892 leaf->worldnodeframe = r_framecount;
1893 if (leaf->contents != CONTENTS_SOLID)
1895 i = (leaf - cl.worldmodel->leafs) - 1;
1896 if (worldvis[i>>3] & (1<<(i&7)))
1898 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1900 pstack[portalstack++] = p;
1904 p = pstack[--portalstack];
1916 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1917 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1918 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1919 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1920 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1922 int Cshader_count = 5;
1923 Cshader_t *Cshaders[5] =
1925 &Cshader_wall_vertex,
1926 &Cshader_wall_lightmap,
1927 &Cshader_wall_fullbright,
1932 void R_PrepareSurfaces(void)
1934 int i, alttextures, texframe, framecount;
1939 for (i = 0;i < Cshader_count;i++)
1940 Cshaders[i]->chain = NULL;
1942 model = currentrenderentity->model;
1943 alttextures = currentrenderentity->frame != 0;
1944 texframe = (int)(cl.time * 5.0f);
1946 for (i = 0;i < model->nummodelsurfaces;i++)
1948 surf = model->modelsortedsurfaces[i];
1949 if (surf->visframe == r_framecount)
1951 if (surf->insertframe != r_framecount)
1953 surf->insertframe = r_framecount;
1955 t = surf->texinfo->texture;
1958 framecount = t->anim_total[alttextures];
1959 if (framecount >= 2)
1960 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1962 surf->currenttexture = t->anim_frames[alttextures][0];
1965 surf->currenttexture = t;
1968 surf->chain = surf->shader->chain;
1969 surf->shader->chain = surf;
1974 void R_DrawSurfaces (int type)
1979 for (i = 0;i < Cshader_count;i++)
1981 shader = Cshaders[i];
1982 if (shader->chain && shader->shaderfunc[type])
1983 shader->shaderfunc[type](shader->chain);
1987 static float portalpointbuffer[256][3];
1989 void R_DrawPortals(void)
1992 mportal_t *portal, *endportal;
1995 drawportals = r_drawportals.integer;
1997 if (drawportals < 1)
2000 memset(&m, 0, sizeof(m));
2001 m.transparent = true;
2002 m.blendfunc1 = GL_SRC_ALPHA;
2003 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2004 m.vertex = &portalpointbuffer[0][0];
2005 m.vertexstep = sizeof(float[3]);
2007 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2009 if (portal->visframe == r_portalframecount)
2011 if (portal->numpoints <= 256)
2013 i = portal - cl.worldmodel->portals;
2014 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2015 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2016 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2017 point = portal->points;
2018 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2020 for (i = portal->numpoints - 1;i >= 0;i--)
2021 VectorCopy(point[i].position, portalpointbuffer[i]);
2025 for (i = 0;i < portal->numpoints;i++)
2026 VectorCopy(point[i].position, portalpointbuffer[i]);
2028 R_Mesh_DrawPolygon(&m, portal->numpoints);
2034 void R_SetupForBModelRendering(void)
2041 // because bmodels can be reused, we have to decide which things to render
2042 // from scratch every time
2044 model = currentrenderentity->model;
2046 softwaretransformforentity (currentrenderentity);
2047 softwareuntransform(r_origin, modelorg);
2049 for (i = 0;i < model->nummodelsurfaces;i++)
2051 surf = model->modelsortedsurfaces[i];
2052 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2053 surf->visframe = r_framecount;
2055 surf->visframe = -1;
2056 surf->worldnodeframe = -1;
2057 surf->lightframe = -1;
2058 surf->dlightframe = -1;
2059 surf->insertframe = -1;
2063 void R_SetupForWorldRendering(void)
2065 // there is only one instance of the world, but it can be rendered in
2068 currentrenderentity = &cl_entities[0].render;
2069 softwaretransformidentity();
2072 static void R_SurfMarkLights (void)
2077 if (r_dynamic.integer)
2080 if (!r_vertexsurfaces.integer)
2082 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2084 surf = currentrenderentity->model->modelsortedsurfaces[i];
2085 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2087 if (surf->cached_dlight
2088 || surf->cached_ambient != r_ambient.value
2089 || surf->cached_lightscalebit != lightscalebit)
2090 R_BuildLightMap(surf, false); // base lighting changed
2091 else if (r_dynamic.integer)
2093 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2094 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2095 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2096 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2097 R_BuildLightMap(surf, false); // base lighting changed
2098 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2099 R_BuildLightMap(surf, true); // only dlights
2106 void R_MarkWorldLights(void)
2108 R_SetupForWorldRendering();
2117 void R_DrawWorld (void)
2119 R_SetupForWorldRendering();
2121 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2122 R_SolidWorldNode ();
2132 void R_DrawBrushModelSky (void)
2134 R_SetupForBModelRendering();
2136 R_PrepareSurfaces();
2137 R_DrawSurfaces(SHADERSTAGE_SKY);
2140 void R_DrawBrushModelNormal (void)
2144 // have to flush queue because of possible lightmap reuse
2147 R_SetupForBModelRendering();
2151 R_PrepareSurfaces();
2153 if (!skyrendermasked)
2154 R_DrawSurfaces(SHADERSTAGE_SKY);
2155 R_DrawSurfaces(SHADERSTAGE_NORMAL);