I have got simple 1x1x1 cube model. I draw 3 of them. They are located at:

1,0,0 2,0,0 3,0,0

I draw them in the same order every time. When I move the camera and look at them from one end everything is fine. If I look at them from the opposite end I can see parts of the cubes that should be occluded by the cubes.

How can I have them draw properly?Cubes being drawn out of order

  • 1
    \$\begingroup\$ Show us your drawing code. It seems you're somehow not using the Z-buffer. \$\endgroup\$ Dec 15, 2011 at 3:45
  • \$\begingroup\$ You were right. I have a frame rate component up there using a sprite batch which seems to be turning it off. Removing that component has everything working fine \$\endgroup\$
    – Mr Bell
    Dec 15, 2011 at 4:12
  • \$\begingroup\$ If you put your comment in the form of an answer, I'll mark it as correct \$\endgroup\$
    – Mr Bell
    Dec 15, 2011 at 4:15
  • \$\begingroup\$ Glad you found the problem so quickly. I was about to post a minimal working sample for you to compare with yours. \$\endgroup\$ Dec 15, 2011 at 4:19

1 Answer 1


The reason why your draw order is messed up is probably because your depth buffer is not being used. With the depth buffer enabled the order in which you draw your meshes makes no difference.

And since you've just stated that you were using a SpriteBatch component, it pretty much confirms that this is the problem:


Quoting that article:

Before you draw anything in 3D you will probably want to reset these states:

GraphicsDevice.BlendState = BlendState.Opaque;

GraphicsDevice.DepthStencilState = DepthStencilState.Default;

Depending on your 3D content, you may also want to set:

GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;

In this case, the culprit is GraphicsDevice.DepthStencilState being set to None instead of Default.

  • \$\begingroup\$ I am constantly forgetting that those sprite batches screw with render states. Thanks for pointing it out \$\endgroup\$
    – Mr Bell
    Dec 15, 2011 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .