Sign in to follow this  

Correct 4x4 orthogonal matrix implementation.

This topic is 1517 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

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

This topic is 1517 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this