adds first connection between octtree volumes

This commit is contained in:
zomseffen 2025-02-18 16:36:48 +01:00
parent b559bd5e08
commit 4e50b1a12e
5 changed files with 374 additions and 109 deletions

View file

@ -7,6 +7,7 @@
use anyhow::{anyhow, Result};
use log::*;
use scene::generators;
use winit::dpi::{LogicalSize, LogicalPosition};
use winit::event::{ElementState, Event, WindowEvent};
use winit::event_loop::EventLoop;
@ -222,6 +223,7 @@ impl App {
image::create_texture_image_view(&device, &mut data)?;
image::create_texture_sampler(&device, &mut data)?;
generators::generate_test_scene(&mut scene_handler, &mut data)?;
scene_handler.prepare_data(&instance, &device, &mut data)?;
buffer::create_uniform_buffers(&instance, &device, &mut data)?;
@ -237,7 +239,7 @@ impl App {
cam_angle_x: 0.0, cam_angle_y: 0.0,
last_pos: LogicalPosition::new(-1 as f32, -1 as f32),
view_direction: vertex::Vec3::new(0.0, 0.0, 0.0),
cur_pos: cgmath::point3(0.0, 0.0, 0.0),
cur_pos: cgmath::point3(5.0, 5.0, 10.0),
scene_handler,
show_frame_rate: false,
synchronized: 0

View file

@ -1,6 +1,6 @@
use cgmath::Vector3;
use cgmath::{ElementWise, Vector3};
use std::cell::RefCell;
use std::cell::{RefCell, Ref};
use std::rc::Rc;
use std::time::Instant;
@ -24,6 +24,8 @@ pub struct EmptyVolume {
pub size_y: usize,
pub size_z: usize,
pub tree_offset: Vector3<usize>,
pub tree_size: usize,
pub position: Vector3<usize>,
pub color_left: Vec<Vector3<u8>>,
@ -63,22 +65,22 @@ impl EmptyVolume {
}
// MARK: From Oct Tree
pub fn from_oct_tree(tree: &OctTree<Cube>) -> (Vec<Rc<RefCell<EmptyVolume>>>, OctTree<Rc<RefCell<EmptyVolume>>>) {
pub fn from_oct_tree(tree: &Rc<RefCell<OctTree<Cube>>>, tree_offset: Vector3<usize>) -> (Vec<Rc<RefCell<EmptyVolume>>>, OctTree<Rc<RefCell<EmptyVolume>>>) {
// todo: ppotentially use a child exist check while going through the oct tree to find some obvios starting empty volumes. Will still need to check for possible expansions though
let mut volumes: Vec<Rc<RefCell<EmptyVolume>>> = vec![];
let mut neighbors: OctTree<Rc<RefCell<EmptyVolume>>> = OctTree::create(tree.size).unwrap();
let mut neighbors: OctTree<Rc<RefCell<EmptyVolume>>> = OctTree::create(tree.borrow().size).unwrap();
let start_time = Instant::now();
// iterate over all block positions in the oct tree
let mut check_its = 0;
let mut x_index = 0;
while x_index < tree.size {
while x_index < tree.borrow().size {
let mut y_index = 0;
while y_index < tree.size {
while y_index < tree.borrow().size {
let mut z_index = 0;
while z_index < tree.size {
while z_index < tree.borrow().size {
// check if there is a block at that position
let query_result = tree.test_element(x_index, y_index, z_index);
let query_result = tree.borrow().test_element(x_index, y_index, z_index);
let mut transparent = false;
let mut transparent_color = Vector3 {x: 0.0, y: 0.0, z: 0.0};
let mut tranparent_roughness = 0;
@ -116,12 +118,12 @@ impl EmptyVolume {
}
let mut grow = true;
while grow {
grow &= (x_index + x_size + 1) < tree.size;
grow &= (x_index + x_size + 1) < tree.borrow().size;
if grow {
let mut z = 0;
let mut y = 0;
while z < z_size.max(1) && y < y_size.max(1) {
let query_result = tree.test_element(x_index + x_size + 1, y_index + y, z_index + z);
let query_result = tree.borrow().test_element(x_index + x_size + 1, y_index + y, z_index + z);
check_its += 1;
grow &= ((!query_result.0 && !transparent) || (transparent && EmptyVolume::check_transparent(query_result.3, &transparent_color, &tranparent_roughness))) &&
neighbors.get_element(x_index + x_size + 1, y_index + y, z_index + z).is_none();
@ -163,12 +165,12 @@ impl EmptyVolume {
println!("start growing volume y");
grow = true;
while grow {
grow &= (y_index + y_size + 1) < tree.size;
grow &= (y_index + y_size + 1) < tree.borrow().size;
if grow {
let mut z = 0;
let mut x = 0;
while z < z_size.max(1) && x < x_size.max(1) {
let query_result = tree.test_element(x_index + x, y_index + y_size + 1, z_index + z);
let query_result = tree.borrow().test_element(x_index + x, y_index + y_size + 1, z_index + z);
check_its += 1;
grow &= ((!query_result.0 && !transparent) || (transparent && EmptyVolume::check_transparent(query_result.3, &transparent_color, &tranparent_roughness))) &&
neighbors.get_element(x_index + x, y_index + y_size + 1, z_index + z).is_none();
@ -211,12 +213,12 @@ impl EmptyVolume {
println!("start growing volume z");
grow = true;
while grow {
grow &= (z_index + z_size + 1) < tree.size;
grow &= (z_index + z_size + 1) < tree.borrow().size;
if grow {
let mut y = 0;
let mut x = 0;
while y < y_size.max(1) && x < x_size.max(1) {
let query_result = tree.test_element(x_index + x, y_index + y, z_index + z_size + 1);
let query_result = tree.borrow().test_element(x_index + x, y_index + y, z_index + z_size + 1);
check_its += 1;
grow &= ((!query_result.0 && !transparent) || (transparent && EmptyVolume::check_transparent(query_result.3, &transparent_color, &tranparent_roughness))) &&
neighbors.get_element(x_index + x, y_index + y, z_index + z_size + 1).is_none();
@ -262,6 +264,8 @@ impl EmptyVolume {
size_x: x_size + 1,
size_y: y_size + 1,
size_z: z_size + 1,
tree_offset,
tree_size: tree.borrow().size,
position: Vector3{x: x_index, y: y_index, z: z_index},
color_left: vec![],
color_right: vec![],
@ -330,7 +334,7 @@ impl EmptyVolume {
let mut bottom_elements_num = 0;
for x in 0..x_size+1 {
for y in 0..y_size+1 {
if let Some(c) = tree.get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_min_pos) {
if let Some(c) = tree.borrow().get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_min_pos) {
bottom_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
bottom_colors.push(u8_color);
@ -356,7 +360,7 @@ impl EmptyVolume {
let mut top_elements_num = 0;
for x in 0..x_size+1 {
for y in 0..y_size+1 {
if let Some(c) = tree.get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_max_pos) {
if let Some(c) = tree.borrow().get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_max_pos) {
top_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
top_colors.push(u8_color);
@ -383,7 +387,7 @@ impl EmptyVolume {
let mut back_elements_num = 0;
for x in 0..x_size+1 {
for z in 0..z_size+1 {
if let Some(c) = tree.get_element(reference.borrow().position.x + x, y_max_pos, reference.borrow().position.z + z) {
if let Some(c) = tree.borrow().get_element(reference.borrow().position.x + x, y_max_pos, reference.borrow().position.z + z) {
back_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
back_colors.push(u8_color);
@ -410,7 +414,7 @@ impl EmptyVolume {
let mut front_elements_num = 0;
for x in 0..x_size+1 {
for z in 0..z_size+1 {
if let Some(c) = tree.get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) {
if let Some(c) = tree.borrow().get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) {
front_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
front_colors.push(u8_color);
@ -437,7 +441,7 @@ impl EmptyVolume {
let mut left_elements_num = 0;
for y in 0..y_size+1 {
for z in 0..z_size+1 {
if let Some(c) = tree.get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
if let Some(c) = tree.borrow().get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
left_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
left_colors.push(u8_color);
@ -464,7 +468,7 @@ impl EmptyVolume {
let mut right_elements_num = 0;
for y in 0..y_size+1 {
for z in 0..z_size+1 {
if let Some(c) = tree.get_element(x_max_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
if let Some(c) = tree.borrow().get_element(x_max_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
right_elements_num += 1;
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
right_colors.push(u8_color);
@ -540,6 +544,7 @@ impl EmptyVolume {
}
else {
bottom_neighbors.push(None);
all_same = all_same && (bottom_neighbors[0] == None);
}
}
}
@ -568,6 +573,7 @@ impl EmptyVolume {
}
else {
top_neighbors.push(None);
all_same = all_same && (top_neighbors[0] == None);
}
}
}
@ -596,6 +602,7 @@ impl EmptyVolume {
}
else {
back_neighbors.push(None);
all_same = all_same && (back_neighbors[0] == None);
}
}
}
@ -625,6 +632,7 @@ impl EmptyVolume {
}
else {
front_neighbors.push(None);
all_same = all_same && (front_neighbors[0] == None);
}
}
}
@ -655,6 +663,7 @@ impl EmptyVolume {
}
else {
left_neighbors.push(None);
all_same = all_same && (left_neighbors[0] == None);
}
}
}
@ -684,6 +693,7 @@ impl EmptyVolume {
}
else {
right_neighbors.push(None);
all_same = all_same && (right_neighbors[0] == None);
}
}
}
@ -705,7 +715,7 @@ impl EmptyVolume {
// MARK: To Quads
pub fn to_quads(&self) -> Vec<Quad> {
let mut quads = vec![];
let float_pos = Vector3 {x: self.position.x as f32, y: self.position.y as f32, z: self.position.z as f32};
let float_pos = Vector3 {x: (self.tree_offset.x * self.tree_size + self.position.x) as f32, y: (self.tree_offset.y * self.tree_size + self.position.y) as f32, z: (self.tree_offset.z * self.tree_size + self.position.z) as f32};
//bottom sides of the volumes, top side of the block
for x in 0..self.size_x {
for y in 0..self.size_y {
@ -890,6 +900,182 @@ impl EmptyVolume {
}
out_index
}
pub fn combine_results(first: &Rc<RefCell<OctTree<Cube>>>,first_neighbors: &Rc<OctTree<Rc<RefCell<EmptyVolume>>>>, second: &Rc<RefCell<OctTree<Cube>>>, second_neighbors: &Rc<OctTree<Rc<RefCell<EmptyVolume>>>>, facing: vertex::Facing) {
let mut first_start;
let mut second_start;
let step_one;
let step_two;
match facing {
vertex::Facing::Back => {
first_start = Vector3{x: 0, y: first.borrow().size - 1, z: 0};
second_start = Vector3{x: 0, y: 0, z: 0};
step_one = Vector3{x: 1, y: 0, z: 0};
step_two = Vector3{x: 0, y: 0, z: 1};
},
vertex::Facing::Front => {
first_start = Vector3{x: 0, y: 0, z: 0};
second_start = Vector3{x: 0, y: first.borrow().size - 1, z: 0};
step_one = Vector3{x: 1, y: 0, z: 0};
step_two = Vector3{x: 0, y: 0, z: 1};
},
vertex::Facing::Top => {
first_start = Vector3{x: 0, y: 0, z: first.borrow().size - 1};
second_start = Vector3{x: 0, y: 0, z: 0};
step_one = Vector3{x: 1, y: 0, z: 0};
step_two = Vector3{x: 0, y: 1, z: 0};
},
vertex::Facing::Bottom => {
first_start = Vector3{x: 0, y: 0, z: 0};
second_start = Vector3{x: 0, y: 0, z: first.borrow().size - 1};
step_one = Vector3{x: 1, y: 0, z: 0};
step_two = Vector3{x: 0, y: 1, z: 0};
},
vertex::Facing::Left => {
first_start = Vector3{x: 0, y: 0, z: 0};
second_start = Vector3{x: first.borrow().size - 1, y: 0, z: 0};
step_one = Vector3{x: 0, y: 1, z: 0};
step_two = Vector3{x: 0, y: 0, z: 1};
},
vertex::Facing::Right => {
first_start = Vector3{x: first.borrow().size - 1, y: 0, z: 0};
second_start = Vector3{x: 0, y: 0, z: 0};
step_one = Vector3{x: 0, y: 1, z: 0};
step_two = Vector3{x: 0, y: 0, z: 1};
}
}
let mut done_volumes = vec![];
for u in 0..first.borrow().size {
for v in 0..first.borrow().size {
let first_pos = first_start + v * step_two + u * step_one;
let second_pos = second_start + v * step_two + u * step_one;
let volume_option = first_neighbors.get_element(first_pos.x, first_pos.y, first_pos.z);
if let Some(volume) = volume_option {
if !done_volumes.contains(&volume) {
let mask = Vector3 {x: 1, y: 1, z: 1} - (step_one + step_two);
let negated_mask = (step_one + step_two);
let volume_start_first = negated_mask.mul_element_wise(volume.borrow().position) + first_pos.mul_element_wise(mask);
let volume_start_second = negated_mask.mul_element_wise(volume.borrow().position) + second_pos.mul_element_wise(mask);
let size_u;
let size_v;
if negated_mask.x == 1 {
size_u = volume.borrow().size_x;
if negated_mask.y == 1 {
size_v = volume.borrow().size_y;
} else {
size_v = volume.borrow().size_z;
}
} else {
size_u = volume.borrow().size_y;
size_v = volume.borrow().size_z;
}
let mut new_colors = vec![];
let mut new_roughness = vec![];
let mut new_neighbors = vec![];
let mut color_elements = 0;
let mut neighbor_elements = 0;
let mut all_same = true;
for volume_u in 0..size_u {
for volume_v in 0..size_v {
let pos = second_pos + volume_u * step_one + volume_v * step_two;
let new_neighbor = second_neighbors.get_element(pos.x, pos.y, pos.z);
let new_cube = second.borrow().get_element(pos.x, pos.y, pos.z);
if let Some(c) = new_cube {
let u8_color = Vector3 {x: (c.color * 255.0).x.min(255.0).max(0.0) as u8, y: (c.color * 255.0).y.min(255.0).max(0.0) as u8, z: (c.color * 255.0).z.min(255.0).max(0.0) as u8};
new_colors.push(u8_color);
new_roughness.push(c.roughness);
color_elements += 1;
} else {
new_colors.push(Vector3 { x: 0, y: 0, z: 0 });
new_roughness.push(0);
}
if let Some(n) = new_neighbor {
neighbor_elements += 1;
new_neighbors.push(Some(n.clone()));
all_same = all_same && (new_neighbors[0] == Some(n));
} else {
new_neighbors.push(None);
all_same = all_same && (new_neighbors[0] == None);
}
}
}
if color_elements > 0 {
new_colors = new_colors;
new_roughness = new_roughness;
}
else {
new_colors= vec![];
new_roughness = vec![];
}
if neighbor_elements > 0 {
if all_same {
new_neighbors = vec![new_neighbors[0].clone()];
}
else {
new_neighbors = new_neighbors;
}
}
else {
new_neighbors = vec![None];
}
match facing {
vertex::Facing::Back => {
volume.borrow_mut().color_back = new_colors;
volume.borrow_mut().roughness_back = new_roughness;
volume.borrow_mut().neighbor_back = new_neighbors;
},
vertex::Facing::Front => {
volume.borrow_mut().color_front = new_colors;
volume.borrow_mut().roughness_front = new_roughness;
volume.borrow_mut().neighbor_front = new_neighbors;
},
vertex::Facing::Top => {
volume.borrow_mut().color_top = new_colors;
volume.borrow_mut().roughness_top = new_roughness;
volume.borrow_mut().neighbor_top = new_neighbors;
},
vertex::Facing::Bottom => {
volume.borrow_mut().color_bottom = new_colors;
volume.borrow_mut().roughness_bottom = new_roughness;
volume.borrow_mut().neighbor_bottom = new_neighbors;
},
vertex::Facing::Left => {
volume.borrow_mut().color_left = new_colors;
volume.borrow_mut().roughness_left = new_roughness;
volume.borrow_mut().neighbor_left = new_neighbors;
},
vertex::Facing::Right => {
volume.borrow_mut().color_right = new_colors;
volume.borrow_mut().roughness_right = new_roughness;
volume.borrow_mut().neighbor_right = new_neighbors;
},
}
done_volumes.push(volume);
}
}
}
}
}
}
impl Memorizable for EmptyVolume {
@ -923,11 +1109,11 @@ impl Memorizable for EmptyVolume {
fn insert_into_memory(&self, mut v: Vec<u32>, data: &AppData, scene: &Scene) -> Vec<u32> {
let mut mem_index = self.memory_start;
//pos
v[mem_index] = self.position.x as u32;
v[mem_index] = (self.tree_offset.x * self.tree_size + self.position.x) as u32;
mem_index += 1;
v[mem_index] = self.position.y as u32;
v[mem_index] = (self.tree_offset.y * self.tree_size + self.position.y) as u32;
mem_index += 1;
v[mem_index] = self.position.z as u32;
v[mem_index] = (self.tree_offset.z * self.tree_size + self.position.z) as u32;
mem_index += 1;
//max sizes
v[mem_index] = self.size_x as u32;

119
src/scene/generators.rs Normal file
View file

@ -0,0 +1,119 @@
use super::Scene;
use super::oct_tree::{CHUNK_SIZE, OctTree};
use crate::primitives::cube::Cube;
use crate::primitives::rec_cuboid::Cuboid;
use crate::primitives::drawable::Drawable;
use crate::app_data::AppData;
extern crate rand;
use rand::Rng;
use anyhow::Result;
use cgmath::{vec2, vec3, Vector3};
use std::cell::RefCell;
use std::rc::Rc;
use super::light::{DirectionalLight, PointLight};
pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<()> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
let mut oct_tree1: OctTree<Cube> = OctTree::create(CHUNK_SIZE)?;
let mut oct_tree2: OctTree<Cube> = OctTree::create(CHUNK_SIZE)?;
for x_index in 0..grid_size {
for y_index in 0..grid_size {
let shade = (rng.gen_range(0..50) as f32) / 100.0;
let cube = Cube {
pos: vec3(x_index as f32, y_index as f32, 5.0),
color: vec3(shade, 1.0, shade),
tex_coord: vec2(0.0, 0.0),
transparent: false,
roughness: 255,
};
oct_tree1.set_cube(cube.clone());
let shade = (rng.gen_range(0..50) as f32) / 100.0;
let cube = Cube {
pos: vec3(x_index as f32, y_index as f32, 6.0),
color: vec3(shade, shade, 0.0),
tex_coord: vec2(0.0, 0.0),
transparent: false,
roughness: 255,
};
oct_tree2.set_cube(cube.clone());
}
}
let shade = (rng.gen_range(0..25) as f32) / 100.0;
let cube = Cube {
pos: vec3(10.0, 10.0, 10.0),
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree1.set_cube(cube.clone());
let cube = Cube {
pos: vec3(10.0, 10.0, 9.0),
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree1.set_cube(cube.clone());
let cube = Cube {
pos: vec3(10.0, 10.0, 10.0),
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree2.set_cube(cube.clone());
let cube = Cube {
pos: vec3(10.0, 10.0, 9.0),
color: vec3(0.9, 0.9, 0.9),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree2.set_cube(cube.clone());
scene.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(11.0, 11.0, 11.0), color: vec3(1.0, 1.0, 1.0), memory_start: 0 })));
scene.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(9.0, 9.0, 11.0), color: vec3(0.5, 0.5, 0.5), memory_start: 0 })));
scene.directional_lights.push(Rc::new(RefCell::new(DirectionalLight { direction: vec3(1.0, 1.0, -1.0), color: vec3(0.1, 0.1, 0.1), memory_start: 0 })));
let cube = Cuboid {
pos: vec3(11.0, 11.0, 11.0),
color: vec3(1.0, 1.0, 1.0),
tex_coord: vec2(0.0, 0.0),
size: Vector3 {x: 0.5, y: 0.5, z: 0.5}
};
let index = scene.sized_vertices.len();
cube.draw(&data.topology, index, scene);
let cube = Cuboid {
pos: vec3(9.0, 9.0, 11.0),
color: vec3(1.0, 1.0, 1.0),
tex_coord: vec2(0.0, 0.0),
size: Vector3 {x: 0.5, y: 0.5, z: 0.5}
};
let index = scene.sized_vertices.len();
cube.draw(&data.topology, index, scene);
let tree_ref_one = Rc::new(RefCell::new(oct_tree1));
let tree_ref_two = Rc::new(RefCell::new(oct_tree2));
scene.oct_trees = vec![vec![vec![tree_ref_one.clone(), tree_ref_two.clone()]]];
Ok(())
}

View file

@ -2,9 +2,10 @@ mod oct_tree;
mod empty_volume;
mod light;
mod memorizable;
pub mod generators;
use anyhow::Ok;
use light::{DirectionalLight, LightSource};
use light::{DirectionalLight, LightSource, PointLight};
use vulkanalia::prelude::v1_0::*;
use anyhow::Result;
@ -22,7 +23,6 @@ use crate::primitives::cube::Cube;
use crate::primitives::drawable::Drawable;
use crate::scene::oct_tree::{OctTree, OctTreeIter, CHUNK_SIZE};
use crate::scene::empty_volume::EmptyVolume;
use crate::scene::light::PointLight;
extern crate rand;
use rand::Rng;
@ -57,102 +57,60 @@ pub struct Scene {
pub rt_memory: Vec<u32>,
pub oct_trees: Vec<Vec<Vec<Rc<RefCell<OctTree<Cube>>>>>>,
point_lights: Vec<Rc<RefCell<PointLight>>>,
directional_lights: Vec<Rc<RefCell<DirectionalLight>>>,
}
impl Scene {
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &mut AppData) -> Result<()> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
// todo store the chunks somewhere (or only use them as intermediary for neighbourhood calculation idc)
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
let mut oct_tree: OctTree<Cube> = OctTree::create(CHUNK_SIZE)?;
let mut empty_volumes: Vec<Rc<RefCell<EmptyVolume>>> = vec![];
for x_index in 0..grid_size {
for y_index in 0..grid_size {
let shade = (rng.gen_range(0..50) as f32) / 100.0;
let cube = Cube {
pos: vec3(x_index as f32, y_index as f32, 5.0),
color: vec3(shade, 1.0, shade),
tex_coord: vec2(0.0, 0.0),
transparent: false,
roughness: 0,
};
let mut neighbor_trees: Vec<Vec<Vec<Rc<OctTree<Rc<RefCell<EmptyVolume>>>>>>> = vec![];
oct_tree.set_cube(cube.clone());
}
}
let mut x_index = 0;
let mut y_index = 0;
let mut z_index = 0;
for oct_tree_plane_xy in &self.oct_trees {
neighbor_trees.push(vec![]);
for oct_tree_line_y in oct_tree_plane_xy {
neighbor_trees[z_index].push(vec![]);
for oct_tree in oct_tree_line_y {
let mut new_volumes: Vec<Rc<RefCell<EmptyVolume>>>;
let new_neighbors;
(new_volumes, new_neighbors) = EmptyVolume::from_oct_tree(oct_tree, Vector3 { x: x_index, y: y_index, z: z_index });
empty_volumes.append(&mut new_volumes);
let shade = (rng.gen_range(0..25) as f32) / 100.0;
let cube = Cube {
pos: vec3(10.0, 10.0, 10.0),
color: vec3(1.0, 0.0, 0.0),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree.set_cube(cube.clone());
neighbor_trees[z_index][y_index].push(Rc::new(new_neighbors));
let cube = Cube {
pos: vec3(10.0, 10.0, 9.0),
color: vec3(1.0, 0.0, 0.0),
tex_coord: vec2(0.0, 0.0),
transparent: true,
roughness: 32,
};
oct_tree.set_cube(cube.clone());
self.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(11.0, 11.0, 11.0), color: vec3(1.0, 1.0, 1.0), memory_start: 0 })));
self.point_lights.push(Rc::new(RefCell::new(PointLight { pos: vec3(9.0, 9.0, 11.0), color: vec3(0.5, 0.5, 0.5), memory_start: 0 })));
self.directional_lights.push(Rc::new(RefCell::new(DirectionalLight { direction: vec3(1.0, 1.0, -1.0), color: vec3(0.1, 0.1, 0.1), memory_start: 0 })));
let empty_volumes: Vec<Rc<RefCell<EmptyVolume>>>;
(empty_volumes, _) = EmptyVolume::from_oct_tree(&oct_tree);
println!("number of empty volumes is {}", empty_volumes.len());
let oct_tree_iter = OctTreeIter::create(&oct_tree)?;
for item in oct_tree_iter {
let sized_index = self.sized_vertices.len();
let index = self.vertices.len();
match item {
Some(cube) => {
/*if (cube.pos.x + cube.pos.y) as usize % 2 == 0{
/**/
let cuboid = Cuboid {
pos: cube.pos,
color: cube.color,
tex_coord: cube.tex_coord,
size: Vector3 {x: 1.0, y: 1.0, z: 1.0},
};
cuboid.draw(&data.topology, sized_index, self);
}
else {
cube.draw(&data.topology, index, self);
}*/
//cube.draw(&data.topology, index, self);
x_index += 1;
}
None => {}
y_index += 1;
}
z_index += 1;
}
let mut x_index = 0;
let mut y_index = 0;
let mut z_index = 0;
for oct_tree_plane_xy in &self.oct_trees {
for oct_tree_line_x in oct_tree_plane_xy {
for oct_tree in oct_tree_line_x {
if oct_tree_line_x.len() > x_index + 1 {
EmptyVolume::combine_results(oct_tree, &neighbor_trees[z_index][y_index][x_index], &oct_tree_line_x[x_index + 1], &neighbor_trees[z_index][y_index][x_index + 1], vertex::Facing::Right);
EmptyVolume::combine_results(&oct_tree_line_x[x_index + 1], &neighbor_trees[z_index][y_index][x_index + 1], oct_tree, &neighbor_trees[z_index][y_index][x_index], vertex::Facing::Left);
}
let cube = Cuboid {
pos: vec3(11.0, 11.0, 11.0),
color: vec3(1.0, 1.0, 1.0),
tex_coord: vec2(0.0, 0.0),
size: Vector3 {x: 0.5, y: 0.5, z: 0.5}
};
let index = self.sized_vertices.len();
cube.draw(&data.topology, index, self);
let cube = Cuboid {
pos: vec3(9.0, 9.0, 11.0),
color: vec3(1.0, 1.0, 1.0),
tex_coord: vec2(0.0, 0.0),
size: Vector3 {x: 0.5, y: 0.5, z: 0.5}
};
let index = self.sized_vertices.len();
cube.draw(&data.topology, index, self);
x_index += 1;
}
y_index += 1;
}
z_index += 1;
}
println!("number of empty volumes is {}", empty_volumes.len());
let mut memory_index = 6;
// 0 - location for the maximum number of lights referenced per chunk (also will be the invalid memory allocation for pointing to a nonexistant neighbor)

View file

@ -7,7 +7,7 @@ use crate::primitives::cube::Cube;
extern crate rand;
pub const CHUNK_SIZE_EXPONENT: u32 = 6;
pub const CHUNK_SIZE_EXPONENT: u32 = 4;
pub const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
pub const MAX_TREE_DEPTH: usize = CHUNK_SIZE_EXPONENT as usize - 2;
pub const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);