Correct 4x4 orthogonal matrix implementation.

Started by
3 comments, last by Pufixas 10 years, 4 months ago

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];
	loadIdentity();
}

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

void Matrix4::loadIdentity()
{
    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
Advertisement

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

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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?

I second L. Spiro's advice about not allocating the array. Make it a float[16] member instead.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

This topic is closed to new replies.

Advertisement