# Normal(vector) pixel shader: possible? how?!

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

## Recommended Posts

I'm a vertex/pixel shader n0_0b and I had the idea(I'm not the first probably) to make a pixel shader that renders 3D models with a 'normal map look'(it seems to be too useless but it may be good for learning purposes). For each pixel, take the components of the normal at there and convert them from XYZ to RGB values. It is not all that complex but, how could I get the normal vector? is it possible? Thanks.

##### Share on other sites
You want to visualise the normal vectors, right?

Basically you have to link the vertex shader output to the pixel shader input. Per-vertex attributes such as normal vectors only get input into the VS unit and it's the shaders (i.e. your) responsibility to propagate that further down the pipeline.

Set up your VS's output semantic to have a float3 with a TEXCOORD[n] semantic and it'll be accessible to the PS and interpolated accordingly. Note that vectors can become denormalized during interpolation so be sure to call the normalize() intrinsic in the PS before you actually use it.

Also, note that normal vectors are in the [-1..+1] range whereas colours are in [0..1] range. This is easy enough, just add one and divide by two, but failing to do this can be a bit of an obscure bug to track down at times [wink]

hth
Jack

##### Share on other sites
Example:

struct VS_OUTPUT	{	float4 Position	: POSITION; 	float3 Normal : TEXCOORD0;	};VS_OUTPUT VertexShader ( float4 vPos : POSITION, float4 normal : NORMAL0)	{	VS_OUTPUT Output;	Output.Position = mul(vPos, [somematrix]); // stuff you should already have	Output.Normal = normal;	}PS_OUTPUT PixelShader ( VS_OUTPUT inVert)	{	Output.Color = inVert.Normal; // you'll probably want to do some extra operations on the normal	}

##### Share on other sites
Thanks for the replies guys =). I think I've understood that but one question remains: the vertex shader function is called for each vertex and the pixel shader function is called for each pixel right? but what happens when the pixel shader works together the vertex shader? could anyone please explain it in a general context for me?

##### Share on other sites
Quote:
 Original post by xissburgthe vertex shader function is called for each vertex and the pixel shader function is called for each pixel right?
Correct.
Quote:
 Original post by xissburgbut what happens when the pixel shader works together the vertex shader?
They always work together. Geometry data gets fed via clipping and the rasterizer and on to the shading units. Are you confused about the difference in resolution (few vertices <-> many pixels)?? If so, it's worth understanding there are a number of interpolators that will linearly interpolate values for the many pixels that might be rasterized between where the vertices were projected. These manifest themselves as the TEXCOORD[n] semantics used as outputs from the VS and inputs into the PS.

hth
Jack

##### Share on other sites
Then the values are automatically interpolated...The last question: and about the program flow? Direct3D first process every vertices by calling the vertex shader function and after that it process every pixels calling the pixel shader function using as input the vertex data processed previously?!

Thanks a lot guys

##### Share on other sites
Yes, in simple terms you've got the right idea.

If you're struggling to visualise the D3D9 pipeline then Rich Thomson's Direct3D 9.0 Graphics Pipeline Poster is a must-see. It might be a bit confusing to start with, but it will be an hour well invested if you sit down and study it fully.

hth
Jack

##### Share on other sites
Thanks thanks Jack and jaafit, thanks for the link to that big n' complete poster. I think I'll print one and put it on the wall of my bedroom :). I'll study that. thanks

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013547
×