Blame c++/perspective/src/matrix.h

14f971
#ifndef MATRIX_H
14f971
#define MATRIX_H
14f971
14f971
14f971
#include "vector.h"
14f971
14f971
14f971
class Matrix4 {
14f971
public:
14f971
	union {
14f971
		struct {
14f971
			Real m00, m01, m02, m03,
14f971
				 m10, m11, m12, m13,
14f971
				 m20, m21, m22, m23,
14f971
				 m30, m31, m32, m33;
14f971
		};
14f971
		struct { Real m[4][4]; };
14f971
		struct { Real a[16]; };
14f971
	};
14f971
	
14f971
	inline explicit Matrix4(
14f971
		const Vector4 &x = Vector4(1, 0, 0, 0),
14f971
		const Vector4 &y = Vector4(0, 1, 0, 0),
14f971
		const Vector4 &z = Vector4(0, 0, 1, 0),
14f971
		const Vector4 &w = Vector4(0, 0, 0, 1)
14f971
	):
14f971
		m00(x.x), m01(x.y), m02(x.z), m03(x.w),
14f971
		m10(y.x), m11(y.y), m12(y.z), m13(y.w),
14f971
		m20(z.x), m21(z.y), m22(z.z), m23(z.w),
14f971
		m30(w.x), m31(w.y), m32(w.z), m33(w.w) { }
14f971
	
14f971
	inline Vector4& operator[] (int index) { return Vector4::cast(m[index]); }
14f971
	inline const Vector4& operator[] (int index) const { return Vector4::cast(m[index]); }
14f971
	
14f971
	inline Vector4& row_x() { return (*this)[0]; }
14f971
	inline Vector4& row_y() { return (*this)[1]; }
14f971
	inline Vector4& row_z() { return (*this)[2]; }
14f971
	inline Vector4& row_w() { return (*this)[3]; }
14f971
14f971
	inline const Vector4& row_x() const { return (*this)[0]; }
14f971
	inline const Vector4& row_y() const { return (*this)[1]; }
14f971
	inline const Vector4& row_z() const { return (*this)[2]; }
14f971
	inline const Vector4& row_w() const { return (*this)[3]; }
14f971
	
14f971
	Real det() const;
14f971
	Matrix4 invert() const;
14f971
	
14f971
	inline Vector4 operator* (const Vector4 &v) const
14f971
		{ return row_x()*v.x + row_y()*v.y + row_z()*v.z + row_w()*v.w; }
14f971
	inline Matrix4 operator* (const Matrix4 &other) const {
14f971
		return Matrix4( *this * other.row_x(),
14f971
						*this * other.row_y(),
14f971
						*this * other.row_z(),
14f971
						*this * other.row_w() );
14f971
	}
14f971
	
14f971
	inline Matrix4& operator*= (const Matrix4 &other)
14f971
		{ return *this = *this * other; }
14f971
	
14f971
	static inline Matrix4 zero() { return Matrix4(Vector4(), Vector4(), Vector4(), Vector4()); }
14f971
	static inline Matrix4 identity() { return Matrix4(); }
14f971
};
14f971
14f971
14f971
typedef Matrix4 Matrix;
14f971
14f971
14f971
#endif