Jump to content
  • Advertisement
Sign in to follow this  
xissburg

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by xissburg
the vertex shader function is called for each vertex and the pixel shader function is called for each pixel right?
Correct.
Quote:
Original post by xissburg
but 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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!