engine/skybox/src/cube.rs

118 lines
3.1 KiB
Rust
Raw Normal View History

2025-03-10 11:34:38 +00:00
use engine::prelude::{cgmath::*, *};
2025-03-10 12:59:51 +00:00
#[derive(Debug)]
2025-03-10 11:34:38 +00:00
pub struct CubeCorners(pub [PositionOnly; 8]);
impl CubeCorners {
2025-03-10 12:59:51 +00:00
#[rustfmt::skip]
2025-03-10 11:34:38 +00:00
pub fn new(offset: Vector3<f32>, extent: Vector3<f32>) -> Self {
Self([
// 0 - left bottom front
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x,
offset.y,
offset.z
)),
2025-03-10 11:34:38 +00:00
// 1 - right bottom front
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x + extent.x,
offset.y,
offset.z
)),
2025-03-10 11:34:38 +00:00
// 2 - right top front
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x + extent.x,
offset.y + extent.y,
offset.z
)),
2025-03-10 11:34:38 +00:00
// 3 - left top front
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x,
offset.y + extent.y,
offset.z
)),
2025-03-10 11:34:38 +00:00
// 4 - left bottom back
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x,
offset.y,
offset.z + extent.z
)),
2025-03-10 11:34:38 +00:00
// 5 - right bottom back
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x + extent.x,
offset.y,
offset.z + extent.z
)),
2025-03-10 11:34:38 +00:00
// 6 - right top back
PositionOnly::new(vec3(
offset.x + extent.x,
offset.y + extent.y,
offset.z + extent.z,
)),
// 7 - left top back
2025-03-10 12:59:51 +00:00
PositionOnly::new(vec3(
offset.x,
offset.y + extent.y,
offset.z + extent.z
)),
2025-03-10 11:34:38 +00:00
])
}
}
2025-03-10 12:59:51 +00:00
#[derive(Debug)]
2025-03-10 11:34:38 +00:00
pub struct Plane([PositionOnly; 4]);
impl Plane {
pub fn triangulate(self) -> [PositionOnly; 6] {
[
self.0[0], self.0[1], self.0[2], self.0[2], self.0[3], self.0[0],
]
}
}
impl<'a> From<(&'a CubeCorners, usize, usize, usize, usize)> for Plane {
fn from((corners, i1, i2, i3, i4): (&'a CubeCorners, usize, usize, usize, usize)) -> Self {
Self([corners.0[i1], corners.0[i2], corners.0[i3], corners.0[i4]])
}
}
2025-03-10 12:59:51 +00:00
#[derive(Debug)]
2025-03-10 11:34:38 +00:00
pub struct Cube {
pub right: Plane,
2025-03-10 12:59:51 +00:00
pub left: Plane,
2025-03-10 11:34:38 +00:00
pub front: Plane,
pub back: Plane,
pub top: Plane,
pub bottom: Plane,
}
impl Cube {
pub fn triangulate(self) -> [PositionOnly; 36] {
[
self.right.triangulate(),
2025-03-10 12:59:51 +00:00
self.left.triangulate(),
self.front.triangulate(),
2025-03-10 11:34:38 +00:00
self.back.triangulate(),
self.top.triangulate(),
self.bottom.triangulate(),
]
.concat()
.try_into()
.unwrap()
}
}
impl From<CubeCorners> for Cube {
fn from(corners: CubeCorners) -> Self {
Self {
2025-03-10 12:59:51 +00:00
right: Plane::from((&corners, 1, 2, 6, 5)),
left: Plane::from((&corners, 3, 0, 4, 7)),
2025-03-10 11:34:38 +00:00
front: Plane::from((&corners, 0, 1, 2, 3)),
back: Plane::from((&corners, 5, 4, 7, 6)),
top: Plane::from((&corners, 3, 2, 6, 7)),
bottom: Plane::from((&corners, 5, 4, 0, 1)),
}
}
}