Sign in to follow this  
Halsafar

Effortless FOG

Recommended Posts

I suppose for fog I have a few choices... VS, PS, FFP I need to get this game I've been working on into development stages. There aint much left to do and I got no brain left to learn. I need easy fog, I know it is exists. 3 questions then, only 1 answerer needed: - a tutorial on using the FFP fog - a listing of how to do the FFP fog - a tutorial provided vertex shader code to generate fog Thanks a bunch, Halsafar

Share this post


Link to post
Share on other sites
Clicky!

edit:

float fogstart=10.0f;
float fogend=100.0f;

Device->SetRenderState( D3DRS_FOGENABLE, true );
Device->SetRenderState( D3DRS_FOGSTART, *(DWORD*)fogstart );
Device->SetRenderState( D3DRS_FOGEND, *(DWORD*)fogend );


edit 2: Beaten to it by matthughson

Share this post


Link to post
Share on other sites
Here is my function for turning fog on/off:

Function Header:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function: "setFog"
//
// Input: turnOn Whether you want to turn the fog on or off
// distanceToFogStart The distance a vert has to be from the camera before it STARTS to fade
// distanceToFogEnd The distance a vert has to be from the camera when it completely disapears
// r,g,b The RGB value to use as the ambient light (values from 0 to 255)
// Return: MPRESULT Result of the method
//
// Purpose: Sets up distance fog.
// NOTE: To turn fog off, simply send the function false as its only parameter
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MPRESULT setFog(bool a_bTurnOn, float a_fDistanceToFogStart = 0, float a_fDistanceToFogEnd = 0, int a_iR = 0, int a_iG = 0, int a_iB = 0);



Function Body:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function: "setFog"
//
// Purpose: Sets up distance fog.
// NOTE: To turn fog off, simply send the function false as its only parameter
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MPRESULT CDX_D3DWrapper::setFog(bool a_bTurnOn, float a_fDistanceToFogStart, float a_fDistanceToFogEnd, int a_iR, int a_iG, int a_iB)
{
// First check if they are just trying to turn fog off
if(!a_bTurnOn)
{
// Disable fog blending.
m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE, FALSE);

// No need to do anything else
return MPERR_OK;
}

// Enable fog blending.
m_pD3DDevice->SetRenderState(D3DRS_FOGENABLE, TRUE);

// Set the fog color.
m_pD3DDevice->SetRenderState(D3DRS_FOGCOLOR, D3DCOLOR_XRGB(a_iR, a_iG, a_iB));

// Set the fog mode
m_pD3DDevice->SetRenderState(D3DRS_FOGVERTEXMODE, D3DFOG_LINEAR);

// Tell direct x where the fog starts and ends
m_pD3DDevice->SetRenderState(D3DRS_FOGSTART, *(DWORD *)(&a_fDistanceToFogStart));
m_pD3DDevice->SetRenderState(D3DRS_FOGEND, *(DWORD *)(&a_fDistanceToFogEnd));

// Everything went fine
return MPERR_OK;
}




Ignore the MPRESULT and MPERR_* stuff. That's just my engines error message handler.

It's pretty straight forwards, but if you have any questions, let me know!

Matt Hughson

Share this post


Link to post
Share on other sites
Quote:
Original post by Halsafar
However, after fiddling with the d3d ffp fog effects... I must say it doesn't do a great job. Oh well.

It's very basic, "placeholder" fog. If you really want good fog, you can roll your own with a preprocessing step or with shaders (recommended).

Edit: Duh! Just realized you want tutorials and ready stuff, here's something:
Real time rendering of heterogenous fog based on the graphics hardware acceleration - HLSL fog.

Share this post


Link to post
Share on other sites
Hey wow, thats actually an interesting read.
Unfortuntly that will have to be used in my next game engine...

I've been working on this current engine for near 5 months and its about ready to an actual game to be written for it. I've had to learn SO much that I feel I now need to use what I've learned or it will all be for not.

Again tho, thanks, thats a great article, nice find.

Share this post


Link to post
Share on other sites
You can do your own custom per-pixel fog using the D3D fixed-function pipeline's texture unit. You enable texture coordinate generation based on the vertex position, then use the Z as a texture coordinate which controls fetches from a 1D ramp texture. You don't have to constrain this to just Z, you can use Y to make the fog hug the ground.


Share this post


Link to post
Share on other sites
Quote:
Original post by don
You can do your own custom per-pixel fog using the D3D fixed-function pipeline's texture unit. You enable texture coordinate generation based on the vertex position, then use the Z as a texture coordinate which controls fetches from a 1D ramp texture. You don't have to constrain this to just Z, you can use Y to make the fog hug the ground.


This sounds really interesting.. would you mind explaining it in slightly more detail? How do you enable texture coordinate generation based on the vertex position? How/where do you set the 1D texture?

Share this post


Link to post
Share on other sites
See the docs for D3DTSS_TEXCOORDINDEX and D3DTSS_TCI_CAMERASPACEPOSITION.

It's basically mapping some coordinate from your world to a value within a texture that corresponds to an amount of fog that you want to apply. You can use a texture transformation matrix to convert the camera space Z value where it lies in the (0, 1) range of a texture coordinate system.

The blending can be performed using the COLOROP, D3DTOP_LERP:

COLOR1 = TEXTURE (this is your fog ramp)
COLOR2 = TFACTOR (this is your fog color)
COLOR0 = CURRENT

The alpha value is passed through untouched.

Share this post


Link to post
Share on other sites
Will this easily create the fog I am looking for...
So you cannot see the oncoming terrain spawning in the distance but rather comes into sight as one since the fog was covering it as it came into the farplane...

Share this post


Link to post
Share on other sites
I am using D3D's pixel fog...
Maybe I am setting the start and end distance badly but no matter how hard I try I cannot get that effect created. Everything still spawns ugly in the farplane just now it is colored like fog...

That may have something to do with the PS I am using, a regular ps1.4 for texture splatting.

Share this post


Link to post
Share on other sites
Well you normally set your fog color to match the background color, so if the object is completely fogged it won't be visible.

It sounds like you have a texture or something as a background so instead of blending with fog, maybe you want to alpha blend the mesh, with the amount of alpha based on distance.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this