Sign in to follow this  
asimov02

Detecting Left side of a triangle

Recommended Posts

Hi, I need some help in figuring out how to detect the left side of a triangle. One of the methods I kind of thought up is find the max and the min Y of all the three vertecies and use them. But this is obvously not the right way. I need to detect this so I can find the slope of this line/edge that is used in the Z (depth buffer) calculation. Any help will be appreciated.

Share this post


Link to post
Share on other sites
Since you're talking about "left side", I assume your triangles are in 2D. So just find the normalized perpendicular vector for each one and see which one produces the largest dot product with the left-vector.

Share this post


Link to post
Share on other sites
I presume this is for a software 3D engine. I have one of those myself.

You don't actually need to know which is the left side to calculate the triangle gradients. The gradient across the projected triangle is constant, and can be calculated once for the whole triangle, rather than per edge.
This can be done mathematically without conditional logic (such as sorting the verticies), other than checking that the area isn't below some epsilon. I.e. no need to know anything about the relative positions or ordering of the points.

v0, v1, and v2 consist of the x & y screen-space values of the triangle plus the camera-space 1/z value (which you got when you converted the points to screen-space).
Point4 simply holds four floats (could be separate variables really), and thePolygon is the structure whose screen-space z-delta values you need to set.
Point4 xys(v1.y-v2.y, v0.y-v2.y, v0.x-v2.x, v1.x-v2.x);
float area = xys[2]*xys[0] - xys[3]*xys[1];
if (fabs(area) > EPSILON) {
xys /= area;
float v0v2 = v0.z - v2.z;
float v1v2 = v1.z - v2.z;
thePolygon.deltax = v0v2*xys[0] - v1v2*xys[1];
thePolygon.deltay = v1v2*xys[2] - v0v2*xys[3];
} // else the polygon is probably too small to render at all



The only caveat is that you have to ensure that none of these points have a zero 'z' value prior to the screen-space projection (obviously), which I take care of by offsetting them by a tiny amount beforehand in the z direction if that happens. But other than that, you can use the unclipped camera-space points.

Share this post


Link to post
Share on other sites
Quote:
Original post by asimov02
The problem is that to do what you suggest i would still need to know which side is the left vector.

Depends on your tranforms, but usually (-1,0,0).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this