Jump to content
  • Advertisement
Sign in to follow this  
Toolmaker

What is wrong with my ortho matrix math?

This topic is 3207 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently attempting to implement some vertex shading effects for our business application to replace the old immediate mode code. However, previously all that was used were the standard glOrtho mode calls and the like to set the matrices. Since I need the projection and view matrix for my shader, I thought I might aswell calculate them myself. However: when I calculate my matrix and then compare the results with the matrix that glOrtho returns, I get differences in it. This is the code I use to calculate the matrix:
TransformationMatrix TransformationMatrix::CalculateOrtho(float left, float right, float top, float bottom, float nearVal, float farVal)
{
	TransformationMatrix ident, orthoMatrix;

	float tx = -((right + left) / (right - left));
	float ty = -((top + bottom) / (top - bottom));
	float tz = -((farVal + nearVal) / (farVal - nearVal));

	orthoMatrix.m_buffer[0][0] = 2 / (right - left);
	orthoMatrix.m_buffer[0][1] = 0.0f;
	orthoMatrix.m_buffer[0][2] = 0.0f;
	orthoMatrix.m_buffer[0][3] = tx;

	orthoMatrix.m_buffer[1][0] = 0.0f;
	orthoMatrix.m_buffer[1][1] = 2 / (top - bottom);
	orthoMatrix.m_buffer[1][2] = 0.0f;
	orthoMatrix.m_buffer[1][3] = ty;

	orthoMatrix.m_buffer[2][0] = 0.0f;
	orthoMatrix.m_buffer[2][1] = 0.0f;
	orthoMatrix.m_buffer[2][2] = -2 / (farVal - nearVal);
	orthoMatrix.m_buffer[2][3] = tz;

	orthoMatrix.m_buffer[3][0] = 0.0f;
	orthoMatrix.m_buffer[3][1] = 0.0f;
	orthoMatrix.m_buffer[3][2] = 0.0f;
	orthoMatrix.m_buffer[3][3] = 1.0f;

	return ident * orthoMatrix;
}

What am I doing wrong? Because the matrix that I get is completely off. Toolmaker

Share this post


Link to post
Share on other sites
Advertisement
When I transpose it, the results are almost the same as what OpenGL returns. It shows something on screen now, but still not the correct stuff...


Mine GL's matrix
[0] 0.0018315018 float [0] 0.0018315018 float
[1] 0.00000000 float [1] 0.00000000 float
[2] 0.00000000 float [2] 0.00000000 float
[3] 0.00000000 float [3] 0.00000000 float
[4] 0.00000000 float [4] 0.00000000 float
[5] -0.0031695722 float [5] 0.0031746032 float
[6] 0.00000000 float [6] 0.00000000 float
[7] 0.00000000 float [7] 0.00000000 float
[8] 0.00000000 float [8] 0.00000000 float
[9] 0.00000000 float [9] 0.00000000 float
[10] -2.0000000 float [10] -2.0000000 float
[11] 0.00000000 float [11] 0.00000000 float
[12] -0.00000000 float [12] -0.00000000 float
[13] 0.00000000 float [13] -0.00000000 float
[14] -1.0000000 float [14] -1.0000000 float
[15] 1.0000000 float [15] 1.0000000 float


Toolmaker

Share this post


Link to post
Share on other sites
TransformationMatrix TransformationMatrix::CalculateOrtho(float left, float right, float top, float bottom, float nearVal, float farVal)

void glOrtho(
GLdouble left,
GLdouble right,
GLdouble bottom,
GLdouble top
,
GLdouble nearVal,
GLdouble farVal);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!