Jump to content
  • Advertisement
Sign in to follow this  
shaqdx

Calculating normals in pixel shader?

This topic is 4833 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

Can anyone help me in calculating normals inside the pixel shader? I want to have some diffuse lighting on a texture I created. So far the input to my vertex shader is a position and texture coordinate. I want to retrieve 3 vectors, and then use the cross product. But how retrieve the vectors. Do I use the function tex2D(object, vector), or something. Thanks

Share this post


Link to post
Share on other sites
Advertisement
Do you want the normal per-pixel or per-vertex? Because the vertex normal can only be passed in from the vertex shader, whereas the pixel normal can be retrieved from a normal map texture using tex2D.

Share this post


Link to post
Share on other sites
Quote:
Original post by shaqdx
So far the input to my vertex shader is a position and texture coordinate. I want to retrieve 3 vectors, and then use the cross product. But how retrieve the vectors.

If you're just looking for standard normal vectors, the best way to do this is at the level of the application instead of inside your shaders. Calculate the normal vectors at the application level and put them into your model data so that they are fed to the vertex shader along with the position and texture coordinates.

If you're looking for per-pixel lighting, then you might want to investigate normal-maps. These are textures that contain a normal vector as an RGB value at every pixel. Your pixel shader will then retrieve this vector using the texture coordinates supplied by the vertex shader and can carry out its lighting computations using it.

My guess is that you're probably looking for the first solution. Once your engine becomes more advanced, you can start thinking about adding realism to your scenes by using normal-maps.

neneboricua

Share this post


Link to post
Share on other sites
Thanks for your responses so far jameswren and neneboricua19. I want to calculate per-pixel lighting, therefore I need to calculate the normals in the pixel shader.

Both of you have mentioned retrieving the normals from the normal-map texture by using the texture coordinates supplied by the vertex shader. I'm not sure exactly how.

This is what I need help with. Much appreciated.

Thanks

Share this post


Link to post
Share on other sites
Even if you have a normal map, you still need to combine it with the "actual" pixel normal. All you need to do to get per-pixel lighting is to take the normal as an input to the VS, transform it to world space (or whatever space your light is in), pass it through to the PS through a TEXCOORD register, and use the normal in the PS. It'll be interpolated across the face, and will look rather nice when dotted with the light vector to get the lighting amount.

Share this post


Link to post
Share on other sites
You cannot calculate the normal from scratch inside your vertex or pixel shader. You just don't have access to the neighboring vertices. This MUST be done inside your application or by the artist when they create the model.

Per-pixel lighting does not mean that you must calculate the normal from scratch inside the pixel shader. This is not possible. As Drakex said, you need to transform the vertex normal into world space and pass it from the vertex shader to the pixel shader. This will interpolate the vertex normal across the shape of the polygon. That way, the normal you have acess to when your pixel shader runs will allow you to do per-pixel lighting. There are some tutorials here on GameDev and Gamasutra that can show you how to do per-pixel lighting using shaders.

It seems that you're just starting out with this material, which is all good. We all have to learn sometime. I suggest you not worry about normal-mapping for now and just get this part to work and, most imprtantly, understand WHY it works.

neneboricua

Share this post


Link to post
Share on other sites
Cool. Thanks neneboricua19 and Drakex for explaining that. I am new to shader programming, so I'm still picking up the basics.

I will attempt what was mentioned and will come back here if I have any more problems.

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!