From 5be9b616f21ef43ff62e8a4225e66f689f4b1a02 Mon Sep 17 00:00:00 2001 From: numzero Date: Sat, 27 Apr 2024 20:17:14 +0300 Subject: [PATCH] Move color conversion out of color calculation --- src/main.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index bef1f0c..b102f18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,7 @@ fn trace_to_mesh(mesh: &[Face], base: Vec3, ray: Vec3) -> Option { } fn render(mesh: &[Face], camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image { + let bkg = vec3(0.0, 0.0, 0.0); let mut img = Image { w: W, h: H, @@ -96,10 +97,13 @@ fn render(mesh: &[Face], camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image { let img_coords = vec2(x as f32, y as f32); let off = (img_coords - img_size * 0.5) / img_size.y; let (base, ray) = camera(off); - if let Some(r) = trace_to_mesh(mesh, base, ray) { - let color = clamp(to_ivec3(r.normal * 120.0 + 128.0), ivec3(0, 0, 0), ivec3(255, 255, 255)); - img.put_pixel(x, y, Color(color.x as u8, color.y as u8, color.z as u8)); - } + let color = if let Some(r) = trace_to_mesh(mesh, base, normalize(ray)) { + r.normal * 0.45 + 0.50 + } else { + bkg + }; + let color = clamp(to_ivec3(color * 255.0), ivec3(0, 0, 0), ivec3(255, 255, 255)); + img.put_pixel(x, y, Color(color.x as u8, color.y as u8, color.z as u8)); } } img