aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2021-10-27 21:49:04 +0300
committereug-vs <eugene@eug-vs.xyz>2021-10-27 21:49:04 +0300
commit47f76ad2898ca5f0f1f94c2b5d9e08db5613187b (patch)
tree062028854298791883b467414cf6ecb8dd1e3c02
parentba78b90209c5fd3a183043ad3f5110c4a60bf335 (diff)
downloadpistol-47f76ad2898ca5f0f1f94c2b5d9e08db5613187b.tar.gz
feat: allow controlling Lights
-rw-r--r--src/camera.rs18
-rw-r--r--src/main.rs26
2 files changed, 24 insertions, 20 deletions
diff --git a/src/camera.rs b/src/camera.rs
index 4773a81..204b9f8 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -1,17 +1,11 @@
+use cgmath::Matrix3;
+use cgmath::Rad;
use cgmath::Vector3;
use cgmath::prelude::*;
use std::{cmp::{max, min}, f32::consts::PI, fmt};
type Vector = Vector3<f32>;
-pub fn rotate_z(vec: Vector, angle: f32) -> Vector {
- return Vector {
- x: vec.x * angle.cos() + vec.y * angle.sin(),
- y: vec.y * angle.cos() - vec.x * angle.sin(),
- z: vec.z,
- }
-}
-
pub const HEIGHT: i32 = 40;
pub const WIDTH: i32 = HEIGHT * 3;
@@ -35,6 +29,7 @@ pub struct Camera {
pub time: f32,
pub position: Vector,
pub direction: Vector,
+ pub light: Vector,
pub angle: f32,
pub distance: f32,
pub brightness: f32,
@@ -102,7 +97,7 @@ impl Camera {
let palette = "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. ";
let (screen_width, screen_height) = self.screen();
- let h_step = rotate_z(self.direction, PI / 2.0).normalize() * screen_width / WIDTH as f32;
+ let h_step = Matrix3::from_angle_z(Rad::turn_div_4()) * self.direction * screen_width / WIDTH as f32;
let v_step = Vector { x: 0.0, y: 0.0, z: -screen_height / HEIGHT as f32 };
// println!("Steps: h{}, v{}", h_step, v_step);
@@ -142,7 +137,6 @@ impl Camera {
}
pub fn shoot_ray(&self, direction: Vector) -> f32 {
- let light = Vector { x: 1.0, y: 1.0, z: -1.0 }.normalize();
let threshold = 0.1;
let ray = direction.normalize();
@@ -160,7 +154,7 @@ impl Camera {
let k = 2.0;
while t < 7.0 {
- let h = self.sdf(point - light * t);
+ let h = self.sdf(point - self.light * t);
if h < 0.001 {
return 0.97
}
@@ -170,7 +164,7 @@ impl Camera {
}
let normal = self.normal(point);
- let dot = -(normal.dot(light));
+ let dot = -(normal.dot(self.light));
return 1.0 - 0.5 * dot.max(0.01).min(0.98) - 0.5 * res.min(0.98)
}
diff --git a/src/main.rs b/src/main.rs
index 804ddfa..7226c8e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -3,15 +3,16 @@ extern crate ncurses;
mod camera;
mod canvas;
use std::{f32::consts::PI, time::Instant};
-use cgmath::Vector3;
+use cgmath::{Angle, InnerSpace, Matrix3, Rad, Vector3};
use ncurses::*;
-use crate::camera::{Buffer, Camera, WIDTH, HEIGHT, rotate_z};
+use crate::camera::{Buffer, Camera, WIDTH, HEIGHT};
fn main() {
let mut cam = Camera {
position: Vector3 { x: 0.0, y: -0.7, z: 0.0 },
- direction: Vector3 { x: 1.0, y: 0.0, z: 0.0 },
+ direction: Vector3 { x: 1.0, y: 0.0, z: 0.0 }.normalize(),
+ 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,
@@ -34,10 +35,15 @@ fn main() {
cam.render();
addstr(&cam.buffer.to_string());
addstr(&format!("\nRendered in {:?}\n", timestamp.elapsed()).to_string());
+ addstr(&format!("Camera: {:?}\n", cam.position));
+ addstr(&format!("Facing: {:?}\n", cam.direction));
+ addstr(&format!("Light: {:?}\n", cam.light));
refresh();
// Handle input
let char = getch();
+ addstr(&format!("\nPressed: {:?}\n", char));
+ refresh();
let cam_speed = 0.5;
let cam_turn_rate = 30.0;
@@ -47,15 +53,19 @@ fn main() {
} else if char == 107 || char == 75 {
cam.position += cam.direction * cam_speed;
} else if char == 104 {
- cam.direction = rotate_z(cam.direction, -2.0 * PI / cam_turn_rate);
+ cam.direction = Matrix3::from_angle_z(-Rad::full_turn() / cam_turn_rate) * cam.direction;
} else if char == 108 {
- cam.direction = rotate_z(cam.direction, 2.0 * PI / cam_turn_rate);
+ cam.direction = Matrix3::from_angle_z(Rad::full_turn() / cam_turn_rate) * cam.direction;
} else if char == 72 {
- cam.position -= rotate_z(cam.direction, PI / 2.0) * cam_speed;
+ cam.position -= Matrix3::from_angle_z(Rad::turn_div_4()) * cam.direction * cam_speed;
} else if char == 76 {
- cam.position += rotate_z(cam.direction, PI / 2.0) * cam_speed;
- } else if char == 70 { // F
+ cam.position += Matrix3::from_angle_z(Rad::turn_div_4()) * cam.direction * cam_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;
}
}