fixes double neighbor assignement and cubes inside of empty volumes after skipping to grow

This commit is contained in:
zomseffen 2025-02-26 12:31:58 +01:00
parent 5353f28acd
commit 457d3e2d6b
7 changed files with 165 additions and 105 deletions

Binary file not shown.

View file

@ -281,7 +281,7 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
uint next_neighbor = sample_neighbor_from_scene_info(volume_index, uvec2(u, v), hit_facing);
uvec4 color_sample = sample_color_from_scene_info(volume_index, uvec2(u, v), hit_facing);
if (color_sample == uvec4(0, 0, 0, 0)) {
if (color_sample.xyz == uvec3(0, 0, 0)) {
// not a color hit, so check neighbor
if (next_neighbor != 0) {
volume_index = next_neighbor;
@ -290,6 +290,8 @@ Tracing trace_ray(uint volume_start, vec3 starting_pos, vec3 start_direction, fl
volume_pos_z = scene_info.infos[volume_index + 2];
} else {
// neighbor miss
end_color_transparent = uvec4(255, 0, 0, 255);
result.end_color = uvec4(255, 0, 0, 255);
break;
}
} else {
@ -377,7 +379,7 @@ vec3 get_lighting_color(uint volume_start, vec3 starting_pos, vec4 orig_color_sa
uint light_num = 0;
// initialize color
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.01);
vec3 color_sum = vec3(0.0, 0.0, 0.0) + (orig_color_sample.xyz * 0.005);
uint max_iterations = max_num_lights * max_iterations_per_light;
uint iteration = 0;

View file

@ -223,8 +223,8 @@ impl App {
image::create_texture_image_view(&device, &mut data)?;
image::create_texture_sampler(&device, &mut data)?;
let cur_pos = generators::generate_test_scene(&mut scene_handler, &mut data)?;
//let cur_pos = generators::generate_test_scene2(&mut scene_handler, &mut data, 1, 1,1, 1)?;
//let cur_pos = generators::generate_test_scene(&mut scene_handler, &mut data)?;
let cur_pos = generators::generate_test_scene2(&mut scene_handler, &mut data, 3, 3,1, 2)?;
scene_handler.prepare_data(&instance, &device, &mut data)?;
buffer::create_uniform_buffers(&instance, &device, &mut data)?;

View file

@ -105,30 +105,38 @@ impl EmptyVolume {
continue;
}
println!("new starting pos: {}, {}, {}", x_index, y_index, z_index);
println!("start growing volume x");
// MARK: Start new Volume
let mut x_size = 0;
let mut y_size = 0;
let mut z_size = 0;
if query_result.1 > 1 {
let neighbor_result = neighbors.test_element(x_index, y_index, z_index);
if query_result.1 > 1 && query_result.2.0 >= neighbor_result.2.0 && query_result.2.1 >= neighbor_result.2.1 && query_result.2.2 >= neighbor_result.2.2 && query_result.1 <= neighbor_result.1 {
x_size = query_result.1 - 1 - (x_index - query_result.2.0);
y_size = query_result.1 - 1 - (y_index - query_result.2.1);
z_size = query_result.1 - 1 - (z_index - query_result.2.2);
println!("enhanced starting size: {}, {}, {}", x_size+1, y_size+1, z_size+1);
}
println!("start growing volume x");
let mut grow = true;
while grow {
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) {
while z < z_size + 1 && y < y_size + 1 {
let query_result = tree.borrow().test_element(x_index + x_size + 1, y_index + y, z_index + z);
let neighbor_result = neighbors.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();
!neighbor_result.0;
if !grow {
break;
}
if query_result.1 > 1 {
if query_result.1 > 1 && query_result.2.1 >= neighbor_result.2.1 && query_result.2.2 >= neighbor_result.2.2 && query_result.1 <= neighbor_result.1 {
let start_x = query_result.2.0;
let start_y = query_result.2.1;
let start_z = query_result.2.2;
@ -137,22 +145,20 @@ impl EmptyVolume {
let end_y = query_result.2.1 + query_result.1;
let end_z = query_result.2.2 + query_result.1;
if start_z <= z && z_index + z_size <= end_z {
if start_z <= z_index + z && z_index + z_size <= end_z {
// we can skip iterating z
z = end_z;
if start_y <= y && y_index + y_size <= end_y {
z = end_z - z_index;
if start_y <= y_index + y && y_index + y_size <= end_y && start_z <= z_index {
// we can skip iterating y
y = end_y;
y = end_y - y_index;
z = 0;
continue;
}
}
}
if !grow {
break;
}
z += 1;
if z >= z_size {
if z >= z_size + 1 {
z = 0;
y += 1;
}
@ -169,13 +175,18 @@ impl EmptyVolume {
if grow {
let mut z = 0;
let mut x = 0;
while z < z_size.max(1) && x < x_size.max(1) {
while z < z_size + 1 && x < x_size + 1 {
let query_result = tree.borrow().test_element(x_index + x, y_index + y_size + 1, z_index + z);
let neighbor_result = neighbors.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();
if query_result.1 > 1 {
!neighbor_result.0;
if !grow {
break;
}
if query_result.1 > 1 && query_result.2.0 >= neighbor_result.2.0 && query_result.2.2 >= neighbor_result.2.2 && query_result.1 <= neighbor_result.1 {
let start_x = query_result.2.0;
let start_y = query_result.2.1;
let start_z = query_result.2.2;
@ -184,22 +195,20 @@ impl EmptyVolume {
let end_y = query_result.2.1 + query_result.1;
let end_z = query_result.2.2 + query_result.1;
if start_z <= z && z_index + z_size <= end_z {
if start_z <= z_index + z && z_index + z_size <= end_z {
// we can skip iterating z
z = end_z;
if start_x <= x && x_index + x_size <= end_x {
z = end_z - z_index;
if start_x <= x_index + x && x_index + x_size <= end_x && start_z <= z_index {
// we can skip iterating x
x = end_x;
x = end_x - x_index;
z = 0;
continue;
}
}
}
if !grow {
break;
}
z += 1;
if z >= z_size {
if z >= z_size + 1 {
z = 0;
x += 1;
}
@ -217,13 +226,18 @@ impl EmptyVolume {
if grow {
let mut y = 0;
let mut x = 0;
while y < y_size.max(1) && x < x_size.max(1) {
while y < y_size + 1 && x < x_size + 1 {
let query_result = tree.borrow().test_element(x_index + x, y_index + y, z_index + z_size + 1);
let neighbor_result = neighbors.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();
!neighbor_result.0;
if !grow {
break;
}
if query_result.1 > 1 {
if query_result.1 > 1 && query_result.2.1 >= neighbor_result.2.1 && query_result.2.0 >= neighbor_result.2.0 && query_result.1 <= neighbor_result.1 {
let start_x = query_result.2.0;
let start_y = query_result.2.1;
let start_z = query_result.2.2;
@ -232,22 +246,20 @@ impl EmptyVolume {
let end_y = query_result.2.1 + query_result.1;
let end_z = query_result.2.2 + query_result.1;
if start_x <= x && x_index + x_size <= end_x {
if start_x <= x_index + x && x_index + x_size <= end_x {
// we can skip iterating x
x = end_x;
if start_y <= y && y_index + y_size <= end_y {
x = end_x - x_index;
if start_y <= y_index + y && y_index + y_size <= end_y && start_x <= x_index {
// we can skip iterating y
y = end_y;
y = end_y - y_index;
x = 0;
continue;
}
}
}
if !grow {
break;
}
x += 1;
if x >= x_size {
if x >= x_size + 1 {
x = 0;
y += 1;
}
@ -509,7 +521,7 @@ impl EmptyVolume {
x_min_pos = 0;
}
else {
x_min_pos = reference.borrow().position.x -1;
x_min_pos = reference.borrow().position.x - 1;
}
let y_min_pos;
if reference.borrow().position.y == 0 {
@ -517,7 +529,7 @@ impl EmptyVolume {
y_min_pos = 0;
}
else {
y_min_pos = reference.borrow().position.y -1;
y_min_pos = reference.borrow().position.y - 1;
}
let z_min_pos;
if reference.borrow().position.z == 0 {
@ -525,7 +537,7 @@ impl EmptyVolume {
z_min_pos = 0;
}
else {
z_min_pos = reference.borrow().position.z -1;
z_min_pos = reference.borrow().position.z - 1;
}
let x_max_pos = reference.borrow().position.x + reference.borrow().size_x;
let y_max_pos = reference.borrow().position.y + reference.borrow().size_y;
@ -534,18 +546,16 @@ impl EmptyVolume {
let mut bottom_neighbors = vec![];
let mut bottom_elements_num = 0;
let mut all_same = true;
if z_min_pos != 0 {
for x in 0..reference.borrow().size_x {
for y in 0..reference.borrow().size_y {
if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_min_pos) {
bottom_elements_num += 1;
bottom_neighbors.push(Some(c.clone()));
all_same = all_same && (bottom_neighbors[0] == Some(c));
}
else {
bottom_neighbors.push(None);
all_same = all_same && (bottom_neighbors[0] == None);
}
for x in 0..reference.borrow().size_x {
for y in 0..reference.borrow().size_y {
if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, reference.borrow().position.y + y, z_min_pos) {
bottom_elements_num += 1;
bottom_neighbors.push(Some(c.clone()));
all_same = all_same && (bottom_neighbors[0] == Some(c));
}
else {
bottom_neighbors.push(None);
all_same = all_same && (bottom_neighbors[0] == None);
}
}
}
@ -622,18 +632,16 @@ impl EmptyVolume {
let mut front_neighbors = vec![];
let mut front_elements_num = 0;
let mut all_same = true;
if y_min_pos != 0{
for x in 0..reference.borrow().size_x {
for z in 0..reference.borrow().size_z {
if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) {
front_elements_num += 1;
front_neighbors.push(Some(c.clone()));
all_same = all_same && (front_neighbors[0] == Some(c));
}
else {
front_neighbors.push(None);
all_same = all_same && (front_neighbors[0] == None);
}
for x in 0..reference.borrow().size_x {
for z in 0..reference.borrow().size_z {
if let Some(c) = neighbors.get_element(reference.borrow().position.x + x, y_min_pos, reference.borrow().position.z + z) {
front_elements_num += 1;
front_neighbors.push(Some(c.clone()));
all_same = all_same && (front_neighbors[0] == Some(c));
}
else {
front_neighbors.push(None);
all_same = all_same && (front_neighbors[0] == None);
}
}
}
@ -653,18 +661,16 @@ impl EmptyVolume {
let mut left_neighbors = vec![];
let mut left_elements_num = 0;
let mut all_same = true;
if x_min_pos != 0 {
for y in 0..reference.borrow().size_y {
for z in 0..reference.borrow().size_z {
if let Some(c) = neighbors.get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
left_elements_num += 1;
left_neighbors.push(Some(c.clone()));
all_same = all_same && (left_neighbors[0] == Some(c));
}
else {
left_neighbors.push(None);
all_same = all_same && (left_neighbors[0] == None);
}
for y in 0..reference.borrow().size_y {
for z in 0..reference.borrow().size_z {
if let Some(c) = neighbors.get_element(x_min_pos, reference.borrow().position.y + y, reference.borrow().position.z + z) {
left_elements_num += 1;
left_neighbors.push(Some(c.clone()));
all_same = all_same && (left_neighbors[0] == Some(c));
}
else {
left_neighbors.push(None);
all_same = all_same && (left_neighbors[0] == None);
}
}
}
@ -723,13 +729,20 @@ impl EmptyVolume {
if self.color_bottom.len() <= index {
continue;
}
if self.neighbor_bottom.len() > index {
if let Some(_) = self.neighbor_bottom[index] {
if self.neighbor_bottom.len() > index || self.neighbor_bottom.len() == 1 {
if let Some(_) = self.neighbor_bottom[index.min(self.neighbor_bottom.len() - 1)] {
if self.color_bottom[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_bottom[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + y, self.position.z);
if self.neighbor_bottom.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + y as f32, z: -0.5 },
pos4: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + y as f32, z: -0.5 },
@ -749,13 +762,21 @@ impl EmptyVolume {
if self.color_top.len() <= 0 {
continue;
}
if self.neighbor_top.len() > index {
if let Some(_) = self.neighbor_top[index] {
if self.neighbor_top.len() > index || self.neighbor_top.len() == 1 {
if let Some(_) = self.neighbor_top[index.min(self.neighbor_top.len() - 1)] {
if self.color_top[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_top[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + y, self.position.z + self.size_z);
if self.neighbor_top.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: 0.5 + x as f32, y: -0.5 + y as f32, z: self.size_z as f32 - 0.5 },
@ -776,13 +797,20 @@ impl EmptyVolume {
if self.color_front.len() <= 0 {
continue;
}
if self.neighbor_front.len() > index {
if let Some(_) = self.neighbor_front[index] {
if self.neighbor_front.len() > index || self.neighbor_front.len() == 1 {
if let Some(_) = self.neighbor_front[index.min(self.neighbor_front.len() - 1)] {
if self.color_front[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_front[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y, self.position.z + z);
if self.neighbor_front.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 - 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + 0 as f32, z: z as f32 + 0.5 },
@ -803,13 +831,20 @@ impl EmptyVolume {
if self.color_back.len() <= 0 {
continue;
}
if self.neighbor_back.len() > index {
if let Some(_) = self.neighbor_back[index] {
if self.neighbor_back.len() > index || self.neighbor_back.len() == 1 {
if let Some(_) = self.neighbor_back[index.min(self.neighbor_back.len() - 1)] {
if self.color_back[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_back[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + x, self.position.y + self.size_y, self.position.z + z);
if self.neighbor_back.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + x as f32, y: -0.5 + self.size_y as f32, z: z as f32 + 0.5 },
@ -830,13 +865,20 @@ impl EmptyVolume {
if self.color_left.len() <= 0 {
continue;
}
if self.neighbor_left.len() > index {
if let Some(_) = self.neighbor_left[index] {
if self.neighbor_left.len() > index || self.neighbor_left.len() == 1 {
if let Some(_) = self.neighbor_left[index.min(self.neighbor_left.len() - 1)] {
if self.color_left[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_left[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x, self.position.y + y, self.position.z + z);
if self.neighbor_left.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos4: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos1: float_pos + Vector3 { x: -0.5 + 0.0 as f32, y: y as f32 - 0.5, z: z as f32 + 0.5 },
@ -857,13 +899,20 @@ impl EmptyVolume {
if self.color_right.len() <= 0 {
continue;
}
if self.neighbor_right.len() > index {
if let Some(_) = self.neighbor_right[index] {
if self.neighbor_right.len() > index || self.neighbor_right.len() == 1 {
if let Some(_) = self.neighbor_right[index.min(self.neighbor_right.len() - 1)] {
if self.color_right[index] == (Vector3 {x: 0, y: 0, z: 0}) {
continue;
}
}
}
if self.color_right[index] == (Vector3 {x: 0, y: 0, z: 0}) {
println!("No neighbor reference, but no color! x: {}, y: {}, z: {}", self.position.x + self.size_x, self.position.y + y, self.position.z + z);
if self.neighbor_right.len() == 1 {
println!("neighbor length is one!");
}
}
let quad = Quad {
pos1: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: z as f32 - 0.5 },
pos4: float_pos + Vector3 { x: -0.5 + self.size_x as f32, y: y as f32 - 0.5, z: z as f32 + 0.5 },
@ -1238,7 +1287,7 @@ impl Memorizable for EmptyVolume {
mem_index += 2;
//color and roughness
//check which endian should be used in conjun´ction of the graphicscard (might already be handled by vulkan)
//check which endian should be used in conjunction of the graphics card (might already be handled by vulkan)
if self.color_top.len() > 0 {
for index in 0..self.color_top.len() {
let value = &self.color_top[index];
@ -1419,7 +1468,7 @@ impl Memorizable for EmptyVolume {
v[mem_index] = 0;
mem_index += 1;
}
println!("last memory index of volume was {}, equivalent to {}kB", mem_index, mem_index * 32 / 8 / 1024);
//println!("last memory index of volume was {}, equivalent to {}kB", mem_index, mem_index * 32 / 8 / 1024);
v
}

View file

@ -121,6 +121,7 @@ pub fn generate_test_scene(scene: &mut Scene, data: &mut AppData) -> Result<(Poi
pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x: usize, chunk_num_y: usize, chunk_num_z: usize, num_gaussians: usize) -> Result<(Point3<f32>)> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
let max_x = chunk_num_x * grid_size as usize;
@ -130,7 +131,7 @@ pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x:
let mut height_map = vec![vec![0.0; max_y]; max_x];
for i in 0..num_gaussians {
let height = rng.gen_range(0..max_z / 2) as f32;
let height = rng.gen_range(16..max_z / 2) as f32;
let center_x = rng.gen_range(0..max_x) as f32;
let center_y = rng.gen_range(0..max_y) as f32;
@ -144,7 +145,17 @@ pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x:
}
}
let oct_trees = vec![vec![vec![Rc::new(RefCell::new(OctTree::<Cube>::create(CHUNK_SIZE)?)); chunk_num_x]; chunk_num_y]; chunk_num_z];
let mut oct_trees = vec![];
for z in 0..chunk_num_z {
oct_trees.push(vec![]);
for y in 0..chunk_num_y {
oct_trees[z].push(vec![]);
for x in 0..chunk_num_x {
oct_trees[z][y].push(Rc::new(RefCell::new(OctTree::<Cube>::create(CHUNK_SIZE)?)));
}
}
}
for x in 0..max_x {
for y in 0..max_y {
@ -153,7 +164,7 @@ pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x:
if height < max_z {
let shade = (rng.gen_range(0..50) as f32) / 100.0;
let cube = Cube {
pos: vec3(x as f32, y as f32, (height % grid_size as usize) as f32),
pos: vec3((x % grid_size as usize) as f32, (y % grid_size as usize) as f32, (height % grid_size as usize) as f32),
color: vec3(shade, 1.0, shade),
tex_coord: vec2(0.0, 0.0),
transparent: false,
@ -171,7 +182,7 @@ pub fn generate_test_scene2(scene: &mut Scene, data: &mut AppData, chunk_num_x:
pillar_height -= 1;
let shade = (rng.gen_range(1..50) as f32) / 100.0;
let cube = Cube {
pos: vec3(x as f32, y as f32, (pillar_height % grid_size as usize) as f32),
pos: vec3((x % grid_size as usize) as f32, (y % grid_size as usize) as f32, (pillar_height % grid_size as usize) as f32),
color: vec3(shade, shade / 2.0, 0.0),
tex_coord: vec2(0.0, 0.0),
transparent: false,

View file

@ -175,6 +175,7 @@ impl Scene {
}
if self.rt_vertices.len() != 0 {
println!("number of quad vertices is {}", self.rt_vertices.len());
(self.vertex_buffer_quad, self.vertex_buffer_memory_quad) = buffer::create_vertex_buffer(instance, device, &data, &self.rt_vertices)?;
(self.index_buffer_quad, self.index_buffer_memory_quad) = buffer::create_index_buffer(&instance, &device, &data, &self.indices_rt)?;
}

View file

@ -191,9 +191,6 @@ impl<T: Clone> OctTree<T> {
}
}
else {
if let Some(_) = self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] {
println!("overwriting block!")
}
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = Some(element);
}
}
@ -471,10 +468,10 @@ impl<T: Clone> OctTree<T> {
}
else {
if let Some(c) = &self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] {
(true, 1, (x, y, z), Some(c.clone()))
(true, 1, (node_start_x + x, node_start_y + y, node_start_z + z), Some(c.clone()))
}
else {
(false, 1, (x, y, z), None)
(false, 1, (node_start_x + x, node_start_y + y, node_start_z + z), None)
}
}
}