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    VertexDesc,
13};
14
15/// Add positions, normals and indices to an [ExampleVertices] for a
16/// flat upward-facing triangle on z=0 of a given size
17pub fn new<R: Renderable>(eg: &mut ExampleVertices<R>, size: f32)
18where
19    <R as Renderable>::Descriptor: Unpin,
20{
21    let vertex_data = [
22        -size, -size, 0.0, size, -size, 0.0, 0.0, size, 0.0, 0., 0., 1., 0., 0., 1., 0., 0., 1.,
23    ];
24    let index_data = [0u8, 1, 2];
25
26    let data_vertices = eg.push_byte_buffer(Box::new(vertex_data));
27    let data_indices = eg.push_byte_buffer(Box::new(index_data));
28
29    let indices = eg.push_index_accessor(data_indices, 3, BufferElementType::UInt8, 0);
30
31    let vertex_desc = eg.push_descriptor(data_vertices, 0, 9 * 4, 0); // stride used to be 0
32    let vertices = eg.push_data_accessor(
33        vertex_desc,
34        VertexDesc::vec(VertexAttr::Position, BufferElementType::Float32, 3, 0),
35    );
36
37    let normal_desc = eg.push_descriptor(data_vertices, 9 * 4, 9 * 4, 0); // stride used to be 0
38    let normals = eg.push_data_accessor(
39        normal_desc,
40        VertexDesc::vec(VertexAttr::Normal, BufferElementType::Float32, 3, 0),
41    );
42
43    // Create set of data (indices, vertex data) to by subset into by the meshes and their primitives
44    eg.push_vertices(Some(indices), vertices, &[normals]);
45}
46
47/// Create a mesh for the triangle given the vertices index and
48/// material index within a parent model3d::Object
49///
50/// The object should have had the vertices for the triangle (created
51/// with new() above) added to it (using a parent [ExampleVertices])
52pub fn mesh(v_id: ShortIndex, m_id: ShortIndex) -> Mesh {
53    let mut mesh = Mesh::default();
54    mesh.add_primitive(Primitive::new(PrimitiveType::Triangles, v_id, 0, 3, m_id));
55    mesh
56}