use crate::render::PixelBuffer;
use std::io::{self, Write};
pub fn depth_cue(color: (u8, u8, u8), z: f32, z_near: f32, z_far: f32) -> (u8, u8, u8) {
if z_near >= z_far {
return color;
}
let depth_factor = (z - z_far) / (z_near - z_far);
let depth_factor = depth_factor.clamp(0.5, 1.0);
(
(color.0 as f32 * depth_factor) as u8,
(color.1 as f32 * depth_factor) as u8,
(color.2 as f32 * depth_factor) as u8,
)
}
pub fn render_half_block(buffer: &PixelBuffer, out: &mut impl Write) -> io::Result<()> {
let width = buffer.width();
let height = buffer.height();
for y in (0..height).step_by(2) {
for x in 0..width {
let top = buffer.get_pixel(x, y);
let bottom = if y + 1 < height {
buffer.get_pixel(x, y + 1)
} else {
(0, 0, 0, 0)
};
let (tr, tg, tb, ta) = top;
let (br, bg, bb, ba) = bottom;
if ta == 0 && ba == 0 {
write!(out, " ")?;
} else if ta == 0 {
write!(out, "\x1b[38;2;{};{};{}m▄\x1b[0m", br, bg, bb)?;
} else if ba == 0 {
write!(out, "\x1b[38;2;{};{};{}m▀\x1b[0m", tr, tg, tb)?;
} else {
write!(out, "\x1b[38;2;{};{};{};48;2;{};{};{}m▀\x1b[0m", tr, tg, tb, br, bg, bb)?;
}
}
writeln!(out)?;
}
Ok(())
}