## Class Definition

class GLMatrix { public: GLMatrix (); GLMatrix (const float *); const float* getMatrix () const; GLMatrix& operator*= (const GLMatrix& rhs); float& operator[] (unsigned int pos); void reset (); void toScreenSpace (const CompOutput *output, float z); void rotate (const float angle, const float x, const float y, const float z); void rotate (const float angle, const GLVector& vector); void scale (const float x, const float y, const float z); void scale (const GLVector& vector); void translate (const float x, const float y, const float z); void translate (const GLVector& vector); };

## Initializing the matrix

A `GLMatrix` is constructed by default to be an identity matrix, eg:

1 |
0 |
0 |
0 |

0 |
1 |
0 |
0 |

0 |
0 |
1 |
0 |

0 |
0 |
0 |
1 |

However, if there is an existing 4x4 matrix represented by some 16-size const float *, then you can also construct the GLMatrix from that, and it will copy those values into it's own internal matrix.

## Operating on the Matrix

There are three main matrix transformation operations which can be used to transform represent a co-ordinate space transformation, rotation, scale and translation.

### Rotation

To rotate the matrix, you can use the `rotate ()` member function, which rotates a matrix by an angle `ang` around some vector `x, y and z`. Typically, you would want to apply the x, y and z rotation individually, so you might call the function like so.

X Rotation |
::rotate (ang, 1.0f, 0.0f, 0.0f); |

Y Rotation |
::rotate (ang, 0.0f, 1.0f, 0.0f); |

Z Rotation |
::rotate (ang, 0.0f, 0.0f, 1.0f); |

However, if you wish to rotate the matrix around some arbitrary vector, then you can provide this vector and the function will automatically normalize it and rotate around the vector.

### Scale

To scale a matrix, you can use the `scale (x, y, z)` member function, which scales a matrix on and x, y and z factor. The default scale value is 1.0, so if you don't wish to scale on an axis, you need to scale by this much.

### Translation

To translate all points on a matrix, you can use the `translate (x, y, z)` function, to translate by x, y, z distance in co-ordinate space.

## Getting values from the matrix

If you need to operate directly on the matrix, you can get a const float[16] by calling the ::getMatrix () member function, or by using the `operator[]` array-access overload to access individual members in the matrix.

Since the matrix is 4x4, each row represents one factor of 4, so matrix[7] will get you matrix[1][4]

Development/zero-nine/OpenGLClasses/GLMatrix (last edited 2010-08-23 09:41:59 by 203-173-33-106)