Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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.

  • You cannot reply to this topic
4 replies to this topic

#1 _greyfox()   Members   -  Reputation: 1134

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];
	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

Sponsor:

#2 KulSeran   Members   -  Reputation: 2581

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.



#3 L. Spiro   Crossbones+   -  Reputation: 14198

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


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#4 mhagain   Crossbones+   -  Reputation: 8275

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?

 

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


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.


#5 _greyfox()   Members   -  Reputation: 1134

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.



PARTNERS