organization

This commit is contained in:
bronku 2026-02-16 00:59:00 +01:00
parent 05d5e55118
commit bdc051633d
4 changed files with 68 additions and 62 deletions

2
src/config.rs Normal file
View file

@ -0,0 +1,2 @@
pub const WINDOW_WIDTH: i32 = 640;
pub const WINDOW_HEIGHT: i32 = 480;

View file

@ -1,65 +1,9 @@
use std::{clone, ops}; mod config;
mod vec2;
mod vec3;
use config::{WINDOW_HEIGHT, WINDOW_WIDTH};
use raylib::prelude::*; use raylib::prelude::*;
use vec3::*;
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),
];
fn main() { fn main() {
let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build(); let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build();
@ -69,7 +13,7 @@ fn main() {
while !rl.window_should_close() { while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread); let mut d = rl.begin_drawing(&thread);
draw_points(&mut d, &c); draw_object(&mut d, &c);
d.clear_background(Color::WHITE); d.clear_background(Color::WHITE);
} }
} }

13
src/vec2.rs Normal file
View file

@ -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,
);
}
}

47
src/vec3.rs Normal file
View file

@ -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),
];