I have been trying to render multiple meshes using one shader for a few days and kinda got it right now (i hope).
But i came across a new problem that whenever i use a different program(shader) to render a different mesh, somehow the meshes before the next(or second) glUseProgram(id) are not being drawn. One of the shaders outputs texture while the other just outputs plain color.
Here is the mesh loading from .obj for the 2 different shaders (i am using .obj with only one object in it):
glGenVertexArrays(1, &this->vao);
glGenBuffers(1, &this->vbo);
glGenBuffers(1, &this->ebo);
WFOLoader a;
a.load(name);
this->indices = a.indices.size();
glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glBufferData(GL_ARRAY_BUFFER, 5 * sizeof(GLfloat) * a.vertices.size(), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0 , 3 * sizeof(GLfloat) * a.vertices.size(), a.vertices.data());
glBufferSubData(GL_ARRAY_BUFFER, 3 * sizeof(GLfloat) * a.vertices.size(), 2 * sizeof(GLfloat) * a.vertices.size(), a.textures.data());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLfloat) * a.indices.size(), a.indices.data(), GL_STATIC_DRAW);
GLuint vertPos = glGetAttribLocation(shader.getProgramID(), "pos");
GLuint vertTex = glGetAttribLocation(shader.getProgramID(), "tex");
glVertexAttribPointer(vertPos, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glVertexAttribPointer(vertTex, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(3 * sizeof(GLfloat) * a.vertices.size()));
glEnableVertexAttribArray(vertPos);
glEnableVertexAttribArray(vertTex);
glBindVertexArray(0);
and the other one:
glGenVertexArrays(1, &this->vao);
glGenBuffers(1, &this->vbo);
glGenBuffers(1, &this->ebo);
WFOLoader a;
a.load(path);
this->indices = a.indices.size();
glBindVertexArray(this->vao);
glBindBuffer(GL_ARRAY_BUFFER, this->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * a.vertices.size(), a.vertices.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLfloat) * a.indices.size(), a.indices.data(), GL_STATIC_DRAW);
GLuint vertPos = glGetAttribLocation(shader.getProgramID(), "pos");
glVertexAttribPointer(vertPos, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(vertPos);
glBindVertexArray(0);
and both of the draw method are same:
glUseProgram(shader.getProgramID());
glBindVertexArray(this->vao);
GLuint modelid = glGetUniformLocation(shader.getProgramID(), "model");
glUniformMatrix4fv(modelid, 1, GL_FALSE, glm::value_ptr(model));
glDrawElements(GL_TRIANGLES, this->indices, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
I wonder if the way im registering the vertex and texture data are correct to begin with. (Although, considering it is rendering multiple meshes with 1 shader correctly, i guess it is ok). Also i tried commenting out the glUseProgram(shader.getProgramID()); from both the mesh class and call glUseProgram(id) in the main rendering method right before the meshes i wanted to draw. Any help? Sorry if my Engrish was horrible and unclear. And i am still learning OpenGL.