rev |
line source |
nuclear@1
|
1 #ifndef VMATH_MATRIX_H_
|
nuclear@1
|
2 #define VMATH_MATRIX_H_
|
nuclear@1
|
3
|
nuclear@1
|
4 #include <math.h>
|
nuclear@1
|
5
|
nuclear@1
|
6 #ifndef M_PI
|
nuclear@1
|
7 #define M_PI 3.141592653
|
nuclear@1
|
8 #endif
|
nuclear@1
|
9
|
nuclear@1
|
10 class Vector3;
|
nuclear@1
|
11
|
nuclear@1
|
12 class Matrix4x4 {
|
nuclear@1
|
13 public:
|
nuclear@1
|
14 float m[4][4];
|
nuclear@1
|
15
|
nuclear@1
|
16 Matrix4x4()
|
nuclear@1
|
17 {
|
nuclear@1
|
18 set_identity();
|
nuclear@1
|
19 }
|
nuclear@1
|
20
|
nuclear@1
|
21 Matrix4x4(float m00, float m01, float m02, float m03,
|
nuclear@1
|
22 float m10, float m11, float m12, float m13,
|
nuclear@1
|
23 float m20, float m21, float m22, float m23,
|
nuclear@1
|
24 float m30, float m31, float m32, float m33)
|
nuclear@1
|
25 {
|
nuclear@1
|
26 m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
|
nuclear@1
|
27 m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
|
nuclear@1
|
28 m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
|
nuclear@1
|
29 m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
|
nuclear@1
|
30 }
|
nuclear@1
|
31
|
nuclear@1
|
32 inline void set_identity();
|
nuclear@1
|
33 inline void translate(float x, float y, float z);
|
nuclear@1
|
34 inline void rotate(float angle, float x, float y, float z);
|
nuclear@1
|
35 inline void scale(float x, float y, float z);
|
nuclear@1
|
36 inline void perspective(float vfov, float aspect, float znear, float zfar);
|
nuclear@13
|
37 void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
|
nuclear@1
|
38
|
nuclear@1
|
39 float *operator [](int idx) { return m[idx]; }
|
nuclear@1
|
40 const float *operator [](int idx) const { return m[idx]; }
|
nuclear@12
|
41
|
nuclear@12
|
42 void transpose();
|
nuclear@12
|
43
|
nuclear@12
|
44 float determinant() const;
|
nuclear@12
|
45 Matrix4x4 adjoint() const;
|
nuclear@12
|
46 Matrix4x4 inverse() const;
|
nuclear@1
|
47 };
|
nuclear@1
|
48
|
nuclear@1
|
49 inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
|
nuclear@1
|
50 {
|
nuclear@1
|
51 Matrix4x4 res;
|
nuclear@1
|
52 for(int i=0; i<4; i++) {
|
nuclear@1
|
53 for(int j=0; j<4; j++) {
|
nuclear@1
|
54 res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
|
nuclear@1
|
55 a[i][2] * b[2][j] + a[i][3] * b[3][j];
|
nuclear@1
|
56 }
|
nuclear@1
|
57 }
|
nuclear@1
|
58 return res;
|
nuclear@1
|
59 }
|
nuclear@1
|
60
|
nuclear@12
|
61 inline Matrix4x4 operator *(const Matrix4x4 &mat, float scalar)
|
nuclear@12
|
62 {
|
nuclear@12
|
63 Matrix4x4 res;
|
nuclear@12
|
64
|
nuclear@12
|
65 for(int i=0; i<4; i++) {
|
nuclear@12
|
66 for(int j=0; j<4; j++) {
|
nuclear@12
|
67 res.m[i][j] = mat.m[i][j] * scalar;
|
nuclear@12
|
68 }
|
nuclear@12
|
69 }
|
nuclear@12
|
70 return res;
|
nuclear@12
|
71 }
|
nuclear@12
|
72
|
nuclear@1
|
73 inline void Matrix4x4::set_identity()
|
nuclear@1
|
74 {
|
nuclear@1
|
75 m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
|
nuclear@1
|
76 m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
|
nuclear@1
|
77 m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
|
nuclear@1
|
78 }
|
nuclear@1
|
79
|
nuclear@1
|
80 inline void Matrix4x4::translate(float x, float y, float z)
|
nuclear@1
|
81 {
|
nuclear@1
|
82 Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
|
nuclear@1
|
83 *this = *this * m;
|
nuclear@1
|
84 }
|
nuclear@1
|
85
|
nuclear@1
|
86 inline void Matrix4x4::rotate(float angle, float x, float y, float z)
|
nuclear@1
|
87 {
|
nuclear@1
|
88 float sina = (float)sin(angle);
|
nuclear@1
|
89 float cosa = (float)cos(angle);
|
nuclear@1
|
90 float rcosa = 1.0f - cosa;
|
nuclear@1
|
91 float nxsq = x * x;
|
nuclear@1
|
92 float nysq = y * y;
|
nuclear@1
|
93 float nzsq = z * z;
|
nuclear@1
|
94
|
nuclear@1
|
95 Matrix4x4 m;
|
nuclear@1
|
96 m[0][0] = nxsq + (1.0f - nxsq) * cosa;
|
nuclear@1
|
97 m[0][1] = x * y * rcosa - z * sina;
|
nuclear@1
|
98 m[0][2] = x * z * rcosa + y * sina;
|
nuclear@1
|
99
|
nuclear@1
|
100 m[1][0] = x * y * rcosa + z * sina;
|
nuclear@1
|
101 m[1][1] = nysq + (1.0f - nysq) * cosa;
|
nuclear@1
|
102 m[1][2] = y * z * rcosa - x * sina;
|
nuclear@1
|
103
|
nuclear@1
|
104 m[2][0] = x * z * rcosa - y * sina;
|
nuclear@1
|
105 m[2][1] = y * z * rcosa + x * sina;
|
nuclear@1
|
106 m[2][2] = nzsq + (1.0f - nzsq) * cosa;
|
nuclear@1
|
107
|
nuclear@1
|
108 *this = *this * m;
|
nuclear@1
|
109 }
|
nuclear@1
|
110
|
nuclear@1
|
111 inline void Matrix4x4::scale(float x, float y, float z)
|
nuclear@1
|
112 {
|
nuclear@1
|
113 Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
|
nuclear@1
|
114 *this = *this * m;
|
nuclear@1
|
115 }
|
nuclear@1
|
116
|
nuclear@1
|
117 inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
|
nuclear@1
|
118 {
|
nuclear@1
|
119 float f = 1.0f / tan(vfov * 0.5f);
|
nuclear@1
|
120 float dz = znear - zfar;
|
nuclear@1
|
121
|
nuclear@1
|
122 Matrix4x4 m;
|
nuclear@1
|
123 m[0][0] = f / aspect;
|
nuclear@1
|
124 m[1][1] = f;
|
nuclear@1
|
125 m[2][2] = (zfar + znear) / dz;
|
nuclear@1
|
126 m[3][2] = -1.0f;
|
nuclear@1
|
127 m[2][3] = 2.0f * zfar * znear / dz;
|
nuclear@1
|
128 m[3][3] = 0.0f;
|
nuclear@1
|
129
|
nuclear@1
|
130 *this = *this * m;
|
nuclear@1
|
131 }
|
nuclear@1
|
132
|
nuclear@1
|
133 #endif // VMATH_MATRIX_H_
|