BARE2D
VAO.cpp
Go to the documentation of this file.
1 #include "VAO.hpp"
2 
3 namespace BARE2D {
4 
6  {
7  m_numAttributes = 0;
8  }
9 
11  {
12  }
13 
14  void VAO::init()
15  {
16  // Generate VAO, VBO, and connect em up
17  // Generate VAO
18  glGenVertexArrays(1, &m_vaoHandle);
19 
20  // Now bind it so the VBO is created as a part of it
21  bind();
22 
23  // Generate VBO inside of the VAO
24  glGenBuffers(1, &m_vboHandle);
25 
26  // Bind the VBO to the VAO
27  glBindBuffer(GL_ARRAY_BUFFER, m_vboHandle);
28 
29  // Unbind, we're done for now.
30  unbind();
31  }
32 
33  void VAO::destroy()
34  {
35  // Unbind first!
36  unbind();
37 
38  // Disable all vertex attributes
39  for(unsigned int i = 0; i < m_numAttributes; i++) {
40  glDisableVertexAttribArray(i);
41  }
42 
43  // Delete buffers
44  glDeleteBuffers(1, &m_vboHandle);
45  glDeleteVertexArrays(1, &m_vaoHandle);
46  }
47 
48  void VAO::bind()
49  {
50  glBindVertexArray(m_vaoHandle);
51  }
52 
53  void VAO::unbind() {
54  glBindVertexArray(0);
55  }
56 
57  void VAO::bindVBO()
58  {
59  glBindBuffer(GL_ARRAY_BUFFER, m_vboHandle);
60  }
61 
62  void VAO::unbindVBO() {
63  glBindBuffer(GL_ARRAY_BUFFER, 0);
64  }
65 
66  void VAO::addVertexAttribute(GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* data)
67  {
68  // Bind first - It doesn't matter that this is inefficient because we're only calling this like 3 times in the whole program
69  bind();
70  bindVBO();
71 
72  // Tell OpenGL what kind of data goes in that array
73  glVertexAttribPointer(m_numAttributes, size, type, normalized, stride, data);
74  // Enable the array
75  glEnableVertexAttribArray(m_numAttributes);
76 
77  // Increment our number of attributes to reflect reality.
79 
80  // Unbind for safety!
81  unbind();
82  }
83 
84 }
BARE2D::VAO::m_vboHandle
GLuint m_vboHandle
Definition: VAO.hpp:61
BARE2D::VAO::destroy
void destroy()
Releases necessary memory.
Definition: VAO.cpp:33
BARE2D::VAO::m_vaoHandle
GLuint m_vaoHandle
Definition: VAO.hpp:60
BARE2D
Definition: App.cpp:13
BARE2D::VAO::unbind
void unbind()
Unbinds the vao.
Definition: VAO.cpp:53
BARE2D::VAO::init
void init()
Initializes the necessary components, combining the VBO and VAO.
Definition: VAO.cpp:14
BARE2D::VAO::~VAO
~VAO()
Definition: VAO.cpp:10
BARE2D::VAO::bind
void bind()
Binds this vertex array object.
Definition: VAO.cpp:48
BARE2D::VAO::unbindVBO
void unbindVBO()
Unbinds this VAO's VBO.
Definition: VAO.cpp:62
BARE2D::VAO::m_numAttributes
unsigned int m_numAttributes
Definition: VAO.hpp:58
BARE2D::VAO::bindVBO
void bindVBO()
Binds the VAO's VBO.
Definition: VAO.cpp:57
VAO.hpp
BARE2D::VAO::addVertexAttribute
void addVertexAttribute(GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data)
Wrapper for glVertexAttribPointer - Adds an attribute to the VBO - each vertex data slot will gain so...
Definition: VAO.cpp:66
BARE2D::VAO::VAO
VAO()
Definition: VAO.cpp:5