gltf_viewer_lib/render/
mesh.rs

1// use std::rc::Rc;
2use std::path::Path;
3
4use collision::{Aabb, Aabb3, Union};
5
6use gltf;
7
8use crate::render::math::*;
9use crate::render::{Primitive, Root};
10use crate::importdata::ImportData;
11
12pub struct Mesh {
13    pub index: usize, // glTF index
14    pub primitives: Vec<Primitive>,
15    // TODO: weights
16    // pub weights: Vec<Rc<?>>
17    pub name: Option<String>,
18
19    pub bounds: Aabb3<f32>,
20}
21
22impl Mesh {
23    pub fn from_gltf(
24        g_mesh: &gltf::Mesh<'_>,
25        root: &mut Root,
26        imp: &ImportData,
27        base_path: &Path,
28    ) -> Mesh {
29        let primitives: Vec<Primitive> = g_mesh.primitives()
30            .enumerate()
31            .map(|(i, g_prim)| {
32                Primitive::from_gltf(&g_prim, i, g_mesh.index(), root, imp, base_path)
33            })
34            .collect();
35
36        let bounds = primitives.iter()
37            .fold(Aabb3::zero(), |bounds, prim| prim.bounds.union(&bounds));
38
39        Mesh {
40            index: g_mesh.index(),
41            primitives,
42            name: g_mesh.name().map(|s| s.into()),
43            bounds,
44        }
45    }
46
47    pub fn draw(&self, model_matrix: &Matrix4, mvp_matrix: &Matrix4, camera_position: &Vector3) {
48        for primitive in &self.primitives {
49            unsafe { primitive.draw(model_matrix, mvp_matrix, camera_position) }
50        }
51    }
52}