Removed pixels

Finished raytracer
Reimplementing imgui
This commit is contained in:
STP
2023-11-15 16:56:19 -05:00
parent 2e69164dde
commit 80fcc6c8bc
8 changed files with 1159 additions and 658 deletions

View File

@@ -5,20 +5,38 @@ use crate::{
scene::Scene,
INFINITY,
};
use lazy_static::lazy_static;
lazy_static! {
static ref VEC3_ONE: Vector3<f32> = Vector3::new(1.0, 1.0, 1.0);
static ref VEC3_ZERO: Vector3<f32> = Vector3::new(1.0, 1.0, 1.0);
}
use std::sync::Arc;
use nalgebra::{distance, Matrix4, Point3, Vector3, Vector4};
pub fn shade_rays(scene: &Scene, rays: &Vec<Ray>, width: u32, height: u32) -> Vec<Vector3<u8>> {
let mut pixel_data = vec![];
for ray in rays {
let intersect = get_closest_intersection(scene, ray);
match intersect {
Some(interect) => {
let colour = phong_shade_point(scene, &interect);
pixel_data.push(colour);
}
None => {
let colour = Vector3::new(0, 0, 0);
pixel_data.push(colour);
}
}
}
pixel_data
}
// Find the closest intersection, given a ray in world coordinates
fn get_closest_intersection<'a>(scene: &'a Scene, ray: &Ray) -> Option<Intersection<'a>> {
pub fn get_closest_intersection(scene: &Scene, ray: &Ray) -> Option<Intersection> {
let mut closest_distance = INFINITY;
let mut closest_intersect: Option<Intersection> = None;
for primitive in &scene.primitives {
if primitive.intersect_bounding_box(ray) == None {
for arc_primitive in &scene.primitives {
let primitive = arc_primitive.clone();
if primitive.intersect_ray(ray).is_none() {
continue;
};
@@ -37,34 +55,36 @@ fn get_closest_intersection<'a>(scene: &'a Scene, ray: &Ray) -> Option<Intersect
}
// We want to shade a point placed in our scene
fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3<f32> {
pub fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3<u8> {
//Useful vectors !!!! CHECK IF WE CAN OPTIMISE
let zero_vector = Vector3::new(0.0, 0.0, 0.0);
let one_vector = Vector3::new(1.0, 1.0, 1.0);
//Unpack the intersection data
let Intersection {
primitive,
point,
normal,
incidence,
material,
..
} = intersect;
let ambient_light = scene.ambient_light;
let material = primitive.get_material();
let binding = scene.ambient_light.clone();
let ambient_light = binding.as_ref();
let kd = material.kd;
let ks = material.ks;
let shininess = material.shininess;
// We should now have all the information for our ray-tracer
// Let us first compute the ambient light component and set it as out base colour
let mut colour = kd.component_mul(&ambient_light);
let mut colour = kd.component_mul(ambient_light);
for arc_light in &scene.lights {
let light = arc_light.clone();
for light in &scene.lights {
let Light {
position: light_position,
colour: light_colour,
falloff: light_falloff,
} = light;
} = light.as_ref();
// Get light incidence vector
let to_light = light_position - point;
@@ -96,5 +116,9 @@ fn phong_shade_point(scene: &Scene, intersect: &Intersection) -> Vector3<f32> {
colour += light_colour.component_mul(&((diffuse + specular) * falloff));
}
nalgebra::clamp(colour, zero_vector, one_vector)
let r = nalgebra::clamp(colour.x * 255.0, 0.0, 255.0) as u8;
let g = nalgebra::clamp(colour.y * 255.0, 0.0, 255.0) as u8;
let b = nalgebra::clamp(colour.z * 255.0, 0.0, 255.0) as u8;
Vector3::new(r, g, b)
}