I've recently started learning how to program in 3D with XNA and I've been trying to implement a Terrain3D class(a very simple height map). I've managed to draw a simple terrain, but I'm getting a weird bug where I can see through the terrain. This bug happens when I'm looking through a hill from the map. Here is a picture of what happens:
I was wondering if this is a common mistake for starters and if any of you ever experienced the same problem and could tell me what I'm doing wrong. If it's not such an obvious problem, here is my Draw method:
public override void Draw()
{
Parent.Engine.SpriteBatch.Begin(SpriteBlendMode.None,
SpriteSortMode.Immediate, SaveStateMode.SaveState);
Camera3D cam = (Camera3D)Parent.Engine.Services.GetService(typeof(Camera3D));
if (cam == null)
throw new Exception("Camera3D couldn't be found. Drawing a 3D terrain requires a 3D camera.");
float triangleCount = indices.Length / 3f;
basicEffect.Begin();
basicEffect.World = worldMatrix;
basicEffect.View = cam.ViewMatrix;
basicEffect.Projection = cam.ProjectionMatrix;
basicEffect.VertexColorEnabled = true;
Parent.Engine.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
Parent.Engine.GraphicsDevice,
VertexPositionColor.VertexElements);
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
{
pass.Begin();
Parent.Engine.GraphicsDevice.Vertices[0].SetSource(vertexBuffer, 0,
VertexPositionColor.SizeInBytes);
Parent.Engine.GraphicsDevice.Indices = indexBuffer;
Parent.Engine.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
0, 0, vertices.Length, 0, (int)triangleCount);
pass.End();
}
basicEffect.End();
Parent.Engine.SpriteBatch.End();
}
Parent is just a property holding the screen that the component belongs to. Engine is a property of that parent screen holding the engine that it belongs to.
If I should post more code(like the initialization code), then just leave a comment and I will.