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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use super::structs::RTTriangle;
use rfw_math::*;
use rtbvh::Aabb;
use std::fmt::Display;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct AreaLight {
pub position: Vec3,
pub energy: f32,
pub normal: Vec3,
pub area: f32,
pub vertex0: Vec3,
pub inst_idx: i32,
pub vertex1: Vec3,
mesh_id: i32,
pub radiance: Vec3,
_dummy1: i32,
pub vertex2: Vec3,
_dummy2: i32,
}
impl Default for AreaLight {
fn default() -> Self {
Self {
position: Vec3::ZERO,
energy: 0.0,
normal: Vec3::ZERO,
area: 0.0,
vertex0: Vec3::ZERO,
inst_idx: 0,
vertex1: Vec3::ZERO,
mesh_id: -1,
radiance: Vec3::ZERO,
_dummy1: 0,
vertex2: Vec3::ZERO,
_dummy2: 0,
}
}
}
impl Display for AreaLight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"AreaLight {{ position: {}, energy: {}, normal: {}, area: {}, vertex0: {}, inst_idx: {}, vertex1: {}, radiance: {}, vertex2: {} }}",
self.position,
self.energy,
self.normal,
self.area,
self.vertex0,
self.inst_idx,
self.vertex1,
self.radiance,
self.vertex2,
)
}
}
impl AreaLight {
#[allow(clippy::too_many_arguments)]
pub fn new(
position: Vec3,
radiance: Vec3,
normal: Vec3,
mesh_id: i32,
inst_id: i32,
vertex0: Vec3,
vertex1: Vec3,
vertex2: Vec3,
) -> AreaLight {
let radiance = radiance.abs();
let energy = radiance.length();
Self {
position,
energy,
normal,
area: RTTriangle::area(vertex0, vertex1, vertex2),
vertex0,
inst_idx: inst_id,
vertex1,
mesh_id,
radiance,
_dummy1: 1,
vertex2,
_dummy2: 2,
}
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C, align(32))]
pub struct PointLight {
pub position: Vec3,
pub energy: f32,
pub radiance: Vec3,
_dummy: f32,
}
impl Default for PointLight {
fn default() -> Self {
Self {
position: Vec3::ZERO,
energy: 0.0,
radiance: Vec3::ZERO,
_dummy: 0.0,
}
}
}
impl PointLight {
pub fn new(position: Vec3, radiance: Vec3) -> PointLight {
Self {
position,
energy: radiance.length(),
radiance,
_dummy: 0.0,
}
}
pub fn set_radiance(&mut self, radiance: Vec3) {
let radiance = radiance.abs();
self.radiance = radiance;
self.energy = radiance.length();
}
pub fn get_matrix(&self, _: &Aabb) -> [Mat4; 6] {
let fov = 90.0_f32.to_radians();
let projection = Mat4::perspective_rh_gl(fov, 1.0, 0.1, 1e3);
let center = self.position;
[
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(1.0, 0.0, 0.0),
Vec3::new(0.0, -1.0, 0.0),
),
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(-1.0, 0.0, 0.0),
Vec3::new(0.0, -1.0, 0.0),
),
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(0.0, 1.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
),
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(0.0, -1.0, 0.0),
Vec3::new(0.0, 0.0, -1.0),
),
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(0.0, 0.0, 1.0),
Vec3::new(0.0, -1.0, 0.0),
),
projection
* Mat4::look_at_rh(
center,
center + Vec3::new(0.0, 0.0, -1.0),
Vec3::new(0.0, -1.0, 0.0),
),
]
}
pub fn get_range(&self, _scene_bounds: &Aabb) -> Aabb {
unimplemented!()
}
pub fn translate_x(&mut self, offset: f32) {
self.position[0] += offset;
}
pub fn translate_y(&mut self, offset: f32) {
self.position[1] += offset;
}
pub fn translate_z(&mut self, offset: f32) {
self.position[2] += offset;
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct SpotLight {
pub position: Vec3,
pub cos_inner: f32,
pub radiance: Vec3,
pub cos_outer: f32,
pub direction: Vec3,
pub energy: f32,
}
impl Default for SpotLight {
fn default() -> Self {
Self {
position: Vec3::ZERO,
cos_inner: 0.0,
radiance: Vec3::ZERO,
cos_outer: 0.0,
direction: Vec3::ZERO,
energy: 0.0,
}
}
}
impl Display for SpotLight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"SpotLight {{ position: {}, cos_inner: {}, radiance: {}, cos_outer: {}, direction: {}, energy: {} }}",
self.position,
self.cos_inner,
self.radiance,
self.cos_outer,
self.direction,
self.energy
)
}
}
impl SpotLight {
pub fn new(
position: Vec3,
direction: Vec3,
inner_angle: f32,
outer_angle: f32,
radiance: Vec3,
) -> SpotLight {
debug_assert!(outer_angle > inner_angle);
let inner_angle = inner_angle.to_radians();
let outer_angle = outer_angle.to_radians();
let radiance = radiance.abs();
Self {
position,
cos_inner: inner_angle.cos(),
radiance,
cos_outer: outer_angle.cos(),
direction: direction.normalize(),
energy: radiance.length(),
}
}
pub fn translate_x(&mut self, offset: f32) {
self.position[0] += offset;
}
pub fn translate_y(&mut self, offset: f32) {
self.position[1] += offset;
}
pub fn translate_z(&mut self, offset: f32) {
self.position[2] += offset;
}
pub fn rotate_x(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_x(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
pub fn rotate_y(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_y(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
pub fn rotate_z(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_z(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct DirectionalLight {
pub direction: Vec3,
pub energy: f32,
pub radiance: Vec3,
_dummy: f32,
}
impl Default for DirectionalLight {
fn default() -> Self {
Self {
direction: Vec3::ZERO,
energy: 0.0,
radiance: Vec3::ZERO,
_dummy: 0.0,
}
}
}
impl Display for DirectionalLight {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"DirectionalLight {{ direction: {}, energy: {}, radiance: {} }}",
self.direction, self.energy, self.radiance,
)
}
}
impl DirectionalLight {
pub fn new(direction: Vec3, radiance: Vec3) -> DirectionalLight {
let radiance = radiance.abs();
Self {
direction: direction.normalize(),
energy: radiance.length(),
radiance,
_dummy: 0.0,
}
}
pub fn rotate_x(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_x(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
pub fn rotate_y(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_y(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
pub fn rotate_z(&mut self, degrees: f32) {
let rotation = Mat4::from_rotation_z(degrees.to_radians());
let direction = rotation * self.direction.extend(0.0);
self.direction = direction.truncate();
}
}