Jump to content

  • Log In with Google      Sign In   
  • Create Account


fog in hlsl (shader)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 29 September 2012 - 03:53 AM

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, 29 September 2012 - 03:55 AM.


Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2165

Like
0Likes
Like

Posted 29 September 2012 - 07:14 AM

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!

#3 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 29 September 2012 - 12:21 PM

but my problem is i dont understand how to send data (such as camera position) from the main program (c++) to the shader (hlsl)

#4 MJP   Moderators   -  Reputation: 10243

Like
0Likes
Like

Posted 29 September 2012 - 02:43 PM

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.

#5 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 02 October 2012 - 09:45 AM

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

Edited by suliman, 02 October 2012 - 09:47 AM.


#6 hunpro   Members   -  Reputation: 779

Like
0Likes
Like

Posted 02 October 2012 - 08:30 PM

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

#7 Seabolt   Members   -  Reputation: 623

Like
0Likes
Like

Posted 03 October 2012 - 12:44 PM

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

#8 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 04 October 2012 - 12:57 PM

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

#9 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 07 October 2012 - 12:51 PM

Noone?

#10 LancerSolurus   Members   -  Reputation: 488

Like
0Likes
Like

Posted 07 October 2012 - 02:43 PM

D3DDEVICE is the DX device you created when initializing DX. pEffect is a pointer to your compiled HLSL file.

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


#11 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 08 October 2012 - 11:25 AM

removed

Edited by suliman, 17 December 2012 - 10:23 AM.


#12 suliman   Members   -  Reputation: 521

Like
0Likes
Like

Posted 17 December 2012 - 10:21 AM

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)
thanks for your help
Erik




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS