diff --git a/src/camera.rs b/src/camera.rs index 0d92805..7848470 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -21,6 +21,8 @@ pub struct Camera { aspect: f32, znear: f32, zfar: f32, + matrix: Matrix4, + inverse: Matrix4, } impl Camera { @@ -33,6 +35,8 @@ impl Camera { ) -> Self { let znear = EPSILON; 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 { eye, target, @@ -44,13 +48,13 @@ impl Camera { } } - pub fn build_view_projection_matrix(&self) -> Matrix4 { - let view = Matrix4::look_at_lh(&self.eye, &self.target, &self.up); - let proj = Matrix4::new_perspective(self.aspect, self.fovy, self.znear, self.zfar); + pub fn build_view_projection_matrix(eye: Point3, target: Point3, up: Vector3, aspect: f32, fovy: f32, znear: f32, zfar: f32) -> Matrix4 { + let view = Matrix4::look_at_lh(eye, target, up); + let proj = Matrix4::new_perspective(aspect, fovy,znear, zfar); proj * view } - pub fn build_inverse_view_projection_matrix(&self) -> Matrix4 { - let view_proj = self.build_view_projection_matrix(); + pub fn build_inverse_view_projection_matrix(eye: Point3, target: Point3, up: Vector3, aspect: f32, fovy: f32, znear: f32, zfar: f32) -> Matrix4 { + let view_proj = self.build_view_projection_matrix(eye, target, up, aspect, fovy, znear, zfar); view_proj.try_inverse().expect("Cannot invert!") } pub fn cast_rays(&self, width: u32, height: u32) -> Vec { @@ -75,4 +79,8 @@ impl Camera { } 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; + } } diff --git a/src/main.rs b/src/main.rs index 02a2220..6895ad9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,19 +5,23 @@ //Cameras +use crate::camera::Camera; use crate::gui::Gui; +use crate::light::Light; +use crate::primitive::*; +use crate::scene::Scene; + use error_iter::ErrorIter as _; use log::error; +use nalgebra::{Point3, Vector3}; use pixels::{Error, Pixels, SurfaceTexture}; +use std::sync::Arc; use winit::dpi::LogicalSize; use winit::event::{Event, VirtualKeyCode}; use winit::event_loop::{ControlFlow, EventLoop}; use winit::window::WindowBuilder; use winit_input_helper::WinitInputHelper; -use crate::camera::Camera; -use crate::scene::Scene; - mod camera; mod gui; mod light; @@ -156,6 +160,53 @@ impl State { let x = (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> = vec![arc_camera.clone()]; + + let arc_material = Arc::new(Material::magenta()); + let arc_cone = Arc::new(Cone::unit(arc_material)); + let primitives: Vec> = vec![arc_cone] + .into_iter() + .map(|arc| arc as Arc) + .collect(); + + let light: Arc; + 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]; pixel.copy_from_slice(&rgba);