diff --git a/Cargo.toml b/Cargo.toml index 8de0f57..6a1ae01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,5 @@ opt-level = 3 [dependencies] rand = "0.8.5" -glm = "0.2.3" +glam = "0.27.0" show-image = "0.14.0" diff --git a/src/bin/mesh/main.rs b/src/bin/mesh/main.rs index 015845d..04a44d6 100644 --- a/src/bin/mesh/main.rs +++ b/src/bin/mesh/main.rs @@ -5,7 +5,7 @@ use std::{env}; use std::error::Error; use std::f32::consts::PI; use std::io::{BufReader}; -use glm::*; +use glam::*; use show_image::{ImageInfo, ImageView, WindowOptions}; use crate::mesh_loader::{Face, load_mesh}; @@ -40,17 +40,17 @@ impl Image { fn ypr_to_mat(ypr: Vec3) -> Mat3 { let Vec3 { x: yaw, y: pitch, z: roll } = ypr; let m_roll = mat3( - roll.cos(), roll.sin(), 0.0, - -roll.sin(), roll.cos(), 0.0, - 0.0, 0.0, 1.0); + vec3(roll.cos(), roll.sin(), 0.0), + vec3(-roll.sin(), roll.cos(), 0.0), + vec3(0.0, 0.0, 1.0)); let m_yaw = mat3( - yaw.cos(), 0.0, yaw.sin(), - 0.0, 1.0, 0.0, - -yaw.sin(), 0.0, yaw.cos()); + vec3(yaw.cos(), 0.0, yaw.sin()), + vec3(0.0, 1.0, 0.0), + vec3(-yaw.sin(), 0.0, yaw.cos())); let m_pitch = mat3( - 1.0, 0.0, 0.0, - 0.0, pitch.cos(), -pitch.sin(), - 0.0, pitch.sin(), pitch.cos()); + vec3(1.0, 0.0, 0.0), + vec3(0.0, pitch.cos(), -pitch.sin()), + vec3(0.0, pitch.sin(), pitch.cos())); m_roll * m_pitch * m_yaw } @@ -67,20 +67,17 @@ fn trace_to_mesh(mesh: &Mesh, base: Vec3, ray: Vec3) -> Option { for f in mesh { let fs = (0..3).map(|k| edge_dist(f.vertices[k], f.vertices[(k + 1) % 3], base, ray)); if fs.into_iter().all(|f| f >= 0.0) { - let m = Mat3 { c0: f.vertices[1] - f.vertices[0], c1: f.vertices[2] - f.vertices[0], c2: -ray }; - if let Some(m) = m.inverse() { - let rel = m * (base - f.vertices[0]); - if rel.z > dist { - continue; - } - dist = rel.z; - ret = Some(TraceResult { - distance: rel.z, - normal: f.normal, - }); - } else { + let m = mat3(f.vertices[1] - f.vertices[0], f.vertices[2] - f.vertices[0], -ray); + let m = m.inverse(); + let rel = m * (base - f.vertices[0]); + if rel.z > dist { continue; } + dist = rel.z; + ret = Some(TraceResult { + distance: rel.z, + normal: f.normal, + }); } } ret @@ -104,13 +101,13 @@ fn render(mesh: &Mesh, camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image { let img_coords = vec2(x as f32, y as f32); let off = (img_coords - img_size * 0.5) / img_size.y; let (base, ray) = camera(off); - let color = if let Some(r) = trace_to_mesh(mesh, base, normalize(ray)) { + let color = if let Some(r) = trace_to_mesh(mesh, base, ray.normalize()) { // to_vec3(0.45) * dot(r.normal, normalize(vec3(-1.0, 1.0, -1.0))) + 0.50 r.normal * 0.45 + 0.50 } else { bkg }; - let color = clamp_s(to_ivec3(color * 255.0), 0, 255); + let color = (color * 255.0).as_ivec3().clamp(IVec3::splat(0), IVec3::splat(255)); img.put_pixel(x, y, Color(color.x as u8, color.y as u8, color.z as u8)); } } @@ -129,7 +126,7 @@ fn main() -> Result<(), Box> { loop { for phi in 0..360 { let m_view = ypr_to_mat(vec3((135.0 + phi as f32) * PI / 180.0, -30.0 * PI / 180.0, 0.0f32)); - let m_camera = transpose(&m_view); + let m_camera = m_view.transpose(); let img = render(mesh.as_slice(), |off| { // perspective projection let base = vec3(0.0, 0.0, -40.0); @@ -150,5 +147,5 @@ fn main() -> Result<(), Box> { fn edge_dist(a: Vec3, b: Vec3, base: Vec3, dir: Vec3) -> f32 { // Note: given that the input is not arbitrary but comes from a cartesian product of certain (a, b) pairs and certain (base, dir) pairs, this can be optimized from Cnm to an+bm+cnm with c