ai_pathtracer/rust/src/camera.rs
2026-09-26 08:13:33 +02:00

33 lines
1 KiB
Rust

use crate::scene::Ray;
use crate::vec3::Vec3;
pub struct Camera {
origin: Vec3,
lower_left_corner: Vec3,
horizontal: Vec3,
vertical: Vec3,
}
impl Camera {
pub fn new(lookfrom: Vec3, lookat: Vec3, vup: Vec3, vfov: f64, aspect: f64, focus_dist: f64) -> Self {
let half_height = (vfov.to_radians() / 2.0).tan();
let half_width = aspect * half_height;
let w = (lookfrom - lookat).normalize();
let u = vup.cross(w).normalize();
let v = w.cross(u);
let horizontal = focus_dist * half_width * u;
let vertical = focus_dist * half_height * v;
let lower_left_corner = lookfrom - horizontal / 2.0 - vertical / 2.0 - focus_dist * w;
Camera {
origin: lookfrom,
lower_left_corner,
horizontal,
vertical,
}
}
pub fn get_ray(&self, s: f64, t: f64) -> Ray {
let direction = self.lower_left_corner + s * self.horizontal + t * self.vertical - self.origin;
Ray::new(self.origin, direction)
}
}