1- #include " Model.h"
2-
3- Model::Model (const std::vector<GLfloat>& vertexPositions)
4- {
5- glGenVertexArrays (1 , &m_vao);
6- glBindVertexArray (m_vao);
7- addVBO (2 , vertexPositions);
8-
9- glBindVertexArray (0 );
10- glBindBuffer (GL_ARRAY_BUFFER, 0 );
11- }
12-
13- Model::~Model ()
14- {
15- glDeleteVertexArrays (1 , &m_vao);
16- glDeleteBuffers (m_buffers.size (), m_buffers.data ());
17- }
18-
19- void Model::bind ()
20- {
21- glBindVertexArray (m_vao);
22- }
23-
24- void Model::unbind ()
25- {
26- glBindVertexArray (0 );
27- }
28-
29- void Model::addVBO (int dim, const std::vector<GLfloat>& data)
30- {
31- GLuint vbo;
32- glGenBuffers (1 , &vbo);
33- glBindBuffer (GL_ARRAY_BUFFER, vbo);
34- glBufferData (GL_ARRAY_BUFFER, data.size () * sizeof (data[0 ]), data.data (), GL_STATIC_DRAW);
35- glVertexAttribPointer (m_vboCount, dim, GL_FLOAT, GL_FALSE, 0 , (GLvoid*) 0 );
36- glEnableVertexAttribArray (m_vboCount++);
37- m_buffers.push_back (vbo);
38- }
1+ #include " Model.h"
2+
3+ Model::Model (const std::vector<GLfloat>& vertexPositions)
4+ {
5+ // generate a vertex array object (VAO)
6+ glGenVertexArrays (1 , &m_vao);
7+ // bind the VAO
8+ glBindVertexArray (m_vao);
9+
10+ // add a vertex buffer object (VBO) for the vertex positions
11+ addVBO (2 , vertexPositions);
12+
13+ // unbind the VAO
14+ glBindVertexArray (0 );
15+ // unbind the buffer
16+ glBindBuffer (GL_ARRAY_BUFFER, 0 );
17+ }
18+
19+ Model::~Model ()
20+ {
21+ // delete the VAO
22+ glDeleteVertexArrays (1 , &m_vao);
23+
24+ // delete the VBOs
25+ glDeleteBuffers (m_buffers.size (), m_buffers.data ());
26+ }
27+
28+ void Model::bind ()
29+ {
30+ // bind the VAO for rendering
31+ glBindVertexArray (m_vao);
32+ }
33+
34+ void Model::unbind ()
35+ {
36+ // unbind the VAO
37+ glBindVertexArray (0 );
38+ }
39+
40+ void Model::addVBO (int dim, const std::vector<GLfloat>& data)
41+ {
42+ GLuint vbo;
43+ // generate a VBO
44+ glGenBuffers (1 , &vbo);
45+ // bind the VBO
46+ glBindBuffer (GL_ARRAY_BUFFER, vbo);
47+ // copy the data to the VBO
48+ glBufferData (GL_ARRAY_BUFFER, data.size () * sizeof (data[0 ]), data.data (), GL_STATIC_DRAW);
49+
50+ // specify the layout of the vertex attribute
51+ glVertexAttribPointer (m_vboCount, dim, GL_FLOAT, GL_FALSE, 0 , (GLvoid*) 0 );
52+ // enable the vertex attribute
53+ glEnableVertexAttribArray (m_vboCount++);
54+
55+ // store the VBO in the list of buffers
56+ m_buffers.push_back (vbo);
57+ }
0 commit comments