Can anyone help me with perspective projection?

Hey all…

I was wondering if anyone could tell me where I am going wrong when implementing a perspective matrix.
The math for creating the matrix appears to be correct from what I can tell, however the end result is a stretched out distorted shape that takes up the entire screen, which is a far cry from the basic cube i had rendering before.
This is purely an educational exercise, hence why I have laid out the matrix deceleration this way…


static Matrix4 CreatePerspectiveMatrix(float fov, float aspect, float zNear, float zFar) {
			Matrix4 r;

			float tanThetaOver2 =(float)tanf(ToRadians(fov / 2));
			float zRange = zNear - zFar;
			float zTotal = zNear + zFar;

			r.m[0][0] = 1.0f / (tanThetaOver2);			r.m[0][1] = 0;						r.m[0][2] = 0;					r.m[0][3] = 0;
			r.m[1][0] = 0;								r.m[1][1] = aspect / tanThetaOver2;	r.m[1][2] = 0;					r.m[1][3] = 0;
			r.m[2][0] = 0;								r.m[2][1] = 0;						r.m[2][2] = zTotal/zRange;	    r.m[2][3] = 2 * zFar * zNear / zRange;
			r.m[3][0] = 0;								r.m[3][1] = 0;						r.m[3][2] = -1;					r.m[3][3] = 0;

			return r;


	void Game::Draw()
		Matrix4 mtxModel = m_transform.GetMatrix();
		Matrix4 mtxProjection = Matrix4::CreatePerspectiveMatrix(60.0f, m_display->GetAspectRatio(), 10.0f, 1000);
		Matrix4 mtxModelProjection = mtxProjection * mtxModel;
		m_shader.SetSharderMatrixUniform("transform", mtxModelProjection);
		//assert(glGetError() == GL_NO_ERROR);

End result

Thanks for reading,

Any help available would be greatly appreciated .

Your matrix looks fine, but it’s in row-major order. OpenGL defaults to column-major order, so it needs to be either explicitly declared as being in row-major order, or transposed.