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);
840 if (!(node = cl.worldmodel->nodes))
845 // if a leaf node, draw stuff
846 if (node->contents < 0)
848 if (node->contents != CONTENTS_SOLID)
851 pleaf = (mleaf_t *)node;
854 if ((c = pleaf->nummarksurfaces))
857 mark = pleaf->firstmarksurface;
860 (*mark)->visframe = r_framecount;
865 // deal with model fragments in this leaf
867 R_StoreEfrags (&pleaf->efrags);
872 node = nodestack[--s].node;
873 dot = nodestack[s].dot;
879 // node is just a decision point, so go down the apropriate sides
881 // find which side of the node we are on
882 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
884 // recurse down the children, front side first
886 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
888 nodestack[s].node = node;
889 nodestack[s++].dot = dot;
890 node = node->children[side];
898 if ((c = node->numsurfaces))
901 surf = cl.worldmodel->surfaces + node->firstsurface;
907 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
909 if (gl_texsort.value)
911 surf->texturechain = surf->texinfo->texture->texturechain;
912 surf->texinfo->texture->texturechain = surf;
915 R_DrawSurf(surf, false, gl_vertex.value);
923 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
925 if (gl_texsort.value)
927 surf->texturechain = surf->texinfo->texture->texturechain;
928 surf->texinfo->texture->texturechain = surf;
931 R_DrawSurf(surf, false, gl_vertex.value);
937 // recurse down the back side
938 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
940 node = node->children[side];
946 node = nodestack[--s].node;
947 dot = nodestack[s].dot;
958 void R_DrawWorld (void)
962 memset (&ent, 0, sizeof(ent));
963 ent.model = cl.worldmodel;
964 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
965 modelalpha = ent.alpha = 1;
968 VectorCopy (r_refdef.vieworg, modelorg);
970 currententity = &ent;
972 softwaretransformidentity(); // LordHavoc: clear transform
977 glClear (GL_DEPTH_BUFFER_BIT);
979 R_PushDlights (); // now mark the lit surfaces
981 DrawTextureChains ();
983 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
984 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
993 void R_MarkLeaves (void)
999 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1003 r_oldviewleaf = r_viewleaf;
1007 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1009 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1012 if (node->visframe == r_visframecount)
1014 node->visframe = r_visframecount;
1015 node = node->parent;
1021 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1023 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1025 if (vis[i>>3] & (1<<(i&7)))
1027 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1030 if (node->visframe == r_visframecount)
1032 node->visframe = r_visframecount;
1033 node = node->parent;
1043 =============================================================================
1047 =============================================================================
1050 // returns a texture number and the position inside it
1051 int AllocBlock (int w, int h, int *x, int *y)
1057 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1059 best = BLOCK_HEIGHT;
1061 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1065 for (j=0 ; j<w ; j++)
1067 if (allocated[texnum][i+j] >= best)
1069 if (allocated[texnum][i+j] > best2)
1070 best2 = allocated[texnum][i+j];
1073 { // this is a valid spot
1079 if (best + h > BLOCK_HEIGHT)
1082 if (nosubimagefragments || nosubimage)
1084 if (!lightmaps[texnum])
1085 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1087 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1088 else if (!allocated[texnum][0])
1090 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1091 memset(blank, 0, sizeof(blank));
1092 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1093 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1094 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1096 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1098 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1101 for (i=0 ; i<w ; i++)
1102 allocated[texnum][*x + i] = best + h;
1107 Sys_Error ("AllocBlock: full");
1112 mvertex_t *r_pcurrentvertbase;
1113 model_t *currentmodel;
1119 BuildSurfaceDisplayList
1122 void BuildSurfaceDisplayList (msurface_t *fa)
1124 int i, j, lindex, lnumverts;
1125 medge_t *pedges, *r_pedge;
1131 // reconstruct the polygon
1132 pedges = currentmodel->edges;
1133 lnumverts = fa->numedges;
1139 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1140 poly->next = fa->polys;
1141 poly->flags = fa->flags;
1143 poly->numverts = lnumverts;
1145 for (i=0 ; i<lnumverts ; i++)
1147 lindex = currentmodel->surfedges[fa->firstedge + i];
1151 r_pedge = &pedges[lindex];
1152 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1156 r_pedge = &pedges[-lindex];
1157 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1159 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1160 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1162 VectorCopy (vec, poly->verts[i]);
1163 poly->verts[i][3] = s / fa->texinfo->texture->width;
1164 poly->verts[i][4] = t / fa->texinfo->texture->height;
1167 // lightmap texture coordinates
1169 s -= fa->texturemins[0];
1170 t -= fa->texturemins[1];
1173 // LordHavoc: calc lightmap data offset
1174 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;
1175 poly->verts[i][7] = j;
1176 s += fa->light_s*16;
1177 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1179 t += fa->light_t*16;
1180 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1182 poly->verts[i][5] = s;
1183 poly->verts[i][6] = t;
1187 // remove co-linear points - Ed
1190 if (!gl_keeptjunctions.value)
1192 for (i = 0 ; i < lnumverts ; ++i)
1195 float *prev, *this, *next;
1197 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1198 this = poly->verts[i];
1199 next = poly->verts[(i + 1) % lnumverts];
1201 VectorSubtract( this, prev, v1 );
1202 VectorNormalize( v1 );
1203 VectorSubtract( next, prev, v2 );
1204 VectorNormalize( v2 );
1206 // skip co-linear points
1207 #define COLINEAR_EPSILON 0.001
1208 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1209 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1210 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1213 for (j = i + 1; j < lnumverts; ++j)
1216 for (k = 0; k < VERTEXSIZE; ++k)
1217 poly->verts[j - 1][k] = poly->verts[j][k];
1221 // retry next vertex next time, which is now current vertex
1227 poly->numverts = lnumverts;
1231 ========================
1232 GL_CreateSurfaceLightmap
1233 ========================
1235 void GL_CreateSurfaceLightmap (msurface_t *surf)
1239 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1242 smax = (surf->extents[0]>>4)+1;
1243 tmax = (surf->extents[1]>>4)+1;
1245 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1246 if (nosubimage || nosubimagefragments)
1248 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1249 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1252 R_BuildLightMap (surf, templight, smax * 4);
1253 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1257 R_BuildLightMap (surf, templight, smax * 3);
1258 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1267 Builds the lightmap texture
1268 with all the surfaces from all brush models
1271 void GL_BuildLightmaps (void)
1276 memset (allocated, 0, sizeof(allocated));
1278 r_framecount = 1; // no dlightcache
1280 if (gl_nosubimagefragments.value)
1281 nosubimagefragments = 1;
1283 nosubimagefragments = 0;
1285 if (gl_nosubimage.value)
1290 if (gl_lightmaprgba.value)
1292 lightmaprgba = true;
1297 lightmaprgba = false;
1301 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1302 // it needs to be aligned on 4 pixel boundaries...
1303 // so I implemented an adjustable lightmap alignment
1304 if (gl_lightmapalign.value < 1)
1305 gl_lightmapalign.value = 1;
1306 if (gl_lightmapalign.value > 16)
1307 gl_lightmapalign.value = 16;
1309 while (lightmapalign < gl_lightmapalign.value)
1310 lightmapalign <<= 1;
1311 gl_lightmapalign.value = lightmapalign;
1312 lightmapalignmask = ~(lightmapalign - 1);
1313 if (nosubimagefragments || nosubimage)
1316 lightmapalignmask = ~0;
1319 if (!lightmap_textures)
1321 lightmap_textures = texture_extension_number;
1322 texture_extension_number += MAX_LIGHTMAPS;
1325 for (j=1 ; j<MAX_MODELS ; j++)
1327 m = cl.model_precache[j];
1330 if (m->name[0] == '*')
1332 r_pcurrentvertbase = m->vertexes;
1334 for (i=0 ; i<m->numsurfaces ; i++)
1336 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1338 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1340 GL_CreateSurfaceLightmap (m->surfaces + i);
1341 BuildSurfaceDisplayList (m->surfaces + i);
1345 if (nosubimage || nosubimagefragments)
1348 qglSelectTexture(gl_mtex_enum+1);
1349 for (i = 0;i < MAX_LIGHTMAPS;i++)
1351 if (!allocated[i][0])
1353 lightmapupdate[i][0] = BLOCK_HEIGHT;
1354 lightmapupdate[i][1] = 0;
1355 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1356 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1357 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1359 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1361 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1364 qglSelectTexture(gl_mtex_enum+0);