Trait glitter::vertex_data::VertexData [] [src]

pub unsafe trait VertexData: Copy {
    fn visit_attributes<F>(f: F)
    where
        F: FnMut(VertexAttribute)
; }

A type that has vertex data.

Safety

Implementors must properly implement the visit_attributes method. Implementing this method incorrectly will lead to memory unsafety in some safe API's in glitter.

Example

#[macro_use] extern crate glitter;
use glitter::prelude::*;

#[derive(Clone, Copy)] // `VertexData` types must implement `Copy`
#[repr(C)] // Use a predictable data layout (for our field offsets)
struct MyVertex {
    position: [f32; 3],
    color: [f32; 3]
}

// Manually implement the `VertexData` trait
unsafe impl glitter::VertexData for MyVertex {
    fn visit_attributes<F>(mut f: F)
        where F: FnMut(glitter::VertexAttribute)
    {
        use std::mem;
        use glitter::{VertexAttribute, VertexAttributeType, VertexDatum};

        // The vertex attribute type for an `[f32; 3]`, or `vec3`
        let vec3 = <[f32; 3] as VertexDatum>::attrib_type();

        // let vec3 = VertexAttributeType {
        //     data: glitter::FLOAT,
        //     components: 3,
        //     normalize: false
        // };

        let stride = mem::size_of::<MyVertex>();
        let position_offset = 0;
        let position_size = mem::size_of::<[f32; 3]>();
        let color_offset = position_offset + position_size;

        let position = VertexAttribute {
            ty: vec3.clone(),
            name: "position".into(),
            offset: position_offset,
            stride: stride
        };
        let color = VertexAttribute {
            ty: vec3,
            name: "color".into(),
            offset: color_offset,
            stride: stride
        };

        // Call the function with the "position" and "color" attributes
        f(position);
        f(color);
    }
}

See also

impl_vertx_data!: A macro that automatically implements VertexData for structs.

Required Methods

Call the given function with each attribute that this vertex data is composed of.

Safety

The function must only be called once per attribute, and the provided values of the VertexAttribute passed to the function must be correct. See the VertexAttribute docs for more details on what each field means.

Implementors