Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
1  
Show us your drawing code. It seems you're somehow not using the Z-buffer. –  David Gouveia Dec 15 '11 at 3:45
    
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 –  Mr Bell Dec 15 '11 at 4:12
    
If you put your comment in the form of an answer, I'll mark it as correct –  Mr Bell Dec 15 '11 at 4:15
    
Glad you found the problem so quickly. I was about to post a minimal working sample for you to compare with yours. –  David Gouveia Dec 15 '11 at 4:19

1 Answer 1

up vote 5 down vote accepted

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:

http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx

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.

share|improve this answer
    
I am constantly forgetting that those sprite batches screw with render states. Thanks for pointing it out –  Mr Bell Dec 15 '11 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.