Why distant objects draw in front of close objects?

Posted by cad on Game Development See other posts from Game Development or by cad
Published on 2012-10-21T10:29:38Z Indexed on 2012/10/21 17:20 UTC
Read the original article Hit count: 201

Filed under:
|

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)

Capture from certain angle

Here is what I see from behind

Opposite angle

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?)

© Game Development or respective owner

Related posts about xna-4.0

Related posts about depth-buffer