GLS
1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
|
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... | |
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:
GLuint name | ( | ) | const |
Retrieve the OpenGL name of this vertex array.
void bind | ( | ) |
Bind this vertex array.
|
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, | ||
T | stride, | ||
U | 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.
the_program | The program in which the attribute resides |
attribute_name | The name of the attribute to bind |
the_buffer | The buffer to source data from |
size | The size (number of components) of an attribute |
type | The data type of an attribute component |
normalized | Whether 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 |
stride | Number of bytes between successive attribute elements in the buffer |
offset | Byte 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, | ||
T | stride, | ||
U | 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.
attribute_location | Location of the attribute |
the_buffer | The buffer to source data from |
size | The size (number of components) of an attribute |
type | The data type of an attribute component |
normalized | Whether 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 |
stride | Number of bytes between successive attribute elements in the buffer |
offset | Byte 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.
the_program | The program in which the attribute resides |
attribute_name | The 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.
attribute_location | The location of the attribute to unbind |
void clear_attribute_bindings | ( | ) |
Clear all attribute bindings.
Calling this method is equivalent to this code:
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.
the_buffer | The 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.