2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_surf.c: surface-related refresh code
24 #define MAX_LIGHTMAP_SIZE 256
26 static unsigned int intblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
27 static float floatblocklights[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*3]; // LordHavoc: *3 for colored lighting
29 static qbyte templight[MAX_LIGHTMAP_SIZE*MAX_LIGHTMAP_SIZE*4];
31 cvar_t r_ambient = {0, "r_ambient", "0"};
32 cvar_t r_vertexsurfaces = {0, "r_vertexsurfaces", "0"};
33 cvar_t r_dlightmap = {CVAR_SAVE, "r_dlightmap", "1"};
34 cvar_t r_drawportals = {0, "r_drawportals", "0"};
35 cvar_t r_testvis = {0, "r_testvis", "0"};
36 cvar_t r_floatbuildlightmap = {0, "r_floatbuildlightmap", "0"};
38 static int dlightdivtable[32768];
40 static int R_IntAddDynamicLights (msurface_t *surf)
42 int sdtable[256], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, red, green, blue, lit, dist2, impacts, impactt, subtract;
44 float dist, impact[3], local[3];
46 // LordHavoc: use 64bit integer... shame it's not very standardized...
47 #if _MSC_VER || __BORLANDC__
55 smax = (surf->extents[0] >> 4) + 1;
56 tmax = (surf->extents[1] >> 4) + 1;
59 for (lnum = 0; lnum < r_numdlights; lnum++)
61 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
62 continue; // not lit by this light
64 softwareuntransform(r_dlight[lnum].origin, local);
65 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
67 // for comparisons to minimum acceptable light
68 // compensate for LIGHTOFFSET
69 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
76 if (surf->plane->type < 3)
78 VectorCopy(local, impact);
79 impact[surf->plane->type] -= dist;
83 impact[0] = local[0] - surf->plane->normal[0] * dist;
84 impact[1] = local[1] - surf->plane->normal[1] * dist;
85 impact[2] = local[2] - surf->plane->normal[2] * dist;
88 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
89 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
91 s = bound(0, impacts, smax * 16) - impacts;
92 t = bound(0, impactt, tmax * 16) - impactt;
93 i = s * s + t * t + dist2;
97 // reduce calculations
98 for (s = 0, i = impacts; s < smax; s++, i -= 16)
99 sdtable[s] = i * i + dist2;
101 maxdist3 = maxdist - dist2;
103 // convert to 8.8 blocklights format
104 red = r_dlight[lnum].light[0];
105 green = r_dlight[lnum].light[1];
106 blue = r_dlight[lnum].light[2];
107 subtract = (int) (r_dlight[lnum].subtract * 4194304.0f);
111 for (t = 0;t < tmax;t++, i -= 16)
114 // make sure some part of it is visible on this line
117 maxdist2 = maxdist - td;
118 for (s = 0;s < smax;s++)
120 if (sdtable[s] < maxdist2)
122 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
125 bl[0] += (red * k) >> 7;
126 bl[1] += (green * k) >> 7;
127 bl[2] += (blue * k) >> 7;
141 static int R_FloatAddDynamicLights (msurface_t *surf)
143 int lnum, s, t, smax, tmax, smax3, lit, impacts, impactt;
144 float sdtable[256], *bl, k, dist, dist2, maxdist, maxdist2, maxdist3, td1, td, red, green, blue, impact[3], local[3], subtract;
148 smax = (surf->extents[0] >> 4) + 1;
149 tmax = (surf->extents[1] >> 4) + 1;
152 for (lnum = 0; lnum < r_numdlights; lnum++)
154 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
155 continue; // not lit by this light
157 softwareuntransform(r_dlight[lnum].origin, local);
158 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
160 // for comparisons to minimum acceptable light
161 // compensate for LIGHTOFFSET
162 maxdist = (int) r_dlight[lnum].cullradius2 + LIGHTOFFSET;
165 dist2 += LIGHTOFFSET;
166 if (dist2 >= maxdist)
169 if (surf->plane->type < 3)
171 VectorCopy(local, impact);
172 impact[surf->plane->type] -= dist;
176 impact[0] = local[0] - surf->plane->normal[0] * dist;
177 impact[1] = local[1] - surf->plane->normal[1] * dist;
178 impact[2] = local[2] - surf->plane->normal[2] * dist;
181 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
182 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
184 td = bound(0, impacts, smax * 16) - impacts;
185 td1 = bound(0, impactt, tmax * 16) - impactt;
186 td = td * td + td1 * td1 + dist2;
190 // reduce calculations
191 for (s = 0, td1 = impacts; s < smax; s++, td1 -= 16.0f)
192 sdtable[s] = td1 * td1 + dist2;
194 maxdist3 = maxdist - dist2;
196 // convert to 8.8 blocklights format
197 red = r_dlight[lnum].light[0];
198 green = r_dlight[lnum].light[1];
199 blue = r_dlight[lnum].light[2];
200 subtract = r_dlight[lnum].subtract * 32768.0f;
201 bl = floatblocklights;
204 for (t = 0;t < tmax;t++, td1 -= 16.0f)
207 // make sure some part of it is visible on this line
210 maxdist2 = maxdist - td;
211 for (s = 0;s < smax;s++)
213 if (sdtable[s] < maxdist2)
215 k = (32768.0f / (sdtable[s] + td)) - subtract;
235 Combine and scale multiple lightmaps into the 8.8 format in blocklights
238 static void R_BuildLightMap (msurface_t *surf, int dlightchanged)
240 if (!r_floatbuildlightmap.integer)
242 int smax, tmax, i, j, size, size3, shift, maps, stride, l;
243 unsigned int *bl, scale;
244 qbyte *lightmap, *out, *stain;
246 // update cached lighting info
247 surf->cached_dlight = 0;
248 surf->cached_lightscalebit = lightscalebit;
249 surf->cached_ambient = r_ambient.value;
250 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
251 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
252 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
253 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
255 smax = (surf->extents[0]>>4)+1;
256 tmax = (surf->extents[1]>>4)+1;
259 lightmap = surf->samples;
261 // set to full bright if no light data
263 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
265 for (i = 0;i < size3;i++)
271 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
274 for (i = 0;i < size3;i++)
278 memset(bl, 0, size*3*sizeof(unsigned int));
280 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
282 surf->cached_dlight = R_IntAddDynamicLights(surf);
283 if (surf->cached_dlight)
285 else if (dlightchanged)
286 return; // don't upload if only updating dlights and none mattered
289 // add all the lightmaps
293 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
294 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
295 bl[i] += lightmap[i] * scale;
299 stain = surf->stainsamples;
302 // deal with lightmap brightness scale
303 shift = 7 + lightscalebit + 8;
304 if (currentrenderentity->model->lightmaprgba)
306 stride = (surf->lightmaptexturestride - smax) * 4;
307 for (i = 0;i < tmax;i++, out += stride)
309 for (j = 0;j < smax;j++)
311 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
312 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
313 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
320 stride = (surf->lightmaptexturestride - smax) * 3;
321 for (i = 0;i < tmax;i++, out += stride)
323 for (j = 0;j < smax;j++)
325 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
326 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
327 l = (*bl++ * *stain++) >> shift;*out++ = min(l, 255);
332 R_UpdateTexture(surf->lightmaptexture, templight);
336 int smax, tmax, i, j, size, size3, maps, stride, l;
338 qbyte *lightmap, *out, *stain;
340 // update cached lighting info
341 surf->cached_dlight = 0;
342 surf->cached_lightscalebit = lightscalebit;
343 surf->cached_ambient = r_ambient.value;
344 surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
345 surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
346 surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
347 surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
349 smax = (surf->extents[0]>>4)+1;
350 tmax = (surf->extents[1]>>4)+1;
353 lightmap = surf->samples;
355 // set to full bright if no light data
356 bl = floatblocklights;
357 if ((currentrenderentity->effects & EF_FULLBRIGHT) || !currentrenderentity->model->lightdata)
360 j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
365 for (i = 0;i < size3;i++)
369 memset(bl, 0, size*3*sizeof(float));
371 if (surf->dlightframe == r_framecount && r_dlightmap.integer)
373 surf->cached_dlight = R_FloatAddDynamicLights(surf);
374 if (surf->cached_dlight)
376 else if (dlightchanged)
377 return; // don't upload if only updating dlights and none mattered
380 // add all the lightmaps
383 bl = floatblocklights;
384 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
385 for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
386 bl[i] += lightmap[i] * scale;
389 stain = surf->stainsamples;
390 bl = floatblocklights;
392 // deal with lightmap brightness scale
393 scale = 1.0f / (1 << (7 + lightscalebit + 8));
394 if (currentrenderentity->model->lightmaprgba)
396 stride = (surf->lightmaptexturestride - smax) * 4;
397 for (i = 0;i < tmax;i++, out += stride)
399 for (j = 0;j < smax;j++)
401 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
402 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
403 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
410 stride = (surf->lightmaptexturestride - smax) * 3;
411 for (i = 0;i < tmax;i++, out += stride)
413 for (j = 0;j < smax;j++)
415 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
416 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
417 l = *bl++ * *stain++ * scale;*out++ = min(l, 255);
422 R_UpdateTexture(surf->lightmaptexture, templight);
426 void R_StainNode (mnode_t *node, model_t *model, vec3_t origin, float radius, int icolor[8])
429 msurface_t *surf, *endsurf;
430 int sdtable[256], td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, smax3, dist2, impacts, impactt, subtract, a, stained, cr, cg, cb, ca, ratio;
433 // LordHavoc: use 64bit integer... shame it's not very standardized...
434 #if _MSC_VER || __BORLANDC__
441 // for comparisons to minimum acceptable light
442 // compensate for 256 offset
443 maxdist = radius * radius + 256.0f;
445 // clamp radius to avoid exceeding 32768 entry division table
446 if (maxdist > 4194304)
449 subtract = (int) ((1.0f / maxdist) * 4194304.0f);
452 if (node->contents < 0)
454 ndist = PlaneDiff(origin, node->plane);
457 node = node->children[0];
462 node = node->children[1];
466 dist2 = ndist * ndist + 256.0f;
469 maxdist3 = maxdist - dist2;
471 if (node->plane->type < 3)
473 VectorCopy(origin, impact);
474 impact[node->plane->type] -= ndist;
478 impact[0] = origin[0] - node->plane->normal[0] * ndist;
479 impact[1] = origin[1] - node->plane->normal[1] * ndist;
480 impact[2] = origin[2] - node->plane->normal[2] * ndist;
483 for (surf = model->surfaces + node->firstsurface, endsurf = surf + node->numsurfaces;surf < endsurf;surf++)
485 if (surf->stainsamples)
487 smax = (surf->extents[0] >> 4) + 1;
488 tmax = (surf->extents[1] >> 4) + 1;
490 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
491 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
493 s = bound(0, impacts, smax * 16) - impacts;
494 t = bound(0, impactt, tmax * 16) - impactt;
495 i = s * s + t * t + dist2;
499 // reduce calculations
500 for (s = 0, i = impacts; s < smax; s++, i -= 16)
501 sdtable[s] = i * i + dist2;
503 // convert to 8.8 blocklights format
504 bl = surf->stainsamples;
509 for (t = 0;t < tmax;t++, i -= 16)
512 // make sure some part of it is visible on this line
515 maxdist2 = maxdist - td;
516 for (s = 0;s < smax;s++)
518 if (sdtable[s] < maxdist2)
520 k = dlightdivtable[(sdtable[s] + td) >> 7] - subtract;
523 ratio = rand() & 255;
524 ca = (((icolor[7] - icolor[3]) * ratio) >> 8) + icolor[3];
528 a = bound(0, a, 256);
529 cr = (((icolor[4] - icolor[0]) * ratio) >> 8) + icolor[0];
530 cg = (((icolor[5] - icolor[1]) * ratio) >> 8) + icolor[1];
531 cb = (((icolor[6] - icolor[2]) * ratio) >> 8) + icolor[2];
532 bl[0] = (qbyte) ((((cr - (int) bl[0]) * a) >> 8) + (int) bl[0]);
533 bl[1] = (qbyte) ((((cg - (int) bl[1]) * a) >> 8) + (int) bl[1]);
534 bl[2] = (qbyte) ((((cb - (int) bl[2]) * a) >> 8) + (int) bl[2]);
545 // force lightmap upload
547 surf->cached_dlight = true;
552 if (node->children[0]->contents >= 0)
554 if (node->children[1]->contents >= 0)
556 R_StainNode(node->children[0], model, origin, radius, icolor);
557 node = node->children[1];
562 node = node->children[0];
566 else if (node->children[1]->contents >= 0)
568 node = node->children[1];
573 void R_Stain (vec3_t origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
576 entity_render_t *ent;
588 model = cl.worldmodel;
589 softwaretransformidentity();
590 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, origin, radius, icolor);
592 // look for embedded bmodels
593 for (n = 0;n < cl_num_brushmodel_entities;n++)
595 ent = cl_brushmodel_entities[n];
597 if (model && model->name[0] == '*')
599 Mod_CheckLoaded(model);
600 if (model->type == mod_brush)
602 softwaretransformforentity(ent);
603 softwareuntransform(origin, org);
604 R_StainNode(model->nodes + model->hulls[0].firstclipnode, model, org, radius, icolor);
612 =============================================================
616 =============================================================
619 static void RSurfShader_Sky(msurface_t *firstsurf)
626 float cr, cg, cb, ca;
629 // LordHavoc: HalfLife maps have freaky skypolys...
630 if (currentrenderentity->model->ishlbsp)
635 skyrendernow = false;
639 for (surf = firstsurf;surf;surf = surf->chain)
641 // draw depth-only polys
642 memset(&m, 0, sizeof(m));
643 m.transparent = false;
646 m.blendfunc1 = GL_ZERO;
647 m.blendfunc2 = GL_ONE;
652 m.blendfunc1 = GL_ONE;
653 m.blendfunc2 = GL_ZERO;
654 m.depthwrite = false;
656 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
658 m.numtriangles = mesh->numtriangles;
659 m.numverts = mesh->numverts;
660 if (R_Mesh_Draw_GetBuffer(&m))
662 cr = fogcolor[0] * m.colorscale;
663 cg = fogcolor[1] * m.colorscale;
664 cb = fogcolor[2] * m.colorscale;
666 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
667 for (i = 0, v = mesh->vertex, outv = m.vertex;i < m.numverts;i++, v++, outv += 4, outc += 4)
669 softwaretransform(v->v, outv);
671 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
683 static int RSurf_LightSeparate(int *dlightbits, int numverts, float *vert, float *color)
686 int i, l, lit = false;
689 for (l = 0;l < r_numdlights;l++)
691 if (dlightbits[l >> 5] & (1 << (l & 31)))
694 // FIXME: support softwareuntransform here and make bmodels use hardware transform?
695 VectorCopy(rd->origin, lightorigin);
696 for (i = 0, v = vert, c = color;i < numverts;i++, v += 4, c += 4)
698 f = VectorDistance2(v, lightorigin) + LIGHTOFFSET;
699 if (f < rd->cullradius2)
701 f = (1.0f / f) - rd->subtract;
702 VectorMA(c, f, rd->light, c);
711 // note: this untransforms lights to do the checking,
712 // and takes surf->mesh->vertex data
713 static int RSurf_LightCheck(int *dlightbits, surfmesh_t *mesh)
719 for (l = 0;l < r_numdlights;l++)
721 if (dlightbits[l >> 5] & (1 << (l & 31)))
724 softwareuntransform(rd->origin, lightorigin);
725 for (i = 0, sv = mesh->vertex;i < mesh->numverts;i++, sv++)
726 if (VectorDistance2(sv->v, lightorigin) < rd->cullradius2)
733 static void RSurfShader_Water_Pass_Base(msurface_t *surf)
737 float *outv, *outc, *outst, cl, diff[3];
738 float base[3], scale, f;
742 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
743 memset(&m, 0, sizeof(m));
744 if (currentrenderentity->effects & EF_ADDITIVE)
746 m.transparent = true;
747 m.blendfunc1 = GL_SRC_ALPHA;
748 m.blendfunc2 = GL_ONE;
750 else if (surf->currenttexture->fogtexture != NULL || alpha < 1)
752 m.transparent = true;
753 m.blendfunc1 = GL_SRC_ALPHA;
754 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
758 m.transparent = false;
759 m.blendfunc1 = GL_ONE;
760 m.blendfunc2 = GL_ZERO;
762 m.depthwrite = false;
763 m.depthdisable = false;
764 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
765 if (surf->flags & SURF_DRAWFULLBRIGHT || currentrenderentity->effects & EF_FULLBRIGHT)
767 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
769 m.numtriangles = mesh->numtriangles;
770 m.numverts = mesh->numverts;
771 if (R_Mesh_Draw_GetBuffer(&m))
773 base[0] = base[1] = base[2] = 1.0f * m.colorscale;
774 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
775 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)
777 softwaretransform(v->v, outv);
779 VectorCopy(base, outc);
785 VectorSubtract(outv, r_origin, diff);
786 f = 1 - exp(fogdensity/DotProduct(diff, diff));
787 VectorScale(outc, f, outc);
795 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
796 base[0] = base[1] = base[2] = (r_ambient.value * (1.0f / 64.0f) + ((surf->flags & SURF_LIGHTMAP) ? 0 : 0.5f));
797 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
799 m.numtriangles = mesh->numtriangles;
800 m.numverts = mesh->numverts;
801 if (R_Mesh_Draw_GetBuffer(&m))
804 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
805 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)
807 softwaretransform(v->v, outv);
809 VectorCopy(base, outc);
814 if (surf->dlightframe == r_framecount)
815 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
816 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
818 if (surf->flags & SURF_LIGHTMAP)
819 if (surf->styles[0] != 255)
821 lm = surf->samples + v->lightmapoffset;
822 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
823 VectorMA(outc, scale, lm, outc);
824 if (surf->styles[1] != 255)
827 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
828 VectorMA(outc, scale, lm, outc);
829 if (surf->styles[2] != 255)
832 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
833 VectorMA(outc, scale, lm, outc);
834 if (surf->styles[3] != 255)
837 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
838 VectorMA(outc, scale, lm, outc);
845 VectorSubtract(outv, r_origin, diff);
846 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
847 VectorScale(outc, f, outc);
850 VectorScale(outc, cl, outc);
857 static void RSurfShader_Water_Pass_Fog(msurface_t *surf)
861 float *outv, *outc, *outst, diff[3];
865 float alpha = currentrenderentity->alpha * (surf->flags & SURF_DRAWNOALPHA ? 1 : r_wateralpha.value);
866 memset(&m, 0, sizeof(m));
867 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || alpha < 1;
868 m.blendfunc1 = GL_SRC_ALPHA;
869 m.blendfunc2 = GL_ONE;
870 m.depthwrite = false;
871 m.depthdisable = false;
872 m.tex[0] = R_GetTexture(surf->currenttexture->fogtexture);
873 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
875 m.numtriangles = mesh->numtriangles;
876 m.numverts = mesh->numverts;
877 if (R_Mesh_Draw_GetBuffer(&m))
879 VectorScale(fogcolor, m.colorscale, base);
880 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
881 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
883 softwaretransform(v->v, outv);
885 VectorSubtract(outv, r_origin, diff);
886 f = exp(fogdensity/DotProduct(diff, diff));
887 VectorScale(base, f, outc);
892 for (i = 0, v = mesh->vertex, outst = m.texcoords[0];i < m.numverts;i++, v++, outv += 4, outst += 2)
902 static void RSurfShader_Water(msurface_t *firstsurf)
905 for (surf = firstsurf;surf;surf = surf->chain)
906 RSurfShader_Water_Pass_Base(surf);
908 for (surf = firstsurf;surf;surf = surf->chain)
909 RSurfShader_Water_Pass_Fog(surf);
912 static void RSurfShader_Wall_Pass_BaseMTex(msurface_t *surf)
916 float *outv, *outc, *outst, *outuv, cl, ca, diff[3];
919 memset(&m, 0, sizeof(m));
920 if (currentrenderentity->effects & EF_ADDITIVE)
922 m.transparent = true;
923 m.blendfunc1 = GL_SRC_ALPHA;
924 m.blendfunc2 = GL_ONE;
926 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
928 m.transparent = true;
929 m.blendfunc1 = GL_SRC_ALPHA;
930 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
934 m.transparent = false;
935 m.blendfunc1 = GL_ONE;
936 m.blendfunc2 = GL_ZERO;
938 m.depthwrite = false;
939 m.depthdisable = false;
940 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
941 m.tex[1] = R_GetTexture(surf->lightmaptexture);
942 ca = currentrenderentity->alpha;
943 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
945 m.numtriangles = mesh->numtriangles;
946 m.numverts = mesh->numverts;
948 if (R_Mesh_Draw_GetBuffer(&m))
950 cl = (float) (1 << lightscalebit) * m.colorscale;
951 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
954 if (softwaretransform_complexity)
956 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)
958 softwaretransform(v->v, outv);
960 VectorSubtract(outv, r_origin, diff);
961 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
971 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)
973 VectorCopy(v->v, outv);
975 VectorSubtract(outv, r_origin, diff);
976 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
987 if (softwaretransform_complexity)
989 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)
991 softwaretransform(v->v, outv);
993 outc[0] = outc[1] = outc[2] = cl;
1003 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)
1005 VectorCopy(v->v, outv);
1007 outc[0] = outc[1] = outc[2] = cl;
1009 outst[0] = v->st[0];
1010 outst[1] = v->st[1];
1011 outuv[0] = v->uv[0];
1012 outuv[1] = v->uv[1];
1020 static void RSurfShader_Wall_Pass_BaseTexture(msurface_t *surf)
1024 float *outv, *outc, *outst, cl, ca;
1026 rmeshbufferinfo_t m;
1027 memset(&m, 0, sizeof(m));
1028 m.transparent = false;
1029 m.blendfunc1 = GL_ONE;
1030 m.blendfunc2 = GL_ZERO;
1031 m.depthwrite = false;
1032 m.depthdisable = false;
1033 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1034 ca = currentrenderentity->alpha;
1035 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1037 m.numtriangles = mesh->numtriangles;
1038 m.numverts = mesh->numverts;
1040 if (R_Mesh_Draw_GetBuffer(&m))
1042 cl = (float) (1 << lightscalebit) * m.colorscale;
1043 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1044 if (softwaretransform_complexity)
1046 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)
1048 softwaretransform(v->v, outv);
1050 outc[0] = outc[1] = outc[2] = cl;
1052 outst[0] = v->st[0];
1053 outst[1] = v->st[1];
1058 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)
1060 VectorCopy(v->v, outv);
1062 outc[0] = outc[1] = outc[2] = cl;
1064 outst[0] = v->st[0];
1065 outst[1] = v->st[1];
1072 static void RSurfShader_Wall_Pass_BaseLightmap(msurface_t *surf)
1076 float *outv, *outc, *outuv, cl, ca, diff[3];
1078 rmeshbufferinfo_t m;
1079 memset(&m, 0, sizeof(m));
1080 m.transparent = false;
1081 m.blendfunc1 = GL_ZERO;
1082 m.blendfunc2 = GL_SRC_COLOR;
1083 m.depthwrite = false;
1084 m.depthdisable = false;
1085 m.tex[0] = R_GetTexture(surf->lightmaptexture);
1086 ca = currentrenderentity->alpha;
1087 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1089 m.numtriangles = mesh->numtriangles;
1090 m.numverts = mesh->numverts;
1092 if (R_Mesh_Draw_GetBuffer(&m))
1094 cl = (float) (1 << lightscalebit) * m.colorscale;
1095 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1098 if (softwaretransform_complexity)
1100 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)
1102 softwaretransform(v->v, outv);
1104 VectorSubtract(outv, r_origin, diff);
1105 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1107 outuv[0] = v->uv[0];
1108 outuv[1] = v->uv[1];
1113 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)
1115 VectorCopy(v->v, outv);
1117 VectorSubtract(outv, r_origin, diff);
1118 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1120 outuv[0] = v->uv[0];
1121 outuv[1] = v->uv[1];
1127 if (softwaretransform_complexity)
1129 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)
1131 softwaretransform(v->v, outv);
1133 outc[0] = outc[1] = outc[2] = cl;
1135 outuv[0] = v->uv[0];
1136 outuv[1] = v->uv[1];
1141 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)
1143 VectorCopy(v->v, outv);
1145 outc[0] = outc[1] = outc[2] = cl;
1147 outuv[0] = v->uv[0];
1148 outuv[1] = v->uv[1];
1156 static void RSurfShader_Wall_Pass_BaseVertex(msurface_t *surf)
1160 float *outv, *outc, *outst, cl, ca, diff[3];
1161 float base[3], scale, f;
1164 rmeshbufferinfo_t m;
1165 memset(&m, 0, sizeof(m));
1166 if (currentrenderentity->effects & EF_ADDITIVE)
1168 m.transparent = true;
1169 m.blendfunc1 = GL_SRC_ALPHA;
1170 m.blendfunc2 = GL_ONE;
1172 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1174 m.transparent = true;
1175 m.blendfunc1 = GL_SRC_ALPHA;
1176 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1180 m.transparent = false;
1181 m.blendfunc1 = GL_ONE;
1182 m.blendfunc2 = GL_ZERO;
1184 m.depthwrite = false;
1185 m.depthdisable = false;
1186 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1188 size3 = ((surf->extents[0]>>4)+1)*((surf->extents[1]>>4)+1)*3;
1190 base[0] = base[1] = base[2] = currentrenderentity->effects & EF_FULLBRIGHT ? 2.0f : r_ambient.value * (1.0f / 64.0f);
1192 ca = currentrenderentity->alpha;
1193 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1195 m.numtriangles = mesh->numtriangles;
1196 m.numverts = mesh->numverts;
1198 if (R_Mesh_Draw_GetBuffer(&m))
1201 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1203 if (currentrenderentity->effects & EF_FULLBRIGHT)
1205 if (softwaretransform_complexity)
1207 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)
1209 softwaretransform(v->v, outv);
1211 VectorSubtract(outv, r_origin, diff);
1212 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1214 outst[0] = v->st[0];
1215 outst[1] = v->st[1];
1220 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)
1222 VectorCopy(v->v, outv);
1224 VectorSubtract(outv, r_origin, diff);
1225 outc[0] = outc[1] = outc[2] = 2.0f * cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1227 outst[0] = v->st[0];
1228 outst[1] = v->st[1];
1234 if (softwaretransform_complexity)
1236 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)
1238 softwaretransform(v->v, outv);
1240 VectorCopy(base, outc);
1242 outst[0] = v->st[0];
1243 outst[1] = v->st[1];
1248 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)
1250 VectorCopy(v->v, outv);
1252 VectorCopy(base, outc);
1254 outst[0] = v->st[0];
1255 outst[1] = v->st[1];
1259 if (surf->dlightframe == r_framecount)
1260 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1262 for (i = 0, v = mesh->vertex, outv = m.vertex, outc = m.color;i < m.numverts;i++, v++, outv += 4, outc += 4)
1264 if (surf->styles[0] != 255)
1266 lm = surf->samples + v->lightmapoffset;
1267 scale = d_lightstylevalue[surf->styles[0]] * (1.0f / 32768.0f);
1268 VectorMA(outc, scale, lm, outc);
1269 if (surf->styles[1] != 255)
1272 scale = d_lightstylevalue[surf->styles[1]] * (1.0f / 32768.0f);
1273 VectorMA(outc, scale, lm, outc);
1274 if (surf->styles[2] != 255)
1277 scale = d_lightstylevalue[surf->styles[2]] * (1.0f / 32768.0f);
1278 VectorMA(outc, scale, lm, outc);
1279 if (surf->styles[3] != 255)
1282 scale = d_lightstylevalue[surf->styles[3]] * (1.0f / 32768.0f);
1283 VectorMA(outc, scale, lm, outc);
1290 VectorSubtract(outv, r_origin, diff);
1291 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1292 VectorScale(outc, f, outc);
1295 VectorScale(outc, cl, outc);
1302 static void RSurfShader_Wall_Pass_BaseFullbright(msurface_t *surf)
1306 float *outv, *outc, *outst, cl, ca, diff[3];
1308 rmeshbufferinfo_t m;
1309 memset(&m, 0, sizeof(m));
1310 if (currentrenderentity->effects & EF_ADDITIVE)
1312 m.transparent = true;
1313 m.blendfunc1 = GL_SRC_ALPHA;
1314 m.blendfunc2 = GL_ONE;
1316 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1318 m.transparent = true;
1319 m.blendfunc1 = GL_SRC_ALPHA;
1320 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1324 m.transparent = false;
1325 m.blendfunc1 = GL_ONE;
1326 m.blendfunc2 = GL_ZERO;
1328 m.depthwrite = false;
1329 m.depthdisable = false;
1330 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1331 ca = currentrenderentity->alpha;
1332 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1334 m.numtriangles = mesh->numtriangles;
1335 m.numverts = mesh->numverts;
1337 if (R_Mesh_Draw_GetBuffer(&m))
1340 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1343 if (softwaretransform_complexity)
1345 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)
1347 softwaretransform(v->v, outv);
1349 VectorSubtract(outv, r_origin, diff);
1350 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1352 outst[0] = v->st[0];
1353 outst[1] = v->st[1];
1358 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)
1360 VectorCopy(v->v, outv);
1362 VectorSubtract(outv, r_origin, diff);
1363 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1365 outst[0] = v->st[0];
1366 outst[1] = v->st[1];
1372 if (softwaretransform_complexity)
1374 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)
1376 softwaretransform(v->v, outv);
1378 outc[0] = outc[1] = outc[2] = cl;
1380 outst[0] = v->st[0];
1381 outst[1] = v->st[1];
1386 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)
1388 VectorCopy(v->v, outv);
1390 outc[0] = outc[1] = outc[2] = cl;
1392 outst[0] = v->st[0];
1393 outst[1] = v->st[1];
1401 static void RSurfShader_Wall_Pass_Light(msurface_t *surf)
1405 float *outv, *outc, *outst, cl, ca, diff[3], f;
1407 rmeshbufferinfo_t m;
1409 if (surf->dlightframe != r_framecount)
1411 if (currentrenderentity->effects & EF_FULLBRIGHT)
1414 memset(&m, 0, sizeof(m));
1415 if (currentrenderentity->effects & EF_ADDITIVE)
1417 m.transparent = true;
1418 m.blendfunc1 = GL_SRC_ALPHA;
1419 m.blendfunc2 = GL_ONE;
1421 else if (surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1)
1423 m.transparent = true;
1424 m.blendfunc1 = GL_SRC_ALPHA;
1425 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
1429 m.transparent = false;
1430 m.blendfunc1 = GL_ONE;
1431 m.blendfunc2 = GL_ZERO;
1433 m.depthwrite = false;
1434 m.depthdisable = false;
1435 m.tex[0] = R_GetTexture(surf->currenttexture->texture);
1436 ca = currentrenderentity->alpha;
1437 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1439 if (RSurf_LightCheck(surf->dlightbits, mesh))
1441 m.numtriangles = mesh->numtriangles;
1442 m.numverts = mesh->numverts;
1444 if (R_Mesh_Draw_GetBuffer(&m))
1447 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1448 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)
1450 softwaretransform(v->v, outv);
1454 outst[0] = v->st[0];
1455 outst[1] = v->st[1];
1457 RSurf_LightSeparate(surf->dlightbits, m.numverts, m.vertex, m.color);
1460 for (i = 0, outv = m.vertex, outc = m.color;i < m.numverts;i++, outv += 4, outc += 4)
1462 VectorSubtract(outv, r_origin, diff);
1463 f = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1464 VectorScale(outc, f, outc);
1468 for (i = 0, outc = m.color;i < m.numverts;i++, outc += 4)
1469 VectorScale(outc, cl, outc);
1475 static void RSurfShader_Wall_Pass_Glow(msurface_t *surf)
1479 float *outv, *outc, *outst, cl, ca, diff[3];
1481 rmeshbufferinfo_t m;
1482 memset(&m, 0, sizeof(m));
1483 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1484 m.blendfunc1 = GL_SRC_ALPHA;
1485 m.blendfunc2 = GL_ONE;
1486 m.tex[0] = R_GetTexture(surf->currenttexture->glowtexture);
1487 ca = currentrenderentity->alpha;
1488 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1490 m.numtriangles = mesh->numtriangles;
1491 m.numverts = mesh->numverts;
1493 if (R_Mesh_Draw_GetBuffer(&m))
1496 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1499 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)
1501 softwaretransform(v->v, outv);
1503 VectorSubtract(outv, r_origin, diff);
1504 outc[0] = outc[1] = outc[2] = cl * (1 - exp(fogdensity/DotProduct(diff, diff)));
1506 outst[0] = v->st[0];
1507 outst[1] = v->st[1];
1512 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)
1514 softwaretransform(v->v, outv);
1516 outc[0] = outc[1] = outc[2] = cl;
1518 outst[0] = v->st[0];
1519 outst[1] = v->st[1];
1526 static void RSurfShader_Wall_Pass_Fog(msurface_t *surf)
1530 float *outv, *outc, *outst, cl, ca, diff[3], f;
1532 rmeshbufferinfo_t m;
1533 memset(&m, 0, sizeof(m));
1534 m.transparent = currentrenderentity->effects & EF_ADDITIVE || surf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1;
1535 m.blendfunc1 = GL_SRC_ALPHA;
1536 m.blendfunc2 = GL_ONE;
1537 ca = currentrenderentity->alpha;
1538 for (mesh = surf->mesh;mesh;mesh = mesh->chain)
1540 m.numtriangles = mesh->numtriangles;
1541 m.numverts = mesh->numverts;
1543 if (R_Mesh_Draw_GetBuffer(&m))
1546 memcpy(m.index, mesh->index, m.numtriangles * sizeof(int[3]));
1547 if (softwaretransform_complexity)
1549 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)
1551 softwaretransform(v->v, outv);
1553 VectorSubtract(outv, r_origin, diff);
1554 f = cl * exp(fogdensity/DotProduct(diff, diff));
1555 VectorScale(fogcolor, f, outc);
1557 outst[0] = v->st[0];
1558 outst[1] = v->st[1];
1563 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)
1565 VectorCopy(v->v, outv);
1567 VectorSubtract(outv, r_origin, diff);
1568 VectorSubtract(outv, r_origin, diff);
1569 f = cl * exp(fogdensity/DotProduct(diff, diff));
1570 VectorScale(fogcolor, f, outc);
1572 outst[0] = v->st[0];
1573 outst[1] = v->st[1];
1580 static void RSurfShader_Wall_Fullbright(msurface_t *firstsurf)
1583 for (surf = firstsurf;surf;surf = surf->chain)
1586 RSurfShader_Wall_Pass_BaseFullbright(surf);
1588 for (surf = firstsurf;surf;surf = surf->chain)
1589 if (surf->currenttexture->glowtexture)
1590 RSurfShader_Wall_Pass_Glow(surf);
1592 for (surf = firstsurf;surf;surf = surf->chain)
1593 RSurfShader_Wall_Pass_Fog(surf);
1596 static void RSurfShader_Wall_Vertex(msurface_t *firstsurf)
1599 for (surf = firstsurf;surf;surf = surf->chain)
1602 RSurfShader_Wall_Pass_BaseVertex(surf);
1604 for (surf = firstsurf;surf;surf = surf->chain)
1605 if (surf->currenttexture->glowtexture)
1606 RSurfShader_Wall_Pass_Glow(surf);
1608 for (surf = firstsurf;surf;surf = surf->chain)
1609 RSurfShader_Wall_Pass_Fog(surf);
1612 static void RSurfShader_Wall_Lightmap(msurface_t *firstsurf)
1615 if (r_vertexsurfaces.integer)
1617 for (surf = firstsurf;surf;surf = surf->chain)
1620 RSurfShader_Wall_Pass_BaseVertex(surf);
1622 for (surf = firstsurf;surf;surf = surf->chain)
1623 if (surf->currenttexture->glowtexture)
1624 RSurfShader_Wall_Pass_Glow(surf);
1626 for (surf = firstsurf;surf;surf = surf->chain)
1627 RSurfShader_Wall_Pass_Fog(surf);
1629 else if (r_multitexture.integer)
1631 if (r_dlightmap.integer)
1633 for (surf = firstsurf;surf;surf = surf->chain)
1636 RSurfShader_Wall_Pass_BaseMTex(surf);
1638 for (surf = firstsurf;surf;surf = surf->chain)
1639 if (surf->currenttexture->glowtexture)
1640 RSurfShader_Wall_Pass_Glow(surf);
1642 for (surf = firstsurf;surf;surf = surf->chain)
1643 RSurfShader_Wall_Pass_Fog(surf);
1647 for (surf = firstsurf;surf;surf = surf->chain)
1650 RSurfShader_Wall_Pass_BaseMTex(surf);
1652 for (surf = firstsurf;surf;surf = surf->chain)
1653 if (surf->dlightframe == r_framecount)
1654 RSurfShader_Wall_Pass_Light(surf);
1655 for (surf = firstsurf;surf;surf = surf->chain)
1656 if (surf->currenttexture->glowtexture)
1657 RSurfShader_Wall_Pass_Glow(surf);
1659 for (surf = firstsurf;surf;surf = surf->chain)
1660 RSurfShader_Wall_Pass_Fog(surf);
1663 else if (firstsurf->currenttexture->fogtexture != NULL || currentrenderentity->alpha != 1 || currentrenderentity->effects & EF_ADDITIVE)
1665 for (surf = firstsurf;surf;surf = surf->chain)
1668 RSurfShader_Wall_Pass_BaseVertex(surf);
1670 for (surf = firstsurf;surf;surf = surf->chain)
1671 if (surf->currenttexture->glowtexture)
1672 RSurfShader_Wall_Pass_Glow(surf);
1674 for (surf = firstsurf;surf;surf = surf->chain)
1675 RSurfShader_Wall_Pass_Fog(surf);
1679 if (r_dlightmap.integer)
1681 for (surf = firstsurf;surf;surf = surf->chain)
1684 RSurfShader_Wall_Pass_BaseTexture(surf);
1686 for (surf = firstsurf;surf;surf = surf->chain)
1687 RSurfShader_Wall_Pass_BaseLightmap(surf);
1688 for (surf = firstsurf;surf;surf = surf->chain)
1689 if (surf->currenttexture->glowtexture)
1690 RSurfShader_Wall_Pass_Glow(surf);
1692 for (surf = firstsurf;surf;surf = surf->chain)
1693 RSurfShader_Wall_Pass_Fog(surf);
1697 for (surf = firstsurf;surf;surf = surf->chain)
1700 RSurfShader_Wall_Pass_BaseTexture(surf);
1702 for (surf = firstsurf;surf;surf = surf->chain)
1703 RSurfShader_Wall_Pass_BaseLightmap(surf);
1704 for (surf = firstsurf;surf;surf = surf->chain)
1705 if (surf->dlightframe == r_framecount)
1706 RSurfShader_Wall_Pass_Light(surf);
1707 for (surf = firstsurf;surf;surf = surf->chain)
1708 if (surf->currenttexture->glowtexture)
1709 RSurfShader_Wall_Pass_Glow(surf);
1711 for (surf = firstsurf;surf;surf = surf->chain)
1712 RSurfShader_Wall_Pass_Fog(surf);
1718 =============================================================
1722 =============================================================
1725 static void R_SolidWorldNode (void)
1727 if (r_viewleaf->contents != CONTENTS_SOLID)
1730 mportal_t *p, *pstack[8192];
1731 msurface_t *surf, **mark, **endmark;
1733 // LordHavoc: portal-passage worldnode; follows portals leading
1734 // outward from viewleaf, if a portal leads offscreen it is not
1735 // followed, in indoor maps this can often cull a great deal of
1736 // geometry away when pvs data is not present (useful with pvs as well)
1739 leaf->worldnodeframe = r_framecount;
1744 leaf->visframe = r_framecount;
1746 if (leaf->nummarksurfaces)
1748 mark = leaf->firstmarksurface;
1749 endmark = mark + leaf->nummarksurfaces;
1753 // make sure surfaces are only processed once
1754 if (surf->worldnodeframe == r_framecount)
1756 surf->worldnodeframe = r_framecount;
1757 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1759 if (surf->flags & SURF_PLANEBACK)
1760 surf->visframe = r_framecount;
1764 if (!(surf->flags & SURF_PLANEBACK))
1765 surf->visframe = r_framecount;
1768 while (mark < endmark);
1771 // follow portals into other leafs
1773 for (;p;p = p->next)
1775 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1778 if (leaf->worldnodeframe != r_framecount)
1780 leaf->worldnodeframe = r_framecount;
1781 if (leaf->contents != CONTENTS_SOLID)
1783 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1785 p->visframe = r_framecount;
1786 pstack[portalstack++] = p;
1790 p = pstack[--portalstack];
1802 mnode_t *nodestack[8192], *node = cl.worldmodel->nodes;
1803 int nodestackpos = 0;
1804 // LordHavoc: recursive descending worldnode; if portals are not
1805 // available, this is a good last resort, can cull large amounts of
1806 // geometry, but is more time consuming than portal-passage and renders
1807 // things behind walls
1810 if (R_NotCulledBox(node->mins, node->maxs))
1812 if (node->numsurfaces)
1814 msurface_t *surf = cl.worldmodel->surfaces + node->firstsurface, *surfend = surf + node->numsurfaces;
1815 if (PlaneDiff (r_origin, node->plane) < 0)
1817 for (;surf < surfend;surf++)
1819 if (surf->flags & SURF_PLANEBACK)
1820 surf->visframe = r_framecount;
1825 for (;surf < surfend;surf++)
1827 if (!(surf->flags & SURF_PLANEBACK))
1828 surf->visframe = r_framecount;
1833 // recurse down the children
1834 if (node->children[0]->contents >= 0)
1836 if (node->children[1]->contents >= 0)
1838 if (nodestackpos < 8192)
1839 nodestack[nodestackpos++] = node->children[1];
1840 node = node->children[0];
1844 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1845 node = node->children[0];
1850 ((mleaf_t *)node->children[0])->visframe = r_framecount;
1851 if (node->children[1]->contents >= 0)
1853 node = node->children[1];
1856 else if (nodestackpos > 0)
1858 ((mleaf_t *)node->children[1])->visframe = r_framecount;
1859 node = nodestack[--nodestackpos];
1864 else if (nodestackpos > 0)
1866 node = nodestack[--nodestackpos];
1872 static int r_portalframecount = 0;
1874 static void R_PVSWorldNode()
1877 mportal_t *p, *pstack[8192];
1878 msurface_t *surf, **mark, **endmark;
1882 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1885 leaf->worldnodeframe = r_framecount;
1890 leaf->visframe = r_framecount;
1892 if (leaf->nummarksurfaces)
1894 mark = leaf->firstmarksurface;
1895 endmark = mark + leaf->nummarksurfaces;
1899 // make sure surfaces are only processed once
1900 if (surf->worldnodeframe == r_framecount)
1902 surf->worldnodeframe = r_framecount;
1903 if (PlaneDist(r_origin, surf->plane) < surf->plane->dist)
1905 if (surf->flags & SURF_PLANEBACK)
1906 surf->visframe = r_framecount;
1910 if (!(surf->flags & SURF_PLANEBACK))
1911 surf->visframe = r_framecount;
1914 while (mark < endmark);
1917 // follow portals into other leafs
1918 for (p = leaf->portals;p;p = p->next)
1920 if (DotProduct(r_origin, p->plane.normal) < p->plane.dist)
1923 if (leaf->worldnodeframe != r_framecount)
1925 leaf->worldnodeframe = r_framecount;
1926 if (leaf->contents != CONTENTS_SOLID)
1928 i = (leaf - cl.worldmodel->leafs) - 1;
1929 if (worldvis[i>>3] & (1<<(i&7)))
1931 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1933 pstack[portalstack++] = p;
1937 p = pstack[--portalstack];
1949 Cshader_t Cshader_wall_vertex = {{NULL, RSurfShader_Wall_Vertex}, NULL};
1950 Cshader_t Cshader_wall_lightmap = {{NULL, RSurfShader_Wall_Lightmap}, NULL};
1951 Cshader_t Cshader_wall_fullbright = {{NULL, RSurfShader_Wall_Fullbright}, NULL};
1952 Cshader_t Cshader_water = {{NULL, RSurfShader_Water}, NULL};
1953 Cshader_t Cshader_sky = {{RSurfShader_Sky, NULL}, NULL};
1955 int Cshader_count = 5;
1956 Cshader_t *Cshaders[5] =
1958 &Cshader_wall_vertex,
1959 &Cshader_wall_lightmap,
1960 &Cshader_wall_fullbright,
1965 void R_PrepareSurfaces(void)
1967 int i, alttextures, texframe, framecount;
1972 for (i = 0;i < Cshader_count;i++)
1973 Cshaders[i]->chain = NULL;
1975 model = currentrenderentity->model;
1976 alttextures = currentrenderentity->frame != 0;
1977 texframe = (int)(cl.time * 5.0f);
1979 for (i = 0;i < model->nummodelsurfaces;i++)
1981 surf = model->modelsortedsurfaces[i];
1982 if (surf->visframe == r_framecount)
1984 if (surf->insertframe != r_framecount)
1986 surf->insertframe = r_framecount;
1988 t = surf->texinfo->texture;
1991 framecount = t->anim_total[alttextures];
1992 if (framecount >= 2)
1993 surf->currenttexture = t->anim_frames[alttextures][texframe % framecount];
1995 surf->currenttexture = t->anim_frames[alttextures][0];
1998 surf->currenttexture = t;
2001 surf->chain = surf->shader->chain;
2002 surf->shader->chain = surf;
2007 void R_DrawSurfaces (int type)
2012 for (i = 0;i < Cshader_count;i++)
2014 shader = Cshaders[i];
2015 if (shader->chain && shader->shaderfunc[type])
2016 shader->shaderfunc[type](shader->chain);
2020 static float portalpointbuffer[256][3];
2022 void R_DrawPortals(void)
2025 mportal_t *portal, *endportal;
2028 drawportals = r_drawportals.integer;
2030 if (drawportals < 1)
2033 memset(&m, 0, sizeof(m));
2034 m.transparent = true;
2035 m.blendfunc1 = GL_SRC_ALPHA;
2036 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
2037 m.vertex = &portalpointbuffer[0][0];
2038 m.vertexstep = sizeof(float[3]);
2040 for (portal = cl.worldmodel->portals, endportal = portal + cl.worldmodel->numportals;portal < endportal;portal++)
2042 if (portal->visframe == r_portalframecount)
2044 if (portal->numpoints <= 256)
2046 i = portal - cl.worldmodel->portals;
2047 m.cr = ((i & 0x0007) >> 0) * (1.0f / 7.0f);
2048 m.cg = ((i & 0x0038) >> 3) * (1.0f / 7.0f);
2049 m.cb = ((i & 0x01C0) >> 6) * (1.0f / 7.0f);
2050 point = portal->points;
2051 if (PlaneDiff(r_origin, (&portal->plane)) > 0)
2053 for (i = portal->numpoints - 1;i >= 0;i--)
2054 VectorCopy(point[i].position, portalpointbuffer[i]);
2058 for (i = 0;i < portal->numpoints;i++)
2059 VectorCopy(point[i].position, portalpointbuffer[i]);
2061 R_Mesh_DrawPolygon(&m, portal->numpoints);
2067 void R_SetupForBModelRendering(void)
2074 // because bmodels can be reused, we have to decide which things to render
2075 // from scratch every time
2077 model = currentrenderentity->model;
2079 softwaretransformforentity (currentrenderentity);
2080 softwareuntransform(r_origin, modelorg);
2082 for (i = 0;i < model->nummodelsurfaces;i++)
2084 surf = model->modelsortedsurfaces[i];
2085 if (((surf->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, surf->plane) >= 0))
2086 surf->visframe = r_framecount;
2088 surf->visframe = -1;
2089 surf->worldnodeframe = -1;
2090 surf->lightframe = -1;
2091 surf->dlightframe = -1;
2092 surf->insertframe = -1;
2096 void R_SetupForWorldRendering(void)
2098 // there is only one instance of the world, but it can be rendered in
2101 currentrenderentity = &cl_entities[0].render;
2102 softwaretransformidentity();
2105 static void R_SurfMarkLights (void)
2110 if (r_dynamic.integer)
2113 if (!r_vertexsurfaces.integer)
2115 for (i = 0;i < currentrenderentity->model->nummodelsurfaces;i++)
2117 surf = currentrenderentity->model->modelsortedsurfaces[i];
2118 if (surf->visframe == r_framecount && surf->lightmaptexture != NULL)
2120 if (surf->cached_dlight
2121 || surf->cached_ambient != r_ambient.value
2122 || surf->cached_lightscalebit != lightscalebit)
2123 R_BuildLightMap(surf, false); // base lighting changed
2124 else if (r_dynamic.integer)
2126 if (surf->styles[0] != 255 && (d_lightstylevalue[surf->styles[0]] != surf->cached_light[0]
2127 || (surf->styles[1] != 255 && (d_lightstylevalue[surf->styles[1]] != surf->cached_light[1]
2128 || (surf->styles[2] != 255 && (d_lightstylevalue[surf->styles[2]] != surf->cached_light[2]
2129 || (surf->styles[3] != 255 && (d_lightstylevalue[surf->styles[3]] != surf->cached_light[3]))))))))
2130 R_BuildLightMap(surf, false); // base lighting changed
2131 else if (surf->dlightframe == r_framecount && r_dlightmap.integer)
2132 R_BuildLightMap(surf, true); // only dlights
2139 void R_MarkWorldLights(void)
2141 R_SetupForWorldRendering();
2150 void R_DrawWorld (void)
2152 R_SetupForWorldRendering();
2154 if (r_viewleaf->contents == CONTENTS_SOLID || r_novis.integer || r_viewleaf->compressed_vis == NULL)
2155 R_SolidWorldNode ();
2165 void R_DrawBrushModelSky (void)
2167 R_SetupForBModelRendering();
2169 R_PrepareSurfaces();
2170 R_DrawSurfaces(SHADERSTAGE_SKY);
2173 void R_DrawBrushModelNormal (void)
2177 // have to flush queue because of possible lightmap reuse
2180 R_SetupForBModelRendering();
2184 R_PrepareSurfaces();
2186 if (!skyrendermasked)
2187 R_DrawSurfaces(SHADERSTAGE_SKY);
2188 R_DrawSurfaces(SHADERSTAGE_NORMAL);
2191 static void gl_surf_start(void)
2195 static void gl_surf_shutdown(void)
2199 static void gl_surf_newmap(void)
2203 void GL_Surf_Init(void)
2206 dlightdivtable[0] = 4194304;
2207 for (i = 1;i < 32768;i++)
2208 dlightdivtable[i] = 4194304 / (i << 7);
2210 Cvar_RegisterVariable(&r_ambient);
2211 Cvar_RegisterVariable(&r_vertexsurfaces);
2212 Cvar_RegisterVariable(&r_dlightmap);
2213 Cvar_RegisterVariable(&r_drawportals);
2214 Cvar_RegisterVariable(&r_testvis);
2215 Cvar_RegisterVariable(&r_floatbuildlightmap);
2217 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);