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"};
37 cvar_t r_detailtextures = {CVAR_SAVE, "r_detailtextures", "1"};
39 static int dlightdivtable[32768];
41 static int R_IntAddDynamicLights (msurface_t *surf)
43 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
45 float dist, impact[3], local[3];
47 // LordHavoc: use 64bit integer... shame it's not very standardized...
48 #if _MSC_VER || __BORLANDC__
56 smax = (surf->extents[0] >> 4) + 1;
57 tmax = (surf->extents[1] >> 4) + 1;
60 for (lnum = 0; lnum < r_numdlights; lnum++)
62 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
63 continue; // not lit by this light
65 softwareuntransform(r_dlight[lnum].origin, local);
66 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
68 // for comparisons to minimum acceptable light
69 // compensate for LIGHTOFFSET
70 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
77 if (surf->plane->type < 3)
79 VectorCopy(local, impact);
80 impact[surf->plane->type] -= dist;
84 impact[0] = local[0] - surf->plane->normal[0] * dist;
85 impact[1] = local[1] - surf->plane->normal[1] * dist;
86 impact[2] = local[2] - surf->plane->normal[2] * dist;
89 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
90 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
92 s = bound(0, impacts, smax * 16) - impacts;
93 t = bound(0, impactt, tmax * 16) - impactt;
94 i = s * s + t * t + dist2;
98 // reduce calculations
99 for (s = 0, i = impacts; s < smax; s++, i -= 16)
100 sdtable[s] = i * i + dist2;
102 maxdist3 = maxdist - dist2;
104 // convert to 8.8 blocklights format
105 red = r_dlight[lnum].light[0];
106 green = r_dlight[lnum].light[1];
107 blue = r_dlight[lnum].light[2];
108 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
112 for (t = 0;t < tmax;t++, i -= 16)
115 // make sure some part of it is visible on this line
118 maxdist2 = maxdist - td;
119 for (s = 0;s < smax;s++)
121 if (sdtable[s] < maxdist2)
123 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
126 bl[0] += (red * k) >> 7;
127 bl[1] += (green * k) >> 7;
128 bl[2] += (blue * k) >> 7;
142 static int R_FloatAddDynamicLights (msurface_t *surf)
144 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
145 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
149 smax = (surf->extents[0] >> 4) + 1;
150 tmax = (surf->extents[1] >> 4) + 1;
153 for (lnum = 0; lnum < r_numdlights; lnum++)
155 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
156 continue; // not lit by this light
158 softwareuntransform(r_dlight[lnum].origin, local);
159 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
161 // for comparisons to minimum acceptable light
162 // compensate for LIGHTOFFSET
163 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
166 dist2 += LIGHTOFFSET;
167 if (dist2 >= maxdist)
170 if (surf->plane->type < 3)
172 VectorCopy(local, impact);
173 impact[surf->plane->type] -= dist;
177 impact[0] = local[0] - surf->plane->normal[0] * dist;
178 impact[1] = local[1] - surf->plane->normal[1] * dist;
179 impact[2] = local[2] - surf->plane->normal[2] * dist;
182 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
183 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
185 td = bound(0, impacts, smax * 16) - impacts;
186 td1 = bound(0, impactt, tmax * 16) - impactt;
187 td = td * td + td1 * td1 + dist2;
191 // reduce calculations
192 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
193 sdtable[s] = td1 * td1 + dist2;
195 maxdist3 = maxdist - dist2;
197 // convert to 8.8 blocklights format
198 red = r_dlight[lnum].light[0];
199 green = r_dlight[lnum].light[1];
200 blue = r_dlight[lnum].light[2];
201 subtract = r_dlight[lnum].subtract * 32768.0f;
202 bl = floatblocklights;
205 for (t = 0;t < tmax;t++, td1 -= 16.0f)
208 // make sure some part of it is visible on this line
211 maxdist2 = maxdist - td;
212 for (s = 0;s < smax;s++)
214 if (sdtable[s] < maxdist2)
216 k = (32768.0f / (sdtable[s] + td)) - subtract;
236 Combine and scale multiple lightmaps into the 8.8 format in blocklights
239 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
241 if (!r_floatbuildlightmap.integer)
243 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
244 unsigned int *bl, scale;
245 qbyte *lightmap, *out, *stain;
247 // update cached lighting info
248 surf->cached_dlight = 0;
249 surf->cached_lightscalebit = lightscalebit;
250 surf->cached_ambient = r_ambient.value;
251 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
252 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
253 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
254 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
256 smax = (surf->extents[0]>>4)+1;
257 tmax = (surf->extents[1]>>4)+1;
260 lightmap = surf->samples;
262 // set to full bright if no light data
264 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
266 for (i = 0;i < size3;i++)
272 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
275 for (i = 0;i < size3;i++)
279 memset(bl, 0, size*3*sizeof(unsigned int));
281 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
283 surf->cached_dlight = R_IntAddDynamicLights(surf);
284 if (surf->cached_dlight)
286 else if (dlightchanged)
287 return; // don't upload if only updating dlights and none mattered
290 // add all the lightmaps
294 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
295 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
296 bl[i] += lightmap[i] * scale;
300 stain = surf->stainsamples;
303 // deal with lightmap brightness scale
304 shift = 7 + lightscalebit + 8;
305 if (currentrenderentity->model->lightmaprgba)
307 stride = (surf->lightmaptexturestride - smax) * 4;
308 for (i = 0;i < tmax;i++, out += stride)
310 for (j = 0;j < smax;j++)
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
314 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
321 stride = (surf->lightmaptexturestride - smax) * 3;
322 for (i = 0;i < tmax;i++, out += stride)
324 for (j = 0;j < smax;j++)
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
328 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
333 R_UpdateTexture(surf->lightmaptexture, templight);
337 int smax, tmax, i, j, size, size3, maps, stride, l;
339 qbyte *lightmap, *out, *stain;
341 // update cached lighting info
342 surf->cached_dlight = 0;
343 surf->cached_lightscalebit = lightscalebit;
344 surf->cached_ambient = r_ambient.value;
345 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
346 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
347 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
348 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
350 smax = (surf->extents[0]>>4)+1;
351 tmax = (surf->extents[1]>>4)+1;
354 lightmap = surf->samples;
356 // set to full bright if no light data
357 bl = floatblocklights;
358 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
361 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
366 for (i = 0;i < size3;i++)
370 memset(bl, 0, size*3*sizeof(float));
372 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
374 surf->cached_dlight = R_FloatAddDynamicLights(surf);
375 if (surf->cached_dlight)
377 else if (dlightchanged)
378 return; // don't upload if only updating dlights and none mattered
381 // add all the lightmaps
384 bl = floatblocklights;
385 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
386 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
387 bl[i] += lightmap[i] * scale;
390 stain = surf->stainsamples;
391 bl = floatblocklights;
393 // deal with lightmap brightness scale
394 scale = 1.0f / (1 << (7 + lightscalebit + 8));
395 if (currentrenderentity->model->lightmaprgba)
397 stride = (surf->lightmaptexturestride - smax) * 4;
398 for (i = 0;i < tmax;i++, out += stride)
400 for (j = 0;j < smax;j++)
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
404 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
411 stride = (surf->lightmaptexturestride - smax) * 3;
412 for (i = 0;i < tmax;i++, out += stride)
414 for (j = 0;j < smax;j++)
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
418 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
423 R_UpdateTexture(surf->lightmaptexture, templight);
427 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
430 msurface_t *surf, *endsurf;
431 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
434 // LordHavoc: use 64bit integer... shame it's not very standardized...
435 #if _MSC_VER || __BORLANDC__
442 // for comparisons to minimum acceptable light
443 // compensate for 256 offset
444 maxdist = radius * radius + 256.0f;
446 // clamp radius to avoid exceeding 32768 entry division table
447 if (maxdist > 4194304)
450 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
453 if (node->contents < 0)
455 ndist = PlaneDiff(origin, node->plane);
458 node = node->children[0];
463 node = node->children[1];
467 dist2 = ndist * ndist + 256.0f;
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, false))
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)
685 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
688 int i, l, lit = false;
691 for (l = 0;l < r_numdlights;l++)
693 if (dlightbits[l >> 5] & (1 << (l & 31)))
696 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
697 VectorCopy(rd->origin, lightorigin);
698 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
700 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
701 if (f < rd->cullradius2)
703 f = (1.0f / f) - rd->subtract;
704 VectorMA(c, f, rd->light, c);
713 // note: this untransforms lights to do the checking,
714 // and takes surf->mesh->vertex data
715 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
721 for (l = 0;l < r_numdlights;l++)
723 if (dlightbits[l >> 5] & (1 << (l & 31)))
726 softwareuntransform(rd->origin, lightorigin);
727 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
728 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
735 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
739 float *outv, *outc, *outst, cl, diff[3];
740 float base[3], scale, f;
744 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
745 memset(&m, 0, sizeof(m));
746 if (currentrenderentity->effects & EF_ADDITIVE)
748 m.transparent = true;
749 m.blendfunc1 = GL_SRC_ALPHA;
750 m.blendfunc2 = GL_ONE;
752 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
754 m.transparent = true;
755 m.blendfunc1 = GL_SRC_ALPHA;
756 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
760 m.transparent = false;
761 m.blendfunc1 = GL_ONE;
762 m.blendfunc2 = GL_ZERO;
764 m.depthwrite = false;
765 m.depthdisable = false;
766 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
767 if (surf->flags & SURF_DRAWFULLBRIGHT || currentrenderentity->effects & EF_FULLBRIGHT)
769 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
771 m.numtriangles = mesh->numtriangles;
772 m.numverts = mesh->numverts;
773 if (R_Mesh_Draw_GetBuffer(&m, true))
775 base[0] = base[1] = base[2] = 1.0f * m.colorscale;
776 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
777 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)
779 softwaretransform(v->v, outv);
781 VectorCopy(base, outc);
787 VectorSubtract(outv, r_origin, diff);
788 f = 1 - exp(fogdensity/DotProduct(diff, diff));
789 VectorScale(outc, f, outc);
798 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
799 base[0] = base[1] = base[2] = (r_ambient.value * (1.0f / 64.0f) + ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f));
800 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
802 m.numtriangles = mesh->numtriangles;
803 m.numverts = mesh->numverts;
804 if (R_Mesh_Draw_GetBuffer(&m, true))
807 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
808 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)
810 softwaretransform(v->v, outv);
812 VectorCopy(base, outc);
817 if (surf->dlightframe == r_framecount)
818 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
819 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
821 if (surf->flags & SURF_LIGHTMAP)
822 if (surf->styles[0] != 255)
824 lm = surf->samples + v->lightmapoffset;
825 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
826 VectorMA(outc, scale, lm, outc);
827 if (surf->styles[1] != 255)
830 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
831 VectorMA(outc, scale, lm, outc);
832 if (surf->styles[2] != 255)
835 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
836 VectorMA(outc, scale, lm, outc);
837 if (surf->styles[3] != 255)
840 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
841 VectorMA(outc, scale, lm, outc);
848 VectorSubtract(outv, r_origin, diff);
849 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
850 VectorScale(outc, f, outc);
853 VectorScale(outc, cl, outc);
861 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
865 float *outv, *outc, *outst, diff[3];
869 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
870 memset(&m, 0, sizeof(m));
871 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || alpha < 1;
872 m.blendfunc1 = GL_SRC_ALPHA;
873 m.blendfunc2 = GL_ONE;
874 m.depthwrite = false;
875 m.depthdisable = false;
876 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
877 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
879 m.numtriangles = mesh->numtriangles;
880 m.numverts = mesh->numverts;
881 if (R_Mesh_Draw_GetBuffer(&m, false))
883 VectorScale(fogcolor, m.colorscale, base);
884 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
885 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
887 softwaretransform(v->v, outv);
889 VectorSubtract(outv, r_origin, diff);
890 f = exp(fogdensity/DotProduct(diff, diff));
891 VectorScale(base, f, outc);
896 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
907 static void RSurfShader_Water(msurface_t *firstsurf)
910 for (surf = firstsurf;surf;surf = surf->chain)
911 RSurfShader_Water_Pass_Base(surf);
913 for (surf = firstsurf;surf;surf = surf->chain)
914 RSurfShader_Water_Pass_Fog(surf);
917 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
921 float *outv, *outc, *outst, cl, ca, diff[3];
922 float base[3], scale, f;
926 memset(&m, 0, sizeof(m));
927 if (currentrenderentity->effects & EF_ADDITIVE)
929 m.transparent = true;
930 m.blendfunc1 = GL_SRC_ALPHA;
931 m.blendfunc2 = GL_ONE;
933 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
935 m.transparent = true;
936 m.blendfunc1 = GL_SRC_ALPHA;
937 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
941 m.transparent = false;
942 m.blendfunc1 = GL_ONE;
943 m.blendfunc2 = GL_ZERO;
945 m.depthwrite = false;
946 m.depthdisable = false;
947 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
949 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
951 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
953 ca = currentrenderentity->alpha;
954 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
956 m.numtriangles = mesh->numtriangles;
957 m.numverts = mesh->numverts;
959 if (R_Mesh_Draw_GetBuffer(&m, true))
962 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
964 if (currentrenderentity->effects & EF_FULLBRIGHT)
966 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)
968 softwaretransform(v->v, outv);
970 VectorSubtract(outv, r_origin, diff);
971 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
979 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)
981 softwaretransform(v->v, outv);
983 VectorCopy(base, outc);
989 if (surf->dlightframe == r_framecount)
990 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
992 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
994 if (surf->styles[0] != 255)
996 lm = surf->samples + v->lightmapoffset;
997 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
998 VectorMA(outc, scale, lm, outc);
999 if (surf->styles[1] != 255)
1002 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1003 VectorMA(outc, scale, lm, outc);
1004 if (surf->styles[2] != 255)
1007 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1008 VectorMA(outc, scale, lm, outc);
1009 if (surf->styles[3] != 255)
1012 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1013 VectorMA(outc, scale, lm, outc);
1020 VectorSubtract(outv, r_origin, diff);
1021 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1022 VectorScale(outc, f, outc);
1025 VectorScale(outc, cl, outc);
1033 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1037 float *outv, *outc, *outst, cl, ca, diff[3];
1039 rmeshbufferinfo_t m;
1040 memset(&m, 0, sizeof(m));
1041 if (currentrenderentity->effects & EF_ADDITIVE)
1043 m.transparent = true;
1044 m.blendfunc1 = GL_SRC_ALPHA;
1045 m.blendfunc2 = GL_ONE;
1047 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1049 m.transparent = true;
1050 m.blendfunc1 = GL_SRC_ALPHA;
1051 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1055 m.transparent = false;
1056 m.blendfunc1 = GL_ONE;
1057 m.blendfunc2 = GL_ZERO;
1059 m.depthwrite = false;
1060 m.depthdisable = false;
1061 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1062 ca = currentrenderentity->alpha;
1063 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1065 m.numtriangles = mesh->numtriangles;
1066 m.numverts = mesh->numverts;
1068 if (R_Mesh_Draw_GetBuffer(&m, false))
1071 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1072 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)
1074 softwaretransform(v->v, outv);
1078 VectorSubtract(outv, r_origin, diff);
1079 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1082 outc[0] = outc[1] = outc[2] = cl;
1084 outst[0] = v->st[0];
1085 outst[1] = v->st[1];
1092 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1096 float *outv, *outc, *outst, cl, ca, diff[3];
1098 rmeshbufferinfo_t m;
1099 memset(&m, 0, sizeof(m));
1100 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1101 m.blendfunc1 = GL_SRC_ALPHA;
1102 m.blendfunc2 = GL_ONE;
1103 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1104 ca = currentrenderentity->alpha;
1105 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1107 m.numtriangles = mesh->numtriangles;
1108 m.numverts = mesh->numverts;
1110 if (R_Mesh_Draw_GetBuffer(&m, false))
1113 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1114 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)
1116 softwaretransform(v->v, outv);
1120 VectorSubtract(outv, r_origin, diff);
1121 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1124 outc[0] = outc[1] = outc[2] = cl;
1126 outst[0] = v->st[0];
1127 outst[1] = v->st[1];
1134 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1138 float *outv, *outc, *outst, cl, ca, diff[3], f;
1140 rmeshbufferinfo_t m;
1141 memset(&m, 0, sizeof(m));
1142 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1143 m.blendfunc1 = GL_SRC_ALPHA;
1144 m.blendfunc2 = GL_ONE;
1145 ca = currentrenderentity->alpha;
1146 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1148 m.numtriangles = mesh->numtriangles;
1149 m.numverts = mesh->numverts;
1151 if (R_Mesh_Draw_GetBuffer(&m, false))
1154 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1155 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)
1157 softwaretransform(v->v, outv);
1159 VectorSubtract(outv, r_origin, diff);
1160 f = cl * exp(fogdensity/DotProduct(diff, diff));
1161 VectorScale(fogcolor, f, outc);
1163 outst[0] = v->st[0];
1164 outst[1] = v->st[1];
1171 static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(msurface_t *surf)
1175 float *outv, *outc, *outst, *outuv, *outab, cl;
1177 rmeshbufferinfo_t m;
1178 memset(&m, 0, sizeof(m));
1179 m.transparent = false;
1180 m.blendfunc1 = GL_ONE;
1181 m.blendfunc2 = GL_ZERO;
1182 m.depthwrite = false;
1183 m.depthdisable = false;
1184 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1185 m.texrgbscale[0] = 1.0f;
1186 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1187 m.texrgbscale[1] = 4.0f;
1188 m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
1189 m.texrgbscale[2] = 2.0f;
1190 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1192 m.numtriangles = mesh->numtriangles;
1193 m.numverts = mesh->numverts;
1195 if (R_Mesh_Draw_GetBuffer(&m, false))
1197 cl = (float) (1 << lightscalebit) * m.colorscale;
1198 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1199 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color, outst = m.texcoords[0], outuv = m.texcoords[1], outab = m.texcoords[2];i < m.numverts;i++, v++, outv += 4, outc += 4, outst += 2, outuv += 2, outab += 2)
1201 softwaretransform(v->v, outv);
1203 outc[0] = outc[1] = outc[2] = cl;
1205 outst[0] = v->st[0];
1206 outst[1] = v->st[1];
1207 outuv[0] = v->uv[0];
1208 outuv[1] = v->uv[1];
1209 outab[0] = v->ab[0];
1210 outab[1] = v->ab[1];
1217 static void RSurfShader_OpaqueWall_Pass_BaseMTex(msurface_t *surf)
1221 float *outv, *outc, *outst, *outuv, cl;
1223 rmeshbufferinfo_t m;
1224 memset(&m, 0, sizeof(m));
1225 m.transparent = false;
1226 m.blendfunc1 = GL_ONE;
1227 m.blendfunc2 = GL_ZERO;
1228 m.depthwrite = false;
1229 m.depthdisable = false;
1230 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1231 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1232 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1234 m.numtriangles = mesh->numtriangles;
1235 m.numverts = mesh->numverts;
1237 if (R_Mesh_Draw_GetBuffer(&m, true))
1239 cl = (float) (1 << lightscalebit) * m.colorscale;
1240 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1241 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)
1243 softwaretransform(v->v, outv);
1245 outc[0] = outc[1] = outc[2] = cl;
1247 outst[0] = v->st[0];
1248 outst[1] = v->st[1];
1249 outuv[0] = v->uv[0];
1250 outuv[1] = v->uv[1];
1257 static void RSurfShader_OpaqueWall_Pass_BaseTexture(msurface_t *surf)
1261 float *outv, *outc, *outst, cl;
1263 rmeshbufferinfo_t m;
1264 memset(&m, 0, sizeof(m));
1265 m.transparent = false;
1266 m.blendfunc1 = GL_ONE;
1267 m.blendfunc2 = GL_ZERO;
1268 m.depthwrite = false;
1269 m.depthdisable = false;
1270 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1271 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1273 m.numtriangles = mesh->numtriangles;
1274 m.numverts = mesh->numverts;
1276 if (R_Mesh_Draw_GetBuffer(&m, false))
1278 cl = (float) (1 << lightscalebit) * m.colorscale;
1279 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1280 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)
1282 softwaretransform(v->v, outv);
1284 outc[0] = outc[1] = outc[2] = cl;
1286 outst[0] = v->st[0];
1287 outst[1] = v->st[1];
1294 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(msurface_t *surf)
1298 float *outv, *outc, *outuv, cl;
1300 rmeshbufferinfo_t m;
1301 memset(&m, 0, sizeof(m));
1302 m.transparent = false;
1303 m.blendfunc1 = GL_ZERO;
1304 m.blendfunc2 = GL_SRC_COLOR;
1305 m.depthwrite = false;
1306 m.depthdisable = false;
1307 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1308 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1310 m.numtriangles = mesh->numtriangles;
1311 m.numverts = mesh->numverts;
1313 if (R_Mesh_Draw_GetBuffer(&m, true))
1315 cl = (float) (1 << lightscalebit) * m.colorscale;
1316 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1317 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)
1319 softwaretransform(v->v, outv);
1321 outc[0] = outc[1] = outc[2] = cl;
1323 outuv[0] = v->uv[0];
1324 outuv[1] = v->uv[1];
1331 static void RSurfShader_OpaqueWall_Pass_Light(msurface_t *surf)
1335 float *outv, *outc, *outst, cl;
1337 rmeshbufferinfo_t m;
1339 if (surf->dlightframe != r_framecount)
1341 if (currentrenderentity->effects & EF_FULLBRIGHT)
1344 memset(&m, 0, sizeof(m));
1345 m.transparent = false;
1346 m.blendfunc1 = GL_SRC_ALPHA;
1347 m.blendfunc2 = GL_ONE;
1348 m.depthwrite = false;
1349 m.depthdisable = false;
1350 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1351 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1353 if (RSurf_LightCheck(surf->dlightbits, mesh))
1355 m.numtriangles = mesh->numtriangles;
1356 m.numverts = mesh->numverts;
1358 if (R_Mesh_Draw_GetBuffer(&m, true))
1361 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1362 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)
1364 softwaretransform(v->v, outv);
1368 outst[0] = v->st[0];
1369 outst[1] = v->st[1];
1371 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1373 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1374 VectorScale(outc, cl, outc);
1381 static void RSurfShader_OpaqueWall_Pass_Fog(msurface_t *surf)
1385 float *outv, *outc, cl, diff[3], fcolor[3];
1387 rmeshbufferinfo_t m;
1388 memset(&m, 0, sizeof(m));
1389 m.transparent = false;
1390 m.blendfunc1 = GL_SRC_ALPHA;
1391 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1392 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1394 m.numtriangles = mesh->numtriangles;
1395 m.numverts = mesh->numverts;
1397 if (R_Mesh_Draw_GetBuffer(&m, false))
1400 VectorScale(fogcolor, cl, fcolor);
1401 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1402 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1404 softwaretransform(v->v, outv);
1406 VectorCopy(fcolor, outc);
1407 VectorSubtract(outv, r_origin, diff);
1408 outc[3] = exp(fogdensity/DotProduct(diff, diff));
1415 static void RSurfShader_OpaqueWall_Pass_BaseDetail(msurface_t *surf)
1419 float *outv, *outc, *outst;
1421 rmeshbufferinfo_t m;
1422 memset(&m, 0, sizeof(m));
1423 m.transparent = false;
1424 m.blendfunc1 = GL_DST_COLOR;
1425 m.blendfunc2 = GL_SRC_COLOR;
1426 m.depthwrite = false;
1427 m.depthdisable = false;
1428 m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
1429 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1431 m.numtriangles = mesh->numtriangles;
1432 m.numverts = mesh->numverts;
1434 if (R_Mesh_Draw_GetBuffer(&m, false))
1436 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1437 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)
1439 softwaretransform(v->v, outv);
1441 outc[0] = outc[1] = outc[2] = outc[3] = 1;
1442 outst[0] = v->ab[0];
1443 outst[1] = v->ab[1];
1450 static void RSurfShader_OpaqueWall_Pass_Glow(msurface_t *surf)
1454 float *outv, *outc, *outst, cl;
1456 rmeshbufferinfo_t m;
1457 memset(&m, 0, sizeof(m));
1458 m.transparent = false;
1459 m.blendfunc1 = GL_SRC_ALPHA;
1460 m.blendfunc2 = GL_ONE;
1461 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1462 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1464 m.numtriangles = mesh->numtriangles;
1465 m.numverts = mesh->numverts;
1467 if (R_Mesh_Draw_GetBuffer(&m, false))
1470 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1471 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)
1473 softwaretransform(v->v, outv);
1475 outc[0] = outc[1] = outc[2] = cl;
1477 outst[0] = v->st[0];
1478 outst[1] = v->st[1];
1485 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1488 for (surf = firstsurf;surf;surf = surf->chain)
1491 RSurfShader_Wall_Pass_BaseFullbright(surf);
1493 for (surf = firstsurf;surf;surf = surf->chain)
1494 if (surf->currenttexture->glowtexture)
1495 RSurfShader_Wall_Pass_Glow(surf);
1497 for (surf = firstsurf;surf;surf = surf->chain)
1498 RSurfShader_Wall_Pass_Fog(surf);
1501 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1504 for (surf = firstsurf;surf;surf = surf->chain)
1507 RSurfShader_Wall_Pass_BaseVertex(surf);
1509 for (surf = firstsurf;surf;surf = surf->chain)
1510 if (surf->currenttexture->glowtexture)
1511 RSurfShader_Wall_Pass_Glow(surf);
1513 for (surf = firstsurf;surf;surf = surf->chain)
1514 RSurfShader_Wall_Pass_Fog(surf);
1517 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1520 if (r_vertexsurfaces.integer || firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1522 for (surf = firstsurf;surf;surf = surf->chain)
1525 RSurfShader_Wall_Pass_BaseVertex(surf);
1527 for (surf = firstsurf;surf;surf = surf->chain)
1528 if (surf->currenttexture->glowtexture)
1529 RSurfShader_Wall_Pass_Glow(surf);
1531 for (surf = firstsurf;surf;surf = surf->chain)
1532 RSurfShader_Wall_Pass_Fog(surf);
1536 if (r_textureunits.integer >= 2)
1538 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1540 for (surf = firstsurf;surf;surf = surf->chain)
1543 RSurfShader_OpaqueWall_Pass_TripleTexCombine(surf);
1548 for (surf = firstsurf;surf;surf = surf->chain)
1551 RSurfShader_OpaqueWall_Pass_BaseMTex(surf);
1553 if (r_detailtextures.integer)
1554 for (surf = firstsurf;surf;surf = surf->chain)
1555 RSurfShader_OpaqueWall_Pass_BaseDetail(surf);
1560 for (surf = firstsurf;surf;surf = surf->chain)
1563 RSurfShader_OpaqueWall_Pass_BaseTexture(surf);
1565 for (surf = firstsurf;surf;surf = surf->chain)
1566 RSurfShader_OpaqueWall_Pass_BaseLightmap(surf);
1567 if (r_detailtextures.integer)
1568 for (surf = firstsurf;surf;surf = surf->chain)
1569 RSurfShader_OpaqueWall_Pass_BaseDetail(surf);
1571 if (!r_dlightmap.integer)
1572 for (surf = firstsurf;surf;surf = surf->chain)
1573 if (surf->dlightframe == r_framecount)
1574 RSurfShader_OpaqueWall_Pass_Light(surf);
1575 for (surf = firstsurf;surf;surf = surf->chain)
1576 if (surf->currenttexture->glowtexture)
1577 RSurfShader_OpaqueWall_Pass_Glow(surf);
1579 for (surf = firstsurf;surf;surf = surf->chain)
1580 RSurfShader_OpaqueWall_Pass_Fog(surf);
1585 =============================================================
1589 =============================================================
1592 static void R_SolidWorldNode (void)
1594 if (r_viewleaf->contents != CONTENTS_SOLID)
1597 mportal_t *p, *pstack[8192];
1598 msurface_t *surf, **mark, **endmark;
1600 // LordHavoc: portal-passage worldnode; follows portals leading
1601 // outward from viewleaf, if a portal leads offscreen it is not
1602 // followed, in indoor maps this can often cull a great deal of
1603 // geometry away when pvs data is not present (useful with pvs as well)
1606 leaf->worldnodeframe = r_framecount;
1611 leaf->visframe = r_framecount;
1613 if (leaf->nummarksurfaces)
1615 mark = leaf->firstmarksurface;
1616 endmark = mark + leaf->nummarksurfaces;
1620 // make sure surfaces are only processed once
1621 if (surf->worldnodeframe == r_framecount)
1623 surf->worldnodeframe = r_framecount;
1624 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1626 if (surf->flags & SURF_PLANEBACK)
1627 surf->visframe = r_framecount;
1631 if (!(surf->flags & SURF_PLANEBACK))
1632 surf->visframe = r_framecount;
1635 while (mark < endmark);
1638 // follow portals into other leafs
1640 for (;p;p = p->next)
1642 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1645 if (leaf->worldnodeframe != r_framecount)
1647 leaf->worldnodeframe = r_framecount;
1648 if (leaf->contents != CONTENTS_SOLID)
1650 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1652 p->visframe = r_framecount;
1653 pstack[portalstack++] = p;
1657 p = pstack[--portalstack];
1669 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1670 int nodestackpos = 0;
1671 // LordHavoc: recursive descending worldnode; if portals are not
1672 // available, this is a good last resort, can cull large amounts of
1673 // geometry, but is more time consuming than portal-passage and renders
1674 // things behind walls
1677 if (R_NotCulledBox(node->mins, node->maxs))
1679 if (node->numsurfaces)
1681 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1682 if (PlaneDiff (r_origin, node->plane) < 0)
1684 for (;surf < surfend;surf++)
1686 if (surf->flags & SURF_PLANEBACK)
1687 surf->visframe = r_framecount;
1692 for (;surf < surfend;surf++)
1694 if (!(surf->flags & SURF_PLANEBACK))
1695 surf->visframe = r_framecount;
1700 // recurse down the children
1701 if (node->children[0]->contents >= 0)
1703 if (node->children[1]->contents >= 0)
1705 if (nodestackpos < 8192)
1706 nodestack[nodestackpos++] = node->children[1];
1707 node = node->children[0];
1711 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1712 node = node->children[0];
1717 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1718 if (node->children[1]->contents >= 0)
1720 node = node->children[1];
1723 else if (nodestackpos > 0)
1725 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1726 node = nodestack[--nodestackpos];
1731 else if (nodestackpos > 0)
1733 node = nodestack[--nodestackpos];
1739 static int r_portalframecount = 0;
1741 static void R_PVSWorldNode()
1744 mportal_t *p, *pstack[8192];
1745 msurface_t *surf, **mark, **endmark;
1749 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1752 leaf->worldnodeframe = r_framecount;
1757 leaf->visframe = r_framecount;
1759 if (leaf->nummarksurfaces)
1761 mark = leaf->firstmarksurface;
1762 endmark = mark + leaf->nummarksurfaces;
1766 // make sure surfaces are only processed once
1767 if (surf->worldnodeframe == r_framecount)
1769 surf->worldnodeframe = r_framecount;
1770 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1772 if (surf->flags & SURF_PLANEBACK)
1773 surf->visframe = r_framecount;
1777 if (!(surf->flags & SURF_PLANEBACK))
1778 surf->visframe = r_framecount;
1781 while (mark < endmark);
1784 // follow portals into other leafs
1785 for (p = leaf->portals;p;p = p->next)
1787 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1790 if (leaf->worldnodeframe != r_framecount)
1792 leaf->worldnodeframe = r_framecount;
1793 if (leaf->contents != CONTENTS_SOLID)
1795 i = (leaf - cl.worldmodel->leafs) - 1;
1796 if (worldvis[i>>3] & (1<<(i&7)))
1798 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1800 pstack[portalstack++] = p;
1804 p = pstack[--portalstack];
1816 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1817 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1818 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1819 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1820 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1822 int Cshader_count = 5;
1823 Cshader_t *Cshaders[5] =
1825 &Cshader_wall_vertex,
1826 &Cshader_wall_lightmap,
1827 &Cshader_wall_fullbright,
1832 void R_PrepareSurfaces(void)
1834 int i, alttextures, texframe, framecount;
1839 for (i = 0;i < Cshader_count;i++)
1840 Cshaders[i]->chain = NULL;
1842 model = currentrenderentity->model;
1843 alttextures = currentrenderentity->frame != 0;
1844 texframe = (int)(cl.time * 5.0f);
1846 for (i = 0;i < model->nummodelsurfaces;i++)
1848 surf = model->modelsortedsurfaces[i];
1849 if (surf->visframe == r_framecount)
1851 if (surf->insertframe != r_framecount)
1853 surf->insertframe = r_framecount;
1855 t = surf->texinfo->texture;
1858 framecount = t->anim_total[alttextures];
1859 if (framecount >= 2)
1860 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1862 surf->currenttexture = t->anim_frames[alttextures][0];
1865 surf->currenttexture = t;
1868 surf->chain = surf->shader->chain;
1869 surf->shader->chain = surf;
1874 void R_DrawSurfaces (int type)
1879 for (i = 0;i < Cshader_count;i++)
1881 shader = Cshaders[i];
1882 if (shader->chain && shader->shaderfunc[type])
1883 shader->shaderfunc[type](shader->chain);
1887 static float portalpointbuffer[256][3];
1889 void R_DrawPortals(void)
1892 mportal_t *portal, *endportal;
1895 drawportals = r_drawportals.integer;
1897 if (drawportals < 1)
1900 memset(&m, 0, sizeof(m));
1901 m.transparent = true;
1902 m.blendfunc1 = GL_SRC_ALPHA;
1903 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1904 m.vertex = &portalpointbuffer[0][0];
1905 m.vertexstep = sizeof(float[3]);
1907 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1909 if (portal->visframe == r_portalframecount)
1911 if (portal->numpoints <= 256)
1913 i = portal - cl.worldmodel->portals;
1914 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
1915 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
1916 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
1917 point = portal->points;
1918 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1920 for (i = portal->numpoints - 1;i >= 0;i--)
1921 VectorCopy(point[i].position, portalpointbuffer[i]);
1925 for (i = 0;i < portal->numpoints;i++)
1926 VectorCopy(point[i].position, portalpointbuffer[i]);
1928 R_Mesh_DrawPolygon(&m, portal->numpoints);
1935 void R_SetupForBModelRendering(void)
1942 // because bmodels can be reused, we have to decide which things to render
1943 // from scratch every time
1945 model = currentrenderentity->model;
1947 softwaretransformforentity (currentrenderentity);
1948 softwareuntransform(r_origin, modelorg);
1950 for (i = 0;i < model->nummodelsurfaces;i++)
1952 surf = model->modelsortedsurfaces[i];
1953 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
1954 surf->visframe = r_framecount;
1956 surf->visframe = -1;
1957 surf->worldnodeframe = -1;
1958 surf->lightframe = -1;
1959 surf->dlightframe = -1;
1960 surf->insertframe = -1;
1964 void R_SetupForWorldRendering(void)
1966 // there is only one instance of the world, but it can be rendered in
1969 currentrenderentity = &cl_entities[0].render;
1970 softwaretransformidentity();
1973 static void R_SurfMarkLights (void)
1978 if (r_dynamic.integer)
1981 if (!r_vertexsurfaces.integer)
1983 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
1985 surf = currentrenderentity->model->modelsortedsurfaces[i];
1986 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
1988 if (surf->cached_dlight
1989 || surf->cached_ambient != r_ambient.value
1990 || surf->cached_lightscalebit != lightscalebit)
1991 R_BuildLightMap(surf, false); // base lighting changed
1992 else if (r_dynamic.integer)
1994 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1995 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1996 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1997 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1998 R_BuildLightMap(surf, false); // base lighting changed
1999 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2000 R_BuildLightMap(surf, true); // only dlights
2007 void R_MarkWorldLights(void)
2009 R_SetupForWorldRendering();
2018 void R_DrawWorld (void)
2020 R_SetupForWorldRendering();
2022 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2023 R_SolidWorldNode ();
2033 void R_DrawBrushModelSky (void)
2035 R_SetupForBModelRendering();
2037 R_PrepareSurfaces();
2038 R_DrawSurfaces(SHADERSTAGE_SKY);
2041 void R_DrawBrushModelNormal (void)
2045 // have to flush queue because of possible lightmap reuse
2048 R_SetupForBModelRendering();
2052 R_PrepareSurfaces();
2054 if (!skyrendermasked)
2055 R_DrawSurfaces(SHADERSTAGE_SKY);
2056 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2059 static void gl_surf_start(void)
2063 static void gl_surf_shutdown(void)
2067 static void gl_surf_newmap(void)
2071 void GL_Surf_Init(void)
2074 dlightdivtable[0] = 4194304;
2075 for (i = 1;i < 32768;i++)
2076 dlightdivtable[i] = 4194304 / (i << 7);
2078 Cvar_RegisterVariable(&r_ambient);
2079 Cvar_RegisterVariable(&r_vertexsurfaces);
2080 Cvar_RegisterVariable(&r_dlightmap);
2081 Cvar_RegisterVariable(&r_drawportals);
2082 Cvar_RegisterVariable(&r_testvis);
2083 Cvar_RegisterVariable(&r_floatbuildlightmap);
2084 Cvar_RegisterVariable(&r_detailtextures);
2086 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);