Jump to content
  • Advertisement
Sign in to follow this  
ilys4

Backface Culling in GLSL

This topic is 4147 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 need the information whethere the face is front facing or back facing in shdaer. In vertex shader, varying vec3 normal; // and.. normal = gl_NormalMatrix * gl_Normal; In fragment shader, I checked like this, if( normalize(normal).z < 0.0 ) It doen't work at all. all text say 'gl_NormalMatrix * gl_Normal' is normal in eyespace. But in my program I think this is just in world space. Position of camera(eye) doesn't affect to this 'gl_NormalMatrix * gl_Normal' at all. How can I get the normal correctly, so I check the back facing vertex? ps. IF I COULDN'T USE 'gl_FrontFacing' VARIABLE [Edited by - ilys4 on February 1, 2007 6:41:48 AM]

Share this post


Link to post
Share on other sites
Advertisement
That's correct, gl_NormalMatrix * gl_Normal
is eye space.

Also, the direction of the face in screen space could be different. eye space is not going to help you there.

Share this post


Link to post
Share on other sites
Then, I misunderstood eye space.

Screen space? How can I get it?
I need normal of vertex(or frament) in the view of camera.
Actually, I want to know both normals just before and after the perspective transformation(after both camera and world transformation, of course).

Must I pass the camera position as attribute in shader, and caluclate it mathematically?

Share this post


Link to post
Share on other sites
To see if the face is a back face do the following:

Subtract a vertex on the face from the position of the camera. This gives you a vector going from the vertex on the face out towards the camera position. Both these positions should be in world coordinates, so in a VS you will have to transform your vertex first to do this. Then just do a dot product between the face normal and the vector you obtained from the previous subtraction. If you're normals are outward facing (which usually most people have them by convention) then the result of the dot product will be negative if the face is a back face, otherwise it will be positive.

Edit:

Damm sloppy posting. If I remember correctly (I haven't used shaders in about 2 years) then the vertex being passed into the shader should already be in world coordinates because you have to transform it by the modelview matrix (if you're using that to do your camera transforms- which you should) at the end of the shader in order to get it into the right position. So just subtract the vertex from the camera position and then do a dot product of this with the face normal. Nothing much more to it. The sign of the dot product will then tell you if it is a back face or not.

[Edited by - Darragh on February 13, 2007 3:37:50 PM]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!