# Normals in a pixel shader

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

## Recommended Posts

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 on other sites
Can you paste the shader that you have written, it is impossible to see what you are doing wrong otherwise.

Dave

##### Share on other sites
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 on other sites
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 on other sites
Quote:
 Original post by skittleoYou 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 on other sites
Can you post the whole shader. We can't see if the vertex shader is passing the correct data down. We can't see if you're using a ps.1.1 target (which doesn't allow negative numbers to be passed from vs to ps).

##### Share on other sites
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 on other sites
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 on other sites
Okay, thanks. I have done it before, I can't seem to remember how to set directx to debug :(

##### Share on other sites
It is in the start menu forlder for the sdk ->Utilities...

1. 1
Rutin
40
2. 2
3. 3
4. 4
5. 5

• 18
• 20
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633363
• Total Posts
3011514
• ### Who's Online (See full list)

There are no registered users currently online

×