mod3d_base/
buffer_index_accessor.rs

1//a Imports
2use std::cell::RefCell;
3use std::ops::DerefMut;
4
5use crate::{BufferData, BufferElementType, Renderable};
6
7//a BufferIndexAccessor
8//tp BufferIndexAccessor
9/// A subset of a `BufferData`, used for vertex attributes;
10/// hence for use in a vertex attribute pointer.
11///
12/// A `BufferIndexAccessor` is used for a single attribute of a set of data, such as
13/// Position or Normal.
14pub struct BufferIndexAccessor<'a, R: Renderable + ?Sized> {
15    /// The `BufferData` that contains the actual index data
16    pub data: &'a BufferData<'a, R>,
17    /// Number of indices in the buffer
18    pub number_indices: u32,
19    /// The type of each element
20    ///
21    /// For indices this must be UInt8, UInt16 or UInt32
22    pub ele_type: BufferElementType,
23    /// Offset from start of buffer to first byte of data
24    pub byte_offset: u32,
25    /// The client bound to data\[byte_offset\] .. + byte_length
26    ///
27    /// This must be held as a [RefCell] as the [BufferData] is
28    /// created early in the process, prior to any `BufferIndexAccessor`s using
29    /// it - which then have shared references to the daata - but the
30    /// client is created afterwards
31    rc_client: RefCell<R::IndexAccessor>,
32}
33
34//ip Display for BufferIndexAccessor
35impl<'a, R: Renderable> std::fmt::Debug for BufferIndexAccessor<'a, R>
36where
37    R: Renderable,
38{
39    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
40        write!(
41            fmt,
42            "BufferIndexAccessor{{ {:?}:{:?} #{}@{}}}",
43            self.data,
44            self.ele_type,
45            self.number_indices,
46            self.byte_offset,
47            //  self.rc_client
48        )
49    }
50}
51
52//ip BufferIndexAccessor
53impl<'a, R: Renderable> BufferIndexAccessor<'a, R> {
54    //fp new
55    /// Create a new index accessor of a `BufferData`
56    pub fn new(
57        data: &'a BufferData<'a, R>,
58        number_indices: u32,
59        ele_type: BufferElementType,
60        byte_offset: u32, // offset in bytes from start of data
61    ) -> Self {
62        let rc_client = RefCell::new(R::IndexAccessor::default());
63        Self {
64            data,
65            number_indices,
66            ele_type,
67            byte_offset,
68            rc_client,
69        }
70    }
71
72    //mp create_client
73    /// Create the render buffer required by the BufferIndexAccessor
74    pub fn create_client(&self, renderable: &mut R) {
75        renderable.init_index_accessor_client(self.rc_client.borrow_mut().deref_mut(), self);
76    }
77
78    //ap borrow_client
79    /// Borrow the client
80    pub fn borrow_client(&self) -> std::cell::Ref<R::IndexAccessor> {
81        self.rc_client.borrow()
82    }
83
84    //zz All done
85}
86
87//ip Display for BufferIndexAccessor
88impl<'a, R: Renderable> std::fmt::Display for BufferIndexAccessor<'a, R> {
89    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
90        std::fmt::Debug::fmt(self, fmt)
91    }
92}
93
94//ip DefaultIndentedDisplay for BufferIndexAccessor
95impl<'a, R: Renderable> indent_display::DefaultIndentedDisplay for BufferIndexAccessor<'a, R> {}