The last few days we worked on our voxel engine. We get to some depth rendering problems if we draw our cubes. See following Youtube-Video: http://youtu.be/lNDAqO7yHBQ
We already searched along this problem and found different approaches but none of them solved our problem.
- GraphicsDevice.Clear(ClearOptions.DepthBuffer | ClearOptions.Target, Color.CornflowerBlue, 1.0f, 0);
- GraphicsDevice.BlendState = BlendState.Opaque;
- GraphicsDevice.DepthStencilState = DepthStencilState.Default;
- GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
Our LoadContent()
Method:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
_spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
_effect = new BasicEffect(GraphicsDevice);
_vertexBuffer = new VertexBuffer(GraphicsDevice, Node.VertexPositionColorNormal.VertexDeclaration, _chunkManager.Vertices.Length, BufferUsage.WriteOnly);
_vertexBuffer.SetData(_chunkManager.Vertices); // copies the data from our local vertices array into the memory on our graphics card
_indexBuffer = new IndexBuffer(GraphicsDevice, typeof(int), _chunkManager.Indices.Length, BufferUsage.WriteOnly);
_indexBuffer.SetData(_chunkManager.Indices);
}
Our Draw()
Method:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.RasterizerState = RasterizerState.CullClockwise;
// Set object and camera info
//_effect.World = Matrix.Identity;
_effect.View = _camera.View;
_effect.Projection = _camera.Projection;
_effect.VertexColorEnabled = true;
_effect.EnableDefaultLighting();
// Begin effect and draw for each pass
foreach (var pass in _effect.CurrentTechnique.Passes)
{
pass.Apply();
GraphicsDevice.SetVertexBuffer(_vertexBuffer);
GraphicsDevice.Indices = _indexBuffer;
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _chunkManager.Vertices.Count(), 0, _chunkManager.Indices.Count() / 3);
}
base.Draw(gameTime);
}
Our View and Projection setup:
Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)Game.Window.ClientBounds.Width / Game.Window.ClientBounds.Height, 1, 500);
View = Matrix.CreateLookAt(CameraPosition, CameraPosition + _cameraDirection, _cameraUp);
We use the Camera (http://www.filedropper.com/camera_1) from Aaron Reed's book (http://shop.oreilly.com/product/0636920013709.do).
Did you see something we missed? Or do you have an idea to solve this problem?