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.
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.
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).