# XNA, how to draw two cubes standing in line parallelly?

I just got a problem with drawing two 3D cubes standing in line. In my code, I made a cube class, and in the game1 class, I built two cubes, A on the right side, B on the left side. I also setup an FPS camera in the 3D world. The problem is if I draw cube B first(Blue), and move the camera to the left side to cube B, A(Red) is still standing in front of B, which is apparently wrong.

I guess some pics can make much sense. Then, I move the camera to the other side, the situation is like: This is wrong.... From this view, the red cube, A should be behind the blue one, B....

Could somebody give me help please?

This is the draw in the Cube class

Matrix center = Matrix.CreateTranslation(
new Vector3(-0.5f, -0.5f, -0.5f));
Matrix scale = Matrix.CreateScale(0.5f);
Matrix translate = Matrix.CreateTranslation(location);
effect.World = center * scale * translate;
effect.View = camera.View;
effect.Projection = camera.Projection;

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.SetVertexBuffer(cubeBuffer);

RasterizerState rs = new RasterizerState();
rs.CullMode = CullMode.None;
rs.FillMode = FillMode.Solid;
device.RasterizerState = rs;

device.DrawPrimitives(
PrimitiveType.TriangleList,
0,
cubeBuffer.VertexCount / 3);
}


This is the Draw method in game1

 B.Draw(camera, effect);
A.Draw(camera, effect);


**

• Can you post your drawing code, so we can see what you are doing? – Seta May 29 '14 at 10:08
• Sure, I have updated my question and add my draw function – user3535716 May 29 '14 at 10:16

It seems to me that you don't have any depth testing going on. For that you have to have a depth buffer and write to that while drawing your geometry.
If you are drawing to a rendertarget then create it like this:

RenderTarget2D rt = new RenderTarget2D(device, device.Viewport.Width, device.Viewport.Height, false, SurfaceFormat.Color, DepthFormat.Depth24);


If you are drawing directly to the backbuffer you already have a depth texture bound to it.

In your drawing code you need to clear the depth too (well it's optional but it should be cleared to avoid some artifacts, also this doesn't cost you much to do so) while clearing the backbuffer (or rendertarget) color:

 device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);
//device is your GraphicsDevice


You also have to turn on depth testing before drawing your geometry, you can do it like this:

 device.DepthStencilState = DepthStencilState.Default;
//device is your GraphicsDevice

• While practically device.DepthStencilState = DepthStencilState.Default; does enable depth testing, technically the way to enable depth testing is with device.DepthStencilState.DepthBufferEnable = true;. DepthStencilState.Default is a depth stencil state in which it depth testing is already enabled, so setting it will (among other things) enable depth testing. – Panda Pajama May 29 '14 at 11:57
• You are right, I will update. – János Turánszki May 29 '14 at 13:09
• Actually he's wrong. You cannot modify a state object once it has been set onto a device. Nor can you modify the default states. You'd need to create a custom DepthStencilState object, if you wanted something other than Default. Source. Suggest you edit it back. – Andrew Russell May 29 '14 at 13:25
• Thanks, truth is, I never tried Panda Pajama's suggestion, just quickly took a look at it through the Intellisense of Visual Studio. :( – János Turánszki May 29 '14 at 13:28
• Thanks for all your replies. However, I'm just starting with XNA. I just googled depth test. Now I know what it is, and what my problem is... Honestly, I still dont really know how to use depth test in my codes. Could you give me an example like a Draw function with the depth test? – user3535716 May 30 '14 at 2:31