Sign in to follow this  
Enerjak

Help with my view-matrix and perspective-matrix calculations.

Recommended Posts

As the title says, I need some help with calculating the view matrix and the perspective matrix. here are my matrices.

 

the view matrix: 

	Matrix4x4 Matrix4x4::makeViewMatrixLH(const Vector3 & eye, const Vector3 & at, const Vector3 & up)
	{	
		Matrix4x4 viewMatrix;
		Vector3 norm;
		norm.setX(at.getX() - eye.getX());
		norm.setY(at.getY() - eye.getY());
		norm.setZ(at.getZ() - eye.getZ());

		Vector3 zAxis = MathUtil::normalize(norm);
		Vector3 xAxis = MathUtil::normalize(MathUtil::crossProduct(up, zAxis));
		Vector3 yAxis = MathUtil::crossProduct(zAxis, xAxis);

		viewMatrix.setMatrix(xAxis.getX(), yAxis.getX(), zAxis.getX(), 0,
			xAxis.getY(), yAxis.getY(), zAxis.getZ(), 0,
			zAxis.getZ(), yAxis.getZ(), zAxis.getZ(), 0,
			-MathUtil::dotProduct(xAxis, eye), -MathUtil::dotProduct(yAxis, eye), -MathUtil::dotProduct(zAxis, eye), 1);

		return viewMatrix;
	}

projection-perspective matrix:
 

Matrix4x4 Matrix4x4::makePerspectiveProjectionMatrix(const Real near_plane, const Real far_plane, const Real fov_horiz, const Real fov_vert)
	{
		Matrix4x4 pers;
		Matrix4x4 actPers = pers.identity();
		Real w, h, q;
		w = (Real)1 / tanf(fov_horiz/2.0f);
		h = (Real)1 / tanf(fov_vert/2.0f);
		q = far_plane / (far_plane - near_plane); 
		
		actPers.setValue(0, 0, w);
		actPers.setValue(1, 1, h);
		actPers.setValue(2, 2,q);
		actPers.setValue(3, 2,-q*near_plane);
		actPers.setValue(2, 3, 1.0f);

		return actPers;
	}

I don't really understand the perspective-projection matrix as the D3D matrix function only needs 5 variables, the out-matrix, the field-of-view and the near and far values. This function I got from the Direct3D9 documentation doesn't seem account for the aspect ratio. Maybe it does and I'm not understanding? I wish there was a clear cut way for me to understand calculating my own matrices for the view and projection-perspective. Any advice you can give, please do.

 

Share this post


Link to post
Share on other sites


This function I got from the Direct3D9 documentation doesn't seem account for the aspect ratio.

 

It does account for aspect ratio, you have two FOV values, horizontal and vertical one, you would pick horizontal FOV as for example 90 degrees and vertical FOV as 90*(h/w), where h and w are height and weight of your screen. No idea why microsoft designed the projection matrix generating function like that though.

 

I am quite unclear as to what your other questions are.

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