adds first connection between octtree volumes
This commit is contained in:
parent
b559bd5e08
commit
4e50b1a12e
5 changed files with 374 additions and 109 deletions
src/scene
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue