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)
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, true))
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, true))
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, false))
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_BaseVertex(msurface_t *surf)
917 float *outv, *outc, *outst, cl, ca, diff[3];
918 float base[3], scale, f;
922 memset(&m, 0, sizeof(m));
923 if (currentrenderentity->effects & EF_ADDITIVE)
925 m.transparent = true;
926 m.blendfunc1 = GL_SRC_ALPHA;
927 m.blendfunc2 = GL_ONE;
929 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
931 m.transparent = true;
932 m.blendfunc1 = GL_SRC_ALPHA;
933 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
937 m.transparent = false;
938 m.blendfunc1 = GL_ONE;
939 m.blendfunc2 = GL_ZERO;
941 m.depthwrite = false;
942 m.depthdisable = false;
943 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
945 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
947 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
949 ca = currentrenderentity->alpha;
950 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
952 m.numtriangles = mesh->numtriangles;
953 m.numverts = mesh->numverts;
955 if (R_Mesh_Draw_GetBuffer(&m, true))
958 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
960 if (currentrenderentity->effects & EF_FULLBRIGHT)
962 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)
964 softwaretransform(v->v, outv);
966 VectorSubtract(outv, r_origin, diff);
967 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
975 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)
977 softwaretransform(v->v, outv);
979 VectorCopy(base, outc);
985 if (surf->dlightframe == r_framecount)
986 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
988 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
990 if (surf->styles[0] != 255)
992 lm = surf->samples + v->lightmapoffset;
993 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
994 VectorMA(outc, scale, lm, outc);
995 if (surf->styles[1] != 255)
998 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
999 VectorMA(outc, scale, lm, outc);
1000 if (surf->styles[2] != 255)
1003 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1004 VectorMA(outc, scale, lm, outc);
1005 if (surf->styles[3] != 255)
1008 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1009 VectorMA(outc, scale, lm, outc);
1016 VectorSubtract(outv, r_origin, diff);
1017 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1018 VectorScale(outc, f, outc);
1021 VectorScale(outc, cl, outc);
1028 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1032 float *outv, *outc, *outst, cl, ca, diff[3];
1034 rmeshbufferinfo_t m;
1035 memset(&m, 0, sizeof(m));
1036 if (currentrenderentity->effects & EF_ADDITIVE)
1038 m.transparent = true;
1039 m.blendfunc1 = GL_SRC_ALPHA;
1040 m.blendfunc2 = GL_ONE;
1042 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1044 m.transparent = true;
1045 m.blendfunc1 = GL_SRC_ALPHA;
1046 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1050 m.transparent = false;
1051 m.blendfunc1 = GL_ONE;
1052 m.blendfunc2 = GL_ZERO;
1054 m.depthwrite = false;
1055 m.depthdisable = false;
1056 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1057 ca = currentrenderentity->alpha;
1058 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1060 m.numtriangles = mesh->numtriangles;
1061 m.numverts = mesh->numverts;
1063 if (R_Mesh_Draw_GetBuffer(&m, false))
1066 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1067 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)
1069 softwaretransform(v->v, outv);
1073 VectorSubtract(outv, r_origin, diff);
1074 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1077 outc[0] = outc[1] = outc[2] = cl;
1079 outst[0] = v->st[0];
1080 outst[1] = v->st[1];
1086 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1090 float *outv, *outc, *outst, cl, ca, diff[3];
1092 rmeshbufferinfo_t m;
1093 memset(&m, 0, sizeof(m));
1094 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1095 m.blendfunc1 = GL_SRC_ALPHA;
1096 m.blendfunc2 = GL_ONE;
1097 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1098 ca = currentrenderentity->alpha;
1099 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1101 m.numtriangles = mesh->numtriangles;
1102 m.numverts = mesh->numverts;
1104 if (R_Mesh_Draw_GetBuffer(&m, false))
1107 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1108 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)
1110 softwaretransform(v->v, outv);
1114 VectorSubtract(outv, r_origin, diff);
1115 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1118 outc[0] = outc[1] = outc[2] = cl;
1120 outst[0] = v->st[0];
1121 outst[1] = v->st[1];
1127 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1131 float *outv, *outc, *outst, cl, ca, diff[3], f;
1133 rmeshbufferinfo_t m;
1134 memset(&m, 0, sizeof(m));
1135 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1136 m.blendfunc1 = GL_SRC_ALPHA;
1137 m.blendfunc2 = GL_ONE;
1138 ca = currentrenderentity->alpha;
1139 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1141 m.numtriangles = mesh->numtriangles;
1142 m.numverts = mesh->numverts;
1144 if (R_Mesh_Draw_GetBuffer(&m, false))
1147 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1148 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)
1150 softwaretransform(v->v, outv);
1152 VectorSubtract(outv, r_origin, diff);
1153 f = cl * exp(fogdensity/DotProduct(diff, diff));
1154 VectorScale(fogcolor, f, outc);
1156 outst[0] = v->st[0];
1157 outst[1] = v->st[1];
1163 static void RSurfShader_OpaqueWall_Pass_TripleTexCombine(msurface_t *surf)
1167 float *outv, *outc, *outst, *outuv, *outab, cl;
1169 rmeshbufferinfo_t m;
1170 memset(&m, 0, sizeof(m));
1171 m.transparent = false;
1172 m.blendfunc1 = GL_ONE;
1173 m.blendfunc2 = GL_ZERO;
1174 m.depthwrite = false;
1175 m.depthdisable = false;
1176 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1177 m.texrgbscale[0] = 1.0f;
1178 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1179 m.texrgbscale[1] = 4.0f;
1180 m.tex[2] = R_GetTexture(surf->currenttexture->detailtexture);
1181 m.texrgbscale[2] = 2.0f;
1182 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1184 m.numtriangles = mesh->numtriangles;
1185 m.numverts = mesh->numverts;
1187 if (R_Mesh_Draw_GetBuffer(&m, false))
1189 cl = (float) (1 << lightscalebit) * m.colorscale;
1190 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1191 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)
1193 softwaretransform(v->v, outv);
1195 outc[0] = outc[1] = outc[2] = cl;
1197 outst[0] = v->st[0];
1198 outst[1] = v->st[1];
1199 outuv[0] = v->uv[0];
1200 outuv[1] = v->uv[1];
1201 outab[0] = v->ab[0];
1202 outab[1] = v->ab[1];
1208 static void RSurfShader_OpaqueWall_Pass_BaseMTex(msurface_t *surf)
1212 float *outv, *outc, *outst, *outuv, cl;
1214 rmeshbufferinfo_t m;
1215 memset(&m, 0, sizeof(m));
1216 m.transparent = false;
1217 m.blendfunc1 = GL_ONE;
1218 m.blendfunc2 = GL_ZERO;
1219 m.depthwrite = false;
1220 m.depthdisable = false;
1221 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1222 m.tex[1] = R_GetTexture(surf->lightmaptexture);
1223 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1225 m.numtriangles = mesh->numtriangles;
1226 m.numverts = mesh->numverts;
1228 if (R_Mesh_Draw_GetBuffer(&m, true))
1230 cl = (float) (1 << lightscalebit) * m.colorscale;
1231 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1232 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)
1234 softwaretransform(v->v, outv);
1236 outc[0] = outc[1] = outc[2] = cl;
1238 outst[0] = v->st[0];
1239 outst[1] = v->st[1];
1240 outuv[0] = v->uv[0];
1241 outuv[1] = v->uv[1];
1247 static void RSurfShader_OpaqueWall_Pass_BaseTexture(msurface_t *surf)
1251 float *outv, *outc, *outst, cl;
1253 rmeshbufferinfo_t m;
1254 memset(&m, 0, sizeof(m));
1255 m.transparent = false;
1256 m.blendfunc1 = GL_ONE;
1257 m.blendfunc2 = GL_ZERO;
1258 m.depthwrite = false;
1259 m.depthdisable = false;
1260 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1261 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1263 m.numtriangles = mesh->numtriangles;
1264 m.numverts = mesh->numverts;
1266 if (R_Mesh_Draw_GetBuffer(&m, false))
1268 cl = (float) (1 << lightscalebit) * m.colorscale;
1269 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1270 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)
1272 softwaretransform(v->v, outv);
1274 outc[0] = outc[1] = outc[2] = cl;
1276 outst[0] = v->st[0];
1277 outst[1] = v->st[1];
1283 static void RSurfShader_OpaqueWall_Pass_BaseLightmap(msurface_t *surf)
1287 float *outv, *outc, *outuv, cl;
1289 rmeshbufferinfo_t m;
1290 memset(&m, 0, sizeof(m));
1291 m.transparent = false;
1292 m.blendfunc1 = GL_ZERO;
1293 m.blendfunc2 = GL_SRC_COLOR;
1294 m.depthwrite = false;
1295 m.depthdisable = false;
1296 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1297 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1299 m.numtriangles = mesh->numtriangles;
1300 m.numverts = mesh->numverts;
1302 if (R_Mesh_Draw_GetBuffer(&m, true))
1304 cl = (float) (1 << lightscalebit) * m.colorscale;
1305 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1306 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)
1308 softwaretransform(v->v, outv);
1310 outc[0] = outc[1] = outc[2] = cl;
1312 outuv[0] = v->uv[0];
1313 outuv[1] = v->uv[1];
1319 static void RSurfShader_OpaqueWall_Pass_Light(msurface_t *surf)
1323 float *outv, *outc, *outst, cl;
1325 rmeshbufferinfo_t m;
1327 if (surf->dlightframe != r_framecount)
1329 if (currentrenderentity->effects & EF_FULLBRIGHT)
1332 memset(&m, 0, sizeof(m));
1333 m.transparent = false;
1334 m.blendfunc1 = GL_SRC_ALPHA;
1335 m.blendfunc2 = GL_ONE;
1336 m.depthwrite = false;
1337 m.depthdisable = false;
1338 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1339 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1341 if (RSurf_LightCheck(surf->dlightbits, mesh))
1343 m.numtriangles = mesh->numtriangles;
1344 m.numverts = mesh->numverts;
1346 if (R_Mesh_Draw_GetBuffer(&m, true))
1349 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1350 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)
1352 softwaretransform(v->v, outv);
1356 outst[0] = v->st[0];
1357 outst[1] = v->st[1];
1359 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1361 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1362 VectorScale(outc, cl, outc);
1368 static void RSurfShader_OpaqueWall_Pass_Fog(msurface_t *surf)
1372 float *outv, *outc, cl, diff[3], fcolor[3];
1374 rmeshbufferinfo_t m;
1375 memset(&m, 0, sizeof(m));
1376 m.transparent = false;
1377 m.blendfunc1 = GL_SRC_ALPHA;
1378 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1379 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1381 m.numtriangles = mesh->numtriangles;
1382 m.numverts = mesh->numverts;
1384 if (R_Mesh_Draw_GetBuffer(&m, false))
1387 VectorScale(fogcolor, cl, fcolor);
1388 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1389 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1391 softwaretransform(v->v, outv);
1393 VectorCopy(fcolor, outc);
1394 VectorSubtract(outv, r_origin, diff);
1395 outc[3] = exp(fogdensity/DotProduct(diff, diff));
1401 static void RSurfShader_OpaqueWall_Pass_BaseDetail(msurface_t *surf)
1405 float *outv, *outc, *outst;
1407 rmeshbufferinfo_t m;
1408 memset(&m, 0, sizeof(m));
1409 m.transparent = false;
1410 m.blendfunc1 = GL_DST_COLOR;
1411 m.blendfunc2 = GL_SRC_COLOR;
1412 m.depthwrite = false;
1413 m.depthdisable = false;
1414 m.tex[0] = R_GetTexture(surf->currenttexture->detailtexture);
1415 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1417 m.numtriangles = mesh->numtriangles;
1418 m.numverts = mesh->numverts;
1420 if (R_Mesh_Draw_GetBuffer(&m, false))
1422 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1423 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)
1425 softwaretransform(v->v, outv);
1427 outc[0] = outc[1] = outc[2] = outc[3] = 1;
1428 outst[0] = v->ab[0];
1429 outst[1] = v->ab[1];
1435 static void RSurfShader_OpaqueWall_Pass_Glow(msurface_t *surf)
1439 float *outv, *outc, *outst, cl;
1441 rmeshbufferinfo_t m;
1442 memset(&m, 0, sizeof(m));
1443 m.transparent = false;
1444 m.blendfunc1 = GL_SRC_ALPHA;
1445 m.blendfunc2 = GL_ONE;
1446 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1447 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1449 m.numtriangles = mesh->numtriangles;
1450 m.numverts = mesh->numverts;
1452 if (R_Mesh_Draw_GetBuffer(&m, false))
1455 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1456 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)
1458 softwaretransform(v->v, outv);
1460 outc[0] = outc[1] = outc[2] = cl;
1462 outst[0] = v->st[0];
1463 outst[1] = v->st[1];
1469 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1472 for (surf = firstsurf;surf;surf = surf->chain)
1475 RSurfShader_Wall_Pass_BaseFullbright(surf);
1477 for (surf = firstsurf;surf;surf = surf->chain)
1478 if (surf->currenttexture->glowtexture)
1479 RSurfShader_Wall_Pass_Glow(surf);
1481 for (surf = firstsurf;surf;surf = surf->chain)
1482 RSurfShader_Wall_Pass_Fog(surf);
1485 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1488 for (surf = firstsurf;surf;surf = surf->chain)
1491 RSurfShader_Wall_Pass_BaseVertex(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_Lightmap(msurface_t *firstsurf)
1504 if (r_vertexsurfaces.integer || firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1506 for (surf = firstsurf;surf;surf = surf->chain)
1509 RSurfShader_Wall_Pass_BaseVertex(surf);
1511 for (surf = firstsurf;surf;surf = surf->chain)
1512 if (surf->currenttexture->glowtexture)
1513 RSurfShader_Wall_Pass_Glow(surf);
1515 for (surf = firstsurf;surf;surf = surf->chain)
1516 RSurfShader_Wall_Pass_Fog(surf);
1520 if (r_textureunits.integer >= 2)
1522 if (r_textureunits.integer >= 3 && gl_combine.integer && r_detailtextures.integer)
1524 for (surf = firstsurf;surf;surf = surf->chain)
1527 RSurfShader_OpaqueWall_Pass_TripleTexCombine(surf);
1532 for (surf = firstsurf;surf;surf = surf->chain)
1535 RSurfShader_OpaqueWall_Pass_BaseMTex(surf);
1537 if (r_detailtextures.integer)
1538 for (surf = firstsurf;surf;surf = surf->chain)
1539 RSurfShader_OpaqueWall_Pass_BaseDetail(surf);
1544 for (surf = firstsurf;surf;surf = surf->chain)
1547 RSurfShader_OpaqueWall_Pass_BaseTexture(surf);
1549 for (surf = firstsurf;surf;surf = surf->chain)
1550 RSurfShader_OpaqueWall_Pass_BaseLightmap(surf);
1551 if (r_detailtextures.integer)
1552 for (surf = firstsurf;surf;surf = surf->chain)
1553 RSurfShader_OpaqueWall_Pass_BaseDetail(surf);
1555 if (!r_dlightmap.integer)
1556 for (surf = firstsurf;surf;surf = surf->chain)
1557 if (surf->dlightframe == r_framecount)
1558 RSurfShader_OpaqueWall_Pass_Light(surf);
1559 for (surf = firstsurf;surf;surf = surf->chain)
1560 if (surf->currenttexture->glowtexture)
1561 RSurfShader_OpaqueWall_Pass_Glow(surf);
1563 for (surf = firstsurf;surf;surf = surf->chain)
1564 RSurfShader_OpaqueWall_Pass_Fog(surf);
1569 =============================================================
1573 =============================================================
1576 static void R_SolidWorldNode (void)
1578 if (r_viewleaf->contents != CONTENTS_SOLID)
1581 mportal_t *p, *pstack[8192];
1582 msurface_t *surf, **mark, **endmark;
1584 // LordHavoc: portal-passage worldnode; follows portals leading
1585 // outward from viewleaf, if a portal leads offscreen it is not
1586 // followed, in indoor maps this can often cull a great deal of
1587 // geometry away when pvs data is not present (useful with pvs as well)
1590 leaf->worldnodeframe = r_framecount;
1595 leaf->visframe = r_framecount;
1597 if (leaf->nummarksurfaces)
1599 mark = leaf->firstmarksurface;
1600 endmark = mark + leaf->nummarksurfaces;
1604 // make sure surfaces are only processed once
1605 if (surf->worldnodeframe == r_framecount)
1607 surf->worldnodeframe = r_framecount;
1608 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1610 if (surf->flags & SURF_PLANEBACK)
1611 surf->visframe = r_framecount;
1615 if (!(surf->flags & SURF_PLANEBACK))
1616 surf->visframe = r_framecount;
1619 while (mark < endmark);
1622 // follow portals into other leafs
1624 for (;p;p = p->next)
1626 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1629 if (leaf->worldnodeframe != r_framecount)
1631 leaf->worldnodeframe = r_framecount;
1632 if (leaf->contents != CONTENTS_SOLID)
1634 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1636 p->visframe = r_framecount;
1637 pstack[portalstack++] = p;
1641 p = pstack[--portalstack];
1653 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1654 int nodestackpos = 0;
1655 // LordHavoc: recursive descending worldnode; if portals are not
1656 // available, this is a good last resort, can cull large amounts of
1657 // geometry, but is more time consuming than portal-passage and renders
1658 // things behind walls
1661 if (R_NotCulledBox(node->mins, node->maxs))
1663 if (node->numsurfaces)
1665 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1666 if (PlaneDiff (r_origin, node->plane) < 0)
1668 for (;surf < surfend;surf++)
1670 if (surf->flags & SURF_PLANEBACK)
1671 surf->visframe = r_framecount;
1676 for (;surf < surfend;surf++)
1678 if (!(surf->flags & SURF_PLANEBACK))
1679 surf->visframe = r_framecount;
1684 // recurse down the children
1685 if (node->children[0]->contents >= 0)
1687 if (node->children[1]->contents >= 0)
1689 if (nodestackpos < 8192)
1690 nodestack[nodestackpos++] = node->children[1];
1691 node = node->children[0];
1695 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1696 node = node->children[0];
1701 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1702 if (node->children[1]->contents >= 0)
1704 node = node->children[1];
1707 else if (nodestackpos > 0)
1709 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1710 node = nodestack[--nodestackpos];
1715 else if (nodestackpos > 0)
1717 node = nodestack[--nodestackpos];
1723 static int r_portalframecount = 0;
1725 static void R_PVSWorldNode()
1728 mportal_t *p, *pstack[8192];
1729 msurface_t *surf, **mark, **endmark;
1733 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1736 leaf->worldnodeframe = r_framecount;
1741 leaf->visframe = r_framecount;
1743 if (leaf->nummarksurfaces)
1745 mark = leaf->firstmarksurface;
1746 endmark = mark + leaf->nummarksurfaces;
1750 // make sure surfaces are only processed once
1751 if (surf->worldnodeframe == r_framecount)
1753 surf->worldnodeframe = r_framecount;
1754 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1756 if (surf->flags & SURF_PLANEBACK)
1757 surf->visframe = r_framecount;
1761 if (!(surf->flags & SURF_PLANEBACK))
1762 surf->visframe = r_framecount;
1765 while (mark < endmark);
1768 // follow portals into other leafs
1769 for (p = leaf->portals;p;p = p->next)
1771 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1774 if (leaf->worldnodeframe != r_framecount)
1776 leaf->worldnodeframe = r_framecount;
1777 if (leaf->contents != CONTENTS_SOLID)
1779 i = (leaf - cl.worldmodel->leafs) - 1;
1780 if (worldvis[i>>3] & (1<<(i&7)))
1782 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1784 pstack[portalstack++] = p;
1788 p = pstack[--portalstack];
1800 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1801 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1802 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1803 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1804 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1806 int Cshader_count = 5;
1807 Cshader_t *Cshaders[5] =
1809 &Cshader_wall_vertex,
1810 &Cshader_wall_lightmap,
1811 &Cshader_wall_fullbright,
1816 void R_PrepareSurfaces(void)
1818 int i, alttextures, texframe, framecount;
1823 for (i = 0;i < Cshader_count;i++)
1824 Cshaders[i]->chain = NULL;
1826 model = currentrenderentity->model;
1827 alttextures = currentrenderentity->frame != 0;
1828 texframe = (int)(cl.time * 5.0f);
1830 for (i = 0;i < model->nummodelsurfaces;i++)
1832 surf = model->modelsortedsurfaces[i];
1833 if (surf->visframe == r_framecount)
1835 if (surf->insertframe != r_framecount)
1837 surf->insertframe = r_framecount;
1839 t = surf->texinfo->texture;
1842 framecount = t->anim_total[alttextures];
1843 if (framecount >= 2)
1844 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1846 surf->currenttexture = t->anim_frames[alttextures][0];
1849 surf->currenttexture = t;
1852 surf->chain = surf->shader->chain;
1853 surf->shader->chain = surf;
1858 void R_DrawSurfaces (int type)
1863 for (i = 0;i < Cshader_count;i++)
1865 shader = Cshaders[i];
1866 if (shader->chain && shader->shaderfunc[type])
1867 shader->shaderfunc[type](shader->chain);
1871 static float portalpointbuffer[256][3];
1873 void R_DrawPortals(void)
1876 mportal_t *portal, *endportal;
1879 drawportals = r_drawportals.integer;
1881 if (drawportals < 1)
1884 memset(&m, 0, sizeof(m));
1885 m.transparent = true;
1886 m.blendfunc1 = GL_SRC_ALPHA;
1887 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1888 m.vertex = &portalpointbuffer[0][0];
1889 m.vertexstep = sizeof(float[3]);
1891 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
1893 if (portal->visframe == r_portalframecount)
1895 if (portal->numpoints <= 256)
1897 i = portal - cl.worldmodel->portals;
1898 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
1899 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
1900 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
1901 point = portal->points;
1902 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
1904 for (i = portal->numpoints - 1;i >= 0;i--)
1905 VectorCopy(point[i].position, portalpointbuffer[i]);
1909 for (i = 0;i < portal->numpoints;i++)
1910 VectorCopy(point[i].position, portalpointbuffer[i]);
1912 R_Mesh_DrawPolygon(&m, portal->numpoints);
1918 void R_SetupForBModelRendering(void)
1925 // because bmodels can be reused, we have to decide which things to render
1926 // from scratch every time
1928 model = currentrenderentity->model;
1930 softwaretransformforentity (currentrenderentity);
1931 softwareuntransform(r_origin, modelorg);
1933 for (i = 0;i < model->nummodelsurfaces;i++)
1935 surf = model->modelsortedsurfaces[i];
1936 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
1937 surf->visframe = r_framecount;
1939 surf->visframe = -1;
1940 surf->worldnodeframe = -1;
1941 surf->lightframe = -1;
1942 surf->dlightframe = -1;
1943 surf->insertframe = -1;
1947 void R_SetupForWorldRendering(void)
1949 // there is only one instance of the world, but it can be rendered in
1952 currentrenderentity = &cl_entities[0].render;
1953 softwaretransformidentity();
1956 static void R_SurfMarkLights (void)
1961 if (r_dynamic.integer)
1964 if (!r_vertexsurfaces.integer)
1966 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
1968 surf = currentrenderentity->model->modelsortedsurfaces[i];
1969 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
1971 if (surf->cached_dlight
1972 || surf->cached_ambient != r_ambient.value
1973 || surf->cached_lightscalebit != lightscalebit)
1974 R_BuildLightMap(surf, false); // base lighting changed
1975 else if (r_dynamic.integer)
1977 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
1978 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
1979 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
1980 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
1981 R_BuildLightMap(surf, false); // base lighting changed
1982 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
1983 R_BuildLightMap(surf, true); // only dlights
1990 void R_MarkWorldLights(void)
1992 R_SetupForWorldRendering();
2001 void R_DrawWorld (void)
2003 R_SetupForWorldRendering();
2005 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2006 R_SolidWorldNode ();
2016 void R_DrawBrushModelSky (void)
2018 R_SetupForBModelRendering();
2020 R_PrepareSurfaces();
2021 R_DrawSurfaces(SHADERSTAGE_SKY);
2024 void R_DrawBrushModelNormal (void)
2028 // have to flush queue because of possible lightmap reuse
2031 R_SetupForBModelRendering();
2035 R_PrepareSurfaces();
2037 if (!skyrendermasked)
2038 R_DrawSurfaces(SHADERSTAGE_SKY);
2039 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2042 static void gl_surf_start(void)
2046 static void gl_surf_shutdown(void)
2050 static void gl_surf_newmap(void)
2054 void GL_Surf_Init(void)
2057 dlightdivtable[0] = 4194304;
2058 for (i = 1;i < 32768;i++)
2059 dlightdivtable[i] = 4194304 / (i << 7);
2061 Cvar_RegisterVariable(&r_ambient);
2062 Cvar_RegisterVariable(&r_vertexsurfaces);
2063 Cvar_RegisterVariable(&r_dlightmap);
2064 Cvar_RegisterVariable(&r_drawportals);
2065 Cvar_RegisterVariable(&r_testvis);
2066 Cvar_RegisterVariable(&r_floatbuildlightmap);
2067 Cvar_RegisterVariable(&r_detailtextures);
2069 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);