fog in hlsl (shader)

Started by
10 comments, last by suliman 11 years, 4 months ago
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:
http://communistgame...og-in-hlsl.html

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

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;
}


Thankful for any help
Erik
Advertisement
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!
but my problem is i dont understand how to send data (such as camera position) from the main program (c++) to the shader (hlsl)
Go through the D3D9 samples in the SDK, they show you how to work with shader constants either with or without the effects framework. BasicHLSL and HLSLWithoutEffects should be relevent.
The structure is simply

struct PS_OUTPUT
{
float4 diffuse : COLOR0;
};


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

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();
}
}
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....
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 this msdn page. Shader constants can be a great performance boost and helpful.
Perception is when one imagination clashes with another
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
Noone?
D3DDEVICE is the DX device you created when initializing DX. pEffect is a pointer to your compiled HLSL file.

******************************************************************************************
Youtube Channel

This topic is closed to new replies.

Advertisement