1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use math::*;

use std::rc::Rc;
use std::sync::Arc;
use std::cell::RefCell;
use std::any::Any;

use crate::core::object::*;
use crate::core::resource::*;
use crate::core::downcast::*;
use crate::core::light::*;
use crate::core::event::*;
use crate::core::transform::*;
use crate::spectrum::*;
use crate::util::uuid::OsRandNewV4;

#[derive(Debug, Default)]
pub struct SpotLight 
{
	pub uuid:uuid::Uuid,
	pub transform: Transform,
	pub spectrum:Spectrum,
	pub lumens:Lumens,
	pub candela:Candela,
	pub direction:float3,
	pub angle:f32
}

impl SpotLight 
{
	pub fn new() -> Self 
	{
		Self
		{
			uuid:uuid::Uuid::new_v4_osrng(),
			transform:Transform::new(),
			spectrum:Spectrum::one(),
			lumens:Lumens(1.0),
			candela:Lumens(1.0).to_cd(LightType::Point),
			direction:-float3::unit_y(),
			angle:60.0
		}
	}

	pub fn direction(&self) -> float3
	{
		self.direction
	}

	pub fn set_direction(&mut self, dir:float3)
	{
		self.direction = dir;
	}

	pub fn angle(&self) -> f32
	{
		self.angle
	}

	pub fn set_angle(&mut self, angle:f32)
	{
		self.angle = angle;
	}
}

impl Light for SpotLight
{
	fn kind(&self) -> LightType
	{
		LightType::Spot
	}

	fn color(&self) -> Spectrum
	{
		self.spectrum
	}

	fn lumens(&self) -> Lumens
	{
		self.lumens
	}

	fn candela(&self) -> Candela
	{
		self.candela
	}

	fn set_color(&mut self, spectrum:Spectrum)
	{
		self.spectrum = spectrum;
	}

	fn set_lumens(&mut self, lm:Lumens)
	{
		self.lumens = lm;
		self.candela = lm.to_cd(LightType::Point);
	}
}

impl Object for SpotLight
{
	#[inline]
	fn translate(&self) -> &float3
	{
		self.transform.translate()
	}

	#[inline]
	fn scale(&self) -> &float3
	{
		self.transform.scale()
	}

	#[inline]
	fn rotation(&self) -> &float3
	{
		self.transform.rotation()
	}

	#[inline]
	fn transform(&self) -> &float4x4
	{
		self.transform.transform()
	}

	#[inline]
	fn transform_inverse(&self) -> &float4x4
	{
		self.transform.transform_inverse()
	}

	#[inline]
	fn set_translate(&mut self, pos:float3)
	{
		self.transform.set_translate(pos)
	}

	#[inline]
	fn set_scale(&mut self, sz:float3)
	{
		self.transform.set_scale(sz)
	}

	#[inline]
	fn set_rotation(&mut self, rot:float3)
	{
		self.transform.set_rotation(rot)
	}
}

impl Resource for SpotLight
{
	#[inline]
	fn uuid(&self) -> &uuid::Uuid
	{
		&self.uuid
	}
}

impl Downcast for SpotLight
{
    fn as_any(&self) -> &Any { self }
    fn as_any_mut(&mut self) -> &mut Any { self }
}

impl UpdateEvent for SpotLight 
{
	fn update(&mut self)
	{
		self.transform.update();
	}
}

impl From<SpotLight> for Rc<Light + 'static>
{
	fn from(light:SpotLight) -> Self
	{
		Rc::new(light)
	}
}

impl From<SpotLight> for Arc<Light + 'static>
{
	fn from(light:SpotLight) -> Self
	{
		Arc::new(light)
	}
}

impl From<SpotLight> for Rc<RefCell<Light + 'static>>
{
	fn from(light:SpotLight) -> Self
	{
		Rc::new(RefCell::new(light))
	}
}

impl From<SpotLight> for Arc<RefCell<Light + 'static>>
{
	fn from(light:SpotLight) -> Self
	{
		Arc::new(RefCell::new(light))
	}
}