Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
In all honesty the screenshot isn't showing a great deal cos of the colour of the white part. A wireframe screenshot would probably do it better justice. –  Ray Dey Feb 13 '11 at 2:07
    
Ok I will change it thank you for pointing it out –  Jesse Emond Feb 13 '11 at 2:08
    
Well when I put the wireframe it just looks like there is no problem... mmm I really don't know how to show it properly... –  Jesse Emond Feb 13 '11 at 2:10
    
What about some basic lighting? That might give it a better sense of depth. –  Ray Dey Feb 13 '11 at 2:17
    
When I enable the default lighting on the effect used when drawing the terrain, the terrain becomes really really dark and I can't hardly see the brightest parts... I guess I need to define the normals of the triangles for the lighting to work properly? I'm really new to 3D sorry... –  Jesse Emond Feb 13 '11 at 2:38
show 5 more comments

2 Answers

up vote 4 down vote accepted

Sprite batches tend to overwrite some render states. Try to set device.RenderState.DepthBufferWriteEnable = true; and device.RenderState.DepthBufferEnable; manually. Hope that helps.

share|improve this answer
    
Plus what Steve H says. –  Dave O. Feb 13 '11 at 3:42
    
Problem solved! Thank you very much. :) That's the Z-Buffer right? I think I've read about it before but I forgot to use it. –  Jesse Emond Feb 13 '11 at 3:46
add comment

You should end your spriteBatch before doing any 3d drawing. Always keep 2d and 3d drawing separate.

By the way, why are you even beginning & ending the spritebatch if you're not drawing any 2d?

Within a spritbatch begin/end, XNA sets various renderstates for 2d drawing. Often these aren't best for 3d drawing... especially if you're using XNA 3.1. In 3.1, SpriteBatch.Begin disables the depthbuffer... which is needed for 3d drawing and could factor into your issue.

Also, if the problem turns out to be what's known as Z-fighting (it is hard to tell from the screen shot), increase your nearclip value when setting up your camera's projection matrix.

share|improve this answer
    
The spriteBatch thing was a stupid mistake from me. Thanks for pointing it out! :) –  Jesse Emond Feb 13 '11 at 3:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.