# OpenGL terrain geometry calculation and rendering

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

## Recommended Posts

Im trying to implement a basic terrain renderer following the one explained in chapter 7 of Opengl game programming book. But the result is a weird mesh far from looking like a terrain. Can somebody see if Im missing something in the following code? Iti copied from the book with very few modifications.
// this is the map for heights
float hm[MAP_Z][MAP_X]={
{0,0,2.0,0,0,0,5.0,0,0,0},
{0,2.0,0,0,0,0,0,0,0,0},
{0,0,0,1.0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{2,0,0,0,0,4.1,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{2,0,0,0,0,0,0,0,2.0,0},
{0,0,0,0,2.0,0,0,0,0,0},
{0,0,3.0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,4.0,0,0,0}
};

//here is the code to calculate the geometry

for (z=0;z<MAP_Z;z++) {
for (x=0;x<MAP_X;x++) {
terrain[x][z][0]=x*ts;
terrain[x][z][1]=hm[z][x];
terrain[x][z][2]=z*ts;
}//for x
}//for z

//this is the rendering code

glBindTexture(GL_TEXTURE_2D,grass);
for (z=0;z<=9;z++) {
glBegin(GL_TRIANGLE_STRIP);
for (x=0;x<=9;x++) {
//vertex 0 glColor3f(terrain[x][z][0]/255.0,terrain[x][z][1]/255.0,terrain[x][z][2]/255.0);
glTexCoord2f(0.0,0.0);
glVertex3f(terrain[x][z][0],terrain[x][z][1],terrain[x][z][2]);
//vertex 1 glColor3f(terrain[x+1][z][1]/255.0,terrain[x+1][z][1]/255.0,terrain[x+1][z][2]/255.0f);
glTexCoord2f(1.0,0.0);
glVertex3f(terrain[x+1][z][1],terrain[x+1][z][1],terrain[x+1][z][2]);
//vertex 2 glColor3f(terrain[x][z+1][1]/255.0,terrain[x][z+1][1]/255.0,terrain[x][z+1][2]/255.0);
glTexCoord2f(0.0,1.0);
glVertex3f(terrain[x][z+1][1],terrain[x][z+1][1],terrain[x][z+1][2]);
//vertex 3 glColor3f(terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][2]/255.0);
glTexCoord2f(1.0,1.0); glVertex3f(terrain[x+1][z+1][1],terrain[x+1][z+1][1],terrain[x+1][z+1][2]);

}
glEnd();
}


I got this:

##### Share on other sites
Quote:
  glTexCoord2f(0.0,0.0); glVertex3f(terrain[x][z][0],terrain[x][z][1],terrain[x][z][2]); //vertex 1 glColor3f(terrain[x+1][z][1]/255.0,terrain[x+1][z][1]/255.0,terrain[x+1][z][2]/255.0f); glTexCoord2f(1.0,0.0); glVertex3f(terrain[x+1][z][1],terrain[x+1][z][1],terrain[x+1][z][2]); //vertex 2 glColor3f(terrain[x][z+1][1]/255.0,terrain[x][z+1][1]/255.0,terrain[x][z+1][2]/255.0); glTexCoord2f(0.0,1.0); glVertex3f(terrain[x][z+1][1],terrain[x][z+1][1],terrain[x][z+1][2]); //vertex 3 glColor3f(terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][1]/255.0,terrain[x+1][z+1][2]/255.0); glTexCoord2f(1.0,1.0); glVertex3f(terrain[x+1][z+1][1],terrain[x+1][z+1][1],terrain[x+1][z+1][2]);

Each line should have one [0], one [1], and one [3] on it.

void Add_glVertex( Terrain_Type terrain, int x, int z ) {   glVertex3f(terrain[x][z][0],terrain[x][z][1],terrain[x][z][2]);}

(I don't know what type your terrain variable is, but Terrain_Type should be that type. Use a typedef if you have to.)

   glTexCoord2f(0.0,0.0);   Add_glVertex(terrain, x, z);   glTexCoord2f(1.0,0.0);   Add_glVertex(terrain, x+1, z);   glTexCoord2f(0.0,1.0);   Add_glVertex(terrain, x, z+1);   glTexCoord2f(1.0,1.0);   Add_glVertex(terrain, x+1, z+1);

Not only is the above code much cleaner looking, it also prevents you from "copy-paste" style typos.

##### Share on other sites
Thanks, that was the problem. For some reason I made a mistake when typing.

##### Share on other sites
That "some reason" you speak of is because you're human and humans make mistakes. The above poster has given you some good advice to account for this fact. Copy-paste errors can be the bane of your existance if you're not careful, conscientiously working to avoid these types of errors will save you a lot of wasted time and trouble in the future :)

• ### 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!

• 16
• 32
• 9
• 16
• 22