glDrawElements .. How to draw multiple objects?

Hi,

Am kinda new to OpenGL 3.0/GLSL 1.5 . So the new thing that they introduced in GLSL 1.5 was that we now have to specify Vertex attributes (gl_Vertex) and normals (gl_Normal) explicitly using glVertexAttribPointer. So i am able to draw one object using this piece of code in my display method.

[b][i]glEnableVertexAttribArray(g_vertices);
glVertexAttribPointer(g_vertices, 4, GL_FLOAT, GL_FALSE, 0, cube.getVertices());

glEnableVertexAttribArray(g_normals);
glVertexAttribPointer(g_normals, 3, GL_FLOAT, GL_FALSE, 0, cube.getNormals());

glDrawElements(GL_TRIANGLES, shape.getNumberIndices(), GL_UNSIGNED_INT, cube.getIndices()[/b]);
[/i]

but when i try to use the same sequence of commands exactly after these commands to draw another object. I get an error accessing location (blah blah blah).

Is there way to draw multiple objects (having their own vertex, normal and index data) and using the same shader???

Help please.

Thanks.

You should be able to with no problem, unless there’s a bug in your code of course.

Try issuing the exact same draw call on the exact same cube:
glDrawElements(GL_TRIANGLES, shape.getNumberIndices(), GL_UNSIGNED_INT, cube.getIndices());
repeatedly. Put it in a loop and do it 100 times! See if that’s stable.

Then investigate drawing other cubes with separate vertex attributes and draw calls.

And once you figure out your memory bug, realize that you can combine the vertex attribute data and index data for “multiple” cubes in the same set of vertex attribute arrays and index lists for better performance. Then you can just set the vertex attribute arrays once, …and then issue one big draw call for all of them, or seperate draw calls only the specific cubes that you want.

[quote]Is there way to draw multiple objects (having their own vertex, normal and index data) and using the same shader???

Barring memory errors on your part, it’ll work no problems. We all do this all the time!

Grab a memory debugging tool such as valgrind.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.