Intersection point of 3 planes

Started by
2 comments, last by McZ 18 years, 2 months ago
I'm trying to draw the frustum for my lights and cameras I have calculated the planes for the light view by multiply modelview and projection matrix and extract the clipping planes. And now I'm trying to calculate the points for each corner wich would be the intersection points for the planes for example the nearest lower left corner would be the intersection of near, left and bottom plane altough I can't get it to work. this is the formula I have found

		d1 ( n2 x n3 ) + d2 ( n3 x n1 ) + d3 ( n1 x n2 )
	 P = ------------------------------------------------
				 n1 . ( n2 x n3 )
and this is the code implementation

CVertex3f getIntersection( CPlane p1, CPlane p2, CPlane p3 )
{
 CVertex3f P;

 P = p1.d * crossProduct(p2.n, p3.n) + p2.d * crossProduct(p3.n, p1.n) + p3.d * crossProduct(p1.n, p2.n);

 P /= dotProduct( n1, crossProduct(p2.n, p3.n) );

 return P;
}

And then I have read in a post here when searching that I have to multiply these points with the inverse if the projection matrix go be able to draw them. But I can't get it to work with or without the projection matrix multiply. This is the code to extract the planes

// Combine matrices
CMatrix mvp;
mvp = Modelview * Projection;

// extract planes
m_Plane[FP_TOP].n.x = mvp[ 3] - mvp[ 1];
m_Plane[FP_TOP].n.y = mvp[ 7] - mvp[ 5];
m_Plane[FP_TOP].n.z = mvp[11] - mvp[ 9];
m_Plane[FP_TOP].d = mvp[15] - mvp[13];
m_Plane[FP_TOP].normalize();

m_Plane[FP_BOTTOM].n.x = mvp[ 3] + mvp[ 1];
m_Plane[FP_BOTTOM].n.y = mvp[ 7] + mvp[ 5];
m_Plane[FP_BOTTOM].n.z = mvp[11] + mvp[ 9];
m_Plane[FP_BOTTOM].d = mvp[15] + mvp[13];
m_Plane[FP_BOTTOM].normalize();

m_Plane[FP_LEFT].n.x = mvp[ 3] + mvp[ 0];
m_Plane[FP_LEFT].n.y = mvp[ 7] + mvp[ 4];
m_Plane[FP_LEFT].n.z = mvp[11] + mvp[ 8];
m_Plane[FP_LEFT].d = mvp[15] + mvp[12];
m_Plane[FP_LEFT].normalize();

m_Plane[FP_RIGHT].n.x = mvp[ 3] - mvp[ 0];
m_Plane[FP_RIGHT].n.y = mvp[ 7] - mvp[ 4];
m_Plane[FP_RIGHT].n.z = mvp[11] - mvp[ 8];
m_Plane[FP_RIGHT].d = mvp[15] - mvp[12];
m_Plane[FP_RIGHT].normalize();

m_Plane[FP_FAR].n.x = mvp[ 3] - mvp[ 2];
m_Plane[FP_FAR].n.y = mvp[ 7] - mvp[ 6];
m_Plane[FP_FAR].n.z = mvp[11] - mvp[10];
m_Plane[FP_FAR].d = mvp[15] - mvp[14];
m_Plane[FP_FAR].normalize();

m_Plane[FP_NEAR].n.x = mvp[ 3] + mvp[ 2];
m_Plane[FP_NEAR].n.y = mvp[ 7] + mvp[ 6];
m_Plane[FP_NEAR].n.z = mvp[11] + mvp[10];
m_Plane[FP_NEAR].d = mvp[15] + mvp[14];
m_Plane[FP_NEAR].normalize();

Advertisement
maybe it can helps: :P
.
The planes that you get from the extraction algorithm should be in world space, therefore the frustum corners as found by intersecting the planes should be in world space also. So unless you're doing something unusual, you shouldn't have to apply any sort of extra transformation to these points.

Here's one possible reason your code might not be working though. That plane extraction algorithm typically returns the planes in the form ax+by+cz+d = 0, whereas your plane intersection function uses the form ax+by+cz = d. So you might try negating the d's in the plane intersection numerator and see if that helps.

Also, you might double check and make sure the modelview-projection multiplication is in the right order for your API (it should be P*M for OpenGL).
Now I can see something but it is far from correct as far as I can see... I draw the bottom and top lines that connects the near and far planes but the lines are drawn as if the frustum would have a fov at 180 degrees and the fov is 20 degrees.

EDIT: it doesn't matter how I change the fov the lines are drawn the same starting at the position of the light and are drawn as if the light frustum would have 180 degree fov.

EDIT 2: I have now drawn the lines around the near and far plane and it looks like the near and far plane are at the same position, at 1.0f the far plane should be at 100.0f

EDIT 3: All planes have the same information according to what I have logged into my logfile
15:20:04 +---------------------------------------------------------------------------------+15:20:04 | Plane 0 light 0                                                                 |15:20:04 +---------------------------------------------------------------------------------+15:20:04 | Normal X:0.02 Y:-0.01 Z:-1.00                                                   |15:20:04 | Distance -0.82                                                                  |15:20:04 +---------------------------------------------------------------------------------+


altough when I calculate the corners for the frustum I get the 8 corners of the near and far plane and when I draw them they are located around the lightsource except that the near and far plane is the "same" plane the quads that visualize the near and far plane are different sizes but it looks like they are the same plane.

EDIT 4: I have fixed the "EDIT 3" problem so now the planes are different as they should be. And I have found out that if I use an Identity matrix as Modelview matrix the frustum planes works as they should.

EDIT 5: Now I have fixed the modelview matrix, and I got nice frustums But they are at the wrong positions not where the light actually are and some of them has the wrong direction too.

[Edited by - McZ on January 31, 2006 12:27:20 PM]

This topic is closed to new replies.

Advertisement