[gameprogrammer] Re: terrain geometry calculation problem

It is the same. Here is an screenshot of the problem:
http://www.granhotelstgo.cu/x/terrain.jpg

Sami Näätänen escribió:
On Monday 16 October 2006 17:02, Roger D Vargas wrote:
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.

I use this 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}
};

And here is the code to calculate the geometry

void create_geometry()
{
  int z,x;
  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
}

And 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++) {

The loops are going too far.

'vertex 1' will use x+1 as x index, which is 10 when the x is 9.
So this will overflow the 0..9 index of the terrain array.
Same happens to the z index in vertices 2 and 3 and to the x index in vertex 3.


//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,terra
in[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();
  }

--------------------- To unsubscribe go to http://gameprogrammer.com/mailinglist.html




-- http://dsgp.blogspot.com | Linux, programación, juegos Have no place I can be since I found Serenity But you can’t take the sky from me




______________________________________________ LLama Gratis a cualquier PC del Mundo. Llamadas a fijos y móviles desde 1 céntimo por minuto. http://es.voice.yahoo.com


---------------------
To unsubscribe go to http://gameprogrammer.com/mailinglist.html


Other related posts: