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;
57 qboolean skyisvisible;
58 extern qboolean gl_arrays;
60 extern int r_dlightframecount;
65 for (i = 0;i < MAX_LIGHTMAPS;i++)
67 Cvar_RegisterVariable(&gl_lightmapalign);
68 Cvar_RegisterVariable(&gl_lightmaprgba);
69 Cvar_RegisterVariable(&gl_nosubimagefragments);
70 Cvar_RegisterVariable(&gl_nosubimage);
71 Cvar_RegisterVariable(&r_ambient);
72 // Cvar_RegisterVariable(&gl_funnywalls);
73 Cvar_RegisterVariable(&gl_vertex);
74 Cvar_RegisterVariable(&gl_texsort);
75 // check if it's the glquake minigl driver
76 if (strncasecmp(gl_vendor,"3Dfx",4)==0)
79 // Cvar_SetValue("gl_nosubimagefragments", 1);
80 // Cvar_SetValue("gl_nosubimage", 1);
81 Cvar_SetValue("gl_lightmode", 0);
85 extern qboolean lighthalf;
90 Combine and scale multiple lightmaps into the 8.8 format in blocklights
93 void R_BuildLightMap (msurface_t *surf, byte *dest, int stride)
103 surf->cached_lighthalf = lighthalf;
104 surf->cached_ambient = r_ambient.value;
106 smax = (surf->extents[0]>>4)+1;
107 tmax = (surf->extents[1]>>4)+1;
109 lightmap = surf->samples;
111 // set to full bright if no light data
112 if (currententity->effects & EF_FULLBRIGHT || !cl.worldmodel->lightdata)
115 for (i=0 ; i<size ; i++)
126 j = r_ambient.value * 512.0f; // would be 256.0f logically, but using 512.0f to match winquake style
127 for (i=0 ; i<size ; i++)
134 // add all the lightmaps
136 for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++)
138 scale = d_lightstylevalue[surf->styles[maps]];
139 surf->cached_light[maps] = scale; // 8.8 fraction
141 for (i=0 ; i<size ; i++)
143 *bl++ += *lightmap++ * scale;
144 *bl++ += *lightmap++ * scale;
145 *bl++ += *lightmap++ * scale;
149 stride -= (smax*lightmapbytes);
153 // LordHavoc: I shift down by 8 unlike GLQuake's 7,
154 // the image is brightened as a processing pass
157 for (i=0 ; i<tmax ; i++, dest += stride)
159 for (j=0 ; j<smax ; j++)
161 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
162 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
163 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
170 for (i=0 ; i<tmax ; i++, dest += stride)
172 for (j=0 ; j<smax ; j++)
174 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
175 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
176 t = *bl++ >> 8;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
185 for (i=0 ; i<tmax ; i++, dest += stride)
187 for (j=0 ; j<smax ; j++)
189 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
190 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
191 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
198 for (i=0 ; i<tmax ; i++, dest += stride)
200 for (j=0 ; j<smax ; j++)
202 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
203 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
204 t = *bl++ >> 7;if (t > 255) t = 255;else if (t < 0) t = 0;*dest++ = t;
211 byte templight[32*32*4];
213 void R_UpdateLightmap(msurface_t *s, int lnum)
216 // upload the new lightmap texture fragment
217 glBindTexture(GL_TEXTURE_2D, lightmap_textures + lnum);
218 if (nosubimage || nosubimagefragments)
220 if (lightmapupdate[lnum][0] > s->light_t)
221 lightmapupdate[lnum][0] = s->light_t;
222 if (lightmapupdate[lnum][1] < (s->light_t + ((s->extents[1]>>4)+1)))
223 lightmapupdate[lnum][1] = (s->light_t + ((s->extents[1]>>4)+1));
225 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 4, BLOCK_WIDTH * 4);
227 R_BuildLightMap (s, lightmaps[s->lightmaptexturenum] + (s->light_t * BLOCK_WIDTH + s->light_s) * 3, BLOCK_WIDTH * 3);
231 smax = ((s->extents[0]>>4)+lightmapalign) & lightmapalignmask;
232 tmax = (s->extents[1]>>4)+1;
235 R_BuildLightMap (s, templight, smax * 4);
236 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
240 R_BuildLightMap (s, templight, smax * 3);
241 glTexSubImage2D(GL_TEXTURE_2D, 0, s->light_s, s->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
251 Returns the proper texture for a given time and base texture
254 texture_t *R_TextureAnimation (texture_t *base)
260 if (currententity->frame)
262 if (base->alternate_anims)
263 base = base->alternate_anims;
266 if (!base->anim_total)
271 relative = (int)(cl.time*10) % base->anim_total;
274 while (base->anim_min > relative || base->anim_max <= relative)
276 base = base->anim_next;
279 Con_Printf("R_TextureAnimation: broken cycle");
284 Con_Printf("R_TextureAnimation: infinite cycle");
294 =============================================================
298 =============================================================
302 extern int solidskytexture;
303 extern int alphaskytexture;
304 extern float speedscale; // for top sky and bottom sky
306 qboolean mtexenabled = false;
308 extern char skyname[];
310 void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
311 //extern cvar_t r_dynamicwater;
314 #include "gl_warp_sin.h"
316 #define TURBSCALE (256.0 / (2 * M_PI))
319 void UploadLightmaps()
322 if (nosubimage || nosubimagefragments)
324 for (i = 0;i < MAX_LIGHTMAPS;i++)
326 if (lightmapupdate[i][0] < lightmapupdate[i][1])
328 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
332 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
334 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
339 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]));
341 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]));
344 lightmapupdate[i][0] = BLOCK_HEIGHT;
345 lightmapupdate[i][1] = 0;
350 void R_SkySurf(msurface_t *s, qboolean transform)
355 for (p=s->polys ; p ; p=p->next)
357 if (currentskypoly < MAX_SKYPOLYS && currentskyvert + p->numverts <= MAX_SKYVERTS)
359 skypoly[currentskypoly].firstvert = currentskyvert;
360 skypoly[currentskypoly++].verts = p->numverts;
363 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
365 softwaretransform(v, skyvert[currentskyvert].v);
371 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
373 skyvert[currentskyvert].v[0] = v[0];
374 skyvert[currentskyvert].v[1] = v[1];
375 skyvert[currentskyvert++].v[2] = v[2];
382 void R_LightSurface(int *dlightbits, glpoly_t *polys, float *wvert)
384 float cr, cg, cb, radius, radius2, f, *v, *wv;
390 for (a = 0;a < 8;a++)
392 if ((c = dlightbits[a]))
394 for (b = 0;c && b < 32;b++)
399 light = &cl_dlights[a * 32 + b];
400 lightorigin = light->origin;
401 cr = light->color[0];
402 cg = light->color[1];
403 cb = light->color[2];
404 radius = light->radius*light->radius*16.0f;
405 radius2 = radius * 16.0f;
407 for (p = polys;p;p = p->next)
409 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
411 f = VectorDistance2(wv, lightorigin);
414 f = radius2 / (f + 65536.0f);
430 void R_WaterSurf(msurface_t *s, texture_t *t, qboolean transform, int alpha)
433 float os = turbsin[(int)(realtime * TURBSCALE) & 255], ot = turbsin[(int)(realtime * TURBSCALE + 96.0) & 255];
435 float wvert[1024*6], *wv, *v;
437 for (p = s->polys;p;p = p->next)
439 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
442 softwaretransform(v, wv);
445 if (r_waterripple.value)
446 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);
447 wv[3] = wv[4] = wv[5] = 128.0f;
451 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
452 R_LightSurface(s->dlightbits, s->polys, wvert);
454 // FIXME: make fog texture if water texture is transparent?
455 for (p=s->polys ; p ; p=p->next)
457 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, TPOLYTYPE_ALPHA);
458 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
459 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);
464 void R_WallSurf(msurface_t *s, texture_t *t, qboolean transform)
468 float wvert[64*6], *wv, *v;
469 // check for lightmap modification
472 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
473 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
474 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
475 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
476 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
477 R_UpdateLightmap(s, s->lightmaptexturenum);
480 for (p = s->polys;p;p = p->next)
482 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
485 softwaretransform(v, wv);
488 wv[3] = wv[4] = wv[5] = 0.0f;
492 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
493 R_LightSurface(s->dlightbits, s->polys, wvert);
495 for (p = s->polys;p;p = p->next)
497 if (currentwallpoly >= MAX_WALLPOLYS)
500 wallpoly[currentwallpoly].texnum = (unsigned short) t->gl_texturenum;
501 wallpoly[currentwallpoly].lighttexnum = (unsigned short) lightmap_textures + s->lightmaptexturenum;
502 wallpoly[currentwallpoly].glowtexnum = (unsigned short) t->gl_glowtexturenum;
503 wallpoly[currentwallpoly].firstvert = currentwallvert;
504 wallpoly[currentwallpoly].numverts = p->numverts;
505 wallpoly[currentwallpoly++].lit = true;
506 for (i = 0;i<p->numverts;i++, v += VERTEXSIZE)
510 wallvert[currentwallvert].r = (byte) (bound(0, (int) wv[3] >> 1, 255));
511 wallvert[currentwallvert].g = (byte) (bound(0, (int) wv[4] >> 1, 255));
512 wallvert[currentwallvert].b = (byte) (bound(0, (int) wv[5] >> 1, 255));
513 wallvert[currentwallvert].a = 255;
517 wallvert[currentwallvert].r = (byte) (bound(0, (int) wv[3], 255));
518 wallvert[currentwallvert].g = (byte) (bound(0, (int) wv[4], 255));
519 wallvert[currentwallvert].b = (byte) (bound(0, (int) wv[5], 255));
520 wallvert[currentwallvert].a = 255;
522 wallvert[currentwallvert].vert[0] = wv[0];
523 wallvert[currentwallvert].vert[1] = wv[1];
524 wallvert[currentwallvert].vert[2] = wv[2];
525 wallvert[currentwallvert].s = v[3];
526 wallvert[currentwallvert].t = v[4];
527 wallvert[currentwallvert].u = v[5];
528 wallvert[currentwallvert++].v = v[6];
534 // LordHavoc: transparent brush models
535 extern int r_dlightframecount;
536 extern float modelalpha;
537 //extern vec3_t shadecolor;
538 //qboolean R_CullBox (vec3_t mins, vec3_t maxs);
539 //void R_DynamicLightPoint(vec3_t color, vec3_t org, int *dlightbits);
540 //void R_DynamicLightPointNoMask(vec3_t color, vec3_t org);
541 //void EmitWaterPolys (msurface_t *fa);
543 void R_WallSurfVertex(msurface_t *s, texture_t *t, qboolean transform, qboolean isbmodel)
547 float wvert[64*6], *wv, *v;
551 size3 = ((s->extents[0]>>4)+1)*((s->extents[1]>>4)+1)*3; // *3 for colored lighting
552 alpha = (int) (modelalpha * 255.0f);
553 // check for lightmap modification
556 if (r_ambient.value != s->cached_ambient || lighthalf != s->cached_lighthalf
557 || (s->styles[0] != 255 && d_lightstylevalue[s->styles[0]] != s->cached_light[0])
558 || (s->styles[1] != 255 && d_lightstylevalue[s->styles[1]] != s->cached_light[1])
559 || (s->styles[2] != 255 && d_lightstylevalue[s->styles[2]] != s->cached_light[2])
560 || (s->styles[3] != 255 && d_lightstylevalue[s->styles[3]] != s->cached_light[3]))
561 R_UpdateLightmap(s, s->lightmaptexturenum);
564 for (p = s->polys;p;p = p->next)
566 for (i = 0, v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE)
569 softwaretransform(v, wv);
572 wv[3] = wv[4] = wv[5] = r_ambient.value * 2.0f;
573 if (s->styles[0] != 255)
575 lm = (byte *)((long) s->samples + (int) v[7]);
576 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;
577 if (s->styles[1] != 255)
579 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;
580 if (s->styles[2] != 255)
582 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;
583 if (s->styles[3] != 255)
585 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;
593 if (s->dlightframe == r_dlightframecount && r_dynamic.value)
594 R_LightSurface(s->dlightbits, s->polys, wvert);
596 if (isbmodel && (currententity->colormod[0] != 1 || currententity->colormod[1] != 1 || currententity->colormod[2] != 1))
598 for (p = s->polys;p;p = p->next)
601 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
602 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
603 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);
609 for (p = s->polys;p;p = p->next)
612 transpolybegin(t->gl_texturenum, t->gl_glowtexturenum, 0, currententity->effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
613 for (i = 0,v = p->verts[0];i < p->numverts;i++, v += VERTEXSIZE, wv += 6)
614 transpolyvert(wv[0], wv[1], wv[2], v[3], v[4], wv[3], wv[4], wv[5], alpha);
625 extern qboolean hlbsp;
626 extern char skyname[];
627 //extern qboolean SV_TestLine (hull_t *hull, int num, vec3_t p1, vec3_t p2);
628 void R_DrawSurf(msurface_t *s, qboolean isbmodel, qboolean vertexlit)
631 if (s->flags & SURF_DRAWSKY)
634 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
638 t = R_TextureAnimation (s->texinfo->texture);
639 if (s->flags & SURF_DRAWTURB)
641 R_WaterSurf(s, t, false, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
645 R_WallSurfVertex(s, t, false, false);
647 R_WallSurf(s, t, false);
650 void DrawTextureChains (void)
656 for (n = 0;n < cl.worldmodel->numtextures;n++)
658 if (!cl.worldmodel->textures[n] || !(s = cl.worldmodel->textures[n]->texturechain))
660 cl.worldmodel->textures[n]->texturechain = NULL;
661 // for (;s;s = s->texturechain)
662 // R_DrawSurf(s, false, gl_vertex.value);
663 // LordHavoc: decide the render type only once, because the surface properties were determined by texture anyway
665 if (s->flags & SURF_DRAWSKY)
668 if (!hlbsp) // LordHavoc: HalfLife maps have freaky skypolys...
669 for (;s;s = s->texturechain)
673 t = R_TextureAnimation (cl.worldmodel->textures[n]);
674 // subdivided water surface warp
675 if (s->flags & SURF_DRAWTURB)
677 int alpha = s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f;
678 for (;s;s = s->texturechain)
679 R_WaterSurf(s, t, false, alpha);
683 for (;s;s = s->texturechain)
684 R_WallSurfVertex(s, t, false, false);
686 for (;s;s = s->texturechain)
687 R_WallSurf(s, t, false);
691 void R_NoVisMarkLights (vec3_t lightorigin, dlight_t *light, int bit, int bitindex, model_t *model);
698 void R_DrawBrushModel (entity_t *e)
704 qboolean rotated, vertexlit = false;
712 if (e->angles[0] || e->angles[1] || e->angles[2])
715 for (i=0 ; i<3 ; i++)
717 mins[i] = e->origin[i] - clmodel->radius;
718 maxs[i] = e->origin[i] + clmodel->radius;
724 VectorAdd (e->origin, clmodel->mins, mins);
725 VectorAdd (e->origin, clmodel->maxs, maxs);
728 if (R_CullBox (mins, maxs))
731 VectorSubtract (r_refdef.vieworg, e->origin, modelorg);
735 vec3_t forward, right, up;
737 VectorCopy (modelorg, temp);
738 AngleVectors (e->angles, forward, right, up);
739 modelorg[0] = DotProduct (temp, forward);
740 modelorg[1] = -DotProduct (temp, right);
741 modelorg[2] = DotProduct (temp, up);
744 s = &clmodel->surfaces[clmodel->firstmodelsurface];
746 // calculate dynamic lighting for bmodel if it's not an
748 for (i = 0;i < MAX_DLIGHTS;i++)
750 if ((cl_dlights[i].die < cl.time) || (!cl_dlights[i].radius))
753 VectorSubtract(cl_dlights[i].origin, currententity->origin, org);
754 R_NoVisMarkLights (org, &cl_dlights[i], 1<<(i&31), i >> 5, clmodel);
756 vertexlit = modelalpha != 1 || clmodel->firstmodelsurface == 0 || (currententity->effects & EF_FULLBRIGHT) || currententity->colormod[0] != 1 || currententity->colormod[2] != 1 || currententity->colormod[2] != 1;
758 e->angles[0] = -e->angles[0]; // stupid quake bug
759 softwaretransformforentity (e);
760 e->angles[0] = -e->angles[0]; // stupid quake bug
763 for (i = 0;i < clmodel->nummodelsurfaces;i++, s++)
765 if (((s->flags & SURF_PLANEBACK) == 0) == (PlaneDiff(modelorg, s->plane) >= 0))
767 // R_DrawSurf(s, true, vertexlit || s->texinfo->texture->transparent);
768 if (s->flags & SURF_DRAWSKY)
773 t = R_TextureAnimation (s->texinfo->texture);
774 if (s->flags & SURF_DRAWTURB)
776 R_WaterSurf(s, t, true, s->flags & SURF_DRAWNOALPHA ? 255 : r_wateralpha.value*255.0f);
779 if (vertexlit || s->texinfo->texture->transparent)
780 R_WallSurfVertex(s, t, true, true);
782 R_WallSurf(s, t, true);
789 =============================================================
793 =============================================================
796 void R_StoreEfrags (efrag_t **ppefrag);
814 if (!(node = cl.worldmodel->nodes))
819 // if a leaf node, draw stuff
820 if (node->contents < 0)
822 if (node->contents != CONTENTS_SOLID)
825 pleaf = (mleaf_t *)node;
828 if ((c = pleaf->nummarksurfaces))
831 mark = pleaf->firstmarksurface;
834 (*mark)->visframe = r_framecount;
839 // deal with model fragments in this leaf
841 R_StoreEfrags (&pleaf->efrags);
846 node = nodestack[--s].node;
847 dot = nodestack[s].dot;
853 // node is just a decision point, so go down the apropriate sides
855 // find which side of the node we are on
856 dot = (node->plane->type < 3 ? modelorg[node->plane->type] : DotProduct (modelorg, node->plane->normal)) - node->plane->dist;
858 // recurse down the children, front side first
860 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
862 nodestack[s].node = node;
863 nodestack[s++].dot = dot;
864 node = node->children[side];
872 if ((c = node->numsurfaces))
875 surf = cl.worldmodel->surfaces + node->firstsurface;
881 if (surf->visframe == r_framecount && !(surf->flags & SURF_PLANEBACK))
883 if (gl_texsort.value)
885 surf->texturechain = surf->texinfo->texture->texturechain;
886 surf->texinfo->texture->texturechain = surf;
889 R_DrawSurf(surf, false, gl_vertex.value);
897 if (surf->visframe == r_framecount && (surf->flags & SURF_PLANEBACK))
899 if (gl_texsort.value)
901 surf->texturechain = surf->texinfo->texture->texturechain;
902 surf->texinfo->texture->texturechain = surf;
905 R_DrawSurf(surf, false, gl_vertex.value);
911 // recurse down the back side
912 if (node->children[side]->visframe == r_visframecount && R_NotCulledBox(node->children[side]->minmaxs, node->children[side]->minmaxs+3))
914 node = node->children[side];
920 node = nodestack[--s].node;
921 dot = nodestack[s].dot;
932 void R_DrawWorld (void)
936 memset (&ent, 0, sizeof(ent));
937 ent.model = cl.worldmodel;
938 ent.colormod[0] = ent.colormod[1] = ent.colormod[2] = 1;
939 modelalpha = ent.alpha = 1;
942 VectorCopy (r_refdef.vieworg, modelorg);
944 currententity = &ent;
946 softwaretransformidentity(); // LordHavoc: clear transform
951 glClear (GL_DEPTH_BUFFER_BIT);
953 R_PushDlights (); // now mark the lit surfaces
955 DrawTextureChains ();
957 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
958 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
967 void R_MarkLeaves (void)
973 if (r_oldviewleaf == r_viewleaf && !r_novis.value)
977 r_oldviewleaf = r_viewleaf;
981 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
983 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
986 if (node->visframe == r_visframecount)
988 node->visframe = r_visframecount;
995 vis = Mod_LeafPVS (r_viewleaf, cl.worldmodel);
997 for (i=0 ; i<cl.worldmodel->numleafs ; i++)
999 if (vis[i>>3] & (1<<(i&7)))
1001 node = (mnode_t *)&cl.worldmodel->leafs[i+1];
1004 if (node->visframe == r_visframecount)
1006 node->visframe = r_visframecount;
1007 node = node->parent;
1017 =============================================================================
1021 =============================================================================
1024 // returns a texture number and the position inside it
1025 int AllocBlock (int w, int h, int *x, int *y)
1031 for (texnum=0 ; texnum<MAX_LIGHTMAPS ; texnum++)
1033 best = BLOCK_HEIGHT;
1035 for (i=0 ; i<BLOCK_WIDTH-w ; i+=lightmapalign) // LordHavoc: NVIDIA has broken subimage, so align the lightmaps
1039 for (j=0 ; j<w ; j++)
1041 if (allocated[texnum][i+j] >= best)
1043 if (allocated[texnum][i+j] > best2)
1044 best2 = allocated[texnum][i+j];
1047 { // this is a valid spot
1053 if (best + h > BLOCK_HEIGHT)
1056 if (nosubimagefragments || nosubimage)
1058 if (!lightmaps[texnum])
1059 lightmaps[texnum] = calloc(BLOCK_WIDTH*BLOCK_HEIGHT*4, 1);
1061 // LordHavoc: clear texture to blank image, fragments are uploaded using subimage
1062 else if (!allocated[texnum][0])
1064 byte blank[BLOCK_WIDTH*BLOCK_HEIGHT*3];
1065 memset(blank, 0, sizeof(blank));
1066 glBindTexture(GL_TEXTURE_2D, lightmap_textures + texnum);
1067 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1068 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1070 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, blank);
1072 glTexImage2D (GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, blank);
1075 for (i=0 ; i<w ; i++)
1076 allocated[texnum][*x + i] = best + h;
1081 Sys_Error ("AllocBlock: full");
1086 mvertex_t *r_pcurrentvertbase;
1087 model_t *currentmodel;
1093 BuildSurfaceDisplayList
1096 void BuildSurfaceDisplayList (msurface_t *fa)
1098 int i, j, lindex, lnumverts;
1099 medge_t *pedges, *r_pedge;
1105 // reconstruct the polygon
1106 pedges = currentmodel->edges;
1107 lnumverts = fa->numedges;
1113 poly = Hunk_Alloc (sizeof(glpoly_t) + (lnumverts-4) * VERTEXSIZE*sizeof(float));
1114 poly->next = fa->polys;
1115 poly->flags = fa->flags;
1117 poly->numverts = lnumverts;
1119 for (i=0 ; i<lnumverts ; i++)
1121 lindex = currentmodel->surfedges[fa->firstedge + i];
1125 r_pedge = &pedges[lindex];
1126 vec = r_pcurrentvertbase[r_pedge->v[0]].position;
1130 r_pedge = &pedges[-lindex];
1131 vec = r_pcurrentvertbase[r_pedge->v[1]].position;
1133 s = DotProduct (vec, fa->texinfo->vecs[0]) + fa->texinfo->vecs[0][3];
1134 t = DotProduct (vec, fa->texinfo->vecs[1]) + fa->texinfo->vecs[1][3];
1136 VectorCopy (vec, poly->verts[i]);
1137 poly->verts[i][3] = s / fa->texinfo->texture->width;
1138 poly->verts[i][4] = t / fa->texinfo->texture->height;
1141 // lightmap texture coordinates
1143 s -= fa->texturemins[0];
1144 t -= fa->texturemins[1];
1147 // LordHavoc: calc lightmap data offset
1148 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;
1149 poly->verts[i][7] = j;
1150 s += fa->light_s*16;
1151 s /= BLOCK_WIDTH*16; //fa->texinfo->texture->width;
1153 t += fa->light_t*16;
1154 t /= BLOCK_HEIGHT*16; //fa->texinfo->texture->height;
1156 poly->verts[i][5] = s;
1157 poly->verts[i][6] = t;
1161 // remove co-linear points - Ed
1164 if (!gl_keeptjunctions.value)
1166 for (i = 0 ; i < lnumverts ; ++i)
1169 float *prev, *this, *next;
1171 prev = poly->verts[(i + lnumverts - 1) % lnumverts];
1172 this = poly->verts[i];
1173 next = poly->verts[(i + 1) % lnumverts];
1175 VectorSubtract( this, prev, v1 );
1176 VectorNormalize( v1 );
1177 VectorSubtract( next, prev, v2 );
1178 VectorNormalize( v2 );
1180 // skip co-linear points
1181 #define COLINEAR_EPSILON 0.001
1182 if ((fabs( v1[0] - v2[0] ) <= COLINEAR_EPSILON) &&
1183 (fabs( v1[1] - v2[1] ) <= COLINEAR_EPSILON) &&
1184 (fabs( v1[2] - v2[2] ) <= COLINEAR_EPSILON))
1187 for (j = i + 1; j < lnumverts; ++j)
1190 for (k = 0; k < VERTEXSIZE; ++k)
1191 poly->verts[j - 1][k] = poly->verts[j][k];
1195 // retry next vertex next time, which is now current vertex
1201 poly->numverts = lnumverts;
1205 ========================
1206 GL_CreateSurfaceLightmap
1207 ========================
1209 void GL_CreateSurfaceLightmap (msurface_t *surf)
1213 if (surf->flags & (SURF_DRAWSKY|SURF_DRAWTURB))
1216 smax = (surf->extents[0]>>4)+1;
1217 tmax = (surf->extents[1]>>4)+1;
1219 surf->lightmaptexturenum = AllocBlock (smax, tmax, &surf->light_s, &surf->light_t);
1220 if (nosubimage || nosubimagefragments)
1222 glBindTexture(GL_TEXTURE_2D, lightmap_textures + surf->lightmaptexturenum);
1223 smax = ((surf->extents[0]>>4)+lightmapalign) & lightmapalignmask;
1226 R_BuildLightMap (surf, templight, smax * 4);
1227 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGBA, GL_UNSIGNED_BYTE, templight);
1231 R_BuildLightMap (surf, templight, smax * 3);
1232 glTexSubImage2D(GL_TEXTURE_2D, 0, surf->light_s, surf->light_t, smax, tmax, GL_RGB , GL_UNSIGNED_BYTE, templight);
1241 Builds the lightmap texture
1242 with all the surfaces from all brush models
1245 void GL_BuildLightmaps (void)
1250 memset (allocated, 0, sizeof(allocated));
1252 r_framecount = 1; // no dlightcache
1254 if (gl_nosubimagefragments.value)
1255 nosubimagefragments = 1;
1257 nosubimagefragments = 0;
1259 if (gl_nosubimage.value)
1264 if (gl_lightmaprgba.value)
1266 lightmaprgba = true;
1271 lightmaprgba = false;
1275 // LordHavoc: NVIDIA seems to have a broken glTexSubImage2D,
1276 // it needs to be aligned on 4 pixel boundaries...
1277 // so I implemented an adjustable lightmap alignment
1278 if (gl_lightmapalign.value < 1)
1279 gl_lightmapalign.value = 1;
1280 if (gl_lightmapalign.value > 16)
1281 gl_lightmapalign.value = 16;
1283 while (lightmapalign < gl_lightmapalign.value)
1284 lightmapalign <<= 1;
1285 gl_lightmapalign.value = lightmapalign;
1286 lightmapalignmask = ~(lightmapalign - 1);
1287 if (nosubimagefragments || nosubimage)
1290 lightmapalignmask = ~0;
1293 if (!lightmap_textures)
1295 lightmap_textures = texture_extension_number;
1296 texture_extension_number += MAX_LIGHTMAPS;
1299 for (j=1 ; j<MAX_MODELS ; j++)
1301 m = cl.model_precache[j];
1304 if (m->name[0] == '*')
1306 r_pcurrentvertbase = m->vertexes;
1308 for (i=0 ; i<m->numsurfaces ; i++)
1310 if ( m->surfaces[i].flags & SURF_DRAWTURB )
1312 if ( m->surfaces[i].flags & SURF_DRAWSKY )
1314 GL_CreateSurfaceLightmap (m->surfaces + i);
1315 BuildSurfaceDisplayList (m->surfaces + i);
1319 if (nosubimage || nosubimagefragments)
1322 qglSelectTexture(gl_mtex_enum+1);
1323 for (i = 0;i < MAX_LIGHTMAPS;i++)
1325 if (!allocated[i][0])
1327 lightmapupdate[i][0] = BLOCK_HEIGHT;
1328 lightmapupdate[i][1] = 0;
1329 glBindTexture(GL_TEXTURE_2D, lightmap_textures + i);
1330 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1331 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1333 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, lightmaps[i]);
1335 glTexImage2D(GL_TEXTURE_2D, 0, 3, BLOCK_WIDTH, BLOCK_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, lightmaps[i]);
1338 qglSelectTexture(gl_mtex_enum+0);