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
mod mesh;
mod scene;

use std::ops::{Index, IndexMut};

use glium::implement_vertex;
use num_traits::{FromPrimitive, ToPrimitive};

use crate::shader::{InstanceInput, InstancingMode, ToUniforms};
use crate::{CreationError, DrawError, Drawable, Mesh};

pub use mesh::{mesh_from_slices, CUBE_INDICES, CUBE_NORMALS, CUBE_POSITIONS};
pub use scene::{Core, Instance};

#[derive(Copy, Clone, PartialEq, Eq, Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)]
pub enum BasicObj {
    Triangle,
    Quad,
    Cube,
    Sphere,

    LineX,
    LineY,
    LineZ,

    TessellatedCube,
    TessellatedCylinder,
}

pub const NUM_TYPES: usize = 9;

#[derive(Copy, Clone, Debug)]
pub struct Vertex {
    pub position: [f32; 3],
    pub normal: [f32; 3],
}

implement_vertex!(Vertex, position, normal);

pub struct Resources {
    pub meshes: Vec<Mesh<Vertex>>,
}

impl Resources {
    pub fn create<F: glium::backend::Facade>(facade: &F) -> Result<Resources, CreationError> {
        // Unfortunately, it doesn't seem easy to use enum_map here,
        // since we need to check for errors in creating buffers
        let mut meshes = Vec::new();

        for i in 0..NUM_TYPES {
            // Safe to unwrap here, since we iterate within the range
            let object: BasicObj = FromPrimitive::from_usize(i).unwrap();

            meshes.push(object.create_mesh(facade)?);
        }

        Ok(Resources { meshes })
    }

    pub fn mesh(&self, object: BasicObj) -> &Mesh<Vertex> {
        // Safe to unwrap since `BasicObj::to_usize()` never fails.
        &self.meshes[object.to_usize().unwrap()]
    }
}

impl BasicObj {
    #[rustfmt::skip]
    pub fn create_mesh<F: glium::backend::Facade>(
        self,
        facade: &F,
    ) -> Result<Mesh<Vertex>, CreationError> {
        mesh::create_mesh(self, facade)
    }
}

pub struct RenderList<I: InstanceInput>(Vec<crate::RenderList<I>>);

impl<I: InstanceInput> RenderList<I> {
    pub fn clear(&mut self) {
        for list in self.0.iter_mut() {
            list.clear();
        }
    }

    pub fn as_drawable<'a>(&'a self, resources: &'a Resources) -> impl Drawable<I, Vertex> + 'a {
        RenderListDrawableImpl(self, resources)
    }
}

impl<I: InstanceInput + Clone> Default for RenderList<I> {
    fn default() -> Self {
        Self(vec![Default::default(); NUM_TYPES])
    }
}

impl<I: InstanceInput> Index<BasicObj> for RenderList<I> {
    type Output = crate::RenderList<I>;

    fn index(&self, object: BasicObj) -> &crate::RenderList<I> {
        // Safe to unwrap since `BasicObj::to_usize()` never fails.
        &self.0[object.to_usize().unwrap()]
    }
}

impl<I: InstanceInput> IndexMut<BasicObj> for RenderList<I> {
    fn index_mut(&mut self, object: BasicObj) -> &mut crate::RenderList<I> {
        // Safe to unwrap since `BasicObj::to_usize()` never fails.
        &mut self.0[object.to_usize().unwrap()]
    }
}

pub struct Instancing<I: InstanceInput>(Vec<crate::Instancing<I>>);

impl<I: InstanceInput> Instancing<I> {
    pub fn create<F: glium::backend::Facade>(facade: &F) -> Result<Self, CreationError> {
        let mut vec = Vec::new();
        for _ in 0..NUM_TYPES {
            vec.push(crate::Instancing::create(facade)?);
        }

        Ok(Instancing(vec))
    }

    pub fn update<F: glium::backend::Facade>(
        &mut self,
        facade: &F,
        render_list: &RenderList<I>,
    ) -> Result<(), CreationError> {
        for i in 0..NUM_TYPES {
            self.0[i].update(facade, render_list.0[i].as_slice())?;
        }

        Ok(())
    }

    pub fn as_drawable<'a>(&'a self, resources: &'a Resources) -> impl Drawable<I, Vertex> + 'a {
        InstancingDrawableImpl(self, resources)
    }
}

struct InstancingDrawableImpl<'a, I: InstanceInput>(&'a Instancing<I>, &'a Resources);

impl<'a, I: InstanceInput> Drawable<I, Vertex> for InstancingDrawableImpl<'a, I> {
    const INSTANCING_MODE: InstancingMode = InstancingMode::Vertex;

    fn draw<U, S>(
        &self,
        program: &glium::Program,
        uniforms: &U,
        draw_params: &glium::DrawParameters,
        target: &mut S,
    ) -> Result<(), DrawError>
    where
        U: ToUniforms,
        S: glium::Surface,
    {
        for i in 0..NUM_TYPES {
            (self.0).0[i].as_drawable(&self.1.meshes[i]).draw(
                program,
                uniforms,
                draw_params,
                target,
            )?;
        }

        Ok(())
    }
}

struct RenderListDrawableImpl<'a, I: InstanceInput>(&'a RenderList<I>, &'a Resources);

impl<'a, I: InstanceInput> Drawable<I, Vertex> for RenderListDrawableImpl<'a, I> {
    const INSTANCING_MODE: InstancingMode = InstancingMode::Uniforms;

    fn draw<U, S>(
        &self,
        program: &glium::Program,
        uniforms: &U,
        draw_params: &glium::DrawParameters,
        target: &mut S,
    ) -> Result<(), DrawError>
    where
        U: ToUniforms,
        S: glium::Surface,
    {
        for i in 0..NUM_TYPES {
            (self.0).0[i].as_drawable(&self.1.meshes[i]).draw(
                program,
                uniforms,
                draw_params,
                target,
            )?;
        }

        Ok(())
    }
}