From 8e2e7bd7057925bb950d571dc0ac3e876ce32d54 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 19 May 2024 19:25:08 +0200 Subject: feat: run at 60 FPS --- src/main.rs | 13 +++++++++++-- src/renderer.rs | 2 +- src/sdf.rs | 9 +++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 3f4dd4a..5675fac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,16 +15,18 @@ use camera::Camera; use renderer::Renderer; use sdf::{Gear, Object, SDBox, Sphere}; +const FPS: i32 = 60; + fn main() { // This vector will later be built // by parsing a JSON scene let mut renderer = Renderer::new( - Buffer::from_height(50.0, 3.0), + Buffer::from_height(100.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(), - radius: 0.4, + radius: 1.1, }), Box::new(Gear { center: Vector3::zero(), @@ -32,6 +34,12 @@ fn main() { thickness: 0.4, turn_rate: 30.0, }), + Box::new(Gear { + center: Vector3::unit_y() * 4.90 - Vector3::unit_z() * 0.65, + radius: 2.0, + thickness: 0.4, + turn_rate: -30.0, + }), Box::new(SDBox { center: Vector3::new(2.0, 2.0, 0.0), size: Vector3::from_value(1.0), @@ -40,6 +48,7 @@ fn main() { ); initscr(); + timeout(1000 / FPS); let mut time = 0.0; diff --git a/src/renderer.rs b/src/renderer.rs index 953607e..f2499f4 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -80,7 +80,7 @@ impl Renderer { let mut dist = 0.0; let mut count = 0; - while dist < 8.0 && count < 30 { + while dist < 15.0 && count < 50 { count += 1; dist = sdf(point); if dist.abs() < threshold { diff --git a/src/sdf.rs b/src/sdf.rs index d940f39..3204305 100644 --- a/src/sdf.rs +++ b/src/sdf.rs @@ -14,8 +14,8 @@ pub struct Sphere { } impl Object for Sphere { - fn sdf(&self, point: Vector, _time: f32) -> f32 { - (point - self.center).magnitude() - self.radius + fn sdf(&self, point: Vector, time: f32) -> f32 { + (point - self.center).magnitude() - self.radius + (time % 5.0) / 50.0 } } @@ -41,6 +41,7 @@ pub struct Gear { impl Object for Gear { fn sdf(&self, point: Vector, time: f32) -> f32 { + let point = point - self.center; let mut dist: f32; let thickness_over_2 = self.thickness / 2.0; @@ -48,8 +49,8 @@ impl Object for Gear { // Ring { - let cylinder_dist = (Vector::new(0.0, point.y, point.z) - self.center).magnitude() - - (self.radius - thickness_over_4); + let cylinder_dist = + (Vector::new(0.0, point.y, point.z)).magnitude() - (self.radius - thickness_over_4); dist = cylinder_dist.abs() - thickness_over_2; // Make cylinder hollow } // Teeth -- cgit v1.2.3