From f7bb59cfeded24dc58359243b29978b5810ab341 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 11 Dec 2020 21:05:34 +0100 Subject: [PATCH] shading --- src/main.rs | 5 +---- src/object.rs | 2 +- src/render.rs | 34 ++++++++++++++++++++-------------- src/sphere.rs | 5 +++-- 4 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9da117c..e20a28c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,6 @@ use sdl2::event::Event; use sdl2::keyboard::Keycode; use sdl2::pixels::PixelFormatEnum; use sdl2::surface::Surface; -use std::time::Duration; fn main() { let sdl_context = sdl2::init().unwrap(); @@ -31,7 +30,7 @@ fn main() { // let surface = Surface::from_data(&mut data, 800, 600, 3, PixelFormatEnum::RGB24).unwrap(); let texture_creator = canvas.texture_creator(); let mut event_pump = sdl_context.event_pump().unwrap(); - let mut i = 0; + // let mut i = 0; 'running: loop { render(&mut *data); // put_pixel( @@ -67,7 +66,5 @@ fn main() { _ => {} } } - // The rest of the game loop goes here... - // ::std::thread::sleep(Duration::new(0, 1_000_000_000u32 / 60)); } } diff --git a/src/object.rs b/src/object.rs index c9b41e6..cec659c 100644 --- a/src/object.rs +++ b/src/object.rs @@ -2,5 +2,5 @@ use super::ray::Ray; pub trait Object { fn get_intersection(&self, ray: Ray) -> Option; - fn get_normal(&self) -> [f64; 3]; + fn get_normal(&self, n: [f64; 3]) -> [f64; 3]; } diff --git a/src/render.rs b/src/render.rs index 278bc45..40f2f41 100644 --- a/src/render.rs +++ b/src/render.rs @@ -6,11 +6,6 @@ use crate::ray::Ray; use crate::sphere::Sphere; use sdl2::pixels::Color; -struct Camera { - rv: [f64; 3], - uv: [f64; 3], -} - fn put_pixel(data: &mut [u8], x: usize, y: usize, color: Color) { data[y * 3 * 800 + x * 3] = color.r; data[y * 3 * 800 + x * 3 + 1] = color.g; @@ -36,10 +31,6 @@ pub fn render(data: &mut [u8]) { &mat4::new_identity(), &vec3::new(1., 1., 0.), ); - let camera = Camera { - rv: [1., 0., 0.], - uv: [0., 1., 0.], - }; for x in 0..800 { for y in 0..600 { let mut ray = Ray { @@ -54,21 +45,36 @@ pub fn render(data: &mut [u8]) { ray.direction[0] /= n; ray.direction[1] /= n; ray.direction[2] /= n; - let ray2 = Box::clone(&Box::new(ray)); + // let ray2 = Box::clone(&Box::new(ray)); // vec3::transform_mat4(&mut ray.direction, &ray2.direction, &projection_matrix); + let ray2 = Box::clone(&Box::new(ray)); vec3::transform_mat4(&mut ray.origin, &ray2.origin, &view_matrix); let ray2 = Box::clone(&Box::new(ray)); vec3::transform_mat4(&mut ray.origin, &ray2.origin, &model_matrix); match sphere.get_intersection(ray) { - Some(v) => { + Some(dist) => { + let n = [ + ray.origin[0] + ray.direction[0] * dist, + ray.origin[1] + ray.direction[1] * dist, + ray.origin[2] + ray.direction[2] * dist, + ]; + let normal = sphere.get_normal(n); + let light = [n[0] - 10., n[1] - 10., n[2] - 10.]; + let scalar = (light[0].powi(2) + light[1].powi(2) + light[2].powi(2)).sqrt(); + let light_d = [light[0] / scalar, light[1] / scalar, light[2] / scalar]; + let mut diffuse = + normal[0] * light_d[0] + normal[1] * light_d[1] + normal[2] * light_d[2]; + if diffuse < 0. { + diffuse = 0.; + } put_pixel( &mut *data, x, y, Color { - r: 255, - g: 0, - b: 0, + r: (diffuse * 255.) as u8, + g: (diffuse * 255.) as u8, + b: (diffuse * 255.) as u8, a: 0, }, ); diff --git a/src/sphere.rs b/src/sphere.rs index 11a560f..ab78bde 100644 --- a/src/sphere.rs +++ b/src/sphere.rs @@ -31,7 +31,8 @@ impl Object for Sphere { None } } - fn get_normal(&self) -> [f64; 3] { - vec3::new_zero() + fn get_normal(&self, n: [f64; 3]) -> [f64; 3] { + let scalar = (n[0].powi(2) + n[1].powi(2) + n[2].powi(2)).sqrt(); + [n[0] / scalar, n[1] / scalar, n[2] / scalar] } }