Jump to content
  • Advertisement

Archived

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

cjmaxwell

Triangle Winding

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

Hey, I am having some trouble finding an efficient way of winding a triangle. My goal is to have a function that takes in three arbitrary 2D (X,Z) points and places them in a triangle with points A,B,C. ‘A’ drawn first and ‘C’ drawn last. I can’t find a repeatable/reliable way of determining which point should be assigned to which triangle letter so the triangle will have a CW (Clock Wise) winding. Any help would be greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Maybe I don''t understand your question, but why not just look at the direction of the cross product?

Share this post


Link to post
Share on other sites
What two points would I take the cross product of and how would I implement the result of the test to a decision of which points go to which triangle letters?

Share this post


Link to post
Share on other sites
Consider two vectors AB and AC. Take the cross product of those two vectors. If the magnitude is positive then it means that A, B and C are in clockwise order A->B->C. If the magnitude is negative then A->B->C is counterclock wise order. (Of course right hand system versus left hand system will reverse this.)

Share this post


Link to post
Share on other sites
My goal is to calculate the center of a triangle.

My algorithm is:

Find to edges of the triangle (I think this step has the problem)
E1 = P1 – P0
E2 = P2 – P0

I find the center (middle) of the line
EP1 = E1 scaled by 0.5
EP2 = E2 scaled by 0.5

I make a line from each middle point to the other triangle point
L1 = (EP1, P2)
L2 = (EP2, P1)

I find the intersection of those two points (My algorithm for this step is stable) and get my triangle’s center point.

I use OpenGL to draw the triangle and the point.

It works with some triangles but not with others so I thought it was my winding? Maybe my overall approach is in correct? My normals are produced correctly so I orignally thought my triangles'' winding was correct. Now I''m just confused...

Share this post


Link to post
Share on other sites
I think the problem is that the center of a triangle is not a very well defined concept. Do you want the centroid, incenter, circumcenter or the orthocenter? (And there are probably a couple other types of triangle centers that I can''t think of off the top of my head.) Basically the question is what properties do you want from the center of the triangle?

Share this post


Link to post
Share on other sites
The biggest problem is that my "center point" is drawn outside of the triangle from time to time. I''m just looking for a way to represent the triangle''s face normal. I draw a line from the center point to the (center point plus the triangle''s normal)

Share this post


Link to post
Share on other sites
the cross product should do it.
you have points A, B, C, and you want to assign them to a counter-clockwise ordered vertex array V[3];


V[0] = A;
V[1] = B;
V[2] = C;

float E = B - A;
float F = C - B;
float sign = E.x * F.y - E.y * F.x;

// swap vertices if listed clockwise

if (sign < 0.0f)
{
Vector Temp = V[0];
V[0] = V[1];
V[1] = Temp;
}

Share this post


Link to post
Share on other sites
I have one more question. What happens if you have a bigger polygon with more vertices. Say containing 6 vertices all in random order and want to make them to be in CCW or CW order. Do you just do same thing as the code above? and jump 2 vertices further to do same thing again (ie. the cross product?) and again or there is a better method to do this?

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.

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

Sign me up!