Skip to content

Commit

Permalink
Commits some code to carefully bind, unbind and rebind GL buffers eve…
Browse files Browse the repository at this point in the history
…ry time the vertex model is changed.
  • Loading branch information
Seb James committed Sep 19, 2020
1 parent 072050e commit 6411246
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
12 changes: 12 additions & 0 deletions morph/VisualDataModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,23 @@ namespace morph {
this->vertexColors.clear();
this->initializeVertices();
// Now re-set up the VBOs
#ifdef CAREFULLY_UNBIND_AND_REBIND // Experimenting with better buffer binding.
glBindVertexArray (this->vao);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vbos[idxVBO]);
#endif
int sz = this->indices.size() * sizeof(VBOint);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sz, this->indices.data(), GL_STATIC_DRAW);
this->setupVBO (this->vbos[posnVBO], this->vertexPositions, posnLoc);
this->setupVBO (this->vbos[normVBO], this->vertexNormals, normLoc);
this->setupVBO (this->vbos[colVBO], this->vertexColors, colLoc);
#ifdef CAREFULLY_UNBIND_AND_REBIND
glBindVertexArray(0);
glBindBuffer (0, this->vbos[posnVBO]);
glBindBuffer (0, this->vbos[normVBO]);
glBindBuffer (0, this->vbos[colVBO]);
glBindBuffer (0, this->vbos[idxVBO]);
#endif
}

//! All data models use a a colour map. Change the type/hue of this colour map
Expand Down
23 changes: 19 additions & 4 deletions morph/VisualModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
typedef GLuint VBOint;
#define VBO_ENUM_TYPE GL_UNSIGNED_INT

// Switches on some changes where I carefully unbind gl buffers after calling
// glBufferData() and rebind when changing the vertex model. Makes no difference on my
// Macbook Air, but should be more correct. Dotting my 'i's and 't's
#define CAREFULLY_UNBIND_AND_REBIND 1

namespace morph {

//! Forward declaration of a Visual class
Expand Down Expand Up @@ -96,26 +101,35 @@ namespace morph {
#endif
glBindVertexArray (this->vao);

// Allocate/create the vertex buffer objects
// Create the vertex buffer objects
this->vbos = new GLuint[numVBO];
#ifdef __MACS_HAD_OPENGL_450__
glCreateBuffers (numVBO, this->vbos); // OpenGL 4.5 only
#else
glGenBuffers (numVBO, this->vbos); // OpenGL 4.4- safe
#endif
// Set up the indices buffer
// Set up the indices buffer - bind and buffer the data in this->indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->vbos[idxVBO]);
//std::cout << "indices.size(): " << this->indices.size() << std::endl;
int sz = this->indices.size() * sizeof(VBOint);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sz, this->indices.data(), GL_STATIC_DRAW);

// Binds data from the "C++ world" to the OpenGL shader world for
// "position", "normalin" and "color"
// (bind, buffer and set vertex array object attribute)
this->setupVBO (this->vbos[posnVBO], this->vertexPositions, posnLoc);
this->setupVBO (this->vbos[normVBO], this->vertexNormals, normLoc);
this->setupVBO (this->vbos[colVBO], this->vertexColors, colLoc);

// Possibly release (unbind) the vertex buffers (but not index buffer)
#ifdef CAREFULLY_UNBIND_AND_REBIND
// Possibly release (unbind) the vertex buffers, but have to unbind vertex
// array object first.
glBindVertexArray(0);
glBindBuffer (0, this->vbos[posnVBO]);
glBindBuffer (0, this->vbos[normVBO]);
glBindBuffer (0, this->vbos[colVBO]);
glBindBuffer (0, this->vbos[idxVBO]);
#endif
// Possible glVertexAttribPointer and glEnableVertexAttribArray?
glUseProgram (this->shaderprog);
}
Expand All @@ -126,6 +140,7 @@ namespace morph {
//! Render the VisualModel
void render (void)
{
// It is only necessary to bind the vertex array object before rendering
glBindVertexArray (this->vao);
glDrawElements (GL_TRIANGLES, this->indices.size(), VBO_ENUM_TYPE, 0);
glBindVertexArray(0);
Expand Down Expand Up @@ -208,7 +223,7 @@ namespace morph {
std::copy (vec.begin(), vec.end(), std::back_inserter (vp));
}

//! Set up a vertex buffer object
//! Set up a vertex buffer object - bind, buffer and set vertex array object attribute
void setupVBO (GLuint& buf, std::vector<float>& dat, unsigned int bufferAttribPosition)
{
int sz = dat.size() * sizeof(float);
Expand Down

0 comments on commit 6411246

Please sign in to comment.