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
src/scene

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)