• Create Account

Correct 4x4 orthogonal matrix implementation.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

#1Edvinas Kilbauskas  Members

1163
Like
0Likes
Like

Posted 21 November 2013 - 09:50 AM

Hi, could anyone send me a link to a picture of CORRECT orthogonal 4x4 matrix implementation? I can't seem to find it on the google. Not sure if I did it right myself.

This is the code I got for the matrix:

#include "Matrix4.h"

Matrix4::Matrix4()
{
m_data = new float[16];
}

void Matrix4::set(const float matrix[16])
{
for(int i = 0; i < 16; i++){
m_data[i] = matrix[i];
}
}

{
m_data[0] = 1.0f;
m_data[1] = 0.0f;
m_data[2] = 0.0f;
m_data[3] = 0.0f;

m_data[4] = 0.0f;
m_data[5] = 1.0f;
m_data[6] = 0.0f;
m_data[7] = 0.0f;

m_data[8] = 0.0f;
m_data[9] = 0.0f;
m_data[10] = 1.0f;
m_data[11] = 0.0f;

m_data[12] = 0.0f;
m_data[13] = 0.0f;
m_data[14] = 0.0f;
m_data[15] = 1.0f;
}

void Matrix4::setFrustum(float left, float right, float bottom, float top, float near, float far)
{
// TODO implement this
}

void Matrix4::setOrtho(float left, float right, float bottom, float top, float near, float far)
{
m_data[0] = 2.0f/(right-left);
m_data[1] = 0.0f;
m_data[2] = 0.0f;
m_data[3] = -((right+left)/(right-left));

m_data[4] = 0.0f;
m_data[5] = 2.0f/(top-bottom);
m_data[6] = 0.0f;
m_data[7] = -((top+bottom)/(top-bottom));

m_data[8] = 0.0f;
m_data[9] = 0.0f;
m_data[10] = 2.0f/(far-near);
m_data[11] = -((far+near)/(far-near));

m_data[12] = 0.0f;
m_data[13] = 0.0f;
m_data[14] = 0.0f;
m_data[15] = 1.0f;
}

Matrix4 Matrix4::operator*(const Matrix4 & matrix)
{
Matrix4 outMatrix;

for(int y = 0; y < 4; y++){

for(int x = 0; x < 4; x++){
float outVal = 0;

for(int i = 0; i < 4; i++){
outVal += getData(i,y)*matrix.getData(x,i);
}
outMatrix.setData(x,y,outVal);
}
}

return outMatrix;
}

void Matrix4::operator=(const Matrix4 & matrix)
{
set(matrix.toArray());
}

std::ostream& operator<<(std::ostream& os, const Matrix4& matrix)
{
for(int y = 0; y < 4; y++){
for(int x = 0; x < 4; x++){
os << matrix.getData(x,y) << "\t";
}
os << std::endl;
}
}

float * Matrix4::toArray() const
{
return m_data;
}

float Matrix4::getData(int col, int row) const
{
return m_data[(row*4)+col];
}

void Matrix4::setData(int col, int row, float data)
{
m_data[(row*4)+col] = data;
}

Matrix4::~Matrix4()
{
delete [] m_data;
}



Does this look good? The multiplication part seems to work fine. But I'm not sure about setOrtho method.

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”― Nigel Marsh

#2KulSeran  Members

3105
Like
0Likes
Like

Posted 21 November 2013 - 10:01 AM

Check out GLM, it's designed for OpenGL and to have have the same semantics as OpenGL shaders.

#3L. Spiro  Members

24837
Like
3Likes
Like

Posted 21 November 2013 - 04:41 PM

I’m not in a position right now where I can check the memory layout of desktop OpenGL, but “m_data[10] = 2.0f/(far-near);” should be “m_data[10] = -2.0f/(far-near);”.

http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/glOrtho.htm

That aside, why are you allocating the matrix array?  It’s extreme overhead that will waste not only precious cycles but also memory.

Make it an array member of Matrix4.

L. Spiro

#4mhagain  Members

12438
Like
2Likes
Like

Posted 21 November 2013 - 05:20 PM

If you want something that you can just use as a replacement for glOrtho, then why not look at the matrix that glOrtho generates and copy from that?

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#5Edvinas Kilbauskas  Members

1163
Like
0Likes
Like

Posted 21 November 2013 - 11:20 PM

I’m not in a position right now where I can check the memory layout of desktop OpenGL, but “m_data[10] = 2.0f/(far-near);” should be “m_data[10] = -2.0f/(far-near);”.

http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.opengl/doc/openglrf/glOrtho.htm

That aside, why are you allocating the matrix array?  It’s extreme overhead that will waste not only precious cycles but also memory.

Make it an array member of Matrix4.

L. Spiro

Thanks, this is exactly what I needed

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”― Nigel Marsh

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.