i wanna walk to terrain... first i created the terrain
void desenha_terreno(float px, float pz){
for (int z = 0; z < iwidth-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < iheight-1; x++)
{
// for each vertex, we calculate the grayscale shade color,
// we set the texture coordinate, and we draw the vertex.
/*
the vertices are drawn in this order:
0 ---> 1
/
/
|/
2 ---> 3
*/
// draw vertex 0
glColor3f(terreno[x][z][1]/255.0f, terreno[x][z][1]/255.0f, terreno[x][z][1]/255.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(terreno[x][z][0], terreno[x][z][1], terreno[x][z][2]);
// draw vertex 1
glTexCoord2f(1.0f, 0.0f);
glColor3f(terreno[x+1][z][1]/255.0f, terreno[x+1][z][1]/255.0f, terreno[x+1][z][1]/255.0f);
glVertex3f(terreno[x+1][z][0], terreno[x+1][z][1], terreno[x+1][z][2]);
// draw vertex 2
glTexCoord2f(0.0f, 1.0f);
glColor3f(terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f, terreno[x][z+1][1]/255.0f);
glVertex3f(terreno[x][z+1][0], terreno[x][z+1][1], terreno[x][z+1][2]);
// draw vertex 3
glColor3f(terreno[x+1][z+1][1]/255.0f, terreno[x+1][z+1][1]/255.0f, terreno[x+1][z+1][1]/255.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(terreno[x+1][z+1][0], terreno[x+1][z+1][1], terreno[x+1][z+1][2]);
}
glEnd();
}
}
I can move the camera with the functions:
void keyboard (unsigned char key, int x, int y) {
if (key=='w')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos += float(sin(yrotrad))*5;
zpos -= float(cos(yrotrad))*5;
ypos -= float(sin(xrotrad))*5;
}
if (key=='s')
{
float xrotrad, yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xrotrad = (xrot / 180 * 3.141592654f);
xpos -= float(sin(yrotrad))*5;
zpos += float(cos(yrotrad))*5;
ypos += float(sin(xrotrad))*5;
}
if (key=='d')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos += float(cos(yrotrad))*5;
zpos += float(sin(yrotrad))*5;
}
if (key=='a')
{
float yrotrad;
yrotrad = (yrot / 180 * 3.141592654f);
xpos -= float(cos(yrotrad))*5;
zpos -= float(sin(yrotrad))*5;
}
if (key=='q')
{
yrot += (float) -0.5*5; //set the xrot to yrot with the addition of the difference in the x position
}
if (key=='e')
{
yrot += (float) 0.5*5; //set the xrot to yrot with the addition of the difference in the x position
}
if (key=='r')
{
xrot += (float) -0.5*5; //set the xrot to yrot with the addition of the difference in the x position
}
if (key=='f')
{
xrot += (float) 0.5*5; //set the xrot to yrot with the addition of the difference in the x position
}
if (key==27)
{
exit(0);
}
glutPostRedisplay();
`
and, i change the position with the values of x,y,z positions
// clear screen and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glTranslatef(0.0f, 0.0f, -cRadius);
glRotatef(xrot,1.0,0.0,0.0);
glRotatef(yrot,0.0,1.0,0.0); //rotate our camera on the y-axis (up and down)
glTranslated(-xpos,-ypos,-zpos); //translate the screen to the position of our camera
// set the camera position
//gluLookAt(cameraX, cameraY, cameraZ, lookX, lookY, lookZ, 0.0, 1.0, 0.0);
// set the current texture to the land texture
//glBindTexture(GL_TEXTURE_2D, land);
desenha_terreno(xpos,ypos);
glEnable(GL_BLEND);
// enable read-only depth buffer
glDepthMask(GL_FALSE);
// set the blend function to what we use for transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
// set back to normal depth buffer mode (writable)
glDepthMask(GL_TRUE);
// disable blending
glDisable(GL_BLEND);
glFlush();
glutSwapBuffers();
angle++; //increase the angle
}
well... i need to walk to the terrain, above the terrain, help me please!