Fix cube faces

This commit is contained in:
Michael Huebner 2025-03-10 13:59:51 +01:00
parent fe9099aac9
commit 45c0a31cb3

View file

@ -1,22 +1,49 @@
use engine::prelude::{cgmath::*, *}; use engine::prelude::{cgmath::*, *};
#[derive(Debug)]
pub struct CubeCorners(pub [PositionOnly; 8]); pub struct CubeCorners(pub [PositionOnly; 8]);
impl CubeCorners { impl CubeCorners {
#[rustfmt::skip]
pub fn new(offset: Vector3<f32>, extent: Vector3<f32>) -> Self { pub fn new(offset: Vector3<f32>, extent: Vector3<f32>) -> Self {
Self([ Self([
// 0 - left bottom front // 0 - left bottom front
PositionOnly::new(vec3(offset.x, offset.y, offset.z)), PositionOnly::new(vec3(
offset.x,
offset.y,
offset.z
)),
// 1 - right bottom front // 1 - right bottom front
PositionOnly::new(vec3(offset.x + extent.x, offset.y, offset.z)), PositionOnly::new(vec3(
offset.x + extent.x,
offset.y,
offset.z
)),
// 2 - right top front // 2 - right top front
PositionOnly::new(vec3(offset.x + extent.x, offset.y + extent.y, offset.z)), PositionOnly::new(vec3(
offset.x + extent.x,
offset.y + extent.y,
offset.z
)),
// 3 - left top front // 3 - left top front
PositionOnly::new(vec3(offset.x, offset.y + extent.y, offset.z)), PositionOnly::new(vec3(
offset.x,
offset.y + extent.y,
offset.z
)),
// 4 - left bottom back // 4 - left bottom back
PositionOnly::new(vec3(offset.x, offset.y, offset.z + extent.z)), PositionOnly::new(vec3(
offset.x,
offset.y,
offset.z + extent.z
)),
// 5 - right bottom back // 5 - right bottom back
PositionOnly::new(vec3(offset.x + extent.x, offset.y, offset.z + extent.z)), PositionOnly::new(vec3(
offset.x + extent.x,
offset.y,
offset.z + extent.z
)),
// 6 - right top back // 6 - right top back
PositionOnly::new(vec3( PositionOnly::new(vec3(
offset.x + extent.x, offset.x + extent.x,
@ -24,11 +51,16 @@ impl CubeCorners {
offset.z + extent.z, offset.z + extent.z,
)), )),
// 7 - left top back // 7 - left top back
PositionOnly::new(vec3(offset.x, offset.y + extent.y, offset.z + extent.z)), PositionOnly::new(vec3(
offset.x,
offset.y + extent.y,
offset.z + extent.z
)),
]) ])
} }
} }
#[derive(Debug)]
pub struct Plane([PositionOnly; 4]); pub struct Plane([PositionOnly; 4]);
impl Plane { impl Plane {
@ -45,9 +77,10 @@ impl<'a> From<(&'a CubeCorners, usize, usize, usize, usize)> for Plane {
} }
} }
#[derive(Debug)]
pub struct Cube { pub struct Cube {
pub left: Plane,
pub right: Plane, pub right: Plane,
pub left: Plane,
pub front: Plane, pub front: Plane,
pub back: Plane, pub back: Plane,
pub top: Plane, pub top: Plane,
@ -57,9 +90,9 @@ pub struct Cube {
impl Cube { impl Cube {
pub fn triangulate(self) -> [PositionOnly; 36] { pub fn triangulate(self) -> [PositionOnly; 36] {
[ [
self.front.triangulate(),
self.left.triangulate(),
self.right.triangulate(), self.right.triangulate(),
self.left.triangulate(),
self.front.triangulate(),
self.back.triangulate(), self.back.triangulate(),
self.top.triangulate(), self.top.triangulate(),
self.bottom.triangulate(), self.bottom.triangulate(),
@ -73,8 +106,8 @@ impl Cube {
impl From<CubeCorners> for Cube { impl From<CubeCorners> for Cube {
fn from(corners: CubeCorners) -> Self { fn from(corners: CubeCorners) -> Self {
Self { Self {
left: Plane::from((&corners, 0, 3, 4, 7)), right: Plane::from((&corners, 1, 2, 6, 5)),
right: Plane::from((&corners, 2, 1, 6, 5)), left: Plane::from((&corners, 3, 0, 4, 7)),
front: Plane::from((&corners, 0, 1, 2, 3)), front: Plane::from((&corners, 0, 1, 2, 3)),
back: Plane::from((&corners, 5, 4, 7, 6)), back: Plane::from((&corners, 5, 4, 7, 6)),
top: Plane::from((&corners, 3, 2, 6, 7)), top: Plane::from((&corners, 3, 2, 6, 7)),