Trait GeometryBuffer

Source
pub trait GeometryBuffer: Downcast {
    // Required methods
    fn set_buffer_data(&self, buffer: usize, data: &[u8]);
    fn element_count(&self) -> usize;
    fn set_triangles(&self, triangles: &[TriangleDefinition]);
    fn set_lines(&self, lines: &[[u32; 2]]);
    fn set_points(&self, points: &[u32]);
}
Expand description

Geometry buffer is a mesh buffer, that could contain multiple vertex buffers and only one element buffer. Element could be either a line or triangle (the most commonly used one).

§Examples

The simplest possible example shows how to create a geometry buffer that has a single triangle:

use fyrox_graphics::{
    buffer::BufferUsage,
    core::{algebra::Vector3, math::TriangleDefinition},
    error::FrameworkError,
    geometry_buffer::{
        AttributeDefinition, AttributeKind, ElementsDescriptor, GeometryBuffer,
        GeometryBufferDescriptor, VertexBufferData, VertexBufferDescriptor,
    },
    server::GraphicsServer,
};
use bytemuck::{Pod, Zeroable};

// Vertex type must implement a bunch of traits, that guarantees that the data will be tightly
// packed with expected order of elements.
#[derive(Pod, Copy, Clone, Zeroable)]
#[repr(C)]
struct Vertex {
    position: Vector3<f32>,
}

fn create_geometry_buffer(
    server: &dyn GraphicsServer,
) -> Result<Box<dyn GeometryBuffer>, FrameworkError> {
    let vertices = [
        Vertex {
            position: Vector3::new(0.0, 0.0, 0.0),
        },
        Vertex {
            position: Vector3::new(0.0, 1.0, 0.0),
        },
        Vertex {
            position: Vector3::new(1.0, 0.0, 0.0),
        },
    ];

    let triangles = [TriangleDefinition([0, 1, 2])];

    server.create_geometry_buffer(GeometryBufferDescriptor {
        buffers: &[VertexBufferDescriptor {
            usage: BufferUsage::StaticDraw,
            attributes: &[AttributeDefinition {
                location: 0,
                kind: AttributeKind::Float,
                component_count: 3,
                normalized: false,
                divisor: 0,
            }],
            data: VertexBufferData::new(Some(&vertices)),
        }],
        usage: BufferUsage::StaticDraw,
        elements: ElementsDescriptor::Triangles(&triangles),
    })
}

Required Methods§

Source

fn set_buffer_data(&self, buffer: usize, data: &[u8])

Write untyped data to a vertex buffer with the given index.

Source

fn element_count(&self) -> usize

Returns total number of elements in the geometry buffer.

Source

fn set_triangles(&self, triangles: &[TriangleDefinition])

Writes triangles to the buffer. Each triangle definition contains triangle indices, that forms the triangle.

Source

fn set_lines(&self, lines: &[[u32; 2]])

Writes lines to the buffer. Each pair defines starting and ending vertex index of the line.

Source

fn set_points(&self, points: &[u32])

Writes points to the buffer. Each index in the slice defines vertex index.

Implementations§

Source§

impl dyn GeometryBuffer

Source

pub fn set_buffer_data_of_type<T>(&mut self, buffer: usize, data: &[T])
where T: Pod,

Writes a typed data to a vertex buffer with the given index. Underlying type must implement Pod trait!

Trait Implementations§

Implementors§