mirror of
https://github.com/Mercantec-GHC/h5-projekt-mst.git
synced 2026-08-27 05:37:39 +02:00
draw shapes
This commit is contained in:
parent
a4ccb9c781
commit
59bcd540fe
@ -20,9 +20,11 @@ pub trait Renderer {
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub enum Color {
|
||||
HEX(u32),
|
||||
WHITE,
|
||||
GREEN,
|
||||
WED,
|
||||
RED,
|
||||
CYAN,
|
||||
}
|
||||
|
||||
pub enum Event {
|
||||
|
||||
@ -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<F: Fn(f64) -> f64>(&self, func: F) -> Self {
|
||||
V3(func(self.0), func(self.1), func(self.2))
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for V3 {
|
||||
|
||||
@ -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);
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@ -148,9 +148,11 @@ impl From<V2> for FPoint {
|
||||
impl From<Color> 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,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<V3>,
|
||||
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
|
||||
|
||||
@ -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<R: engine::Renderer> engine::Object<R> 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<R: engine::Renderer> engine::Object<R> 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<dyn std::error::Error>> {
|
||||
let mut game = engine::Game::<engine::SdlIo, MyObject>::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);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user