diff --git a/.vscode/launch.json b/.vscode/launch.json index b498c0b..003e43c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -7,23 +7,20 @@ { "type": "lldb", "request": "launch", - "name": "Debug", + "name": "Debug executable 'rustray'", "cargo": { "args": [ - "build" + "build", + "--bin=rustray", + "--package=rustray" ], "filter": { - "name": "rust_opengl", + "name": "rustray", "kind": "bin" } }, - "linux": { - "env": { - "RUST_BACKTRACE": "1" - } - }, "args": [], - "cwd": "${workspaceRoot}" + "cwd": "${workspaceFolder}" } ] } \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 1f13205..63c5cf2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ authors = ["hodasemi "] cgmath = "*" utilities = { git = "https://gitlab.com/hodasemi/utilities.git" } image = "*" +rand = "*" diff --git a/src/acceleration_data.rs b/src/acceleration_data.rs index c90ef51..8dc5734 100644 --- a/src/acceleration_data.rs +++ b/src/acceleration_data.rs @@ -57,7 +57,21 @@ impl<'a> AccelerationData<'a> { discovered[v] = true; - println!("{}\n{:?}", v, self.data[v]); + print_aabb(v, self.data[v]); + + let left_child = self.data[v].left_child; + if left_child != -1 { + if !discovered[left_child as usize] { + stack.push(left_child as usize); + } + } + + let right_child = self.data[v].right_child; + if right_child != -1 { + if !discovered[right_child as usize] { + stack.push(right_child as usize); + } + } } } } @@ -122,12 +136,12 @@ impl<'a> AccelerationData<'a> { let next_level_count = (current_level_count as f32 / 2.0).ceil() as u32; let next_level_start = start_index + current_level_count; - for i in next_level_start..(next_level_start + next_level_count) { + for i in 0..next_level_count { let mut aabb = AABB::new(); let index = (start_index + i * 2) as usize; - if i == (next_level_count + next_level_start - 1) && odd { + if i == (next_level_count - 1) && odd { aabb.bounds[0] = data[index].bounds[0]; aabb.bounds[1] = data[index].bounds[1]; aabb.left_child = index as isize; @@ -142,7 +156,7 @@ impl<'a> AccelerationData<'a> { data[0] = aabb; break; } else { - data[i as usize] = aabb; + data[(next_level_start + i) as usize] = aabb; } } @@ -163,3 +177,7 @@ impl<'a> AccelerationData<'a> { Vector3::new(v0.x.max(v1.x), v0.y.max(v1.y), v0.z.max(v1.z)) } } + +fn print_aabb(index: usize, aabb: AABB) { + println!("({}):\t{:?}", index, aabb); +} diff --git a/src/main.rs b/src/main.rs index ffea722..69a017a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use cgmath::{vec2, vec3, Vector3}; +use crate::rand::Rng; use image::ImageBuffer; use utilities::prelude::*; @@ -20,7 +21,11 @@ fn generate_grid(width: u32, height: u32) -> Vec { for y in 0..height { for x in 0..width { - let color = vec3(1.0, 0.0, 0.0); + let color = vec3( + rand::thread_rng().gen_range(0.0, 1.0), + rand::thread_rng().gen_range(0.0, 1.0), + rand::thread_rng().gen_range(0.0, 1.0), + ); grid.push(Triangle::new( Vertex::new(vec3(x as f32, y as f32, 0.0), vec2(0.0, 0.0)), @@ -42,7 +47,7 @@ fn generate_grid(width: u32, height: u32) -> Vec { } fn main() { - let input_data = generate_grid(3, 3); + let input_data = generate_grid(5, 5); /* let camera = Camera::new( @@ -74,6 +79,8 @@ fn debug_raytracer_camera(dim_x: u32, dim_y: u32, camera: &Camera, data: &[Trian let acceleration_data = AccelerationData::create_tree(data, 8); + acceleration_data.print_tree(); + for (x, y, pixel) in imgbuf.enumerate_pixels_mut() { let ray = camera.primary_ray(x, dim_y - y, dim_x, dim_y);