Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57

 Error when I try to run.
  Posted by: Anonymous
  When: 15/03/2013 at 19:21:06

 Error With the Effect File
  Posted by: Anonymous
  When: 15/03/2013 at 18:21:01

 Can only get shadowmap
  Posted by: Anonymous
  When: 15/03/2013 at 15:48:52

 Vertex and Pixel Shader Versions?
  Posted by: Anonymous
  When: 15/03/2013 at 15:07:16

 Unsupported properties
  Posted by: Anonymous
  When: 15/03/2013 at 14:23:00

 Problem Loading Skybox
  Posted by: Rana
  When: 15/03/2013 at 10:34:45

 Black Screen Of Death - Help!
  Posted by: Anonymous
  When: 15/03/2013 at 03:43:43

 2.0 anyone?
  Posted by: Anonymous
  When: 15/03/2013 at 02:19:48

 Defitinition of tha rotation axis
  Posted by: Anonymous
  When: 15/03/2013 at 00:55:14




Topic: Need a Distance Fog Crash-Tutorial ;)



  
Goto parent category
  
Create a new user account


   Need a Distance Fog Crash-Tutorial ;)
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 09/12/2008 at 10:45:29
Hi folks.

thanks to Riemer for this great Tut on using MDX.
Now, that i used it for many different things, i wanted to try somwthing other.
So i created a big terrain (617x617) an textured it.
Now i wondered how i can make the part of terrain farer away from the camera slowly disapper in a "wall of fog"?
So the farer the terrain is, the more it should be covered by fog.

Can someone give me a Crashcourse please?
Thank You!

Chu
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 09/12/2008 at 12:58:50
In your pixel shader determine the distance between your camera and the current pixel's position in world coordinates.

Now you can blend the pixel's actual color with the fog color depending on the distance.

It is very easy to make up a formula on your own, like: blendFactor = (distance - maxDist)^2 / maxDist^2, where maxDist is the distance at which nothing should be visible any more.

Also, there should be plenty of information on this topic on the internet (especially basic fog formuals like linear fog, exponential fog and double exponential fog)

Tell me, if you need more detailed informations about the implementation.

Greets.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 11/12/2008 at 23:11:40
So, tahnks to You first =)

But to be honest the formula wasnīt what I searched, more the code example how to enable and use fog on an existing device =)
I know there is an part device.EnableFog or device.FogEnabled
But i thinks it is like the Lights, and i need all the other Instructions before the enabeling...

Greetz
Chu
 Poster : riemer
 Posts: 1392
 Country : Belgium
 City: Antwerp

  
Posted by riemer on 12/12/2008 at 07:52:40
There is a lot of useful information in radulph's post.
To get up and running with fog really fast, you may indeed want to use some pre-defined renderstates. However, for better control (what you want when dealing with fog) I strongly recommend you to code your own HLSL effect.
Series3 was written as a complete introduction to HLSL. Next, radulph's post above contains what you need to implement you fog.
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 12/12/2008 at 17:17:45
Hi chudan,

I didn't know that you weren't going to code your fog in hlsl.

XNA offers some predefinded fog functions you can use. You'll need the following render states:
FogColor : The Color of your fog.
FogTableMode : One of {FogMode.Linear, FogMode.Exponent, FogMode.ExponentSquared}
This attribute cotrols the behavoir of your fog (the blending factor depending on the distance).

If you use FogMode.Linear, you need to specify 2 more render states: FogStart and FogEnd. XNA expects depth values at this point (within range 0 and 1), not distances!

At the end you just need to enable your fog using:
FogEnable = true

Greets.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 13/12/2008 at 02:01:46
So thatīs all I wanted =) Thank you.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 14/12/2008 at 11:53:09
Okay, iI am back with a new question ;)

I created the following terrain from an heightmap and an texture:


And I tried the following code:


device.RenderState.FogColor = Color.Gray;
device.RenderState.FogTableMode = FogMode.Exp;
device.RenderState.FogDensity = 0.5f;
device.RenderState.FogEnable = true;


but all I get is my terrain, completely colored gray...


what have i done wrong?
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 14/12/2008 at 13:22:00
Exponential fog is computed the following way:

1 / (2.718^(distance * densitiy))

let's say:
distance = 20
density = 0.5

=> blendfactor = 0.0005 (guessed). That's the amount of the original color that will be used. The rest is gray.

You should rather try values like 0.025 (equals 20% at 50 distance) or lower.

greets.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 15/12/2008 at 00:23:37
Hm, this explains why I see nothing but gray =)
But what i still wonder about is where to set the distance. There is no Option saying device.RenderState.FogDistance or something like this.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 15/12/2008 at 00:29:57
<<A "Edit Post"-Option woukd be usefull>>

Second quetsion:
When I use Exponential FogMode, ALL the Terrain is colored gray. But i wanted to have a Fog with an increasing density to the increasing distance. U know?
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 15/12/2008 at 07:40:04
Using linear fog you can exactly specify where your fog shall start and where it shall end. Xna interpolates within this range. But from my point of view linear fog is very unrealistic.

Exponential fog does take into account the distance between your camera and the 3d geometry. It is being computed by Xna for every pixel. So, the only thing left you need to specify is the density. You really should try smaller values and have a look at the results.

Since the function that computes exponential fog grows with bigger distance, your terrain will fade out instead of being all gray.

greets.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 15/12/2008 at 12:10:34
but... why donīt my fog grows in density with increasing distance? this terrain has a side-length of 617 points... so in my opinion long enough to see some difference between the first pixel-line and the last one.
but nothing like this, all the terrain is fogged the same density, just like to see in the pic below...


the code I used is the following:


device.RenderState.FogColor = Color.Gray;
device.RenderState.FogTableMode = FogMode.Exp;
device.RenderState.FogDensity = 0.001f;
device.RenderState.FogEnable = true;


I tried to use linear fog too (wich would be enough for my purposes) and get the following result


with this code:


device.RenderState.FogColor = Color.Gray;
device.RenderState.FogTableMode = FogMode.Linear;
device.RenderState.FogStart = 0.1f;
device.RenderState.FogEnd = 0.5f;
device.RenderState.FogDensity = 0.00001f;
device.RenderState.FogEnable = true;


you see I used a depth-index between 0 and 1 for the start end end, and my density-value is very low. but... my world sills gray =(
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 17/12/2008 at 07:32:08
Unfortunately, I think, that I can't help you.

I get the following result using the build in fog functions:


My Settings are:
FogMode.ExponentSquared
FogDensitiy = 0.007f

greets
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 17/12/2008 at 07:48:03
LoL? Your Terrain gets more fogged the farer it is...
Hm... i will try it also with ExpSquare, lets see what happens.
 Poster : chudan
 Posts: 14
 Country : Germany
 City: Dresden

  
Posted by chudan on 17/12/2008 at 07:56:48
YEESS!! It works! Thank you so much!
The only question I still have is:
Could it be, that the Scene-Rendering takes a bit more time compared to No-fog-Scenes? Or does it make no matter?
 Poster : radulph
 Posts: 225
 Country : germany
 City: hamburg

  
Posted by radulph on 17/12/2008 at 10:45:23
Well, I think that it worked using just exponential fog too, but the results weren't as nice.

However, of course fog computation takes time and could have an impact on your frame rate.
Instead of using FogTableMode you can use FogVertexMode. The difference is, that the first one is based on pixels, the sedond one on vertices. For terrains however, where you have lots of vertices, vertex based fog should be satisfiing.

greets
 Poster : Nemo Krad
 Posts: 61
 Country : England
 City: Leicester

  
Posted by Nemo Krad on 18/12/2008 at 16:22:42
Nice fog effects, not read the whole post so I may be telling you something you already know, but the render state fog functions wont work on the Xbox as the 360 uses Shader Model 3 (SM3), even if you write your shaders to use SM2 the compiler will force them to use SM3 on the 360.

Best way to calculate your fog is in the shader, even if you are using SM2 as (IMHO) you have more control over it. Might just be me not totally understanding the render state functions.

I have a few fog samples (old now) on my blog if you want to take a look here:

http://xna-uk.net/blogs/randomchaos/archive/tags/Fog/default.aspx

  
Post a new reply
 





Google
 
Web www.riemers.net
If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !
- Website design & DirectX code : Riemer Grootjans -
©2006 Riemer Grootjans


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)