Jump to content
  • Advertisement
Sign in to follow this  
gdevmon

OpenGL Meshes not rendering correctly

This topic is 614 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by gdevmon

Share this post


Link to post
Share on other sites
Advertisement

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!