mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-27 05:37:39 +02:00
add shape
This commit is contained in:
parent
0032673ff8
commit
133fafe459
@ -1,8 +1,9 @@
|
||||
use std::ops::{Add, AddAssign, Mul, MulAssign};
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub struct V2(pub f64, pub f64);
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
#[derive(Clone, Copy, PartialEq, Debug)]
|
||||
pub struct V3(pub f64, pub f64, pub f64);
|
||||
|
||||
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 {
|
||||
vertices: [Vertex; 12],
|
||||
}
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
|
||||
impl Cube {
|
||||
pub fn new(V3(x, y, z): V3, V3(w, h, d): V3) -> Self {
|
||||
Self {
|
||||
vertices: [
|
||||
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)),
|
||||
Vertex(V3(x + w, y, z), V3(x + w, y + h, z), V3(x + w, y, z + d)),
|
||||
Vertex(
|
||||
V3(x + w, y + h, z + d),
|
||||
V3(x + w, y, z + d),
|
||||
V3(x, y + h, z + d),
|
||||
),
|
||||
Vertex(V3(x, y + h, z), V3(x + w, y + h, z), V3(x, y + h, z + d)),
|
||||
Vertex(
|
||||
V3(x + w, y + h, z),
|
||||
V3(x + w, y + h, z + d),
|
||||
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),
|
||||
),
|
||||
],
|
||||
}
|
||||
#[test]
|
||||
fn v3_ops() {
|
||||
let v2 = |v: f64| V2(v, v);
|
||||
let v3 = |v: f64| V3(v, v, v);
|
||||
|
||||
assert_eq!(v3(1.0) + v3(2.0), v3(3.0));
|
||||
|
||||
let mut a = v3(1.0);
|
||||
a += v3(2.0);
|
||||
assert_eq!(a, v3(3.0));
|
||||
|
||||
assert_eq!(v3(2.0) * 2.0, v3(4.0));
|
||||
|
||||
let mut a = v3(2.0);
|
||||
a *= 2.0;
|
||||
assert_eq!(a, v3(4.0));
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ mod error;
|
||||
mod game;
|
||||
pub mod math;
|
||||
mod sdl_io;
|
||||
pub mod shapes;
|
||||
mod system;
|
||||
|
||||
pub use game::{Event, Game, Io, Object, Renderer};
|
||||
|
||||
50
game/src/engine/shapes.rs
Normal file
50
game/src/engine/shapes.rs
Normal 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]))
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused)]
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user