Sign in to follow this  

Inverting normals when rendering backfaces

This topic is 684 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've turned off culling because I have some geometry that I want to render both front and backfaces of.  Is there a way to invert the normal when rendering the backface?  Or should I turn culling back on and make the geometry two-sided...

Edited by Funkymunky

Share this post


Link to post
Share on other sites

I should have specified that I am looking to use the normal in the vertex shader.  And now that I've said that I realize that there's no way for the vertex shader to know if it is part of a front/back face yet, so I'll have to duplicate the geometry to get both normals, or try to trick it by transforming it and using abs().  Thanks though!

Edited by Funkymunky

Share this post


Link to post
Share on other sites

You can determine the vertex "facing" by looking at the sign of the view space normal's z component (this can be determined in the vertex shader). This is just an estimate though since vertex normals aren't face normals. "Facing" can now change within a triangle. (But has it's use: I've seen similar to achieve early back face culling of whole patches in a hull shader - using a bias).

 

Just throwing around ideas. 

 

Edit: Seeing the whole picture might help. What are you after exactly ? Maybe another setup is warranted (geo shader, tesselation shader).

Edited by unbird

Share this post


Link to post
Share on other sites

You can try taking the dot product of the view vector and the vertex normal, and then multiplying the normal by the sign of the result.

vertexNormal *= sign(dot(viewVector, vertexNormal));

You can also do this via a conditional operation, which may be a bit faster than using sign().

vertexNormal *= dot(viewVector, vertexNormal) < 0 ? -1.0f : 1.0f;

Share this post


Link to post
Share on other sites

>> Is there a way to invert the normal when rendering the backface?

 

https://en.wikipedia.org/wiki/Normal_(geometry)

 

"For a convex polygon (such as a triangle), a surface normal can be calculated as the vector cross product of two (non-parallel) edges of the polygon."

 

so you should be able to calculate normals for both faces by reversing the order (winding) of the vertices used in the calculation.

see also:

https://en.wikipedia.org/wiki/Vertex_normal

Share this post


Link to post
Share on other sites

This topic is 684 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.

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