I would like to present here my Question about VBOs and VAOs. Basically from what i understand you can have different VAOs each of which can contain up to several VBOs , and by binding to the VAO's ID you can draw from the VBOs stored inside.
I have Wrote a simple class that reads a obj file then creates vertices, normals and uvs vector lists and upload those in 3 VBOs (vboID 1,2 and 3)
void Model::loadToVBO()
{
glGenVertexArrays(1, &vaoID);
glBindVertexArray(this->vaoID);
glBindBuffer(GL_ARRAY_BUFFER, vertexID);
glBufferData(GL_ARRAY_BUFFER, this->vertices.size()*sizeof(vec3), &this->vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, normalID);
glBufferData(GL_ARRAY_BUFFER, this->normals.size()*sizeof(vec3), &this->normals[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, uvcoordID);
glBufferData(GL_ARRAY_BUFFER, this->uvs.size()*sizeof(vec2), &this->uvs[0], GL_STATIC_DRAW);
glBindVertexArray(0);
}
if I do that in the render function i try to Bind to vaoID , and draw elements , but it would not work. To make it work i have to RELOAD vbo data every frame in Render Function (that happens because i have 2 models each model's constructor calls loadVBO , and obviously the second model overrides the first vbo's 1,2 and 3 ID positions(i was assuming that the vao generated from the first object will be entirely separate from the vao generated from the second model and each vao will have its own 3 VBOs). So maybe my code doesnt setup VAOs properly. Was reading something about how glEnableVertexAttribArray and glVertexAttribPointer are used to set up current vbo state into vao , but could not understand it really well. Maybe because i use the same functions to bind to shaders variables ....
This code below is in the shader class and it loads the vbo data into shader's variables
void StaticShader::loadAttribute(GLuint bufferID, string variable, GLuint size)
{
GLuint variableID;
variableID = glGetAttribLocation(this->shaderProgramID, variable.c_str());
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glEnableVertexAttribArray(variableID);
glVertexAttribPointer(variableID, size, GL_FLOAT, GL_FALSE, 0, 0);
}
The Model Render Functions looks like that. The loadToShader function loads: all model matrices + vertices + normals + uvs This function calls the function loadAttribute for v, n and uvs ( i assume thats not where its supposed to be ... maybe)
void Model::Render()
{
glBindVertexArray(vaoID);
//loadToVBO();
loadToShader();
glDrawArrays(GL_TRIANGLES, 0, this->vertices.size());
glBindVertexArray(0);
}
My question is - Is it Bad (performance wise) to load VBO data every time render is called (i assume YES). Second part - Should i reload model data(meaning v/n/uvs) in shader every time its called to be rendered ?