summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 11:56:55 +0700
committerRosyid Haryadi <rosyid_haryadi@protonmail.com>2025-03-01 11:56:55 +0700
commitf3b0b1846c03bb2252830147481ba56056d6a405 (patch)
treec12ffef584b5a3ec318a2795d0a76638e8d28d70
parent202f7a315bcc0f7cd836348a29e9d06c7a4fa198 (diff)
upd: color struct
-rw-r--r--src/global.rs37
-rw-r--r--src/main.rs2
-rw-r--r--src/renderer.rs21
3 files changed, 46 insertions, 14 deletions
diff --git a/src/global.rs b/src/global.rs
index 2f99cb9..b322746 100644
--- a/src/global.rs
+++ b/src/global.rs
@@ -3,8 +3,8 @@ use crate::calculus::calculus::Point3;
// "Ideal" aspect ratio, allowing fraction
-pub const ASPECT_RATIO: f32 = 9.0f32 / 5.0f32;
-pub const IMG_WIDTH: usize = 1000;
+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 };
@@ -28,6 +28,11 @@ impl Pixel {
b: (255.999 * b) as u8,
}
}
+
+ pub fn from_color(color: &Color) -> Self {
+ let (r, g, b) = (color.r, color.g, color.b);
+ Self::from_frac(r, g, b)
+ }
}
impl Default for Pixel {
@@ -37,3 +42,31 @@ 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
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index a2d8646..9bdb6ed 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -10,7 +10,7 @@ use crate::view::{render_viewer, View};
fn main() {
let mut display_buffer: DisplayBuffer = [[Pixel::default(); IMG_WIDTH]; IMG_HEIGHT];
- // test_render(&mut data);
+ // test_render(&mut display_buffer);
render(&mut display_buffer);
diff --git a/src/renderer.rs b/src/renderer.rs
index 59a6b7b..7e46834 100644
--- a/src/renderer.rs
+++ b/src/renderer.rs
@@ -1,16 +1,15 @@
use crate::calculus::calculus::{Point3, Ray, Vec3};
-use crate::global::{DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
+use crate::global::{Color, DisplayBuffer, Pixel, CAMERA_CENTER, FOCAL_LENGTH, IMG_HEIGHT, IMG_WIDTH, VIEWPORT_HEIGHT, VIEWPORT_WIDTH};
-fn ray_color(ray: &Ray) -> Vec3 {
+fn ray_color(ray: &Ray) -> Color {
if hit_sphere(&Point3 {x: 0f32, y: 0f32, z: -1f32}, 0.5, ray) {
- return Vec3 {x: 1f32, y: 0f32, z: 0f32}
+ return Color {r: 1f32, g: 0f32, b: 0f32}
}
let unit_direction = ray.direction.unit();
let a = 0.5 * (unit_direction.y + 1.0);
- let vec_1 = Vec3 { x:1.0, y: 1.0, z: 1.0 };
- let vec_2 = Vec3 { x: 0.5, y: 0.7, z: 1.0 };
- vec_1.scalar_mul(1.0 - a)
- .add(&vec_2.scalar_mul(a))
+ let color1 = Color::new(1.0, 1.0, 1.0).mul_scalar(1.0 - a);
+ let color2 = Color::new(0.5, 0.7, 1.0).mul_scalar(a);
+ color1.add(&color2)
}
fn hit_sphere(center: &Point3, radius: f32, ray: &Ray) -> bool {
@@ -26,8 +25,8 @@ pub fn render(display_buffer: &mut DisplayBuffer) {
let viewport_hor_vector = Vec3{ x: VIEWPORT_WIDTH as f32, y: 0.0, z: 0.0 };
let viewport_ver_vector = Vec3 { x: 0.0, y: -1f32 * VIEWPORT_HEIGHT as f32, z: 0.0 };
- let delta_pixel_u = viewport_hor_vector.scalar_mul(1.0 / VIEWPORT_WIDTH as f32);
- let delta_pixel_v = viewport_ver_vector.scalar_mul(1.0 / VIEWPORT_WIDTH as f32);
+ let delta_pixel_u = viewport_hor_vector.scalar_mul(1.0 / IMG_WIDTH as f32);
+ let delta_pixel_v = viewport_ver_vector.scalar_mul(1.0 / IMG_HEIGHT as f32);
let viewport_upper_left = CAMERA_CENTER
.sub(&Vec3 { x: 0f32, y: 0f32, z: FOCAL_LENGTH})
@@ -47,8 +46,8 @@ pub fn render(display_buffer: &mut DisplayBuffer) {
origin: &CAMERA_CENTER,
direction: &ray_direction,
};
- let Vec3 {x, y, z} = ray_color(&ray);
- display_buffer[j][i] = Pixel::from_frac(x, y, z);
+ let color = ray_color(&ray);
+ display_buffer[j][i] = Pixel::from_color(&color);
})
})
} \ No newline at end of file