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