# Terrain generation from heightmap

This topic is 2895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, everybody. This is my first post here, but I've been reading these forums for years and it's always been very helpful to me. I'm having a problem creating an index buffer for terrains generated from heightmaps with dimensions that are greater than 256x256. I've searched for hours and I've tried to solve the problem on my own for days, but sadly nothing works. I have everything else working though, loading, getting height values, vertex generation. It works perfectly for heightmaps 256x256 and below...

Before I post my code I'll show you some screenshots. Ignore the blue thing, it's a camera that I want to roam (heh heh, ROAM...) around the terrain so I can test out various culling and LOD algorithms to find out which one is the best for specific iDevices.

256x256

512x512

This is making me pull my hair out... And I know it's not a loading/setting vertices thing because when I render the vertex buffer (not the index buffer) I get this
512x512

This is what my index buffer construction code looks like:
 void TerrainSceneNode::CreateFromHeightMap(const HeightMap &HMap,const Dimension2D &TerrainSize,float MinHeight,float MaxHeight) { Dimension2D TexSize = HMap.GetDimensions(); for(int X = 0;X < TexSize.Width;X++) { for(int Z = 0;Z < TexSize.Height;Z++) { Vertices += HMap.GetVertexAtPos(Vector2D(X,Z),TerrainSize,MinHeight,MaxHeight); } } for(int X = 0;X < TexSize.Width;X++) { IndexBuffer Strip; Strip.SetVertexBuffer(&Vertices); for(int Z = 0;Z < TexSize.Height;Z++) { Strip += X * TexSize.Width + (Z + 1); Strip += (X + 1) * TexSize.Width + (Z + 1); } TStrips.push_back(Strip); } TerrainArea = Rectangle2D(-TerrainSize.Width * 0.5f,-TerrainSize.Height * 0.5f,TerrainSize); Loaded = true; } 

The first set of for loops works perfectly, the problem has to be something with the index buffer code.

I've looked over that a million times and I don't understand what's going on. Vertices is a vertex buffer I use that has TexSize.Width * TexSize.Height vertices in it, so the index of a 2D position of an element in the buffer (a 1D std::vector) would be X * TexSize.Width + Z. Why does that work perfectly for heightmaps 256x256 and below, but not a SINGLE pixel more? I cannot find my error.

Also, this is probably important. When I render the vertices as points like in the last 512x512 image but with a 1024x1024 heightmap I get this:
1024x1024

It's kind of neat that 1,048,576 vertices looks like a regular mesh here, but that's irrelevant. Also sorry for the large image size, I forgot to click resize on tinypic...
The left side of the terrain is identical to about 1/4 of the right side! What the heck is going on here? Please somebody help me before I pull all my hair out and my girlfriend leaves me.

##### Share on other sites
How are you creating the indexbuffer?
256*256=65536, or the limit of a 16bit number. At a guess it looks like you need to use 32bit indices.

##### Share on other sites
Why didn't I think of that?! That's probably it. Thank you so much. I'm using 16bit indices because OpenGL ES requires 16bit or less. So it looks like I'll just have to take out the brute force render completely and render patches of terrain instead.

Thank you, RabidDog!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 13
• 9
• 11
• 15
• 21