ROTATION WORKS!!!!

This commit is contained in:
CDaut 2023-11-24 18:39:39 +01:00
parent 053a69e5b7
commit ab590a4844
7 changed files with 15 additions and 541 deletions

View file

@ -1,5 +1,5 @@
use std::ops::Mul;
use cgmath::{Angle, InnerSpace, Matrix4, SquareMatrix, Vector3, Vector4};
use cgmath::{Angle, InnerSpace, Matrix3, Matrix4, Point3, SquareMatrix, Vector3, Vector4};
use easy_gltf::{Camera, Projection};
pub struct Ray {
@ -15,24 +15,25 @@ pub fn construct_primary_rays(camera: &Camera,
//TODO: ignoring aspect ratio here
let (fovy, aspect_ratio) = match camera.projection {
Projection::Perspective { yfov, aspect_ratio } => {
(yfov, aspect_ratio) }
(yfov, aspect_ratio)
}
Projection::Orthographic { .. } => { panic!("Orthographic rendering not supported.") }
};
//ray origin in world space
let origin_world_space = camera.position();
//dbg!(camera.transform);
//use a custom transform matrix because the one from easy_gltf is fucked
let transform_matrix = Matrix4::from_cols(
camera.right().extend(0.0),
-camera.up().extend(0.0),
-camera.forward().extend(0.0),
origin_world_space.extend(1.0),
);
// the distance from the camera origin to the view plane
let z: f32 = height as f32 / (2.0 * fovy.mul(0.5).tan());
//obtain the inverse transformation Matrix
let inverse_transform = camera.transform.invert().unwrap_or_else(||
panic!("Non invertible transform Matrix. giving up.")
);
dbg!(camera.transform);
//TODO: take ray multiplier per pixel into account here
let mut rays: Vec<Ray> = Vec::with_capacity(height * width);
@ -41,7 +42,7 @@ pub fn construct_primary_rays(camera: &Camera,
rays.push(generate_single_primary_ray(
width,
height,
&inverse_transform,
&transform_matrix,
z,
pixel_x_coord,
pixel_y_coord,
@ -52,7 +53,7 @@ pub fn construct_primary_rays(camera: &Camera,
fn generate_single_primary_ray(image_width: usize,
image_height: usize,
inverse_transform: &Matrix4<f32>,
cam_to_world_transform: &Matrix4<f32>,
focal_length: f32,
u: usize,
v: usize,
@ -63,9 +64,8 @@ fn generate_single_primary_ray(image_width: usize,
v as f32 - (image_height as f32 / 2.0),
focal_length,
0.0);
//TODO: Rotation is fucked
//x rotation has sign wrong, y and z are flipped
let direction_world_space = inverse_transform * direction_view_space.normalize();
let direction_world_space = cam_to_world_transform * direction_view_space.normalize();
Ray { source: ray_origin, direction: direction_world_space.truncate().normalize() }
}