Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to render terrain, everything compiles fine, but I can't see anything on screen. ( I'm moving camera around in case it was on the other side)

Can't figure out whats wrong.

public class Terrain
        public VertexBuffer vertexBuffer;
        public IndexBuffer indexBuffer;
        public VertexPositionNormalTexture[] vertices;
        public int[] indices;
        //int gridSize;

        Camera camera;
        ContentManager Content;
        GraphicsDevice device;

        Texture2D heightmap;
        public Texture2D grass;

        Effect effect;

        public Terrain(ContentManager Content, GraphicsDevice device, Camera camera)
            this.Content = Content;
            this.device = device;
   = camera;

            heightmap = Content.Load<Texture2D>("Gfx//heightmap");
            grass = Content.Load<Texture2D>("Gfx//grass");

            effect = Content.Load<Effect>("Effects//terrain");


        public void Draw()
            effect.CurrentTechnique = effect.Techniques["Texture1"];
            effect.Parameters["xWorldViewProjection"].SetValue(camera.worldMatrix * camera.viewMatrix * camera.projMatrix);

            foreach (EffectPass pass in effect.CurrentTechnique.Passes)

                device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);

        private void CreateVertices()
            vertices = new VertexPositionNormalTexture[heightmap.Width * heightmap.Height];
            for (int x = 0; x < heightmap.Width; x++)
                for (int y = 0; y < heightmap.Height; y++)
                    vertices[y * heightmap.Width + x].Position = new Vector3(x, 0, y);
                    vertices[y * heightmap.Width + x].Normal = new Vector3(0, 1, 0);
                    vertices[y * heightmap.Width + x].TextureCoordinate = new Vector2(y, x);

        private void CreateIndices()
            indices = new int[(heightmap.Width - 1) * (heightmap.Height - 1) * 6];
            int counter = 0;
            for (int x = 0; x < heightmap.Width - 1; x++)
                for (int y = 0; y < heightmap.Height - 1; y++)
                    int lowerLeft = x + y * heightmap.Width;
                    int lowerRight = (x + 1) + y * heightmap.Width;
                    int topLeft = x + (y + 1) * heightmap.Width;
                    int topRight = (x + 1) + (y + 1) * heightmap.Width;

                    indices[counter++] = topLeft;
                    indices[counter++] = lowerRight;
                    indices[counter++] = lowerLeft;

                    indices[counter++] = topLeft;
                    indices[counter++] = topRight;
                    indices[counter++] = lowerRight;
share|improve this question

closed as off-topic by Josh Petrie Dec 18 '14 at 17:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

Start with a working example and expand on that. Run frequently to know if any changes affect your output. And run the debugger to find out what values are set to, and see where things are going wrong. – Byte56 May 1 '12 at 19:08
Try reversing the winding of your terrain vertices. Try turning off culling. Try setting the r, g, b, and a values of the terrain pixel shader to 1 (you should see black terrain). Inspect your final terrain index and vertex buffers to see if they look like what you would expect. Compile and run between any and all changes like these. – Nic Foster May 2 '12 at 1:16
up vote 0 down vote accepted
  • Do you have lighting enabled ? If so, does the terrain have correct normals otherwise it might result in an opaque black as the color
  • What was the last thing you changed, change it back see if it works, if so figure out whats wrong with the snippet / algorithm
  • Are you setting the texture correctly or are you using opaque black as some sort of default color

Otherwise you might want to check errors after rendering with your index buffer, it might well be that the indices are incorrect.

share|improve this answer

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