draw shapes

This commit is contained in:
sfja 2026-03-26 18:44:14 +01:00
parent a4ccb9c781
commit 59bcd540fe
6 changed files with 95 additions and 18 deletions

View File

@ -20,9 +20,11 @@ pub trait Renderer {
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum Color { pub enum Color {
HEX(u32),
WHITE, WHITE,
GREEN, GREEN,
WED, RED,
CYAN,
} }
pub enum Event { pub enum Event {

View File

@ -20,11 +20,24 @@ impl V3 {
let V3(bx, by, bz) = rhs; let V3(bx, by, bz) = rhs;
Self(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx) Self(ay * bz - az * by, az * bx - ax * bz, ax * by - ay * bx)
} }
pub fn dot(&self, rhs: Self) -> f64 { pub fn dot(&self, rhs: Self) -> f64 {
let V3(ax, ay, az) = self; let V3(ax, ay, az) = self;
let V3(bx, by, bz) = rhs; let V3(bx, by, bz) = rhs;
ax * bx + ay * by + az * bz 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<F: Fn(f64) -> f64>(&self, func: F) -> Self {
V3(func(self.0), func(self.1), func(self.2))
}
} }
impl Add for V3 { impl Add for V3 {

View File

@ -22,6 +22,11 @@ impl Triangle3 {
self.2.project_2d(), 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> { 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); 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) { pub fn draw_shape(&mut self, pos: V3, shape: &Shape, outline_color: Color, fill_color: Color) {
let shape = Shape::new_cube(size);
for face in shape.faces() { for face in shape.faces() {
let normal_vector = face.normal_vector(); let normal_vector = face.normal_vector();
let pxyz = face.0 + normal_vector; let pxyz = face.middle() + normal_vector;
self.draw_line(face.0 + pos, pxyz + pos, Color::WED); 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; let bingbongvector = face.0 - CAMERA_POS + pos;
if normal_vector.dot(bingbongvector) < 0.0 { if normal_vector.dot(bingbongvector) < 0.0 {
@ -61,9 +69,9 @@ impl<'r, R: Renderer> R3d<'r, R> {
} }
} }
for vertex in shape.vertices() { // for vertex in shape.vertices() {
self.r // self.r
.draw_point((vertex + pos).project_2d(), outline_color); // .draw_point((vertex + pos).project_2d(), outline_color);
} // }
} }
} }

View File

@ -148,9 +148,11 @@ impl From<V2> for FPoint {
impl From<Color> for SdlColor { impl From<Color> for SdlColor {
fn from(value: Color) -> Self { fn from(value: Color) -> Self {
match value { match value {
Color::HEX(v) => SdlColor::from_u32(&PixelFormat::BGR24, v),
Color::WHITE => SdlColor::WHITE, Color::WHITE => SdlColor::WHITE,
Color::GREEN => SdlColor::GREEN, Color::GREEN => SdlColor::GREEN,
Color::WED => SdlColor::RED, Color::RED => SdlColor::RED,
Color::CYAN => SdlColor::CYAN,
} }
} }
} }

View File

@ -1,4 +1,4 @@
use crate::engine::{math::V3, Triangle3}; use crate::engine::{math::V3, Triangle3, V2};
static CUBE_VERTICES: [(i8, i8, i8); 8] = [ static CUBE_VERTICES: [(i8, i8, i8); 8] = [
(0, 1, 0), // 0 front top left (0, 1, 0), // 0 front top left
@ -47,6 +47,27 @@ static CUBE_FACES: [(usize, usize, usize); 12] = [
(2, 6, 0), (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 { pub struct Shape {
vertices: Vec<V3>, vertices: Vec<V3>,
edges: Vec<(usize, usize)>, edges: Vec<(usize, usize)>,
@ -54,6 +75,17 @@ pub struct Shape {
} }
impl 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 { pub fn new_cube(dim: V3) -> Self {
Self { Self {
vertices: CUBE_VERTICES vertices: CUBE_VERTICES

View File

@ -2,7 +2,7 @@
use std::time::Duration; use std::time::Duration;
use crate::engine::{Color, R3d, V3}; use crate::engine::{Color, R3d, Shape, Triangle3, V3};
mod engine; mod engine;
@ -15,6 +15,9 @@ impl<R: engine::Renderer> engine::Object<R> for MyObject {
match self { match self {
MyObject::Player { pos, vel } => { MyObject::Player { pos, vel } => {
*pos += *vel * delta_time.as_secs_f64(); *pos += *vel * delta_time.as_secs_f64();
if pos.0 >= 1.0 {
pos.0 = -1.0;
}
} }
} }
} }
@ -23,10 +26,27 @@ impl<R: engine::Renderer> engine::Object<R> for MyObject {
match self { match self {
MyObject::Player { pos, .. } => { MyObject::Player { pos, .. } => {
let mut r3d = R3d::new(r); 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), *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::GREEN,
Color::WHITE, Color::WHITE,
); );
@ -40,8 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut game = engine::Game::<engine::SdlIo, MyObject>::new()?; let mut game = engine::Game::<engine::SdlIo, MyObject>::new()?;
let player = MyObject::Player { let player = MyObject::Player {
pos: V3(-0.1, -0.1, 0.0), pos: V3(0.0, -0.1, 0.0),
vel: V3(0.0, 0.1, 0.0), vel: V3(0.4, 0.0, 0.0),
}; };
game.spawn(player); game.spawn(player);