Sign in to follow this  
suliman

fog in hlsl (shader)

Recommended Posts

Hi

Im using hlsl code from a forum that makes textures splatting on my terrain (i use a directx engine, irrlicht). Now i need fog but im a bit confused on how to add it.

I look at examples like this one:
[url="http://communistgames.blogspot.se/2010/10/fog-in-hlsl.html"]http://communistgame...og-in-hlsl.html[/url]

But im still confused on how to add this to my own shader code I already have. I have figured out i can scale the color of the entire terrain by multiplying the diffuse-color I return in the PS_OUTPUT ps_main(in VS_OUTPUT input) function but dont understand how the shader code can use/know the actual distance from the camera (this is needed to do fog right?). Linear fog is fine for now.

The output function i have now is
[CODE]
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureFour, mul(input.tex, texScale));
vector j = tex2D(TextureFive, mul(input.tex, texScale));
vector k = tex2D(TextureSix, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;
return output;
}
[/CODE]

Thankful for any help
Erik Edited by suliman

Share this post


Link to post
Share on other sites
Without knowing the contents of the VS_OUTPUT structure it is hard to give precise instructions.
However, probably you can include the camera/view space position to the VS_OUTPUT structure (although clip space position can be used too). The position's z-component can be used as a distance from the view point. The typical fog formulas are available in MSDN or wikipedia.

Cheers!

Share this post


Link to post
Share on other sites
The structure is simply
[CODE]
struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};
[/CODE]

I can change the diffuse before returning it and this has its desired effect. I just need to know how to know the distance to the camera so i can interpolate the existing color with my fog color.

I know this might come off as lazy but ive skimmed through some hlsl tutorials and they are all very extensive and i just need this to move on with all the other things in the project that has now come to an halt (learning shaders properly must be a later task for me right now).

Thanks for your help!
Erik

The fulll shader code is posted below for reference
[CODE]
float4x4 matViewProjection : ViewProjection;
sampler AlphaMap = sampler_state
{
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureOne = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureTwo = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};
sampler TextureThree = sampler_state
{
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
ADDRESSU = WRAP;
ADDRESSV = WRAP;
ADDRESSW = WRAP;
};



struct VS_INPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
};
struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
};
struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};
VS_OUTPUT vs_main( VS_INPUT Input )
{
VS_OUTPUT Output;
Output.Position = mul( Input.Position, matViewProjection );
Output.alphamap = Input.alphamap;
Output.tex = Input.tex;
return( Output );
}
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureOne, mul(input.tex, texScale));
vector j = tex2D(TextureTwo, mul(input.tex, texScale));
vector k = tex2D(TextureThree, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;

return output;
}
technique Default_DirectX_Effect
{
pass Pass_0
{
VertexShader = compile vs_2_0 vs_main();
PixelShader = compile ps_2_0 ps_main();
}
}
[/CODE] Edited by suliman

Share this post


Link to post
Share on other sites
Need something like this?

in HLSL:
float3 camposition; // put it before 'float4x4 matViewProjection : ViewProjection;' (not in shader code body)

in cpp:
float campos[] = {0,0,-100}; // some random nums for you
pEffect->SetValue("camposition",campos,3*sizeof(float)); // pEffect is a valid ID3DXEffect object

For fog, you need the distance between the pixels and the camrea, vertex shader knows about vertex positions, so just give that to the pixel shader.

struct VS_OUTPUT
{
float4 Position : POSITION0;
float2 alphamap : TEXCOORD0;
float2 tex : TEXCOORD1;
float3 pixelcoord: TEXCOORD2; // thadam!!
};

in VS:

Output.pixelcoord = input.Position.xyz; // world space, i suppose

in PS:

float distance = length(input.pixelcoord - camposition); // dist between pixel and camera

// do some fancy fogging with it....

Share this post


Link to post
Share on other sites
Alternatively, you can use shader constants, which is what MJP was referring to. The syntax is as such:

In hlsl at global scope:
float3 camPosition : register( c0 );

In cpp:
float camPosition[] = {0,0,0}
D3DDEVICE->SetVertexShaderConstantF( 0, camPosition, 1);

For reference you can use [url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb509704(v=vs.85).aspx"]this[/url] msdn page. Shader constants can be a great performance boost and helpful.

Share this post


Link to post
Share on other sites
both D3DDEVICE and pEffect give me undeclared identifier.
Do they need to be created (i guess so)? I dont find how to in the msdn page or its general search.

thanks for your help

Share this post


Link to post
Share on other sites
Hi again
Ive managed to send the camPos to hlsl now i think.
Is there no way to step through the code to see what values you have/what actually happens like i can do in the c++ code? Its very hard to understand whats going on in the shader part of this...
So i have the correct (i hope) pos in the shader now. This is the PS_OUTPUT
[code]
float3 camPos : register( c0 ); //at top of file
PS_OUTPUT ps_main(in VS_OUTPUT input)
{
float texScale = 1.0;
PS_OUTPUT output = (PS_OUTPUT)0;
vector a = tex2D(AlphaMap, input.alphamap);
vector i = tex2D(TextureOne, mul(input.tex, texScale));
vector j = tex2D(TextureTwo, mul(input.tex, texScale));
vector k = tex2D(TextureThree, mul(input.tex, texScale));

float4 oneminusx = 1.0 - a.x;
float4 oneminusy = 1.0 - a.y;
float4 oneminusz = 1.0 - a.z;
vector l = a.x * i + oneminusx * i;
vector m = a.y * j + oneminusy * l;
vector n = a.z * k + oneminusz * m;
output.diffuse = n;
return output;
} [/code]
This obviously do not take any notice of the camPos (so no fog yet).
But how would i use the camPos to get fog? I try something like
output.diffuse = n*camPos.x;
just to see a reaction but it just stops rendering the texture completely (shows color value only)
thanks for your help
Erik

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