aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2021-10-27 10:15:50 +0300
committereug-vs <eugene@eug-vs.xyz>2021-10-27 10:15:50 +0300
commit92d5cd982d87963165ee175b985a8a477154a5e7 (patch)
treebd95d1bf9f0f833ad5a638d08dcbe133888c989f
parent4c231404b9a11f42503cc6e51fe92470429acbbf (diff)
downloadpistol-92d5cd982d87963165ee175b985a8a477154a5e7.tar.gz
feat: use cgmath::Vector3
-rw-r--r--src/camera.rs25
-rw-r--r--src/main.rs12
2 files changed, 22 insertions, 15 deletions
diff --git a/src/camera.rs b/src/camera.rs
index dc0e9ae..646307f 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -1,6 +1,17 @@
-use crate::vector::Vector;
+use cgmath::{Vector3, Matrix};
+use cgmath::prelude::*;
use std::{cmp::{max, min}, f32::consts::PI, f64::MAX_EXP, 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,
+ }
+}
+
const WIDTH: i32 = 120;
const HEIGHT: i32 = 60;
@@ -76,12 +87,12 @@ impl Camera {
let palette = "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'.";
let (screen_width, screen_height) = self.screen();
- let h_step = self.direction.rotate_z(PI / 2.0).normalized() * screen_width / WIDTH as f32;
+ let h_step = rotate_z(self.direction, PI / 2.0).normalize() * 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);
// Initialize with a corner
- let point = self.position + (self.direction.normalized() * self.distance) - (h_step * (WIDTH / 2) as f32 + v_step * (HEIGHT / 2) as f32);
+ let point = self.position + (self.direction.normalize() * self.distance) - (h_step * (WIDTH / 2) as f32 + v_step * (HEIGHT / 2) as f32);
// println!("Corner: {}", point);
let mut ray_dir = point - self.position;
@@ -112,14 +123,14 @@ impl Camera {
let dz = Vector { x: 0.0, y: 0.0, z: d };
let dfdz = (self.sdf(point + dz) - self.sdf(point - dz)) / (2.0 * d);
- Vector { x: dfdx, y: dfdy, z: dfdz }.normalized()
+ Vector { x: dfdx, y: dfdy, z: dfdz }.normalize()
}
pub fn shoot_ray(&self, direction: Vector) -> f32 {
- let light = Vector { x: 1.0, y: 1.0, z: -1.0 }.normalized();
+ let light = Vector { x: 1.0, y: 1.0, z: -1.0 }.normalize();
let threshold = 0.1;
- let ray = direction.normalized();
+ let ray = direction.normalize();
let mut point = self.position;
let mut dist = 0.0;
let mut count = 0;
@@ -130,7 +141,7 @@ impl Camera {
if dist < threshold {
// Collision in point! Let's calculate lights now:
let normal = self.normal(point);
- let dot = -(normal * light);
+ let dot = -(normal.dot(light));
return 1.0 - dot.max(0.01).min(0.98);
}
point = point + ray * dist;
diff --git a/src/main.rs b/src/main.rs
index bf70415..3a757db 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,18 +1,14 @@
-mod vector;
mod camera;
mod canvas;
use std::f32::consts::PI;
+use cgmath::Vector3;
-use crate::{camera::{Buffer, Camera}, vector::Vector};
-
-trait Object {
- fn sdf(&self, point: Vector) -> f32;
-}
+use crate::camera::{Buffer, Camera};
fn main() {
let mut cam = Camera {
- position: Vector { x: 0.0, y: -0.7, z: 0.0 },
- direction: Vector { x: 1.0, y: 0.0, z: 0.0 },
+ position: Vector3 { x: 0.0, y: -0.7, z: 0.0 },
+ direction: Vector3 { x: 1.0, y: 0.0, z: 0.0 },
angle: PI / 2.0,
distance: 1.0,
aspect_ratio: 1.0,