aboutsummaryrefslogtreecommitdiffstats
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..f35c157
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,36 @@
+
+pub fn view_matrix(position: &[f32; 3],
+ direction: &[f32; 3],
+ up: &[f32; 3]) -> [[f32; 4]; 4] {
+ let f = {
+ let f = direction;
+ let len = f[0] * f[0] + f[1] * f[1] + f[2] * f[2];
+ let len = len.sqrt();
+ [f[0] / len, f[1] / len, f[2] / len]
+ };
+
+ let s = [up[1] * f[2] - up[2] * f[1],
+ up[2] * f[0] - up[0] * f[2],
+ up[0] * f[1] - up[1] * f[0]];
+
+ let s_norm = {
+ let len = s[0] * s[0] + s[1] * s[1] + s[2] * s[2];
+ let len = len.sqrt();
+ [s[0] / len, s[1] / len, s[2] / len]
+ };
+
+ let u = [f[1] * s_norm[2] - f[2] * s_norm[1],
+ f[2] * s_norm[0] - f[0] * s_norm[2],
+ f[0] * s_norm[1] - f[1] * s_norm[0]];
+
+ let p = [-position[0] * s_norm[0] - position[1] * s_norm[1] - position[2] * s_norm[2],
+ -position[0] * u[0] - position[1] * u[1] - position[2] * u[2],
+ -position[0] * f[0] - position[1] * f[1] - position[2] * f[2]];
+
+ [
+ [s[0], u[0], f[0], 0.0],
+ [s[1], u[1], f[1], 0.0],
+ [s[2], u[2], f[2], 0.0],
+ [p[0], p[1], p[2], 1.0],
+ ]
+}