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

• 02-06-2012
manasij7479
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];     } };```
• 02-07-2012
GReaper
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!
• 02-07-2012
manasij7479
Quote:

Originally Posted by GReaper
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.
Quote:

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.
Quote:

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.