I have to create a 2d menu, over a 3d model, using XNA. Right now, I have created spritebatch for 2D, and a 3d model. But, as i have noticed, and was mentioned in other places, model is not being displayed properly, because of z buffer issue. According to the tutorial, I am supposed to enable the DepthBuffer again, in the draw method. But, somehow, when I use:
GraphicsDevice.DepthStencilState.DepthBufferEnable = true;
code throws an error during debugging, saying,
Cannot change read-only DepthStencilState. State objects become read-only the first time they are bound to a GraphicsDevice. To change property values, create a new DepthStencilState instance.
Now, I have tried to create a new instance of DepthStencilState too, but, even that doesn't seem to work. I get the same error always, even though, the help document suggests its a Read/Write value.
Kindly help me figure out how to get the 3D model displayed properly.
Here is the Draw code for reference.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);
foreach (ModelMesh mesh in myModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
//effect.DirectionalLight0.Enabled = true;
//effect.DirectionalLight0.DiffuseColor = Color.AntiqueWhite.ToVector3();
//effect.DirectionalLight0.Direction = new Vector3(0, 0, 0);
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(myModelRotation) * Matrix.CreateTranslation(myModelPosition);
effect.View = Matrix.CreateLookAt(new Vector3(0, 0, 3000), Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45f),
GraphicsDevice.Viewport.AspectRatio, 1, 5000);
}
mesh.Draw();
}
spriteBatch.Begin();
spriteBatch.Draw(myTexture, new Vector2(0, 0), Color.White);
spriteBatch.End();
DepthStencilState d = new DepthStencilState();
d.DepthBufferEnable = true;
GraphicsDevice.DepthStencilState = d;
base.Draw(gameTime);
}