Generating terrain

I’ve been reading a few online tutorials on how to render terrain and use it with opengl, but I’m having difficulty finding the best solution. I think I need a more in depth study into how best to go by it.

I was wondering whether anyone knows of any good books that I can reference to aid my understanding. I’m using the LWJGL binding of opengl in java, if that matters.

Anyway, any help would be appreciated. Thanks!

Paul

Take a look at the 2D Perlin noise function to create height maps.

I used the Simplex noise of Stefan Gustavsson, but I had to tweak the functions a little to get them normalized.

Thanks for replying. I’ll do some research into that, cheers.

I’m just trying to get this height map working. I’m got a class called Terrain which has a 2D array called ground. This code just loops through the x and z rows/cols and returns the y to the glVertex call. Here’s the code:


	public void drawGround()
	{
		GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
			GL11.glColor3f(1.0f,1.0f,1.0f);
			int h = terrain.ground.length;
			int w = 0;
			if(h > 0)
				w = terrain.ground[0].length;
			float h1 = 0.0f;
			float h2 = 0.0f;
			for(int x = 1; x < h; x++)
			{
				for(int z = 1; z < w; z++)
				{
					h1 = terrain.ground[x][z];
					if(x + 1 < h)
						h2 = terrain.ground[x + 1][z];
					else
						h2 = 0.0f;
					GL11.glVertex3f(x * 10.0f,h1,z * 10.0f);
					GL11.glVertex3f((x + 1) * 10.0f,h2,z * 10.0f);
				}
			}
		GL11.glEnd();
	}

It seems to be working almost perfectly, except there seems to be this one line that goes right down each column, presumably because I haven’t finished the last triangle row.

Here’s an image of what this produces:

http://i.imgur.com/CjDDF.png

The Terrain class is basically structured:


class Terrain
{

public float[][] = new float[][]
{
{0.0f,0.0f,0.0f, ...},
{0.0f,0.0f,0.0f, ...},
{0.0f,0.0f,0.0f, ...},
...
}
};

Just in case you wanted to know. Can you see any way of solving this, or is there perhaps a better method of implementing this?

Thanks,

Paul

It is impossible to find the “best” solution that suites all needs. What kind of terrain are you trying to implement?

Probably one of the most versatile books about terrain rendering is:
3D Engine Design for Virtual Globes

Also, many state-of-the-art solutions can be find in:
Virtual Terrain Project

Cheers mate, that’s exactly what I needed. I’ll give those a look through.

i guess you can get rid of the extra lines by putting the glBegin/glEnd commands into the first loop:

for(int x = 1; x < h; x++)
{
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
for(int z = 1; z < w; z++)
{

}
GL11.glEnd();
}

Ah, I see. So it having numerous calls to glBegin wouldn’t be an inefficient way to implement a sort of mesh terrain?

Very inefficient, if there are a bunch of them. They basically break a batch (a group of triangles/primitives that are sent to the GPU to be drawn together in one go).

Also, I think in some past posts, folks have observed with some drivers that display lists don’t optimize across Begin/End boundaries.

Generally speaking, break a batch when you need to perform a state change, but otherwise avoid doing so (unless you’ve just got a ton of vertices in your batch already, or you’re view-frustum culling groups of triangles).