Bvh added
This commit is contained in:
+72
-23
@@ -1,5 +1,6 @@
|
||||
//Use linear algebra module
|
||||
|
||||
use crate::bvh::BVH;
|
||||
use crate::camera::Camera;
|
||||
use crate::ray::Ray;
|
||||
use crate::{gui::Gui, scene::Scene};
|
||||
@@ -21,16 +22,45 @@ use winit::window::{Window, WindowBuilder};
|
||||
|
||||
const START_WIDTH: i32 = 1200;
|
||||
const START_HEIGHT: i32 = 700;
|
||||
const RAY_SAMPLES: i8 = 5;
|
||||
const RAY_RANDOMNESS: f64 = 100.0;
|
||||
const COLOUR_CLEAR: [u8; 4] = [0x22, 0x00, 0x11, 0x55];
|
||||
const PIXEL_CLEAR: [u8; 4] = [0x55, 0x00, 0x22, 0x55];
|
||||
|
||||
pub const INIT_FILE: &str = "rhai/scene.rhai";
|
||||
pub const SAVE_FILE: &str = "img.png";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct RaytracingOption {
|
||||
pub ray_samples: u32,
|
||||
pub ray_randomness: f64,
|
||||
pub clear_color: [u8; 4],
|
||||
pub pixel_clear: [u8; 4],
|
||||
pub rays_per_pass: u32,
|
||||
pub buffer_proportion: f32,
|
||||
pub buffer_fov: f64,
|
||||
pub ray_depth: u8,
|
||||
pub diffuse_rays: u8,
|
||||
pub diffuse_coefficient: f32,
|
||||
pub bvh_active: bool,
|
||||
}
|
||||
impl RaytracingOption {
|
||||
pub fn default() -> RaytracingOption {
|
||||
RaytracingOption {
|
||||
ray_samples: 10,
|
||||
ray_randomness: 100.0,
|
||||
clear_color: [0x22, 0x00, 0x11, 0x55],
|
||||
pixel_clear: [0x55, 0x00, 0x22, 0x55],
|
||||
rays_per_pass: 200,
|
||||
buffer_proportion: 1.0,
|
||||
buffer_fov: 110.0,
|
||||
ray_depth: 5,
|
||||
diffuse_rays: 5,
|
||||
diffuse_coefficient: 0.5,
|
||||
bvh_active: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
scene: Scene,
|
||||
bvh: Option<BVH>,
|
||||
camera: Camera,
|
||||
window: Window,
|
||||
|
||||
@@ -42,6 +72,7 @@ pub struct State {
|
||||
|
||||
rays: Vec<Ray>,
|
||||
ray_queue: Vec<usize>,
|
||||
raytracing_options: RaytracingOption,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -53,39 +84,46 @@ impl State {
|
||||
|
||||
Self {
|
||||
scene,
|
||||
bvh: None,
|
||||
camera,
|
||||
window,
|
||||
buffer_width: window_size.width as u32,
|
||||
buffer_height: window_size.height as u32,
|
||||
pixels: pixels,
|
||||
pixels,
|
||||
gui,
|
||||
rays,
|
||||
ray_queue: Vec::new(),
|
||||
raytracing_options: RaytracingOption::default(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
if let Some(event) = self.gui.event.take() {
|
||||
match event {
|
||||
GuiEvent::BufferResize(proportion, fov) => {
|
||||
self.resize_buffer(proportion, fov as f64)?
|
||||
GuiEvent::RaytracerOption(options) => {
|
||||
self.raytracing_options = options;
|
||||
match self.raytracing_options.bvh_active {
|
||||
true => self.bvh = Some(BVH::build(&mut self.scene.nodes)),
|
||||
false => self.bvh = None,
|
||||
}
|
||||
self.resize_buffer()?
|
||||
}
|
||||
GuiEvent::CameraUpdate(camera, fovy) => {
|
||||
GuiEvent::CameraUpdate(camera) => {
|
||||
self.rays = Ray::cast_rays(
|
||||
&camera.eye,
|
||||
&camera.target,
|
||||
&camera.up,
|
||||
fovy as f64,
|
||||
self.raytracing_options.buffer_fov,
|
||||
self.buffer_width,
|
||||
self.buffer_height,
|
||||
);
|
||||
self.camera = camera;
|
||||
self.clear()?;
|
||||
self.clear_buffer()?;
|
||||
self.reset_queue();
|
||||
}
|
||||
GuiEvent::SceneLoad(scene) => {
|
||||
self.scene = scene;
|
||||
self.clear()?;
|
||||
self.clear_buffer()?;
|
||||
self.reset_queue();
|
||||
}
|
||||
GuiEvent::SaveImage(filename) => {
|
||||
@@ -103,14 +141,16 @@ impl State {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn resize_buffer(&mut self, proportion: f32, fovy: f64) -> Result<(), Box<dyn Error>> {
|
||||
fn resize_buffer(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
// Calculate new buffer dimensions based on proportion
|
||||
let size = self.window.inner_size();
|
||||
let proportion = &self.raytracing_options.buffer_proportion;
|
||||
let fovy = self.raytracing_options.buffer_fov;
|
||||
self.buffer_width = (size.width as f32 * proportion) as u32;
|
||||
self.buffer_height = (size.height as f32 * proportion) as u32;
|
||||
|
||||
// Clear the buffer and reset the ray queue
|
||||
self.clear()?;
|
||||
self.clear_buffer()?;
|
||||
self.reset_queue();
|
||||
|
||||
// Recalculate rays with new buffer dimensions
|
||||
@@ -149,7 +189,10 @@ impl State {
|
||||
fn draw(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
//Draw ray_num in a block
|
||||
let frame = self.pixels.frame_mut();
|
||||
for _ in 0..self.gui.ray_num {
|
||||
let randomness = &self.raytracing_options.ray_randomness;
|
||||
let samples = &self.raytracing_options.ray_samples;
|
||||
let samples_f32 = *samples as f32;
|
||||
for _ in 0..self.raytracing_options.rays_per_pass {
|
||||
//Get random index from queue
|
||||
let index = match self.ray_queue.pop() {
|
||||
Some(index) => index,
|
||||
@@ -157,39 +200,45 @@ impl State {
|
||||
};
|
||||
//Shade colour for selected ray
|
||||
let mut colour = Vector3::zeros();
|
||||
for _ in 0..RAY_SAMPLES {
|
||||
for _ in 0..*samples {
|
||||
let ray = &self.rays[index];
|
||||
let point = ray.a;
|
||||
let dir = ray.b;
|
||||
let rx = (random::<f64>() - 0.5) / RAY_RANDOMNESS;
|
||||
let ry = (random::<f64>() - 0.5) / RAY_RANDOMNESS;
|
||||
let rz = (random::<f64>() - 0.5) / RAY_RANDOMNESS;
|
||||
let rx = (random::<f64>() - 0.5) / randomness;
|
||||
let ry = (random::<f64>() - 0.5) / randomness;
|
||||
let rz = (random::<f64>() - 0.5) / randomness;
|
||||
let nx = dir.x + rx;
|
||||
let ny = dir.y + ry;
|
||||
let nz = dir.z + rz;
|
||||
|
||||
let rand_ray = Ray::new(point, Vector3::new(nx, ny, nz));
|
||||
|
||||
if let Some(ray_colour) = rand_ray.shade_ray(&self.scene, 0) {
|
||||
if let Some(ray_colour) =
|
||||
rand_ray.shade_ray(&self.scene, 0, &self.raytracing_options, &self.bvh)
|
||||
{
|
||||
colour += ray_colour;
|
||||
};
|
||||
}
|
||||
colour = (colour / RAY_SAMPLES as f32) * 255.0;
|
||||
colour = (colour / samples_f32) * 255.0;
|
||||
let rgba = [colour.x as u8, colour.y as u8, colour.z as u8, 0xff];
|
||||
frame[index * 4..(index + 1) * 4].copy_from_slice(&rgba);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn clear(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
fn clear_buffer(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
let frame = self.pixels.frame_mut();
|
||||
for pixel in frame.chunks_exact_mut(4) {
|
||||
pixel.copy_from_slice(&COLOUR_CLEAR);
|
||||
pixel.copy_from_slice(&self.raytracing_options.pixel_clear);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn reset_queue(&mut self) {
|
||||
match self.raytracing_options.bvh_active {
|
||||
true => self.bvh = Some(BVH::build(&mut self.scene.nodes)),
|
||||
false => self.bvh = None,
|
||||
}
|
||||
let size = self.buffer_height as usize * self.buffer_width as usize;
|
||||
let mut ray_queue: Vec<usize> = (0..size).collect();
|
||||
ray_queue.shuffle(&mut thread_rng());
|
||||
@@ -234,7 +283,7 @@ pub fn run() -> Result<(), Box<dyn Error>> {
|
||||
let gui = Gui::new(&window, &pixels);
|
||||
|
||||
let mut state = State::new(window, pixels, gui);
|
||||
state.resize_buffer(1.0, 90.0)?;
|
||||
state.resize_buffer()?;
|
||||
|
||||
event_loop.run(move |event, _, control_flow| {
|
||||
state.gui.handle_event(&state.window, &event);
|
||||
|
||||
Reference in New Issue
Block a user