Use camera settings from pbrt file

This commit is contained in:
hal8174 2025-09-12 21:58:58 +02:00
parent 6403aabc11
commit 0a9748a1b7
Signed by: hal8174
SSH key fingerprint: SHA256:NN98ZYwnrreQLSOV/g+amY7C3yL/mS1heD7bi5t6PPw
4 changed files with 51 additions and 15 deletions

View file

@ -40,6 +40,10 @@ impl AffineTransform {
Some(Self { mat, inv }) Some(Self { mat, inv })
} }
pub fn get(&self, i: usize, j: usize) -> Float {
self.mat[j][i]
}
pub fn identity() -> Self { pub fn identity() -> Self {
Self { Self {
mat: [ mat: [
@ -127,7 +131,7 @@ impl AffineTransform {
pub fn look_at(pos: Pos3, look: Pos3, up: Dir3) -> Option<Self> { pub fn look_at(pos: Pos3, look: Pos3, up: Dir3) -> Option<Self> {
let dir = (look - pos).normalize(); let dir = (look - pos).normalize();
let right = Dir3::cross(up.normalize(), dir).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 = [ let mat = [
[right.x(), new_up.x(), dir.x(), pos.x()], [right.x(), new_up.x(), dir.x(), pos.x()],

View file

@ -1,4 +1,4 @@
use crate::prelude::*; use crate::{affine_transform::AffineTransform, prelude::*};
pub trait Camera<R: Rng> { pub trait Camera<R: Rng> {
fn forward(&self, x: u32, y: u32, rng: &mut R) -> Ray; 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( pub fn from_look_at(
width: u32, width: u32,
height: u32, height: u32,

View file

@ -46,7 +46,7 @@ impl Lexer {
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] #[allow(dead_code)]
enum CameraType { pub enum CameraType {
Orthographic { Orthographic {
frame_aspect_ratio: Option<Float>, frame_aspect_ratio: Option<Float>,
screen_window: Option<Float>, screen_window: Option<Float>,
@ -64,8 +64,8 @@ enum CameraType {
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)] #[allow(dead_code)]
struct PbrtCamera { pub struct PbrtCamera {
camera_type: CameraType, pub camera_type: CameraType,
shutter_open: Float, shutter_open: Float,
shutter_close: Float, shutter_close: Float,
} }
@ -640,10 +640,9 @@ impl<R: Rng> Pbrt<R> {
} }
#[derive(Debug)] #[derive(Debug)]
#[allow(dead_code)]
pub struct PbrtWorldSettings { pub struct PbrtWorldSettings {
camera: PbrtCamera, pub camera: PbrtCamera,
camera_ctm: AffineTransform, pub camera_ctm: AffineTransform,
} }
#[derive(Debug)] #[derive(Debug)]
@ -790,7 +789,7 @@ fn inner_parse_pbrt<R: Rng + std::fmt::Debug>(
*context.ctm.last_mut().unwrap() = affine_transform *context.ctm.last_mut().unwrap() = affine_transform
} }
Statement::Shape(shape_type, shape_alpha) => { Statement::Shape(shape_type, shape_alpha) => {
dbg!(&context); // dbg!(&context);
if context.area_light.is_empty() { if context.area_light.is_empty() {
pbrt.scene.shapes.push(Shape { pbrt.scene.shapes.push(Shape {
ctm: context.get_ctm(), ctm: context.get_ctm(),

View file

@ -7,7 +7,7 @@ use ray_tracing_core::{
renderer::ClassicalRenderer, renderer::ClassicalRenderer,
scene::Scene, scene::Scene,
}; };
use ray_tracing_pbrt_scene::parse_pbrt_v4; use ray_tracing_pbrt_scene::{parse_pbrt_v4, CameraType};
use ray_tracing_renderer::{ use ray_tracing_renderer::{
depth_renderer::DepthRenderer, mis::MIS, next_event_estimation::NextEventEstimation, depth_renderer::DepthRenderer, mis::MIS, next_event_estimation::NextEventEstimation,
path_tracer::PathTracer, path_tracer_importance::PathTracerImportance, 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.width,
args.height, args.height,
Pos3::new(3.0, 4.0, 1.5), pbrt.settings.camera_ctm,
Pos3::new(0.5, 0.5, 0.0), Float::to_radians(fov),
Dir3::new(0.0, 0.0, 1.0),
Float::to_radians(90.0),
); );
// 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; let s = &pbrt.scene;
choose_renderer( choose_renderer(