use ray_tracing_core::{camera::Camera, prelude::*, renderer::ClassicalRenderer, scene::Scene};

pub struct DepthRenderer {
    width: u32,
    height: u32,
}

impl DepthRenderer {
    pub fn new(width: u32, height: u32) -> Self {
        Self { width, height }
    }
}

impl<S, C, R> ClassicalRenderer<R, S, C> for DepthRenderer
where
    S: Scene<R>,
    C: Camera<R>,
    R: Rng,
{
    fn render_pixel(&self, scene: &S, camera: &C, x: u32, y: u32, rng: &mut R) -> Color {
        let r = camera.forward(x, y, rng);

        if let Some(i) = scene.intersect(r, 0.0, Float::INFINITY) {
            // Color::gray(1.0 / i.t())
            let c = 0.5 * (i.normal() + Dir3::new(1.0, 1.0, 1.0));
            // let c = i.normal();
            Color::new(c.x(), c.y(), c.z())
        } else {
            Color::black()
        }
    }

    fn width(&self) -> u32 {
        self.width
    }

    fn height(&self) -> u32 {
        self.height
    }
}
