Jump to content
  • Advertisement
Sign in to follow this  
rogerdv

OpenGL terrain geometry calculation and rendering

This topic is 4320 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

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 this post


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

May I advise writing:

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

Then your code becomes:

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 this post


Link to post
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 :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!