Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

GuybrushNivek

Using face normals for back-face culling

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

Hi all. I am having some problems with understanding normals... I want to use them for back-face culling, and I think if I get this to work, then I may know how to use them for light-sourcing, etc... The problem is... If I have a triangle. - X , Y , Z 0 - 85 , 100 , 80 1 - 200 , 75 , 60 2 - 100 , 50 , 40 ;In anticlockwise order :-) I know how to get the surface normal (is this right?) Firstly, I get two 2 vectors from this triangle Vector1X = triangle(0 , 0) - triangle(1 , 0) ;Vector 1 -> 0 Vector1Y = triangle(0 , 1) - triangle(1 , 1) Vector1Z = triangle(0 , 2) - triangle(1 , 2) Vector2X = triangle(1 , 0) - triangle(2 , 0) ;Vector 2 -> 1 Vector2Y = triangle(1 , 1) - triangle(2 , 1) Vector2Z = triangle(1 , 2) - triangle(2 , 2) Next I find the magnitude of both these vectors Mag1 = Sqr((Vector1X * Vector1X) + (Vector1Y * Vector1Y) + (Vector1Z * Vector1Z)) Mag2 = Sqr((Vector2X * Vector2X) + (Vector2Y * Vector2Y) + (Vector2Z * Vector2Z)) Next, I normalise these vectors Vector1X = Vector1X / Mag1 Vector1Y = Vector1Y / Mag1 Vector1Z = Vector1Z / Mag1 Vector2X = Vector2X / Mag2 Vector2Y = Vector2Y / Mag2 Vector2Z = Vector2Z / Mag2 Finally, I get the normal components for the triangle face. NormalX = Float Vector1Y * Vector2Z - Vector1Z * Vector2Y NormalY = Float Vector1Z * Vector2X - Vector1X * Vector2Z NormalZ = Float Vector1X * Vector2Y - Vector1Y * Vector2X Now, this data I have stored at the start of my program for every triangle face I use. Every time I rotate the triangles around their axis'' I also rotate all the Normals...so, they should be facing away from their respective triangle no matter what... But I am stuck on the math behind actually using this data to determine if the triangle is facing away from me. Could anyone help ? Thanks, in advance. :-)

Share this post


Link to post
Share on other sites
Advertisement
first, get the vector pointing from your camera position to the triangle. then, compare the angle between that ray and the triangle normal. if the angle is 0-90 then it's back facing. if it's 90-180 then it's front facing. to be a bit more efficient, rather than casting a ray you can probably just take the near clip plane surface normal and use that for all your tests. i would guess that the slight errors you will get will not be all that visually significant. you might drop/add a few extra triangle but it would probably not be noticable.

as an aside, it seems like it might help you a great deal to spend some time learning more linear algebra so a lot of this stuff can become a little bit more sensical. a broad overview of what is a normal/vector/point/plane/etc would probably give you a really intuitive grasp of the subject.

-me

[edited by - Palidine on November 20, 2002 1:46:48 PM]

Share this post


Link to post
Share on other sites
just check the normal.z value...


if (normal.z < 0)
{
DrawTriangle();
}
else
{
DontDrawTriangle();
}

If the normal is facing towards you (it''s pointing in the negative direction) then you are seeing the triangle from the front, otherwise, it''s facing backwards, so don''t draw it.

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites
By the way.. if the only thing you use it for is face culling.. you don''t even need the normalx and normaly values . But if you are using it for color.. then you do need these.

Share this post


Link to post
Share on other sites
Ready4Dis''s method will only work if you are transforming world coordinates rather than transforming camera coordinates between renders. i.e. the move the world, not the camera method of motion. if you move the camera about, then the object''s normals will never change. it definitely doesn''t matter which method you choose, b/c either way you do the same number of transforms. just a heads up.

-me

Share this post


Link to post
Share on other sites
"Every time I rotate the triangles around their axis'' I also rotate all the Normals...so, they should be facing away from their respective triangle no matter what..."

He''s rotating the triangles + normals.

Share this post


Link to post
Share on other sites
Yeah, thanks. I have these details, and just tried the NormalZ comparison, but it isnt working...the thing is, I''m rotating my triangle for camera space, but I''m not sure how to rotate the normal I have stored...I am currently rotating it along with my vertices. Either way, the Z Comparison isnt working....

Bugger!

Any ideas ?


The normals have been stored, just like the vertices, then all are rotated by the overall angles...then these values are used for the current frame...

:-)

Share this post


Link to post
Share on other sites
Theres something up with my rotations with the normals...I got the Z comparision to work, and also display the normal as I''m using it...the triangle is disappearing and reappearing when it shouldnt...Am I to rotate the normals as I would vertices...i.e. around a central axis...and in this way, can I think of the normal as a vertice?

:-)

Share this post


Link to post
Share on other sites
Yes, when I wrote my software engine, I rotated normals along with the vertices. But, you must rotate it around it''s own center, not the triangle, or camera''s center. Otherwise, it would no longer be a normal .

Share this post


Link to post
Share on other sites
Thanks you guys who replied...for the Z Check especially as it works...

It turns out I had a faulty set of rotation formulas, which didnt correspond to the rotations of the vertices...those vertices and normals were outta whack !!!



:-)

Share this post


Link to post
Share on other sites

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