rayzor

view src/vmath.cc @ 12:d94a69933a71

lots of stuff, can't remember
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:28:24 +0300
parents a826bf0fb169
children 79609d482762
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 m(
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 translate(-pos.x, -pos.y, -pos.z);
17 *this = *this * m;
18 }
20 void Matrix4x4::transpose()
21 {
22 Matrix4x4 tmp = *this;
23 for(int i=0; i<4; i++) {
24 for(int j=0; j<4; j++) {
25 m[i][j] = tmp[j][i];
26 }
27 }
28 }
30 float Matrix4x4::determinant() const
31 {
32 float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
33 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
34 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
36 float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
37 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
38 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
40 float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
41 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
42 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
44 float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
45 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
46 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
48 return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14;
49 }
52 Matrix4x4 Matrix4x4::adjoint() const
53 {
54 Matrix4x4 coef;
56 coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
57 (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
58 (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
59 coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
60 (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
61 (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
62 coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
63 (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
64 (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
65 coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
66 (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
67 (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
69 coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
70 (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) +
71 (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2]));
72 coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) -
73 (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
74 (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2]));
75 coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) -
76 (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) +
77 (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
78 coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) -
79 (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) +
80 (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1]));
82 coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
83 (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) +
84 (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2]));
85 coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) -
86 (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
87 (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2]));
88 coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) -
89 (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) +
90 (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
91 coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) -
92 (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) +
93 (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1]));
95 coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
96 (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) +
97 (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]));
98 coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) -
99 (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
100 (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]));
101 coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) -
102 (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) +
103 (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
104 coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) -
105 (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) +
106 (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1]));
108 coef.transpose();
110 for(int i=0; i<4; i++) {
111 for(int j=0; j<4; j++) {
112 coef.m[i][j] = j % 2 ? -coef.m[i][j] : coef.m[i][j];
113 if(i % 2) coef.m[i][j] = -coef.m[i][j];
114 }
115 }
117 return coef;
118 }
120 Matrix4x4 Matrix4x4::inverse() const
121 {
122 Matrix4x4 adj = adjoint();
123 return adj * (1.0f / determinant());
124 }