From 27615d4a701b9d2d86cf02e0342fe6404af385dd Mon Sep 17 00:00:00 2001 From: bronku Date: Mon, 16 Feb 2026 00:16:59 +0100 Subject: [PATCH] cube --- src/main.rs | 87 ++++++++++++++++++----------------------------------- 1 file changed, 29 insertions(+), 58 deletions(-) diff --git a/src/main.rs b/src/main.rs index 585ec58..56fa453 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); } }