2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 #define MAX_LIGHTMAP_SIZE 256
26 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31 cvar_t r_ambient = {0, "r_ambient", "0"};
32 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
33 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
38 static int dlightdivtable[32768];
40 static int R_IntAddDynamicLights (msurface_t *surf)
42 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
44 float dist, impact[3], local[3];
46 // LordHavoc: use 64bit integer... shame it's not very standardized...
47 #if _MSC_VER || __BORLANDC__
55 smax = (surf->extents[0] >> 4) + 1;
56 tmax = (surf->extents[1] >> 4) + 1;
59 for (lnum = 0; lnum < r_numdlights; lnum++)
61 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
62 continue; // not lit by this light
64 softwareuntransform(r_dlight[lnum].origin, local);
65 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
67 // for comparisons to minimum acceptable light
68 // compensate for LIGHTOFFSET
69 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
76 if (surf->plane->type < 3)
78 VectorCopy(local, impact);
79 impact[surf->plane->type] -= dist;
83 impact[0] = local[0] - surf->plane->normal[0] * dist;
84 impact[1] = local[1] - surf->plane->normal[1] * dist;
85 impact[2] = local[2] - surf->plane->normal[2] * dist;
88 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
89 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
91 s = bound(0, impacts, smax * 16) - impacts;
92 t = bound(0, impactt, tmax * 16) - impactt;
93 i = s * s + t * t + dist2;
97 // reduce calculations
98 for (s = 0, i = impacts; s < smax; s++, i -= 16)
99 sdtable[s] = i * i + dist2;
101 maxdist3 = maxdist - dist2;
103 // convert to 8.8 blocklights format
104 red = r_dlight[lnum].light[0];
105 green = r_dlight[lnum].light[1];
106 blue = r_dlight[lnum].light[2];
107 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
111 for (t = 0;t < tmax;t++, i -= 16)
114 // make sure some part of it is visible on this line
117 maxdist2 = maxdist - td;
118 for (s = 0;s < smax;s++)
120 if (sdtable[s] < maxdist2)
122 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
125 bl[0] += (red * k) >> 7;
126 bl[1] += (green * k) >> 7;
127 bl[2] += (blue * k) >> 7;
141 static int R_FloatAddDynamicLights (msurface_t *surf)
143 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
144 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
148 smax = (surf->extents[0] >> 4) + 1;
149 tmax = (surf->extents[1] >> 4) + 1;
152 for (lnum = 0; lnum < r_numdlights; lnum++)
154 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155 continue; // not lit by this light
157 softwareuntransform(r_dlight[lnum].origin, local);
158 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
160 // for comparisons to minimum acceptable light
161 // compensate for LIGHTOFFSET
162 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
165 dist2 += LIGHTOFFSET;
166 if (dist2 >= maxdist)
169 if (surf->plane->type < 3)
171 VectorCopy(local, impact);
172 impact[surf->plane->type] -= dist;
176 impact[0] = local[0] - surf->plane->normal[0] * dist;
177 impact[1] = local[1] - surf->plane->normal[1] * dist;
178 impact[2] = local[2] - surf->plane->normal[2] * dist;
181 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
184 td = bound(0, impacts, smax * 16) - impacts;
185 td1 = bound(0, impactt, tmax * 16) - impactt;
186 td = td * td + td1 * td1 + dist2;
190 // reduce calculations
191 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192 sdtable[s] = td1 * td1 + dist2;
194 maxdist3 = maxdist - dist2;
196 // convert to 8.8 blocklights format
197 red = r_dlight[lnum].light[0];
198 green = r_dlight[lnum].light[1];
199 blue = r_dlight[lnum].light[2];
200 subtract = r_dlight[lnum].subtract * 32768.0f;
201 bl = floatblocklights;
204 for (t = 0;t < tmax;t++, td1 -= 16.0f)
207 // make sure some part of it is visible on this line
210 maxdist2 = maxdist - td;
211 for (s = 0;s < smax;s++)
213 if (sdtable[s] < maxdist2)
215 k = (32768.0f / (sdtable[s] + td)) - subtract;
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
238 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
240 if (!r_floatbuildlightmap.integer)
242 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
243 unsigned int *bl, scale;
244 qbyte *lightmap, *out, *stain;
246 // update cached lighting info
247 surf->cached_dlight = 0;
248 surf->cached_lightscalebit = lightscalebit;
249 surf->cached_ambient = r_ambient.value;
250 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
251 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
252 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
253 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
255 smax = (surf->extents[0]>>4)+1;
256 tmax = (surf->extents[1]>>4)+1;
259 lightmap = surf->samples;
261 // set to full bright if no light data
263 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
265 for (i = 0;i < size3;i++)
271 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
274 for (i = 0;i < size3;i++)
278 memset(bl, 0, size*3*sizeof(unsigned int));
280 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
282 surf->cached_dlight = R_IntAddDynamicLights(surf);
283 if (surf->cached_dlight)
285 else if (dlightchanged)
286 return; // don't upload if only updating dlights and none mattered
289 // add all the lightmaps
293 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
294 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
295 bl[i] += lightmap[i] * scale;
299 stain = surf->stainsamples;
302 // deal with lightmap brightness scale
303 shift = 7 + lightscalebit + 8;
304 if (currentrenderentity->model->lightmaprgba)
306 stride = (surf->lightmaptexturestride - smax) * 4;
307 for (i = 0;i < tmax;i++, out += stride)
309 for (j = 0;j < smax;j++)
311 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 stride = (surf->lightmaptexturestride - smax) * 3;
321 for (i = 0;i < tmax;i++, out += stride)
323 for (j = 0;j < smax;j++)
325 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
332 R_UpdateTexture(surf->lightmaptexture, templight);
336 int smax, tmax, i, j, size, size3, maps, stride, l;
338 qbyte *lightmap, *out, *stain;
340 // update cached lighting info
341 surf->cached_dlight = 0;
342 surf->cached_lightscalebit = lightscalebit;
343 surf->cached_ambient = r_ambient.value;
344 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
345 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
346 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
347 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
349 smax = (surf->extents[0]>>4)+1;
350 tmax = (surf->extents[1]>>4)+1;
353 lightmap = surf->samples;
355 // set to full bright if no light data
356 bl = floatblocklights;
357 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
360 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
365 for (i = 0;i < size3;i++)
369 memset(bl, 0, size*3*sizeof(float));
371 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
373 surf->cached_dlight = R_FloatAddDynamicLights(surf);
374 if (surf->cached_dlight)
376 else if (dlightchanged)
377 return; // don't upload if only updating dlights and none mattered
380 // add all the lightmaps
383 bl = floatblocklights;
384 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
385 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
386 bl[i] += lightmap[i] * scale;
389 stain = surf->stainsamples;
390 bl = floatblocklights;
392 // deal with lightmap brightness scale
393 scale = 1.0f / (1 << (7 + lightscalebit + 8));
394 if (currentrenderentity->model->lightmaprgba)
396 stride = (surf->lightmaptexturestride - smax) * 4;
397 for (i = 0;i < tmax;i++, out += stride)
399 for (j = 0;j < smax;j++)
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 stride = (surf->lightmaptexturestride - smax) * 3;
411 for (i = 0;i < tmax;i++, out += stride)
413 for (j = 0;j < smax;j++)
415 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
422 R_UpdateTexture(surf->lightmaptexture, templight);
426 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
429 msurface_t *surf, *endsurf;
430 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
433 // LordHavoc: use 64bit integer... shame it's not very standardized...
434 #if _MSC_VER || __BORLANDC__
441 // for comparisons to minimum acceptable light
442 // compensate for 4096 offset
443 maxdist = radius * radius + 4096;
445 // clamp radius to avoid exceeding 32768 entry division table
446 if (maxdist > 4194304)
449 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
452 if (node->contents < 0)
454 ndist = PlaneDiff(origin, node->plane);
457 node = node->children[0];
462 node = node->children[1];
466 dist2 = ndist * ndist;
470 maxdist3 = maxdist - dist2;
472 if (node->plane->type < 3)
474 VectorCopy(origin, impact);
475 impact[node->plane->type] -= ndist;
479 impact[0] = origin[0] - node->plane->normal[0] * ndist;
480 impact[1] = origin[1] - node->plane->normal[1] * ndist;
481 impact[2] = origin[2] - node->plane->normal[2] * ndist;
484 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
486 if (surf->stainsamples)
488 smax = (surf->extents[0] >> 4) + 1;
489 tmax = (surf->extents[1] >> 4) + 1;
491 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
492 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
494 s = bound(0, impacts, smax * 16) - impacts;
495 t = bound(0, impactt, tmax * 16) - impactt;
496 i = s * s + t * t + dist2;
500 // reduce calculations
501 for (s = 0, i = impacts; s < smax; s++, i -= 16)
502 sdtable[s] = i * i + dist2;
504 // convert to 8.8 blocklights format
505 bl = surf->stainsamples;
510 for (t = 0;t < tmax;t++, i -= 16)
513 // make sure some part of it is visible on this line
516 maxdist2 = maxdist - td;
517 for (s = 0;s < smax;s++)
519 if (sdtable[s] < maxdist2)
521 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
524 ratio = rand() & 255;
525 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
529 a = bound(0, a, 256);
530 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
531 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
532 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
533 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
534 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
535 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
546 // force lightmap upload
548 surf->cached_dlight = true;
553 if (node->children[0]->contents >= 0)
555 if (node->children[1]->contents >= 0)
557 R_StainNode(node->children[0], model, origin, radius, icolor);
558 node = node->children[1];
563 node = node->children[0];
567 else if (node->children[1]->contents >= 0)
569 node = node->children[1];
574 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
577 entity_render_t *ent;
589 model = cl.worldmodel;
590 softwaretransformidentity();
591 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
593 // look for embedded bmodels
594 for (n = 0;n < cl_num_brushmodel_entities;n++)
596 ent = cl_brushmodel_entities[n];
598 if (model && model->name[0] == '*')
600 Mod_CheckLoaded(model);
601 if (model->type == mod_brush)
603 softwaretransformforentity(ent);
604 softwareuntransform(origin, org);
605 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
613 =============================================================
617 =============================================================
620 static void RSurfShader_Sky(msurface_t *firstsurf)
627 float cr, cg, cb, ca;
630 // LordHavoc: HalfLife maps have freaky skypolys...
631 if (currentrenderentity->model->ishlbsp)
636 skyrendernow = false;
640 for (surf = firstsurf;surf;surf = surf->chain)
642 // draw depth-only polys
643 memset(&m, 0, sizeof(m));
644 m.transparent = false;
647 m.blendfunc1 = GL_ZERO;
648 m.blendfunc2 = GL_ONE;
653 m.blendfunc1 = GL_ONE;
654 m.blendfunc2 = GL_ZERO;
655 m.depthwrite = false;
657 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
659 m.numtriangles = mesh->numtriangles;
660 m.numverts = mesh->numverts;
661 if (R_Mesh_Draw_GetBuffer(&m))
663 cr = fogcolor[0] * m.colorscale;
664 cg = fogcolor[1] * m.colorscale;
665 cb = fogcolor[2] * m.colorscale;
667 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
668 for (i = 0, v = mesh->vertex, outv = m.vertex;i < m.numverts;i++, v++, outv += 4, outc += 4)
670 softwaretransform(v->v, outv);
672 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
684 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
687 int i, l, lit = false;
690 for (l = 0;l < r_numdlights;l++)
692 if (dlightbits[l >> 5] & (1 << (l & 31)))
695 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
696 VectorCopy(rd->origin, lightorigin);
697 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
699 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
700 if (f < rd->cullradius2)
702 f = (1.0f / f) - rd->subtract;
703 VectorMA(c, f, rd->light, c);
712 // note: this untransforms lights to do the checking,
713 // and takes surf->mesh->vertex data
714 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
720 for (l = 0;l < r_numdlights;l++)
722 if (dlightbits[l >> 5] & (1 << (l & 31)))
725 softwareuntransform(rd->origin, lightorigin);
726 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
727 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
734 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
738 float *outv, *outc, *outst, cl, diff[3];
739 float base[3], scale, f;
743 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
744 memset(&m, 0, sizeof(m));
745 if (currentrenderentity->effects & EF_ADDITIVE)
747 m.transparent = true;
748 m.blendfunc1 = GL_SRC_ALPHA;
749 m.blendfunc2 = GL_ONE;
751 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
753 m.transparent = true;
754 m.blendfunc1 = GL_SRC_ALPHA;
755 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
759 m.transparent = false;
760 m.blendfunc1 = GL_ONE;
761 m.blendfunc2 = GL_ZERO;
763 m.depthwrite = false;
764 m.depthdisable = false;
765 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
766 if (surf->flags & SURF_DRAWFULLBRIGHT || currentrenderentity->effects & EF_FULLBRIGHT)
768 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
770 m.numtriangles = mesh->numtriangles;
771 m.numverts = mesh->numverts;
772 if (R_Mesh_Draw_GetBuffer(&m))
774 base[0] = base[1] = base[2] = 1.0f * m.colorscale;
775 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
776 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)
778 softwaretransform(v->v, outv);
780 VectorCopy(base, outc);
786 VectorSubtract(outv, r_origin, diff);
787 f = 1 - exp(fogdensity/DotProduct(diff, diff));
788 VectorScale(outc, f, outc);
796 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
797 base[0] = base[1] = base[2] = (r_ambient.value * (1.0f / 64.0f) + ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f));
798 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
800 m.numtriangles = mesh->numtriangles;
801 m.numverts = mesh->numverts;
802 if (R_Mesh_Draw_GetBuffer(&m))
805 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
806 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)
808 softwaretransform(v->v, outv);
810 VectorCopy(base, outc);
815 if (surf->dlightframe == r_framecount)
816 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
817 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
819 if (surf->flags & SURF_LIGHTMAP)
820 if (surf->styles[0] != 255)
822 lm = surf->samples + v->lightmapoffset;
823 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
824 VectorMA(outc, scale, lm, outc);
825 if (surf->styles[1] != 255)
828 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
829 VectorMA(outc, scale, lm, outc);
830 if (surf->styles[2] != 255)
833 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
834 VectorMA(outc, scale, lm, outc);
835 if (surf->styles[3] != 255)
838 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
839 VectorMA(outc, scale, lm, outc);
846 VectorSubtract(outv, r_origin, diff);
847 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
848 VectorScale(outc, f, outc);
851 VectorScale(outc, cl, outc);
858 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
862 float *outv, *outc, *outst, diff[3];
866 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
867 memset(&m, 0, sizeof(m));
868 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || alpha < 1;
869 m.blendfunc1 = GL_SRC_ALPHA;
870 m.blendfunc2 = GL_ONE;
871 m.depthwrite = false;
872 m.depthdisable = false;
873 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
874 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
876 m.numtriangles = mesh->numtriangles;
877 m.numverts = mesh->numverts;
878 if (R_Mesh_Draw_GetBuffer(&m))
880 VectorScale(fogcolor, m.colorscale, base);
881 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
882 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
884 softwaretransform(v->v, outv);
886 VectorSubtract(outv, r_origin, diff);
887 f = exp(fogdensity/DotProduct(diff, diff));
888 VectorScale(base, f, outc);
893 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
903 static void RSurfShader_Water(msurface_t *firstsurf)
906 for (surf = firstsurf;surf;surf = surf->chain)
907 RSurfShader_Water_Pass_Base(surf);
909 for (surf = firstsurf;surf;surf = surf->chain)
910 RSurfShader_Water_Pass_Fog(surf);
913 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
917 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
920 memset(&m, 0, sizeof(m));
921 if (currentrenderentity->effects & EF_ADDITIVE)
923 m.transparent = true;
924 m.blendfunc1 = GL_SRC_ALPHA;
925 m.blendfunc2 = GL_ONE;
927 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
929 m.transparent = true;
930 m.blendfunc1 = GL_SRC_ALPHA;
931 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
935 m.transparent = false;
936 m.blendfunc1 = GL_ONE;
937 m.blendfunc2 = GL_ZERO;
939 m.depthwrite = false;
940 m.depthdisable = false;
941 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
942 m.tex[1] = R_GetTexture(surf->lightmaptexture);
943 ca = currentrenderentity->alpha;
944 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
946 m.numtriangles = mesh->numtriangles;
947 m.numverts = mesh->numverts;
949 if (R_Mesh_Draw_GetBuffer(&m))
951 cl = (float) (1 << lightscalebit) * m.colorscale;
952 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
955 if (softwaretransform_complexity)
957 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)
959 softwaretransform(v->v, outv);
961 VectorSubtract(outv, r_origin, diff);
962 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
972 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)
974 VectorCopy(v->v, outv);
976 VectorSubtract(outv, r_origin, diff);
977 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
988 if (softwaretransform_complexity)
990 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)
992 softwaretransform(v->v, outv);
994 outc[0] = outc[1] = outc[2] = cl;
1004 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)
1006 VectorCopy(v->v, outv);
1008 outc[0] = outc[1] = outc[2] = cl;
1010 outst[0] = v->st[0];
1011 outst[1] = v->st[1];
1012 outuv[0] = v->uv[0];
1013 outuv[1] = v->uv[1];
1021 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1025 float *outv, *outc, *outst, cl, ca;
1027 rmeshbufferinfo_t m;
1028 memset(&m, 0, sizeof(m));
1029 m.transparent = false;
1030 m.blendfunc1 = GL_ONE;
1031 m.blendfunc2 = GL_ZERO;
1032 m.depthwrite = false;
1033 m.depthdisable = false;
1034 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1035 ca = currentrenderentity->alpha;
1036 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1038 m.numtriangles = mesh->numtriangles;
1039 m.numverts = mesh->numverts;
1041 if (R_Mesh_Draw_GetBuffer(&m))
1043 cl = (float) (1 << lightscalebit) * m.colorscale;
1044 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1045 if (softwaretransform_complexity)
1047 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)
1049 softwaretransform(v->v, outv);
1051 outc[0] = outc[1] = outc[2] = cl;
1053 outst[0] = v->st[0];
1054 outst[1] = v->st[1];
1059 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)
1061 VectorCopy(v->v, outv);
1063 outc[0] = outc[1] = outc[2] = cl;
1065 outst[0] = v->st[0];
1066 outst[1] = v->st[1];
1073 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1077 float *outv, *outc, *outuv, cl, ca, diff[3];
1079 rmeshbufferinfo_t m;
1080 memset(&m, 0, sizeof(m));
1081 m.transparent = false;
1082 m.blendfunc1 = GL_ZERO;
1083 m.blendfunc2 = GL_SRC_COLOR;
1084 m.depthwrite = false;
1085 m.depthdisable = false;
1086 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1087 ca = currentrenderentity->alpha;
1088 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1090 m.numtriangles = mesh->numtriangles;
1091 m.numverts = mesh->numverts;
1093 if (R_Mesh_Draw_GetBuffer(&m))
1095 cl = (float) (1 << lightscalebit) * m.colorscale;
1096 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1099 if (softwaretransform_complexity)
1101 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)
1103 softwaretransform(v->v, outv);
1105 VectorSubtract(outv, r_origin, diff);
1106 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1108 outuv[0] = v->uv[0];
1109 outuv[1] = v->uv[1];
1114 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)
1116 VectorCopy(v->v, outv);
1118 VectorSubtract(outv, r_origin, diff);
1119 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1121 outuv[0] = v->uv[0];
1122 outuv[1] = v->uv[1];
1128 if (softwaretransform_complexity)
1130 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)
1132 softwaretransform(v->v, outv);
1134 outc[0] = outc[1] = outc[2] = cl;
1136 outuv[0] = v->uv[0];
1137 outuv[1] = v->uv[1];
1142 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)
1144 VectorCopy(v->v, outv);
1146 outc[0] = outc[1] = outc[2] = cl;
1148 outuv[0] = v->uv[0];
1149 outuv[1] = v->uv[1];
1157 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1161 float *outv, *outc, *outst, cl, ca, diff[3];
1162 float base[3], scale, f;
1165 rmeshbufferinfo_t m;
1166 memset(&m, 0, sizeof(m));
1167 if (currentrenderentity->effects & EF_ADDITIVE)
1169 m.transparent = true;
1170 m.blendfunc1 = GL_SRC_ALPHA;
1171 m.blendfunc2 = GL_ONE;
1173 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1175 m.transparent = true;
1176 m.blendfunc1 = GL_SRC_ALPHA;
1177 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1181 m.transparent = false;
1182 m.blendfunc1 = GL_ONE;
1183 m.blendfunc2 = GL_ZERO;
1185 m.depthwrite = false;
1186 m.depthdisable = false;
1187 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1189 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1191 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1193 ca = currentrenderentity->alpha;
1194 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1196 m.numtriangles = mesh->numtriangles;
1197 m.numverts = mesh->numverts;
1199 if (R_Mesh_Draw_GetBuffer(&m))
1202 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1204 if (currentrenderentity->effects & EF_FULLBRIGHT)
1206 if (softwaretransform_complexity)
1208 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)
1210 softwaretransform(v->v, outv);
1212 VectorSubtract(outv, r_origin, diff);
1213 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1215 outst[0] = v->st[0];
1216 outst[1] = v->st[1];
1221 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)
1223 VectorCopy(v->v, outv);
1225 VectorSubtract(outv, r_origin, diff);
1226 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1228 outst[0] = v->st[0];
1229 outst[1] = v->st[1];
1235 if (softwaretransform_complexity)
1237 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)
1239 softwaretransform(v->v, outv);
1241 VectorCopy(base, outc);
1243 outst[0] = v->st[0];
1244 outst[1] = v->st[1];
1249 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)
1251 VectorCopy(v->v, outv);
1253 VectorCopy(base, outc);
1255 outst[0] = v->st[0];
1256 outst[1] = v->st[1];
1260 if (surf->dlightframe == r_framecount)
1261 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1263 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1265 if (surf->styles[0] != 255)
1267 lm = surf->samples + v->lightmapoffset;
1268 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1269 VectorMA(outc, scale, lm, outc);
1270 if (surf->styles[1] != 255)
1273 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1274 VectorMA(outc, scale, lm, outc);
1275 if (surf->styles[2] != 255)
1278 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1279 VectorMA(outc, scale, lm, outc);
1280 if (surf->styles[3] != 255)
1283 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1284 VectorMA(outc, scale, lm, outc);
1291 VectorSubtract(outv, r_origin, diff);
1292 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1293 VectorScale(outc, f, outc);
1296 VectorScale(outc, cl, outc);
1303 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1307 float *outv, *outc, *outst, cl, ca, diff[3];
1309 rmeshbufferinfo_t m;
1310 memset(&m, 0, sizeof(m));
1311 if (currentrenderentity->effects & EF_ADDITIVE)
1313 m.transparent = true;
1314 m.blendfunc1 = GL_SRC_ALPHA;
1315 m.blendfunc2 = GL_ONE;
1317 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1319 m.transparent = true;
1320 m.blendfunc1 = GL_SRC_ALPHA;
1321 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1325 m.transparent = false;
1326 m.blendfunc1 = GL_ONE;
1327 m.blendfunc2 = GL_ZERO;
1329 m.depthwrite = false;
1330 m.depthdisable = false;
1331 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1332 ca = currentrenderentity->alpha;
1333 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1335 m.numtriangles = mesh->numtriangles;
1336 m.numverts = mesh->numverts;
1338 if (R_Mesh_Draw_GetBuffer(&m))
1341 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1344 if (softwaretransform_complexity)
1346 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)
1348 softwaretransform(v->v, outv);
1350 VectorSubtract(outv, r_origin, diff);
1351 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1353 outst[0] = v->st[0];
1354 outst[1] = v->st[1];
1359 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)
1361 VectorCopy(v->v, outv);
1363 VectorSubtract(outv, r_origin, diff);
1364 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1366 outst[0] = v->st[0];
1367 outst[1] = v->st[1];
1373 if (softwaretransform_complexity)
1375 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)
1377 softwaretransform(v->v, outv);
1379 outc[0] = outc[1] = outc[2] = cl;
1381 outst[0] = v->st[0];
1382 outst[1] = v->st[1];
1387 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)
1389 VectorCopy(v->v, outv);
1391 outc[0] = outc[1] = outc[2] = cl;
1393 outst[0] = v->st[0];
1394 outst[1] = v->st[1];
1402 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1406 float *outv, *outc, *outst, cl, ca, diff[3], f;
1408 rmeshbufferinfo_t m;
1410 if (surf->dlightframe != r_framecount)
1412 if (currentrenderentity->effects & EF_FULLBRIGHT)
1415 memset(&m, 0, sizeof(m));
1416 if (currentrenderentity->effects & EF_ADDITIVE)
1418 m.transparent = true;
1419 m.blendfunc1 = GL_SRC_ALPHA;
1420 m.blendfunc2 = GL_ONE;
1422 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1424 m.transparent = true;
1425 m.blendfunc1 = GL_SRC_ALPHA;
1426 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1430 m.transparent = false;
1431 m.blendfunc1 = GL_ONE;
1432 m.blendfunc2 = GL_ZERO;
1434 m.depthwrite = false;
1435 m.depthdisable = false;
1436 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1437 ca = currentrenderentity->alpha;
1438 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1440 if (RSurf_LightCheck(surf->dlightbits, mesh))
1442 m.numtriangles = mesh->numtriangles;
1443 m.numverts = mesh->numverts;
1445 if (R_Mesh_Draw_GetBuffer(&m))
1448 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1449 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)
1451 softwaretransform(v->v, outv);
1455 outst[0] = v->st[0];
1456 outst[1] = v->st[1];
1458 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1461 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
1463 VectorSubtract(outv, r_origin, diff);
1464 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1465 VectorScale(outc, f, outc);
1469 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1470 VectorScale(outc, cl, outc);
1476 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1480 float *outv, *outc, *outst, cl, ca, diff[3];
1482 rmeshbufferinfo_t m;
1483 memset(&m, 0, sizeof(m));
1484 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1485 m.blendfunc1 = GL_SRC_ALPHA;
1486 m.blendfunc2 = GL_ONE;
1487 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1488 ca = currentrenderentity->alpha;
1489 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1491 m.numtriangles = mesh->numtriangles;
1492 m.numverts = mesh->numverts;
1494 if (R_Mesh_Draw_GetBuffer(&m))
1497 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1500 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)
1502 softwaretransform(v->v, outv);
1504 VectorSubtract(outv, r_origin, diff);
1505 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1507 outst[0] = v->st[0];
1508 outst[1] = v->st[1];
1513 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)
1515 softwaretransform(v->v, outv);
1517 outc[0] = outc[1] = outc[2] = cl;
1519 outst[0] = v->st[0];
1520 outst[1] = v->st[1];
1527 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1531 float *outv, *outc, *outst, cl, ca, diff[3], f;
1533 rmeshbufferinfo_t m;
1534 memset(&m, 0, sizeof(m));
1535 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1536 m.blendfunc1 = GL_SRC_ALPHA;
1537 m.blendfunc2 = GL_ONE;
1538 ca = currentrenderentity->alpha;
1539 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1541 m.numtriangles = mesh->numtriangles;
1542 m.numverts = mesh->numverts;
1544 if (R_Mesh_Draw_GetBuffer(&m))
1547 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1548 if (softwaretransform_complexity)
1550 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)
1552 softwaretransform(v->v, outv);
1554 VectorSubtract(outv, r_origin, diff);
1555 f = cl * exp(fogdensity/DotProduct(diff, diff));
1556 VectorScale(fogcolor, f, outc);
1558 outst[0] = v->st[0];
1559 outst[1] = v->st[1];
1564 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)
1566 VectorCopy(v->v, outv);
1568 VectorSubtract(outv, r_origin, diff);
1569 VectorSubtract(outv, r_origin, diff);
1570 f = cl * exp(fogdensity/DotProduct(diff, diff));
1571 VectorScale(fogcolor, f, outc);
1573 outst[0] = v->st[0];
1574 outst[1] = v->st[1];
1581 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1584 for (surf = firstsurf;surf;surf = surf->chain)
1587 RSurfShader_Wall_Pass_BaseFullbright(surf);
1589 for (surf = firstsurf;surf;surf = surf->chain)
1590 if (surf->currenttexture->glowtexture)
1591 RSurfShader_Wall_Pass_Glow(surf);
1593 for (surf = firstsurf;surf;surf = surf->chain)
1594 RSurfShader_Wall_Pass_Fog(surf);
1597 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1600 for (surf = firstsurf;surf;surf = surf->chain)
1603 RSurfShader_Wall_Pass_BaseVertex(surf);
1605 for (surf = firstsurf;surf;surf = surf->chain)
1606 if (surf->currenttexture->glowtexture)
1607 RSurfShader_Wall_Pass_Glow(surf);
1609 for (surf = firstsurf;surf;surf = surf->chain)
1610 RSurfShader_Wall_Pass_Fog(surf);
1613 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1616 if (r_vertexsurfaces.integer)
1618 for (surf = firstsurf;surf;surf = surf->chain)
1621 RSurfShader_Wall_Pass_BaseVertex(surf);
1623 for (surf = firstsurf;surf;surf = surf->chain)
1624 if (surf->currenttexture->glowtexture)
1625 RSurfShader_Wall_Pass_Glow(surf);
1627 for (surf = firstsurf;surf;surf = surf->chain)
1628 RSurfShader_Wall_Pass_Fog(surf);
1630 else if (r_multitexture.integer)
1632 if (r_dlightmap.integer)
1634 for (surf = firstsurf;surf;surf = surf->chain)
1637 RSurfShader_Wall_Pass_BaseMTex(surf);
1639 for (surf = firstsurf;surf;surf = surf->chain)
1640 if (surf->currenttexture->glowtexture)
1641 RSurfShader_Wall_Pass_Glow(surf);
1643 for (surf = firstsurf;surf;surf = surf->chain)
1644 RSurfShader_Wall_Pass_Fog(surf);
1648 for (surf = firstsurf;surf;surf = surf->chain)
1651 RSurfShader_Wall_Pass_BaseMTex(surf);
1653 for (surf = firstsurf;surf;surf = surf->chain)
1654 if (surf->dlightframe == r_framecount)
1655 RSurfShader_Wall_Pass_Light(surf);
1656 for (surf = firstsurf;surf;surf = surf->chain)
1657 if (surf->currenttexture->glowtexture)
1658 RSurfShader_Wall_Pass_Glow(surf);
1660 for (surf = firstsurf;surf;surf = surf->chain)
1661 RSurfShader_Wall_Pass_Fog(surf);
1664 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1666 for (surf = firstsurf;surf;surf = surf->chain)
1669 RSurfShader_Wall_Pass_BaseVertex(surf);
1671 for (surf = firstsurf;surf;surf = surf->chain)
1672 if (surf->currenttexture->glowtexture)
1673 RSurfShader_Wall_Pass_Glow(surf);
1675 for (surf = firstsurf;surf;surf = surf->chain)
1676 RSurfShader_Wall_Pass_Fog(surf);
1680 if (r_dlightmap.integer)
1682 for (surf = firstsurf;surf;surf = surf->chain)
1685 RSurfShader_Wall_Pass_BaseTexture(surf);
1687 for (surf = firstsurf;surf;surf = surf->chain)
1688 RSurfShader_Wall_Pass_BaseLightmap(surf);
1689 for (surf = firstsurf;surf;surf = surf->chain)
1690 if (surf->currenttexture->glowtexture)
1691 RSurfShader_Wall_Pass_Glow(surf);
1693 for (surf = firstsurf;surf;surf = surf->chain)
1694 RSurfShader_Wall_Pass_Fog(surf);
1698 for (surf = firstsurf;surf;surf = surf->chain)
1701 RSurfShader_Wall_Pass_BaseTexture(surf);
1703 for (surf = firstsurf;surf;surf = surf->chain)
1704 RSurfShader_Wall_Pass_BaseLightmap(surf);
1705 for (surf = firstsurf;surf;surf = surf->chain)
1706 if (surf->dlightframe == r_framecount)
1707 RSurfShader_Wall_Pass_Light(surf);
1708 for (surf = firstsurf;surf;surf = surf->chain)
1709 if (surf->currenttexture->glowtexture)
1710 RSurfShader_Wall_Pass_Glow(surf);
1712 for (surf = firstsurf;surf;surf = surf->chain)
1713 RSurfShader_Wall_Pass_Fog(surf);
1719 =============================================================
1723 =============================================================
1726 static void R_SolidWorldNode (void)
1728 if (r_viewleaf->contents != CONTENTS_SOLID)
1731 mportal_t *p, *pstack[8192];
1732 msurface_t *surf, **mark, **endmark;
1734 // LordHavoc: portal-passage worldnode; follows portals leading
1735 // outward from viewleaf, if a portal leads offscreen it is not
1736 // followed, in indoor maps this can often cull a great deal of
1737 // geometry away when pvs data is not present (useful with pvs as well)
1740 leaf->worldnodeframe = r_framecount;
1745 leaf->visframe = r_framecount;
1747 if (leaf->nummarksurfaces)
1749 mark = leaf->firstmarksurface;
1750 endmark = mark + leaf->nummarksurfaces;
1754 // make sure surfaces are only processed once
1755 if (surf->worldnodeframe == r_framecount)
1757 surf->worldnodeframe = r_framecount;
1758 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1760 if (surf->flags & SURF_PLANEBACK)
1761 surf->visframe = r_framecount;
1765 if (!(surf->flags & SURF_PLANEBACK))
1766 surf->visframe = r_framecount;
1769 while (mark < endmark);
1772 // follow portals into other leafs
1774 for (;p;p = p->next)
1776 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1779 if (leaf->worldnodeframe != r_framecount)
1781 leaf->worldnodeframe = r_framecount;
1782 if (leaf->contents != CONTENTS_SOLID)
1784 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1786 p->visframe = r_framecount;
1787 pstack[portalstack++] = p;
1791 p = pstack[--portalstack];
1803 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1804 int nodestackpos = 0;
1805 // LordHavoc: recursive descending worldnode; if portals are not
1806 // available, this is a good last resort, can cull large amounts of
1807 // geometry, but is more time consuming than portal-passage and renders
1808 // things behind walls
1811 if (R_NotCulledBox(node->mins, node->maxs))
1813 if (node->numsurfaces)
1815 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1816 if (PlaneDiff (r_origin, node->plane) < 0)
1818 for (;surf < surfend;surf++)
1820 if (surf->flags & SURF_PLANEBACK)
1821 surf->visframe = r_framecount;
1826 for (;surf < surfend;surf++)
1828 if (!(surf->flags & SURF_PLANEBACK))
1829 surf->visframe = r_framecount;
1834 // recurse down the children
1835 if (node->children[0]->contents >= 0)
1837 if (node->children[1]->contents >= 0)
1839 if (nodestackpos < 8192)
1840 nodestack[nodestackpos++] = node->children[1];
1841 node = node->children[0];
1845 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1846 node = node->children[0];
1851 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1852 if (node->children[1]->contents >= 0)
1854 node = node->children[1];
1857 else if (nodestackpos > 0)
1859 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1860 node = nodestack[--nodestackpos];
1865 else if (nodestackpos > 0)
1867 node = nodestack[--nodestackpos];
1873 static int r_portalframecount = 0;
1875 static void R_PVSWorldNode()
1878 mportal_t *p, *pstack[8192];
1879 msurface_t *surf, **mark, **endmark;
1883 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1886 leaf->worldnodeframe = r_framecount;
1891 leaf->visframe = r_framecount;
1893 if (leaf->nummarksurfaces)
1895 mark = leaf->firstmarksurface;
1896 endmark = mark + leaf->nummarksurfaces;
1900 // make sure surfaces are only processed once
1901 if (surf->worldnodeframe == r_framecount)
1903 surf->worldnodeframe = r_framecount;
1904 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1906 if (surf->flags & SURF_PLANEBACK)
1907 surf->visframe = r_framecount;
1911 if (!(surf->flags & SURF_PLANEBACK))
1912 surf->visframe = r_framecount;
1915 while (mark < endmark);
1918 // follow portals into other leafs
1919 for (p = leaf->portals;p;p = p->next)
1921 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1924 if (leaf->worldnodeframe != r_framecount)
1926 leaf->worldnodeframe = r_framecount;
1927 if (leaf->contents != CONTENTS_SOLID)
1929 i = (leaf - cl.worldmodel->leafs) - 1;
1930 if (worldvis[i>>3] & (1<<(i&7)))
1932 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1934 pstack[portalstack++] = p;
1938 p = pstack[--portalstack];
1950 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1951 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1952 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1953 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1954 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1956 int Cshader_count = 5;
1957 Cshader_t *Cshaders[5] =
1959 &Cshader_wall_vertex,
1960 &Cshader_wall_lightmap,
1961 &Cshader_wall_fullbright,
1966 void R_PrepareSurfaces(void)
1968 int i, alttextures, texframe, framecount;
1973 for (i = 0;i < Cshader_count;i++)
1974 Cshaders[i]->chain = NULL;
1976 model = currentrenderentity->model;
1977 alttextures = currentrenderentity->frame != 0;
1978 texframe = (int)(cl.time * 5.0f);
1980 for (i = 0;i < model->nummodelsurfaces;i++)
1982 surf = model->modelsortedsurfaces[i];
1983 if (surf->visframe == r_framecount)
1985 if (surf->insertframe != r_framecount)
1987 surf->insertframe = r_framecount;
1989 t = surf->texinfo->texture;
1992 framecount = t->anim_total[alttextures];
1993 if (framecount >= 2)
1994 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1996 surf->currenttexture = t->anim_frames[alttextures][0];
1999 surf->currenttexture = t;
2002 surf->chain = surf->shader->chain;
2003 surf->shader->chain = surf;
2008 void R_DrawSurfaces (int type)
2013 for (i = 0;i < Cshader_count;i++)
2015 shader = Cshaders[i];
2016 if (shader->chain && shader->shaderfunc[type])
2017 shader->shaderfunc[type](shader->chain);
2021 static float portalpointbuffer[256][3];
2023 void R_DrawPortals(void)
2026 mportal_t *portal, *endportal;
2029 drawportals = r_drawportals.integer;
2031 if (drawportals < 1)
2034 memset(&m, 0, sizeof(m));
2035 m.transparent = true;
2036 m.blendfunc1 = GL_SRC_ALPHA;
2037 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2038 m.vertex = &portalpointbuffer[0][0];
2039 m.vertexstep = sizeof(float[3]);
2041 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2043 if (portal->visframe == r_portalframecount)
2045 if (portal->numpoints <= 256)
2047 i = portal - cl.worldmodel->portals;
2048 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2049 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2050 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2051 point = portal->points;
2052 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2054 for (i = portal->numpoints - 1;i >= 0;i--)
2055 VectorCopy(point[i].position, portalpointbuffer[i]);
2059 for (i = 0;i < portal->numpoints;i++)
2060 VectorCopy(point[i].position, portalpointbuffer[i]);
2062 R_Mesh_DrawPolygon(&m, portal->numpoints);
2068 void R_SetupForBModelRendering(void)
2075 // because bmodels can be reused, we have to decide which things to render
2076 // from scratch every time
2078 model = currentrenderentity->model;
2080 softwaretransformforentity (currentrenderentity);
2081 softwareuntransform(r_origin, modelorg);
2083 for (i = 0;i < model->nummodelsurfaces;i++)
2085 surf = model->modelsortedsurfaces[i];
2086 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2087 surf->visframe = r_framecount;
2089 surf->visframe = -1;
2090 surf->worldnodeframe = -1;
2091 surf->lightframe = -1;
2092 surf->dlightframe = -1;
2093 surf->insertframe = -1;
2097 void R_SetupForWorldRendering(void)
2099 // there is only one instance of the world, but it can be rendered in
2102 currentrenderentity = &cl_entities[0].render;
2103 softwaretransformidentity();
2106 static void R_SurfMarkLights (void)
2111 if (r_dynamic.integer)
2114 if (!r_vertexsurfaces.integer)
2116 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2118 surf = currentrenderentity->model->modelsortedsurfaces[i];
2119 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2121 if (surf->cached_dlight
2122 || surf->cached_ambient != r_ambient.value
2123 || surf->cached_lightscalebit != lightscalebit)
2124 R_BuildLightMap(surf, false); // base lighting changed
2125 else if (r_dynamic.integer)
2127 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2128 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2129 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2130 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2131 R_BuildLightMap(surf, false); // base lighting changed
2132 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2133 R_BuildLightMap(surf, true); // only dlights
2140 void R_MarkWorldLights(void)
2142 R_SetupForWorldRendering();
2151 void R_DrawWorld (void)
2153 R_SetupForWorldRendering();
2155 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2156 R_SolidWorldNode ();
2166 void R_DrawBrushModelSky (void)
2168 R_SetupForBModelRendering();
2170 R_PrepareSurfaces();
2171 R_DrawSurfaces(SHADERSTAGE_SKY);
2174 void R_DrawBrushModelNormal (void)
2178 // have to flush queue because of possible lightmap reuse
2181 R_SetupForBModelRendering();
2185 R_PrepareSurfaces();
2187 if (!skyrendermasked)
2188 R_DrawSurfaces(SHADERSTAGE_SKY);
2189 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2192 static void gl_surf_start(void)
2196 static void gl_surf_shutdown(void)
2200 static void gl_surf_newmap(void)
2204 void GL_Surf_Init(void)
2207 dlightdivtable[0] = 4194304;
2208 for (i = 1;i < 32768;i++)
2209 dlightdivtable[i] = 4194304 / (i << 7);
2211 Cvar_RegisterVariable(&r_ambient);
2212 Cvar_RegisterVariable(&r_vertexsurfaces);
2213 Cvar_RegisterVariable(&r_dlightmap);
2214 Cvar_RegisterVariable(&r_drawportals);
2215 Cvar_RegisterVariable(&r_testvis);
2216 Cvar_RegisterVariable(&r_floatbuildlightmap);
2218 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);