GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
vertexarray Class Reference

Class encapsulating an OpenGL vertex array object. More...

#include <vertexarray.hpp>

Public Member Functions

GLuint name () const
 Retrieve the OpenGL name of this vertex array. More...
 
void bind ()
 Bind this vertex array. More...
 
template<GLenum BufferType, typename T , typename U >
void bind_attribute (const program &the_program, const std::string &attribute_name, const buffer< BufferType > &the_buffer, GLint size, GLenum type, GLboolean normalized, T stride, U offset)
 Bind a shader attribute to a buffer source. More...
 
template<GLenum BufferType, typename T , typename U >
void bind_attribute (GLuint attribute_location, const buffer< BufferType > &the_buffer, GLint size, GLenum type, GLboolean normalized, T stride, U offset)
 Bind a shader attribute to a buffer source. More...
 
void unbind_attribute (const program &the_program, const std::string &attribute_name)
 Unbind a shader attribute. More...
 
void unbind_attribute (GLuint attribute_location)
 Unbind a shader attribute. More...
 
void clear_attribute_bindings ()
 Clear all attribute bindings. More...
 
template<GLenum BufferType>
void bind_index_buffer (const buffer< BufferType > &the_buffer)
 Bind an index buffer to this vertex array. More...
 
void unbind_index_buffer ()
 Unbind the index buffer from this vertex array. More...
 

Static Public Member Functions

static void unbind ()
 Unbind the currently bound vertex array. More...
 

Detailed Description

Class encapsulating an OpenGL vertex array object.

A gls::vertexarray is an object that encapsulates an OpenGL vertex array object.

Like all objects in GLS, the underlying name is generated at object construction and deleted at destruction. This can be retrieved with name().

WARNING: Unlike other OpenGL objects, vertex array objects are not shareable between contexts. As such, a gls::vertexarray object is only valid for use within the context where it was constructed. No checks are made to ensure it is used properly. If strange behaviour occurs, be sure to check the reported OpenGL errors in a debug configuration and assure you are not transferring the gls::vertexarray between contexts.

Vertex array objects are basically a way to store the layout and sources of attribute data. While multiple programs can use the same vertex array, it makes less sense to use different vertex arrays to reference the same data. Vertex arrays are meant to be set up once and reused many times. They only save the source of vertex attributes and not the data themselves. This means that it is not necessary to update a vertex array if the data within a buffer object is changed. It does mean however, that if a buffer object is recreated, deleted, or has its name changed, you will need to rebind/unbind the attribute(s) sourcing from that buffer object.

In addition to storing the layout and sources of attribute data, vertex arrays also store the element array binding. This means that you can store a reference to an index buffer within the vertex array as well. The same applies here as well however: If the index buffer object is recreated, deleted, or has its name changed, you will need to rebind/unbind it to/from the vertex array.

To use a gls::vertexarray, simply call bind(). It will bind the gls::vertexarray as the current one used for rendering and replace any that was previously bound. To clear the binding, call unbind(). It is an error in newer versions of OpenGL to render without a bound vertex array.

Example usage:

// Create and link the program from our compiled shaders
auto program = gls::program();
program.link( vertex_shader, fragment_shader );
auto position_data = std::vector<float>();
position_data = { -.5f, -.5f, -1.f, .5f, -.5f, -1.f, 0.f, .5f, -1.f };
position_buffer.data( position_data.size() * sizeof( float ), position_data.data() );
auto vertexarray = gls::vertexarray();
// Bind a "position" attribute in the vertex shader to our position buffer
vertexarray.bind_attribute( program, "position", position_buffer, 3, GL_FLOAT, GL_FALSE, 0, 0 );

Member Function Documentation

GLuint name ( ) const

Retrieve the OpenGL name of this vertex array.

Returns
OpenGL name of this vertex array
void bind ( )

Bind this vertex array.

static void unbind ( )
static

Unbind the currently bound vertex array.

void bind_attribute ( const program the_program,
const std::string &  attribute_name,
const buffer< BufferType > &  the_buffer,
GLint  size,
GLenum  type,
GLboolean  normalized,
stride,
offset 
)

Bind a shader attribute to a buffer source.

Shader attributes need to have a data source to provide the required attribute data to a vertex shader. This method binds an attribute with the given name in the given program to the given buffer source. Since attributes may be interleaved within the same buffer, providing a non-zero stride and offset might be necessary as well.

Parameters
the_programThe program in which the attribute resides
attribute_nameThe name of the attribute to bind
the_bufferThe buffer to source data from
sizeThe size (number of components) of an attribute
typeThe data type of an attribute component
normalizedWhether to normalize integer data to the floating point range [0.f, 1.f] for unsigned or [-1.f, 1.f] for signed in the shader
strideNumber of bytes between successive attribute elements in the buffer
offsetByte offset into the buffer where the first element is located
void bind_attribute ( GLuint  attribute_location,
const buffer< BufferType > &  the_buffer,
GLint  size,
GLenum  type,
GLboolean  normalized,
stride,
offset 
)

Bind a shader attribute to a buffer source.

Shader attributes need to have a data source to provide the required attribute data to a vertex shader. This method binds an attribute with the given location to the given buffer source. Since attributes may be interleaved within the same buffer, providing a non-zero stride and offset might be necessary as well.

Parameters
attribute_locationLocation of the attribute
the_bufferThe buffer to source data from
sizeThe size (number of components) of an attribute
typeThe data type of an attribute component
normalizedWhether to normalize integer data to the floating point range [0.f, 1.f] for unsigned or [-1.f, 1.f] for signed in the shader
strideNumber of bytes between successive attribute elements in the buffer
offsetByte offset into the buffer where the first element is located
void unbind_attribute ( const program the_program,
const std::string &  attribute_name 
)

Unbind a shader attribute.

This method disables the vertex attribute with the given name in the given program from sourcing its data from a buffer. If the attribute is present in the shader, it will be set to a constant value for every invocation.

Parameters
the_programThe program in which the attribute resides
attribute_nameThe name of the attribute to unbind
void unbind_attribute ( GLuint  attribute_location)

Unbind a shader attribute.

This method disables the vertex attribute with the given location from sourcing its data from a buffer. If the attribute is present in the shader, it will be set to a constant value for every invocation.

Parameters
attribute_locationThe location of the attribute to unbind
void clear_attribute_bindings ( )

Clear all attribute bindings.

Calling this method is equivalent to this code:

auto max_vertex_attributes = GLint();
check_gl_error( glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max_vertex_attributes ) );
vertexarray.bind();
for( GLuint index = 0; index < max_vertex_attributes; ++index ) {
glDisableVertexAttribArray( index );
}
vertexarray.unbind();
void bind_index_buffer ( const buffer< BufferType > &  the_buffer)

Bind an index buffer to this vertex array.

When doing indexed rendering, it is required that you bind a buffer containing the indices of the vertices to be rendered to the element array buffer binding within this vertex array. That is what this method does.

Parameters
the_bufferThe index buffer to bind to this vertex array
void unbind_index_buffer ( )

Unbind the index buffer from this vertex array.

This will clear the element array buffer binding within this vertex array.