Wednesday, 27 June 2012

Engine Design - Fog


I thought I would post this class first as it is relatively simple to implement and can be put into any XNA engine and is not specific to the HM Engine.
Right, the effect of this class can be seen in the earlier post I gave for fog. So without further a do, here is the class:

public sealed class Fog
{
    public static Color FogColor;
    public static bool FogEnable;
    public static float FogStart;
    public static float FogEnd;
    public static FogMode FogTableMode;
    public static void Draw(GraphicsDevice myDevice, GameTime gameTime)
    {
        if (myDevice.RenderState.FogEnable != FogEnable)
            myDevice.RenderState.FogEnable = FogEnable;
        if (FogEnable)
        {
            if (myDevice.RenderState.FogColor != FogColor)
                myDevice.RenderState.FogColor = FogColor;
            if (myDevice.RenderState.FogStart != FogStart)
                myDevice.RenderState.FogStart = FogStart;
            if (myDevice.RenderState.FogEnd != FogEnd)
                myDevice.RenderState.FogEnd = FogEnd;
            if (myDevice.RenderState.FogTableMode != FogTableMode)
                myDevice.RenderState.FogTableMode = FogTableMode;
        }
    }
}

So all you have to do is populate the Fog class static properties with the required values and in your assemblies Draw method call the static Draw method, like this:

protected override void Draw(GameTime gameTime)
{
    graphics.GraphicsDevice.Clear(Color.Black);
    Fog.FogEnable = true;
    Fog.FogColor = Color.WhiteSmoke;
    Fog.FogStart = 95f;
    Fog.FogEnd = .999f;
    Fog.FogTableMode = FogMode.Linear;
    
    // Your drawing stuff here...
    Fog.Draw(graphics.GraphicsDevice, gameTime);

    // Possibly more code here...
    base.Draw(gameTime);
}

And with that you can have a nice fog effect in your engine.

Posted Mon, Oct 15 2007 1:24 PM by Charles Humphrey | Add post to favorites | Add blog to favorites
Filed under: Randomchaos 3D Engine, Fog, XNA 1.0 [Edit Tags]

No comments:

Post a Comment