mod3d_base/
primitive.rs

1//a Imports
2use crate::{PrimitiveType, ShortIndex};
3
4//a Primitive
5//tp Primitive
6/// A primitive consisting of a material and a subset of
7/// vertices using a particular range of indices
8///
9/// This might be, for example, the arm of a robot.
10///
11/// The [Primitive] depends on being in an 3D model [crate::Object] (or its derived [crate::Instantiable], as it is the
12/// object that contains the actual materials and vertices to use
13///
14/// This *SHOULD* be optimized to fit within half a cache line (32 bytes)
15///
16/// Missing:
17///
18/// uses bones?
19/// index type (u8, u16, u32) - is this part of indices?
20#[derive(Debug, Clone)]
21pub struct Primitive {
22    /// Byte offset to first index to use
23    ///
24    /// If vertices_index is None then the first PrimitiveType to draw in the array
25    byte_offset: u32,
26    /// Number of indices to use
27    index_count: u32,
28    /// Material to be used in drawing - index within the [crate::Object]
29    material_index: ShortIndex,
30    /// Vertices index within the [crate::Object]
31    ///
32    /// This provides (effectively) the set of attribute `BufferView`s that the mesh utilizes
33    ///
34    /// May be 'None'
35    vertices_index: ShortIndex,
36    /// Type of the primitive (u8)
37    primitive_type: PrimitiveType,
38}
39
40//ip Primitive
41impl Primitive {
42    //fp new
43    /// Create a new Primitive from a Vertices
44    ///
45    /// use the indices' BufferView.ele_type: BufferElementType as index size
46    pub fn new(
47        primitive_type: PrimitiveType,
48        vertices_index: ShortIndex,
49        byte_offset: u32,
50        index_count: u32,
51        material_index: ShortIndex,
52    ) -> Self {
53        Self {
54            byte_offset,
55            index_count,
56            material_index,
57            vertices_index,
58            primitive_type,
59        }
60    }
61
62    //mp vertices
63    /// Retrieve the data for the vertices in the primitive
64    ///
65    /// This is the vertices index, the offset index, and the count
66    #[inline]
67    pub fn vertices(&self) -> (Option<usize>, u32, u32) {
68        (
69            self.vertices_index.into(),
70            self.byte_offset,
71            self.index_count,
72        )
73    }
74
75    //mp material
76    /// Retrieve the material for the primitive - this is the material index
77    #[inline]
78    pub fn material(&self) -> ShortIndex {
79        self.material_index as ShortIndex
80    }
81
82    //mp primitive_type
83    /// Retrieve the [PrimitiveType] of the primitive
84    #[inline]
85    pub fn primitive_type(&self) -> PrimitiveType {
86        self.primitive_type
87    }
88
89    //mp vertices_index
90    /// Retrieve the index into the [crate::Object] vertices array that this
91    /// primitive uses
92    pub fn vertices_index(&self) -> ShortIndex {
93        self.vertices_index
94    }
95
96    //mp material_index
97    /// Retrieve the index into the [crate::Object] materials array that this
98    /// primitive uses
99    pub fn material_index(&self) -> ShortIndex {
100        self.material_index
101    }
102
103    //mp index_count
104    /// Get the number of indices required to draw this primitive
105    pub fn index_count(&self) -> u32 {
106        self.index_count
107    }
108
109    //mp byte_offset
110    /// Get the byte offset within the indices buffer view to the
111    /// first byte used by this primitive
112    pub fn byte_offset(&self) -> u32 {
113        self.byte_offset
114    }
115
116    //zz All done
117}