//
refdef_t r_refdef;
-// 8.8 fraction of base light value
-unsigned short d_lightstylevalue[256];
-
cvar_t r_showtris = {0, "r_showtris", "0"};
cvar_t r_drawentities = {0, "r_drawentities","1"};
cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1"};
rtexture_t *r_texture_notexture;
rtexture_t *r_texture_whitecube;
rtexture_t *r_texture_normalizationcube;
+rtexture_t *r_texture_fogattenuation;
+rtexture_t *r_texture_fogintensity;
void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
{
vec3_t fogcolor;
vec_t fogdensity;
+vec_t fogrange;
+vec_t fograngerecip;
+int fogtableindex;
+vec_t fogtabledistmultiplier;
+float fogtable[FOGTABLEWIDTH];
float fog_density, fog_red, fog_green, fog_blue;
qboolean fogenabled;
qboolean oldgl_fogenable;
{
fogenabled = true;
fogdensity = -4000.0f / (fog_density * fog_density);
+ // this is the point where the fog reaches 0.9986 alpha, which we
+ // consider a good enough cutoff point for the texture
+ // (0.9986 * 256 == 255.6)
+ fogrange = 400 / fog_density;
+ fograngerecip = 1.0f / fogrange;
+ fogtabledistmultiplier = FOGTABLEWIDTH * fograngerecip;
// fog color was already set
}
else
// FIXME: move this to client?
void FOG_registercvars(void)
{
+ int x;
+ double r, alpha;
+
if (gamemode == GAME_NEHAHRA)
{
Cvar_RegisterVariable (&gl_fogenable);
Cvar_RegisterVariable (&gl_fogstart);
Cvar_RegisterVariable (&gl_fogend);
}
+
+ r = (-1.0/256.0) * (FOGTABLEWIDTH * FOGTABLEWIDTH);
+ for (x = 0;x < FOGTABLEWIDTH;x++)
+ {
+ alpha = exp(r / ((double)x*(double)x));
+ if (x == FOGTABLEWIDTH - 1)
+ alpha = 1;
+ fogtable[x] = bound(0, alpha, 1);
+ }
}
static void R_BuildBlankTextures(void)
{
- qbyte data[4];
+ unsigned char data[4];
data[0] = 128; // normal X
data[1] = 128; // normal Y
data[2] = 255; // normal Z
static void R_BuildNoTexture(void)
{
int x, y;
- qbyte pix[16][16][4];
+ unsigned char pix[16][16][4];
// this makes a light grey/dark grey checkerboard texture
for (y = 0;y < 16;y++)
{
static void R_BuildWhiteCube(void)
{
- qbyte data[6*1*1*4];
+ unsigned char data[6*1*1*4];
data[ 0] = 255;data[ 1] = 255;data[ 2] = 255;data[ 3] = 255;
data[ 4] = 255;data[ 5] = 255;data[ 6] = 255;data[ 7] = 255;
data[ 8] = 255;data[ 9] = 255;data[10] = 255;data[11] = 255;
vec3_t v;
vec_t s, t, intensity;
#define NORMSIZE 64
- qbyte data[6][NORMSIZE][NORMSIZE][4];
+ unsigned char data[6][NORMSIZE][NORMSIZE][4];
for (side = 0;side < 6;side++)
{
for (y = 0;y < NORMSIZE;y++)
r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
}
+static void R_BuildFogTexture(void)
+{
+ int x, b;
+ double r, alpha;
+#define FOGWIDTH 64
+ unsigned char data1[FOGWIDTH][4];
+ unsigned char data2[FOGWIDTH][4];
+ r = (-1.0/256.0) * (FOGWIDTH * FOGWIDTH);
+ for (x = 0;x < FOGWIDTH;x++)
+ {
+ alpha = exp(r / ((double)x*(double)x));
+ if (x == FOGWIDTH - 1)
+ alpha = 1;
+ b = (int)(256.0 * alpha);
+ b = bound(0, b, 255);
+ data1[x][0] = 255 - b;
+ data1[x][1] = 255 - b;
+ data1[x][2] = 255 - b;
+ data1[x][3] = 255;
+ data2[x][0] = b;
+ data2[x][1] = b;
+ data2[x][2] = b;
+ data2[x][3] = 255;
+ }
+ r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+ r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+}
+
void gl_main_start(void)
{
r_main_texturepool = R_AllocTexturePool();
R_BuildWhiteCube();
R_BuildNormalizationCube();
}
+ R_BuildFogTexture();
}
void gl_main_shutdown(void)
for (i = 0;i < r_refdef.numentities;i++)
{
ent = r_refdef.entities[i];
- Mod_CheckLoaded(ent->model);
// some of the renderer still relies on origin...
Matrix4x4_OriginFromMatrix(&ent->matrix, ent->origin);
// some of the renderer still relies on scale...
for (i = 0;i < r_refdef.numentities;i++)
{
ent = r_refdef.entities[i];
- Mod_CheckLoaded(ent->model);
// some of the renderer still relies on origin...
Matrix4x4_OriginFromMatrix(&ent->matrix, ent->origin);
// some of the renderer still relies on scale...
R_ClearScreen();
R_Textures_Frame();
R_UpdateFog();
- R_UpdateLights();
R_TimeReport("setup");
qglDepthFunc(GL_LEQUAL);
{
for (i = 0, v = vertex, c = color;i < 8;i++, v += 4, c += 4)
{
- VectorSubtract(v, r_vieworigin, diff);
- f2 = exp(fogdensity/DotProduct(diff, diff));
+ f2 = VERTEXFOGTABLE(VectorDistance(v, r_vieworigin));
f1 = 1 - f2;
c[0] = c[0] * f1 + fogcolor[0] * f2;
c[1] = c[1] * f1 + fogcolor[1] * f2;
{
const entity_render_t *ent = (entity_render_t *)calldata1;
int i;
- float f1, f2, *c, diff[3];
+ float f1, f2, *c;
float color4f[6*4];
rmeshstate_t m;
R_Mesh_Matrix(&ent->matrix);
{
memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
m.pointer_color = color4f;
- VectorSubtract(ent->origin, r_vieworigin, diff);
- f2 = exp(fogdensity/DotProduct(diff, diff));
+ f2 = VERTEXFOGTABLE(VectorDistance(ent->origin, r_vieworigin));
f1 = 1 - f2;
for (i = 0, c = color4f;i < 6;i++, c += 4)
{
float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
-void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, int depthdisable, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
+void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, int depthdisable, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
{
- float diff[3];
+ float fog = 0.0f, ifog;
rmeshstate_t m;
if (fogenabled)
- {
- VectorSubtract(origin, r_vieworigin, diff);
- ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
- }
+ fog = VERTEXFOGTABLE(VectorDistance(origin, r_vieworigin));
+ ifog = 1 - fog;
R_Mesh_Matrix(&r_identitymatrix);
GL_BlendFunc(blendfunc1, blendfunc2);
m.pointer_texcoord[0] = spritetexcoord2f;
m.pointer_vertex = varray_vertex3f;
R_Mesh_State(&m);
- GL_Color(cr, cg, cb, ca);
+ GL_Color(cr * ifog, cg * ifog, cb * ifog, ca);
R_Mesh_Draw(0, 4, 2, polygonelements);
+
+ if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
+ {
+ R_Mesh_TexBind(0, R_GetTexture(fogtexture));
+ GL_BlendFunc(blendfunc1, GL_ONE);
+ GL_Color(fogcolor[0] * fog, fogcolor[1] * fog, fogcolor[2] * fog, ca);
+ R_Mesh_Draw(0, 4, 2, polygonelements);
+ }
}
int R_Mesh_AddVertex3f(rmesh_t *mesh, const float *v)
// would normally be baked into the lightmaptexture must be
// applied to the color
if (ent->model->type == mod_brushq3)
- colorscale *= d_lightstylevalue[0] * (1.0f / 256.0f);
+ colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
// transparent and fullbright are not affected by r_lightmapintensity
if (!(t->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
colorscale *= r_lightmapintensity;
int i;
float f;
float *v, *c, *c2;
- vec3_t diff;
if (lightmode >= 2)
{
// model lighting
{
for (i = 0, c = varray_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
{
- const qbyte *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
+ const unsigned char *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
if (lm)
{
- float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
+ float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
VectorScale(lm, scale, c);
if (surface->lightmapinfo->styles[1] != 255)
{
int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
lm += size3;
- scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
VectorMA(c, scale, lm, c);
if (surface->lightmapinfo->styles[2] != 255)
{
lm += size3;
- scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
VectorMA(c, scale, lm, c);
if (surface->lightmapinfo->styles[3] != 255)
{
lm += size3;
- scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
+ scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
VectorMA(c, scale, lm, c);
}
}
{
for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
{
- VectorSubtract(v, modelorg, diff);
- f = 1 - exp(fogdensity/DotProduct(diff, diff));
+ f = 1 - VERTEXFOGTABLE(VectorDistance(v, modelorg));
c2[0] = c[0] * f;
c2[1] = c[1] * f;
c2[2] = c[2] * f;
{
for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
{
- VectorSubtract(v, modelorg, diff);
- f = 1 - exp(fogdensity/DotProduct(diff, diff));
+ f = 1 - VERTEXFOGTABLE(VectorDistance(v, modelorg));
c2[0] = f;
c2[1] = f;
c2[2] = f;
for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
{
int i;
- float diff[3];
float f, *v, *c;
surface = texturesurfacelist[texturesurfaceindex];
RSurf_SetVertexPointer(ent, texture, surface, modelorg);
R_Mesh_ColorPointer(varray_color4f);
for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
{
- VectorSubtract(v, modelorg, diff);
- f = exp(fogdensity/DotProduct(diff, diff));
+ f = VERTEXFOGTABLE(VectorDistance(v, modelorg));
c[0] = layercolor[0];
c[1] = layercolor[1];
c[2] = layercolor[2];
{
for (i = 0;i < model->brushq1.light_styles;i++)
{
- if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
+ if (model->brushq1.light_stylevalue[i] != r_refdef.lightstylevalue[model->brushq1.light_style[i]])
{
- model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
+ model->brushq1.light_stylevalue[i] = r_refdef.lightstylevalue[model->brushq1.light_style[i]];
if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
for (;(surface = *surfacechain);surfacechain++)
surface->cached_dlight = true;