diff --git a/src/bin/mesh/main.rs b/src/bin/mesh/main.rs index db0281e..b8c27fe 100644 --- a/src/bin/mesh/main.rs +++ b/src/bin/mesh/main.rs @@ -88,6 +88,27 @@ fn render(mesh: &Mesh, camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image { img } +fn persp(dist: f32, off: Vec2) -> (Vec3, Vec3) { + (vec3(0., 0., -dist), vec3(off.x, off.y, dist)) +} + +fn ortho(dist: f32, off: Vec2) -> (Vec3, Vec3) { + (vec3(off.x, off.y, -dist), vec3(0., 0., 1.)) +} + +#[test] +fn test_projs() { + fn check(f: fn(dist: f32, off: Vec2) -> (Vec3, Vec3), x: f32, y: f32, z: f32) { + let (base, ray) = f(z, vec2(x, y)); + let at_dist = base + ray * (z / ray.z); + assert_eq!(at_dist, vec3(x, y, 0.)); + } + check(persp, 1., 2., 3.); + check(ortho, 1., 2., 3.); + check(persp, 5., 3., 7.); + check(ortho, 9., 1., 8.); +} + #[show_image::main] fn main() -> Result<(), Box> { let args: Vec = env::args().collect(); @@ -100,6 +121,7 @@ fn main() -> Result<(), Box> { let mut f = BufReader::new(f); load_mesh(&mut f)? }; + let proj = persp; let window = show_image::create_window("Raytracing", WindowOptions::default())?; loop { for phi in 0..360 { @@ -110,14 +132,7 @@ fn main() -> Result<(), Box> { )); let m_camera = m_view.transpose(); let img = render(mesh.as_slice(), |off| { - // perspective projection - let base = vec3(0.0, 0.0, -40.0); - let ray = vec3(off.x, off.y, 2.0); - - // orthographic projection - // let base = vec3(off.x, off.y, -10.0); - // let ray = vec3(0.0, 0.0, 1.0); - + let (base, ray) = proj(40., 20. * off); (m_camera * base, m_camera * ray) });