rayzor
view src/vmath.cc @ 21:8c4859442200
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 15 Apr 2014 20:52:05 +0300 |
parents | d94a69933a71 |
children |
line source
1 #include "vmathmat.h"
2 #include "vmath.h"
4 void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up)
5 {
6 Vector3 vk = normalize(targ - pos);
7 Vector3 vj = normalize(up);
8 Vector3 vi = normalize(cross(vk, vj));
9 vj = cross(vi, vk);
11 Matrix4x4 rm(
12 vi.x, vi.y, vi.z, 0,
13 vj.x, vj.y, vj.z, 0,
14 -vk.x, -vk.y, -vk.z, 0,
15 0, 0, 0, 1);
16 Matrix4x4 tm(
17 1, 0, 0, -pos.x,
18 0, 1, 0, -pos.y,
19 0, 0, 1, -pos.z,
20 0, 0, 0, 1);
21 *this = *this * (rm * tm);
22 }
24 void Matrix4x4::transpose()
25 {
26 Matrix4x4 tmp = *this;
27 for(int i=0; i<4; i++) {
28 for(int j=0; j<4; j++) {
29 m[i][j] = tmp[j][i];
30 }
31 }
32 }
34 float Matrix4x4::determinant() const
35 {
36 float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
37 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
38 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
40 float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
41 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
42 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
44 float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
45 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
46 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
48 float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
49 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
50 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
52 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
53 }
56 Matrix4x4 Matrix4x4::adjoint() const
57 {
58 Matrix4x4 coef;
60 coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
61 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
62 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
63 coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
64 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
65 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
66 coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
67 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
68 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
69 coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
70 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
71 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
73 coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
74 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
75 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
76 coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
77 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
78 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
79 coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
80 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
81 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
82 coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
83 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
84 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
86 coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
87 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
88 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
89 coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
90 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
91 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
92 coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
93 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
94 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
95 coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
96 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
97 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
99 coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
100 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
101 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
102 coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
103 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
104 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
105 coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
106 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
107 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
108 coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
109 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
110 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
112 coef.transpose();
114 for(int i=0; i<4; i++) {
115 for(int j=0; j<4; j++) {
116 coef.m[i][j] = j % 2 ? -coef.m[i][j] : coef.m[i][j];
117 if(i % 2) coef.m[i][j] = -coef.m[i][j];
118 }
119 }
121 return coef;
122 }
124 Matrix4x4 Matrix4x4::inverse() const
125 {
126 Matrix4x4 adj = adjoint();
127 return adj * (1.0f / determinant());
128 }