Skip to content

Commit

Permalink
Merge pull request #52 from davids91/optimization/redefine-leaf
Browse files Browse the repository at this point in the history
Resolves #29 - Implemented handling of homogeneous nodes
  • Loading branch information
davids91 authored Oct 20, 2024
2 parents 877bf22 + b0ca9c0 commit cefb871
Show file tree
Hide file tree
Showing 23 changed files with 3,310 additions and 1,206 deletions.
589 changes: 342 additions & 247 deletions assets/shaders/viewport_render.wgsl

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions examples/dot_cube.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fn handle_zoom(
mut viewing_glass: ResMut<ShocoVoxViewingGlass>,
mut angles_query: Query<&mut DomePosition>,
) {
const ADDITION: f32 = 0.05;
const ADDITION: f32 = 0.005;
let angle_update_fn = |angle, delta| -> f32 {
let new_angle = angle + delta;
if new_angle < 360. {
Expand All @@ -192,6 +192,11 @@ fn handle_zoom(
0.
}
};
let multiplier = if keys.pressed(KeyCode::ShiftLeft) {
10.0 // Doesn't have any effect?!
} else {
1.0
};
if keys.pressed(KeyCode::ArrowUp) {
angles_query.single_mut().roll = angle_update_fn(angles_query.single().roll, ADDITION);
}
Expand All @@ -207,10 +212,18 @@ fn handle_zoom(
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 0.9;
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
}
if keys.pressed(KeyCode::PageDown) {
angles_query.single_mut().radius *= 1.1;
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
}
if keys.pressed(KeyCode::Home) {
viewing_glass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
viewing_glass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
}
if keys.pressed(KeyCode::End) {
viewing_glass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
viewing_glass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
}
}

Expand Down
108 changes: 97 additions & 11 deletions examples/minecraft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use bevy::{prelude::*, window::WindowPlugin};
#[cfg(feature = "bevy_wgpu")]
use shocovox_rs::octree::{
raytracing::{
bevy::create_viewing_glass, ShocoVoxRenderPlugin, ShocoVoxViewingGlass, Viewport,
bevy::create_viewing_glass, Ray, ShocoVoxRenderPlugin, ShocoVoxViewingGlass, Viewport,
},
Albedo, V3c,
Albedo, V3c, VoxelData,
};

#[cfg(feature = "bevy_wgpu")]
Expand All @@ -22,6 +22,18 @@ use iyes_perf_ui::{
#[cfg(feature = "bevy_wgpu")]
const DISPLAY_RESOLUTION: [u32; 2] = [1024, 768];

#[cfg(feature = "bevy_wgpu")]
const BRICK_DIMENSION: usize = 32;

#[cfg(feature = "bevy_wgpu")]
#[derive(Resource)]
struct TreeResource<T, const DIM: usize>
where
T: Default + Clone + PartialEq + VoxelData,
{
tree: Octree<T, DIM>,
}

#[cfg(feature = "bevy_wgpu")]
fn main() {
App::new()
Expand Down Expand Up @@ -53,9 +65,11 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
let tree;
let tree_path = "example_junk_minecraft";
if std::path::Path::new(tree_path).exists() {
tree = Octree::<Albedo, 16>::load(&tree_path).ok().unwrap();
tree = Octree::<Albedo, BRICK_DIMENSION>::load(&tree_path)
.ok()
.unwrap();
} else {
tree = match shocovox_rs::octree::Octree::<Albedo, 16>::load_vox_file(
tree = match shocovox_rs::octree::Octree::<Albedo, BRICK_DIMENSION>::load_vox_file(
"assets/models/minecraft.vox",
) {
Ok(tree_) => tree_,
Expand All @@ -78,11 +92,7 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
let render_data = tree.create_bevy_view();
let viewing_glass = create_viewing_glass(
&Viewport {
origin: V3c {
x: 0.,
y: 0.,
z: 0.,
},
origin,
direction: V3c {
x: 0.,
y: 0.,
Expand All @@ -102,6 +112,7 @@ fn setup(mut commands: Commands, images: ResMut<Assets<Image>>) {
..default()
});
commands.spawn(Camera2dBundle::default());
commands.insert_resource(TreeResource { tree });
commands.insert_resource(render_data);
commands.insert_resource(viewing_glass);

Expand Down Expand Up @@ -154,6 +165,7 @@ fn handle_zoom(
keys: Res<ButtonInput<KeyCode>>,
mut viewing_glass: ResMut<ShocoVoxViewingGlass>,
mut angles_query: Query<&mut DomePosition>,
tree: Res<TreeResource<Albedo, BRICK_DIMENSION>>,
) {
const ADDITION: f32 = 0.05;
let angle_update_fn = |angle, delta| -> f32 {
Expand All @@ -164,6 +176,72 @@ fn handle_zoom(
0.
}
};
if keys.pressed(KeyCode::Tab) {
// Render the current view with CPU
let viewport_up_direction = V3c::new(0., 1., 0.);
let viewport_right_direction = viewport_up_direction
.cross(viewing_glass.viewport.direction)
.normalized();
let pixel_width = viewing_glass.viewport.w_h_fov.x as f32 / DISPLAY_RESOLUTION[0] as f32;
let pixel_height = viewing_glass.viewport.w_h_fov.y as f32 / DISPLAY_RESOLUTION[1] as f32;
let viewport_bottom_left = viewing_glass.viewport.origin
+ (viewing_glass.viewport.direction * viewing_glass.viewport.w_h_fov.z)
- (viewport_up_direction * (viewing_glass.viewport.w_h_fov.y / 2.))
- (viewport_right_direction * (viewing_glass.viewport.w_h_fov.x / 2.));

// define light
let diffuse_light_normal = V3c::new(0., -1., 1.).normalized();

use image::ImageBuffer;
use image::Rgb;
let mut img = ImageBuffer::new(DISPLAY_RESOLUTION[0], DISPLAY_RESOLUTION[1]);

// cast each ray for a hit
for x in 0..DISPLAY_RESOLUTION[0] {
for y in 0..DISPLAY_RESOLUTION[1] {
let actual_y_in_image = DISPLAY_RESOLUTION[1] - y - 1;
//from the origin of the camera to the current point of the viewport
let glass_point = viewport_bottom_left
+ viewport_right_direction * x as f32 * pixel_width
+ viewport_up_direction * y as f32 * pixel_height;
let ray = Ray {
origin: viewing_glass.viewport.origin,
direction: (glass_point - viewing_glass.viewport.origin).normalized(),
};

use std::io::Write;
std::io::stdout().flush().ok().unwrap();

if let Some(hit) = tree.tree.get_by_ray(&ray) {
let (data, _, normal) = hit;
//Because both vector should be normalized, the dot product should be 1*1*cos(angle)
//That means it is in range -1, +1, which should be accounted for
let diffuse_light_strength =
1. - (normal.dot(&diffuse_light_normal) / 2. + 0.5);
img.put_pixel(
x,
actual_y_in_image,
Rgb([
(data.r as f32 * diffuse_light_strength) as u8,
(data.g as f32 * diffuse_light_strength) as u8,
(data.b as f32 * diffuse_light_strength) as u8,
]),
);
} else {
img.put_pixel(x, actual_y_in_image, Rgb([128, 128, 128]));
}
}
}

img.save("example_junk_cpu_render.png").ok().unwrap();
}

let multiplier = if keys.pressed(KeyCode::ShiftLeft) {
10.0 // Doesn't have any effect?!
} else {
1.0
};

if keys.pressed(KeyCode::ArrowUp) {
angles_query.single_mut().roll = angle_update_fn(angles_query.single().roll, ADDITION);
}
Expand All @@ -179,10 +257,18 @@ fn handle_zoom(
// println!("viewport: {:?}", viewing_glass.viewport);
}
if keys.pressed(KeyCode::PageUp) {
angles_query.single_mut().radius *= 0.9;
angles_query.single_mut().radius *= 1. - 0.02 * multiplier;
}
if keys.pressed(KeyCode::PageDown) {
angles_query.single_mut().radius *= 1.1;
angles_query.single_mut().radius *= 1. + 0.02 * multiplier;
}
if keys.pressed(KeyCode::Home) {
viewing_glass.viewport.w_h_fov.x *= 1. + 0.09 * multiplier;
viewing_glass.viewport.w_h_fov.y *= 1. + 0.09 * multiplier;
}
if keys.pressed(KeyCode::End) {
viewing_glass.viewport.w_h_fov.x *= 1. - 0.09 * multiplier;
viewing_glass.viewport.w_h_fov.y *= 1. - 0.09 * multiplier;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/object_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ where
&mut self.buffer[key].item
}

pub(crate) fn swap(&mut self, src: usize, dst: usize) {
self.buffer.swap(src, dst);
}

pub(crate) fn key_is_valid(&self, key: usize) -> bool {
key < self.buffer.len() && self.buffer[key].reserved
}
Expand Down
Loading

0 comments on commit cefb871

Please sign in to comment.