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
use {
    super::{LineCommand, 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 {
    Line(LineCommand),
    Model(ModelCommand),
    PointLight(PointLightCommand),
    RectLight(RectLightCommand),
    Spotlight(SpotlightCommand),
    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([
            LineVertex {
                color: start_color.into(),
                pos: start.into(),
            },
            LineVertex {
                color: end_color.into(),
                pos: end.into(),
            },
        ]))
    }

    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,
        })
    }

    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()
    }
}

#[derive(Debug)]
pub struct Mesh {
    pub filter: Option<MeshFilter>,
    pub model: ModelRef,
    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, Pose)> for Mesh {
    fn from((model, pose): (ModelRef, Pose)) -> Self {
        Self {
            filter: None,
            model,
            pose: Some(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),
        }
    }
}

#[derive(Debug)]
pub struct ModelCommand {
    pub(super) camera_order: f32, // TODO: Could probably be u16?
    pub material: Material,
    pub mesh_filter: Option<MeshFilter>,
    pub model: ModelRef,
    pub pose: Option<Pose>,
    pub transform: Mat4,
}

#[derive(Clone, Debug)]
pub struct PointLightCommand {
    pub center: Vec3,
    pub color: Color,
    pub lumens: f32,
    pub radius: f32,
}

#[derive(Clone, Debug)]
pub struct RectLightCommand {
    pub color: Color, // full-bright and penumbra-to-transparent color
    pub dims: CoordF,
    pub lumens: f32,
    pub normal: Vec3,
    pub position: Vec3, // top-left corner when viewed from above
    pub radius: f32, // size of the penumbra area beyond the box formed by `pos` and `range` which fades from `color` to transparent
    pub range: f32,  // distance from `pos` to the bottom of the rectangular light
}

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!();
    }
}

#[derive(Clone, Debug)]
pub struct SpotlightCommand {
    pub color: Color,     // `cone` and penumbra-to-transparent color
    pub cone_radius: f32, // radius of the spotlight cone from the center to the edge of the full-bright area
    pub lumens: f32,
    pub normal: Vec3,         // direction from `pos` which the spotlight shines
    pub penumbra_radius: f32, // Additional radius beyond `cone_radius` which fades from `color` to transparent
    pub position: Vec3,       // position of the pointy end
    pub range: Range<f32>, // lit distance from `pos` and to the bottom of the spotlight (does not account for the lens-shaped end)
    pub top_radius: f32,
}

#[derive(Clone, Debug)]
pub struct SunlightCommand {
    pub color: Color,
    pub lumens: f32,
    pub normal: Vec3,
}