2020-03-10 06:43:40 +00:00
|
|
|
use super::Color;
|
|
|
|
use crate::{math, prelude::Translation, render::camera};
|
2019-12-02 04:03:04 +00:00
|
|
|
use std::ops::Range;
|
|
|
|
use zerocopy::{AsBytes, FromBytes};
|
|
|
|
|
|
|
|
pub struct Light {
|
2020-03-10 06:43:40 +00:00
|
|
|
pub color: Color,
|
2019-12-02 04:03:04 +00:00
|
|
|
pub fov: f32,
|
|
|
|
pub depth: Range<f32>,
|
2020-02-18 03:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Light {
|
|
|
|
fn default() -> Self {
|
|
|
|
Light {
|
2020-03-10 06:43:40 +00:00
|
|
|
color: Color::rgb(1.0, 1.0, 1.0),
|
2020-02-18 03:06:12 +00:00
|
|
|
depth: 0.1..50.0,
|
|
|
|
fov: f32::to_radians(60.0),
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 04:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone, Copy, AsBytes, FromBytes)]
|
|
|
|
pub struct LightRaw {
|
|
|
|
pub proj: [[f32; 4]; 4],
|
|
|
|
pub pos: [f32; 4],
|
|
|
|
pub color: [f32; 4],
|
|
|
|
}
|
|
|
|
|
2019-12-03 17:01:15 +00:00
|
|
|
impl LightRaw {
|
|
|
|
pub fn from(light: &Light, transform: &math::Mat4, translation: &Translation) -> LightRaw {
|
2020-01-11 10:11:27 +00:00
|
|
|
let proj = camera::get_perspective_projection_matrix(
|
|
|
|
light.fov,
|
|
|
|
1.0,
|
|
|
|
light.depth.start,
|
|
|
|
light.depth.end,
|
|
|
|
) * *transform;
|
2019-12-04 08:11:14 +00:00
|
|
|
let (x, y, z) = translation.0.into();
|
2019-12-02 04:03:04 +00:00
|
|
|
LightRaw {
|
2019-12-04 08:11:14 +00:00
|
|
|
proj: proj.to_cols_array_2d(),
|
|
|
|
pos: [x, y, z, 1.0],
|
2020-02-18 03:06:12 +00:00
|
|
|
color: light.color.into(),
|
2019-12-02 04:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-11 10:11:27 +00:00
|
|
|
}
|