Skip to content

Commit 32d40f7

Browse files
committed
Improve fog vertex interpolation
The old var_TexCoords.t (fraction of distance between viewer and vertex inside fog) was a ratio which does not give the correct result when linearly interpolating between vertexes. Use instead the previous numerator of that fraction, which gives a true result when linearly interpolated. Effects: - Reduces the extent of artifacts at the intersection of the fog plane and a model (when viewed from inside the fog). - Models that use larger triangles (more than a few qu) look better. - Incorrectly set up fog volumes which can be entered from more than one plane can now be rendered when viewed from the opposite side of the designated fog plane. For example Pulse, 2609 -4097 -2109 -1 4 - The edges of BSP surfaces near the fog plane are slightly more fogged when viewed from outside the fog. (Not clearly good or bad.)
1 parent f812b98 commit 32d40f7

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/engine/renderer/glsl_source/fogQuake3_fp.glsl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2626

2727
uniform sampler2D u_FogMap;
2828

29+
uniform float u_FogEyeT;
30+
2931
IN(smooth) vec2 var_TexCoords;
3032
IN(smooth) vec4 var_Color;
3133

@@ -35,7 +37,17 @@ void main()
3537
{
3638
#insert material_fp
3739

38-
vec4 color = texture2D(u_FogMap, var_TexCoords);
40+
float t = step( 0, var_TexCoords.t );
41+
42+
if ( u_FogEyeT < 0 ) // eye outside fog
43+
{
44+
// fraction of the viewer-to-vertex ray which is inside fog
45+
t *= var_TexCoords.t / ( max( 0, var_TexCoords.t ) - u_FogEyeT );
46+
}
47+
48+
t = 1.0 / 32.0 + ( 30.0 / 32.0 ) * t;
49+
50+
vec4 color = texture2D(u_FogMap, vec2(var_TexCoords.s, t));
3951

4052
color *= var_Color;
4153

src/engine/renderer/glsl_source/fogQuake3_vp.glsl

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ uniform mat4 u_ModelViewProjectionMatrix;
3636

3737
uniform vec4 u_FogDistanceVector; // view axis, scaled by fog density
3838
uniform vec4 u_FogDepthVector; // fog plane
39-
uniform float u_FogEyeT;
4039

4140
// var_TexCoords.s is distance from viewer to vertex dotted with view axis
42-
// var_TexCoords.t is the fraction of the viewer-to-vertex ray which is inside fog
41+
// var_TexCoords.t is how far the vertex is under the fog plane
4342
OUT(smooth) vec2 var_TexCoords;
4443
OUT(smooth) vec4 var_Color;
4544

@@ -79,29 +78,12 @@ void main()
7978
float s = dot(position.xyz, u_FogDistanceVector.xyz) + u_FogDistanceVector.w;
8079
float t = dot(position.xyz, u_FogDepthVector.xyz) + u_FogDepthVector.w;
8180

82-
// partially clipped fogs use the T axis
83-
if(u_FogEyeT < 0.0)
84-
{
85-
if(t < 1.0)
86-
{
87-
t = 1.0 / 32.0; // point is outside, so no fogging
88-
}
89-
else
90-
{
91-
t = 1.0 / 32.0 + 30.0 / 32.0 * t / (t - u_FogEyeT); // cut the distance at the fog plane
92-
}
93-
}
94-
else
95-
{
96-
if(t < 0.0)
97-
{
98-
t = 1.0 / 32.0; // point is outside, so no fogging
99-
}
100-
else
101-
{
102-
t = 31.0 / 32.0;
103-
}
104-
}
81+
// HACK: increase cutoff to avoid dark lines at the edge of BSP triangles that stop
82+
// right at the fog plane (when viewed from under fog)
83+
// But don't do it for models as this causes a doubly-fogged light band at the fog plane
84+
#if !defined(USE_VERTEX_ANIMATION) && !defined(USE_VERTEX_SKINNING)
85+
t += 1.5;
86+
#endif
10587

10688
var_TexCoords = vec2(s, t);
10789

0 commit comments

Comments
 (0)