0
votes

I'm in the process of trying to figure out how 3D rendering in XNA works, it seems to be very similar to DirectX which makes it fairly straight forward.

But there is one kind of behaviour I'm getting which I didn't expect when drawing with sprite batches.

When I just draw the primitive, a 3D cube, without any sprites I get this result: http://i.stack.imgur.com/DQAHg.png

But when I'm trying to draw it on top of 2D sprites (drawn from SpriteBatch) in the following fashion:

        SpriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        base.Draw(gameTime);
        SpriteBatch.End();

        shape.Draw(gameTime);

THis is the result I get: http://imgur.com/7QZlj

Which is obviously wrong. My first thought was it was rendering using orthographic projection (for some reason) but I'm not sure.

Here's the code I use to draw the cube, it's almost 1:1 from the XNA primitives sample (I haven't started working on a proper 3D framework for the game) so it might be messy

            GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;

            // Create camera matrices, making the object spin.
            float time = (float)gameTime.TotalGameTime.TotalSeconds;

            Vector3 cameraPosition = new Vector3(0, 0, 2.5f);

            float aspect = GraphicsDevice.Viewport.AspectRatio;

            float yaw = 3.05f;
            float pitch = 5.34f;
            float roll = 8.396f;

            Matrix world = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll);
            Matrix view = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
            Matrix projection = Matrix.CreatePerspectiveFieldOfView(1, aspect, 1, 10);

            //  currentPrimitive.Draw(world, view, projection, Color.Red);

            // Reset the fill mode renderstate.
            GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;


            Color color = Color.Red;

            // Set BasicEffect parameters.
            basicEffect.World = world;
            basicEffect.View = view;
            basicEffect.Projection = projection;
            basicEffect.DiffuseColor = color.ToVector3();
            basicEffect.Alpha = 1.0f;// color.A / 255.0f;

            GraphicsDevice device = basicEffect.GraphicsDevice;
            device.DepthStencilState = DepthStencilState.Default;


            GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;

            GraphicsDevice graphicsDevice = basicEffect.GraphicsDevice;

            // Set our vertex declaration, vertex buffer, and index buffer.
            graphicsDevice.SetVertexBuffer(vertexBuffer);

            graphicsDevice.Indices = indexBuffer;


            foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
            {
                effectPass.Apply();

                int primitiveCount = indices.Count / 3;

                graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0,
                                                     vertices.Count, 0, primitiveCount);

            }

I should also clarify, this isn't the result of spritebatch.begin/spritebatch.end, if I just begin/end and don't draw anything the end result is fine but if I draw a font or sprite it breaks the cube.

Anyone have any ideas?

1

1 Answers

1
votes

My advice would be to check the GraphicsDevice settings after you do your SpriteBatch.End() method call. When you use SpriteBatch it does alter some render states in the GraphicsDevice. Probably worth storing the values of the GraphicsDevice before the SpriteBatch.Begin() and then reset them after the SpriteBatch.End() method calls.