diff --git a/ray-tracing-core/src/affine_transform.rs b/ray-tracing-core/src/affine_transform.rs index a0f52b5..81b3aaa 100644 --- a/ray-tracing-core/src/affine_transform.rs +++ b/ray-tracing-core/src/affine_transform.rs @@ -40,6 +40,10 @@ impl AffineTransform { Some(Self { mat, inv }) } + pub fn get(&self, i: usize, j: usize) -> Float { + self.mat[j][i] + } + pub fn identity() -> Self { Self { mat: [ @@ -127,7 +131,7 @@ impl AffineTransform { pub fn look_at(pos: Pos3, look: Pos3, up: Dir3) -> Option { let dir = (look - pos).normalize(); let right = Dir3::cross(up.normalize(), dir).normalize(); - let new_up = Dir3::cross(dir, right); + let new_up = Dir3::cross(right, dir); let mat = [ [right.x(), new_up.x(), dir.x(), pos.x()], diff --git a/ray-tracing-core/src/camera.rs b/ray-tracing-core/src/camera.rs index bc202ba..b5751eb 100644 --- a/ray-tracing-core/src/camera.rs +++ b/ray-tracing-core/src/camera.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::{affine_transform::AffineTransform, prelude::*}; pub trait Camera { fn forward(&self, x: u32, y: u32, rng: &mut R) -> Ray; @@ -41,6 +41,25 @@ impl BasicCamera { } } + pub fn from_affine_transform( + width: u32, + height: u32, + tm: AffineTransform, + horizontal_fov: Float, + ) -> Self { + assert!(horizontal_fov < FloatConsts::PI); + + let l = Float::sin(0.5 * horizontal_fov); + + Self { + width, + height, + pos: Pos3::new(tm.get(3, 0), tm.get(3, 1), tm.get(3, 2)), + dir: Dir3::new(tm.get(2, 0), tm.get(2, 1), tm.get(2, 2)), + h: l * Dir3::new(tm.get(0, 0), tm.get(0, 1), tm.get(0, 2)), + v: l * Dir3::new(tm.get(1, 0), tm.get(1, 1), tm.get(1, 2)), + } + } pub fn from_look_at( width: u32, height: u32, diff --git a/ray-tracing-pbrt-scene/src/lib.rs b/ray-tracing-pbrt-scene/src/lib.rs index 2344d78..5891908 100644 --- a/ray-tracing-pbrt-scene/src/lib.rs +++ b/ray-tracing-pbrt-scene/src/lib.rs @@ -46,7 +46,7 @@ impl Lexer { #[derive(Debug)] #[allow(dead_code)] -enum CameraType { +pub enum CameraType { Orthographic { frame_aspect_ratio: Option, screen_window: Option, @@ -64,8 +64,8 @@ enum CameraType { #[derive(Debug)] #[allow(dead_code)] -struct PbrtCamera { - camera_type: CameraType, +pub struct PbrtCamera { + pub camera_type: CameraType, shutter_open: Float, shutter_close: Float, } @@ -640,10 +640,9 @@ impl Pbrt { } #[derive(Debug)] -#[allow(dead_code)] pub struct PbrtWorldSettings { - camera: PbrtCamera, - camera_ctm: AffineTransform, + pub camera: PbrtCamera, + pub camera_ctm: AffineTransform, } #[derive(Debug)] @@ -790,7 +789,7 @@ fn inner_parse_pbrt( *context.ctm.last_mut().unwrap() = affine_transform } Statement::Shape(shape_type, shape_alpha) => { - dbg!(&context); + // dbg!(&context); if context.area_light.is_empty() { pbrt.scene.shapes.push(Shape { ctm: context.get_ctm(), diff --git a/ray-tracing-tev/src/main.rs b/ray-tracing-tev/src/main.rs index 93b69eb..6c4981e 100644 --- a/ray-tracing-tev/src/main.rs +++ b/ray-tracing-tev/src/main.rs @@ -7,7 +7,7 @@ use ray_tracing_core::{ renderer::ClassicalRenderer, scene::Scene, }; -use ray_tracing_pbrt_scene::parse_pbrt_v4; +use ray_tracing_pbrt_scene::{parse_pbrt_v4, CameraType}; use ray_tracing_renderer::{ depth_renderer::DepthRenderer, mis::MIS, next_event_estimation::NextEventEstimation, path_tracer::PathTracer, path_tracer_importance::PathTracerImportance, @@ -230,15 +230,29 @@ fn main() { } }; - let c = BasicCamera::from_look_at( + let fov = match pbrt.settings.camera.camera_type { + CameraType::Perspective { fov, .. } => 2.0 * fov, + _ => todo!(), + }; + + let c = BasicCamera::from_affine_transform( args.width, args.height, - Pos3::new(3.0, 4.0, 1.5), - Pos3::new(0.5, 0.5, 0.0), - Dir3::new(0.0, 0.0, 1.0), - Float::to_radians(90.0), + pbrt.settings.camera_ctm, + Float::to_radians(fov), ); + // let c = BasicCamera::from_look_at( + // args.width, + // args.height, + // Pos3::new(-0.3, 0.5, -0.5), + // Pos3::new(0.0, 0.75, 0.0), + // Dir3::new(0.0, 1.0, 0.0), + // Float::to_radians(2.0 * 37.0), + // ); + + // dbg!(c); + let s = &pbrt.scene; choose_renderer(