PDA

View Full Version : Drawing multiple objects.



ajith.mk
12-30-2015, 11:24 PM
Hi everyone,

I have 7 objects. Since they are not going to move in my world, I packed all of them in a buffer. But each of the objects have their own position and orientation, means each have their own model matrix. The projection and view matrix are same. Now how do I draw these? Should I maintain array of seven model matrices? If so, then how do I find to which object the current vertex belongs. If I make the model matrix also a vertex attribute then I want to advance it per object not per vertex.

ajith.mk
12-31-2015, 12:16 AM
Meanwhile I found this thread "What is best practice for batch drawing objects with different transformations?" Pretty summed up my problem. So I need to use seven draw calls. Just a question though. Would it be a bad idea if I combine all those objects into a single model(all assembled in proper orientations in 3D application) and export it to opengl. But then I suppose UV layouts become complicated.

GClements
12-31-2015, 05:49 AM
Meanwhile I found this thread "What is best practice for batch drawing objects with different transformations?" Pretty summed up my problem. So I need to use seven draw calls.
You only need to use multiple draw calls if none of the other options are practical.

If the only state which needs to be changed between objects is uniform variables, you can use an array of uniforms with the index stored as a vertex attribute.



Just a question though. Would it be a bad idea if I combine all those objects into a single model(all assembled in proper orientations in 3D application) and export it to opengl. But then I suppose UV layouts become complicated.
If the objects' transformations are constant relative to each other, you can apply the transformations to the vertex positions before passing them to OpenGL, e.g. at load time.

Combining objects doesn't make UV layouts "complicated"; you just need to allocate a rectangular portion of the texture space to each one. Array textures can be used to limit the size of the texture, prevent distinct sub-textures from bleeding into each other for the lower-resolution mipmap levels, and to allow the use of repeat modes. However, this does require that the individual textures have the same basic properties (number of channels, precision, sRGB versus linear, etc) and will use the same filter and repeat modes.

ajith.mk
12-31-2015, 08:07 PM
Thank you very much, Using uniform array to store matrices did not occur to me.

Btw, I am not sure if this should be a separate thread but I will try here anyway. I have those 7 draw commands. If I issue them individually they draw their corresponding model. But if I combine them, I mean if I issue them one after the other, I get garbage displayed. Sorry if this is a noob question, should I clear depth buffer once per scene or between each draw command. Draw commands are fine by themselves since they display my models. Its only if I combine them they draw nonsense.

GClements
01-01-2016, 02:46 AM
should I clear depth buffer once per scene or between each draw command.

Once per scene. The point of a depth buffer is so that farther objects don't get drawn over closer objects, regardless of the order in which they're drawn. That requires that its contents are preserved between drawing the different objects.