diff --git a/game/src/engine/game.rs b/game/src/engine/game.rs index e48a079..ea2a310 100644 --- a/game/src/engine/game.rs +++ b/game/src/engine/game.rs @@ -20,9 +20,11 @@ pub trait Renderer { #[derive(Clone, Copy)] pub enum Color { + HEX(u32), WHITE, GREEN, - WED, + RED, + CYAN, } pub enum Event { diff --git a/game/src/engine/math.rs b/game/src/engine/math.rs index d598ada..d75d056 100644 --- a/game/src/engine/math.rs +++ b/game/src/engine/math.rs @@ -20,11 +20,24 @@ impl V3 { let V3(bx, by, bz) = rhs; Self(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx) } + pub fn dot(&self, rhs: Self) -> f64 { let V3(ax, ay, az) = self; let V3(bx, by, bz) = rhs; ax * bx + ay * by + az * bz } + + pub fn len(&self) -> f64 { + f64::sqrt(self.0.powi(2) + self.1.powi(2) + self.2.powi(2)) + } + + pub fn unit(&self) -> V3 { + self.map(|v| v / self.len()) + } + + pub fn map f64>(&self, func: F) -> Self { + V3(func(self.0), func(self.1), func(self.2)) + } } impl Add for V3 { diff --git a/game/src/engine/r3d.rs b/game/src/engine/r3d.rs index 3a1de51..5031ad3 100644 --- a/game/src/engine/r3d.rs +++ b/game/src/engine/r3d.rs @@ -22,6 +22,11 @@ impl Triangle3 { self.2.project_2d(), ) } + + pub fn middle(&self) -> V3 { + let m1 = self.0 + (self.1 - self.0).map(|v| v / 2.0); + m1 + (self.2 - m1).map(|v| v / 2.0) + } } pub struct R3d<'r, R: Renderer> { @@ -47,13 +52,16 @@ impl<'r, R: Renderer> R3d<'r, R> { self.r.draw_line(triangle.2, triangle.0, color); } - pub fn draw_cube(&mut self, pos: V3, size: V3, outline_color: Color, fill_color: Color) { - let shape = Shape::new_cube(size); - + pub fn draw_shape(&mut self, pos: V3, shape: &Shape, outline_color: Color, fill_color: Color) { for face in shape.faces() { let normal_vector = face.normal_vector(); - let pxyz = face.0 + normal_vector; - self.draw_line(face.0 + pos, pxyz + pos, Color::WED); + let pxyz = face.middle() + normal_vector; + self.draw_line(face.middle() + pos, pxyz + pos, Color::RED); + // self.draw_line( + // face.middle() + pos, + // face.middle() + face.0.unit() * 0.1 + pos, + // Color::HEX(0x33dd33), + // ); let bingbongvector = face.0 - CAMERA_POS + pos; if normal_vector.dot(bingbongvector) < 0.0 { @@ -61,9 +69,9 @@ impl<'r, R: Renderer> R3d<'r, R> { } } - for vertex in shape.vertices() { - self.r - .draw_point((vertex + pos).project_2d(), outline_color); - } + // for vertex in shape.vertices() { + // self.r + // .draw_point((vertex + pos).project_2d(), outline_color); + // } } } diff --git a/game/src/engine/sdl_io.rs b/game/src/engine/sdl_io.rs index 2dbe32c..e9d7ac7 100644 --- a/game/src/engine/sdl_io.rs +++ b/game/src/engine/sdl_io.rs @@ -148,9 +148,11 @@ impl From for FPoint { impl From for SdlColor { fn from(value: Color) -> Self { match value { + Color::HEX(v) => SdlColor::from_u32(&PixelFormat::BGR24, v), Color::WHITE => SdlColor::WHITE, Color::GREEN => SdlColor::GREEN, - Color::WED => SdlColor::RED, + Color::RED => SdlColor::RED, + Color::CYAN => SdlColor::CYAN, } } } diff --git a/game/src/engine/shapes.rs b/game/src/engine/shapes.rs index 4979d16..8f01f33 100644 --- a/game/src/engine/shapes.rs +++ b/game/src/engine/shapes.rs @@ -1,4 +1,4 @@ -use crate::engine::{math::V3, Triangle3}; +use crate::engine::{math::V3, Triangle3, V2}; static CUBE_VERTICES: [(i8, i8, i8); 8] = [ (0, 1, 0), // 0 front top left @@ -47,6 +47,27 @@ static CUBE_FACES: [(usize, usize, usize); 12] = [ (2, 6, 0), ]; +static PLANE_VERTICES: [(i8, i8, i8); 4] = [ + // + (0, 0, 0), + (0, 0, 1), + (1, 0, 0), + (1, 0, 1), +]; +static PLANE_EDGES: [(usize, usize); 5] = [ + // + (0, 1), + (1, 2), + (2, 3), + (0, 2), + (1, 3), +]; +static PLANE_FACES: [(usize, usize, usize); 2] = [ + // + (0, 1, 2), + (3, 2, 1), +]; + pub struct Shape { vertices: Vec, edges: Vec<(usize, usize)>, @@ -54,6 +75,17 @@ pub struct Shape { } impl Shape { + pub fn new_plane(dim: V3) -> Self { + Self { + vertices: PLANE_VERTICES + .iter() + .map(|p| scale_i8_vertex(*p, &dim)) + .collect(), + edges: Vec::from_iter(PLANE_EDGES), + faces: Vec::from_iter(PLANE_FACES), + } + } + pub fn new_cube(dim: V3) -> Self { Self { vertices: CUBE_VERTICES diff --git a/game/src/main.rs b/game/src/main.rs index 4f96463..dd9c5ed 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -2,7 +2,7 @@ use std::time::Duration; -use crate::engine::{Color, R3d, V3}; +use crate::engine::{Color, R3d, Shape, Triangle3, V3}; mod engine; @@ -15,6 +15,9 @@ impl engine::Object for MyObject { match self { MyObject::Player { pos, vel } => { *pos += *vel * delta_time.as_secs_f64(); + if pos.0 >= 1.0 { + pos.0 = -1.0; + } } } } @@ -23,10 +26,27 @@ impl engine::Object for MyObject { match self { MyObject::Player { pos, .. } => { let mut r3d = R3d::new(r); - r3d.draw_cube(*pos, V3(0.2, 0.2, 0.2), Color::GREEN, Color::WHITE); - r3d.draw_cube( + + for z in 0..10 { + for x in -10..10 { + r3d.draw_shape( + V3(x as f64 * 0.1, -0.5, z as f64 * 0.1), + &Shape::new_plane(V3(0.1, 0.0, 0.1)), + Color::CYAN, + Color::WHITE, + ); + } + } + + r3d.draw_shape( + *pos + V3(0.0, 0.2, 0.0), + &Shape::new_cube(V3(0.2, 0.1, 0.2)), + Color::GREEN, + Color::WHITE, + ); + r3d.draw_shape( *pos + V3(-0.4, -0.2, 0.0), - V3(0.2, 0.2, 0.2), + &Shape::new_cube(V3(0.2, 0.2, 0.2)), Color::GREEN, Color::WHITE, ); @@ -40,8 +60,8 @@ fn main() -> Result<(), Box> { let mut game = engine::Game::::new()?; let player = MyObject::Player { - pos: V3(-0.1, -0.1, 0.0), - vel: V3(0.0, 0.1, 0.0), + pos: V3(0.0, -0.1, 0.0), + vel: V3(0.4, 0.0, 0.0), }; game.spawn(player);