Added voxel and voxel map definitions

This commit is contained in:
Romain 2023-09-16 08:53:17 +02:00
parent b24aace092
commit f3167ac12d
2 changed files with 42 additions and 0 deletions

View File

@ -1,3 +1,5 @@
mod voxel;
use std::borrow::Cow;
use winit::{
event::{Event, WindowEvent},

40
src/voxel.rs Normal file
View File

@ -0,0 +1,40 @@
#[derive(Clone, Copy)]
struct Voxel {
r: u8,
g: u8,
b: u8
}
const AIR_VOXEL: Voxel = Voxel { r: 0, g: 0, b: 0 };
struct VoxelMap {
data: Vec<Voxel>,
width: usize,
height: usize,
depth: usize
}
impl VoxelMap {
fn new(width: usize, height: usize, depth: usize) -> Self {
Self {
data: vec![AIR_VOXEL, width * height * depth],
width,
height,
depth
}
}
fn set(&mut self, x: usize, y: usize, z: usize, voxel: Voxel) {
if x < self.width && y < self.height && z < self.depth {
self.data[x + y * self.width + z * self.width * self.height] = voxel;
}
}
fn get(&self, x: usize, y: usize, z: usize) -> Voxel {
if x < self.width && y < self.height && z < self.depth {
self.data[x + y * self.width + z * self.width * self.height]
} else {
AIR_VOXEL
}
}
}