//! Scene data handed to the renderer: one triangle mesh plus instanced boxes. type Mat4 = [[f32; 4]; 4]; /// A triangle mesh with positions in metres and `u32` triangle indices. #[derive(Debug, Clone, Default, PartialEq)] pub struct MeshData { /// Vertex positions in world space. pub vertices: Vec<[f32; 3]>, /// Triples of vertex indices. pub indices: Vec, } impl MeshData { /// The number of complete triangles in the mesh. #[must_use] pub fn triangle_count(&self) -> usize { self.indices.len() / 3 } } /// One drawable box: a model matrix and a flat colour. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Instance { /// Column-major model matrix. pub model: Mat4, /// Linear RGBA colour. pub color: [f32; 4], } impl Instance { /// The identity model matrix. pub const IDENTITY: Mat4 = [ [1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0], ]; /// Builds an instance from a model matrix and a colour. #[must_use] pub fn new(model: Mat4, color: [f32; 4]) -> Self { Self { model, color } } /// A box centred at `center` with the given size along each axis. #[must_use] pub fn box_at(center: [f32; 3], size: [f32; 3], color: [f32; 4]) -> Self { let model = [ [size[0], 0.0, 0.0, 0.0], [0.0, size[1], 0.0, 0.0], [0.0, 0.0, size[2], 0.0], [center[0], center[1], center[2], 1.0], ]; Self::new(model, color) } } /// Everything the renderer draws in one frame. #[derive(Debug, Clone, Default, PartialEq)] pub struct Scene { /// The terrain surface. pub mesh: MeshData, /// Colour multiplied into the terrain vertices. pub mesh_color: [f32; 4], /// Boxes drawn with instancing. pub instances: Vec, } impl Scene { /// A scene with one terrain mesh and the default terrain colour. #[must_use] pub fn new(mesh: MeshData) -> Self { Self { mesh, mesh_color: [0.34, 0.46, 0.29, 1.0], instances: Vec::new(), } } }