summaryrefslogtreecommitdiff
path: root/src/global.rs
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 23:07:43 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 23:07:43 +0700
commit5fb34460b02b2b151dd775e43675bd4f09d2633a (patch)
treea596b722257e7c376255ba4a3372f4cabe7a370c /src/global.rs
parentfb30b41fb1f4ebbe5274d2cb93e98a1f2bec445f (diff)
refactor camera
Diffstat (limited to 'src/global.rs')
-rw-r--r--src/global.rs69
1 files changed, 36 insertions, 33 deletions
diff --git a/src/global.rs b/src/global.rs
index 9357920..fc9e0c9 100644
--- a/src/global.rs
+++ b/src/global.rs
@@ -1,18 +1,48 @@
use crate::calculus::calculus::Point3;
-
-
// "Ideal" aspect ratio, allowing fraction
pub const ASPECT_RATIO: f32 = 16.0f32 / 9.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 IMG_WIDTH: usize = 1000;
+pub const IMG_HEIGHT: usize = get_image_height(IMG_WIDTH, ASPECT_RATIO);
pub const VIEWPORT_HEIGHT: f32 = 2.0;
-pub const VIEWPORT_WIDTH: f32 = VIEWPORT_HEIGHT * (IMG_WIDTH as f32 / IMG_HEIGHT as f32);
pub const FOCAL_LENGTH: f32 = 1.0;
pub const CAMERA_CENTER: Point3 = Point3 { x: 0f32, y: 0f32, z: 0f32 };
+pub const fn get_image_height(image_width: usize, aspect_ratio: f32) -> usize {
+ let image_height = (image_width as f32 / aspect_ratio) as usize;
+ if image_width < 1 { 1 } else { image_height }
+}
+
+pub struct Color {
+ pub r: f32,
+ pub g: f32,
+ pub b: f32,
+}
+
+impl Color {
+ pub fn new(r: f32, g: f32, b: f32) -> Self {
+ Self { r, g, b }
+ }
+
+ pub fn mul_scalar(&self, scalar: f32) -> Color {
+ Self {
+ r: self.r * scalar,
+ g: self.g * scalar,
+ b: self.b * scalar,
+ }
+ }
+
+ pub fn add(&self, other: &Self) -> Color {
+ Self {
+ r: self.r + other.r,
+ g: self.g + other.g,
+ b: self.b + other.b
+ }
+ }
+}
+
+
#[derive(Debug, Copy, Clone)]
pub struct Pixel {
pub r: u8,
@@ -43,30 +73,3 @@ impl Default for Pixel {
pub type DisplayBuffer = [[Pixel; IMG_WIDTH]; IMG_HEIGHT];
-pub struct Color {
- pub r: f32,
- pub g: f32,
- pub b: f32,
-}
-
-impl Color {
- pub fn new(r: f32, g: f32, b: f32) -> Self {
- Self { r, g, b }
- }
-
- pub fn mul_scalar(&self, scalar: f32) -> Color {
- Self {
- r: self.r * scalar,
- g: self.g * scalar,
- b: self.b * scalar,
- }
- }
-
- pub fn add(&self, other: &Self) -> Color {
- Self {
- r: self.r + other.r,
- g: self.g + other.g,
- b: self.b + other.b
- }
- }
-}