enigma_3d/
light.rs

1use glium::implement_uniform_block;
2use serde::{Deserialize, Serialize};
3
4pub enum LightEmissionType {
5    Source,
6    Ambient,
7}
8
9#[derive(Serialize, Deserialize)]
10pub struct LightSerializer {
11    pub position: [f32; 3],
12    pub color: [f32; 3],
13    pub intensity: f32,
14    pub direction: [f32; 3],
15    pub cast_shadow: bool,
16}
17
18#[derive(Copy, Clone)]
19pub struct Light {
20    pub position: [f32; 3],
21    pub color: [f32; 3],
22    pub intensity: f32,
23    pub direction: [f32; 3],
24    pub cast_shadow: bool
25}
26
27impl Light {
28    pub fn new(position: [f32; 3], color: [f32; 3], intensity: f32, direction: Option<[f32;3]>, cast_shadow: bool) -> Self {
29        Self {
30            position,
31            color,
32            intensity,
33            direction : direction.unwrap_or_else(|| [0.0, 0.0, 0.0]),
34            cast_shadow,
35        }
36    }
37
38    pub fn is_directional(&self) -> bool {
39        self.direction != [0.0, 0.0, 0.0]
40    }
41
42    pub fn from_serializer(serializer: LightSerializer) -> Self {
43        Self {
44            position: serializer.position,
45            color: serializer.color,
46            intensity: serializer.intensity,
47            direction: serializer.direction,
48            cast_shadow: serializer.cast_shadow,
49        }
50    }
51
52    pub fn to_serializer(&self) -> LightSerializer {
53        LightSerializer {
54            position: self.position,
55            color: self.color,
56            intensity: self.intensity,
57            direction: self.direction,
58            cast_shadow: self.cast_shadow,
59        }
60    }
61}
62
63pub struct LightBlock {
64    pub position: [[f32; 4]; 4],
65    pub directions: [[f32; 4]; 4],
66    pub color: [[f32; 4]; 4],
67    pub intensity: [f32; 4],
68    pub cast_shadow: [i32; 4],
69    pub amount: i32,
70    pub ambient_color: [f32; 3],
71    pub ambient_intensity: f32,
72}
73
74impl std::fmt::Debug for LightBlock {
75    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
76        f.debug_struct("LightBlock")
77            .field("position", &self.position)
78            .field("color", &self.color)
79            .field("intensity", &self.intensity)
80            .field("amount", &self.amount)
81            .field("ambient_color", &self.ambient_color)
82            .finish()
83    }
84}
85
86glium::implement_uniform_block!(Light, position, color, intensity, direction, cast_shadow);
87glium::implement_uniform_block!(LightBlock, position, directions, cast_shadow, color, intensity, amount, ambient_color, ambient_intensity);