   Vertex winding. Woowee!
  Sorry for making a thread about this. I originally posted this question in another thread i've made not too long ago,but it seems like people don't notice it so i thought it would be best to make a new thread.I thought of an algorithm that is relatively complex compared to what i've done so far. One of the calculations i need to do in that algorithm is take a single triangle,move from vertex A to B to C and see if it is a clockwise triangle or a counter clockwise triangle. i think i saw some method in google that relies on adjacent triangles,but in this case i have a single triangle so nope.How do I actually do that?   
  Suppose that we have a triangle and that a guy is above it. Suppose that when we successively pass through the points p1, p2 and p3 (points of the triangle), we describe a clockwise movement from the guy's point of view. Now, suppose the guy's under the triangle. We pass again through p1, p2 and p3. Is the motion described still clockwise or anti-clockwise from his point of view?[Edited by - johnstanp on June 7, 2010 9:15:39 AM];  First, you need to pick the direction along which you want to categorize the triangle. A CW triangle from one direction is CCW from the opposite direction.One possible approach is to generate two vectors, one from vertex 0 to vertex 1, the other from vertex 0 to vertex 2. Calculate the cross-product of those vectors. Depending on your coordinate system (right-handed or left-handed), the cross-product will be positive for CW winding, negative for CCW winding.   ;  Suppose he picks the z-axis as the direction to categorize triangles.What happens if the triangle is in the x-z plane or y-z plane? Clockwise is defined in terms of rotation. You have an axis and you rotate either clockwise or counterclockwise. If you have only three points and "no axis of rotation" (i.e no other information), there's no way to know if a triangle is clockwise or counterclockwise. You need to know the normal of the triangle first: and its "orientation" is arbitrary. For a triangle, I can chose either normal = cross( v1, v2 ) or normal = cross( v2, v1 ). There's no constraint. If you have a mesh without normals you just have to assume that the normal is defined by normalize( cross( v1, v2 ) ) or normalize( cross( v2, v1 ) ) and see if your model is lit correctly.   ;Quote:Original post by johnstanpIf you have only three points and no axis of rotation (i.e no other information), there's no way to know if a triangle is "clockwise" or "counterclockwise"Then how do D3D and OpenGL classify triangles as back-facing? All they have are the 3 vertices of the triangle.   ;Quote:Original post by Gage64Quote:Original post by johnstanpIf you have only three points and no axis of rotation (i.e no other information), there's no way to know if a triangle is "clockwise" or "counterclockwise"Then how do D3D and OpenGL classify triangles as back-facing? All they have are the 3 vertices of the triangle.They compute the normal as:normal = normalize( cross( v1, v2 ) )if given in succession v1, v2 and v3.That's implicit. They could have chosennormal = normalize( cross( v2, v1 ) )if given in succession v1, v2 and v3.The first choice is more natural, but it is arbitrary.P.S: normalize(...) if v1 and v2 cannot be unit long.   ;  My guess that openGL and directX calculate the winding in screen space. A 2D cross product (or whatever that's called...) then check, is it's positive or negative.Again: just a guess.Okay, I should shut up if I have no clue about something   ;Quote:Original post by szecsMy guess that openGL and directX calculate the winding in screen space. A 2D cross product (or whatever that's called...) then check, is it's positive or negative.Again: just a guess. Or: it's in the rasterizer somehow.Okay, I should shut up if I have no clue about somethingThey postulate that the normal is:normal = normalize( cross( v1, v2 ) ).As the cross product of v1 and v2 is a vector which has the y- and x-components null, they just take into account the sign of the z-component. In 2D, the choice of the z-component is natural, but still arbitrary. Nothing restricts me from choosing -z-component.My first post was to illustrate that counterclockwise or clockwise winding is arbitrary when you don't have a normal explicitly defined. Now, everyone assumes that when given v1, v2 and v3 the normal is normalize( cross( v1, v2 ) ).There's an implicit postulate: the order gives you directly the clockwise information, if you don't have a normal already provided.[Edited by - johnstanp on June 7, 2010 10:52:02 AM];Quote:Original post by johnstanpQuote:Original post by szecsMy guess that openGL and directX calculate the winding in screen space. A 2D cross product (or whatever that's called...) then check, is it's positive or negative.Again: just a guess. Or: it's in the rasterizer somehow.Okay, I should shut up if I have no clue about somethingThey postulate that the normal is:normal = normalize( cross( v1, v2 ) ).As the cross product of v1 and v2 is a vector which has the y- and x-components null, they just take into account the sign of the z-component. In 2D, the choice of the z-component is natural, but still arbitrary. Nothing restricts me from choosing -z-component.My first post was to illustrate that counterclockwise or clockwise winding is arbitrary when you don't have a normal explicitly defined. Now, you can arbitrarily choose one and do everything you want after that.I bet they don't normalize the vector.   ;  You're right. It is obvious that no normalization is needed if at the end I say that only the sign of the z-component matters.So what is the point? In what way does it lessen what I said on the real subject?[Edited by - johnstanp on June 7, 2010 11:20:04 AM]