object struct
This commit is contained in:
parent
bdc051633d
commit
713c5fc67b
3 changed files with 51 additions and 37 deletions
10
src/main.rs
10
src/main.rs
|
|
@ -1,19 +1,21 @@
|
|||
mod config;
|
||||
mod object;
|
||||
mod vec2;
|
||||
mod vec3;
|
||||
use config::{WINDOW_HEIGHT, WINDOW_WIDTH};
|
||||
use config::*;
|
||||
use object::*;
|
||||
use raylib::prelude::*;
|
||||
use vec3::*;
|
||||
|
||||
fn main() {
|
||||
let (mut rl, thread) = raylib::init().size(WINDOW_WIDTH, WINDOW_HEIGHT).build();
|
||||
|
||||
let mut c = CUBE.clone();
|
||||
move_by(&mut c, &Vec3(0.0, 0.0, 2.0));
|
||||
let mut c = new_cube();
|
||||
c.move_by(&Vec3(0.0, 0.0, 2.0));
|
||||
|
||||
while !rl.window_should_close() {
|
||||
let mut d = rl.begin_drawing(&thread);
|
||||
draw_object(&mut d, &c);
|
||||
c.draw_object(&mut d);
|
||||
d.clear_background(Color::WHITE);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
44
src/object.rs
Normal file
44
src/object.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::Vec3;
|
||||
use raylib::prelude::*;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Object {
|
||||
indices: Vec<Vec3>,
|
||||
}
|
||||
|
||||
pub fn new_cube() -> Object {
|
||||
Object {
|
||||
indices: vec![
|
||||
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 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);
|
||||
}
|
||||
|
||||
impl Object {
|
||||
pub fn draw_object(&self, d: &mut RaylibDrawHandle) {
|
||||
for point in &self.indices {
|
||||
draw_point(d, point)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_by(&mut self, delta_pos: &Vec3) {
|
||||
for point in &mut self.indices {
|
||||
(point).0 += delta_pos.0;
|
||||
(point).1 += delta_pos.1;
|
||||
(point).2 += delta_pos.2;
|
||||
}
|
||||
}
|
||||
}
|
||||
34
src/vec3.rs
34
src/vec3.rs
|
|
@ -1,11 +1,10 @@
|
|||
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) {
|
||||
pub fn to_screen_space(&self) -> (i32, i32) {
|
||||
const FOCAL_LENGTH: f64 = 1.0;
|
||||
let camera_pos = Vec2(
|
||||
FOCAL_LENGTH * self.0 / self.2,
|
||||
|
|
@ -14,34 +13,3 @@ impl Vec3 {
|
|||
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