MinGfx Toolkit  1.0
A minimal library for writing cross-platform (Windows, OSX, linux) graphics programs.
Public Member Functions | Static Public Member Functions | List of all members
mingfx::Matrix4 Class Reference

Detailed Description

A 4x4 transformation matrix stored internally as an array of floats in column-major order so as to be compatible with OpenGL.

Examples:

// constructing various matrices:
Matrix4 T = Matrix4::Translation(Vector3(1,0,0));
Matrix4 S = Matrix4::Scale(Vector3(2,2,2));
Matrix4 R = Matrix4::RotateX(GfxMath::toRadians(45.0));
// compose matrices together by multiplication
Matrix4 M = T * R * S;
Matrix4 Minv = M.Inverse();
// transforming points, vectors, etc.
Point3 p1(1,1,1);
Point3 p2 = M * p1;
Vector3 v1(1,1,1);
Vector3 v2 = M * v1;
Ray r1(p1, v1);
Ray r2 = M * r1;
static Matrix4 Scale(const Vector3 &v)
Returns the scale matrix described by the vector.
Matrix4()
The default constructor creates an identity matrix:
static Matrix4 Translation(const Vector3 &v)
Returns the translation matrix described by the vector.

Definition at line 50 of file matrix4.h.

#include <matrix4.h>

Public Member Functions

 Matrix4 ()
 The default constructor creates an identity matrix: More...
 
 Matrix4 (const float *a)
 Constructs a matrix given from an array of 16 floats in OpenGL matrix format (i.e., column major). More...
 
 Matrix4 (const std::vector< float > &a)
 Constructs a matrix given from a vector of 16 floats in OpenGL matrix format (i.e., column major). More...
 
 Matrix4 (const Matrix4 &m2)
 Copy constructor. More...
 
virtual ~Matrix4 ()
 Destructor. More...
 
bool operator== (const Matrix4 &m2) const
 Check for "equality", taking floating point imprecision into account. More...
 
bool operator!= (const Matrix4 &m2) const
 Check for "inequality", taking floating point imprecision into account. More...
 
Matrix4operator= (const Matrix4 &m2)
 Matrix assignment operator. More...
 
const float * value_ptr () const
 Returns a pointer to the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order. More...
 
float operator[] (const int i) const
 Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order. More...
 
float & operator[] (const int i)
 Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order. More...
 
float operator() (const int row, const int col) const
 Access an individual element of the array using the syntax: Matrix4 mat; float row1col2 = mat(1,2);. More...
 
float & operator() (const int row, const int col)
 Access an individual element of the array using the syntax: Matrix4 mat; mat(1,2) = 1.0;. More...
 
Vector3 ColumnToVector3 (int c) const
 Returns the c-th column of the matrix as a Vector type, e.g.,: Vector3 xAxis = mat.getColumnAsVector3(0); Vector3 yAxis = mat.getColumnAsVector3(1); Vector3 zAxis = mat.getColumnAsVector3(2);. More...
 
Point3 ColumnToPoint3 (int c) const
 Returns the c-th column of the matrix as a Vector type, e.g.,: Point3 pos = mat.getColumnAsPoint3(3);. More...
 
std::vector< float > ToVector () const
 
Matrix4 Inverse () const
 Returns the inverse of the 4x4 matrix if it is nonsingular. If it is singular, then returns the identity matrix. More...
 
Matrix4 Orthonormal () const
 Returns an orthonormal version of the matrix, i.e., guarantees that the rotational component of the matrix is built from column vectors that are all unit vectors and orthogonal to each other. More...
 
Matrix4 Transpose () const
 Returns the transpose of the matrix. More...
 
float SubDeterminant (int exclude_row, int exclude_col) const
 Returns the determinant of the 3x3 matrix formed by excluding the specified row and column from the 4x4 matrix. More...
 
Matrix4 Cofactor () const
 Returns the cofactor matrix. More...
 
float Determinant () const
 Returns the determinant of the 4x4 matrix. More...
 

Static Public Member Functions

static Matrix4 FromRowMajorElements (const float r1c1, const float r1c2, const float r1c3, const float r1c4, const float r2c1, const float r2c2, const float r2c3, const float r2c4, const float r3c1, const float r3c2, const float r3c3, const float r3c4, const float r4c1, const float r4c2, const float r4c3, const float r4c4)
 Returns a matrix constructed from individual elements passed in row major order so that the matrix looks "correct" on the screen as you write this constructor on 4 lines of code as below. More...
 
static Matrix4 Scale (const Vector3 &v)
 Returns the scale matrix described by the vector. More...
 
static Matrix4 Translation (const Vector3 &v)
 Returns the translation matrix described by the vector. More...
 
static Matrix4 RotationX (const float radians)
 Returns the rotation matrix about the x axis by the specified angle. More...
 
static Matrix4 RotationY (const float radians)
 Returns the rotation matrix about the y axis by the specified angle. More...
 
static Matrix4 RotationZ (const float radians)
 Returns the rotation matrix about the z axis by the specified angle. More...
 
static Matrix4 Rotation (const Point3 &p, const Vector3 &v, const float a)
 Returns the rotation matrix around the vector v placed at point p, rotate by angle a. More...
 
static Matrix4 Align (const Point3 &a_p, const Vector3 &a_v1, const Vector3 &a_v2, const Point3 &b_p, const Vector3 &b_v1, const Vector3 &b_v2)
 Creates a transformation matrix that maps a coordinate space, a, defined one point, a_p, and two vectors, a_v1 and a_v2, to a new coordinate space, b, also defined by one point, b_p, and two vectors, b_v1 and b_v2. The transformation will thus include both some rotation and some translation. Pseudocode example of aligning a billboard defined in the XY plane with a normal in the +Z direction and that rotates around the Y axis with a camera: More...
 
static Matrix4 LookAt (Point3 eye, Point3 target, Vector3 up)
 Returns a view matrix that centers the camera at the 'eye' position and orients it to look at the desired 'target' point with the top of the screen pointed as closely as possible in the direction of the 'up' vector. More...
 
static Matrix4 Perspective (float fov_y_in_degrees, float aspect_ratio, float near_plane_dist, float far_plane_dist)
 Returns a perspective projection matrix equivalent to the one gluPerspective creates. More...
 
static Matrix4 Frustum (float left, float right, float bottom, float top, float near_plane_dist, float far_plane_dist)
 Returns a projection matrix equivalent the one glFrustum creates. More...
 

Constructor & Destructor Documentation

◆ Matrix4() [1/4]

mingfx::Matrix4::Matrix4 ( )

The default constructor creates an identity matrix:

◆ Matrix4() [2/4]

mingfx::Matrix4::Matrix4 ( const float *  a)

Constructs a matrix given from an array of 16 floats in OpenGL matrix format (i.e., column major).

◆ Matrix4() [3/4]

mingfx::Matrix4::Matrix4 ( const std::vector< float > &  a)

Constructs a matrix given from a vector of 16 floats in OpenGL matrix format (i.e., column major).

◆ Matrix4() [4/4]

mingfx::Matrix4::Matrix4 ( const Matrix4 m2)

Copy constructor.

◆ ~Matrix4()

virtual mingfx::Matrix4::~Matrix4 ( )
virtual

Destructor.

Member Function Documentation

◆ Align()

static Matrix4 mingfx::Matrix4::Align ( const Point3 a_p,
const Vector3 a_v1,
const Vector3 a_v2,
const Point3 b_p,
const Vector3 b_v1,
const Vector3 b_v2 
)
static

Creates a transformation matrix that maps a coordinate space, a, defined one point, a_p, and two vectors, a_v1 and a_v2, to a new coordinate space, b, also defined by one point, b_p, and two vectors, b_v1 and b_v2. The transformation will thus include both some rotation and some translation. Pseudocode example of aligning a billboard defined in the XY plane with a normal in the +Z direction and that rotates around the Y axis with a camera:

// define a coordiante space for a canonical billboard geometry defined
// right at the origin.
Point3 a_p = Point3(0,0,0); // billboard's initial base position
Vector3 a_v1 = Vector3(0,1,0); // billboard's initial up direction
Vector3 a_v2 = Vector3(0,0,1); // billboard's initial normal direction
// define a coordinate space for where we want this billboard to go and
// the direction it should be facing
Point3 b_p = desired_base_pos; // new position for the billboard
Vector3 b_v1 = Vector3(0,1,0); // +Y is still up, doesn't change
Vector3 b_v2 = (camera.eye() - desired_base_pos); // the normal should point toward the camera
b_v2[1] = 0.0; // with 0 change in Y so the billboard does not tilt
b_v2.Normalize(); // convert to a unit vector
Matrix4 billboard_model_matrix = Matrix4::Align(a_p, a_v1, a_v2, b_p, b_v1, b_v2);
static Matrix4 Align(const Point3 &a_p, const Vector3 &a_v1, const Vector3 &a_v2, const Point3 &b_p, const Vector3 &b_v1, const Vector3 &b_v2)
Creates a transformation matrix that maps a coordinate space, a, defined one point,...

◆ Cofactor()

Matrix4 mingfx::Matrix4::Cofactor ( ) const

Returns the cofactor matrix.

◆ ColumnToPoint3()

Point3 mingfx::Matrix4::ColumnToPoint3 ( int  c) const

Returns the c-th column of the matrix as a Vector type, e.g.,: Point3 pos = mat.getColumnAsPoint3(3);.

◆ ColumnToVector3()

Vector3 mingfx::Matrix4::ColumnToVector3 ( int  c) const

Returns the c-th column of the matrix as a Vector type, e.g.,: Vector3 xAxis = mat.getColumnAsVector3(0); Vector3 yAxis = mat.getColumnAsVector3(1); Vector3 zAxis = mat.getColumnAsVector3(2);.

◆ Determinant()

float mingfx::Matrix4::Determinant ( ) const

Returns the determinant of the 4x4 matrix.

◆ FromRowMajorElements()

static Matrix4 mingfx::Matrix4::FromRowMajorElements ( const float  r1c1,
const float  r1c2,
const float  r1c3,
const float  r1c4,
const float  r2c1,
const float  r2c2,
const float  r2c3,
const float  r2c4,
const float  r3c1,
const float  r3c2,
const float  r3c3,
const float  r3c4,
const float  r4c1,
const float  r4c2,
const float  r4c3,
const float  r4c4 
)
static

Returns a matrix constructed from individual elements passed in row major order so that the matrix looks "correct" on the screen as you write this constructor on 4 lines of code as below.

Note the that internally the matrix constructed will be stored in a 16 element column major array to be consistent with OpenGL.

◆ Frustum()

static Matrix4 mingfx::Matrix4::Frustum ( float  left,
float  right,
float  bottom,
float  top,
float  near_plane_dist,
float  far_plane_dist 
)
static

Returns a projection matrix equivalent the one glFrustum creates.

◆ Inverse()

Matrix4 mingfx::Matrix4::Inverse ( ) const

Returns the inverse of the 4x4 matrix if it is nonsingular. If it is singular, then returns the identity matrix.

◆ LookAt()

static Matrix4 mingfx::Matrix4::LookAt ( Point3  eye,
Point3  target,
Vector3  up 
)
static

Returns a view matrix that centers the camera at the 'eye' position and orients it to look at the desired 'target' point with the top of the screen pointed as closely as possible in the direction of the 'up' vector.

◆ operator!=()

bool mingfx::Matrix4::operator!= ( const Matrix4 m2) const

Check for "inequality", taking floating point imprecision into account.

◆ operator()() [1/2]

float& mingfx::Matrix4::operator() ( const int  row,
const int  col 
)

Access an individual element of the array using the syntax: Matrix4 mat; mat(1,2) = 1.0;.

◆ operator()() [2/2]

float mingfx::Matrix4::operator() ( const int  row,
const int  col 
) const

Access an individual element of the array using the syntax: Matrix4 mat; float row1col2 = mat(1,2);.

◆ operator=()

Matrix4& mingfx::Matrix4::operator= ( const Matrix4 m2)

Matrix assignment operator.

◆ operator==()

bool mingfx::Matrix4::operator== ( const Matrix4 m2) const

Check for "equality", taking floating point imprecision into account.

◆ operator[]() [1/2]

float& mingfx::Matrix4::operator[] ( const int  i)

Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order.

◆ operator[]() [2/2]

float mingfx::Matrix4::operator[] ( const int  i) const

Accesses the ith element of the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order.

◆ Orthonormal()

Matrix4 mingfx::Matrix4::Orthonormal ( ) const

Returns an orthonormal version of the matrix, i.e., guarantees that the rotational component of the matrix is built from column vectors that are all unit vectors and orthogonal to each other.

◆ Perspective()

static Matrix4 mingfx::Matrix4::Perspective ( float  fov_y_in_degrees,
float  aspect_ratio,
float  near_plane_dist,
float  far_plane_dist 
)
static

Returns a perspective projection matrix equivalent to the one gluPerspective creates.

◆ Rotation()

static Matrix4 mingfx::Matrix4::Rotation ( const Point3 p,
const Vector3 v,
const float  a 
)
static

Returns the rotation matrix around the vector v placed at point p, rotate by angle a.

◆ RotationX()

static Matrix4 mingfx::Matrix4::RotationX ( const float  radians)
static

Returns the rotation matrix about the x axis by the specified angle.

◆ RotationY()

static Matrix4 mingfx::Matrix4::RotationY ( const float  radians)
static

Returns the rotation matrix about the y axis by the specified angle.

◆ RotationZ()

static Matrix4 mingfx::Matrix4::RotationZ ( const float  radians)
static

Returns the rotation matrix about the z axis by the specified angle.

◆ Scale()

static Matrix4 mingfx::Matrix4::Scale ( const Vector3 v)
static

Returns the scale matrix described by the vector.

◆ SubDeterminant()

float mingfx::Matrix4::SubDeterminant ( int  exclude_row,
int  exclude_col 
) const

Returns the determinant of the 3x3 matrix formed by excluding the specified row and column from the 4x4 matrix.

◆ ToVector()

std::vector<float> mingfx::Matrix4::ToVector ( ) const

◆ Translation()

static Matrix4 mingfx::Matrix4::Translation ( const Vector3 v)
static

Returns the translation matrix described by the vector.

◆ Transpose()

Matrix4 mingfx::Matrix4::Transpose ( ) const

Returns the transpose of the matrix.

◆ value_ptr()

const float* mingfx::Matrix4::value_ptr ( ) const

Returns a pointer to the raw data array used to store the matrix. This is a 1D array of 16-elements stored in column-major order.


The documentation for this class was generated from the following file: