diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..949a0a3 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,2 @@ +pub const WINDOW_WIDTH: i32 = 640; +pub const WINDOW_HEIGHT: i32 = 480; diff --git a/src/main.rs b/src/main.rs index b193b13..f9ebfb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,65 +1,9 @@ -use std::{clone, ops}; - +mod config; +mod vec2; +mod vec3; +use config::{WINDOW_HEIGHT, WINDOW_WIDTH}; use raylib::prelude::*; - -const WINDOW_WIDTH: i32 = 640; -const WINDOW_HEIGHT: i32 = 480; - -struct Vec2(f64, f64); - -impl Vec2 { - fn to_screen_space(&self) -> (i32, i32) { - 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, - ); - } -} - -#[derive(Clone, Copy, Debug)] -struct Vec3(f64, f64, f64); - -impl Vec3 { - fn to_screen_space(&self) -> (i32, i32) { - const FOCAL_LENGTH: f64 = 1.0; - 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) { - 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_points(d: &mut RaylibDrawHandle, points: &[Vec3]) { - for point in points { - draw_point(d, point) - } -} - -fn move_by(points: &mut [Vec3], delta_pos: &Vec3) { - for point in points { - (*point).0 += delta_pos.0; - (*point).1 += delta_pos.1; - (*point).2 += delta_pos.2; - } -} - -const CUBE: [Vec3; 8] = [ - Vec3(-0.5, -0.5, -0.5), - Vec3(0.5, -0.5, -0.5), - Vec3(0.5, 0.5, -0.5), - Vec3(-0.5, 0.5, -0.5), - Vec3(-0.5, -0.5, 0.5), - Vec3(0.5, -0.5, 0.5), - Vec3(0.5, 0.5, 0.5), - Vec3(-0.5, 0.5, 0.5), -]; +use vec3::*; fn main() { let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build(); @@ -69,7 +13,7 @@ fn main() { while !rl.window_should_close() { let mut d = rl.begin_drawing(&thread); - draw_points(&mut d, &c); + draw_object(&mut d, &c); d.clear_background(Color::WHITE); } } diff --git a/src/vec2.rs b/src/vec2.rs new file mode 100644 index 0000000..fcd3df7 --- /dev/null +++ b/src/vec2.rs @@ -0,0 +1,13 @@ +use crate::config::{WINDOW_HEIGHT, WINDOW_WIDTH}; + +#[derive(Clone, Copy, Debug)] +pub struct Vec2(pub f64, pub f64); + +impl Vec2 { + pub fn to_screen_space(&self) -> (i32, i32) { + 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, + ); + } +} diff --git a/src/vec3.rs b/src/vec3.rs new file mode 100644 index 0000000..5a2f9ab --- /dev/null +++ b/src/vec3.rs @@ -0,0 +1,47 @@ +use crate::vec2::*; +use raylib::prelude::*; + +#[derive(Clone, Copy, Debug)] +pub struct Vec3(pub f64, pub f64, pub f64); + +impl Vec3 { + fn to_screen_space(&self) -> (i32, i32) { + const FOCAL_LENGTH: f64 = 1.0; + 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) { + 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); +} + +pub fn draw_object(d: &mut RaylibDrawHandle, points: &[Vec3]) { + for point in points { + draw_point(d, point) + } +} + +pub fn move_by(points: &mut [Vec3], delta_pos: &Vec3) { + for point in points { + (*point).0 += delta_pos.0; + (*point).1 += delta_pos.1; + (*point).2 += delta_pos.2; + } +} + +pub const CUBE: [Vec3; 8] = [ + Vec3(-0.5, -0.5, -0.5), + Vec3(0.5, -0.5, -0.5), + Vec3(0.5, 0.5, -0.5), + Vec3(-0.5, 0.5, -0.5), + Vec3(-0.5, -0.5, 0.5), + Vec3(0.5, -0.5, 0.5), + Vec3(0.5, 0.5, 0.5), + Vec3(-0.5, 0.5, 0.5), +];