Added voxel and voxel map definitions
This commit is contained in:
parent
b24aace092
commit
72ebb412f0
@ -1,3 +1,5 @@
|
||||
mod voxel;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use winit::{
|
||||
event::{Event, WindowEvent},
|
||||
|
61
src/voxel.rs
Normal file
61
src/voxel.rs
Normal file
@ -0,0 +1,61 @@
|
||||
#[derive(PartialEq, Clone, Copy, Debug)]
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_voxel_map() {
|
||||
let mut map = VoxelMap::new(10, 10, 10);
|
||||
map.set(0, 0, 0, Voxel { r: 255, g: 0, b: 0 });
|
||||
assert_eq!(map.get(0, 0, 0), Voxel { r: 255, g: 0, b: 0 });
|
||||
assert_eq!(map.get(1, 0, 0), AIR_VOXEL);
|
||||
assert_eq!(map.get(0, 1, 0), AIR_VOXEL);
|
||||
assert_eq!(map.get(0, 0, 1), AIR_VOXEL);
|
||||
assert_eq!(map.get(10, 0, 0), AIR_VOXEL);
|
||||
assert_eq!(map.get(0, 10, 0), AIR_VOXEL);
|
||||
assert_eq!(map.get(0, 0, 10), AIR_VOXEL);
|
||||
|
||||
// out of bounds
|
||||
assert_eq!(map.get(0, 0, 11), AIR_VOXEL);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user