134 lines
3.6 KiB
Rust
134 lines
3.6 KiB
Rust
use crate::{
|
|
config::VOXEL_SCALE,
|
|
coordinates::{self, Coordinates},
|
|
vertex::Vertex,
|
|
};
|
|
|
|
#[repr(C)]
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
pub struct Light {
|
|
pub red: u8,
|
|
pub green: u8,
|
|
pub blue: u8,
|
|
pub sun: u8,
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
pub struct VoxelData {
|
|
pub id: u32,
|
|
pub light_level: Light,
|
|
}
|
|
impl VoxelData {
|
|
pub fn new(id: u32, light_level: Light) -> Self {
|
|
Self { id, light_level }
|
|
}
|
|
|
|
pub fn construct_vertexes(&self, coordinates: Coordinates) -> Vec<Vertex> {
|
|
let mut shape = Vec::new();
|
|
let half_voxel = VOXEL_SCALE / 2.0;
|
|
{
|
|
// Top Hexagon
|
|
let vert_center = Vertex {
|
|
position: (coordinates.x, coordinates.y, coordinates.z),
|
|
};
|
|
|
|
let vert_1 = Vertex {
|
|
position: (
|
|
coordinates.x - half_voxel,
|
|
coordinates.y,
|
|
coordinates.z + VOXEL_SCALE,
|
|
),
|
|
};
|
|
let vert_2 = Vertex {
|
|
position: (
|
|
coordinates.x + half_voxel,
|
|
coordinates.y,
|
|
coordinates.z + VOXEL_SCALE,
|
|
),
|
|
};
|
|
|
|
let vert_3 = Vertex {
|
|
position: (coordinates.x + VOXEL_SCALE, coordinates.y, coordinates.z),
|
|
};
|
|
|
|
let vert_4 = Vertex {
|
|
position: (
|
|
coordinates.x + half_voxel,
|
|
coordinates.y,
|
|
coordinates.z - VOXEL_SCALE,
|
|
),
|
|
};
|
|
|
|
let vert_5 = Vertex {
|
|
position: (
|
|
coordinates.x - half_voxel,
|
|
coordinates.y,
|
|
coordinates.z - VOXEL_SCALE,
|
|
),
|
|
};
|
|
|
|
let vert_6 = Vertex {
|
|
position: (coordinates.x - VOXEL_SCALE, coordinates.y, coordinates.z),
|
|
};
|
|
|
|
// TRIANGLE 1
|
|
shape.append(&mut vec![vert_1, vert_2, vert_center]);
|
|
|
|
// TRIANGLE 2
|
|
shape.append(&mut vec![vert_2, vert_3, vert_center]);
|
|
|
|
// TRIANGLE 3
|
|
shape.append(&mut vec![vert_3, vert_4, vert_center]);
|
|
|
|
// TRIANGLE 4
|
|
shape.append(&mut vec![vert_4, vert_5, vert_center]);
|
|
|
|
// TRIANGLE 5
|
|
shape.append(&mut vec![vert_5, vert_6, vert_center]);
|
|
|
|
// TRIANGLE 6
|
|
shape.append(&mut vec![vert_6, vert_1, vert_center]);
|
|
}
|
|
{
|
|
// Side Face 1
|
|
let vert_1 = Vertex {
|
|
position: (
|
|
coordinates.x - VOXEL_SCALE,
|
|
coordinates.y + VOXEL_SCALE,
|
|
coordinates.z,
|
|
),
|
|
};
|
|
let vert_2 = Vertex {
|
|
position: (
|
|
coordinates.x + VOXEL_SCALE,
|
|
coordinates.y + VOXEL_SCALE,
|
|
coordinates.z,
|
|
),
|
|
};
|
|
|
|
let vert_3 = Vertex {
|
|
position: (
|
|
coordinates.x - VOXEL_SCALE,
|
|
coordinates.y - VOXEL_SCALE,
|
|
coordinates.z,
|
|
),
|
|
};
|
|
let vert_4 = Vertex {
|
|
position: (
|
|
coordinates.x + VOXEL_SCALE,
|
|
coordinates.y - VOXEL_SCALE,
|
|
coordinates.z,
|
|
),
|
|
};
|
|
|
|
// TRIANGLE 1
|
|
shape.append(&mut vec![vert_1, vert_2, vert_3]);
|
|
|
|
// TRIANGLE 2
|
|
shape.append(&mut vec![vert_2, vert_3, vert_4]);
|
|
}
|
|
|
|
shape
|
|
}
|
|
}
|