Mercurial > touhou
comparison src/util/math.rs @ 643:01849ffd0180
Add an anmrenderer binary.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 02 Aug 2019 20:24:45 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
642:9e40bd5cc26d | 643:01849ffd0180 |
---|---|
1 //! Various helpers to deal with vectors and matrices. | |
2 | |
3 /// A 4×4 f32 matrix type. | |
4 pub struct Mat4 { | |
5 inner: [[f32; 4]; 4] | |
6 } | |
7 | |
8 impl Mat4 { | |
9 /// Create a new matrix from a set of 16 f32. | |
10 pub fn new(inner: [[f32; 4]; 4]) -> Mat4 { | |
11 Mat4 { | |
12 inner | |
13 } | |
14 } | |
15 | |
16 fn zero() -> Mat4 { | |
17 Mat4 { | |
18 inner: [[0.; 4]; 4] | |
19 } | |
20 } | |
21 | |
22 fn identity() -> Mat4 { | |
23 Mat4 { | |
24 inner: [[1., 0., 0., 0.], | |
25 [0., 1., 0., 0.], | |
26 [0., 0., 1., 0.], | |
27 [0., 0., 0., 1.]] | |
28 } | |
29 } | |
30 | |
31 /// Immutably borrow the array of f32 inside this matrix. | |
32 pub fn borrow_inner(&self) -> &[[f32; 4]; 4] { | |
33 &self.inner | |
34 } | |
35 | |
36 /// Scale the matrix in 2D. | |
37 pub fn scale2d(&mut self, x: f32, y: f32) { | |
38 for i in 0..4 { | |
39 self.inner[0][i] *= x; | |
40 self.inner[1][i] *= y; | |
41 } | |
42 } | |
43 | |
44 /// Flip the matrix. | |
45 pub fn flip(&mut self) { | |
46 for i in 0..4 { | |
47 self.inner[0][i] = -self.inner[0][i]; | |
48 } | |
49 } | |
50 | |
51 /// Rotate the matrix around its x angle (in radians). | |
52 pub fn rotate_x(&mut self, angle: f32) { | |
53 let mut lines: [f32; 8] = [0.; 8]; | |
54 let cos_a = angle.cos(); | |
55 let sin_a = angle.sin(); | |
56 for i in 0..4 { | |
57 lines[ i] = self.inner[0][i]; | |
58 lines[4 + i] = self.inner[1][i]; | |
59 } | |
60 for i in 0..4 { | |
61 self.inner[1][i] = cos_a * lines[i] - sin_a * lines[4+i]; | |
62 self.inner[2][i] = sin_a * lines[i] + cos_a * lines[4+i]; | |
63 } | |
64 } | |
65 | |
66 /// Rotate the matrix around its y angle (in radians). | |
67 pub fn rotate_y(&mut self, angle: f32) { | |
68 let mut lines: [f32; 8] = [0.; 8]; | |
69 let cos_a = angle.cos(); | |
70 let sin_a = angle.sin(); | |
71 for i in 0..4 { | |
72 lines[ i] = self.inner[0][i]; | |
73 lines[4 + i] = self.inner[2][i]; | |
74 } | |
75 for i in 0..4 { | |
76 self.inner[0][i] = cos_a * lines[i] + sin_a * lines[4+i]; | |
77 self.inner[2][i] = -sin_a * lines[i] + cos_a * lines[4+i]; | |
78 } | |
79 } | |
80 | |
81 /// Rotate the matrix around its z angle (in radians). | |
82 pub fn rotate_z(&mut self, angle: f32) { | |
83 let mut lines: [f32; 8] = [0.; 8]; | |
84 let cos_a = angle.cos(); | |
85 let sin_a = angle.sin(); | |
86 for i in 0..4 { | |
87 lines[ i] = self.inner[0][i]; | |
88 lines[4 + i] = self.inner[1][i]; | |
89 } | |
90 for i in 0..4 { | |
91 self.inner[0][i] = cos_a * lines[i] - sin_a * lines[4+i]; | |
92 self.inner[1][i] = sin_a * lines[i] + cos_a * lines[4+i]; | |
93 } | |
94 } | |
95 | |
96 /// Translate the matrix by a 3D offset. | |
97 pub fn translate(&mut self, offset: [f32; 3]) { | |
98 let mut item: [f32; 3] = [0.; 3]; | |
99 for i in 0..3 { | |
100 item[i] = self.inner[3][i] * offset[i]; | |
101 } | |
102 for i in 0..3 { | |
103 for j in 0..4 { | |
104 self.inner[i][j] += item[i]; | |
105 } | |
106 } | |
107 } | |
108 | |
109 /// Translate the matrix by a 2D offset. | |
110 pub fn translate_2d(&mut self, x: f32, y: f32) { | |
111 let offset = [x, y, 0.]; | |
112 self.translate(offset); | |
113 } | |
114 } | |
115 | |
116 impl std::ops::Mul<Mat4> for Mat4 { | |
117 type Output = Mat4; | |
118 fn mul(self, rhs: Mat4) -> Mat4 { | |
119 let mut tmp = Mat4::zero(); | |
120 for i in 0..4 { | |
121 for j in 0..4 { | |
122 for k in 0..4 { | |
123 tmp.inner[i][j] += self.inner[i][k] * rhs.inner[k][j]; | |
124 } | |
125 } | |
126 } | |
127 tmp | |
128 } | |
129 } | |
130 | |
131 /// Create an orthographic projection matrix. | |
132 pub fn ortho_2d(left: f32, right: f32, bottom: f32, top: f32) -> Mat4 { | |
133 let mut mat = Mat4::identity(); | |
134 mat.inner[0][0] = 2. / (right - left); | |
135 mat.inner[1][1] = 2. / (top - bottom); | |
136 mat.inner[2][2] = -1.; | |
137 mat.inner[3][0] = -(right + left) / (right - left); | |
138 mat.inner[3][1] = -(top + bottom) / (top - bottom); | |
139 mat | |
140 } | |
141 | |
142 /// Setup a camera view matrix. | |
143 pub fn setup_camera(dx: f32, dy: f32, dz: f32) -> Mat4 { | |
144 // Some explanations on the magic constants: | |
145 // 192. = 384. / 2. = width / 2. | |
146 // 224. = 448. / 2. = height / 2. | |
147 // 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2)) | |
148 // This is so that objects on the (O, x, y) plane use pixel coordinates | |
149 look_at([192., 224., -835.979370 * dz], [192. + dx, 224. - dy, 0.], [0., -1., 0.]) | |
150 } | |
151 | |
152 /// Creates a perspective projection matrix. | |
153 pub fn perspective(fov_y: f32, aspect: f32, z_near: f32, z_far: f32) -> Mat4 { | |
154 let top = (fov_y / 2.).tan() * z_near; | |
155 let bottom = -top; | |
156 let left = -top * aspect; | |
157 let right = top * aspect; | |
158 | |
159 let mut mat = Mat4::identity(); | |
160 mat.inner[0][0] = (2. * z_near) / (right - left); | |
161 mat.inner[1][1] = (2. * z_near) / (top - bottom); | |
162 mat.inner[2][2] = -(z_far + z_near) / (z_far - z_near); | |
163 mat.inner[2][3] = -1.; | |
164 mat.inner[3][2] = -(2. * z_far * z_near) / (z_far - z_near); | |
165 mat.inner[3][3] = 0.; | |
166 mat | |
167 } | |
168 | |
169 type Vec3 = [f32; 3]; | |
170 | |
171 fn look_at(eye: Vec3, center: Vec3, up: Vec3) -> Mat4 { | |
172 let f = normalize(sub(center, eye)); | |
173 let u = normalize(up); | |
174 let s = normalize(cross(f, u)); | |
175 let u = cross(s, f); | |
176 | |
177 Mat4::new([[s[0], u[0], -f[0], 0.], | |
178 [s[1], u[1], -f[1], 0.], | |
179 [s[2], u[2], -f[2], 0.], | |
180 [-dot(s, eye), -dot(u, eye), dot(f, eye), 1.]]) | |
181 } | |
182 | |
183 fn sub(a: Vec3, b: Vec3) -> Vec3 { | |
184 [a[0] - b[0], | |
185 a[1] - b[1], | |
186 a[2] - b[2]] | |
187 } | |
188 | |
189 fn normalize(vec: Vec3) -> Vec3 { | |
190 let normal = 1. / (vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]).sqrt(); | |
191 [vec[0] * normal, vec[1] * normal, vec[2] * normal] | |
192 } | |
193 | |
194 fn cross(a: Vec3, b: Vec3) -> Vec3 { | |
195 [a[1] * b[2] - b[1] * a[2], | |
196 a[2] * b[0] - b[2] * a[0], | |
197 a[0] * b[1] - b[0] * a[1]] | |
198 } | |
199 | |
200 fn dot(a: Vec3, b: Vec3) -> f32 { | |
201 a[0] * b[0] + a[1] * b[1] + a[2] * b[2] | |
202 } |