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…
Creation
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;
}
Implementation
void Game::Draw()
{
m_display->SwapBuffer();
m_renderer.Draw();
m_shader.Bind();
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);
m_renderer.DrawMesh(m_mesh);
m_shader.UnBind();
//assert(glGetError() == GL_NO_ERROR);
}
End result
[ATTACH=CONFIG]1455[/ATTACH]
Thanks for reading,
Any help available would be greatly appreciated .