PDA

View Full Version : Generating terrain

PaulR
01-14-2012, 01:42 PM
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

Kopelrativ
01-15-2012, 01:16 AM
Take a look at the 2D Perlin noise (http://en.wikipedia.org/wiki/Perlin_noise) function to create height maps.

I used the Simplex noise (http://webstaff.itn.liu.se/~stegu/aqsis/aqsis-newnoise/simplexnoise/) of Stefan Gustavsson, but I had to tweak the functions a little to get them normalized.

PaulR
01-15-2012, 01:36 AM
Thanks for replying. I'll do some research into that, cheers.

PaulR
01-15-2012, 03:38 AM
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

Aleksandar
01-15-2012, 06:53 AM
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.

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

I was wondering whether anyone knows of any good books that I can reference to aid my understanding.

Probably one of the most versatile books about terrain rendering is:
3D Engine Design for Virtual Globes (http://www.amazon.com/3D-Engine-Design-Virtual-Globes/dp/1568817118)

Also, many state-of-the-art solutions can be find in:
Virtual Terrain Project (http://vterrain.org/)

PaulR
01-15-2012, 11:09 PM
Cheers mate, that's exactly what I needed. I'll give those a look through.

RigidBody
01-16-2012, 10:51 AM
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();
}

PaulR
01-16-2012, 11:25 AM
Ah, I see. So it having numerous calls to glBegin wouldn't be an inefficient way to implement a sort of mesh terrain?

Dark Photon
01-16-2012, 07:46 PM
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).