Jump to content
  • Advertisement
Sign in to follow this  
MichaelDeCicco

Terrain generation from heightmap

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

If you intended to correct an error in the post then please contact us.

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
2po2j2r.png

512x512
2a7d83a.png

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
14xl2bl.png

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
2qapgnq.png
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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!