0
\$\begingroup\$

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

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

I found the answer in a related question

The problem was my depth buffer. As I was using a spritebuffer in my main draw

spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);

Some GraphicsDevice properties were incorrect. BlendState value was AlphaBlend and DepthStencilState was none.

In order to fix this only have to add two lines at the beginning of ServerManager draw method:

        graphicsDevice.BlendState = BlendState.Opaque;
        graphicsDevice.DepthStencilState = DepthStencilState.Default;

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

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .