Thread: 4x4 Matrix for OpenGL: What else is required ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    4x4 Matrix for OpenGL: What else is required ?

    I am starting to get familiar with the basics of OpenGL and decided that it is a good time to make a matrix class.
    The following classes works as expected for transformations.
    What else do I need to add to make it sufficient and generic enough for most future projects I'd do ?
    (Keep in mind that the Matrix is in column major form.)
    Code:
    template<typename T>
    class vector3
    {
    public:
        T raw[3];
        vector3(T x,T y,T z){raw[0]=x;raw[1]=y;raw[2]=z;}
        vector3(T* t){raw[0]=t[0];raw[1]=t[1];raw[2]=t[2];}
        vector3(std::initializer_list<T> t)
        {
            auto it = t.begin();
            raw[0]=*(it++);
            raw[1]=*(it++);
            raw[2]=(*it++);
        };
        const T& operator[](int n){return raw[n];}
    
    };
    /*Reference: 
     * [0][4][08][12]
     * [1][5][09][13]
     * [2][6][10][14]
     * [3][7][11][15]
     */
    template<typename T>
    class matrix
    {
    public:
        T* raw; 
        matrix(T unity)//Allocates an identity matrix
        {
            raw = new T[16];//May change this to allocate from a Allocator maintained pool
            raw[0]=raw[5]=raw[10]=raw[15]=unity;
        }
        matrix(){};
        void translate(T x,T y,T z){raw[12]=x;raw[13]=y;raw[14]=z;}
    
        void transform(vector3<double> x,vector3<double> y, vector3<double> z)
        {
            //Only rotation and scaling
            raw[0]=x[0];raw[1]=x[1];raw [2]=x[2];
            raw[4]=y[0];raw[5]=y[1];raw [6]=y[2];
            raw[8]=z[0];raw[9]=z[1];raw[10]=z[2];
        }
    };
    Last edited by manasij7479; 02-06-2012 at 02:42 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Both need a lot of things/operators/addons, look up vector and matrix math.

    For the vector, you need add/subtract, inner/outer product, get/set lenght, unit, scalar multiplication ( and division? ), and maybe some addons that do, like, rotations for example.
    The matrix needs add/subtract, scalar/matrix-matrix/matrix-vector multiplication ( and division? ), load identity or something, and for addons you may need translation/rotation/scaling or even projection matrices.

    Also, I suggest that your matrix uses static memory, like "raw[16]"

    EDIT: Remember that an identity matrix, has all of its main diagonal elements set to one, and all the others set to zero!
    Last edited by GReaper; 02-07-2012 at 12:53 PM.
    Devoted my life to programming...

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by GReaper View Post
    For the vector, you need add/subtract, inner/outer product, get/set lenght, unit, scalar multiplication ( and division? ), and maybe some addons that do, like, rotations for example.

    The matrix needs add/subtract, scalar/matrix-matrix/matrix-vector multiplication ( and division? ), load identity or something, and for addons you may need translation/rotation/scaling or even projection matrices.
    I won't implement most of them myself, because OpenGL already has most of them as functions (except for stuff like addition... is addition needed at all ?) and I'm sure those will be faster than if I write native versions myself.

    Also, I suggest that your matrix uses static memory, like "raw[16]"
    I just used the default new here as proof of concept.
    I can change them to use allocators to get memory from a pool managed by myself...which would (afaik) be much faster than new but still not mess up the stack with a lot of matrices.
    EDIT: Remember that an identity matrix, has all of its main diagonal elements set to one, and all the others set to zero!
    Well, that is why I used new.. it default constructs the rest.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-02-2010, 01:26 PM
  2. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. My Matrix streaming code rip (openGL)
    By Jeremy G in forum Game Programming
    Replies: 10
    Last Post: 04-18-2003, 03:06 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM