I'm currently trying to render terrain in an OpenGL application I'm making (using OpenTK in C#). Something really odd is happening and I'm not sure why.

Here is the code I use to load my terrain from a heightmap (Yes I know setting up indices the way I am at the moment is silly, but it's just so I can use the mesh renderer class I made). I render the terrain using Triangle Strips.

    public Terrain(string heightmapFile)
        if (!File.Exists(heightmapFile)) throw new FileNotFoundException("File not found at path '" + heightmapFile + "'");

        using (var bitmap = new Bitmap(heightmapFile))
            var vertices = new List<Vertex>();
            var indices = new List<int>();

            int index = 0;

            for (int y = 0; y < bitmap.Height - 1; y++)
                for (int x = 0; x < bitmap.Width; x++)
                    float z = bitmap.GetPixel(x, y).R / 127.5f - 1.0f;
                    float z1 = bitmap.GetPixel(x, y + 1).R / 127.5f - 1.0f;

                    vertices.Add(new Vertex(new Vector3(x, y, z)));
                    vertices.Add(new Vertex(new Vector3(x, y + 1, z1)));



            AddVertices(vertices.ToArray(), indices.ToArray());

Here is the result:

enter image description here

What's going on here? What am I doing wrong?


You need to start a new triangle strip per y value.

If I remember right, a triangle strip draws one new triangle for each vertex you give it, reusing the previous two. It's trying to make one very long triangle strip by wrapping the end of row y==0 over to the beginning of row y==1. The vertex at y == 1, x == 0 is using the last two vertices from when y == 0, x == bitmap.Width - 1 and y == 0, x == bitmap.Width - 2.

You can either make a separate triangle strip object (probably easiest), or you can use primitive restart. Or maybe switch to non-strip triangles.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy