organization
This commit is contained in:
parent
05d5e55118
commit
bdc051633d
4 changed files with 68 additions and 62 deletions
2
src/config.rs
Normal file
2
src/config.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub const WINDOW_WIDTH: i32 = 640;
|
||||
pub const WINDOW_HEIGHT: i32 = 480;
|
||||
68
src/main.rs
68
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
13
src/vec2.rs
Normal file
13
src/vec2.rs
Normal 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
47
src/vec3.rs
Normal 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),
|
||||
];
|
||||
Loading…
Add table
Add a link
Reference in a new issue