Hi everyone,
I have code in a function called updatebuffers() that creates VAOs for me and vbos and sets the appropriate bindings I want. This function sets my global GLuint* vaos variable and generates the VAOs into it then binds them and sets up other bindings.
However, in my draw function when I try to bind vaos[0] to draw my first object, it fails to bind! Nothing else happens to my vaos variable between the code.
What can cause a VAO to not bind? I have found difficulty separating certain OpenGL setup calls into separate functions without breaking code that previously rendered just like this problem.
Thanks for any help with this.
My updatebuffer code to set up my VAOs and VBOs for my geometries:
void UpdateBuffers()
{
std::cout << "
Initializing buffers...
";
// Create a VAO for each geometry
vaos = new GLuint[geometry_count];
glGenVertexArrays(geometry_count, &vaos[0]);
std::cout << "
Creating vaos: " << geometry_count << std::endl;
ExitOnGLError("ERROR: Could not generate VAOS");
// Create 3 VBOs for each geometry
// Position, Normals, Texture Coords
vbos = new GLuint[3 * geometry_count];
glGenBuffers(3 * geometry_count, &vbos[0]);
ExitOnGLError("ERROR: Could not generate VBOS");
// Configure VBOs to hold data
GLuint attrib_location;
int index = 0;
std::cout << "Looping through geometries
";
for(geometries_it = geometries.begin(); geometries_it != geometries.end(); geometries_it++)
{
std::cout << "configuring geometry " << index << " with vao: " << vaos[index] << std::endl;
glBindVertexArray(vaos[index]);
ExitOnGLError("ERROR: Could not bind the VAO for setup purposes");
// Set vertex position data
glBindBuffer(GL_ARRAY_BUFFER, vbos[3 * index]);
glBufferData(
GL_ARRAY_BUFFER,
(*geometries_it).second.vertices.size,
(*geometries_it).second.vertices.data,
GL_STATIC_DRAW);
attrib_location = glGetAttribLocation(current_program, "in_coords");
glVertexAttribPointer(
attrib_location,
(*geometries_it).second.vertices.stride,
(*geometries_it).second.vertices.type,
GL_FALSE,
0,
0);
glEnableVertexAttribArray(0);
// Set normal vector data
glBindBuffer(GL_ARRAY_BUFFER, vbos[3 * index + 1]);
glBufferData(
GL_ARRAY_BUFFER,
(*geometries_it).second.normals.size,
(*geometries_it).second.normals.data,
GL_STATIC_DRAW);
attrib_location = glGetAttribLocation(current_program, "in_normals");
glVertexAttribPointer(
attrib_location,
(*geometries_it).second.normals.stride,
(*geometries_it).second.normals.type,
GL_FALSE,
0,
0);
glEnableVertexAttribArray(1);
// Set texture data
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
And then my render function:
void DrawCursor()
{
std::cout << "Drawing cursor." << std::endl;
int index = 0;
/* Draw all parts of the cursor */
for(geometries_it=geometries.begin() ; geometries_it != geometries.end(); geometries_it++)
{
std::cout << "Drawing: " << (*geometries_it).second.id << std::endl;
//ModelMatrix = (*it).second.model_matrix;
ModelMatrix.identity();
/* Position and rotate the cursor */
/* Rotate cursor. */
matrix_rotate_about_local_axis(ModelMatrix, 0, ((float) cursor_angle[0]));
matrix_rotate_about_local_axis(ModelMatrix, 1, ((float) cursor_angle[1]));
matrix_rotate_about_local_axis(ModelMatrix, 2, ((float) cursor_angle[2]));
/* Position cursor. */
//matrix_set_translation(ModelMatrix, cursor_position[0], cursor_position[1], cursor_position[2]);
glBindVertexArray(vaos[index]);
ExitOnGLError("ERROR: Could not bind the VAO for drawing purposes");
glUniformMatrix4fv(ModelMatrixLoc, 1, GL_FALSE, ModelMatrix.data());
ExitOnGLError("ERROR: Could not set the shader uniforms");
int num_elements = (*geometries_it).second.vert_indices.count;
std::cout << "Number of indices for this object: " << num_elements << std::endl;
glDrawElements(GL_TRIANGLES, num_elements, GL_UNSIGNED_INT, (GLvoid*)0);
ExitOnGLError("ERROR: Could not draw the cube");
glBindVertexArray(0);
glUseProgram(0);
index++;
}
}