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.

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

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.

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

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.

Create an account

Register a new account

• Forum Statistics

• Total Topics
628730
• Total Posts
2984431

• 25
• 11
• 10
• 16
• 14