use std::f32;

use image::RgbImage;

use crate::texture::ImageTexture;
use crate::texture::Texture;
use crate::vec3::Vec3;

#[derive(Debug)]
pub struct EnvMap {
    img_tex: ImageTexture,
}

impl EnvMap {
    pub fn new(img: RgbImage) -> EnvMap {
        EnvMap {
            img_tex: ImageTexture::new(img),
        }
    }
    pub fn color(&self, ray: Vec3) -> Vec3 {
        let zero = Vec3::new(0., 0., 0.);
        let u = ray.x.atan2(ray.z) / f32::consts::PI / 2.0 + 0.5;
        let v = ray.y / 2.0 + 0.5;
        self.img_tex.value(u, v, zero)
    }
}
