rev |
line source |
nuclear@1
|
1 #include "vmathmat.h"
|
nuclear@1
|
2 #include "vmath.h"
|
nuclear@1
|
3
|
nuclear@1
|
4 void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up)
|
nuclear@1
|
5 {
|
nuclear@1
|
6 Vector3 vk = normalize(targ - pos);
|
nuclear@1
|
7 Vector3 vj = normalize(up);
|
nuclear@1
|
8 Vector3 vi = normalize(cross(vk, vj));
|
nuclear@1
|
9 vj = cross(vi, vk);
|
nuclear@1
|
10
|
nuclear@1
|
11 Matrix4x4 m(
|
nuclear@1
|
12 vi.x, vi.y, vi.z, 0,
|
nuclear@1
|
13 vj.x, vj.y, vj.z, 0,
|
nuclear@1
|
14 -vk.x, -vk.y, -vk.z, 0,
|
nuclear@1
|
15 0, 0, 0, 1);
|
nuclear@1
|
16 translate(-pos.x, -pos.y, -pos.z);
|
nuclear@1
|
17 *this = *this * m;
|
nuclear@1
|
18 }
|
nuclear@12
|
19
|
nuclear@12
|
20 void Matrix4x4::transpose()
|
nuclear@12
|
21 {
|
nuclear@12
|
22 Matrix4x4 tmp = *this;
|
nuclear@12
|
23 for(int i=0; i<4; i++) {
|
nuclear@12
|
24 for(int j=0; j<4; j++) {
|
nuclear@12
|
25 m[i][j] = tmp[j][i];
|
nuclear@12
|
26 }
|
nuclear@12
|
27 }
|
nuclear@12
|
28 }
|
nuclear@12
|
29
|
nuclear@12
|
30 float Matrix4x4::determinant() const
|
nuclear@12
|
31 {
|
nuclear@12
|
32 float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
33 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@12
|
34 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@12
|
35
|
nuclear@12
|
36 float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
37 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
38 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@12
|
39
|
nuclear@12
|
40 float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@12
|
41 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
42 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
43
|
nuclear@12
|
44 float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@12
|
45 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@12
|
46 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
47
|
nuclear@12
|
48 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
|
nuclear@12
|
49 }
|
nuclear@12
|
50
|
nuclear@12
|
51
|
nuclear@12
|
52 Matrix4x4 Matrix4x4::adjoint() const
|
nuclear@12
|
53 {
|
nuclear@12
|
54 Matrix4x4 coef;
|
nuclear@12
|
55
|
nuclear@12
|
56 coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
57 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@12
|
58 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@12
|
59 coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
60 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
61 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@12
|
62 coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@12
|
63 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
64 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
65 coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@12
|
66 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@12
|
67 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
68
|
nuclear@12
|
69 coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
70 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
|
nuclear@12
|
71 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
|
nuclear@12
|
72 coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
|
nuclear@12
|
73 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
74 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
|
nuclear@12
|
75 coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
|
nuclear@12
|
76 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
|
nuclear@12
|
77 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
78 coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
|
nuclear@12
|
79 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
|
nuclear@12
|
80 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
|
nuclear@12
|
81
|
nuclear@12
|
82 coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
|
nuclear@12
|
83 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
|
nuclear@12
|
84 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
|
nuclear@12
|
85 coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
|
nuclear@12
|
86 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
|
nuclear@12
|
87 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
|
nuclear@12
|
88 coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
|
nuclear@12
|
89 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
|
nuclear@12
|
90 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
|
nuclear@12
|
91 coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
|
nuclear@12
|
92 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
|
nuclear@12
|
93 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
|
nuclear@12
|
94
|
nuclear@12
|
95 coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
|
nuclear@12
|
96 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
|
nuclear@12
|
97 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
|
nuclear@12
|
98 coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
|
nuclear@12
|
99 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
|
nuclear@12
|
100 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
|
nuclear@12
|
101 coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
|
nuclear@12
|
102 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
|
nuclear@12
|
103 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
|
nuclear@12
|
104 coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
|
nuclear@12
|
105 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
|
nuclear@12
|
106 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
|
nuclear@12
|
107
|
nuclear@12
|
108 coef.transpose();
|
nuclear@12
|
109
|
nuclear@12
|
110 for(int i=0; i<4; i++) {
|
nuclear@12
|
111 for(int j=0; j<4; j++) {
|
nuclear@12
|
112 coef.m[i][j] = j % 2 ? -coef.m[i][j] : coef.m[i][j];
|
nuclear@12
|
113 if(i % 2) coef.m[i][j] = -coef.m[i][j];
|
nuclear@12
|
114 }
|
nuclear@12
|
115 }
|
nuclear@12
|
116
|
nuclear@12
|
117 return coef;
|
nuclear@12
|
118 }
|
nuclear@12
|
119
|
nuclear@12
|
120 Matrix4x4 Matrix4x4::inverse() const
|
nuclear@12
|
121 {
|
nuclear@12
|
122 Matrix4x4 adj = adjoint();
|
nuclear@12
|
123 return adj * (1.0f / determinant());
|
nuclear@12
|
124 }
|