This commit is contained in:
bronku 2026-02-16 17:46:34 +01:00
parent 0f6b242ebf
commit f74f056836
2 changed files with 62 additions and 12 deletions

View file

@ -4,6 +4,7 @@ use raylib::prelude::*;
#[derive(Debug, Clone)]
pub struct Object {
indices: Vec<Vec3>,
faces: Vec<[usize; 3]>,
position: Vec3,
}
@ -19,25 +20,71 @@ pub fn new_cube() -> Object {
Vec3(0.5, 0.5, 0.5),
Vec3(-0.5, 0.5, 0.5),
],
faces: vec![
[0, 1, 2],
[2, 3, 0],
[4, 5, 6],
[6, 7, 4],
[1, 2, 5],
[2, 6, 5],
[4, 3, 0],
[4, 7, 3],
[0, 1, 5],
[5, 4, 0],
[2, 3, 6],
[3, 7, 6],
],
position: Vec3(0.0, 0.0, 0.0),
}
}
fn draw_point(d: &mut RaylibDrawHandle, pos: &Vec3) {
fn _draw_point(d: &mut RaylibDrawHandle, pos: &(i32, i32)) {
const POINT_SIZE: i32 = 3;
let (window_x, window_y) = pos.to_screen_space();
d.draw_rectangle(window_x, window_y, POINT_SIZE, POINT_SIZE, Color::RED);
d.draw_rectangle(pos.0, pos.1, POINT_SIZE, POINT_SIZE, Color::RED);
}
fn draw_triangle(
d: &mut RaylibDrawHandle,
screen_pos: &Vec<(i32, i32)>,
vertices: &[usize; 3],
color: Color,
) {
let v0: Vector2 = Vector2 {
x: screen_pos[vertices[0]].0 as f32,
y: screen_pos[vertices[0]].1 as f32,
};
let v1: Vector2 = Vector2 {
x: screen_pos[vertices[1]].0 as f32,
y: screen_pos[vertices[1]].1 as f32,
};
let v2: Vector2 = Vector2 {
x: screen_pos[vertices[2]].0 as f32,
y: screen_pos[vertices[2]].1 as f32,
};
d.draw_triangle(v2, v1, v0, color);
}
impl Object {
pub fn draw_object(&self, d: &mut RaylibDrawHandle) {
for point in &self.indices {
let moved = Vec3(
point.0 + self.position.0,
point.1 + self.position.1,
point.2 + self.position.2,
);
draw_point(d, &moved);
let world_pos: Vec<Vec3> = self
.indices
.iter()
.map(|&point| {
Vec3(
point.0 + self.position.0,
point.1 + self.position.1,
point.2 + self.position.2,
)
})
.collect();
let screen_pos: Vec<(i32, i32)> = world_pos
.iter()
.map(|&point| point.to_screen_space())
.collect();
for face in &self.faces {
draw_triangle(d, &screen_pos, &face, Color::new(255, 0, 0, 64))
}
}

View file

@ -1,3 +1,5 @@
use std::cmp::min;
use crate::config::{WINDOW_HEIGHT, WINDOW_WIDTH};
#[derive(Clone, Copy, Debug)]
@ -5,9 +7,10 @@ pub struct Vec2(pub f64, pub f64);
impl Vec2 {
pub fn to_screen_space(&self) -> (i32, i32) {
let scale: i32 = min(WINDOW_HEIGHT, WINDOW_WIDTH);
return (
((WINDOW_WIDTH as f64) * ((self.0 + 1.0) / 2.0)) as i32,
((WINDOW_HEIGHT as f64) * ((self.1 + 1.0) / 2.0)) as i32,
((scale as f64) * ((self.0 + 1.0) / 2.0)) as i32,
((scale as f64) * ((self.1 + 1.0) / 2.0)) as i32,
);
}
}