Skip to main content
GameDev.net gamedev.net
🔒 Locked

Extracting Matrix Parameters

Started by DrGUI Apr 1, 2005 at 8:59 AM 1 replies 950+ views
Original Post
DrGUI
DrGUI
I want to be able to extract the left (l), right (r), top (t), bottom (b), znear (zn) and zfar (zf) parameters out of a projection matrix. I borrowed this from the DirectX docs for your convenience: 2*zn/(r-l) 0 0 0 0 2*zn/(t-b) 0 0 (l+r)/(l-r) (t+b)/(b-t) zf/(zf-zn) 1 0 0 zn*zf/(zn-zf) 0 EDIT: sorry about GameDev taking out my tabs, then collapsing my spaces I imagine doing this would entail solving a few simultaneous equations but it looks daunting to me and I am revising for my GCSEs (progress on my game has stopped completely :-( ). Someone must have done this before, but my Google search with 8 keywords kept on giving me thousands of irrelevant results. I will be grateful for your help and many ratings will come your way...
Monder
Monder
There's a PDF on extracting the planes of the view frustum from the projection matrix here. You should be able to work out what you need from the planes.
DrGUI
DrGUI
Thanks for your reply! I've already got the code to extract planes, and I worked out from that how to find the corners (to draw my view frustum). Here's the code to help solve this problem:
/// <summary>/// Extracts planes from an arbitrary matrix.</summary>/// <param name="matrixIn">The matrix from which to extract the planes.</param>/// <param name="normalizePlanes">/// Whether to normalize the planes. Non-normalized planes are only suitable for half-space testing./// No distance values calculated using these planes will be accurate other than to show whether positions lie in the positive or/// negative half-space of the plane.</param>/// <param name="outPlanes">The array of 6 planes to receive the extracted planes.</param>/// <exception cref="System.ArgumentNullException">/// outPlanes is a null reference (Nothing in Visual Basic).</exception>/// <exception cref="System.ArgumentException">/// The length of outPlanes is not 6.</exception>public static void ExtractPlanes(Matrix matrixIn, bool normalizePlanes, ref Plane[] outPlanes){	if (outPlanes == null) throw new System.ArgumentNullException("outPlanes");	if (outPlanes.Length != 6) throw new System.ArgumentException("The length of outPlanes is not 6.");	//Left	outPlanes[(int)FrustumPlane.Left].A = matrixIn.M14 + matrixIn.M11;	outPlanes[(int)FrustumPlane.Left].B = matrixIn.M24 + matrixIn.M21;	outPlanes[(int)FrustumPlane.Left].C = matrixIn.M34 + matrixIn.M31;	outPlanes[(int)FrustumPlane.Left].D = matrixIn.M44 + matrixIn.M41;	//Right	outPlanes[(int)FrustumPlane.Right].A = matrixIn.M14 - matrixIn.M11;	outPlanes[(int)FrustumPlane.Right].B = matrixIn.M24 - matrixIn.M21;	outPlanes[(int)FrustumPlane.Right].C = matrixIn.M34 - matrixIn.M31;	outPlanes[(int)FrustumPlane.Right].D = matrixIn.M44 - matrixIn.M41;	//Top	outPlanes[(int)FrustumPlane.Top].A = matrixIn.M14 - matrixIn.M12;	outPlanes[(int)FrustumPlane.Top].B = matrixIn.M24 - matrixIn.M22;	outPlanes[(int)FrustumPlane.Top].C = matrixIn.M34 - matrixIn.M32;	outPlanes[(int)FrustumPlane.Top].D = matrixIn.M44 - matrixIn.M42;	//Bottom	outPlanes[(int)FrustumPlane.Bottom].A = matrixIn.M14 + matrixIn.M12;	outPlanes[(int)FrustumPlane.Bottom].B = matrixIn.M24 + matrixIn.M22;	outPlanes[(int)FrustumPlane.Bottom].C = matrixIn.M34 + matrixIn.M32;	outPlanes[(int)FrustumPlane.Bottom].D = matrixIn.M44 + matrixIn.M42;	//Near	outPlanes[(int)FrustumPlane.Near].A = matrixIn.M13;	outPlanes[(int)FrustumPlane.Near].B = matrixIn.M23;	outPlanes[(int)FrustumPlane.Near].C = matrixIn.M33;	outPlanes[(int)FrustumPlane.Near].D = matrixIn.M43;	//Far	outPlanes[(int)FrustumPlane.Far].A = matrixIn.M14 - matrixIn.M13;	outPlanes[(int)FrustumPlane.Far].B = matrixIn.M24 - matrixIn.M23;	outPlanes[(int)FrustumPlane.Far].C = matrixIn.M34 - matrixIn.M33;	outPlanes[(int)FrustumPlane.Far].D = matrixIn.M44 - matrixIn.M43;	//Non-normalized planes are only suitable for half-space testing.	//No distance values calculated using these planes will be accurate	//other than to show whether positions lie in the positive or negative	//half-space of the plane.	if (normalizePlanes) {		outPlanes[(int)FrustumPlane.Left].Normalize();		outPlanes[(int)FrustumPlane.Right].Normalize();		outPlanes[(int)FrustumPlane.Top].Normalize();		outPlanes[(int)FrustumPlane.Bottom].Normalize();		outPlanes[(int)FrustumPlane.Near].Normalize();		outPlanes[(int)FrustumPlane.Far].Normalize();	}}/// <summary>/// Extracts points from an arbitrary matrix.</summary>/// <param name="matrixIn">The matrix from which to extract the points.</param>/// <param name="outPoints">The array of 8 vectors to receive the extracted points.</param>/// <exception cref="System.ArgumentNullException">/// outPoints is a null reference (Nothing in Visual Basic).</exception>/// <exception cref="System.ArgumentException">/// The length of outPoints is not 8.</exception>public static void ExtractPoints(Matrix matrixIn, ref Vector3[] outPoints){	if (outPoints == null) throw new System.ArgumentNullException("outPoints");	if (outPoints.Length != 8) throw new System.ArgumentException("The length of outPoints is not 8.");	//A projection matrix concat is likely to be used, so everything needs to be divided by w	float w;	//Near bottom left	w = 1 / (-matrixIn.M14 - matrixIn.M24 + matrixIn.M44);	outPoints[(int)FrustumPoint.NearBottomLeft].X = (-matrixIn.M11 - matrixIn.M21 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.NearBottomLeft].Y = (-matrixIn.M12 - matrixIn.M22 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.NearBottomLeft].Z = (-matrixIn.M13 - matrixIn.M23 + matrixIn.M43) * w;	//Near bottom right	w = 1 / (matrixIn.M14 - matrixIn.M24 + matrixIn.M44);	outPoints[(int)FrustumPoint.NearBottomRight].X = (matrixIn.M11 - matrixIn.M21 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.NearBottomRight].Y = (matrixIn.M12 - matrixIn.M22 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.NearBottomRight].Z = (matrixIn.M13 - matrixIn.M23 + matrixIn.M43) * w;	//Near top left	w = 1 / (-matrixIn.M14 + matrixIn.M24 + matrixIn.M44);	outPoints[(int)FrustumPoint.NearTopLeft].X = (-matrixIn.M11 + matrixIn.M21 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.NearTopLeft].Y = (-matrixIn.M12 + matrixIn.M22 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.NearTopLeft].Z = (-matrixIn.M13 + matrixIn.M23 + matrixIn.M43) * w;	//Near top right	w= 1 / (matrixIn.M14 + matrixIn.M24 + matrixIn.M44);	outPoints[(int)FrustumPoint.NearTopRight].X = (matrixIn.M11 + matrixIn.M21 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.NearTopRight].Y = (matrixIn.M12 + matrixIn.M22 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.NearTopRight].Z = (matrixIn.M13 + matrixIn.M23 + matrixIn.M43) * w;	//Far bottom left	w = 1 / (-matrixIn.M14 - matrixIn.M24 + matrixIn.M34 + matrixIn.M44);	outPoints[(int)FrustumPoint.FarBottomLeft].X = (-matrixIn.M11 - matrixIn.M21 + matrixIn.M31 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.FarBottomLeft].Y = (-matrixIn.M12 - matrixIn.M22 + matrixIn.M32 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.FarBottomLeft].Z = (-matrixIn.M13 - matrixIn.M23 + matrixIn.M33 + matrixIn.M43) * w;	//Far bottom right	w = 1 / (matrixIn.M14 - matrixIn.M24 + matrixIn.M34 + matrixIn.M44);	outPoints[(int)FrustumPoint.FarBottomRight].X = (matrixIn.M11 - matrixIn.M21 + matrixIn.M31 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.FarBottomRight].Y = (matrixIn.M12 - matrixIn.M22 + matrixIn.M32 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.FarBottomRight].Z = (matrixIn.M13 - matrixIn.M23 + matrixIn.M33 + matrixIn.M43) * w;	//Far top left	w = 1 / (-matrixIn.M14 + matrixIn.M24 + matrixIn.M34 + matrixIn.M44);	outPoints[(int)FrustumPoint.FarTopLeft].X = (-matrixIn.M11 + matrixIn.M21 + matrixIn.M31 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.FarTopLeft].Y = (-matrixIn.M12 + matrixIn.M22 + matrixIn.M32 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.FarTopLeft].Z = (-matrixIn.M13 + matrixIn.M23 + matrixIn.M33 + matrixIn.M43) * w;	//Far top right	w = 1 / (matrixIn.M14 + matrixIn.M24 + matrixIn.M34 + matrixIn.M44);	outPoints[(int)FrustumPoint.FarTopRight].X = (matrixIn.M11 + matrixIn.M21 + matrixIn.M31 + matrixIn.M41) * w;	outPoints[(int)FrustumPoint.FarTopRight].Y = (matrixIn.M12 + matrixIn.M22 + matrixIn.M32 + matrixIn.M42) * w;	outPoints[(int)FrustumPoint.FarTopRight].Z = (matrixIn.M13 + matrixIn.M23 + matrixIn.M33 + matrixIn.M43) * w;}


[Edited by - DrGUI on April 3, 2005 12:47:30 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.