mod3d_gl/
vertices.rs

1//a Imports
2use std::rc::Rc;
3
4use crate::{Gl, IndexBuffer, VertexBuffer};
5
6//a Vertices
7//tp Vertices
8/// This is a set of OpenGL vertices with [crate::GlBuffer] for all of its contents
9///
10/// This is part of the [RenderContext], and so has a different
11/// lifetime to the model3d objects and vertices. It is created by
12/// invoking create_client on a [mod3d_rs::Object]
13#[derive(Debug)]
14pub struct Vertices<G>
15where
16    G: Gl,
17{
18    indices: Rc<IndexBuffer<G>>,
19    position: Rc<VertexBuffer<G>>,
20    attrs: Rc<Vec<(mod3d_base::VertexAttr, VertexBuffer<G>)>>,
21}
22
23//ip Clone for Vertices
24impl<G> Clone for Vertices<G>
25where
26    G: Gl,
27{
28    fn clone(&self) -> Self {
29        let indices = self.indices.clone();
30        let position = self.position.clone();
31        let attrs = self.attrs.clone();
32        Self {
33            indices,
34            position,
35            attrs,
36        }
37    }
38}
39
40//ip Vertices
41impl<G> Vertices<G>
42where
43    G: Gl,
44{
45    //mp create
46    /// Create based on [mod3d_rs::Vertices]
47    ///
48    /// Todo: remove unwrap() in position; make it do nothing if there is no position attr
49    ///
50    /// Todo: support indics being an option
51    pub fn create(vertices: &mod3d_base::Vertices<G>, _renderer: &mut G) -> Self {
52        let indices = vertices
53            .borrow_indices()
54            .unwrap()
55            .borrow_client()
56            .as_index_buffer()
57            .clone()
58            .into();
59        let position = vertices
60            .borrow_attr(mod3d_base::VertexAttr::Position)
61            .unwrap()
62            .borrow_client()
63            .as_vertex_buffer()
64            .clone()
65            .into();
66        let mut attrs = Vec::new();
67        for buffer in vertices.iter_attrs() {
68            attrs.push((
69                buffer.vertex_attr(),
70                buffer.borrow_client().as_vertex_buffer().clone(),
71            ));
72        }
73        let attrs = attrs.into();
74        Self {
75            indices,
76            position,
77            attrs,
78        }
79    }
80    //fp borrow
81    /// Borrow the indices, positions, and the array of other attributes
82    pub fn borrow(
83        &self,
84    ) -> (
85        &IndexBuffer<G>,
86        &VertexBuffer<G>,
87        &Vec<(mod3d_base::VertexAttr, VertexBuffer<G>)>,
88    ) {
89        (&self.indices, &self.position, &self.attrs)
90    }
91}
92
93//ip Display for Vertices
94impl<G> std::fmt::Display for Vertices<G>
95where
96    G: Gl,
97{
98    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
99        writeln!(fmt, "ind:{}", self.indices)?;
100        writeln!(fmt, "pos:{}", self.position)
101    }
102}
103
104//ip Default for Vertices
105impl<G> Default for Vertices<G>
106where
107    G: Gl,
108{
109    /// Create a none
110    fn default() -> Self {
111        let indices = IndexBuffer::default().into();
112        let position = VertexBuffer::default().into();
113        let attrs = Vec::new().into();
114        Self {
115            indices,
116            position,
117            attrs,
118        }
119    }
120}
121
122//ip VerticesClient for Vertices
123impl<G> mod3d_base::VerticesClient for Vertices<G> where G: Gl {}