Meshes not rendering correctly

Started by
2 comments, last by gdevmon 7 years, 5 months ago

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.

Error 404: Signature not found.

Advertisement

Where and how do you send your texture information (unit, id...) to OpenGL ?

Make sure the second program is compiled and linked correctly.There is tutorials on how to get compile errors.

Solved it. i wasnt passing in the uniform variable to the shader in the mesh class and instead was passing them in the main render method using only 1 of the shaders.

like this:


GLuint viewid = glGetUniformLocation(shader1.getID(), "view");
GLuint projid = glGetUniformLocation(shader1.getID(), "proj");

glUniformMatrix4fv(viewid, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projid, 1, GL_FALSE, glm::value_ptr(proj));

glUseProgram(shader1);
cone.draw(shader1);
glUseProgram(shader2);
cone.draw(shader2);

Thus the second glUseProgram() uses that glUniformMatrix4fv call but not the first and creating conflict.

There was shader1 and shader2. shader1 outputs textures and shader2 outputs plain color.

Error 404: Signature not found.

This topic is closed to new replies.

Advertisement