Move color conversion out of color calculation

This commit is contained in:
numzero 2024-04-27 20:17:14 +03:00
parent ea68369012
commit 5be9b616f2

View File

@ -85,6 +85,7 @@ fn trace_to_mesh(mesh: &[Face], base: Vec3, ray: Vec3) -> Option<TraceResult> {
} }
fn render(mesh: &[Face], camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image { fn render(mesh: &[Face], camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image {
let bkg = vec3(0.0, 0.0, 0.0);
let mut img = Image { let mut img = Image {
w: W, w: W,
h: H, h: H,
@ -96,12 +97,15 @@ fn render(mesh: &[Face], camera: impl Fn(Vec2) -> (Vec3, Vec3)) -> Image {
let img_coords = vec2(x as f32, y as f32); let img_coords = vec2(x as f32, y as f32);
let off = (img_coords - img_size * 0.5) / img_size.y; let off = (img_coords - img_size * 0.5) / img_size.y;
let (base, ray) = camera(off); let (base, ray) = camera(off);
if let Some(r) = trace_to_mesh(mesh, base, ray) { let color = if let Some(r) = trace_to_mesh(mesh, base, normalize(ray)) {
let color = clamp(to_ivec3(r.normal * 120.0 + 128.0), ivec3(0, 0, 0), ivec3(255, 255, 255)); 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.put_pixel(x, y, Color(color.x as u8, color.y as u8, color.z as u8));
} }
} }
}
img img
} }