Sign in to follow this  
_Sauce_

Construct a plane from 3 points

Recommended Posts

I'm attempting to construct a plane from 3 points for my raytracer, and wanted to check that my code is correct. I can construct a plane from a normal and a distance along that normal to the origin, which renders fine, however when constructing a plane from 3 points, nothing is rendered at all. I'm guessing I end up with an invalid plane, so I wanted someone to check over my code and tell me if I'm still sane.
RTPlane::RTPlane(const Vector3f &A, const Vector3f &B, const Vector3f &C, const RTMaterial &material)
{
	mMaterial = material;

	Vector3f v1 = B - A;
	Vector3f v2 = C - A;

	mNormal = (v1.cross(v2)).normalise();

	mDistance = mNormal.dot(A);
}
The points are wound in a clockwise order.

Share this post


Link to post
Share on other sites
Thanks for the advice, but it doesn't seem to be making any difference.

A = (-1, -1, 0)
B = (1, -1, 0)
C = (1, 1, 0)

mDistance = 0
mNormal = (0, 0, 4) //(0, 0, 1) normalised


As a sanity check I've got another plane with the same coords in reverse winding order...

A = (1, 1, 0)
B = (-1, -1, 0)
C = (1, -1, 0)

mDistance = 0
mNormal = (0, 0, 4)


Are these values correct? If that's not the problem then I'm beginning to wonder whether or not my ray-plane intersection algorithm fails horribly on any plane that isn't facing straight up or down (something which I have yet to test :S)

edit: ray-plane intersection definitely performs as expected.

Share this post


Link to post
Share on other sites
Then length of the normal is irrelevant as long as you satisfy the equation Ax + By + Cz + D = 0, which will always be the case so long as you calculate the distance after the normal. By convention though normals are unit length, which is where the normalization comes in. You should not, however, modify the normal after the distance is calculated or else your plane definition will be incorrect. If you rearrange the equation as Ax + By + Cz = -D, then you can see that if the normal (A,B,C) changes, D must change accordingly to maintain equality.

Long story short, if you modify the normal you must recalculate the distance to maintain correctness. Everything else isn't mathematically necessary, including unit normal length.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?
If you're going to normalize, you should normalize before calculating the distance. (As for whether or not you *need* to normalize the plane normal, that depends on what you'll be doing with the plane exactly.)

Share this post


Link to post
Share on other sites
Quote:
Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?

Absolutely. Remember that another definition of the dot product is:

A·B = |A||B|cos(Θ)

If one of your input vectors has a large magnitude, then your dot product would also be quite large if the vectors were relative parallel. After the normalization, you still have this gigantic distance simply because one of the input vectors had a large magnitude and has nothing to do with the new normal.

Share this post


Link to post
Share on other sites
Quote:
Original post by jyk
Quote:
Original post by _Sauce_
So I should be performing a normalisation before calculating the distance afterall?
If you're going to normalize, you should normalize before calculating the distance. (As for whether or not you *need* to normalize the plane normal, that depends on what you'll be doing with the plane exactly.)

I'll be using the normals for lighting, so yes it's important :)

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