Google
 

Saturday, May 19, 2007

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.

2 comments:

  1. Glad to see another UK dev out there, look forward to seeing even more good work from you!!!

    ReplyDelete
  2. :) Thanks for the support, just had a look at your link and I see you have a fix for Blender exporting to X files, Blender can be a real pain in the Butt, this should help.

    ReplyDelete

Note: Only a member of this blog may post a comment.