Added inverse matrix to camera

This commit is contained in:
STP
2023-11-16 01:30:35 -05:00
parent 94aea2678c
commit a80da71455
2 changed files with 67 additions and 8 deletions

View File

@@ -21,6 +21,8 @@ pub struct Camera {
aspect: f32, aspect: f32,
znear: f32, znear: f32,
zfar: f32, zfar: f32,
matrix: Matrix4<f32>,
inverse: Matrix4<f32>,
} }
impl Camera { impl Camera {
@@ -33,6 +35,8 @@ impl Camera {
) -> Self { ) -> Self {
let znear = EPSILON; let znear = EPSILON;
let zfar = INFINITY; let zfar = INFINITY;
let matrix = self.build_view_projection_matrix(eye, target, up, aspect, fovy, znear, zfar);
let inverse = self.build_inverse_view_projection_matrix(eye, target, up, aspect, fovy, znear, zfar);
Camera { Camera {
eye, eye,
target, target,
@@ -44,13 +48,13 @@ impl Camera {
} }
} }
pub fn build_view_projection_matrix(&self) -> Matrix4<f32> { pub fn build_view_projection_matrix(eye: Point3<f32>, target: Point3<f32>, up: Vector3<f32>, aspect: f32, fovy: f32, znear: f32, zfar: f32) -> Matrix4<f32> {
let view = Matrix4::look_at_lh(&self.eye, &self.target, &self.up); let view = Matrix4::look_at_lh(eye, target, up);
let proj = Matrix4::new_perspective(self.aspect, self.fovy, self.znear, self.zfar); let proj = Matrix4::new_perspective(aspect, fovy,znear, zfar);
proj * view proj * view
} }
pub fn build_inverse_view_projection_matrix(&self) -> Matrix4<f32> { pub fn build_inverse_view_projection_matrix(eye: Point3<f32>, target: Point3<f32>, up: Vector3<f32>, aspect: f32, fovy: f32, znear: f32, zfar: f32) -> Matrix4<f32> {
let view_proj = self.build_view_projection_matrix(); let view_proj = self.build_view_projection_matrix(eye, target, up, aspect, fovy, znear, zfar);
view_proj.try_inverse().expect("Cannot invert!") view_proj.try_inverse().expect("Cannot invert!")
} }
pub fn cast_rays(&self, width: u32, height: u32) -> Vec<Ray> { pub fn cast_rays(&self, width: u32, height: u32) -> Vec<Ray> {
@@ -75,4 +79,8 @@ impl Camera {
} }
rays rays
} }
pub fn cast_ray(&self, width: u32, height: u32, x: u32, y: u32) -> Ray {
let dx = 2.0 / width as f32;
let dy = 2.0 / height as f32;
}
} }

View File

@@ -5,19 +5,23 @@
//Cameras //Cameras
use crate::camera::Camera;
use crate::gui::Gui; use crate::gui::Gui;
use crate::light::Light;
use crate::primitive::*;
use crate::scene::Scene;
use error_iter::ErrorIter as _; use error_iter::ErrorIter as _;
use log::error; use log::error;
use nalgebra::{Point3, Vector3};
use pixels::{Error, Pixels, SurfaceTexture}; use pixels::{Error, Pixels, SurfaceTexture};
use std::sync::Arc;
use winit::dpi::LogicalSize; use winit::dpi::LogicalSize;
use winit::event::{Event, VirtualKeyCode}; use winit::event::{Event, VirtualKeyCode};
use winit::event_loop::{ControlFlow, EventLoop}; use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder; use winit::window::WindowBuilder;
use winit_input_helper::WinitInputHelper; use winit_input_helper::WinitInputHelper;
use crate::camera::Camera;
use crate::scene::Scene;
mod camera; mod camera;
mod gui; mod gui;
mod light; mod light;
@@ -156,6 +160,53 @@ impl State {
let x = (i % self.width as usize) as i16; let x = (i % self.width as usize) as i16;
let y = (i / self.width as usize) as i16; let y = (i / self.width as usize) as i16;
//Create our scene
let eye = Point3::new(0.0, 0.0, -1.0);
let target = Point3::new(0.0, 0.0, 0.0);
let up = Vector3::new(0.0, 1.0, 0.0);
let arc_camera = Arc::new(Camera::new(
eye,
target,
up,
90.0,
(self.width / self.height) as f32,
));
let cameras: Vec<Arc<Camera>> = vec![arc_camera.clone()];
let arc_material = Arc::new(Material::magenta());
let arc_cone = Arc::new(Cone::unit(arc_material));
let primitives: Vec<Arc<dyn Primitive>> = vec![arc_cone]
.into_iter()
.map(|arc| arc as Arc<dyn Primitive>)
.collect();
let light: Arc<Light>;
let light = Arc::new(Light::white());
let lights = vec![light];
let ambient_light = Arc::new(Vector3::new(1.0, 1.0, 1.0));
let scene = Scene::new(primitives, lights, cameras, ambient_light);
let rays = arc_camera.as_ref().cast_rays(self.width, self.height);
let colours = raytracer::shade_rays(&scene, &rays, self.width, self.height);
println!("{}", colours.len());
//
// let pixels = pixels.frame().chunks_exact_mut(4);
// for (i, colour) in colours.iter().enumerate() {
// let colour = colours[i];
// let pixel = &mut pixels[i];
// pixel[0] = colour.x;
// pixel[1] = colour.y;
// pixel[2] = colour.z;
// }
//
// // Render the frame
// if pixels.render().is_err() {
// eprintln!("Failed to render frame");
// }
let rgba = [0x48, 0xb2, 0xe8, 0xff]; let rgba = [0x48, 0xb2, 0xe8, 0xff];
pixel.copy_from_slice(&rgba); pixel.copy_from_slice(&rgba);