Move color conversion out of color calculation
This commit is contained in:
parent
ea68369012
commit
5be9b616f2
12
src/main.rs
12
src/main.rs
|
|
@ -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 {
|
||||
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user