This topic is 1891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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:
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 Edited by suliman

##### 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 on other sites
but my problem is i dont understand how to send data (such as camera position) from the main program (c++) to the shader (hlsl)

##### Share on other sites
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.

##### Share on other sites
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).

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(); } }  Edited by suliman

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

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

For reference you can use this msdn page. Shader constants can be a great performance boost and helpful.

##### 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.

Noone?

##### Share on other sites
D3DDEVICE is the DX device you created when initializing DX. pEffect is a pointer to your compiled HLSL file.

##### Share on other sites
removed Edited by suliman

##### 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
 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; } 
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)
Erik