draw square
This commit is contained in:
parent
842f0d1f22
commit
6c73b01a52
1 changed files with 48 additions and 40 deletions
88
src/main.rs
88
src/main.rs
|
|
@ -4,26 +4,26 @@ const WINDOW_WIDTH: i32 = 640;
|
|||
const WINDOW_HEIGHT: i32 = 480;
|
||||
|
||||
struct Vec2 {
|
||||
x: f32,
|
||||
y: f32,
|
||||
x: f64,
|
||||
y: f64,
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
fn to_screen_space(self) -> (i32, i32) {
|
||||
return (
|
||||
((WINDOW_WIDTH as f32) * ((self.x + 1.0) / 2.0)) as i32,
|
||||
((WINDOW_HEIGHT as f32) * ((self.y + 1.0) / 2.0)) as i32,
|
||||
((WINDOW_WIDTH as f64) * ((self.x + 1.0) / 2.0)) as i32,
|
||||
((WINDOW_HEIGHT as f64) * ((self.y + 1.0) / 2.0)) as i32,
|
||||
);
|
||||
}
|
||||
}
|
||||
struct Vec3 {
|
||||
x: f32,
|
||||
y: f32,
|
||||
z: f32,
|
||||
x: f64,
|
||||
y: f64,
|
||||
z: f64,
|
||||
}
|
||||
impl Vec3 {
|
||||
fn to_screen_space(self) -> (i32, i32) {
|
||||
const FOCAL_LENGTH: f32 = 1.0;
|
||||
const FOCAL_LENGTH: f64 = 1.0;
|
||||
let camera_pos = Vec2 {
|
||||
x: FOCAL_LENGTH * self.x / self.z,
|
||||
y: FOCAL_LENGTH * self.y / self.z,
|
||||
|
|
@ -38,44 +38,52 @@ fn draw_point(d: &mut RaylibDrawHandle, pos: Vec3) {
|
|||
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 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_point(
|
||||
&mut d,
|
||||
Vec3 {
|
||||
x: -0.5,
|
||||
y: -0.5,
|
||||
z: 1.0,
|
||||
},
|
||||
);
|
||||
draw_point(
|
||||
&mut d,
|
||||
Vec3 {
|
||||
x: 0.5,
|
||||
y: -0.5,
|
||||
z: 1.0,
|
||||
},
|
||||
);
|
||||
draw_point(
|
||||
&mut d,
|
||||
Vec3 {
|
||||
x: 0.5,
|
||||
y: 0.5,
|
||||
z: 1.0,
|
||||
},
|
||||
);
|
||||
draw_point(
|
||||
&mut d,
|
||||
Vec3 {
|
||||
x: -0.5,
|
||||
y: 0.5,
|
||||
z: 1.0,
|
||||
},
|
||||
);
|
||||
// draw_square(&mut d, (time - 5.0) / 10.0);
|
||||
|
||||
draw_square(&mut d, 1.0);
|
||||
draw_square(&mut d, 2.0);
|
||||
|
||||
d.clear_background(Color::WHITE);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue