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 int lightmap_textures;
26 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
27 #define BLOCK_WIDTH 256
28 #define BLOCK_HEIGHT 256
29 // LordHavoc: increased lightmap limit from 64 to 1024
30 #define MAX_LIGHTMAPS 1024
31 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
35 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
37 byte *lightmaps[MAX_LIGHTMAPS];
38 short lightmapupdate[MAX_LIGHTMAPS][2];
40 signed int blocklights[BLOCK_WIDTH*BLOCK_HEIGHT*3]; // LordHavoc: *3 for colored lighting
42 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
43 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
44 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "1"};
45 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
46 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
47 cvar_t r_ambient = {"r_ambient", "0"};
48 cvar_t gl_vertex = {"gl_vertex", "0"};
49 cvar_t r_dlightmap = {"r_dlightmap", "1"};
50 cvar_t r_drawportals = {"r_drawportals", "0"};
52 qboolean lightmaprgba, nosubimagefragments, nosubimage;
61 void gl_surf_shutdown()
72 for (i = 0;i < MAX_LIGHTMAPS;i++)
74 Cvar_RegisterVariable(&gl_lightmapalign);
75 Cvar_RegisterVariable(&gl_lightmaprgba);
76 Cvar_RegisterVariable(&gl_nosubimagefragments);
77 Cvar_RegisterVariable(&gl_nosubimage);
78 Cvar_RegisterVariable(&r_ambient);
79 Cvar_RegisterVariable(&gl_vertex);
80 Cvar_RegisterVariable(&r_dlightmap);
81 Cvar_RegisterVariable(&r_drawportals);
83 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown, gl_surf_newmap);
86 int dlightdivtable[32768];
91 int R_AddDynamicLights (msurface_t *surf)
93 int sdtable[18], lnum, td, maxdist, maxdist2, maxdist3, i, s, t, smax, tmax, red, green, blue, lit, dist2, impacts, impactt;
98 // LordHavoc: use 64bit integer... shame it's not very standardized...
99 #if _MSC_VER || __BORLANDC__
107 if (!dlightdivtable[1])
109 dlightdivtable[0] = 4194304;
110 for (s = 1; s < 32768; s++)
111 dlightdivtable[s] = 4194304 / (s << 7);
114 smax = (surf->extents[0] >> 4) + 1;
115 tmax = (surf->extents[1] >> 4) + 1;
117 for (lnum = 0; lnum < MAX_DLIGHTS; lnum++)
119 if (!(surf->dlightbits[lnum >> 5] & (1 << (lnum & 31))))
120 continue; // not lit by this light
122 VectorSubtract (cl_dlights[lnum].origin, currententity->render.origin, local);
123 dist = DotProduct (local, surf->plane->normal) - surf->plane->dist;
125 // for comparisons to minimum acceptable light
126 maxdist = (int) ((cl_dlights[lnum].radius * cl_dlights[lnum].radius));
128 // clamp radius to avoid exceeding 32768 entry division table
129 if (maxdist > 4194304)
133 if (dist2 >= maxdist)
136 impact[0] = cl_dlights[lnum].origin[0] - surf->plane->normal[0] * dist;
137 impact[1] = cl_dlights[lnum].origin[1] - surf->plane->normal[1] * dist;
138 impact[2] = cl_dlights[lnum].origin[2] - surf->plane->normal[2] * dist;
140 impacts = DotProduct (impact, surf->texinfo->vecs[0]) + surf->texinfo->vecs[0][3] - surf->texturemins[0];
141 impactt = DotProduct (impact, surf->texinfo->vecs[1]) + surf->texinfo->vecs[1][3] - surf->texturemins[1];
143 s = bound(0, impacts, smax * 16) - impacts;
144 t = bound(0, impactt, tmax * 16) - impactt;
145 i = s * s + t * t + dist2;
149 // reduce calculations
150 for (s = 0, i = impacts; s < smax; s++, i -= 16)
151 sdtable[s] = i * i + dist2 + LIGHTOFFSET;
153 maxdist3 = maxdist - (int) (dist * dist);
155 // convert to 8.8 blocklights format and scale up by radius
156 red = cl_dlights[lnum].color[0] * maxdist;
157 green = cl_dlights[lnum].color[1] * maxdist;
158 blue = cl_dlights[lnum].color[2] * maxdist;
162 for (t = 0; t < tmax; t++, i -= 16)
165 // make sure some part of it is visible on this line
168 maxdist2 = maxdist - td;
169 for (s = 0; s < smax; s++)
171 if (sdtable[s] < maxdist2)
173 k = dlightdivtable[(sdtable[s] + td) >> 7];
174 bl[0] += (red * k) >> 9;
175 bl[1] += (green * k) >> 9;
176 bl[2] += (blue * k) >> 9;
190 void R_ConvertLightmap (int *in, byte *out, int width, int height, int stride)
193 stride -= (width*lightmapbytes);
196 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
197 // the image is brightened as a processing pass
200 for (i = 0;i < height;i++, out += stride)
202 for (j = 0;j < width;j++, in += 3, out += 4)
204 out[0] = min(in[0] >> 8, 255);
205 out[1] = min(in[1] >> 8, 255);
206 out[2] = min(in[2] >> 8, 255);
213 for (i = 0;i < height;i++, out += stride)
215 for (j = 0;j < width;j++, in += 3, out += 3)
217 out[0] = min(in[0] >> 8, 255);
218 out[1] = min(in[1] >> 8, 255);
219 out[2] = min(in[2] >> 8, 255);
228 for (i = 0;i < height;i++, out += stride)
230 for (j = 0;j < width;j++, in += 3, out += 4)
232 out[0] = min(in[0] >> 7, 255);
233 out[1] = min(in[1] >> 7, 255);
234 out[2] = min(in[2] >> 7, 255);
241 for (i = 0;i < height;i++, out += stride)
243 for (j = 0;j < width;j++, in += 3, out += 3)
245 out[0] = min(in[0] >> 7, 255);
246 out[1] = min(in[1] >> 7, 255);
247 out[2] = min(in[2] >> 7, 255);
258 Combine and scale multiple lightmaps into the 8.8 format in blocklights
261 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
264 int i, j, size, size3;
270 surf->cached_dlight = 0;
271 surf->cached_lighthalf = lighthalf;
272 surf->cached_ambient = r_ambient.value;
274 smax = (surf->extents[0]>>4)+1;
275 tmax = (surf->extents[1]>>4)+1;
278 lightmap = surf->samples;
280 // set to full bright if no light data
281 if ((currententity && (currententity->render.effects & EF_FULLBRIGHT)) || !cl.worldmodel->lightdata)
284 for (i=0 ; i<size ; i++)
294 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
298 for (i = 0;i < size3;i++)
302 memset(&blocklights[0], 0, size*3*sizeof(int));
304 // add all the lightmaps
307 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
309 scale = d_lightstylevalue[surf->styles[maps]];
310 surf->cached_light[maps] = scale; // 8.8 fraction
312 for (i = 0;i < size3;i++)
313 *bl++ += *lightmap++ * scale;
316 if (r_dlightmap.value && surf->dlightframe == r_framecount)
317 if ((surf->cached_dlight = R_AddDynamicLights(surf)))
320 R_ConvertLightmap(blocklights, dest, smax, tmax, stride);
323 byte templight[BLOCK_WIDTH*BLOCK_HEIGHT*4];
325 void R_UpdateLightmap(msurface_t *s, int lnum)
328 // upload the new lightmap texture fragment
330 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
331 if (nosubimage || nosubimagefragments)
333 if (lightmapupdate[lnum][0] > s->light_t)
334 lightmapupdate[lnum][0] = s->light_t;
335 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
336 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
338 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
340 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
344 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
345 tmax = (s->extents[1]>>4)+1;
348 R_BuildLightMap (s, templight, smax * 4);
350 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
354 R_BuildLightMap (s, templight, smax * 3);
356 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
366 Returns the proper texture for a given time and base texture
369 texture_t *R_TextureAnimation (texture_t *base)
371 // texture_t *original;
375 if (currententity->render.frame)
377 if (base->alternate_anims)
378 base = base->alternate_anims;
381 if (!base->anim_total)
384 return base->anim_frames[(int)(cl.time*5) % base->anim_total];
389 relative = (int)(cl.time*5) % base->anim_total;
392 while (base->anim_min > relative || base->anim_max <= relative)
394 base = base->anim_next;
397 Con_Printf("R_TextureAnimation: broken cycle");
402 Con_Printf("R_TextureAnimation: infinite cycle");
413 =============================================================
417 =============================================================
421 extern int solidskytexture;
422 extern int alphaskytexture;
423 extern float speedscale; // for top sky and bottom sky
425 extern char skyname[];
429 #include "gl_warp_sin.h"
431 #define TURBSCALE (256.0 / (2 * M_PI))
434 void UploadLightmaps()
437 if (nosubimage || nosubimagefragments)
439 for (i = 0;i < MAX_LIGHTMAPS;i++)
441 if (lightmapupdate[i][0] < lightmapupdate[i][1])
445 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
449 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
451 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
456 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 4 * lightmapupdate[i][0]));
458 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, lightmapupdate[i][0], BLOCK_WIDTH, lightmapupdate[i][1] - lightmapupdate[i][0], GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i] + (BLOCK_WIDTH * 3 * lightmapupdate[i][0]));
462 lightmapupdate[i][0] = BLOCK_HEIGHT;
463 lightmapupdate[i][1] = 0;
468 float wvert[1024*6]; // used by the following functions
470 void RSurf_DrawSky(msurface_t *s, int transform)
476 // LordHavoc: HalfLife maps have freaky skypolys...
480 for (p=s->polys ; p ; p=p->next)
482 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
484 skypoly[currentskypoly].firstvert = currentskyvert;
485 skypoly[currentskypoly++].verts = p->numverts;
488 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
490 softwaretransform(v, skyvert[currentskyvert].v);
496 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
498 VectorCopy(v, skyvert[currentskyvert].v);
506 int RSurf_Light(int *dlightbits, glpoly_t *polys)
508 float cr, cg, cb, radius, radius2, f, *v, *wv;
509 int i, a, b, lit = false;
514 for (a = 0;a < 8;a++)
516 if ((c = dlightbits[a]))
518 for (b = 0, d = 1;c;b++, d <<= 1)
523 light = &cl_dlights[a * 32 + b];
524 lightorigin = light->origin;
525 cr = light->color[0];
526 cg = light->color[1];
527 cb = light->color[2];
528 radius = light->radius*light->radius;
529 radius2 = radius * 256.0f;
531 for (p = polys;p;p = p->next)
533 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
535 f = VectorDistance2(wv, lightorigin);
538 f = radius2 / (f + LIGHTOFFSET);
554 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
557 float os = turbsin[(int)(cl.time * TURBSCALE) & 255], ot = turbsin[(int)(cl.time * TURBSCALE + 96.0) & 255];
560 // FIXME: make fog texture if water texture is transparent?
562 if (s->dlightframe != r_framecount)
565 // LordHavoc: fast path for no vertex lighting cases
568 if (r_waterripple.value)
570 for (p=s->polys ; p ; p=p->next)
572 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
573 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
575 softwaretransform(v, temp);
576 transpolyvert(temp[0], temp[1], temp[2] + r_waterripple.value * turbsin[(int)((temp[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((temp[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
583 for (p=s->polys ; p ; p=p->next)
585 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
586 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
588 softwaretransform(v, temp);
589 transpolyvert(temp[0], temp[1], temp[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
597 if (r_waterripple.value)
599 for (p=s->polys ; p ; p=p->next)
601 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
602 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
603 transpolyvert(v[0], v[1], v[2] + r_waterripple.value * turbsin[(int)((v[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((v[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f), (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
609 for (p=s->polys ; p ; p=p->next)
611 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
612 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
613 transpolyvert(v[0], v[1], v[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), 128, 128, 128, alpha);
623 for (p = s->polys;p;p = p->next)
625 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
628 softwaretransform(v, wv);
631 if (r_waterripple.value)
632 wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+cl.time) * TURBSCALE) & 255] * (1.0f / 64.0f);
633 wv[3] = wv[4] = wv[5] = 128.0f;
637 if (s->dlightframe == r_framecount)
638 RSurf_Light(s->dlightbits, s->polys);
640 for (p=s->polys ; p ; p=p->next)
642 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, TPOLYTYPE_ALPHA);
643 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
644 transpolyvert(wv[0], wv[1], wv[2], (v[3] + os) * (1.0f/64.0f), (v[4] + ot) * (1.0f/64.0f), wv[3], wv[4], wv[5], alpha);
650 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
652 int i, lit = false, polys = 0, verts = 0;
657 wallvertcolor_t *outcolor;
658 // check for lightmap modification
660 || (r_dynamic.value && r_dlightmap.value && s->dlightframe == r_framecount)
661 || r_ambient.value != s->cached_ambient
662 || lighthalf != s->cached_lighthalf
664 && ((s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
665 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
666 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
667 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))))
668 R_UpdateLightmap(s, s->lightmaptexturenum);
669 if (r_dlightmap.value || s->dlightframe != r_framecount)
671 // LordHavoc: fast path version for no vertex lighting cases
672 wp = &wallpoly[currentwallpoly];
673 out = &wallvert[currentwallvert];
674 for (p = s->polys;p;p = p->next)
676 if ((currentwallpoly >= MAX_WALLPOLYS) || (currentwallvert+p->numverts > MAX_WALLVERTS))
678 wp->texnum = (unsigned short) R_GetTexture(t->texture);
679 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
680 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
681 wp->firstvert = currentwallvert;
682 wp->numverts = p->numverts;
686 currentwallvert += p->numverts;
690 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
692 softwaretransform(v, out->vert);
702 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, out++)
704 VectorCopy(v, out->vert);
711 memcpy(out, v, sizeof(vec_t) * VERTEXSIZE * p->numverts);
720 for (p = s->polys;p;p = p->next)
722 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
725 softwaretransform(v, wv);
728 wv[3] = wv[4] = wv[5] = 0.0f;
731 verts += p->numverts;
734 if ((currentwallpoly + polys > MAX_WALLPOLYS) || (currentwallvert+verts > MAX_WALLVERTS))
736 if ((!r_dlightmap.value) && s->dlightframe == r_framecount)
737 lit = RSurf_Light(s->dlightbits, s->polys);
739 wp = &wallpoly[currentwallpoly];
740 out = &wallvert[currentwallvert];
741 outcolor = &wallvertcolor[currentwallvert];
742 currentwallpoly += polys;
743 for (p = s->polys;p;p = p->next)
746 wp->texnum = (unsigned short) R_GetTexture(t->texture);
747 wp->lighttexnum = (unsigned short) (lightmap_textures + s->lightmaptexturenum);
748 wp->glowtexnum = (unsigned short) R_GetTexture(t->glowtexture);
749 wp->firstvert = currentwallvert;
750 wp->numverts = p->numverts;
753 currentwallvert += p->numverts;
754 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++, outcolor++)
760 outcolor->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
761 outcolor->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
762 outcolor->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
767 outcolor->r = (byte) (bound(0, (int) wv[3], 255));
768 outcolor->g = (byte) (bound(0, (int) wv[4], 255));
769 outcolor->b = (byte) (bound(0, (int) wv[5], 255));
773 out->vert[0] = wv[0];
774 out->vert[1] = wv[1];
775 out->vert[2] = wv[2];
785 // LordHavoc: transparent brush models
786 extern float modelalpha;
788 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
791 float *v, *wv, scale;
794 alpha = (int) (modelalpha * 255.0f);
795 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
797 for (p = s->polys;p;p = p->next)
799 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
802 softwaretransform(v, wv);
805 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
806 if (s->styles[0] != 255)
808 lm = (byte *)((long) s->samples + (int) v[7]);
809 scale = d_lightstylevalue[s->styles[0]] * (1.0f / 128.0f);wv[3] += lm[size3*0+0] * scale;wv[4] += lm[size3*0+1] * scale;wv[5] += lm[size3*0+2] * scale;
810 if (s->styles[1] != 255)
812 scale = d_lightstylevalue[s->styles[1]] * (1.0f / 128.0f);wv[3] += lm[size3*1+0] * scale;wv[4] += lm[size3*1+1] * scale;wv[5] += lm[size3*1+2] * scale;
813 if (s->styles[2] != 255)
815 scale = d_lightstylevalue[s->styles[2]] * (1.0f / 128.0f);wv[3] += lm[size3*2+0] * scale;wv[4] += lm[size3*2+1] * scale;wv[5] += lm[size3*2+2] * scale;
816 if (s->styles[3] != 255)
818 scale = d_lightstylevalue[s->styles[3]] * (1.0f / 128.0f);wv[3] += lm[size3*3+0] * scale;wv[4] += lm[size3*3+1] * scale;wv[5] += lm[size3*3+2] * scale;
826 if (s->dlightframe == r_framecount)
827 RSurf_Light(s->dlightbits, s->polys);
829 if (isbmodel && (currententity->render.colormod[0] != 1 || currententity->render.colormod[1] != 1 || currententity->render.colormod[2] != 1))
831 for (p = s->polys;p;p = p->next)
834 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
835 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
836 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->render.colormod[0], wv[4] * currententity->render.colormod[1], wv[5] * currententity->render.colormod[2], alpha);
842 for (p = s->polys;p;p = p->next)
845 transpolybegin(R_GetTexture(t->texture), R_GetTexture(t->glowtexture), 0, currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
846 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
847 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
853 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
860 void R_DrawBrushModel (entity_t *e)
866 int rotated, vertexlit = false;
871 clmodel = e->render.model;
873 if (e->render.angles[0] || e->render.angles[1] || e->render.angles[2])
876 for (i=0 ; i<3 ; i++)
878 mins[i] = e->render.origin[i] - clmodel->radius;
879 maxs[i] = e->render.origin[i] + clmodel->radius;
885 VectorAdd (e->render.origin, clmodel->mins, mins);
886 VectorAdd (e->render.origin, clmodel->maxs, maxs);
889 if (R_VisibleCullBox (mins, maxs))
894 VectorSubtract (r_origin, e->render.origin, modelorg);
898 vec3_t forward, right, up;
900 VectorCopy (modelorg, temp);
901 AngleVectors (e->render.angles, forward, right, up);
902 modelorg[0] = DotProduct (temp, forward);
903 modelorg[1] = -DotProduct (temp, right);
904 modelorg[2] = DotProduct (temp, up);
907 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
909 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
910 s->visframe = r_framecount;
915 // calculate dynamic lighting for bmodel if it's not an
917 for (i = 0;i < MAX_DLIGHTS;i++)
919 if (!cl_dlights[i].radius)
922 VectorSubtract(cl_dlights[i].origin, currententity->render.origin, org);
923 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
925 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->render.effects & EF_FULLBRIGHT) || currententity->render.colormod[0] != 1 || currententity->render.colormod[2] != 1 || currententity->render.colormod[2] != 1;
927 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
928 softwaretransformforentity (e);
929 e->render.angles[0] = -e->render.angles[0]; // stupid quake bug
932 for (i = 0, s = &clmodel->surfaces[clmodel->firstmodelsurface];i < clmodel->nummodelsurfaces;i++, s++)
934 if (s->visframe == r_framecount)
936 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
937 if (s->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
939 // sky and liquid don't need sorting (skypoly/transpoly)
940 if (s->flags & SURF_DRAWSKY)
941 RSurf_DrawSky(s, true);
943 RSurf_DrawWater(s, R_TextureAnimation(s->texinfo->texture), true, s->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
947 texture_t *t = R_TextureAnimation(s->texinfo->texture);
948 if (vertexlit || s->texinfo->texture->transparent)
949 RSurf_DrawWallVertex(s, t, true, true);
951 RSurf_DrawWall(s, t, true);
959 =============================================================
963 =============================================================
966 static byte *worldvis;
967 extern cvar_t r_novis;
969 void R_MarkLeaves (void)
971 static float noviscache;
972 if (r_oldviewleaf == r_viewleaf && noviscache == r_novis.value)
975 r_oldviewleaf = r_viewleaf;
976 noviscache = r_novis.value;
978 worldvis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
981 void R_SolidWorldNode ()
985 msurface_t *surf, **mark, **endmark;
987 for (l = 0, leaf = cl.worldmodel->leafs;l < cl.worldmodel->numleafs;l++, leaf++)
989 if (leaf->nummarksurfaces)
991 if (R_CullBox(leaf->mins, leaf->maxs))
996 leaf->visframe = r_framecount;
998 if (leaf->nummarksurfaces)
1000 mark = leaf->firstmarksurface;
1001 endmark = mark + leaf->nummarksurfaces;
1005 // make sure surfaces are only processed once
1006 if (surf->worldnodeframe == r_framecount)
1008 surf->worldnodeframe = r_framecount;
1009 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1011 if (surf->flags & SURF_PLANEBACK)
1012 surf->visframe = r_framecount;
1016 if (!(surf->flags & SURF_PLANEBACK))
1017 surf->visframe = r_framecount;
1020 while (mark < endmark);
1027 // experimental and inferior to the other in recursion depth allowances
1028 void R_PortalWorldNode ()
1032 msurface_t *surf, **mark, **endmark;
1033 mleaf_t *leaf, *llistbuffer[8192], **l, **llist;
1036 leaf->worldnodeframe = r_framecount;
1037 l = llist = &llistbuffer[0];
1038 *llist++ = r_viewleaf;
1045 leaf->visframe = r_framecount;
1047 if (leaf->nummarksurfaces)
1049 mark = leaf->firstmarksurface;
1050 endmark = mark + leaf->nummarksurfaces;
1054 // make sure surfaces are only processed once
1055 if (surf->worldnodeframe == r_framecount)
1057 surf->worldnodeframe = r_framecount;
1058 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1060 if (surf->flags & SURF_PLANEBACK)
1061 surf->visframe = r_framecount;
1065 if (!(surf->flags & SURF_PLANEBACK))
1066 surf->visframe = r_framecount;
1069 while (mark < endmark);
1072 // follow portals into other leafs
1074 for (;p;p = p->next)
1077 if (leaf->worldnodeframe != r_framecount)
1079 leaf->worldnodeframe = r_framecount;
1080 i = (leaf - cl.worldmodel->leafs) - 1;
1081 if ((worldvis[i>>3] & (1<<(i&7))) && R_NotCulledBox(leaf->mins, leaf->maxs))
1089 p = r_viewleaf->portals;
1090 for (;p;p = p->next)
1093 if (p->past->worldnodeframe != r_framecount)
1097 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, j);
1101 void R_PortalWorldNode ()
1104 mportal_t *p, *pstack[8192];
1105 msurface_t *surf, **mark, **endmark;
1109 leaf->worldnodeframe = r_framecount;
1114 leaf->visframe = r_framecount;
1116 if (leaf->nummarksurfaces)
1118 mark = leaf->firstmarksurface;
1119 endmark = mark + leaf->nummarksurfaces;
1123 // make sure surfaces are only processed once
1124 if (surf->worldnodeframe == r_framecount)
1126 surf->worldnodeframe = r_framecount;
1127 if (PlaneDist(modelorg, surf->plane) < surf->plane->dist)
1129 if (surf->flags & SURF_PLANEBACK)
1130 surf->visframe = r_framecount;
1134 if (!(surf->flags & SURF_PLANEBACK))
1135 surf->visframe = r_framecount;
1138 while (mark < endmark);
1141 // follow portals into other leafs
1143 for (;p;p = p->next)
1146 if (leaf->worldnodeframe != r_framecount)
1148 leaf->worldnodeframe = r_framecount;
1149 if (leaf->contents != CONTENTS_SOLID)
1151 i = (leaf - cl.worldmodel->leafs) - 1;
1152 if (worldvis[i>>3] & (1<<(i&7)))
1154 if (R_NotCulledBox(leaf->mins, leaf->maxs))
1156 pstack[portalstack++] = p;
1160 p = pstack[--portalstack];
1172 p = r_viewleaf->portals;
1173 for (;p;p = p->next)
1176 if (p->past->worldnodeframe != r_framecount)
1180 Con_Printf("%i portals of viewleaf (%i portals) were not checked\n", i, portalstack);
1183 void R_DrawSurfaces (void)
1185 msurface_t *surf, *endsurf;
1187 int vertex = gl_vertex.value;
1189 surf = &cl.worldmodel->surfaces[cl.worldmodel->firstmodelsurface];
1190 endsurf = surf + cl.worldmodel->nummodelsurfaces;
1191 for (;surf < endsurf;surf++)
1193 if (surf->visframe == r_framecount)
1196 if (surf->flags & (SURF_DRAWSKY | SURF_DRAWTURB))
1198 // sky and liquid don't need sorting (skypoly/transpoly)
1199 if (surf->flags & SURF_DRAWSKY)
1200 RSurf_DrawSky(surf, false);
1202 RSurf_DrawWater(surf, R_TextureAnimation(surf->texinfo->texture), false, surf->flags & SURF_DRAWNOALPHA ? 255 : wateralpha);
1206 t = R_TextureAnimation(surf->texinfo->texture);
1208 RSurf_DrawWallVertex(surf, t, false, false);
1210 RSurf_DrawWall(surf, t, false);
1216 void R_DrawPortals()
1218 int drawportals, i, r, g, b;
1219 mleaf_t *leaf, *endleaf;
1221 mvertex_t *point, *endpoint;
1222 drawportals = (int)r_drawportals.value;
1223 if (drawportals < 1)
1225 leaf = cl.worldmodel->leafs;
1226 endleaf = leaf + cl.worldmodel->numleafs;
1227 for (;leaf < endleaf;leaf++)
1229 if (leaf->visframe == r_framecount && leaf->portals)
1231 i = leaf - cl.worldmodel->leafs;
1232 r = (i & 0x0007) << 5;
1233 g = (i & 0x0038) << 2;
1234 b = (i & 0x01C0) >> 1;
1235 portal = leaf->portals;
1238 transpolybegin(0, 0, 0, TPOLYTYPE_ALPHA);
1239 point = portal->points + portal->numpoints - 1;
1240 endpoint = portal->points;
1241 for (;point >= endpoint;point--)
1242 transpolyvertub(point->position[0], point->position[1], point->position[2], 0, 0, r, g, b, 32);
1244 portal = portal->next;
1255 void R_DrawWorld (void)
1259 wateralpha = bound(0, r_wateralpha.value*255.0f, 255);
1261 memset (&ent, 0, sizeof(ent));
1262 ent.render.model = cl.worldmodel;
1263 ent.render.colormod[0] = ent.render.colormod[1] = ent.render.colormod[2] = 1;
1264 modelalpha = ent.render.alpha = 1;
1265 ent.render.scale = 1;
1267 VectorCopy (r_origin, modelorg);
1269 currententity = &ent;
1271 softwaretransformidentity(); // LordHavoc: clear transform
1275 if (r_viewleaf->contents == CONTENTS_SOLID)
1276 R_SolidWorldNode ();
1280 R_PortalWorldNode ();
1284 R_PushDlights (); // now mark the lit surfaces
1292 =============================================================================
1296 =============================================================================
1299 // returns a texture number and the position inside it
1300 int AllocBlock (int w, int h, short *x, short *y)
1306 for (texnum = 0;texnum < MAX_LIGHTMAPS;texnum++)
1308 best = BLOCK_HEIGHT;
1310 for (i = 0;i < BLOCK_WIDTH - w;i += lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1314 for (j=0 ; j<w ; j++)
1316 if (allocated[texnum][i+j] >= best)
1318 if (allocated[texnum][i+j] > best2)
1319 best2 = allocated[texnum][i+j];
1322 { // this is a valid spot
1328 if (best + h > BLOCK_HEIGHT)
1331 if (nosubimagefragments || nosubimage)
1333 if (!lightmaps[texnum])
1335 lightmaps[texnum] = qmalloc(BLOCK_WIDTH*BLOCK_HEIGHT*4);
1336 memset(lightmaps[texnum], 0, BLOCK_WIDTH*BLOCK_HEIGHT*4);
1339 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1340 else if (!allocated[texnum][0])
1342 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*4];
1343 memset(blank, 0, sizeof(blank));
1346 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1347 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1348 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1350 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1352 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1356 for (i = 0;i < w;i++)
1357 allocated[texnum][*x + i] = best + h;
1362 Host_Error ("AllocBlock: full, unable to find room for %i by %i lightmap", w, h);
1367 mvertex_t *r_pcurrentvertbase;
1368 model_t *currentmodel;
1374 BuildSurfaceDisplayList
1377 void BuildSurfaceDisplayList (msurface_t *fa)
1379 int i, j, lindex, lnumverts;
1380 medge_t *pedges, *r_pedge;
1386 // reconstruct the polygon
1387 pedges = currentmodel->edges;
1388 lnumverts = fa->numedges;
1394 poly = Hunk_AllocName (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float), "surfaces");
1395 poly->next = fa->polys;
1396 poly->flags = fa->flags;
1398 poly->numverts = lnumverts;
1400 for (i=0 ; i<lnumverts ; i++)
1402 lindex = currentmodel->surfedges[fa->firstedge + i];
1406 r_pedge = &pedges[lindex];
1407 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1411 r_pedge = &pedges[-lindex];
1412 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1414 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1415 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1417 VectorCopy (vec, poly->verts[i]);
1418 poly->verts[i][3] = s / fa->texinfo->texture->width;
1419 poly->verts[i][4] = t / fa->texinfo->texture->height;
1422 // lightmap texture coordinates
1424 s -= fa->texturemins[0];
1425 t -= fa->texturemins[1];
1428 // LordHavoc: calc lightmap data offset
1429 j = (bound(0l, (int)t>>4, fa->extents[1]>>4) * ((fa->extents[0]>>4)+1) + bound(0l, (int)s>>4, fa->extents[0]>>4)) * 3;
1430 poly->verts[i][7] = j;
1431 s += fa->light_s*16;
1432 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1434 t += fa->light_t*16;
1435 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1437 poly->verts[i][5] = s;
1438 poly->verts[i][6] = t;
1442 // remove co-linear points - Ed
1445 if (!gl_keeptjunctions.value)
1447 for (i = 0 ; i < lnumverts ; ++i)
1450 float *prev, *this, *next;
1452 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1453 this = poly->verts[i];
1454 next = poly->verts[(i + 1) % lnumverts];
1456 VectorSubtract( this, prev, v1 );
1457 VectorNormalize( v1 );
1458 VectorSubtract( next, prev, v2 );
1459 VectorNormalize( v2 );
1461 // skip co-linear points
1462 #define COLINEAR_EPSILON 0.001
1463 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1464 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1465 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1468 for (j = i + 1; j < lnumverts; ++j)
1471 for (k = 0; k < VERTEXSIZE; ++k)
1472 poly->verts[j - 1][k] = poly->verts[j][k];
1476 // retry next vertex next time, which is now current vertex
1482 poly->numverts = lnumverts;
1486 ========================
1487 GL_CreateSurfaceLightmap
1488 ========================
1490 void GL_CreateSurfaceLightmap (msurface_t *surf)
1494 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1497 smax = (surf->extents[0]>>4)+1;
1498 tmax = (surf->extents[1]>>4)+1;
1500 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1501 if (nosubimage || nosubimagefragments)
1503 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1504 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1507 R_BuildLightMap (surf, templight, smax * 4);
1509 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1513 R_BuildLightMap (surf, templight, smax * 3);
1515 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1524 Builds the lightmap texture
1525 with all the surfaces from all brush models
1528 void GL_BuildLightmaps (void)
1533 memset (allocated, 0, sizeof(allocated));
1535 r_framecount = 1; // no dlightcache
1537 if (gl_nosubimagefragments.value)
1538 nosubimagefragments = 1;
1540 nosubimagefragments = 0;
1542 if (gl_nosubimage.value)
1547 if (gl_lightmaprgba.value)
1549 lightmaprgba = true;
1554 lightmaprgba = false;
1558 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1559 // it needs to be aligned on 4 pixel boundaries...
1560 // so I implemented an adjustable lightmap alignment
1561 if (gl_lightmapalign.value < 1)
1562 gl_lightmapalign.value = 1;
1563 if (gl_lightmapalign.value > 16)
1564 gl_lightmapalign.value = 16;
1566 while (lightmapalign < gl_lightmapalign.value)
1567 lightmapalign <<= 1;
1568 gl_lightmapalign.value = lightmapalign;
1569 lightmapalignmask = ~(lightmapalign - 1);
1570 if (nosubimagefragments || nosubimage)
1573 lightmapalignmask = ~0;
1576 if (!lightmap_textures)
1577 lightmap_textures = R_GetTextureSlots(MAX_LIGHTMAPS);
1579 for (j=1 ; j<MAX_MODELS ; j++)
1581 m = cl.model_precache[j];
1584 if (m->name[0] == '*')
1586 r_pcurrentvertbase = m->vertexes;
1588 for (i=0 ; i<m->numsurfaces ; i++)
1590 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1592 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1594 GL_CreateSurfaceLightmap (m->surfaces + i);
1595 BuildSurfaceDisplayList (m->surfaces + i);
1599 if (nosubimage || nosubimagefragments)
1603 qglSelectTexture(gl_mtex_enum+1);
1604 for (i = 0;i < MAX_LIGHTMAPS;i++)
1606 if (!allocated[i][0])
1608 lightmapupdate[i][0] = BLOCK_HEIGHT;
1609 lightmapupdate[i][1] = 0;
1612 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1613 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1614 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1616 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1618 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1623 qglSelectTexture(gl_mtex_enum+0);