GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
vertexarray.hpp
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <gls/headercheck.hpp>
8 #include <gls/errorcheck.hpp>
9 #include <gls/objects/object.hpp>
10 #include <gls/objects/buffer.hpp>
11 #include <cassert>
12 
13 namespace gls {
14 
15 namespace priv {
16  void gen_vertex_arrays( GLsizei size, GLuint* name ) { glGenVertexArrays( size, name ); }
17  void delete_vertex_arrays( GLsizei size, const GLuint* name ) { glDeleteVertexArrays( size, name ); }
18 }
19 
24 class vertexarray {
25 public:
32  GLuint name() const {
33  return m_object.name();
34  }
35 
40  void bind() {
41  check_gl_error( glBindVertexArray( m_object.name() ) );
42  }
43 
48  static void unbind() {
49  check_gl_error( glBindVertexArray( 0 ) );
50  }
51 
71  template<GLenum BufferType, typename T, typename U>
72  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 ) {
73  auto attribute_location = the_program.get_attribute_location( attribute_name );
74 
75  if( attribute_location < 0 ) {
76  return;
77  }
78 
79  bind_attribute( static_cast<GLuint>( attribute_location ), the_buffer, size, type, normalized, stride, offset );
80  }
81 
100  template<GLenum BufferType, typename T, typename U>
101  void bind_attribute( GLuint attribute_location, const buffer<BufferType>& the_buffer, GLint size, GLenum type, GLboolean normalized, T stride, U offset ) {
102  bind();
103  check_gl_error( glBindBuffer( GL_ARRAY_BUFFER, the_buffer.name() ) );
104  check_gl_error( glEnableVertexAttribArray( attribute_location ) );
105  check_gl_error( glVertexAttribPointer( attribute_location, size, type, normalized, static_cast<GLsizei>( stride ), reinterpret_cast<const GLvoid*>( offset ) ) );
106  check_gl_error( glBindBuffer( GL_ARRAY_BUFFER, 0 ) );
107  unbind();
108  check_gl_error( glDisableVertexAttribArray( attribute_location ) );
109  }
110 
122  void unbind_attribute( const program& the_program, const std::string& attribute_name ) {
123  auto attribute_location = the_program.get_attribute_location( attribute_name );
124 
125  if( attribute_location < 0 ) {
126  return;
127  }
128 
129  unbind_attribute( static_cast<GLuint>( attribute_location ) );
130  }
131 
142  void unbind_attribute( GLuint attribute_location ) {
143  bind();
144  check_gl_error( glDisableVertexAttribArray( static_cast<GLuint>( attribute_location ) ) );
145  unbind();
146  }
147 
164  auto max_vertex_attributes = GLint();
165 
166  check_gl_error( glGetIntegerv( GL_MAX_VERTEX_ATTRIBS, &max_vertex_attributes ) );
167 
168  bind();
169 
170  for( GLuint index = 0; index < static_cast<GLuint>( max_vertex_attributes ); ++index ) {
171  check_gl_error( glDisableVertexAttribArray( index ) );
172  }
173 
174  unbind();
175  }
176 
188  template<GLenum BufferType>
189  void bind_index_buffer( const buffer<BufferType>& the_buffer ) {
190  bind();
191  check_gl_error( glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, the_buffer.name() ) );
192  unbind();
193  check_gl_error( glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ) );
194  }
195 
203  bind();
204  check_gl_error( glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ) );
205  unbind();
206  }
207 
208 private:
210 };
211 
212 }
213 
GLint get_attribute_location(const std::string &attribute_name) const
Get the location of an attribute.
Definition: program.hpp:154
Class encapsulating an OpenGL program object.
Definition: program.hpp:36
void bind_index_buffer(const buffer< BufferType > &the_buffer)
Bind an index buffer to this vertex array.
Definition: vertexarray.hpp:189
void unbind_index_buffer()
Unbind the index buffer from this vertex array.
Definition: vertexarray.hpp:202
Class encapsulating an OpenGL buffer object.
Definition: buffer.hpp:27
Class encapsulating an OpenGL vertex array object.
Definition: vertexarray.hpp:24
static void unbind()
Unbind the currently bound vertex array.
Definition: vertexarray.hpp:48
GLuint name() const
Retrieve the OpenGL name of this buffer.
Definition: buffer.hpp:35
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.
Definition: vertexarray.hpp:101
GLuint name() const
Retrieve the OpenGL name of this vertex array.
Definition: vertexarray.hpp:32
void clear_attribute_bindings()
Clear all attribute bindings.
Definition: vertexarray.hpp:163
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.
Definition: vertexarray.hpp:72
Definition: buffer.hpp:12
void bind()
Bind this vertex array.
Definition: vertexarray.hpp:40
void unbind_attribute(const program &the_program, const std::string &attribute_name)
Unbind a shader attribute.
Definition: vertexarray.hpp:122
void unbind_attribute(GLuint attribute_location)
Unbind a shader attribute.
Definition: vertexarray.hpp:142