This commit is contained in:
bronku 2026-02-16 00:16:59 +01:00
parent 6c73b01a52
commit 27615d4a70

View file

@ -3,88 +3,59 @@ use raylib::prelude::*;
const WINDOW_WIDTH: i32 = 640;
const WINDOW_HEIGHT: i32 = 480;
struct Vec2 {
x: f64,
y: f64,
}
struct Vec2(f64, f64);
impl Vec2 {
fn to_screen_space(self) -> (i32, i32) {
fn to_screen_space(&self) -> (i32, i32) {
return (
((WINDOW_WIDTH as f64) * ((self.x + 1.0) / 2.0)) as i32,
((WINDOW_HEIGHT as f64) * ((self.y + 1.0) / 2.0)) as i32,
((WINDOW_WIDTH as f64) * ((self.0 + 1.0) / 2.0)) as i32,
((WINDOW_HEIGHT as f64) * ((self.1 + 1.0) / 2.0)) as i32,
);
}
}
struct Vec3 {
x: f64,
y: f64,
z: f64,
}
struct Vec3(f64, f64, f64);
impl Vec3 {
fn to_screen_space(self) -> (i32, i32) {
fn to_screen_space(&self) -> (i32, i32) {
const FOCAL_LENGTH: f64 = 1.0;
let camera_pos = Vec2 {
x: FOCAL_LENGTH * self.x / self.z,
y: FOCAL_LENGTH * self.y / self.z,
};
let camera_pos = Vec2(
FOCAL_LENGTH * self.0 / self.2,
FOCAL_LENGTH * self.1 / self.2,
);
return camera_pos.to_screen_space();
}
}
fn draw_point(d: &mut RaylibDrawHandle, pos: Vec3) {
fn draw_point(d: &mut RaylibDrawHandle, pos: &Vec3) {
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);
}
fn draw_square(d: &mut RaylibDrawHandle, dist: f64) {
draw_point(
d,
Vec3 {
x: -0.5,
y: -0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: 0.5,
y: -0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: 0.5,
y: 0.5,
z: dist,
},
);
draw_point(
d,
Vec3 {
x: -0.5,
y: 0.5,
z: dist,
},
);
fn draw_points(d: &mut RaylibDrawHandle, points: &[Vec3]) {
for point in points {
draw_point(d, point)
}
}
const CUBE: [Vec3; 8] = [
Vec3(-0.5, -0.5, 1.0),
Vec3(0.5, -0.5, 1.0),
Vec3(0.5, 0.5, 1.0),
Vec3(-0.5, 0.5, 1.0),
Vec3(-0.5, -0.5, 1.5),
Vec3(0.5, -0.5, 1.5),
Vec3(0.5, 0.5, 1.5),
Vec3(-0.5, 0.5, 1.5),
];
fn main() {
let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build();
while !rl.window_should_close() {
// let time = rl.get_time();
let mut d = rl.begin_drawing(&thread);
// draw_square(&mut d, (time - 5.0) / 10.0);
draw_square(&mut d, 1.0);
draw_square(&mut d, 2.0);
draw_points(&mut d, &CUBE);
d.clear_background(Color::WHITE);
}
}