Why distant objects draw in front of close objects?
- by cad
I am rendering two cubes in the space using XNA 4.0 and the layering of objects only works from certain angles.
Here is what I see from the front angle (everything ok)
Here is what I see from behind
This is my draw method. Cubes are drawn by serverManager and serverManager1
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
switch (_gameStateFSM.State)
{
case GameFSMState.GameStateFSM.INTROSCREEN:
spriteBatch.Begin();
introscreen.Draw(spriteBatch);
spriteBatch.End();
break;
case GameFSMState.GameStateFSM.GAME:
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
// Text
screenMessagesManager.Draw(spriteBatch, firstPersonCamera.cameraPosition, fpsHelper.framesPerSecond);
// Camera
firstPersonCamera.Draw();
// Servers
serverManager.Draw(GraphicsDevice, firstPersonCamera.viewMatrix, firstPersonCamera.projMatrix);
serverManager1.Draw(GraphicsDevice, firstPersonCamera.viewMatrix, firstPersonCamera.projMatrix);
// Room
//roomManager.Draw(GraphicsDevice, firstPersonCamera.viewMatrix);
spriteBatch.End();
break;
case GameFSMState.GameStateFSM.EXITGAME:
break;
default:
break;
}
base.Draw(gameTime);
fpsHelper.IncrementFrameCounter();
}
serverManager and serverManager1 are instances of the same class ServerManager that draws a cube. The draw method for ServerManager is:
public void Draw(GraphicsDevice graphicsDevice, Matrix viewMatrix, Matrix projectionMatrix)
{
cubeEffect.World = Matrix.CreateTranslation(modelPosition); // Set the World matrix which defines the position of the cube
cubeEffect.View = viewMatrix; // Set the View matrix which defines the camera and what it's looking at
cubeEffect.Projection = projectionMatrix;
// Enable textures on the Cube Effect. this is necessary to texture the model
cubeEffect.TextureEnabled = true;
cubeEffect.Texture = cubeTexture;
// Enable some pretty lights
cubeEffect.EnableDefaultLighting();
// apply the effect and render the cube
foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
{
pass.Apply();
cubeToDraw.RenderToDevice(graphicsDevice);
}
}
Obviously there is something I am doing wrong. Any hint of where to look? (Maybe z-buffer or occlusion tests?)