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
#![macro_use]
//!
//! A collection of geometries implementing the [Geometry] trait.
//!
//! A geometry together with a [material] can be rendered directly, or combined into an [object] (see [Gm]) that can be used in a render call, for example [RenderTarget::render].
//!

macro_rules! impl_geometry_body {
    ($inner:ident) => {
        fn draw(
            &self,
            camera: &Camera,
            program: &Program,
            render_states: RenderStates,
            attributes: FragmentAttributes,
        ) {
            self.$inner()
                .draw(camera, program, render_states, attributes)
        }

        fn vertex_shader_source(&self, required_attributes: FragmentAttributes) -> String {
            self.$inner().vertex_shader_source(required_attributes)
        }

        fn id(&self, required_attributes: FragmentAttributes) -> u16 {
            self.$inner().id(required_attributes)
        }

        fn render_with_material(
            &self,
            material: &dyn Material,
            camera: &Camera,
            lights: &[&dyn Light],
        ) {
            self.$inner().render_with_material(material, camera, lights)
        }

        fn render_with_effect(
            &self,
            material: &dyn Effect,
            camera: &Camera,
            lights: &[&dyn Light],
            color_texture: Option<ColorTexture>,
            depth_texture: Option<DepthTexture>,
        ) {
            self.$inner()
                .render_with_effect(material, camera, lights, color_texture, depth_texture)
        }

        fn aabb(&self) -> AxisAlignedBoundingBox {
            self.$inner().aabb()
        }
    };
}

mod mesh;
#[doc(inline)]
pub use mesh::*;

mod instanced_mesh;
#[doc(inline)]
pub use instanced_mesh::*;

mod sprites;
#[doc(inline)]
pub use sprites::*;

mod particles;
#[doc(inline)]
pub use particles::*;

mod bounding_box;
#[doc(inline)]
pub use bounding_box::*;

mod line;
#[doc(inline)]
pub use line::*;

mod rectangle;
#[doc(inline)]
pub use rectangle::*;

mod circle;
#[doc(inline)]
pub use circle::*;

use crate::core::*;
use crate::renderer::*;

pub use three_d_asset::{
    Geometry as CpuGeometry, Indices, KeyFrameAnimation, KeyFrames, PointCloud, Positions,
    TriMesh as CpuMesh,
};

///
/// Represents a 3D geometry that, together with a [material], can be rendered using [Geometry::render_with_material].
/// Alternatively, a geometry and a material can be combined in a [Gm],
/// thereby creating an [Object] which can be used in a render call, for example [RenderTarget::render].
///
/// If requested by the material, the geometry has to support the following attributes in the vertex shader source code.
/// - position: `out vec3 pos;` (must be in world space)
/// - normal: `out vec3 nor;`
/// - tangent: `out vec3 tang;`
/// - bitangent: `out vec3 bitang;`
/// - uv coordinates: `out vec2 uvs;` (must be flipped in v compared to standard uv coordinates, ie. do `uvs = vec2(uvs.x, 1.0 - uvs.y);` in the vertex shader or do the flip before constructing the uv coordinates vertex buffer)
/// - color: `out vec4 col;`
///
pub trait Geometry {
    ///
    /// Draw this geometry.
    ///
    fn draw(
        &self,
        camera: &Camera,
        program: &Program,
        render_states: RenderStates,
        attributes: FragmentAttributes,
    );

    ///
    /// Returns the vertex shader source for this geometry given that the fragment shader needs the given vertex attributes.
    ///
    fn vertex_shader_source(&self, required_attributes: FragmentAttributes) -> String;

    ///
    /// Returns a unique ID for each variation of the shader source returned from `Geometry::vertex_shader_source`.
    ///
    /// **Note:** The last bit is reserved to internally implemented geometries, so if implementing the `Geometry` trait
    /// outside of this crate, always return an id that is smaller than `0b1u16 << 15`.
    ///
    fn id(&self, required_attributes: FragmentAttributes) -> u16;

    ///
    /// Render the geometry with the given [Material].
    /// Must be called in the callback given as input to a [RenderTarget], [ColorTarget] or [DepthTarget] write method.
    /// Use an empty array for the `lights` argument, if the material does not require lights to be rendered.
    ///
    fn render_with_material(&self, material: &dyn Material, camera: &Camera, lights: &[&dyn Light]);

    ///
    /// Render the geometry with the given [Effect].
    /// Must be called in the callback given as input to a [RenderTarget], [ColorTarget] or [DepthTarget] write method.
    /// Use an empty array for the `lights` argument, if the material does not require lights to be rendered.
    ///
    fn render_with_effect(
        &self,
        material: &dyn Effect,
        camera: &Camera,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    );

    ///
    /// Returns the [AxisAlignedBoundingBox] for this geometry in the global coordinate system.
    ///
    fn aabb(&self) -> AxisAlignedBoundingBox;

    ///
    /// For updating the animation of this geometry if it is animated, if not, this method does nothing.
    /// The time parameter should be some continious time, for example the time since start.
    ///
    fn animate(&mut self, _time: f32) {}
}

use std::ops::Deref;
impl<T: Geometry + ?Sized> Geometry for &T {
    impl_geometry_body!(deref);
}

impl<T: Geometry + ?Sized> Geometry for &mut T {
    impl_geometry_body!(deref);

    fn animate(&mut self, time: f32) {
        self.deref().animate(time)
    }
}

impl<T: Geometry> Geometry for Box<T> {
    impl_geometry_body!(as_ref);
}

impl<T: Geometry> Geometry for std::rc::Rc<T> {
    impl_geometry_body!(as_ref);
}

impl<T: Geometry> Geometry for std::sync::Arc<T> {
    impl_geometry_body!(as_ref);
}

impl<T: Geometry> Geometry for std::cell::RefCell<T> {
    impl_geometry_body!(borrow);

    fn animate(&mut self, time: f32) {
        self.borrow_mut().animate(time)
    }
}

impl<T: Geometry> Geometry for std::sync::RwLock<T> {
    fn draw(
        &self,
        camera: &Camera,
        program: &Program,
        render_states: RenderStates,
        attributes: FragmentAttributes,
    ) {
        self.read()
            .unwrap()
            .draw(camera, program, render_states, attributes)
    }

    fn vertex_shader_source(&self, required_attributes: FragmentAttributes) -> String {
        self.read()
            .unwrap()
            .vertex_shader_source(required_attributes)
    }

    fn id(&self, required_attributes: FragmentAttributes) -> u16 {
        self.read().unwrap().id(required_attributes)
    }

    fn render_with_material(
        &self,
        material: &dyn Material,
        camera: &Camera,
        lights: &[&dyn Light],
    ) {
        self.read()
            .unwrap()
            .render_with_material(material, camera, lights)
    }

    fn render_with_effect(
        &self,
        material: &dyn Effect,
        camera: &Camera,
        lights: &[&dyn Light],
        color_texture: Option<ColorTexture>,
        depth_texture: Option<DepthTexture>,
    ) {
        self.read().unwrap().render_with_effect(
            material,
            camera,
            lights,
            color_texture,
            depth_texture,
        )
    }

    fn aabb(&self) -> AxisAlignedBoundingBox {
        self.read().unwrap().aabb()
    }

    fn animate(&mut self, time: f32) {
        self.write().unwrap().animate(time)
    }
}

struct BaseMesh {
    indices: Option<ElementBuffer>,
    positions: VertexBuffer,
    normals: Option<VertexBuffer>,
    tangents: Option<VertexBuffer>,
    uvs: Option<VertexBuffer>,
    colors: Option<VertexBuffer>,
}

impl BaseMesh {
    pub fn new(context: &Context, cpu_mesh: &CpuMesh) -> Self {
        #[cfg(debug_assertions)]
        cpu_mesh.validate().expect("invalid cpu mesh");

        Self {
            indices: match &cpu_mesh.indices {
                Indices::U8(ind) => Some(ElementBuffer::new_with_data(context, ind)),
                Indices::U16(ind) => Some(ElementBuffer::new_with_data(context, ind)),
                Indices::U32(ind) => Some(ElementBuffer::new_with_data(context, ind)),
                Indices::None => None,
            },
            positions: VertexBuffer::new_with_data(context, &cpu_mesh.positions.to_f32()),
            normals: cpu_mesh
                .normals
                .as_ref()
                .map(|data| VertexBuffer::new_with_data(context, data)),
            tangents: cpu_mesh
                .tangents
                .as_ref()
                .map(|data| VertexBuffer::new_with_data(context, data)),
            uvs: cpu_mesh.uvs.as_ref().map(|data| {
                VertexBuffer::new_with_data(
                    context,
                    &data
                        .iter()
                        .map(|uv| vec2(uv.x, 1.0 - uv.y))
                        .collect::<Vec<_>>(),
                )
            }),
            colors: cpu_mesh.colors.as_ref().map(|data| {
                VertexBuffer::new_with_data(
                    context,
                    &data.iter().map(|c| c.to_linear_srgb()).collect::<Vec<_>>(),
                )
            }),
        }
    }

    pub fn draw(
        &self,
        program: &Program,
        render_states: RenderStates,
        camera: &Camera,
        attributes: FragmentAttributes,
    ) {
        self.use_attributes(program, attributes);
        if let Some(index_buffer) = &self.indices {
            program.draw_elements(render_states, camera.viewport(), index_buffer)
        } else {
            program.draw_arrays(
                render_states,
                camera.viewport(),
                self.positions.vertex_count(),
            )
        }
    }

    pub fn draw_instanced(
        &self,
        program: &Program,
        render_states: RenderStates,
        camera: &Camera,
        attributes: FragmentAttributes,
        instance_count: u32,
    ) {
        self.use_attributes(program, attributes);

        if let Some(index_buffer) = &self.indices {
            program.draw_elements_instanced(
                render_states,
                camera.viewport(),
                index_buffer,
                instance_count,
            )
        } else {
            program.draw_arrays_instanced(
                render_states,
                camera.viewport(),
                self.positions.vertex_count(),
                instance_count,
            )
        }
    }

    fn use_attributes(&self, program: &Program, attributes: FragmentAttributes) {
        program.use_vertex_attribute("position", &self.positions);

        if attributes.normal {
            program.use_vertex_attribute(
                "normal",
                self.normals.as_ref().unwrap_or_else(|| {
                    panic!(
                        "the material requires normal attributes but the geometry did not provide it"
                    )
                }),
            );
        }

        if attributes.tangents {
            program.use_vertex_attribute(
                "tangent",
                self.tangents.as_ref().unwrap_or_else(|| {
                    panic!(
                        "the material requires tangent attributes but the geometry did not provide it"
                    )
                }),
            );
        }

        if attributes.uv {
            program.use_vertex_attribute(
                "uv_coordinates",
                self.uvs.as_ref().unwrap_or_else(|| {
                    panic!(
                        "the material requires uv coordinate attributes but the geometry did not provide it"
                    )
                }),
            );
        }

        if attributes.color {
            if let Some(colors) = &self.colors {
                program.use_vertex_attribute("color", colors);
            }
        }
    }
}