Fog calculation should use camera position instead of entity's head position, fixes #191. (Thanks goodlyay)

This commit is contained in:
UnknownShadow200 2016-05-08 13:31:31 +10:00
parent 3aca306645
commit b352f1c7c9
4 changed files with 27 additions and 25 deletions

View File

@ -131,8 +131,6 @@ namespace ClassicalSharp.GraphicsAPI {
}
public override void SetFogStart( float value ) {
if( value == fogStart ) return;
fogStart = value;
device.SetRenderState( RenderState.FogStart, value );
}

View File

@ -81,13 +81,13 @@ namespace ClassicalSharp.GraphicsAPI {
}
}
float lastFogStart = -1, lastFogEnd = -1, lastFogDensity = -1;
float lastFogEnd = -1, lastFogDensity = -1;
public override void SetFogDensity( float value ) {
FogParam( FogParameter.FogDensity, value, ref lastFogDensity );
}
public override void SetFogStart( float value ) {
FogParam( FogParameter.FogStart, value, ref lastFogStart );
GL.Fogf( FogParameter.FogStart, value );
}
public override void SetFogEnd( float value ) {
@ -95,10 +95,9 @@ namespace ClassicalSharp.GraphicsAPI {
}
static void FogParam( FogParameter param, float value, ref float last ) {
if( value != last ) {
GL.Fogf( param, value );
last = value;
}
if( value == last ) return;
GL.Fogf( param, value );
last = value;
}
Fog lastFogMode = (Fog)999;

View File

@ -56,6 +56,13 @@ namespace ClassicalSharp {
other.Max.X <= Max.X && other.Max.Y <= Max.Y && other.Max.Z <= Max.Z;
}
/// <summary> Determines whether this bounding box entirely contains
/// the coordinates on all axes. </summary>
public bool Contains( Vector3 P ) {
return P.X >= Min.X && P.Y >= Min.Y && P.Z >= Min.Z &&
P.X <= Max.X && P.Y <= Max.Y && P.Z <= Max.Z;
}
/// <summary> Determines whether this bounding box intersects
/// the given bounding box on the X axis. </summary>
public bool XIntersects( BoundingBox box ) {

View File

@ -37,14 +37,14 @@ namespace ClassicalSharp.Renderers {
}
RenderClouds( deltaTime );
ResetFog();
UpdateFog();
}
protected override void EnvVariableChanged( object sender, EnvVarEventArgs e ) {
if( e.Var == EnvVar.SkyColour ) {
ResetSky();
} else if( e.Var == EnvVar.FogColour ) {
ResetFog();
UpdateFog();
} else if( e.Var == EnvVar.CloudsColour ) {
ResetClouds();
} else if( e.Var == EnvVar.CloudsLevel ) {
@ -55,6 +55,7 @@ namespace ClassicalSharp.Renderers {
public override void Init( Game game ) {
base.Init( game );
graphics.SetFogStart( 0 );
graphics.Fog = true;
ResetAllEnv( null, null );
game.Events.ViewDistanceChanged += ResetAllEnv;
@ -73,7 +74,7 @@ namespace ClassicalSharp.Renderers {
}
void ResetAllEnv( object sender, EventArgs e ) {
ResetFog();
UpdateFog();
ResetSky();
ResetClouds();
}
@ -116,30 +117,27 @@ namespace ClassicalSharp.Renderers {
return blend;
}
void ResetFog() {
void UpdateFog() {
if( map.IsNotLoaded ) return;
FastColour adjFogCol = FastColour.White;
BlockInfo info = game.BlockInfo;
BoundingBox pBounds = game.LocalPlayer.CollisionBounds;
Vector3I headCoords = Vector3I.Floor( game.LocalPlayer.EyePosition );
Vector3 pos = (Vector3)headCoords;
byte headBlock = game.World.SafeGetBlock( headCoords );
BoundingBox blockBB = new BoundingBox( pos + game.BlockInfo.MinBB[headBlock],
pos + game.BlockInfo.MaxBB[headBlock] );
BoundingBox localBB = game.LocalPlayer.CollisionBounds;
bool intersecting = blockBB.Intersects( localBB );
Vector3 pos = game.CurrentCameraPos;
Vector3I coords = Vector3I.Floor( pos );
byte block = game.World.SafeGetBlock( coords );
BoundingBox blockBB = new BoundingBox(
(Vector3)coords + info.MinBB[block],
(Vector3)coords + info.MaxBB[block] );
if( intersecting && info.FogDensity[headBlock] != 0 ) {
if( blockBB.Contains( pos ) && info.FogDensity[block] != 0 ) {
graphics.SetFogMode( Fog.Exp );
graphics.SetFogDensity( info.FogDensity[headBlock] );
adjFogCol = info.FogColour[headBlock];
graphics.SetFogDensity( info.FogDensity[block] );
adjFogCol = info.FogColour[block];
} else {
// Blend fog and sky together
float blend = (float)BlendFactor( game.ViewDistance );
adjFogCol = FastColour.Lerp( map.FogCol, map.SkyCol, blend );
graphics.SetFogMode( Fog.Linear );
graphics.SetFogStart( 0 );
graphics.SetFogEnd( game.ViewDistance );
}
graphics.ClearColour( adjFogCol );
@ -173,7 +171,7 @@ namespace ClassicalSharp.Renderers {
extent = Utils.AdjViewDist( extent );
int x1 = -extent, x2 = map.Width + extent;
int z1 = -extent, z2 = map.Length + extent;
skyVertices = Utils.CountVertices( x2 - x1, z2 - z1, axisSize );
skyVertices = Utils.CountVertices( x2 - x1, z2 - z1, axisSize );
VertexP3fC4b[] vertices = new VertexP3fC4b[skyVertices];
int height = Math.Max( map.Height + 2 + 6, map.CloudHeight + 6);