aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs74
1 files changed, 47 insertions, 27 deletions
diff --git a/src/main.rs b/src/main.rs
index 5873996..d733fb3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,30 +1,26 @@
extern crate ncurses;
-mod camera;
mod buffer;
+mod camera;
mod renderer;
+mod screen;
mod sdf;
-use std::{f32::consts::PI, time::Instant};
use cgmath::{Angle, Array, Matrix3, Rad, Vector3, Zero};
use ncurses::*;
+use std::{f32::consts::PI, time::Instant};
-use camera::Camera;
use buffer::Buffer;
+use camera::Camera;
use renderer::Renderer;
-use sdf::{Sphere, Gear, Object, SDBox};
+use sdf::{Gear, Object, SDBox, Sphere};
fn main() {
// This vector will later be built
// by parsing a JSON scene
let mut renderer = Renderer::new(
- Box::new(Buffer::from_height(50.0, 3.0)),
- Box::new(Camera::new(
- Vector3::new(-4.0, 0.0, 0.0),
- Vector3::zero(),
- PI / 2.0,
- 1.0
- )),
+ Buffer::from_height(50.0, 3.0),
+ Camera::new(Vector3::new(-4.0, 0.0, 0.0), Vector3::zero(), PI / 2.0, 1.0),
vec![
Box::new(Sphere {
center: Vector3::zero(),
@@ -40,7 +36,7 @@ fn main() {
center: Vector3::new(2.0, 2.0, 0.0),
size: Vector3::from_value(1.0),
}),
- ]
+ ],
);
initscr();
@@ -56,9 +52,16 @@ fn main() {
// Render
let timestamp = Instant::now();
renderer.render(time);
- addstr(&format!("\nRendered in {:?} ({:.1} FPS)\n", timestamp.elapsed(), 1.0 / timestamp.elapsed().as_secs_f64()));
+ addstr(&format!(
+ "\nRendered in {:?} ({:.1} FPS)\n",
+ timestamp.elapsed(),
+ 1.0 / timestamp.elapsed().as_secs_f64()
+ ));
addstr(&format!("Camera: {:?}\n", renderer.camera.position));
- addstr(&format!("Facing: {:?}, Up: {:?}\n", renderer.camera.direction, renderer.camera.up));
+ addstr(&format!(
+ "Facing: {:?}, Up: {:?}\n",
+ renderer.camera.direction, renderer.camera.up
+ ));
refresh();
@@ -68,39 +71,56 @@ fn main() {
addstr(&format!("\nPressed: {:?}\n", char));
refresh();
- if char == 107 { // k to move forward
+ if char == 107 {
+ // k to move forward
renderer.camera.position += renderer.camera.direction * renderer.camera.speed;
- } else if char == 106 { // j to move backward
+ } else if char == 106 {
+ // j to move backward
renderer.camera.position -= renderer.camera.direction * renderer.camera.speed;
- } else if char == 72 { // H to move left
- renderer.camera.position += Matrix3::from_axis_angle(renderer.camera.up, Rad::turn_div_4()) * renderer.camera.direction * renderer.camera.speed;
- } else if char == 76 { // L to move right
- renderer.camera.position -= Matrix3::from_axis_angle(renderer.camera.up, Rad::turn_div_4()) * renderer.camera.direction * renderer.camera.speed;
- } else if char == 104 { // h to rotate left
+ } else if char == 72 {
+ // H to move left
+ renderer.camera.position +=
+ Matrix3::from_axis_angle(renderer.camera.up, Rad::turn_div_4())
+ * renderer.camera.direction
+ * renderer.camera.speed;
+ } else if char == 76 {
+ // L to move right
+ renderer.camera.position -=
+ Matrix3::from_axis_angle(renderer.camera.up, Rad::turn_div_4())
+ * renderer.camera.direction
+ * renderer.camera.speed;
+ } else if char == 104 {
+ // h to rotate left
let rotation = Matrix3::from_angle_z(Rad::full_turn() / renderer.camera.turn_rate);
renderer.camera.direction = rotation * renderer.camera.direction;
renderer.camera.up = rotation * renderer.camera.up;
- } else if char == 108 { // l to rotate right
+ } else if char == 108 {
+ // l to rotate right
let rotation = Matrix3::from_angle_z(-Rad::full_turn() / renderer.camera.turn_rate);
renderer.camera.direction = rotation * renderer.camera.direction;
renderer.camera.up = rotation * renderer.camera.up;
- } else if char == 75 { // K to rotate up
+ } else if char == 75 {
+ // K to rotate up
let axis = renderer.camera.up.cross(renderer.camera.direction);
let angle = -Rad::full_turn() / renderer.camera.turn_rate;
let rotation = Matrix3::from_axis_angle(axis, angle);
renderer.camera.up = rotation * renderer.camera.up;
renderer.camera.direction = rotation * renderer.camera.direction;
- } else if char == 74 { // J to rotate down
+ } else if char == 74 {
+ // J to rotate down
let axis = renderer.camera.up.cross(renderer.camera.direction);
let angle = Rad::full_turn() / renderer.camera.turn_rate;
let rotation = Matrix3::from_axis_angle(axis, angle);
renderer.camera.up = rotation * renderer.camera.up;
renderer.camera.direction = rotation * renderer.camera.direction;
- } else if char == 117 { // u to move up along Z
+ } else if char == 117 {
+ // u to move up along Z
renderer.camera.position += Vector3::unit_z() * renderer.camera.speed;
- } else if char == 100 { // d to move down along Z
+ } else if char == 100 {
+ // d to move down along Z
renderer.camera.position -= Vector3::unit_z() * renderer.camera.speed;
- } else if char == 70 { // F to reverse camera direction
+ } else if char == 70 {
+ // F to reverse camera direction
renderer.camera.direction = -renderer.camera.direction;
}
}