Module glium::vertex

source ·
Expand description

Contains everything related to vertex sources.

When you draw, you need to pass one or several sources of vertex attributes. This is done with the first parameter to the draw function.

Vertex

The main trait of this module is Vertex, which must be implemented on structs whose instances describe individual vertices. The trait is unsafe to implement, so you are encouraged to use the implement_vertex! macro instead:

#[derive(Copy, Clone)]
struct MyVertex {
    position: [f32; 3],
    texcoords: [f32; 2],
}

// you must pass the list of members to the macro
implement_vertex!(MyVertex, position, texcoords);

Vertex buffer

Once you have a struct that implements the Vertex trait, you can build an array of vertices and upload it to the video memory by creating a VertexBuffer.

let data = &[
    MyVertex {
        position: [0.0, 0.0, 0.4],
        texcoords: [0.0, 1.0]
    },
    MyVertex {
        position: [12.0, 4.5, -1.8],
        texcoords: [1.0, 0.5]
    },
    MyVertex {
        position: [-7.124, 0.1, 0.0],
        texcoords: [0.0, 0.4]
    },
];

let vertex_buffer = glium::vertex::VertexBuffer::new(&display, data);

Drawing

When you draw, you can pass either a single vertex source or a tuple of multiple sources. Each source can be:

  • A reference to a VertexBuffer.
  • A slice of a vertex buffer, by calling vertex_buffer.slice(start .. end).unwrap().
  • A vertex buffer where each element corresponds to an instance, by caling vertex_buffer.per_instance().
  • The same with a slice, by calling vertex_buffer.slice(start .. end).unwrap().per_instance().
  • A marker indicating a number of vertex sources, with glium::vertex::EmptyVertexAttributes.
  • A marker indicating a number of instances, with glium::vertex::EmptyInstanceAttributes.
// drawing with a single vertex buffer
frame.draw(&vertex_buffer, &indices, &program, &uniforms, &Default::default()).unwrap();

// drawing with two parallel vertex buffers
frame.draw((&vertex_buffer, &vertex_buffer2), &indices, &program,
           &uniforms, &Default::default()).unwrap();

// drawing without a vertex source
frame.draw(glium::vertex::EmptyVertexAttributes { len: 12 }, &indices, &program,
           &uniforms, &Default::default()).unwrap();

// drawing a slice of a vertex buffer
frame.draw(vertex_buffer.slice(6 .. 24).unwrap(), &indices, &program,
           &uniforms, &Default::default()).unwrap();

// drawing slices of two vertex buffers
frame.draw((vertex_buffer.slice(6 .. 24).unwrap(), vertex_buffer2.slice(128 .. 146).unwrap()),
           &indices, &program, &uniforms, &Default::default()).unwrap();

// treating `vertex_buffer2` as a source of attributes per-instance instead of per-vertex
frame.draw((&vertex_buffer, vertex_buffer2.per_instance().unwrap()), &indices,
           &program, &uniforms, &Default::default()).unwrap();

// instancing without any per-instance attribute
frame.draw((&vertex_buffer, glium::vertex::EmptyInstanceAttributes { len: 36 }), &indices,
           &program, &uniforms, &Default::default()).unwrap();

Note that if you use index::EmptyIndices as indices the length of all vertex sources must be the same, or a DrawError::VerticesSourcesLengthMismatch will be produced.

In all situation, the length of all per-instance sources must match, or DrawError::InstancesCountMismatch will be retured.

Transform feedback

Transform feedback allows you to write in a buffer the list of primitives that are generated by the GPU.

To use it, you must first create a TransformFeedbackSession with TransformFeedbackSession::new(). This function requires you to pass a buffer of the correct type and a program. Then you must pass the &TransformFeedbackSession to the draw parameters. The program you use when drawing must be the same as you the one you created the session with, or else you will get an error.

Structs

  • Marker that can be passed instead of a buffer to indicate an empty list of buffers.
  • Marker that can be passed instead of a buffer to indicate an empty list of buffers.
  • Marker that instructs glium that the buffer is to be used per instance.
  • Transform feedback allows you to obtain in a buffer the list of the vertices generated by the vertex shader, geometry shader, or tessellation evaluation shader of your program. This is usually used to cache the result in order to draw the vertices multiple times with multiple different fragment shaders.
  • A list of vertices loaded in the graphics card’s memory.
  • A list of vertices loaded in the graphics card’s memory.
  • Represents a slice of a VertexBuffer.

Enums

Traits

Functions

Type Definitions

  • Describes the layout of each vertex in a vertex buffer.