nuclear@1: #ifndef VMATH_MATRIX_H_ nuclear@1: #define VMATH_MATRIX_H_ nuclear@1: nuclear@1: #include nuclear@1: nuclear@1: #ifndef M_PI nuclear@1: #define M_PI 3.141592653 nuclear@1: #endif nuclear@1: nuclear@1: class Vector3; nuclear@1: nuclear@1: class Matrix4x4 { nuclear@1: public: nuclear@1: float m[4][4]; nuclear@1: nuclear@1: Matrix4x4() nuclear@1: { nuclear@1: set_identity(); nuclear@1: } nuclear@1: nuclear@1: Matrix4x4(float m00, float m01, float m02, float m03, nuclear@1: float m10, float m11, float m12, float m13, nuclear@1: float m20, float m21, float m22, float m23, nuclear@1: float m30, float m31, float m32, float m33) nuclear@1: { nuclear@1: m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; nuclear@1: m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; nuclear@1: m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; nuclear@1: m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; nuclear@1: } nuclear@1: nuclear@1: inline void set_identity(); nuclear@1: inline void translate(float x, float y, float z); nuclear@1: inline void rotate(float angle, float x, float y, float z); nuclear@1: inline void scale(float x, float y, float z); nuclear@1: inline void perspective(float vfov, float aspect, float znear, float zfar); nuclear@13: void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up); nuclear@1: nuclear@1: float *operator [](int idx) { return m[idx]; } nuclear@1: const float *operator [](int idx) const { return m[idx]; } nuclear@12: nuclear@12: void transpose(); nuclear@12: nuclear@12: float determinant() const; nuclear@12: Matrix4x4 adjoint() const; nuclear@12: Matrix4x4 inverse() const; nuclear@1: }; nuclear@1: nuclear@1: inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) nuclear@1: { nuclear@1: Matrix4x4 res; nuclear@1: for(int i=0; i<4; i++) { nuclear@1: for(int j=0; j<4; j++) { nuclear@1: res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + nuclear@1: a[i][2] * b[2][j] + a[i][3] * b[3][j]; nuclear@1: } nuclear@1: } nuclear@1: return res; nuclear@1: } nuclear@1: nuclear@12: inline Matrix4x4 operator *(const Matrix4x4 &mat, float scalar) nuclear@12: { nuclear@12: Matrix4x4 res; nuclear@12: nuclear@12: for(int i=0; i<4; i++) { nuclear@12: for(int j=0; j<4; j++) { nuclear@12: res.m[i][j] = mat.m[i][j] * scalar; nuclear@12: } nuclear@12: } nuclear@12: return res; nuclear@12: } nuclear@12: nuclear@1: inline void Matrix4x4::set_identity() nuclear@1: { nuclear@1: m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; nuclear@1: m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0; nuclear@1: m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0; nuclear@1: } nuclear@1: nuclear@1: inline void Matrix4x4::translate(float x, float y, float z) nuclear@1: { nuclear@1: Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); nuclear@1: *this = *this * m; nuclear@1: } nuclear@1: nuclear@1: inline void Matrix4x4::rotate(float angle, float x, float y, float z) nuclear@1: { nuclear@1: float sina = (float)sin(angle); nuclear@1: float cosa = (float)cos(angle); nuclear@1: float rcosa = 1.0f - cosa; nuclear@1: float nxsq = x * x; nuclear@1: float nysq = y * y; nuclear@1: float nzsq = z * z; nuclear@1: nuclear@1: Matrix4x4 m; nuclear@1: m[0][0] = nxsq + (1.0f - nxsq) * cosa; nuclear@1: m[0][1] = x * y * rcosa - z * sina; nuclear@1: m[0][2] = x * z * rcosa + y * sina; nuclear@1: nuclear@1: m[1][0] = x * y * rcosa + z * sina; nuclear@1: m[1][1] = nysq + (1.0f - nysq) * cosa; nuclear@1: m[1][2] = y * z * rcosa - x * sina; nuclear@1: nuclear@1: m[2][0] = x * z * rcosa - y * sina; nuclear@1: m[2][1] = y * z * rcosa + x * sina; nuclear@1: m[2][2] = nzsq + (1.0f - nzsq) * cosa; nuclear@1: nuclear@1: *this = *this * m; nuclear@1: } nuclear@1: nuclear@1: inline void Matrix4x4::scale(float x, float y, float z) nuclear@1: { nuclear@1: Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); nuclear@1: *this = *this * m; nuclear@1: } nuclear@1: nuclear@1: inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar) nuclear@1: { nuclear@1: float f = 1.0f / tan(vfov * 0.5f); nuclear@1: float dz = znear - zfar; nuclear@1: nuclear@1: Matrix4x4 m; nuclear@1: m[0][0] = f / aspect; nuclear@1: m[1][1] = f; nuclear@1: m[2][2] = (zfar + znear) / dz; nuclear@1: m[3][2] = -1.0f; nuclear@1: m[2][3] = 2.0f * zfar * znear / dz; nuclear@1: m[3][3] = 0.0f; nuclear@1: nuclear@1: *this = *this * m; nuclear@1: } nuclear@1: nuclear@1: #endif // VMATH_MATRIX_H_