Detecting Left side of a triangle

Started by
3 comments, last by Sneftel 14 years, 2 months ago
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.
Advertisement
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.
The triangles are actually in 3D but the z axis can be ignored without any problem.

The problem is that to do what you suggest i would still need to know which side is the left vector.
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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).

This topic is closed to new replies.

Advertisement