rayzor
view src/vmathmat.h @ 14:a9a948809c6f
starting the renderer screen, plus misc stuff
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 13 Apr 2014 08:06:21 +0300 |
parents | d94a69933a71 |
children | 79609d482762 |
line source
1 #ifndef VMATH_MATRIX_H_
2 #define VMATH_MATRIX_H_
4 #include <math.h>
6 #ifndef M_PI
7 #define M_PI 3.141592653
8 #endif
10 class Vector3;
12 class Matrix4x4 {
13 public:
14 float m[4][4];
16 Matrix4x4()
17 {
18 set_identity();
19 }
21 Matrix4x4(float m00, float m01, float m02, float m03,
22 float m10, float m11, float m12, float m13,
23 float m20, float m21, float m22, float m23,
24 float m30, float m31, float m32, float m33)
25 {
26 m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
27 m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
28 m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
29 m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
30 }
32 inline void set_identity();
33 inline void translate(float x, float y, float z);
34 inline void rotate(float angle, float x, float y, float z);
35 inline void scale(float x, float y, float z);
36 inline void perspective(float vfov, float aspect, float znear, float zfar);
37 void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
39 float *operator [](int idx) { return m[idx]; }
40 const float *operator [](int idx) const { return m[idx]; }
42 void transpose();
44 float determinant() const;
45 Matrix4x4 adjoint() const;
46 Matrix4x4 inverse() const;
47 };
49 inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
50 {
51 Matrix4x4 res;
52 for(int i=0; i<4; i++) {
53 for(int j=0; j<4; j++) {
54 res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
55 a[i][2] * b[2][j] + a[i][3] * b[3][j];
56 }
57 }
58 return res;
59 }
61 inline Matrix4x4 operator *(const Matrix4x4 &mat, float scalar)
62 {
63 Matrix4x4 res;
65 for(int i=0; i<4; i++) {
66 for(int j=0; j<4; j++) {
67 res.m[i][j] = mat.m[i][j] * scalar;
68 }
69 }
70 return res;
71 }
73 inline void Matrix4x4::set_identity()
74 {
75 m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
76 m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
77 m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
78 }
80 inline void Matrix4x4::translate(float x, float y, float z)
81 {
82 Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
83 *this = *this * m;
84 }
86 inline void Matrix4x4::rotate(float angle, float x, float y, float z)
87 {
88 float sina = (float)sin(angle);
89 float cosa = (float)cos(angle);
90 float rcosa = 1.0f - cosa;
91 float nxsq = x * x;
92 float nysq = y * y;
93 float nzsq = z * z;
95 Matrix4x4 m;
96 m[0][0] = nxsq + (1.0f - nxsq) * cosa;
97 m[0][1] = x * y * rcosa - z * sina;
98 m[0][2] = x * z * rcosa + y * sina;
100 m[1][0] = x * y * rcosa + z * sina;
101 m[1][1] = nysq + (1.0f - nysq) * cosa;
102 m[1][2] = y * z * rcosa - x * sina;
104 m[2][0] = x * z * rcosa - y * sina;
105 m[2][1] = y * z * rcosa + x * sina;
106 m[2][2] = nzsq + (1.0f - nzsq) * cosa;
108 *this = *this * m;
109 }
111 inline void Matrix4x4::scale(float x, float y, float z)
112 {
113 Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
114 *this = *this * m;
115 }
117 inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
118 {
119 float f = 1.0f / tan(vfov * 0.5f);
120 float dz = znear - zfar;
122 Matrix4x4 m;
123 m[0][0] = f / aspect;
124 m[1][1] = f;
125 m[2][2] = (zfar + znear) / dz;
126 m[3][2] = -1.0f;
127 m[2][3] = 2.0f * zfar * znear / dz;
128 m[3][3] = 0.0f;
130 *this = *this * m;
131 }
133 #endif // VMATH_MATRIX_H_