HTML clipboardFog Effect is the built-in effect that can be used with BasicEffect.Implementing
this interface I will show you how to do it.
You can implement it writing IEffectFog near Game.And this implementation will
add some functions.
#region
IEffectFog Members
public
Vector3 FogColor
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public bool
FogEnabled
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public float
FogEnd
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public float
FogStart
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
#endregion
We have used Fog Effect in our previously article. Let's go over from it!
Here's our full code:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
Microsoft.Xna.Framework;
using
Microsoft.Xna.Framework.Audio;
using
Microsoft.Xna.Framework.Content;
using
Microsoft.Xna.Framework.GamerServices;
using
Microsoft.Xna.Framework.Graphics;
using
Microsoft.Xna.Framework.Input;
using
Microsoft.Xna.Framework.Media;
using
System.IO;
using
System.Diagnostics;
namespace
WindowsGame9
{
public
class Game1 :
Microsoft.Xna.Framework.Game,IEffectFog
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
float aspectRatio;
Vector3 modelPosition2 =
new Vector3(200.0f,
0.0f, 100.0f);
Vector3 cameraPosition2 =
new Vector3(0.0f,
0.0f, 100.0f);
public Game1()
{
graphics = new
GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override
void Initialize()
{
base.Initialize();
}
protected override
void LoadContent()
{
spriteBatch = new
SpriteBatch(GraphicsDevice);
aspectRatio = graphics.GraphicsDevice.Viewport.Width /
graphics.GraphicsDevice.Viewport.Height;
}
protected
override void UnloadContent()
{
}
protected override
void Update(GameTime
gameTime)
{
base.Update(gameTime);
KeyboardState newstate =
Keyboard.GetState();
if (newstate.IsKeyDown(Keys.W))
{
modelPosition2 += new
Vector3(0.0f, 0.0f, 1.0f);
}
else if
(newstate.IsKeyDown(Keys.S))
{
modelPosition2 -= new
Vector3(0.0f, 0.0f, 1.0f);
}
else if
(newstate.IsKeyDown(Keys.A))
{
modelPosition2 -= new
Vector3(1.0f, 0.0f, 0.0f);
}
else if
(newstate.IsKeyDown(Keys.D))
{
modelPosition2 += new
Vector3(1.0f, 0.0f, 0.0f);
}
else if
(newstate.IsKeyDown(Keys.Escape))
{
this.Exit();
}
}
protected
override void Draw(GameTime
gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
load_model(Content.Load<Model>("modeller\\araba1"),
modelPosition2 += new
Vector3(0.0f, 0.0f, 0.0f), aspectRatio);
base.Draw(gameTime);
}
public void
load_model(Model model_path,
Vector3 camera_position,
float aspect)
{
Matrix[] transforms =
new Matrix[model_path.Bones.Count];
model_path.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh
mesh in model_path.Meshes)
{
foreach (BasicEffect
effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.FogEnabled = true;
effect.FogStart = 200.0f;
effect.FogEnd = 250.0f;
effect.FogColor = new
Vector3(255.0f, 255.0f, 255.0f);
effect.View = Matrix.CreateLookAt(camera_position,
Vector3.Zero,
Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
aspect, 1.0f, 1000.0f);
}
mesh.Draw();
}
}
#region
IEffectFog Members
public Vector3
FogColor
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public bool
FogEnabled
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public float
FogEnd
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
public float
FogStart
{
get
{
throw
new NotImplementedException();
}
set
{
throw
new NotImplementedException();
}
}
#endregion
}
}
When we run it (I added a car with some textures on it) we will see a car with
fog effect.
Let's make some changes in our Interface!
Add this variable in your code:
Vector3
fogclr = new Vector3(0.0f,
0.0f, 0.0f);
This vector3 defines a color(Black) which we will be using to change the color
of Fog.
Then Update your "FogColor" Property as seen below:
public
Vector3 FogColor
{
get
{
return fogclr;
}
set
{
fogclr = value;
}
}
Run it and see what happens!
Fog Color is Black!
Allright here is another sample;
Set FogEnabled,FogStart,FogEnd:
Create 3 variables...
float
fogend = 250.0f;
float
fogstart = 200.0f;
bool
fogenabled = true;
Update your loadmodel function:
public
bool FogEnabled
{
get
{
return fogenabled;
}
set
{
fogenabled = value;
}
}
public float
FogEnd
{
get
{
return fogend;
}
set
{
fogend = value;
}
}
public float
FogStart
{
get
{
return fogstart;
}
set
{
fogstart = value;
}
}
See what happens:
Same thing as you make it without using interfaces?
Using interfaces you can seperate some codes that will not mix Game object.With
that way you will only write codes on your interface and its functions.