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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use {
    super::{LineVertex, Material},
    crate::{
        color::{AlphaColor, Color},
        gpu::{MeshFilter, ModelRef, Pose},
        math::{vec3_is_finite, CoordF, Mat4, Sphere, Vec3},
    },
    std::{marker::PhantomData, num::FpCategory, ops::Range},
};

pub type PointLightIter<'a> = CommandIter<'a, PointLightCommand>;
pub type RectLightIter<'a> = CommandIter<'a, RectLightCommand>;
pub type SpotlightIter<'a> = CommandIter<'a, SpotlightCommand>;
pub type SunlightIter<'a> = CommandIter<'a, SunlightCommand>;

// TODO: Voxels, landscapes, water, god rays, particle systems
/// An expressive type which allows specification of individual drawing operations.
pub enum Command {
    /// Draws a line segment.
    Line(LineCommand),

    /// Draws a model.
    Model(ModelCommand),

    /// Draws a point light.
    PointLight(PointLightCommand),

    /// Draws a rectangular light.
    RectLight(RectLightCommand),

    /// Draws a spotlight.
    Spotlight(SpotlightCommand),

    /// Draws sunlight.
    Sunlight(SunlightCommand),
}

impl Command {
    pub(crate) fn as_line(&self) -> Option<&LineCommand> {
        match self {
            Self::Line(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn as_model(&self) -> Option<&ModelCommand> {
        match self {
            Self::Model(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn as_point_light(&self) -> Option<&PointLightCommand> {
        match self {
            Self::PointLight(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn as_rect_light(&self) -> Option<&RectLightCommand> {
        match self {
            Self::RectLight(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn as_spotlight(&self) -> Option<&SpotlightCommand> {
        match self {
            Self::Spotlight(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn as_sunlight(&self) -> Option<&SunlightCommand> {
        match self {
            Self::Sunlight(res) => Some(res),
            _ => None,
        }
    }

    pub(crate) fn is_line(&self) -> bool {
        self.as_line().is_some()
    }

    pub(crate) fn is_model(&self) -> bool {
        self.as_model().is_some()
    }

    pub(crate) fn is_point_light(&self) -> bool {
        self.as_point_light().is_some()
    }

    pub(crate) fn is_rect_light(&self) -> bool {
        self.as_rect_light().is_some()
    }

    pub(crate) fn is_spotlight(&self) -> bool {
        self.as_spotlight().is_some()
    }

    pub(crate) fn is_sunlight(&self) -> bool {
        self.as_sunlight().is_some()
    }

    /// Draws a line between the given coordinates using a constant width and two colors. The colors
    /// specify a gradient if
    /// they differ. Generally intended to support debugging use cases such as drawing bounding
    /// boxes.
    pub fn line<S: Into<Vec3>, SC: Into<AlphaColor>, E: Into<Vec3>, EC: Into<AlphaColor>>(
        start: S,
        start_color: SC,
        end: E,
        end_color: EC,
    ) -> Self {
        Self::Line(LineCommand {
            vertices: [
                LineVertex {
                    color: start_color.into(),
                    pos: start.into(),
                },
                LineVertex {
                    color: end_color.into(),
                    pos: end.into(),
                },
            ],
        })
    }

    /// Draws a model using the given material and world transform.
    pub fn model<M: Into<Mesh>>(mesh: M, material: Material, transform: Mat4) -> Self {
        let mesh = mesh.into();
        Self::Model(ModelCommand {
            camera_order: f32::NAN,
            material,
            mesh_filter: mesh.filter,
            model: mesh.model,
            pose: mesh.pose,
            transform,
        })
    }

    /// Draws a point light using the given values.
    pub fn point_light(center: Vec3, color: Color, lumens: f32, radius: f32) -> Self {
        Self::PointLight(PointLightCommand {
            center,
            color,
            lumens,
            radius,
        })
    }

    /// Draws a spotlight with the given color, position/orientation, and shape.
    ///
    /// _Note_: Spotlights have a hemispherical cap on the bottom, so a given `range` will be the
    /// maximum range and you may not see any light on objects at that distance. Move the light a
    /// bit towards the object to enter the penumbra.
    ///
    /// # Arguments
    ///
    /// * `color` - Full-bright color of the cone area.
    /// * `power` - sRGB power value normalized for gamma, can be greater than 1.0.
    /// * `pos` - Position of the pointy end of the spotlight.
    /// * `normal` - Orientation of the spotlight from `pos` towards the spotlight target.
    /// * `range` - Define the full-bright section along `normal`.
    /// * `cone_angle` - Interior angle of the full-bright portion of the spotlight, in degrees.
    /// * `penumbra_angle` - Additional angle which fades from `color` to tranparent, in degrees.
    pub fn spotlight(
        color: Color,
        lumens: f32,
        position: Vec3,
        normal: Vec3,
        range: Range<f32>,
        cone_angle: f32,
        penumbra_angle: f32,
    ) -> Self {
        assert_eq!(lumens.classify(), FpCategory::Normal);
        assert!(vec3_is_finite(position));
        assert!(vec3_is_finite(normal));
        assert_eq!(range.start.classify(), FpCategory::Normal);
        assert_eq!(range.end.classify(), FpCategory::Normal);
        assert!(range.start < range.end);
        assert_eq!(cone_angle.classify(), FpCategory::Normal);
        assert!(penumbra_angle >= 0.0);
        assert!(cone_angle + penumbra_angle < 180.0);

        let half = cone_angle * 0.5;
        let cone_radius = half.tan() * range.end;
        let penumbra_radius = (half + penumbra_angle).tan() * range.end;
        let top_radius = 0.0; // TODO!

        Self::Spotlight(SpotlightCommand {
            color,
            cone_radius,
            lumens,
            normal,
            penumbra_radius,
            position,
            range,
            top_radius,
        })
    }
}

pub struct CommandIter<'a, T> {
    __: PhantomData<T>,
    cmds: &'a [Command],
    idx: usize,
}

impl<'a, T> CommandIter<'a, T> {
    pub fn new(cmds: &'a [Command]) -> Self {
        Self {
            __: PhantomData,
            cmds,
            idx: 0,
        }
    }
}

impl<'a> Iterator for CommandIter<'a, PointLightCommand> {
    type Item = &'a PointLightCommand;

    fn next(&mut self) -> Option<Self::Item> {
        self.cmds
            .get(self.idx)
            .map(|cmd| {
                self.idx += 1;
                cmd.as_point_light()
            })
            .unwrap_or_default()
    }
}

impl<'a> Iterator for CommandIter<'a, RectLightCommand> {
    type Item = &'a RectLightCommand;

    fn next(&mut self) -> Option<Self::Item> {
        self.cmds
            .get(self.idx)
            .map(|cmd| {
                self.idx += 1;
                cmd.as_rect_light()
            })
            .unwrap_or_default()
    }
}

impl<'a> Iterator for CommandIter<'a, SpotlightCommand> {
    type Item = &'a SpotlightCommand;

    fn next(&mut self) -> Option<Self::Item> {
        self.cmds
            .get(self.idx)
            .map(|cmd| {
                self.idx += 1;
                cmd.as_spotlight()
            })
            .unwrap_or_default()
    }
}

impl<'a> Iterator for CommandIter<'a, SunlightCommand> {
    type Item = &'a SunlightCommand;

    fn next(&mut self) -> Option<Self::Item> {
        self.cmds
            .get(self.idx)
            .map(|cmd| {
                self.idx += 1;
                cmd.as_sunlight()
            })
            .unwrap_or_default()
    }
}

// TODO: This is crufty, fix.
/// Description of a single line segment.
#[derive(Clone, Debug)]
pub struct LineCommand {
    /// The start and end vertices to draw.
    pub vertices: [LineVertex; 2],
}

/// Container of a shared `Model` reference and the optional filter or pose that might be used
/// during rendering.
#[derive(Debug)]
pub struct Mesh {
    /// The mesh or meshes, if set, to render from within the larger collection of meshes this model
    /// may contain.
    ///
    /// `MeshFilter` instances are acquired from the `Model::filter(&self, ...)` function and can
    /// only be used with the same model they are retrieved from.
    pub filter: Option<MeshFilter>,

    /// The shared model reference to render.
    pub model: ModelRef,

    /// The animation pose, if set, to use while rendering this model.
    pub pose: Option<Pose>,
}

impl From<ModelRef> for Mesh {
    fn from(model: ModelRef) -> Self {
        Self {
            filter: None,
            model,
            pose: None,
        }
    }
}

impl From<(ModelRef, MeshFilter)> for Mesh {
    fn from((model, filter): (ModelRef, MeshFilter)) -> Self {
        Self {
            filter: Some(filter),
            model,
            pose: None,
        }
    }
}

impl From<(ModelRef, Option<MeshFilter>)> for Mesh {
    fn from((model, filter): (ModelRef, Option<MeshFilter>)) -> Self {
        Self {
            filter,
            model,
            pose: None,
        }
    }
}

impl From<(ModelRef, Pose)> for Mesh {
    fn from((model, pose): (ModelRef, Pose)) -> Self {
        Self {
            filter: None,
            model,
            pose: Some(pose),
        }
    }
}

impl From<(ModelRef, Option<Pose>)> for Mesh {
    fn from((model, pose): (ModelRef, Option<Pose>)) -> Self {
        Self {
            filter: None,
            model,
            pose,
        }
    }
}

impl From<(ModelRef, MeshFilter, Pose)> for Mesh {
    fn from((model, filter, pose): (ModelRef, MeshFilter, Pose)) -> Self {
        Self {
            filter: Some(filter),
            model,
            pose: Some(pose),
        }
    }
}

impl From<(ModelRef, Option<MeshFilter>, Option<Pose>)> for Mesh {
    fn from((model, filter, pose): (ModelRef, Option<MeshFilter>, Option<Pose>)) -> Self {
        Self {
            filter,
            model,
            pose,
        }
    }
}

/// Description of a model, which may be posed or filtered.
#[derive(Debug)]
pub struct ModelCommand {
    // TODO: Could probably be u16?
    pub(super) camera_order: f32,

    /// The material to use while rendering this model.
    pub material: Material,

    /// The mesh or meshes, if set, to render from within the larger collection of meshes this model
    /// may contain.
    ///
    /// `MeshFilter` instances are acquired from the `Model::filter(&self, ...)` function and can
    /// only be used with the same model they are retrieved from.
    pub mesh_filter: Option<MeshFilter>,

    /// The shared model reference to render.
    pub model: ModelRef,

    /// The animation pose, if set, to use while rendering this model.
    pub pose: Option<Pose>,

    /// The generalized matrix transform usually used to desctibe translation, scale and rotation.
    ///
    /// _NOTE_: This is the "world" matrix; view and projection matrices are handled automatically.
    pub transform: Mat4,
}

/// Description of a point light shining on models.
#[derive(Clone, Debug)]
pub struct PointLightCommand {
    /// The location of the center of this light in world space.
    pub center: Vec3,

    /// Color of the projected light.
    pub color: Color,

    /// Scalar light "power" value, modelled afterm lumens but not realistically.
    pub lumens: f32,

    /// Distance from `center` to the furthest reach of the point light.
    pub radius: f32,
}

/// Description of a rectangular light shining on models.
///
/// The lit area forms a [square frustum](https://en.wikipedia.org/wiki/Frustum).
#[derive(Clone, Debug)]
pub struct RectLightCommand {
    /// Color of the projected light.
    pub color: Color,

    /// The width and height of the rectangular light, when viewed from above (the unlit side).
    pub dims: CoordF,

    /// Scalar light "power" value, modelled afterm lumens but not realistically.
    pub lumens: f32,

    /// Direction of the light rays.
    pub normal: Vec3,

    /// Top-left corner, when viewed from above (the unlit side).
    pub position: Vec3,

    /// size of the penumbra area beyond the box formed by `position` and `range` which fades from
    /// `color` to transparent.
    pub radius: f32,

    /// Distance from `position` to the bottom of the rectangular light.
    pub range: f32,
}

impl RectLightCommand {
    /// Returns a tightly fitting sphere around the lit area of this rectangular light, including
    /// the penumbra
    pub(self) fn bounds(&self) -> Sphere {
        todo!();
    }
}

/// Description of a spotlight shining on models.
#[derive(Clone, Debug)]
pub struct SpotlightCommand {
    /// Color of the projected light.
    pub color: Color,

    /// radius of the spotlight cone from the center to the edge.
    pub cone_radius: f32,

    /// Scalar light "power" value, modelled afterm lumens but not realistically.
    pub lumens: f32,

    /// Direction of the light rays.
    pub normal: Vec3,

    /// Additional radius beyond `cone_radius` which fades from `color` to transparent.
    pub penumbra_radius: f32,

    /// Position of the pointy end.
    pub position: Vec3,

    /// Lit distance from `position` and to the bottom of the spotlight.
    pub range: Range<f32>,

    /// TODO: Refactor
    pub top_radius: f32,
}

/// Description of sunlight shining on models.
#[derive(Clone, Debug)]
pub struct SunlightCommand {
    /// Color of the projected light.
    pub color: Color,

    /// Scalar light "power" value, modelled afterm lumens but not realistically.
    pub lumens: f32,

    /// Direction of the light rays.
    pub normal: Vec3,
}