Files
rustpt/src/camera.rs
Robert Garrett 5cc0b49cd9 Mid-Material save point
I get lazy with commits when following guided material. There's a design
challenge to approach, now. I'm saving here so I can revert changes in
case it goes sideways.

Materials are proving to be a little complicated in Rust semantics. The
Ray Tracing in a Weekend book uses shared_ptr's, but Rust doesn't really
like that. I'm doing references with lifetime annotations. Hopefully I
can get that the right way around to work out.

The materials themselves look like reasonable candidates for describing
as Enums. This takes away the ability to add new ones by simply impl'ing
a trait, but that was never gonna happen anyway. The code would be
modified and recompiled. There's no difference in maintenance cost if
that's a new struct impl'ing a trait, or adding enum members.
2023-06-03 09:48:54 -05:00

53 lines
1.3 KiB
Rust

/*
* let viewport = (aspect_ratio * 2.0, 2.0);
let focal_length = 1.0;
let origin = Vec3::new(0.0, 0.0, 0.0);
let horizontal = Vec3::new(viewport.0, 0.0, 0.0);
let vertical = Vec3::new(0.0, viewport.1, 0.0);
let lower_left_corner = origin - horizontal/2.0 - vertical/2.0 - Vec3::new(0.0, 0.0, focal_length);
*/
use crate::vec3::Vec3;
use crate::ray::Ray;
pub struct Camera {
pub origin: Vec3,
pub lower_left_corner: Vec3,
pub horizontal: Vec3,
pub vertical: Vec3,
}
impl Camera {
pub fn new() -> Camera {
let aspect_ratio = 16.0 / 9.0;
let vp_height = 2.0;
let vp_width = aspect_ratio * vp_height;
let focal_length = 1.0;
let horiz = Vec3::new(vp_width, 0.0, 0.0);
let verti = Vec3::new(0.0, vp_height, 0.0);
let orig = Vec3::zero();
Camera{
origin: orig,
lower_left_corner: orig - horiz/2.0 - verti/2.0 - Vec3::new(0.0, 0.0, focal_length),
horizontal: horiz,
vertical: verti,
}
}
pub fn get_ray(&self, u: f32, v: f32) -> Ray {
let dir = self.lower_left_corner
+ self.horizontal * u
+ self.vertical * v
- self.origin;
Ray{
orig: self.origin,
dir,
}
}
}