diff options
author | eug-vs <eugene@eug-vs.xyz> | 2021-10-25 23:12:34 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2021-10-25 23:12:34 +0300 |
commit | 430ebecd2c3fe35393a3eb7861bdcff84525e9eb (patch) | |
tree | 93f6084ae02d40282e4002ca9105ba6a9ffaa7c0 | |
parent | 67d7492477808dc1765b1a9ed493dee79dff9320 (diff) | |
download | pistol-430ebecd2c3fe35393a3eb7861bdcff84525e9eb.tar.gz |
feat: add floor
-rw-r--r-- | src/camera.rs | 23 | ||||
-rw-r--r-- | src/main.rs | 2 |
2 files changed, 16 insertions, 9 deletions
diff --git a/src/camera.rs b/src/camera.rs index d2fbd5e..59283ab 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -1,16 +1,16 @@ use crate::vector::Vector; use std::{cmp::{max, min}, f32::consts::PI, f64::MAX_EXP, fmt}; -const WIDTH: i32 = 60; -const HEIGHT: i32 = 30; +const WIDTH: i32 = 100; +const HEIGHT: i32 = 50; #[derive(Debug)] -pub struct Buffer (pub [[char; 60]; 30]); +pub struct Buffer (pub [[char; 100]; 50]); impl fmt::Display for Buffer { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - for i in 0..30 { - for j in 0..60 { + for i in 0..50 { + for j in 0..100 { write!(f, "{}", self.0[i][j])?; } writeln!(f)?; @@ -32,10 +32,15 @@ pub struct Camera { impl Camera { pub fn sdf(&self, point: Vector) -> f32 { + // Sphere let center = Vector { x: 3.0, y: 0.0, z: 0.0 }; let radius = 1.0; - return (point - center).magnitude() - radius + let sphere_dist = (point - center).magnitude() - radius; + + let floor_dist = point.z + 1.0; // Floor at z = -1 + + sphere_dist.min(floor_dist) } pub fn screen(&self) -> (f32, f32) { @@ -45,7 +50,7 @@ impl Camera { (width, height) } pub fn render(& mut self) { - let palette = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "; + 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; @@ -78,8 +83,10 @@ impl Camera { let ray = direction.normalized(); let mut point = self.position; let mut dist = 0.0; + let mut count = 0; - while dist < self.brightness { + while dist < self.brightness && count < 25 { + count += 1; dist = self.sdf(point); if dist < threshold { // println!("Dist: {}, point {}", dist, point); diff --git a/src/main.rs b/src/main.rs index 3bf9bb2..7b63c04 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ fn main() { distance: 1.0, aspect_ratio: 1.0, brightness: 10.0, - buffer: Buffer([['.'; 60]; 30]) + buffer: Buffer([['.'; 100]; 50]) }; for _i in 0..60 { |