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
|
@ -642,9 +642,9 @@ unsafe fn create_logical_device(
|
|||
}
|
||||
|
||||
unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> {
|
||||
let vert = include_bytes!("../shaders/vert.spv");
|
||||
let geo = include_bytes!("../shaders/geo.spv");
|
||||
let frag = include_bytes!("../shaders/frag.spv");
|
||||
let vert = include_bytes!("../shaders/vert_cube.spv");
|
||||
let geo = include_bytes!("../shaders/geo_cube.spv");
|
||||
let frag = include_bytes!("../shaders/frag_cube.spv");
|
||||
|
||||
let vert_shader_module = create_shader_module(device, &vert[..])?;
|
||||
let geo_shader_module = create_shader_module(device, &geo[..])?;
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
pub mod cube;
|
|
@ -2,6 +2,7 @@ use vulkanalia::prelude::v1_0::*;
|
|||
use cgmath::vec3;
|
||||
use crate::vertex;
|
||||
use crate::scene::Scene;
|
||||
use crate::primitives::drawable::Drawable;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Cube{
|
||||
|
@ -10,8 +11,8 @@ pub struct Cube{
|
|||
pub tex_coord: vertex::Vec2
|
||||
}
|
||||
|
||||
impl Cube {
|
||||
pub fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
|
||||
impl Drawable for Cube {
|
||||
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
|
||||
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
|
||||
// 0 top left far
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
|
|
6
src/primitives/drawable.rs
Normal file
6
src/primitives/drawable.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use vulkanalia::prelude::v1_0::*;
|
||||
use crate::scene::Scene;
|
||||
|
||||
pub trait Drawable {
|
||||
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene);
|
||||
}
|
3
src/primitives/mod.rs
Normal file
3
src/primitives/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
pub mod cube;
|
||||
pub mod rec_cuboid;
|
||||
pub mod drawable;
|
133
src/primitives/rec_cuboid.rs
Normal file
133
src/primitives/rec_cuboid.rs
Normal file
|
@ -0,0 +1,133 @@
|
|||
use vulkanalia::prelude::v1_0::*;
|
||||
use cgmath::vec3;
|
||||
use crate::vertex;
|
||||
use crate::scene::Scene;
|
||||
use crate::primitives::drawable::Drawable;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Cuboid{
|
||||
pub pos: vertex::Vec3,
|
||||
pub color: vertex::Vec3,
|
||||
pub tex_coord: vertex::Vec2,
|
||||
pub size: vertex::Vec3,
|
||||
}
|
||||
|
||||
impl Drawable for Cuboid {
|
||||
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
|
||||
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
|
||||
// 0 top left far
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 1 top right far
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 2 top left near
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 3 top right near
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
|
||||
// 4 bottom left far
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 5 bottom right far
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 6 bottom left near
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
// 7 bottom right near
|
||||
scene.vertices.push(vertex::Vertex::new(
|
||||
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
|
||||
self.color,
|
||||
self.tex_coord
|
||||
));
|
||||
|
||||
|
||||
// top
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 0);
|
||||
scene.indices.push(start_index as u32 + 2);
|
||||
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 1);
|
||||
scene.indices.push(start_index as u32 + 0);
|
||||
|
||||
// bottom
|
||||
scene.indices.push(start_index as u32 + 6);
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
scene.indices.push(start_index as u32 + 7);
|
||||
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
scene.indices.push(start_index as u32 + 5);
|
||||
scene.indices.push(start_index as u32 + 7);
|
||||
|
||||
// left
|
||||
scene.indices.push(start_index as u32 + 0);
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
scene.indices.push(start_index as u32 + 2);
|
||||
|
||||
scene.indices.push(start_index as u32 + 6);
|
||||
scene.indices.push(start_index as u32 + 2);
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
|
||||
// right
|
||||
scene.indices.push(start_index as u32 + 1);
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 5);
|
||||
|
||||
scene.indices.push(start_index as u32 + 5);
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 7);
|
||||
|
||||
// near
|
||||
scene.indices.push(start_index as u32 + 6);
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 2);
|
||||
|
||||
scene.indices.push(start_index as u32 + 3);
|
||||
scene.indices.push(start_index as u32 + 6);
|
||||
scene.indices.push(start_index as u32 + 7);
|
||||
|
||||
// far
|
||||
scene.indices.push(start_index as u32 + 0);
|
||||
scene.indices.push(start_index as u32 + 1);
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
|
||||
scene.indices.push(start_index as u32 + 5);
|
||||
scene.indices.push(start_index as u32 + 4);
|
||||
scene.indices.push(start_index as u32 + 1);
|
||||
}
|
||||
if *topology == vk::PrimitiveTopology::POINT_LIST {
|
||||
scene.sized_vertices.push(vertex::SizedVertex::new(
|
||||
self.pos,
|
||||
self.color,
|
||||
self.tex_coord,
|
||||
self.size,
|
||||
));
|
||||
scene.indices.push(start_index as u32);
|
||||
}
|
||||
}
|
||||
}
|
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -76,4 +76,75 @@ impl Hash for Vertex {
|
|||
self.tex_coord[0].to_bits().hash(state);
|
||||
self.tex_coord[1].to_bits().hash(state);
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct SizedVertex {
|
||||
pub pos: Vec3,
|
||||
pub color: Vec3,
|
||||
pub tex_coord: Vec2,
|
||||
pub size: Vec3,
|
||||
}
|
||||
|
||||
impl SizedVertex {
|
||||
pub const fn new(pos: Vec3, color: Vec3, tex_coord: Vec2, size: Vec3) -> Self {
|
||||
Self { pos, color, tex_coord, size }
|
||||
}
|
||||
|
||||
pub fn binding_description() -> vk::VertexInputBindingDescription {
|
||||
vk::VertexInputBindingDescription::builder()
|
||||
.binding(0)
|
||||
.stride(size_of::<Vertex>() as u32)
|
||||
.input_rate(vk::VertexInputRate::VERTEX)
|
||||
.build()
|
||||
}
|
||||
|
||||
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
|
||||
let pos = vk::VertexInputAttributeDescription::builder()
|
||||
.binding(0)
|
||||
.location(0)
|
||||
.format(vk::Format::R32G32B32_SFLOAT)
|
||||
.offset(0)
|
||||
.build();
|
||||
|
||||
let color = vk::VertexInputAttributeDescription::builder()
|
||||
.binding(0)
|
||||
.location(1)
|
||||
.format(vk::Format::R32G32B32_SFLOAT)
|
||||
.offset(size_of::<Vec3>() as u32)
|
||||
.build();
|
||||
|
||||
let tex_coord = vk::VertexInputAttributeDescription::builder()
|
||||
.binding(0)
|
||||
.location(2)
|
||||
.format(vk::Format::R32G32_SFLOAT)
|
||||
.offset((size_of::<Vec3>() + size_of::<Vec3>()) as u32)
|
||||
.build();
|
||||
[pos, color, tex_coord]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl PartialEq for SizedVertex {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.pos == other.pos
|
||||
&& self.color == other.color
|
||||
&& self.tex_coord == other.tex_coord
|
||||
}
|
||||
}
|
||||
|
||||
impl Eq for SizedVertex {}
|
||||
|
||||
impl Hash for SizedVertex {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
self.pos[0].to_bits().hash(state);
|
||||
self.pos[1].to_bits().hash(state);
|
||||
self.pos[2].to_bits().hash(state);
|
||||
self.color[0].to_bits().hash(state);
|
||||
self.color[1].to_bits().hash(state);
|
||||
self.color[2].to_bits().hash(state);
|
||||
self.tex_coord[0].to_bits().hash(state);
|
||||
self.tex_coord[1].to_bits().hash(state);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue