diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 15:50:04 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-02-26 15:50:04 +0700 |
commit | 2961d488078fd3e8297fa5c98232f1d998069fa7 (patch) | |
tree | 9f7c54a61561bcab379dc68df9e290c96a542930 | |
parent | 7b0375eee4a945cc8fbb3c03cfc0657e0f1ee443 (diff) |
upd: constants
-rw-r--r-- | src/calculus.rs | 44 | ||||
-rw-r--r-- | src/global.rs | 16 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/renderer.rs | 2 |
4 files changed, 44 insertions, 19 deletions
diff --git a/src/calculus.rs b/src/calculus.rs index 677230e..40caff8 100644 --- a/src/calculus.rs +++ b/src/calculus.rs @@ -1,19 +1,15 @@ pub mod calculus { - // struct Vec3Cache { - // mag: Option<f64>, - // mag_sql: Option<f64>, - // unit: Option<Vec3>, - // } - pub struct Vec3 { - pub x: f64, - pub y: f64, - pub z: f64, + pub x: f32, + pub y: f32, + pub z: f32, // cached: Vec3Cache, } + pub type Point3 = Vec3; + impl Vec3 { - pub fn new(x: f64, y: f64, z: f64) -> Self { + pub fn new(x: f32, y: f32, z: f32) -> Self { Self { x, y, z } } @@ -33,21 +29,23 @@ pub mod calculus { } } - pub fn scalar_mul(&mut self, multiplier: f64) { - self.x *= multiplier; - self.y *= multiplier; - self.z *= multiplier; + pub fn scalar_mul(&self, multiplier: f32) -> Self { + Self { + x: self.x * multiplier, + y: self.y * multiplier, + z: self.z * multiplier, + } } - pub fn mag_sqr(&self) -> f64 { + pub fn mag_sqr(&self) -> f32 { self.x * self.x + self.y * self.y + self.z * self.z } - pub fn mag(&self) -> f64 { + pub fn mag(&self) -> f32 { self.mag_sqr().sqrt() } - pub fn dot_prod(&self, other: &Self) -> f64 { + pub fn dot_prod(&self, other: &Self) -> f32 { self.x * other.x + self.y * other.y + self.z * other.z } @@ -68,4 +66,16 @@ pub mod calculus { } } } + + pub struct Ray { + pub origin: Point3, + pub direction: Vec3, + } + + impl Ray { + pub fn at(&self, t: f32) -> Point3 { + // Get parametric location + self.origin.add(&self.direction.scalar_mul(t)) + } + } }
\ No newline at end of file diff --git a/src/global.rs b/src/global.rs index 3a1e329..932daa2 100644 --- a/src/global.rs +++ b/src/global.rs @@ -1,5 +1,17 @@ -pub const IMG_WIDTH: usize = 256; -pub const IMG_HEIGHT: usize = 256; +use crate::calculus::calculus::Point3; + + + +// "Ideal" aspect ratio, allowing fraction +pub const ASPECT_RATIO: f32 = 9.0f32 / 5.0f32; +pub const IMG_WIDTH: usize = 400; +const IMG_HEIGHT_TMP: usize = (IMG_WIDTH as f32 / ASPECT_RATIO) as usize; +pub const IMG_HEIGHT: usize = if (IMG_HEIGHT_TMP < 1) { 1 } else { IMG_HEIGHT_TMP }; + +pub const VIEWPORT_HEIGHT: usize = 2; +pub const VIEWPORT_WIDTH: usize = VIEWPORT_HEIGHT * (IMG_WIDTH / IMG_HEIGHT); +pub const FOCAL_LENGTH: f32 = 2.0; +pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 }; #[derive(Debug, Copy, Clone)] pub struct Pixel { diff --git a/src/main.rs b/src/main.rs index abad7ff..65dc271 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod view; mod global; mod calculus; +mod renderer; use crate::global::*; use crate::view::{render_viewer, View}; diff --git a/src/renderer.rs b/src/renderer.rs new file mode 100644 index 0000000..28dbb36 --- /dev/null +++ b/src/renderer.rs @@ -0,0 +1,2 @@ +fn render() { +}
\ No newline at end of file |