Template class for working with 4x4 matrices. If you use matrix for transforming vector pay attention on the fact that the matrix is designed to be used for such a purpose in the way that vector should be multiplied to the matrix. If you try to multiply, for example rotate, matrix to a vector, you will get wrong transformation. The matrix give you access to the entire elements data by returning pointer to it via data()
method. This can be used, for example, for direct data uploading into your shader program without any temporary storages(in you shader program you, as well, should multiply vector on matrix and not vise versa) and without transposing.
You can find more information in comments below.
template<class TYPE>
class TMat4
{
public:
// Indices
static const size_t _11{ 0 }, _21{ 1 }, _31{ 2 }, _41{ 3 };
static const size_t _12{ 4 }, _22{ 5 }, _32{ 6 }, _42{ 7 };
static const size_t _13{ 8 }, _23{ 9 }, _33{ 10 }, _43{ 11 };
static const size_t _14{ 12 }, _24{ 13 }, _34{ 14 }, _44{ 15 };
TMat4();
TMat4(const TYPE diagonal); // Create diaginal matrix with all diagonal elements equal to the specified value
TMat4(const TYPE _11, const TYPE _22, const TYPE _33, const TYPE _44); // Create diaginal matrix with diagonal elements equal to the specified values
TMat4(const TVec4<TYPE> &row1, const TVec4<TYPE> &row2, const TVec4<TYPE> &row3, const TVec4<TYPE> &row4); // Create with specified rows
TMat4(const TMat4 &other);
TMat4(TMat4 &&other);
~TMat4();
const TYPE &operator[](const size_t index) const; // Return constant reference to the element with specified index
TYPE &operator[](const size_t index); // Return reference to the element with specified index
bool operator==(const TMat4 &other) const; // Strict comparison for equality
bool operator!=(const TMat4 &other) const; // Strict comparison for inequality
TMat4 &operator=(const TMat4 &other);
TMat4 &operator=(TMat4 &&other);
operator TMat3<TYPE>() const; // Convert to 3x3 matrix
TMat4 operator+(const TYPE value) const; // Add scalar value to all elements and return new matrix
TMat4 operator-(const TYPE value) const; // Subtract scalar value from all elements and return new matrix
TMat4 operator*(const TYPE value) const; // Multiply scalar value on all elements and return new matrix
TMat4 operator/(const TYPE value) const; // Divide all elements on scalar value and return new matrix
TMat4 &operator+=(const TYPE value); // Add scalar value to all elements of this matrix
TMat4 &operator-=(const TYPE value); // Subtract scalar value from all elements of this matrix
TMat4 &operator*=(const TYPE value); // Multiply scalar value to all elements of this matrix
TMat4 &operator/=(const TYPE value); // Divide all elements on scalar value of this matrix
TMat4 operator+(const TMat4 &other) const; // Add elements of another matrix to corresponding elements of this matrix and return new matrix
TMat4 operator-(const TMat4 &other) const; // Subtract elements of another matrix from corresponding elements of this matrix and return new matrix
TMat4 operator*(const TMat4 &other) const; // Multiply elements of another matrix on corresponding elements of this matrix and return new matrix
TMat4 operator/(const TMat4 &other) const; // Divide elements of this matrix on corresponding elements of another matrix and return new matrix
TMat4 operator^(const TMat4 &other) const; // Multiply matrices
TMat4 &operator+=(const TMat4 &other); // Add elements of another matrix to corresponding elements of this matrix
TMat4 &operator-=(const TMat4 &other); // Subtract elements of another matrix from corresponding elements of this matrix
TMat4 &operator*=(const TMat4 &other); // Multiply elements of another matrix on corresponding elements of this matrix
TMat4 &operator/=(const TMat4 &other); // Divide elements of this matrix on corresponding elements of another matrix
TMat4 &operator^=(const TMat4 &other); // Multiply matrices and store result in this one
const TYPE *data() const; // Return pointer to elements array
TYPE get(const size_t column, const size_t row) const; // Get specified element
void set(const size_t column, const size_t row, const TYPE value); // Set specified element
bool is_equal(const TMat4 &other, const TYPE tolerance) const; // Soft comparison for equality with another matrix
bool is_not_equal(const TMat4 &other, const TYPE tolerance) const; // Soft comparison for inequality with another matrix
TMat3<TYPE> minor(const size_t column, const size_t row) const; // Return minor
TYPE determinant() const; // Calculate determinant
void transpose(); // Transpose this matrix
TMat4 transposed() const; // Return matrix transposed to this one
void inverse(); // Inverse this matrix
TMat4 inversed() const; // Return matrix inversed to this one
void identity(); // Make this matrix identity
void diagonal(const TYPE value); // Make this matrix diagonal with all diagonal elements equal to specified value
void diagonal(const TYPE _11, const TYPE _22, const TYPE _33, const TYPE _44); // Make this matrix diagonal with diagonal elements equal to specified values
void translate(const TYPE x, const TYPE y, const TYPE z); // Make translation matrix
void scale(const TYPE x, const TYPE y, const TYPE z); // Make scaling matrix
void rotate_x(const TYPE angle); // Make rotation around X-axis matrix
void rotate_y(const TYPE angle); // Make rotation around Y-axis matrix
void rotate_z(const TYPE angle); // Make rotation around Z-axis matrix
void rotate(const TYPE angle_z, const TYPE angle_y, const TYPE angle_x); // Make rotation matrix with spaceified rotation angles around axes
void rotate(const TYPE angle, const TVec3<TYPE> &v); // Make rotation matrix around specified vector/axes
void lookat(const TVec3<TYPE> &camera, const TVec3<TYPE> &target, const TVec3<TYPE> &up); // Make "look at" projection matrix
void frustum(const TYPE l, const TYPE t, const TYPE r, const TYPE b, const TYPE n, const TYPE f); // Make "frustum" projection matrix
void perspective(const TYPE fov, const TYPE aspect, const TYPE n, const TYPE f); // Make "perspective" projection matrix
void ortho(const TYPE l, const TYPE t, const TYPE r, const TYPE b, const TYPE n, const TYPE f); // Make orthogonal projection matrix
};
Additionally following operators exists and can be used for arithmetic operations with scalar value and 4x4 matrices and 4D vectors. So as a final result you may use the 4x4 matrix template with scalar/4D vector values in both directions(scalar * matrix
and matrix * scalar
). Pay attention on the -, /, and ^ operators. The result of operation like scalar - matrix
will be different from the result of operation like matrix - scalar
.
template<class TYPE> TMat4<TYPE> operator+(const TYPE value, const TMat4<TYPE> &m);
template<class TYPE> TMat4<TYPE> operator-(const TYPE value, const TMat4<TYPE> &m);
template<class TYPE> TMat4<TYPE> operator*(const TYPE value, const TMat4<TYPE> &m);
template<class TYPE> TMat4<TYPE> operator/(const TYPE value, const TMat4<TYPE> &m);
template<class TYPE> TVec4<TYPE> operator^(const TVec4<TYPE> &v, const TMat4<TYPE> &m);
template<class TYPE> TVec4<TYPE> &operator^=(TVec4<TYPE> &v, const TMat4<TYPE> &m);
template<class TYPE> TVec4<TYPE> operator^(const TMat4<TYPE> &m, const TVec4<TYPE> &v);
Namespace: | nitisa::math |
Include: | Nitisa/Modules/Math/Matrix.h |