# What is wrong with my ortho matrix math?

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

## 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 on other sites
You probably make each column into what OpenGL believes to be each row instead. According to http://www.opengl.org/resources/faq/technical/transformations.htm the translation components occupy the 13th 14th and 15th element in the matrix.

##### Share on other sites
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 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);

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015442
×