#ifndef __MATRIX4x4_H #define __MATRIX4x4_H #include namespace CHCDemoEngine { class Vector3; class AxisAlignedBox3; class Matrix4x4 { public: /** Default constructor initialising nothig. */ Matrix4x4(); /** here xXY - 'X' is row, and 'Y' is column - classical mathematical notation */ Matrix4x4(float x11, float x12, float x13, float x14, float x21, float x22, float x23, float x24, float x31, float x32, float x33, float x34, float x41, float x42, float x43, float x44); /** Constructor setting the columns of the 3x3 part of the matrix. */ Matrix4x4(const Vector3 &a, const Vector3 &b, const Vector3 &c); //////////////// //-- Assignment operators Matrix4x4& operator+= (const Matrix4x4 &A); // add-to Matrix4x4& operator-= (const Matrix4x4 &A); // subtract-from Matrix4x4& operator*= (const Matrix4x4 &A); // multiply by matrix Matrix4x4& operator*= (float A); // scale by scalar //////////////// //-- Fundamental operations /** Invert the matrix .. returns 0 = regular */ int Invert(); /** Transpose the matrix */ void Transpose(); /** Sets the columns of the 3x3 part of the matrix. */ void SetColumns(const Vector3 &a, const Vector3 &b, const Vector3 &c); float Det3x3() const; /** multiply with homogen vector form, where the 4 coordinat is in h, the resulting homogen coordinate returned in w */ Vector3 Transform(float &w, const Vector3 &v, float h) const; //////////// //-- members float x[4][4]; // first index is column [x], the second is row [y] //////////////////// // Invert a given matrix friend Matrix4x4 Invert(const Matrix4x4 &M); // Transpose a given matrix friend Matrix4x4 Transpose(const Matrix4x4 &M); /////////// //-- Create various types of matrix. friend Matrix4x4 IdentityMatrix(); friend Matrix4x4 ZeroMatrix(); friend Matrix4x4 TranslationMatrix(const Vector3 &Location); friend Matrix4x4 RotationXMatrix(float Angle); friend Matrix4x4 RotationYMatrix(float Angle); friend Matrix4x4 RotationZMatrix(float Angle); friend Matrix4x4 RotationYPRMatrix(float Yaw, float Pitch, float Roll); // about axis 'axis' by angle 'Angle' friend Matrix4x4 RotationAxisMatrix(const Vector3 &axis, float Angle); // create the rotation matrix that rotates 'vecFrom' to 'vecTo' friend Matrix4x4 RotationVectorsMatrix(const Vector3 &vecFrom, const Vector3 &vecTo); friend Matrix4x4 ScaleMatrix(float X, float Y, float Z); friend Matrix4x4 GenRotation(const Vector3 &x, const Vector3 &y, const Vector3 &z); friend Matrix4x4 QuadricMatrix(float a, float b, float c, float d, float e, float f, float g, float h, float j, float k); // returns matrix for transforming normal friend Matrix4x4 NormalTransformMatrix(const Matrix4x4 &M); friend Matrix4x4 MirrorX(); friend Matrix4x4 MirrorY(); friend Matrix4x4 MirrorZ(); friend Matrix4x4 RotationOnly(const Matrix4x4 &x); // Binary operators friend Matrix4x4 operator+ (const Matrix4x4 &A, const Matrix4x4 &B); friend Matrix4x4 operator- (const Matrix4x4 &A, const Matrix4x4 &B); friend Matrix4x4 operator* (const Matrix4x4 &A, float B); friend Matrix4x4 operator* (const Matrix4x4 &A, const Matrix4x4 &B); // friends returning Vector3 friend Vector3 operator*(const Matrix4x4 &M, const Vector3 &v); friend Vector3 RotateOnly(const Matrix4x4 &M, const Vector3 &v); friend Vector3 TransformNormal(const Matrix4x4 &M, const Vector3 &v); friend Vector3 GetTranslation(const Matrix4x4 &M); friend Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box); //output is initialized with the same result as glFrustum friend Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); // look from position into given direction with given up vector friend Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); // Overloaded output operator. friend std::ostream& operator<< (std::ostream &s, const Matrix4x4 &M); }; ///////// //-- forward declaration Matrix4x4 IdentityMatrix(); Matrix4x4 Invert(const Matrix4x4 &M); Matrix4x4 Transpose(const Matrix4x4 &M); Matrix4x4 IdentityMatrix(); Matrix4x4 ZeroMatrix(); Matrix4x4 TranslationMatrix(const Vector3 &Location); Matrix4x4 RotationXMatrix(float Angle); Matrix4x4 RotationYMatrix(float Angle); Matrix4x4 RotationZMatrix(float Angle); Matrix4x4 RotationYPRMatrix(float Yaw, float Pitch, float Roll); Matrix4x4 RotationAxisMatrix(const Vector3 &axis, float Angle); Matrix4x4 RotationVectorsMatrix(const Vector3 &vecFrom, const Vector3 &vecTo); Matrix4x4 ScaleMatrix(float X, float Y, float Z); Matrix4x4 GenRotation(const Vector3 &x, const Vector3 &y, const Vector3 &z); Matrix4x4 QuadricMatrix(float a, float b, float c, float d, float e, float f, float g, float h, float j, float k); Matrix4x4 NormalTransformMatrix(const Matrix4x4 &M); Matrix4x4 MirrorX(); Matrix4x4 MirrorY(); Matrix4x4 MirrorZ(); Matrix4x4 RotationOnly(const Matrix4x4 &x); Matrix4x4 operator+ (const Matrix4x4 &A, const Matrix4x4 &B); Matrix4x4 operator- (const Matrix4x4 &A, const Matrix4x4 &B); Matrix4x4 operator* (const Matrix4x4 &A, float B); Matrix4x4 operator* (const Matrix4x4 &A, const Matrix4x4 &B); Vector3 operator*(const Matrix4x4 &M, const Vector3 &v); Vector3 RotateOnly(const Matrix4x4 &M, const Vector3 &v); Vector3 TransformNormal(const Matrix4x4 &M, const Vector3 &v); Vector3 GetTranslation(const Matrix4x4 &M); Matrix4x4 GetFittingProjectionMatrix(const AxisAlignedBox3 &box); Matrix4x4 GetFrustum(float left, float right, float bottom, float top, float near, float far); Matrix4x4 LookAt(const Vector3 &pos, const Vector3 &dir, const Vector3& up); Matrix4x4 GetOrtho(float left, float right, float bottom, float top, float near, float far); Matrix4x4 GetPerspective(float fov, float aspect, float near, float far); } #endif