aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2021-10-29 22:18:43 +0300
committereug-vs <eugene@eug-vs.xyz>2021-10-29 22:37:55 +0300
commit61c757f2499d79efe4066ca2d8c1ff7827604543 (patch)
tree3593de9b7eba2c390cddc546d0013ef7ac81677d /src/main.rs
parentb79ea97978a38fcb2ac7c776a37531d6babcb84e (diff)
downloadpistol-61c757f2499d79efe4066ca2d8c1ff7827604543.tar.gz
refactor!: separate rendering functionality
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs108
1 files changed, 59 insertions, 49 deletions
diff --git a/src/main.rs b/src/main.rs
index da16e28..694a843 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,90 +1,100 @@
extern crate ncurses;
mod camera;
+mod buffer;
+mod renderer;
+mod sdf;
+
use std::{f32::consts::PI, time::Instant};
-use cgmath::{Angle, InnerSpace, Matrix3, Rad, Vector3};
+use cgmath::{Angle, Matrix3, Rad, Vector3, Zero};
use ncurses::*;
-use crate::camera::{Camera, WIDTH, HEIGHT};
+use camera::Camera;
+use buffer::Buffer;
+use renderer::Renderer;
+use sdf::sd_gear;
+
fn main() {
- let mut cam = Camera {
- position: Vector3::new(-5.0, 0.0, 2.0),
- direction: Vector3::unit_x(),
- up: Vector3::unit_z(),
- light: Vector3 { x: 1.0, y: 1.0, z: -1.0 }.normalize(),
- angle: PI / 2.0,
- distance: 1.0,
- aspect_ratio: 2.0 * HEIGHT as f32 / WIDTH as f32,
- brightness: 15.0,
- time: 0.0,
- speed: 0.5,
- turn_rate: 30.0,
- width: 2.0,
- height: 4.0 * HEIGHT as f32 / WIDTH as f32,
- palette: "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ".chars().collect(),
+ let mut renderer = Renderer {
+ buffer:Buffer::from_height(50.0, 3.0),
+ camera: Camera::new(
+ Vector3::new(-4.0, 0.0, 0.0),
+ Vector3::zero(),
+ PI / 2.0,
+ 1.0
+ )
+ };
+
+ // This closure will later be built
+ // by parsing a JSON scene
+ let sdf_global = |point: Vector3<f32>, time: f32| -> f32 {
+ sd_gear(point, time, Vector3::zero(), 2.0, 0.4, 30.0)
};
initscr();
+ let mut time = 0.0;
+
while true {
clear();
flushinp();
+ time += 1.0;
+
+ let sdf = |point: Vector3<f32>| -> f32 {
+ sdf_global(point, time)
+ };
+
// Render
- cam.time += 1.0;
let timestamp = Instant::now();
- cam.render();
- addstr(&format!("\nRendered in {:?} ({:.0} FPS)\n", timestamp.elapsed(), 1.0 / timestamp.elapsed().as_secs_f64()));
- addstr(&format!("\nTime: {:?}\n", cam.time));
- addstr(&format!("Camera: {:?}\n", cam.position));
- addstr(&format!("Facing: {:?}, Up: {:?}\n", cam.direction, cam.up));
- addstr(&format!("Light: {:?}\n", cam.light));
+ renderer.render(&sdf);
+ 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));
+
refresh();
// Handle input
+ // TODO: move all bullshit below to a separate file
let char = getch();
addstr(&format!("\nPressed: {:?}\n", char));
refresh();
if char == 107 { // k to move forward
- cam.position += cam.direction * cam.speed;
+ renderer.camera.position += renderer.camera.direction * renderer.camera.speed;
} else if char == 106 { // j to move backward
- cam.position -= cam.direction * cam.speed;
+ renderer.camera.position -= renderer.camera.direction * renderer.camera.speed;
} else if char == 72 { // H to move left
- cam.position += Matrix3::from_axis_angle(cam.up, Rad::turn_div_4()) * cam.direction * cam.speed;
+ 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
- cam.position -= Matrix3::from_axis_angle(cam.up, Rad::turn_div_4()) * cam.direction * cam.speed;
+ 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() / cam.turn_rate);
- cam.direction = rotation * cam.direction;
- cam.up = rotation * cam.up;
+ 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
- let rotation = Matrix3::from_angle_z(-Rad::full_turn() / cam.turn_rate);
- cam.direction = rotation * cam.direction;
- cam.up = rotation * cam.up;
+ 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
- let axis = cam.up.cross(cam.direction);
- let angle = -Rad::full_turn() / cam.turn_rate;
+ 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);
- cam.up = rotation * cam.up;
- cam.direction = rotation * cam.direction;
+ renderer.camera.up = rotation * renderer.camera.up;
+ renderer.camera.direction = rotation * renderer.camera.direction;
} else if char == 74 { // J to rotate down
- let axis = cam.up.cross(cam.direction);
- let angle = Rad::full_turn() / cam.turn_rate;
+ 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);
- cam.up = rotation * cam.up;
- cam.direction = rotation * cam.direction;
+ renderer.camera.up = rotation * renderer.camera.up;
+ renderer.camera.direction = rotation * renderer.camera.direction;
} else if char == 117 { // u to move up along Z
- cam.position += Vector3::unit_z() * cam.speed;
+ renderer.camera.position += Vector3::unit_z() * renderer.camera.speed;
} else if char == 100 { // d to move down along Z
- cam.position -= Vector3::unit_z() * cam.speed;
+ renderer.camera.position -= Vector3::unit_z() * renderer.camera.speed;
} else if char == 70 { // F to reverse camera direction
- cam.direction = -cam.direction;
- } else if char == 101 { // e to change lights
- cam.light = Matrix3::from_angle_z(Rad::turn_div_2() / cam.turn_rate) * cam.light;
- } else if char == 69 { // E to change lights vertically
- cam.light = Matrix3::from_angle_y(Rad::turn_div_2() / cam.turn_rate) * cam.light;
+ renderer.camera.direction = -renderer.camera.direction;
}
}