Changed to unit vectors

This commit is contained in:
STP
2023-11-17 15:11:08 -05:00
parent 7d3426254b
commit ab3705d575

View File

@@ -1,11 +1,7 @@
use crate::ray::Ray;
use crate::{EPSILON, INFINITY};
use log::error;
use nalgebra as nm;
use nalgebra::Matrix4;
use nalgebra::Perspective3;
use nalgebra::Point3;
use nalgebra::Vector3;
use nalgebra::{Matrix4, Perspective3, Point3, Unit, Vector3};
use std::env;
#[rustfmt::skip]
@@ -68,15 +64,21 @@ impl Camera {
let inverse = view.try_inverse().expect("No view") * proj.inverse();
(matrix, inverse)
}
pub fn cast_rays(&self, width: i32, height: i32) -> Vec<Ray> {
//All good
let aspect = width as f64 / height as f64;
let fovy_radians = (self.fovy as f64).to_radians();
let fovh_radians = 2.0 * ((fovy_radians / 2.0).tan() * aspect).atan();
let view_direction = (self.target - self.eye).normalize(); // Normalize the view direction vector
// All good
let view_direction = self.target - self.eye;
//All good
let hor = view_direction.cross(&self.up).normalize(); // pointing right
let vert = view_direction.cross(&hor).normalize(); // pointing up
//All good
let h_width = 2.0 * (fovh_radians / 2.0).tan();
let v_height = 2.0 * (fovy_radians / 2.0).tan();
//All good
let d_hor_vec = hor * (h_width / width as f64) as f32;
let d_vert_vec = vert * (v_height / height as f64) as f32;
@@ -84,10 +86,11 @@ impl Camera {
for j in 0..height {
for i in 0..width {
let horizontal = (i as f32 - width as f32 / 2.0) * d_hor_vec;
let vertical = (j as f32 - height as f32 / 2.0) * d_vert_vec;
let horizontal = (i as f32 - width as f32 / 2.0) * (d_hor_vec);
let vertical = (j as f32 - height as f32 / 2.0) * (d_vert_vec);
let direction = view_direction + horizontal + vertical;
let ray = Ray::new(self.eye, direction);
let ray = Ray::new(self.eye, Unit::new_normalize(direction));
rays.push(ray);
}
}
@@ -113,7 +116,9 @@ impl Camera {
let vertical = ((y as f32 / height as f32) - 0.5) * v_height as f32;
// Calculate the ray direction by summing up the components
let direction = view_direction + (horizontal * d_hor_vec) + (vertical * d_vert_vec);
let direction = Unit::new_normalize(
view_direction + (horizontal * d_hor_vec) + (vertical * d_vert_vec),
);
Ray::new(self.eye, direction)
}