Correct way to select drawing order

I have a scene containing primitives created by the user (mostly cubes and spheres with custom colors and sizes).

Now, if I draw them all in the order they appeared, obviously I get problems, like a cube added later behind another one showing up on top of it even though it should not be visible without moving the camera.

If I check for their location on the z axis, that would make everything better, but it would still look bad if two cubes with different rotations were on top of each other. Instead of seeing part of both.

The “expected” result would be something like that

But it wouldn’t work with this technique.

So what is the recommended way to select the order of drawing in order to get a scene as realistic as possible? There’s probably more to it than just the order, so I’d like to know how should those primitive be drawn to obtain a realistic result?

Hi @Kaito_Kid,

I think what you’re trying to do is easiest achieved by enabling the depth test.

ofEnableDepthTest() //... draw your primitives (it doesn't matter in which order) // cube1.draw(); // icosahedron.draw(); // ... ofDisableDepthTest();

Depth testing (or z-testing, or z-buffering, as it’s also called) is very useful for a lot of things, especially when rendering realistically. In short, it makes sure that nothing is drawn to the screen if something has already been drawn which was closer to the camera.

The wikipedia article will probably explain it better than me =)

This is exactly what I was looking for, thank you very much!