Advertisement

Backface culling!

Started by September 24, 2002 12:52 PM
7 comments, last by Zoot 22 years, 4 months ago
Like allways... it never works... I was read about backface culling on http://www.cfxweb.net/~baskuene/backface.htm and i came up with this code but something is wrong... //Vectex of the polygon vector a(0,0,0); vector b(0,1,0); vector c(0,0,1); vector N = ((a - b) * (c - a)).Normalize(); vector V = renderpos; //cam pos = where in the level i am bool bVisible = acos(N.x * V.x + N.y * V.y + N.z * V.z) < 90; if (bVisible) { //render the polygon }
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
quote:
Original post by Zoot
bool bVisible = acos(N.x * V.x + N.y * V.y + N.z * V.z) < 90;

I expect acos() gives angle in radians, not degrees.
Advertisement
Ok, i fixed that but no differnt..
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
Ok, you must also divide with the length of V (before taking acos) or use a normalized vector instead.
Ok, first you should not use the camera position as V, but rather the difference of the camera position and polygon position, or something like that... Also you must divide with the length of V before taking acos, or use a normalized vector instead.
I change it to


    vector a = pPipeline[x]->v[0];vector b = pPipeline[x]->v[1];vector c = pPipeline[x]->v[2];vector N = ((b - a) * (c - a)).Normalize();vector V = (a - renderpos).Normalize();bool bVisible = acosf(N.x * V.x + N.y * V.y + N.z * V.z) * 180.0f/PI < 90.0f; if (bVisible){//render the polygon}    


but it is still not working...


[edited by - Zoot on September 24, 2002 3:16:27 PM]
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!
Advertisement
Ok, take this with a big pinch of salt, cos I'm posting without thinking much about it - but do you really need to do an acos()? Can't you just use the dot product's property of giving you the projection of 1 vector on another? So the answer will be positive in one case (like facing forwards), and negative for the other (like facing backwards).

Like I said, this is all without much thought. I've never done a backface culling thingy before, and I haven't looked at the link you posted

Oh, and if you're looking for something quick to try, change

vector N = ((a - b) * (c - a)).Normalize();

to:

vector N = ((a - b) * (a - c)).Normalize();

unless of course you know the first one to be correct. Still worth a shot though

Miles

[EDIT: sorry, didn't see you'd already tried the last bit.]

[edited by - Miles Clapham on September 24, 2002 4:02:13 PM]
Ok, this thread is a huge jumbled mess, so allow me to straighten everything out.

This is the information we need:
1) Normal of Polygon
2) Distance between polygon and origin
3) Point to check

And this is how you manipulate the data to get a result:
1) Dot product between point to check and polygon normal, minus distance between polygon and origin
2) If previous dot product < 0, point BEHIND polygon. If previous dot product > 0, point IN FRONT of polygon. If previous dot product = 0, point ON polygon, or rather coplanar with polygon.

This is how we go about finding your initial information:
1) Normal of Polygon
The normal of the polygon is calculated by first creating two vectors out of three points that lie on the polygon. The direction these vectors face is important - the cross product used to find the normal follows the right hand rule. So take the cross product, and normalize the result.


2) Distance between polygon and origin
Under the assumption that the normal created in part 1 actually describes a plane that has distance zero from the origin, we can take the dot product between a point on our polygon and our normal to find the distance between the origin and the polygon.


3) Point to check
Whatever you want


Finally, the code sample that will do all of the above:

  vector a = pPipeline[x]->v[0];vector b = pPipeline[x]->v[1];vector c = pPipeline[x]->v[2];vector ab = (b - a);vector ac = (c - a);// Remember, ORDER COUNTS! ab x ac != ac x abvector normal = CrossProduct(ab, ac).Normalize();// a can be replaced with any of the polygon points (b, c, ...)// can also be -DotProduct, but later we add the distance insteadfloat d = DotProduct(a, normal);// We now have a normal, a distance, and hopefullya point to check.  Let''s go...// If we used -DotProduct above, then use + d herefloat point_distance = DotProduct(normal, PointToCheck) - d;if(point_distance < 0){   // Point behind polygon}else if(point_distance > 0){   // Point in front of polygon}else{   // Point on polygon}  

Enjoy, but most importantly, I hope you learned something
Lovly... it works perfectly.. +20 fps in my engine...
GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don't post crap!

This topic is closed to new replies.

Advertisement