pre double pipeline commit. The following will attempt to add another rendering stage for textured cuboids.
This commit is contained in:
parent
31d56ded3f
commit
c5bcd148ca
22 changed files with 854 additions and 53 deletions
src
409
src/scene.rs
409
src/scene.rs
|
@ -1,24 +1,33 @@
|
|||
use anyhow::Ok;
|
||||
use vulkanalia::prelude::v1_0::*;
|
||||
use anyhow::Result;
|
||||
|
||||
use cgmath::{vec2, vec3};
|
||||
use cgmath::{vec2, vec3, Vector3};
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::app_data::AppData;
|
||||
use crate::buffer;
|
||||
use crate::primitives::rec_cuboid::Cuboid;
|
||||
use crate::vertex;
|
||||
use crate::primitives::cube::Cube;
|
||||
use crate::primitives::drawable::Drawable;
|
||||
|
||||
extern crate rand;
|
||||
use rand::Rng;
|
||||
|
||||
const CHUNK_SIZE: usize = 500;
|
||||
const CHUNK_SIZE_EXPONENT: u32 = 10;
|
||||
const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
|
||||
const MAX_TREE_DEPTH: usize = 8;
|
||||
const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Scene {
|
||||
pub vertices: Vec<vertex::Vertex>,
|
||||
pub sized_vertices: Vec<vertex::SizedVertex>,
|
||||
pub indices: Vec<u32>,
|
||||
|
||||
pub vertex_buffer: vk::Buffer,
|
||||
|
@ -34,7 +43,7 @@ impl Scene {
|
|||
let grid_size = CHUNK_SIZE as i32;
|
||||
|
||||
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
|
||||
let mut chunks = vec![Chunk::create()?];
|
||||
let mut oct_tree = OctTree::create(CHUNK_SIZE)?;
|
||||
|
||||
//todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting?
|
||||
// have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives
|
||||
|
@ -46,17 +55,25 @@ impl Scene {
|
|||
color: vec3(shade, 1.0, shade),
|
||||
tex_coord: vec2(0.0, 0.0)
|
||||
};
|
||||
chunks[0].set_cube(cube);
|
||||
|
||||
oct_tree.set_cube(cube.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let chunk = &chunks[0];
|
||||
let chunk_iter = ChunkIter::create(chunk)?;
|
||||
for item in chunk_iter {
|
||||
let oct_tree_iter = OctTreeIter::create(&oct_tree)?;
|
||||
for item in oct_tree_iter {
|
||||
let index = self.vertices.len();
|
||||
match item {
|
||||
Some(cube) => {
|
||||
/*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, index, self);*/
|
||||
cube.draw(&data.topology, index, self);
|
||||
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
|
@ -79,16 +96,47 @@ impl Scene {
|
|||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Chunk {
|
||||
//todo change to hashmap?
|
||||
blocks: HashMap<cgmath::Vector3<u32>, Cube, rustc_hash::FxBuildHasher>,
|
||||
struct OctTree {
|
||||
pub child_XYZ: Option<Rc<RefCell<Self>>>,
|
||||
pub child_xYZ: Option<Rc<RefCell<Self>>>,
|
||||
pub child_xyZ: Option<Rc<RefCell<Self>>>,
|
||||
pub child_XyZ: Option<Rc<RefCell<Self>>>,
|
||||
pub child_XYz: Option<Rc<RefCell<Self>>>,
|
||||
pub child_xYz: Option<Rc<RefCell<Self>>>,
|
||||
pub child_xyz: Option<Rc<RefCell<Self>>>,
|
||||
pub child_Xyz: Option<Rc<RefCell<Self>>>,
|
||||
|
||||
pub blocks: Vec<Option<Cube>>,
|
||||
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl Chunk {
|
||||
pub fn create() -> Result<Self> {
|
||||
let mut map: HashMap<cgmath::Vector3<u32>, Cube, rustc_hash::FxBuildHasher> = FxHashMap::default();
|
||||
impl OctTree {
|
||||
pub fn create(size: usize) -> Result<Self> {
|
||||
let mut blocks: Vec<Option<Cube>> = vec![];
|
||||
if size == MIN_CHUNK_SIZE {
|
||||
for _ in 0..MIN_CHUNK_SIZE {
|
||||
for _ in 0..MIN_CHUNK_SIZE {
|
||||
for _ in 0..MIN_CHUNK_SIZE {
|
||||
blocks.push(None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Self {
|
||||
blocks: map
|
||||
child_XYZ: None,
|
||||
child_xYZ: None,
|
||||
child_xyZ: None,
|
||||
child_XyZ: None,
|
||||
child_XYz: None,
|
||||
child_xYz: None,
|
||||
child_xyz: None,
|
||||
child_Xyz: None,
|
||||
|
||||
blocks: blocks,
|
||||
|
||||
size,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -96,62 +144,335 @@ impl Chunk {
|
|||
let x = cube.pos.x as usize;
|
||||
let y = cube.pos.y as usize;
|
||||
let z = cube.pos.z as usize;
|
||||
assert!(x < CHUNK_SIZE, "x value out of range!");
|
||||
assert!(y < CHUNK_SIZE, "y value out of range!");
|
||||
assert!(z < CHUNK_SIZE, "z value out of range!");
|
||||
self.blocks.insert(vec3(x as u32, y as u32, z as u32), cube);
|
||||
assert!(x < self.size, "x value out of range!");
|
||||
assert!(y < self.size, "y value out of range!");
|
||||
assert!(z < self.size, "z value out of range!");
|
||||
self.set_cube_internal(cube, x, y, z);
|
||||
}
|
||||
|
||||
fn set_cube_internal(&mut self, cube: Cube, x: usize, y: usize, z: usize) {
|
||||
if self.size > MIN_CHUNK_SIZE {
|
||||
let mid_point = self.size / 2;
|
||||
if x >= mid_point {
|
||||
if y >= mid_point {
|
||||
if z >= mid_point {
|
||||
match &self.child_XYZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
|
||||
self.child_XYZ = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_XYz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x - mid_point, y - mid_point, z);
|
||||
self.child_XYz = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if z >= mid_point {
|
||||
match &self.child_XyZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z - mid_point);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x - mid_point, y, z - mid_point);
|
||||
self.child_XyZ = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_Xyz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x - mid_point, y, z);
|
||||
self.child_Xyz = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if y >= mid_point {
|
||||
if z >= mid_point {
|
||||
match &self.child_xYZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z - mid_point);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x, y - mid_point, z - mid_point);
|
||||
self.child_xYZ = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_xYz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x, y - mid_point, z);
|
||||
self.child_xYz = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if z >= mid_point {
|
||||
match &self.child_xyZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x, y, z - mid_point);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x, y, z - mid_point);
|
||||
self.child_xyZ = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_xyz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().set_cube_internal(cube, x, y, z);
|
||||
},
|
||||
None => {
|
||||
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||
child.set_cube_internal(cube, x, y, z);
|
||||
self.child_xyz = Some(Rc::new(RefCell::new(child)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = Some(cube);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clear_cube(&mut self, x: usize, y: usize, z: usize) {
|
||||
assert!(x < CHUNK_SIZE, "x value out of range!");
|
||||
assert!(y < CHUNK_SIZE, "y value out of range!");
|
||||
assert!(z < CHUNK_SIZE, "z value out of range!");
|
||||
self.blocks.remove(&vec3(x as u32, y as u32, z as u32));
|
||||
assert!(x < self.size, "x value out of range!");
|
||||
assert!(y < self.size, "y value out of range!");
|
||||
assert!(z < self.size, "z value out of range!");
|
||||
//self.blocks.remove(&vec3(x as u32, y as u32, z as u32));
|
||||
|
||||
self.clear_cube_internal(x, y, z)
|
||||
}
|
||||
|
||||
fn clear_cube_internal(&mut self, x: usize, y: usize, z: usize) {
|
||||
if self.size > MIN_CHUNK_SIZE {
|
||||
let mid_point = self.size / 2;
|
||||
if x >= mid_point {
|
||||
if y >= mid_point {
|
||||
if z >= mid_point {
|
||||
match &self.child_XYZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x - mid_point, y - mid_point, z - mid_point);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_XYz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x - mid_point, y - mid_point, z);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if z >= mid_point {
|
||||
match &self.child_XyZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x - mid_point, y, z - mid_point);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_Xyz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x - mid_point, y, z);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if y >= mid_point {
|
||||
if z >= mid_point {
|
||||
match &self.child_xYZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x, y - mid_point, z - mid_point);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_xYz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x, y - mid_point, z);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
if z >= mid_point {
|
||||
match &self.child_xyZ {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x, y, z - mid_point);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
else {
|
||||
match &self.child_xyz {
|
||||
Some(child) => {
|
||||
child.borrow_mut().clear_cube_internal(x, y, z);
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ChunkIter<'a> {
|
||||
struct OctTreeIter<'a> {
|
||||
iter_x: usize,
|
||||
iter_y: usize,
|
||||
iter_z: usize,
|
||||
chunk: &'a Chunk
|
||||
todo: Vec<Rc<RefCell<OctTree>>>,
|
||||
chunk: &'a OctTree
|
||||
}
|
||||
|
||||
impl<'a> ChunkIter<'a> {
|
||||
pub fn create(chunk: &'a Chunk) -> Result<Self> {
|
||||
|
||||
Ok(Self {
|
||||
impl<'a> OctTreeIter<'a> {
|
||||
pub fn create(chunk: &'a OctTree) -> Result<Self> {
|
||||
let mut out = Self {
|
||||
iter_x: 0,
|
||||
iter_y: 0,
|
||||
iter_z: 0,
|
||||
todo: vec![],
|
||||
chunk
|
||||
})
|
||||
};
|
||||
out.add_todo(&chunk);
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn add_todo(&mut self, oct_tree: &OctTree) {
|
||||
match &oct_tree.child_XYZ {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_xYZ {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_xyZ {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_XyZ {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_XYz {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_xYz {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_xyz {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match &oct_tree.child_Xyz {
|
||||
Some(child) => {
|
||||
self.todo.push(child.clone());
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ChunkIter<'a> {
|
||||
type Item = Option<&'a Cube>;
|
||||
impl<'a> Iterator for OctTreeIter<'a> {
|
||||
type Item = Option<Cube>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.iter_x < CHUNK_SIZE && self.iter_y < CHUNK_SIZE && self.iter_z < CHUNK_SIZE {
|
||||
let result = self.chunk.blocks.get(&vec3(self.iter_x as u32, self.iter_y as u32, self.iter_z as u32));
|
||||
if self.todo.len() != 0 {
|
||||
while self.todo.last().unwrap().borrow().blocks.len() == 0 {
|
||||
let oct_tree = self.todo.pop().unwrap();
|
||||
self.add_todo(&oct_tree.borrow());
|
||||
}
|
||||
|
||||
self.iter_x += 1;
|
||||
if self.iter_x >= CHUNK_SIZE {
|
||||
self.iter_x = 0;
|
||||
self.iter_y += 1;
|
||||
if self.iter_x < MIN_CHUNK_SIZE && self.iter_y < MIN_CHUNK_SIZE && self.iter_z < MIN_CHUNK_SIZE {
|
||||
let result = self.todo.last().unwrap().borrow().blocks[self.iter_x + self.iter_y * MIN_CHUNK_SIZE + self.iter_z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE].clone();
|
||||
|
||||
self.iter_x += 1;
|
||||
if self.iter_x >= MIN_CHUNK_SIZE {
|
||||
self.iter_x = 0;
|
||||
self.iter_y += 1;
|
||||
}
|
||||
if self.iter_y >= MIN_CHUNK_SIZE {
|
||||
self.iter_y = 0;
|
||||
self.iter_z += 1;
|
||||
}
|
||||
|
||||
if self.iter_z == MIN_CHUNK_SIZE {
|
||||
self.todo.pop();
|
||||
self.iter_x = 0;
|
||||
self.iter_y = 0;
|
||||
self.iter_z = 0;
|
||||
}
|
||||
|
||||
return Some(result)
|
||||
}
|
||||
if self.iter_y >= CHUNK_SIZE {
|
||||
self.iter_y = 0;
|
||||
self.iter_z += 1;
|
||||
}
|
||||
return Some(result.clone())
|
||||
}
|
||||
|
||||
self.iter_x = 0;
|
||||
self.iter_y = 0;
|
||||
self.iter_z = 0;
|
||||
self.add_todo(&self.chunk);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue