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
227 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
228 if (nosubimage || nosubimagefragments)
230 if (lightmapupdate[lnum][0] > s->light_t)
231 lightmapupdate[lnum][0] = s->light_t;
232 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
233 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
235 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
237 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
241 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
242 tmax = (s->extents[1]>>4)+1;
245 R_BuildLightMap (s, templight, smax * 4);
247 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
251 R_BuildLightMap (s, templight, smax * 3);
253 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
263 Returns the proper texture for a given time and base texture
266 texture_t *R_TextureAnimation (texture_t *base)
272 if (currententity->frame)
274 if (base->alternate_anims)
275 base = base->alternate_anims;
278 if (!base->anim_total)
283 relative = (int)(cl.time*10) % base->anim_total;
286 while (base->anim_min > relative || base->anim_max <= relative)
288 base = base->anim_next;
291 Con_Printf("R_TextureAnimation: broken cycle");
296 Con_Printf("R_TextureAnimation: infinite cycle");
306 =============================================================
310 =============================================================
314 extern int solidskytexture;
315 extern int alphaskytexture;
316 extern float speedscale; // for top sky and bottom sky
318 extern char skyname[];
320 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
323 #include "gl_warp_sin.h"
325 #define TURBSCALE (256.0 / (2 * M_PI))
328 void UploadLightmaps()
331 if (nosubimage || nosubimagefragments)
333 for (i = 0;i < MAX_LIGHTMAPS;i++)
335 if (lightmapupdate[i][0] < lightmapupdate[i][1])
339 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
343 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
345 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
350 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]));
352 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]));
356 lightmapupdate[i][0] = BLOCK_HEIGHT;
357 lightmapupdate[i][1] = 0;
362 float wvert[1024*6]; // used by the following functions
364 void RSurf_DrawSky(msurface_t *s, int transform)
369 for (p=s->polys ; p ; p=p->next)
371 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
373 skypoly[currentskypoly].firstvert = currentskyvert;
374 skypoly[currentskypoly++].verts = p->numverts;
377 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
379 softwaretransform(v, skyvert[currentskyvert].v);
385 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
387 skyvert[currentskyvert].v[0] = v[0];
388 skyvert[currentskyvert].v[1] = v[1];
389 skyvert[currentskyvert++].v[2] = v[2];
396 int RSurf_Light(int *dlightbits, glpoly_t *polys)
398 float cr, cg, cb, radius, radius2, f, *v, *wv;
399 int i, a, b, lit = false;
404 for (a = 0;a < 8;a++)
406 if ((c = dlightbits[a]))
408 for (b = 0, d = 1;c;b++, d <<= 1)
413 light = &cl_dlights[a * 32 + b];
414 lightorigin = light->origin;
415 cr = light->color[0];
416 cg = light->color[1];
417 cb = light->color[2];
418 radius = light->radius*light->radius*LIGHTSCALE;
419 radius2 = radius * (256.0f / LIGHTSCALE);
421 for (p = polys;p;p = p->next)
423 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
425 f = VectorDistance2(wv, lightorigin);
428 f = radius2 / (f + LIGHTOFFSET);
444 void RSurf_DrawWater(msurface_t *s, texture_t *t, int transform, int alpha)
447 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
451 for (p = s->polys;p;p = p->next)
453 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
456 softwaretransform(v, wv);
459 if (r_waterripple.value)
460 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);
461 wv[3] = wv[4] = wv[5] = 128.0f;
465 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
466 RSurf_Light(s->dlightbits, s->polys);
468 // FIXME: make fog texture if water texture is transparent?
469 for (p=s->polys ; p ; p=p->next)
471 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
472 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
473 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);
478 void RSurf_CheckLightmap(msurface_t *s)
482 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
483 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
484 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
485 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
486 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
487 R_UpdateLightmap(s, s->lightmaptexturenum);
491 void RSurf_Transform(glpoly_t *p, int transform)
494 float *v, *wv = wvert;
497 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
500 softwaretransform(v, wv);
503 wv[3] = wv[4] = wv[5] = 0.0f;
509 void RSurf_EmitWallpolys(int lightmap, glpoly_t *p, texture_t *t, int lit)
512 float *v, *wv = wvert;
513 wallpoly_t *wp = &wallpoly[currentwallpoly];
514 wallvert_t *out = &wallvert[currentwallvert];
517 if (currentwallpoly >= MAX_WALLPOLYS)
519 if (currentwallvert+p->numverts > MAX_WALLVERTS)
522 wp->texnum = (unsigned short) t->gl_texturenum;
523 wp->lighttexnum = (unsigned short) lightmap;
524 wp->glowtexnum = (unsigned short) t->gl_glowtexturenum;
525 wp->firstvert = currentwallvert;
526 wp->numverts = p->numverts;
530 currentwallvert += p->numverts;
531 for (i = 0;i < p->numverts;i++, v += VERTEXSIZE, wv += 6, out++)
537 out->r = (byte) (bound(0, (int) wv[3] >> 1, 255));
538 out->g = (byte) (bound(0, (int) wv[4] >> 1, 255));
539 out->b = (byte) (bound(0, (int) wv[5] >> 1, 255));
544 out->r = (byte) (bound(0, (int) wv[3], 255));
545 out->g = (byte) (bound(0, (int) wv[4], 255));
546 out->b = (byte) (bound(0, (int) wv[5], 255));
550 out->vert[0] = wv[0];
551 out->vert[1] = wv[1];
552 out->vert[2] = wv[2];
561 void RSurf_DrawWall(msurface_t *s, texture_t *t, int transform)
564 // check for lightmap modification
565 RSurf_CheckLightmap(s);
566 RSurf_Transform(s->polys, transform);
567 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
568 lit = RSurf_Light(s->dlightbits, s->polys);
569 RSurf_EmitWallpolys(lightmap_textures + s->lightmaptexturenum, s->polys, t, lit);
572 // LordHavoc: transparent brush models
573 extern int r_dlightframecount;
574 extern float modelalpha;
576 void RSurf_EmitWallVertex(glpoly_t *p, texture_t *t, int modulate, int alpha)
579 float *v, *wv = wvert;
585 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
586 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
587 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);
596 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
597 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
598 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
604 void RSurf_WallVertexTransform(msurface_t *s, texture_t *t, int transform)
612 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
614 for (p = s->polys;p;p = p->next)
616 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
619 softwaretransform(v, wv);
622 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
623 if (s->styles[0] != 255)
625 lm = (byte *)((long) s->samples + (int) v[7]);
626 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;
627 if (s->styles[1] != 255)
629 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;
630 if (s->styles[2] != 255)
632 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;
633 if (s->styles[3] != 255)
635 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;
645 void RSurf_DrawWallVertex(msurface_t *s, texture_t *t, int transform, int isbmodel)
647 RSurf_WallVertexTransform(s, t, transform);
648 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
649 RSurf_Light(s->dlightbits, s->polys);
650 RSurf_EmitWallVertex(s->polys, t, isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1), (int) (modelalpha * 255.0f));
658 extern qboolean hlbsp;
659 extern char skyname[];
660 void R_DrawSurf(msurface_t *s, int isbmodel, int vertexlit)
663 if (s->flags & SURF_DRAWSKY)
666 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
667 RSurf_DrawSky(s, false);
670 t = R_TextureAnimation (s->texinfo->texture);
671 if (s->flags & SURF_DRAWTURB)
673 RSurf_DrawWater(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
677 RSurf_DrawWallVertex(s, t, false, false);
679 RSurf_DrawWall(s, t, false);
682 void DrawTextureChains (void)
688 for (n = 0;n < cl.worldmodel->numtextures;n++)
690 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
692 cl.worldmodel->textures[n]->texturechain = NULL;
693 // for (;s;s = s->texturechain)
694 // R_DrawSurf(s, false, gl_vertex.value);
695 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
697 if (s->flags & SURF_DRAWSKY)
700 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
701 for (;s;s = s->texturechain)
702 RSurf_DrawSky(s, false);
705 t = R_TextureAnimation (cl.worldmodel->textures[n]);
706 // subdivided water surface warp
707 if (s->flags & SURF_DRAWTURB)
709 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
710 for (;s;s = s->texturechain)
711 RSurf_DrawWater(s, t, false, alpha);
715 for (;s;s = s->texturechain)
716 RSurf_DrawWallVertex(s, t, false, false);
718 for (;s;s = s->texturechain)
719 RSurf_DrawWall(s, t, false);
723 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
730 void R_DrawBrushModel (entity_t *e)
736 int rotated, vertexlit = false;
744 if (e->angles[0] || e->angles[1] || e->angles[2])
747 for (i=0 ; i<3 ; i++)
749 mins[i] = e->origin[i] - clmodel->radius;
750 maxs[i] = e->origin[i] + clmodel->radius;
756 VectorAdd (e->origin, clmodel->mins, mins);
757 VectorAdd (e->origin, clmodel->maxs, maxs);
760 if (R_CullBox (mins, maxs))
763 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
767 vec3_t forward, right, up;
769 VectorCopy (modelorg, temp);
770 AngleVectors (e->angles, forward, right, up);
771 modelorg[0] = DotProduct (temp, forward);
772 modelorg[1] = -DotProduct (temp, right);
773 modelorg[2] = DotProduct (temp, up);
776 s = &clmodel->surfaces[clmodel->firstmodelsurface];
778 // calculate dynamic lighting for bmodel if it's not an
780 for (i = 0;i < MAX_DLIGHTS;i++)
782 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
785 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
786 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
788 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
790 e->angles[0] = -e->angles[0]; // stupid quake bug
791 softwaretransformforentity (e);
792 e->angles[0] = -e->angles[0]; // stupid quake bug
795 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
797 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
799 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
800 if (s->flags & SURF_DRAWSKY)
802 RSurf_DrawSky(s, true);
805 t = R_TextureAnimation (s->texinfo->texture);
806 if (s->flags & SURF_DRAWTURB)
808 RSurf_DrawWater(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
811 if (vertexlit || s->texinfo->texture->transparent)
812 RSurf_DrawWallVertex(s, t, true, true);
814 RSurf_DrawWall(s, t, true);
821 =============================================================
825 =============================================================
828 void R_StoreEfrags (efrag_t **ppefrag);
843 int side, texsort, vertex;
844 struct nodestack_s *nstack;
847 msurface_t *surf, *endsurf, **mark, **endmark;
849 texsort = gl_texsort.value;
850 vertex = gl_vertex.value;
852 if (!(node = cl.worldmodel->nodes))
857 // if a leaf node, draw stuff
858 if (node->contents < 0)
860 if (node->contents != CONTENTS_SOLID)
862 pleaf = (mleaf_t *)node;
865 if (pleaf->nummarksurfaces)
867 mark = pleaf->firstmarksurface;
868 endmark = mark + pleaf->nummarksurfaces;
871 (*mark)->visframe = r_framecount;
874 while (mark < endmark);
877 // deal with model fragments in this leaf
879 R_StoreEfrags (&pleaf->efrags);
882 if (nstack <= nodestack)
892 // node is just a decision point, so go down the apropriate sides
894 // find which side of the node we are on
895 side = PlaneDist(modelorg, node->plane) < node->plane->dist;
897 // recurse down the children, front side first
898 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
901 nstack->side = !side; // go down back side when we come back up
903 node = node->children[side];
910 if (node->numsurfaces)
912 surf = cl.worldmodel->surfaces + node->firstsurface;
913 endsurf = surf + node->numsurfaces;
921 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
923 surf->texturechain = surf->texinfo->texture->texturechain;
924 surf->texinfo->texture->texturechain = surf;
928 while (surf < endsurf);
934 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
936 surf->texturechain = surf->texinfo->texture->texturechain;
937 surf->texinfo->texture->texturechain = surf;
941 while (surf < endsurf);
950 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
951 R_DrawSurf(surf, false, vertex);
954 while (surf < endsurf);
960 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
961 R_DrawSurf(surf, false, vertex);
964 while (surf < endsurf);
969 // recurse down the back side
970 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
972 node = node->children[side];
976 if (nstack <= nodestack)
991 void R_DrawWorld (void)
995 memset (&ent, 0, sizeof(ent));
996 ent.model = cl.worldmodel;
997 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
998 modelalpha = ent.alpha = 1;
1001 VectorCopy (r_refdef.vieworg, modelorg);
1003 currententity = &ent;
1005 softwaretransformidentity(); // LordHavoc: clear transform
1010 R_PushDlights (); // now mark the lit surfaces
1012 DrawTextureChains ();
1021 void R_MarkLeaves (void)
1027 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
1031 r_oldviewleaf = r_viewleaf;
1035 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1037 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1040 if (node->visframe == r_visframecount)
1042 node->visframe = r_visframecount;
1043 node = node->parent;
1049 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
1051 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
1053 if (vis[i>>3] & (1<<(i&7)))
1055 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1058 if (node->visframe == r_visframecount)
1060 node->visframe = r_visframecount;
1061 node = node->parent;
1071 =============================================================================
1075 =============================================================================
1078 // returns a texture number and the position inside it
1079 int AllocBlock (int w, int h, short *x, short *y)
1085 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1087 best = BLOCK_HEIGHT;
1089 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1093 for (j=0 ; j<w ; j++)
1095 if (allocated[texnum][i+j] >= best)
1097 if (allocated[texnum][i+j] > best2)
1098 best2 = allocated[texnum][i+j];
1101 { // this is a valid spot
1107 if (best + h > BLOCK_HEIGHT)
1110 if (nosubimagefragments || nosubimage)
1112 if (!lightmaps[texnum])
1113 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1115 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1116 else if (!allocated[texnum][0])
1118 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1119 memset(blank, 0, sizeof(blank));
1122 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1123 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1124 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1126 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1128 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1132 for (i=0 ; i<w ; i++)
1133 allocated[texnum][*x + i] = best + h;
1138 Sys_Error ("AllocBlock: full");
1143 mvertex_t *r_pcurrentvertbase;
1144 model_t *currentmodel;
1150 BuildSurfaceDisplayList
1153 void BuildSurfaceDisplayList (msurface_t *fa)
1155 int i, j, lindex, lnumverts;
1156 medge_t *pedges, *r_pedge;
1162 // reconstruct the polygon
1163 pedges = currentmodel->edges;
1164 lnumverts = fa->numedges;
1170 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1171 poly->next = fa->polys;
1172 poly->flags = fa->flags;
1174 poly->numverts = lnumverts;
1176 for (i=0 ; i<lnumverts ; i++)
1178 lindex = currentmodel->surfedges[fa->firstedge + i];
1182 r_pedge = &pedges[lindex];
1183 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1187 r_pedge = &pedges[-lindex];
1188 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1190 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1191 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1193 VectorCopy (vec, poly->verts[i]);
1194 poly->verts[i][3] = s / fa->texinfo->texture->width;
1195 poly->verts[i][4] = t / fa->texinfo->texture->height;
1198 // lightmap texture coordinates
1200 s -= fa->texturemins[0];
1201 t -= fa->texturemins[1];
1204 // LordHavoc: calc lightmap data offset
1205 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;
1206 poly->verts[i][7] = j;
1207 s += fa->light_s*16;
1208 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1210 t += fa->light_t*16;
1211 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1213 poly->verts[i][5] = s;
1214 poly->verts[i][6] = t;
1218 // remove co-linear points - Ed
1221 if (!gl_keeptjunctions.value)
1223 for (i = 0 ; i < lnumverts ; ++i)
1226 float *prev, *this, *next;
1228 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1229 this = poly->verts[i];
1230 next = poly->verts[(i + 1) % lnumverts];
1232 VectorSubtract( this, prev, v1 );
1233 VectorNormalize( v1 );
1234 VectorSubtract( next, prev, v2 );
1235 VectorNormalize( v2 );
1237 // skip co-linear points
1238 #define COLINEAR_EPSILON 0.001
1239 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1240 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1241 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1244 for (j = i + 1; j < lnumverts; ++j)
1247 for (k = 0; k < VERTEXSIZE; ++k)
1248 poly->verts[j - 1][k] = poly->verts[j][k];
1252 // retry next vertex next time, which is now current vertex
1258 poly->numverts = lnumverts;
1262 ========================
1263 GL_CreateSurfaceLightmap
1264 ========================
1266 void GL_CreateSurfaceLightmap (msurface_t *surf)
1270 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1273 smax = (surf->extents[0]>>4)+1;
1274 tmax = (surf->extents[1]>>4)+1;
1276 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1277 if (nosubimage || nosubimagefragments)
1279 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1280 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1283 R_BuildLightMap (surf, templight, smax * 4);
1285 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1289 R_BuildLightMap (surf, templight, smax * 3);
1291 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1300 Builds the lightmap texture
1301 with all the surfaces from all brush models
1304 void GL_BuildLightmaps (void)
1309 memset (allocated, 0, sizeof(allocated));
1311 r_framecount = 1; // no dlightcache
1313 if (gl_nosubimagefragments.value)
1314 nosubimagefragments = 1;
1316 nosubimagefragments = 0;
1318 if (gl_nosubimage.value)
1323 if (gl_lightmaprgba.value)
1325 lightmaprgba = true;
1330 lightmaprgba = false;
1334 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1335 // it needs to be aligned on 4 pixel boundaries...
1336 // so I implemented an adjustable lightmap alignment
1337 if (gl_lightmapalign.value < 1)
1338 gl_lightmapalign.value = 1;
1339 if (gl_lightmapalign.value > 16)
1340 gl_lightmapalign.value = 16;
1342 while (lightmapalign < gl_lightmapalign.value)
1343 lightmapalign <<= 1;
1344 gl_lightmapalign.value = lightmapalign;
1345 lightmapalignmask = ~(lightmapalign - 1);
1346 if (nosubimagefragments || nosubimage)
1349 lightmapalignmask = ~0;
1352 if (!lightmap_textures)
1354 lightmap_textures = texture_extension_number;
1355 texture_extension_number += MAX_LIGHTMAPS;
1358 for (j=1 ; j<MAX_MODELS ; j++)
1360 m = cl.model_precache[j];
1363 if (m->name[0] == '*')
1365 r_pcurrentvertbase = m->vertexes;
1367 for (i=0 ; i<m->numsurfaces ; i++)
1369 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1371 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1373 GL_CreateSurfaceLightmap (m->surfaces + i);
1374 BuildSurfaceDisplayList (m->surfaces + i);
1378 if (nosubimage || nosubimagefragments)
1382 qglSelectTexture(gl_mtex_enum+1);
1383 for (i = 0;i < MAX_LIGHTMAPS;i++)
1385 if (!allocated[i][0])
1387 lightmapupdate[i][0] = BLOCK_HEIGHT;
1388 lightmapupdate[i][1] = 0;
1391 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1392 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1393 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1395 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1397 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1402 qglSelectTexture(gl_mtex_enum+0);