Sunday, April 1, 2018

xna 4.0 - XNA objects not occluding properly


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



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...