Archived

This topic is now archived and is closed to further replies.

Zaei

Perspective Projection Matrix

Recommended Posts

I am trying to recreate several of te D3DX matrix functions. My Matrix looks like this:
  
struct MATRIX
{
	float _11;
	float _12;
	float _13;
	float _14;

	float _21;
	float _22;
	float _23;
	float _24;

	float _31;
	float _32;
	float _33;
	float _34;

	float _41;
	float _42;
	float _43;
	float _44;
};
  
At the moment, I am trying to get my Perspective projection working, using the D3D Documentation. Here is what I came up with:
  
VOID MatrixMatrixPerspectiveFovLH(MATRIX* r, float FOV, float aspect, float fNear, float fFar)
{
	float h = (float)cos(FOV/2.0f) / (float)sin(FOV/2.0f);
	float w = h / aspect;

	MatrixZero(&r);

	r->_11 = (2.0f * fNear) / w;
	r->_22 = (2.0f * fNear) / h;
	r->_33 = fFar / (fFar - fNear);
	r->_43 = (fNear * fFar) / (fNear - fFar);
	r->_34 = 1.0f;

	return;
}
  
This is pretty much a copy of what is in the D3D8.1 docs for this function. Sure enough, It works, but not correctly. Instead of a large spinning triangle, like I get using the D3DX function with the same parameters, I get a really tiny spinning triangle. For reference, I am using the function D3DXMatrixLookAtLH() to create my view matrix, and the above function for my projection matrix. What am I doing wrong? Z.

Share this post


Link to post
Share on other sites
Try:

  
float w,h,Q;

w = aspect * ( cosf(fov/2.0f)/sinf(fov/2.0f) );
h = cosf(fov/2.0f)/sinf(fov/2.0f);
Q = far_plane / ( far_plane - near_plane );

m_mat._11 = w;
m_mat._22 = h;
m_mat._33 = Q;
m_mat._34 = 1.0f;
m_mat._43 = -Q*near_plane;


--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites