mod3d-base 0.3.0

3D Models for Gl processing
Documentation
//a Imports
use std::cell::RefCell;
use std::ops::DerefMut;

use crate::{BufferData, BufferElementType, Renderable};

//a BufferIndexAccessor
//tp BufferIndexAccessor
/// A subset of a `BufferData`, used for vertex attributes;
/// hence for use in a vertex attribute pointer.
///
/// A `BufferIndexAccessor` is used for a single attribute of a set of data, such as
/// Position or Normal.
pub struct BufferIndexAccessor<'a, R: Renderable> {
    /// The `BufferData` that contains the actual index data
    data: &'a BufferData<'a, R>,

    /// Number of indices in the buffer
    number_indices: u32,

    /// The type of each element
    ///
    /// For indices this must be UInt8, UInt16 or UInt32
    ele_type: BufferElementType,

    /// Offset from start of buffer to first byte of data
    byte_offset: u32,

    /// The client bound to data\[byte_offset\] .. + byte_length
    ///
    /// This must be held as a [RefCell] as the [BufferData] is
    /// created early in the process, prior to any `BufferIndexAccessor`s using
    /// it - which then have shared references to the daata - but the
    /// client is created afterwards
    rc_client: RefCell<R::IndexAccessor>,
}

//ip Display for BufferIndexAccessor
impl<'a, R: Renderable> std::fmt::Debug for BufferIndexAccessor<'a, R>
where
    R: Renderable,
{
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(
            fmt,
            "BufferIndexAccessor{{ {:?}:{:?} #{}@{}}}",
            self.data,
            self.ele_type,
            self.number_indices,
            self.byte_offset,
            //  self.rc_client
        )
    }
}

//ip AsRef<[u8]> for BufferIndexAccessor
impl<'a, R> AsRef<[u8]> for BufferIndexAccessor<'a, R>
where
    R: Renderable,
{
    fn as_ref(&self) -> &[u8] {
        let data: &[u8] = self.data.as_ref();
        let start = self.byte_offset as usize;
        let byte_length = self.number_indices * self.ele_type.byte_length();
        let end = (self.byte_offset + byte_length) as usize;
        &data[start..end]
    }
}

//ip BufferIndexAccessor
impl<'a, R: Renderable> BufferIndexAccessor<'a, R> {
    //ap data
    /// Get a reference to the underlying [BufferData]
    pub fn data(&self) -> &BufferData<'a, R> {
        self.data
    }

    //ap number_indices
    /// Number of indices in the buffer
    pub fn number_indices(&self) -> u32 {
        self.number_indices
    }

    //ap ele_type
    /// The type of each element
    ///
    /// For indices this must be UInt8, UInt16 or UInt32
    pub fn ele_type(&self) -> BufferElementType {
        self.ele_type
    }

    //ap byte_offset
    /// Offset from start of buffer to first byte of data
    pub fn byte_offset(&self) -> u32 {
        self.byte_offset
    }

    //fp new
    /// Create a new index accessor of a `BufferData`
    pub fn new(
        data: &'a BufferData<'a, R>,
        number_indices: u32,
        ele_type: BufferElementType,
        byte_offset: u32, // offset in bytes from start of data
    ) -> Self {
        let rc_client = RefCell::new(R::IndexAccessor::default());
        Self {
            data,
            number_indices,
            ele_type,
            byte_offset,
            rc_client,
        }
    }

    //mp create_client
    /// Create the render buffer required by the BufferIndexAccessor
    pub fn create_client(&self, renderable: &mut R) {
        renderable.init_index_accessor_client(self.rc_client.borrow_mut().deref_mut(), self);
    }

    //ap borrow_client
    /// Borrow the client
    pub fn borrow_client(&self) -> std::cell::Ref<R::IndexAccessor> {
        self.rc_client.borrow()
    }

    //zz All done
}

//ip Display for BufferIndexAccessor
impl<'a, R: Renderable> std::fmt::Display for BufferIndexAccessor<'a, R> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
        std::fmt::Debug::fmt(self, fmt)
    }
}

//ip DefaultIndentedDisplay for BufferIndexAccessor
impl<'a, R: Renderable> indent_display::DefaultIndentedDisplay for BufferIndexAccessor<'a, R> {}