Skip to content

Commit efc492d

Browse files
committed
NUKE fog image but try to emulate it exactly in GLSL
I want to NUKE the fog image and replace it with a simple calculation in GLSL. But first, to make sure I'm not making a mistake somewhere, I wanted to try to replicate the old behavior as exactly as possible. Turns out I had to emulate texture filtering to get it reasonably close; just using FogFactor with the actual s and t values resulted in substantial differences in brightness. So this commit NUKES the fog image but replicates it with overly complicated GLSL. Fixes the artifacts where the fog plane intersects a model when viewed from inside the fog.
1 parent 8c24825 commit efc492d

File tree

12 files changed

+84
-168
lines changed

12 files changed

+84
-168
lines changed

src.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ set(GLSLSOURCELIST
152152
# Common shader libraries
153153
${ENGINE_DIR}/renderer/glsl_source/common.glsl
154154
${ENGINE_DIR}/renderer/glsl_source/common_cp.glsl
155+
${ENGINE_DIR}/renderer/glsl_source/fogEquation_fp.glsl
155156
${ENGINE_DIR}/renderer/glsl_source/shaderProfiler_vp.glsl
156157
${ENGINE_DIR}/renderer/glsl_source/shaderProfiler_fp.glsl
157158

src/engine/renderer/Material.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,6 @@ void BindShaderFog( Material* material ) {
11151115
gl_fogQuake3ShaderMaterial->SetUniform_ModelViewProjectionMatrix( glState.modelViewProjectionMatrix[glState.stackIndex] );
11161116

11171117
gl_fogQuake3ShaderMaterial->SetUniform_Time( backEnd.refdef.floatTime - backEnd.currentEntity->e.shaderTime );
1118-
1119-
// bind u_ColorMap
1120-
gl_fogQuake3ShaderMaterial->SetUniform_FogMapBindless(
1121-
GL_BindToTMU( 0, tr.fogImage )
1122-
);
11231118
}
11241119

11251120
void ProcessMaterialNONE( Material*, shaderStage_t*, MaterialSurface* ) {

src/engine/renderer/gl_shader.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,7 +2580,6 @@ GLShader_skyboxMaterial::GLShader_skyboxMaterial() :
25802580
GLShader_fogQuake3::GLShader_fogQuake3() :
25812581
GLShader( "fogQuake3", ATTR_POSITION | ATTR_QTANGENT,
25822582
false, "fogQuake3", "fogQuake3" ),
2583-
u_FogMap( this ),
25842583
u_ModelMatrix( this ),
25852584
u_ModelViewProjectionMatrix( this ),
25862585
u_ColorGlobal_Float( this ),
@@ -2597,15 +2596,9 @@ GLShader_fogQuake3::GLShader_fogQuake3() :
25972596
{
25982597
}
25992598

2600-
void GLShader_fogQuake3::SetShaderProgramUniforms( ShaderProgramDescriptor *shaderProgram )
2601-
{
2602-
glUniform1i( glGetUniformLocation( shaderProgram->id, "u_FogMap" ), 0 );
2603-
}
2604-
26052599
GLShader_fogQuake3Material::GLShader_fogQuake3Material() :
26062600
GLShader( "fogQuake3Material", ATTR_POSITION | ATTR_QTANGENT,
26072601
true, "fogQuake3", "fogQuake3" ),
2608-
u_FogMap( this ),
26092602
u_ModelMatrix( this ),
26102603
u_ModelViewProjectionMatrix( this ),
26112604
u_ColorGlobal_Uint( this ),
@@ -2619,7 +2612,6 @@ GLShader_fogQuake3Material::GLShader_fogQuake3Material() :
26192612
GLShader_fogGlobal::GLShader_fogGlobal() :
26202613
GLShader( "fogGlobal", ATTR_POSITION,
26212614
false, "screenSpace", "fogGlobal" ),
2622-
u_ColorMap( this ),
26232615
u_DepthMap( this ),
26242616
u_UnprojectMatrix( this ),
26252617
u_Color_Float( this ),

src/engine/renderer/gl_shader.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,18 +1826,6 @@ class u_CloudMap :
18261826
}
18271827
};
18281828

1829-
class u_FogMap :
1830-
GLUniformSampler2D {
1831-
public:
1832-
u_FogMap( GLShader* shader ) :
1833-
GLUniformSampler2D( shader, "u_FogMap" ) {
1834-
}
1835-
1836-
void SetUniform_FogMapBindless( GLuint64 bindlessHandle ) {
1837-
this->SetValueBindless( bindlessHandle );
1838-
}
1839-
};
1840-
18411829
class u_DepthTile1 :
18421830
GLUniformSampler2D {
18431831
public:
@@ -3426,7 +3414,6 @@ class GLShader_skyboxMaterial :
34263414

34273415
class GLShader_fogQuake3 :
34283416
public GLShader,
3429-
public u_FogMap,
34303417
public u_ModelMatrix,
34313418
public u_ModelViewProjectionMatrix,
34323419
public u_ColorGlobal_Float,
@@ -3443,12 +3430,10 @@ class GLShader_fogQuake3 :
34433430
{
34443431
public:
34453432
GLShader_fogQuake3();
3446-
void SetShaderProgramUniforms( ShaderProgramDescriptor *shaderProgram ) override;
34473433
};
34483434

34493435
class GLShader_fogQuake3Material :
34503436
public GLShader,
3451-
public u_FogMap,
34523437
public u_ModelMatrix,
34533438
public u_ModelViewProjectionMatrix,
34543439
public u_ColorGlobal_Uint,
@@ -3463,7 +3448,6 @@ class GLShader_fogQuake3Material :
34633448

34643449
class GLShader_fogGlobal :
34653450
public GLShader,
3466-
public u_ColorMap,
34673451
public u_DepthMap,
34683452
public u_UnprojectMatrix,
34693453
public u_Color_Float,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
===========================================================================
3+
Copyright (C) 1999-2005 Id Software, Inc.
4+
Copyright (C) 2006-2011 Robert Beckebans <trebor_7@users.sourceforge.net>
5+
6+
This file is part of Daemon source code.
7+
8+
Daemon source code is free software; you can redistribute it
9+
and/or modify it under the terms of the GNU General Public License as
10+
published by the Free Software Foundation; either version 2 of the License,
11+
or (at your option) any later version.
12+
13+
Daemon source code is distributed in the hope that it will be
14+
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with Daemon source code; if not, write to the Free Software
20+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
===========================================================================
22+
*/
23+
24+
float FogTable(float s)
25+
{
26+
return sqrt(floor(s * 255.0) / 255);
27+
}
28+
29+
float FogFactor(float s, float t)
30+
{
31+
s -= 1.0f / 512.0f;
32+
33+
if ( s < 0 )
34+
{
35+
return 0;
36+
}
37+
38+
if ( t < 1.0f / 32.0f )
39+
{
40+
return 0;
41+
}
42+
43+
if ( t < 31.0f / 32.0f )
44+
{
45+
s *= ( t - 1.0f / 32.0f ) / ( 30.0f / 32.0f );
46+
}
47+
48+
s *= 8;
49+
50+
if ( s > 1.0f )
51+
{
52+
s = 1.0f;
53+
}
54+
55+
return FogTable(s);
56+
}
57+
58+
float GetFogAlpha(float s, float t)
59+
{
60+
float sfloor = floor(s * 256 + 0.5) - 0.5;
61+
float sceil = sfloor + 1;
62+
sfloor = clamp(sfloor, 0.5, 255.5) / 256;
63+
sceil = clamp(sceil, 0.5, 255.5) / 256;
64+
float smix = sfloor < sceil ? (s - sfloor) * 256 : 0.5;
65+
66+
float tfloor = floor(t * 32 + 0.5) - 0.5;
67+
float tceil = tfloor + 1;
68+
tfloor = clamp(tfloor, 0.5, 31.5) / 32;
69+
tceil = clamp(tceil, 0.5, 31.5) / 32;
70+
float tmix = tfloor < tceil ? (t - tfloor) * 32 : 0.5;
71+
72+
float f00 = FogFactor(sfloor, tfloor);
73+
float f01 = FogFactor(sfloor, tceil);
74+
float f10 = FogFactor(sceil, tfloor);
75+
float f11 = FogFactor(sceil, tceil);
76+
return mix(mix(f00, f01, tmix), mix(f10, f11, tmix), smix);
77+
}

src/engine/renderer/glsl_source/fogGlobal_fp.glsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2323
/* fogGlobal_fp.glsl */
2424

2525
#insert common
26+
#insert fogEquation_fp
2627

27-
uniform sampler2D u_ColorMap; // fog texture
2828
uniform sampler2D u_DepthMap;
2929

3030
uniform colorPack u_Color;
@@ -46,10 +46,9 @@ void main()
4646
P.xyz /= P.w;
4747

4848
// calculate the length in fog (t is always 1 if eye is in fog)
49-
st.s = distance(u_ViewOrigin, P.xyz) * u_FogDensity;
50-
st.t = 1.0;
49+
float s = distance(u_ViewOrigin, P.xyz) * u_FogDensity;
5150

52-
vec4 color = texture2D(u_ColorMap, st);
51+
vec4 color = vec4(1, 1, 1, GetFogAlpha(s, 1.0));
5352

5453
outputColor = UnpackColor( u_Color ) * color;
5554
}

src/engine/renderer/glsl_source/fogQuake3_fp.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2222

2323
/* fogQuake3_fp.glsl */
2424

25-
#define FOGQUAKE3_GLSL
25+
#insert fogEquation_fp
2626

27-
uniform sampler2D u_FogMap;
27+
#define FOGQUAKE3_GLSL
2828

2929
uniform float u_FogEyeT;
3030

@@ -52,7 +52,7 @@ void main()
5252

5353
t = 1.0 / 32.0 + ( 30.0 / 32.0 ) * t;
5454

55-
vec4 color = texture2D(u_FogMap, vec2(s, t));
55+
vec4 color = vec4(1, 1, 1, GetFogAlpha(s, t));
5656

5757
color *= var_Color;
5858

src/engine/renderer/tr_backend.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,11 +1410,6 @@ void RB_RenderGlobalFog()
14101410

14111411
gl_fogGlobalShader->SetUniform_UnprojectMatrix( backEnd.viewParms.unprojectionMatrix );
14121412

1413-
// bind u_ColorMap
1414-
gl_fogGlobalShader->SetUniform_ColorMapBindless(
1415-
GL_BindToTMU( 0, tr.fogImage )
1416-
);
1417-
14181413
// bind u_DepthMap
14191414
gl_fogGlobalShader->SetUniform_DepthMapBindless(
14201415
GL_BindToTMU( 1, tr.currentDepthImage )

src/engine/renderer/tr_image.cpp

Lines changed: 0 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,121 +2351,6 @@ image_t *R_FindCubeImage( const char *name, imageParams_t &imageParams )
23512351
return nullptr;
23522352
}
23532353

2354-
/*
2355-
=================
2356-
R_InitFogTable
2357-
=================
2358-
*/
2359-
void R_InitFogTable()
2360-
{
2361-
int i;
2362-
float d;
2363-
float exp;
2364-
2365-
exp = 0.5;
2366-
2367-
for ( i = 0; i < FOG_TABLE_SIZE; i++ )
2368-
{
2369-
d = pow( ( float ) i / ( FOG_TABLE_SIZE - 1 ), exp );
2370-
2371-
tr.fogTable[ i ] = d;
2372-
}
2373-
}
2374-
2375-
/*
2376-
================
2377-
R_FogFactor
2378-
2379-
Returns a 0.0 to 1.0 fog density value
2380-
This is called for each texel of the fog texture on startup
2381-
and for each vertex of transparent shaders in fog dynamically
2382-
================
2383-
*/
2384-
float R_FogFactor( float s, float t )
2385-
{
2386-
float d;
2387-
2388-
s -= 1.0f / 512.0f;
2389-
2390-
if ( s < 0 )
2391-
{
2392-
return 0;
2393-
}
2394-
2395-
if ( t < 1.0f / 32.0f )
2396-
{
2397-
return 0;
2398-
}
2399-
2400-
if ( t < 31.0f / 32.0f )
2401-
{
2402-
s *= ( t - 1.0f / 32.0f ) / ( 30.0f / 32.0f );
2403-
}
2404-
2405-
// we need to leave a lot of clamp range
2406-
s *= 8;
2407-
2408-
if ( s > 1.0f )
2409-
{
2410-
s = 1.0f;
2411-
}
2412-
2413-
d = tr.fogTable[( int )( s * ( FOG_TABLE_SIZE - 1 ) ) ];
2414-
2415-
return d;
2416-
}
2417-
2418-
/*
2419-
================
2420-
R_CreateFogImage
2421-
================
2422-
*/
2423-
static void R_CreateFogImage()
2424-
{
2425-
// Fog image is always created because disabling fog is cheat.
2426-
2427-
int x, y;
2428-
byte *data, *ptr;
2429-
float d;
2430-
float borderColor[ 4 ];
2431-
2432-
constexpr int FOG_S = 256;
2433-
constexpr int FOG_T = 32;
2434-
2435-
ptr = data = (byte*) ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * 4 );
2436-
2437-
// S is distance, T is depth
2438-
for ( y = 0; y < FOG_T; y++ )
2439-
{
2440-
for ( x = 0; x < FOG_S; x++ )
2441-
{
2442-
d = R_FogFactor( ( x + 0.5f ) / FOG_S, ( y + 0.5f ) / FOG_T );
2443-
2444-
ptr[ 0 ] = ptr[ 1 ] = ptr[ 2 ] = 255;
2445-
ptr[ 3 ] = 255 * d;
2446-
ptr += 4;
2447-
}
2448-
}
2449-
2450-
// standard openGL clamping doesn't really do what we want -- it includes
2451-
// the border color at the edges. OpenGL 1.2 has clamp-to-edge, which does
2452-
// what we want.
2453-
imageParams_t imageParams = {};
2454-
imageParams.bits = IF_NOPICMIP;
2455-
imageParams.filterType = filterType_t::FT_DEFAULT;
2456-
imageParams.wrapType = wrapTypeEnum_t::WT_CLAMP;
2457-
2458-
tr.fogImage = R_CreateImage( "_fog", ( const byte ** ) &data, FOG_S, FOG_T, 1, imageParams );
2459-
ri.Hunk_FreeTempMemory( data );
2460-
2461-
borderColor[ 0 ] = 1.0;
2462-
borderColor[ 1 ] = 1.0;
2463-
borderColor[ 2 ] = 1.0;
2464-
borderColor[ 3 ] = 1;
2465-
2466-
glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
2467-
}
2468-
24692354
/*
24702355
==================
24712356
R_CreateDefaultImage
@@ -2811,7 +2696,6 @@ void R_CreateBuiltinImages()
28112696
image = R_CreateImage( "_cinematic", ( const byte ** ) &dataPtr, 1, 1, 1, imageParams );
28122697
}
28132698

2814-
R_CreateFogImage();
28152699
R_CreateContrastRenderFBOImage();
28162700
R_CreateBloomRenderFBOImages();
28172701
R_CreateCurrentRenderImage();

src/engine/renderer/tr_init.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,6 @@ ScreenshotCmd screenshotPNGRegistration("screenshotPNG", ssFormat_t::SSF_PNG, "p
13241324
}
13251325
}
13261326

1327-
R_InitFogTable();
1328-
13291327
R_NoiseInit();
13301328

13311329
R_Register();

0 commit comments

Comments
 (0)