Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't render more than one mesh #11

Open
quinn opened this issue Aug 23, 2017 · 3 comments
Open

can't render more than one mesh #11

quinn opened this issue Aug 23, 2017 · 3 comments

Comments

@quinn
Copy link

quinn commented Aug 23, 2017

Hi there, your tutorial is excellent, thank you for putting so much work into this. My question is about the Mesh class, I am unable to get it to render multiple meshes. As far as I can tell this is due to the m_vertexArrayObject allocating the same ID for the mesh. Also, i had to make some changes to make this make more sense. For example, I changed m_vertexArrayObject from a GLuint to an array of length one:

enum {
	SINGLE_VAO,
	NUM_VAO
};

GLuint m_vertexArrayObject[NUM_VAO];

glGenVertexArrays(NUM_VAO, m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject[SINGLE_VAO]);

Secondly, the second argument to glGenVertexArrays is no longer passed as a pointer. Again, this is based on examples I found elsewhere. The problem I am facing is that m_vertexArrayObject[SINGLE_VAO] is always zero (i assume to that being the default value of the array), so (as far as i know) this means I can never have more than one mesh.

Thanks for your help with this!

@lukawarren
Copy link

Sorry to bring up an old thread, but I'm having the same issue regarding multiple meshes. Did you ever manage to fix it?

@quinn
Copy link
Author

quinn commented Jan 29, 2018

nope. was stumped by this one and gave up learning OpenGL. if you crack it let me know the solution :)

@lukawarren
Copy link

Wow how time flies...

I randomly found this old project collecting dust on my hard drive today and remembered getting stuck all those years ago before I'd properly toyed with OpenGL. Facepalmed as soon as I had a look.. turns out the reason I was only seeing "one mesh" was because they both shared the same transform, so completely overlapped :)

For anyone stumbling upon this years later, you'll want each mesh to have its own transform, then update the shader uniforms accordingly:

shader.Bind();
shader.Update(transforms[0], camera);
meshes[0].Draw();

shader.Update(transforms[1], camera);
meshes[1].Draw();

// etc, etc..

Ideally you'd make some sort of "entity" class or struct to make life easier:

struct Entity
{
    Mesh* mesh; // pointer so entities can share meshes
    Transform transform;
}

std::vector<Entity> entities;

// ...

shader.Bind();
for (const auto& entity: entities)
{
    shader.Update(entity.transform, camera);
    entity.mesh->Draw();
}

quinn's issue may be something else, but at least for me it was just a case of "over-debugging"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants