rayzor

diff src/vmathmat.h @ 0:2a5340a6eee4

rayzor first commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 08:46:27 +0300
parents
children a826bf0fb169
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/vmathmat.h	Sat Apr 05 08:46:27 2014 +0300
     1.3 @@ -0,0 +1,116 @@
     1.4 +#ifndef VMATH_MATRIX_H_
     1.5 +#define VMATH_MATRIX_H_
     1.6 +
     1.7 +#include <math.h>
     1.8 +
     1.9 +#ifndef M_PI
    1.10 +#define M_PI	3.141592653
    1.11 +#endif
    1.12 +
    1.13 +class Vector3;
    1.14 +
    1.15 +class Matrix4x4 {
    1.16 +public:
    1.17 +	float m[4][4];
    1.18 +
    1.19 +	Matrix4x4()
    1.20 +	{
    1.21 +		set_identity();
    1.22 +	}
    1.23 +
    1.24 +	Matrix4x4(float m00, float m01, float m02, float m03,
    1.25 +			float m10, float m11, float m12, float m13,
    1.26 +			float m20, float m21, float m22, float m23,
    1.27 +			float m30, float m31, float m32, float m33)
    1.28 +	{
    1.29 +		m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
    1.30 +		m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
    1.31 +		m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
    1.32 +		m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
    1.33 +	}
    1.34 +
    1.35 +	inline void set_identity();
    1.36 +	inline void translate(float x, float y, float z);
    1.37 +	inline void rotate(float angle, float x, float y, float z);
    1.38 +	inline void scale(float x, float y, float z);
    1.39 +	inline void perspective(float vfov, float aspect, float znear, float zfar);
    1.40 +	inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
    1.41 +
    1.42 +	float *operator [](int idx) { return m[idx]; }
    1.43 +	const float *operator [](int idx) const { return m[idx]; }
    1.44 +};
    1.45 +
    1.46 +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
    1.47 +{
    1.48 +	Matrix4x4 res;
    1.49 +	for(int i=0; i<4; i++) {
    1.50 +		for(int j=0; j<4; j++) {
    1.51 +			res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
    1.52 +				a[i][2] * b[2][j] + a[i][3] * b[3][j];
    1.53 +		}
    1.54 +	}
    1.55 +	return res;
    1.56 +}
    1.57 +
    1.58 +inline void Matrix4x4::set_identity()
    1.59 +{
    1.60 +	m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
    1.61 +	m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
    1.62 +	m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
    1.63 +}
    1.64 +
    1.65 +inline void Matrix4x4::translate(float x, float y, float z)
    1.66 +{
    1.67 +	Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
    1.68 +	*this = *this * m;
    1.69 +}
    1.70 +
    1.71 +inline void Matrix4x4::rotate(float angle, float x, float y, float z)
    1.72 +{
    1.73 +	float sina = (float)sin(angle);
    1.74 +	float cosa = (float)cos(angle);
    1.75 +	float rcosa = 1.0f - cosa;
    1.76 +	float nxsq = x * x;
    1.77 +	float nysq = y * y;
    1.78 +	float nzsq = z * z;
    1.79 +
    1.80 +	Matrix4x4 m;
    1.81 +	m[0][0] = nxsq + (1.0f - nxsq) * cosa;
    1.82 +	m[0][1] = x * y * rcosa - z * sina;
    1.83 +	m[0][2] = x * z * rcosa + y * sina;
    1.84 +
    1.85 +	m[1][0] = x * y * rcosa + z * sina;
    1.86 +	m[1][1] = nysq + (1.0f - nysq) * cosa;
    1.87 +	m[1][2] = y * z * rcosa - x * sina;
    1.88 +
    1.89 +	m[2][0] = x * z * rcosa - y * sina;
    1.90 +	m[2][1] = y * z * rcosa + x * sina;
    1.91 +	m[2][2] = nzsq + (1.0f - nzsq) * cosa;
    1.92 +
    1.93 +	*this = *this * m;
    1.94 +}
    1.95 +
    1.96 +inline void Matrix4x4::scale(float x, float y, float z)
    1.97 +{
    1.98 +	Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
    1.99 +	*this = *this * m;
   1.100 +}
   1.101 +
   1.102 +inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
   1.103 +{
   1.104 +	float f = 1.0f / tan(vfov * 0.5f);
   1.105 +	float dz = znear - zfar;
   1.106 +
   1.107 +	Matrix4x4 m;
   1.108 +	m[0][0] = f / aspect;
   1.109 +	m[1][1] = f;
   1.110 +	m[2][2] = (zfar + znear) / dz;
   1.111 +	m[3][2] = -1.0f;
   1.112 +	m[2][3] = 2.0f * zfar * znear / dz;
   1.113 +	m[3][3] = 0.0f;
   1.114 +
   1.115 +	*this = *this * m;
   1.116 +}
   1.117 +
   1.118 +
   1.119 +#endif	// VMATH_MATRIX_H_