add shape

This commit is contained in:
sfja 2026-03-24 23:36:31 +01:00
parent 0032673ff8
commit 133fafe459
4 changed files with 75 additions and 35 deletions

View File

@ -1,8 +1,9 @@
use std::ops::{Add, AddAssign, Mul, MulAssign}; use std::ops::{Add, AddAssign, Mul, MulAssign};
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct V2(pub f64, pub f64); pub struct V2(pub f64, pub f64);
#[derive(Copy, Clone)] #[derive(Clone, Copy, PartialEq, Debug)]
pub struct V3(pub f64, pub f64, pub f64); pub struct V3(pub f64, pub f64, pub f64);
impl Add for V3 { impl Add for V3 {
@ -39,41 +40,28 @@ impl V3 {
} }
} }
pub struct Vertex(V3, V3, V3); #[derive(Clone, PartialEq, Debug)]
pub struct Vertex(pub V3, pub V3, pub V3);
pub struct Cube { #[cfg(test)]
vertices: [Vertex; 12], mod test {
} use super::*;
impl Cube { #[test]
pub fn new(V3(x, y, z): V3, V3(w, h, d): V3) -> Self { fn v3_ops() {
Self { let v2 = |v: f64| V2(v, v);
vertices: [ let v3 = |v: f64| V3(v, v, v);
Vertex(V3(x, y, z), V3(x, y + h, z), V3(x + w, y + h, z)),
Vertex(V3(x + w, y + h, z), V3(x + w, y, z), V3(x, y + h, z)), assert_eq!(v3(1.0) + v3(2.0), v3(3.0));
Vertex(V3(x + w, y, z), V3(x + w, y + h, z), V3(x + w, y, z + d)),
Vertex( let mut a = v3(1.0);
V3(x + w, y + h, z + d), a += v3(2.0);
V3(x + w, y, z + d), assert_eq!(a, v3(3.0));
V3(x, y + h, z + d),
), assert_eq!(v3(2.0) * 2.0, v3(4.0));
Vertex(V3(x, y + h, z), V3(x + w, y + h, z), V3(x, y + h, z + d)),
Vertex( let mut a = v3(2.0);
V3(x + w, y + h, z), a *= 2.0;
V3(x + w, y + h, z + d), assert_eq!(a, v3(4.0));
V3(x, y + h, z + d),
),
Vertex(V3(x, y, z), V3(x, y + h, z), V3(x, y, z + d)),
Vertex(V3(x, y + h, z), V3(x, y + h, z + d), V3(x, y, z + d)),
Vertex(V3(x, y, z), V3(x + w, y, z), V3(x, y, z + d)),
Vertex(V3(x + w, y, z), V3(x + w, y, z + d), V3(x, y, z + d)),
Vertex(V3(x, y, z + d), V3(x + w, y, z + d), V3(x, y + h, z + d)),
Vertex(
V3(x + w, y, z + d),
V3(x + w, y + h, z + d),
V3(x, y + h, z + d),
),
],
}
} }
} }

View File

@ -2,6 +2,7 @@ mod error;
mod game; mod game;
pub mod math; pub mod math;
mod sdl_io; mod sdl_io;
pub mod shapes;
mod system; mod system;
pub use game::{Event, Game, Io, Object, Renderer}; pub use game::{Event, Game, Io, Object, Renderer};

50
game/src/engine/shapes.rs Normal file
View File

@ -0,0 +1,50 @@
use crate::engine::math::{Vertex, V3};
pub struct Shape {
vertices: Vec<V3>,
fragments: Vec<(usize, usize, usize)>,
}
impl Shape {
pub fn new_cube(V3(x, y, z): V3, V3(w, h, d): V3) -> Self {
Self {
vertices: vec![
V3(x, y, z), // 0 front top left
V3(x + w, y, z), // 1 front top right
V3(x, y + h, z), // 2 front bottom left
V3(x + w, y + h, z), // 3 front bottom right
V3(x, y, z + d), // 4 back top left
V3(x + w, y, z + d), // 5 back top right
V3(x, y + h, z + d), // 6 back bottom left
V3(x + w, y + h, z + d), // 7 back bottom right
],
fragments: vec![
// back
(0, 1, 2),
(3, 2, 1),
// bottom
(4, 5, 0),
(1, 0, 5),
// left
(4, 0, 6),
(2, 6, 0),
// front
(5, 4, 7),
(6, 7, 4),
// top
(7, 6, 3),
(2, 3, 6),
// right
(1, 5, 3),
(7, 3, 5),
],
}
}
pub fn iter<'a>(&'a self) -> impl Iterator<Item = Vertex> + 'a {
let verts: &[V3] = &self.vertices;
self.fragments
.iter()
.map(|(a, b, c)| Vertex(verts[*a], verts[*b], verts[*c]))
}
}

View File

@ -1,4 +1,5 @@
#![allow(dead_code)] #![allow(dead_code)]
#![allow(unused)]
use std::time::Duration; use std::time::Duration;