Sign in to follow this  
Programmer101

Normals in a pixel shader

Recommended Posts

Programmer101    199
In a pixel shader, how can you integrate normal processing into the color outputted on the screen? I tried using some examples from the sdk where you take the dot product between the light direction and the normal and multiply the color by that scalar but my terrain always comes out black and the dot product always seems to be zero. Could anyone tell me the correct way to do this?

Share this post


Link to post
Share on other sites
gunning    749
You should check that you're taking the negated dot product between the light direction and normal, since dot products are only >0 when the vectors are facing the same direction.

Share this post


Link to post
Share on other sites
Programmer101    199
Okay, here it is. It seems kinda simple to me, but this was in an sdk example:

PS_OUTPUT ps_main( in PS_INPUT In )
{
PS_OUTPUT Out;
//Texture splatting vars
float4 splat = tex2D(Tex1, In.Texture/32.0/2.0-0.5);
float4 s2;
float4 s3;

//Texture splatting
Out.Color = tex2D(Tex0, In.Texture)*(1.0-splat[0]-splat[1]);
s2 = tex2D(Tex2, In.Texture) * splat[0];
Out.Color += s2;
s3 = tex2D(Tex3, In.Texture) * splat[1];
Out.Color += s3;
//Normals
Out.Color.rgb *= dot(LightDir,In.Normal);

return Out;
}

Share this post


Link to post
Share on other sites
Programmer101    199
Quote:
Original post by skittleo
You should check that you're taking the negated dot product between the light direction and normal, since dot products are only >0 when the vectors are facing the same direction.


Even negating it it is black. Thats why I thought it was 0.

Share this post


Link to post
Share on other sites
Programmer101    199
I put in a test normal that I set in the pixel shader and it worked quite well, but when I use the PS_INPUT structure the normals seem to all be zero which makes me think that they are not being sent to the pixel shader correctly. I know they work b/c it's fine in fixed function so I looked at my vertex declaration. I don't see anything wrong with it but you might:

D3DVERTEXELEMENT9 decl[] = {{0,
0,
D3DDECLTYPE_FLOAT3,
D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_POSITION,
0},
{0,
12,
D3DDECLTYPE_FLOAT3,
D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_NORMAL,
0},
{0,
24,
D3DDECLTYPE_FLOAT2,
D3DDECLMETHOD_DEFAULT,
D3DDECLUSAGE_TEXCOORD,
0},
D3DDECL_END()};



and this is my vertex structure:

typedef struct {
float x,y,z;
float nx,ny,nz;
FLOAT u, v;
} sVertex;


and the FVF

#define VertexFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

Share this post


Link to post
Share on other sites
janta    345
FVF codes are used with the fixed function pipeline only, not with the programmable pipeline (aka vertex/pixel shaders)
If you're calling SetFVF() before your draw call, it may create conflict (I'm not too sure about what problems it could cause but I'm sure you cant use FVF with shaders)

Also, when the result of a rendering call does not give the expected result, one of the first thigs you want to do is turn DirectX i to debug mode with maximum output and validation. Have you tried this yet ?

Edited. Sorry from the wrng info.

[Edited by - janta on March 5, 2007 10:13:29 AM]

Share this post


Link to post
Share on other sites
Defrag    175
Are you actually creating the vertex declaration and setting it before the draw call? As others have pointed out, FVF & vertex declarations don't really mix (they can live happily together for some things, but there are certain vertex declarations that cannot be represented by a FVF).

I'm not sure of the 'perfect' way to do this, but when using shaders I generally set the FVF to 0, then set the vertex declaration to whatever it's meant to be.

Share this post


Link to post
Share on other sites
Shaders can use an FVF just fine. You don't need to set the FVF to 0. Whatever was set last, either FVF or VDecl, is active.

Still waiting for the vertex shader/shader version information. As you've said, the pixel shader works with a hard-coded normal, so the problem isn't there.

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