Jump to content
  • Advertisement
Sign in to follow this  
Funkymunky

Inverting normals when rendering backfaces

This topic is 995 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
Advertisement

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

a vertex can be shared between a front and a back face. you'd need to simply calculate both and pass both to the pixel shader and decide on pixel shader side which side to use.

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
Depending on the complexity of the model and performance, the quickest solution sounds like using "doublesided" geometry

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
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!