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 extern int skytexturenum;
26 int lightmap_textures;
28 signed blocklights[18*18*3]; // LordHavoc: *3 for colored lighting
30 // LordHavoc: skinny but tall lightmaps for quicker subimage uploads
31 #define BLOCK_WIDTH 128
32 #define BLOCK_HEIGHT 128
33 // LordHavoc: increased lightmap limit from 64 to 1024
34 #define MAX_LIGHTMAPS 1024
35 #define LIGHTMAPSIZE (BLOCK_WIDTH*BLOCK_HEIGHT*4)
39 short allocated[MAX_LIGHTMAPS][BLOCK_WIDTH];
41 byte *lightmaps[MAX_LIGHTMAPS];
42 short lightmapupdate[MAX_LIGHTMAPS][2];
44 int lightmapalign, lightmapalignmask; // LordHavoc: NVIDIA's broken subimage fix, see BuildLightmaps for notes
45 cvar_t gl_lightmapalign = {"gl_lightmapalign", "4"};
46 cvar_t gl_lightmaprgba = {"gl_lightmaprgba", "0"};
47 cvar_t gl_nosubimagefragments = {"gl_nosubimagefragments", "0"};
48 cvar_t gl_nosubimage = {"gl_nosubimage", "0"};
49 cvar_t r_ambient = {"r_ambient", "0"};
50 cvar_t gl_vertex = {"gl_vertex", "0"};
51 cvar_t gl_texsort = {"gl_texsort", "1"};
52 //cvar_t gl_funnywalls = {"gl_funnywalls", "0"}; // LordHavoc: see BuildSurfaceDisplayList
54 qboolean lightmaprgba, nosubimagefragments, nosubimage, skyisvisible;
57 extern qboolean gl_arrays;
59 extern int r_dlightframecount;
65 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_funnywalls);
80 Cvar_RegisterVariable(&gl_vertex);
81 Cvar_RegisterVariable(&gl_texsort);
82 // check if it's the glquake minigl driver
83 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
86 // Cvar_SetValue("gl_nosubimagefragments", 1);
87 // Cvar_SetValue("gl_nosubimage", 1);
88 Cvar_SetValue("gl_lightmode", 0);
91 R_RegisterModule("GL_Surf", gl_surf_start, gl_surf_shutdown);
94 extern qboolean lighthalf;
99 Combine and scale multiple lightmaps into the 8.8 format in blocklights
102 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
112 surf->cached_lighthalf = lighthalf;
113 surf->cached_ambient = r_ambient.value;
115 smax = (surf->extents[0]>>4)+1;
116 tmax = (surf->extents[1]>>4)+1;
118 lightmap = surf->samples;
120 // set to full bright if no light data
121 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
124 for (i=0 ; i<size ; i++)
135 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
136 for (i=0 ; i<size ; i++)
143 // add all the lightmaps
145 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
147 scale = d_lightstylevalue[surf->styles[maps]];
148 surf->cached_light[maps] = scale; // 8.8 fraction
150 for (i=0 ; i<size ; i++)
152 *bl++ += *lightmap++ * scale;
153 *bl++ += *lightmap++ * scale;
154 *bl++ += *lightmap++ * scale;
158 stride -= (smax*lightmapbytes);
162 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
163 // the image is brightened as a processing pass
166 for (i=0 ; i<tmax ; i++, dest += stride)
168 for (j=0 ; j<smax ; j++)
170 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
171 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
172 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
179 for (i=0 ; i<tmax ; i++, dest += stride)
181 for (j=0 ; j<smax ; j++)
183 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
184 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
185 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
194 for (i=0 ; i<tmax ; i++, dest += stride)
196 for (j=0 ; j<smax ; j++)
198 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
199 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
200 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
207 for (i=0 ; i<tmax ; i++, dest += stride)
209 for (j=0 ; j<smax ; j++)
211 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
212 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
213 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
220 byte templight[32*32*4];
222 void R_UpdateLightmap(msurface_t *s, int lnum)
225 // upload the new lightmap texture fragment
226 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
227 if (nosubimage || nosubimagefragments)
229 if (lightmapupdate[lnum][0] > s->light_t)
230 lightmapupdate[lnum][0] = s->light_t;
231 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
232 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
234 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
236 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
240 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
241 tmax = (s->extents[1]>>4)+1;
244 R_BuildLightMap (s, templight, smax * 4);
245 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
249 R_BuildLightMap (s, templight, smax * 3);
250 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
260 Returns the proper texture for a given time and base texture
263 texture_t *R_TextureAnimation (texture_t *base)
269 if (currententity->frame)
271 if (base->alternate_anims)
272 base = base->alternate_anims;
275 if (!base->anim_total)
280 relative = (int)(cl.time*10) % base->anim_total;
283 while (base->anim_min > relative || base->anim_max <= relative)
285 base = base->anim_next;
288 Con_Printf("R_TextureAnimation: broken cycle");
293 Con_Printf("R_TextureAnimation: infinite cycle");
303 =============================================================
307 =============================================================
311 extern int solidskytexture;
312 extern int alphaskytexture;
313 extern float speedscale; // for top sky and bottom sky
315 extern char skyname[];
317 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
320 #include "gl_warp_sin.h"
322 #define TURBSCALE (256.0 / (2 * M_PI))
325 void UploadLightmaps()
328 if (nosubimage || nosubimagefragments)
330 for (i = 0;i < MAX_LIGHTMAPS;i++)
332 if (lightmapupdate[i][0] < lightmapupdate[i][1])
334 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
338 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
340 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
345 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]));
347 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]));
350 lightmapupdate[i][0] = BLOCK_HEIGHT;
351 lightmapupdate[i][1] = 0;
356 float wvert[1024*6]; // used by the following functions
358 void RSurf_DrawSky(msurface_t *s, int transform)
363 for (p=s->polys ; p ; p=p->next)
365 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
367 skypoly[currentskypoly].firstvert = currentskyvert;
368 skypoly[currentskypoly++].verts = p->numverts;
371 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
373 softwaretransform(v, skyvert[currentskyvert].v);
379 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
381 skyvert[currentskyvert].v[0] = v[0];
382 skyvert[currentskyvert].v[1] = v[1];
383 skyvert[currentskyvert++].v[2] = v[2];
390 int RSurf_Light(int *dlightbits, glpoly_t *polys)
392 float cr, cg, cb, radius, radius2, f, *v, *wv;
393 int i, a, b, lit = false;
398 for (a = 0;a < 8;a++)
400 if ((c = dlightbits[a]))
402 for (b = 0, d = 1;c;b++, d <<= 1)
407 light = &cl_dlights[a * 32 + b];
408 lightorigin = light->origin;
409 cr = light->color[0];
410 cg = light->color[1];
411 cb = light->color[2];
412 radius = light->radius*light->radius*LIGHTSCALE;
413 radius2 = radius * (256.0f / LIGHTSCALE);
415 for (p = polys;p;p = p->next)
417 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
419 f = VectorDistance2(wv, lightorigin);
422 f = radius2 / (f + LIGHTOFFSET);
438 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
441 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
445 for (p = s->polys;p;p = p->next)
447 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
450 softwaretransform(v, wv);
453 if (r_waterripple.value)
454 wv[2] += r_waterripple.value * turbsin[(int)((wv[0]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * turbsin[(int)((wv[1]*(1.0f/32.0f)+realtime) * TURBSCALE) & 255] * (1.0f / 64.0f);
455 wv[3] = wv[4] = wv[5] = 128.0f;
459 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
460 RSurf_Light(s->dlightbits, s->polys);
462 // FIXME: make fog texture if water texture is transparent?
463 for (p=s->polys ; p ; p=p->next)
465 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
466 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
467 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);
472 void RSurf_CheckLightmap(msurface_t *s)
476 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
477 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
478 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
479 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
480 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
481 R_UpdateLightmap(s, s->lightmaptexturenum);
485 void RSurf_Transform(glpoly_t *p, int transform)
488 float *v, *wv = wvert;
491 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
494 softwaretransform(v, wv);
497 wv[3] = wv[4] = wv[5] = 0.0f;
503 void RSurf_EmitWallpolys(int lightmap, glpoly_t *p, texture_t *t, int lit)
506 float *v, *wv = wvert;
507 wallpoly_t *wp = &wallpoly[currentwallpoly];
508 wallvert_t *out = &wallvert[currentwallvert];
511 if (currentwallpoly >= MAX_WALLPOLYS)
513 if (currentwallvert+p->numverts > MAX_WALLVERTS)
516 wp->texnum = (unsigned short) t->gl_texturenum;
517 wp->lighttexnum = (unsigned short) lightmap;
518 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
519 wp->firstvert = currentwallvert;
520 wp->numverts = p->numverts;
524 currentwallvert += p->numverts;
525 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
531 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
532 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
533 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
538 out->r = (byte) (bound(0, (int) wv[3], 255));
539 out->g = (byte) (bound(0, (int) wv[4], 255));
540 out->b = (byte) (bound(0, (int) wv[5], 255));
544 out->vert[0] = wv[0];
545 out->vert[1] = wv[1];
546 out->vert[2] = wv[2];
555 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
558 // check for lightmap modification
559 RSurf_CheckLightmap(s);
560 RSurf_Transform(s->polys, transform);
561 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
562 lit = RSurf_Light(s->dlightbits, s->polys);
563 RSurf_EmitWallpolys(lightmap_textures + s->lightmaptexturenum, s->polys, t, lit);
566 // LordHavoc: transparent brush models
567 extern int r_dlightframecount;
568 extern float modelalpha;
570 void RSurf_EmitWallVertex(glpoly_t *p, texture_t *t, int modulate, int alpha)
573 float *v, *wv = wvert;
579 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
580 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
581 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3] * currententity->colormod[0], wv[4] * currententity->colormod[1], wv[5] * currententity->colormod[2], alpha);
590 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
591 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
592 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
598 void RSurf_WallVertexTransform(msurface_t *s, texture_t *t, int transform)
606 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
608 for (p = s->polys;p;p = p->next)
610 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
613 softwaretransform(v, wv);
616 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
617 if (s->styles[0] != 255)
619 lm = (byte *)((long) s->samples + (int) v[7]);
620 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;
621 if (s->styles[1] != 255)
623 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;
624 if (s->styles[2] != 255)
626 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;
627 if (s->styles[3] != 255)
629 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;
639 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
641 RSurf_WallVertexTransform(s, t, transform);
642 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
643 RSurf_Light(s->dlightbits, s->polys);
644 RSurf_EmitWallVertex(s->polys, t, isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1), (int) (modelalpha * 255.0f));
652 extern qboolean hlbsp;
653 extern char skyname[];
654 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
657 if (s->flags & SURF_DRAWSKY)
660 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
661 RSurf_DrawSky(s, false);
664 t = R_TextureAnimation (s->texinfo->texture);
665 if (s->flags & SURF_DRAWTURB)
667 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
671 RSurf_DrawWallVertex(s, t, false, false);
673 RSurf_DrawWall(s, t, false);
676 void DrawTextureChains (void)
682 for (n = 0;n < cl.worldmodel->numtextures;n++)
684 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
686 cl.worldmodel->textures[n]->texturechain = NULL;
687 // for (;s;s = s->texturechain)
688 // R_DrawSurf(s, false, gl_vertex.value);
689 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
691 if (s->flags & SURF_DRAWSKY)
694 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
695 for (;s;s = s->texturechain)
696 RSurf_DrawSky(s, false);
699 t = R_TextureAnimation (cl.worldmodel->textures[n]);
700 // subdivided water surface warp
701 if (s->flags & SURF_DRAWTURB)
703 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
704 for (;s;s = s->texturechain)
705 RSurf_DrawWater(s, t, false, alpha);
709 for (;s;s = s->texturechain)
710 RSurf_DrawWallVertex(s, t, false, false);
712 for (;s;s = s->texturechain)
713 RSurf_DrawWall(s, t, false);
717 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
724 void R_DrawBrushModel (entity_t *e)
730 int rotated, vertexlit = false;
738 if (e->angles[0] || e->angles[1] || e->angles[2])
741 for (i=0 ; i<3 ; i++)
743 mins[i] = e->origin[i] - clmodel->radius;
744 maxs[i] = e->origin[i] + clmodel->radius;
750 VectorAdd (e->origin, clmodel->mins, mins);
751 VectorAdd (e->origin, clmodel->maxs, maxs);
754 if (R_CullBox (mins, maxs))
757 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
761 vec3_t forward, right, up;
763 VectorCopy (modelorg, temp);
764 AngleVectors (e->angles, forward, right, up);
765 modelorg[0] = DotProduct (temp, forward);
766 modelorg[1] = -DotProduct (temp, right);
767 modelorg[2] = DotProduct (temp, up);
770 s = &clmodel->surfaces[clmodel->firstmodelsurface];
772 // calculate dynamic lighting for bmodel if it's not an
774 for (i = 0;i < MAX_DLIGHTS;i++)
776 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
779 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
780 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
782 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
784 e->angles[0] = -e->angles[0]; // stupid quake bug
785 softwaretransformforentity (e);
786 e->angles[0] = -e->angles[0]; // stupid quake bug
789 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
791 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
793 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
794 if (s->flags & SURF_DRAWSKY)
796 RSurf_DrawSky(s, true);
799 t = R_TextureAnimation (s->texinfo->texture);
800 if (s->flags & SURF_DRAWTURB)
802 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
805 if (vertexlit || s->texinfo->texture->transparent)
806 RSurf_DrawWallVertex(s, t, true, true);
808 RSurf_DrawWall(s, t, true);
815 =============================================================
819 =============================================================
822 void R_StoreEfrags (efrag_t **ppefrag);
837 int side, texsort, vertex;
838 struct nodestack_s *nstack;
841 msurface_t *surf, *endsurf, **mark, **endmark;
843 texsort = gl_texsort.value;
844 vertex = gl_vertex.value;
846 if (!(node = cl.worldmodel->nodes))
851 // if a leaf node, draw stuff
852 if (node->contents < 0)
854 if (node->contents != CONTENTS_SOLID)
856 pleaf = (mleaf_t *)node;
859 if (pleaf->nummarksurfaces)
861 mark = pleaf->firstmarksurface;
862 endmark = mark + pleaf->nummarksurfaces;
865 (*mark)->visframe = r_framecount;
868 while (mark < endmark);
871 // deal with model fragments in this leaf
873 R_StoreEfrags (&pleaf->efrags);
876 if (nstack <= nodestack)
886 // node is just a decision point, so go down the apropriate sides
888 // find which side of the node we are on
889 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
891 // recurse down the children, front side first
892 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
895 nstack->side = !side; // go down back side when we come back up
897 node = node->children[side];
904 if (node->numsurfaces)
906 surf = cl.worldmodel->surfaces + node->firstsurface;
907 endsurf = surf + node->numsurfaces;
915 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
917 surf->texturechain = surf->texinfo->texture->texturechain;
918 surf->texinfo->texture->texturechain = surf;
922 while (surf < endsurf);
928 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
930 surf->texturechain = surf->texinfo->texture->texturechain;
931 surf->texinfo->texture->texturechain = surf;
935 while (surf < endsurf);
944 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
945 R_DrawSurf(surf, false, vertex);
948 while (surf < endsurf);
954 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
955 R_DrawSurf(surf, false, vertex);
958 while (surf < endsurf);
963 // recurse down the back side
964 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
966 node = node->children[side];
970 if (nstack <= nodestack)
985 void R_DrawWorld (void)
989 memset (&ent, 0, sizeof(ent));
990 ent.model = cl.worldmodel;
991 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
992 modelalpha = ent.alpha = 1;
995 VectorCopy (r_refdef.vieworg, modelorg);
997 currententity = &ent;
999 softwaretransformidentity(); // LordHavoc: clear transform
1004 R_PushDlights (); // now mark the lit surfaces
1006 DrawTextureChains ();
1015 void R_MarkLeaves (void)
1021 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1025 r_oldviewleaf = r_viewleaf;
1029 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1031 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1034 if (node->visframe == r_visframecount)
1036 node->visframe = r_visframecount;
1037 node = node->parent;
1043 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1045 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1047 if (vis[i>>3] & (1<<(i&7)))
1049 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1052 if (node->visframe == r_visframecount)
1054 node->visframe = r_visframecount;
1055 node = node->parent;
1065 =============================================================================
1069 =============================================================================
1072 // returns a texture number and the position inside it
1073 int AllocBlock (int w, int h, short *x, short *y)
1079 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1081 best = BLOCK_HEIGHT;
1083 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1087 for (j=0 ; j<w ; j++)
1089 if (allocated[texnum][i+j] >= best)
1091 if (allocated[texnum][i+j] > best2)
1092 best2 = allocated[texnum][i+j];
1095 { // this is a valid spot
1101 if (best + h > BLOCK_HEIGHT)
1104 if (nosubimagefragments || nosubimage)
1106 if (!lightmaps[texnum])
1107 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1109 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1110 else if (!allocated[texnum][0])
1112 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1113 memset(blank, 0, sizeof(blank));
1114 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1115 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1116 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1118 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1120 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1123 for (i=0 ; i<w ; i++)
1124 allocated[texnum][*x + i] = best + h;
1129 Sys_Error ("AllocBlock: full");
1134 mvertex_t *r_pcurrentvertbase;
1135 model_t *currentmodel;
1141 BuildSurfaceDisplayList
1144 void BuildSurfaceDisplayList (msurface_t *fa)
1146 int i, j, lindex, lnumverts;
1147 medge_t *pedges, *r_pedge;
1153 // reconstruct the polygon
1154 pedges = currentmodel->edges;
1155 lnumverts = fa->numedges;
1161 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1162 poly->next = fa->polys;
1163 poly->flags = fa->flags;
1165 poly->numverts = lnumverts;
1167 for (i=0 ; i<lnumverts ; i++)
1169 lindex = currentmodel->surfedges[fa->firstedge + i];
1173 r_pedge = &pedges[lindex];
1174 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1178 r_pedge = &pedges[-lindex];
1179 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1181 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1182 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1184 VectorCopy (vec, poly->verts[i]);
1185 poly->verts[i][3] = s / fa->texinfo->texture->width;
1186 poly->verts[i][4] = t / fa->texinfo->texture->height;
1189 // lightmap texture coordinates
1191 s -= fa->texturemins[0];
1192 t -= fa->texturemins[1];
1195 // LordHavoc: calc lightmap data offset
1196 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;
1197 poly->verts[i][7] = j;
1198 s += fa->light_s*16;
1199 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1201 t += fa->light_t*16;
1202 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1204 poly->verts[i][5] = s;
1205 poly->verts[i][6] = t;
1209 // remove co-linear points - Ed
1212 if (!gl_keeptjunctions.value)
1214 for (i = 0 ; i < lnumverts ; ++i)
1217 float *prev, *this, *next;
1219 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1220 this = poly->verts[i];
1221 next = poly->verts[(i + 1) % lnumverts];
1223 VectorSubtract( this, prev, v1 );
1224 VectorNormalize( v1 );
1225 VectorSubtract( next, prev, v2 );
1226 VectorNormalize( v2 );
1228 // skip co-linear points
1229 #define COLINEAR_EPSILON 0.001
1230 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1231 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1232 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1235 for (j = i + 1; j < lnumverts; ++j)
1238 for (k = 0; k < VERTEXSIZE; ++k)
1239 poly->verts[j - 1][k] = poly->verts[j][k];
1243 // retry next vertex next time, which is now current vertex
1249 poly->numverts = lnumverts;
1253 ========================
1254 GL_CreateSurfaceLightmap
1255 ========================
1257 void GL_CreateSurfaceLightmap (msurface_t *surf)
1261 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1264 smax = (surf->extents[0]>>4)+1;
1265 tmax = (surf->extents[1]>>4)+1;
1267 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1268 if (nosubimage || nosubimagefragments)
1270 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1271 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1274 R_BuildLightMap (surf, templight, smax * 4);
1275 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1279 R_BuildLightMap (surf, templight, smax * 3);
1280 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1289 Builds the lightmap texture
1290 with all the surfaces from all brush models
1293 void GL_BuildLightmaps (void)
1298 memset (allocated, 0, sizeof(allocated));
1300 r_framecount = 1; // no dlightcache
1302 if (gl_nosubimagefragments.value)
1303 nosubimagefragments = 1;
1305 nosubimagefragments = 0;
1307 if (gl_nosubimage.value)
1312 if (gl_lightmaprgba.value)
1314 lightmaprgba = true;
1319 lightmaprgba = false;
1323 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1324 // it needs to be aligned on 4 pixel boundaries...
1325 // so I implemented an adjustable lightmap alignment
1326 if (gl_lightmapalign.value < 1)
1327 gl_lightmapalign.value = 1;
1328 if (gl_lightmapalign.value > 16)
1329 gl_lightmapalign.value = 16;
1331 while (lightmapalign < gl_lightmapalign.value)
1332 lightmapalign <<= 1;
1333 gl_lightmapalign.value = lightmapalign;
1334 lightmapalignmask = ~(lightmapalign - 1);
1335 if (nosubimagefragments || nosubimage)
1338 lightmapalignmask = ~0;
1341 if (!lightmap_textures)
1343 lightmap_textures = texture_extension_number;
1344 texture_extension_number += MAX_LIGHTMAPS;
1347 for (j=1 ; j<MAX_MODELS ; j++)
1349 m = cl.model_precache[j];
1352 if (m->name[0] == '*')
1354 r_pcurrentvertbase = m->vertexes;
1356 for (i=0 ; i<m->numsurfaces ; i++)
1358 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1360 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1362 GL_CreateSurfaceLightmap (m->surfaces + i);
1363 BuildSurfaceDisplayList (m->surfaces + i);
1367 if (nosubimage || nosubimagefragments)
1370 qglSelectTexture(gl_mtex_enum+1);
1371 for (i = 0;i < MAX_LIGHTMAPS;i++)
1373 if (!allocated[i][0])
1375 lightmapupdate[i][0] = BLOCK_HEIGHT;
1376 lightmapupdate[i][1] = 0;
1377 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1378 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1379 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1381 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1383 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1386 qglSelectTexture(gl_mtex_enum+0);