mod3d_base/example_objects/
triangle.rs

1//a Documentation
2/*!
3
4This provides a function to create [ExampleVertices] object that is a triangle of a specified size at z=0
5
6 */
7
8//a Imports
9use super::ExampleVertices;
10use crate::{
11    BufferElementType, Mesh, Primitive, PrimitiveType, Renderable, ShortIndex, VertexAttr,
12};
13
14/// Add positions, normals and indices to an [ExampleVertices] for a
15/// flat upward-facing triangle on z=0 of a given size
16pub fn new<R: Renderable>(eg: &mut ExampleVertices<R>, size: f32) {
17    let vertex_data = [
18        -size, -size, 0.0, size, -size, 0.0, 0.0, size, 0.0, 0., 0., 1., 0., 0., 1., 0., 0., 1.,
19    ];
20    let index_data = [0u8, 1, 2];
21
22    let data_vertices = eg.push_byte_buffer(Box::new(vertex_data));
23    let data_indices = eg.push_byte_buffer(Box::new(index_data));
24
25    let indices = eg.push_index_accessor(data_indices, 3, BufferElementType::UInt8, 0);
26    let vertices = eg.push_data_accessor(data_vertices, 3, BufferElementType::Float32, 0, 0);
27    let normals = eg.push_data_accessor(data_vertices, 3, BufferElementType::Float32, 9 * 4, 0);
28
29    // Create set of data (indices, vertex data) to by subset into by the meshes and their primitives
30    eg.push_vertices(Some(indices), vertices, &[(VertexAttr::Normal, normals)]);
31}
32
33/// Create a mesh for the triangle given the vertices index and
34/// material index within a parent model3d::Object
35///
36/// The object should have had the vertices for the triangle (created
37/// with new() above) added to it (using a parent [ExampleVertices])
38pub fn mesh(v_id: ShortIndex, m_id: ShortIndex) -> Mesh {
39    let mut mesh = Mesh::default();
40    mesh.add_primitive(Primitive::new(PrimitiveType::Triangles, v_id, 0, 3, m_id));
41    mesh
42}