Walkin on terrain - Opengl

Gabriel
  • Walkin on terrain - Opengl Gabriel

    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!

  • You may have some luck with using gluLookAt() to manipulate the camera. It'd probably be easier to move the camera position and the point that it's looking at instead of rotating the camera yourself. This would also simplify doing things like "looking around" from a stationary point.

  • The answer is very simple.

    When you render terrain you supply 3 values, x,y,z. Lets assume that z value is height. So when walking you just need to keep camera above z value! You get camera x,y query height in that spot and place camera above z value.When there is no z value for this spot interpolate from near by points.

    Thats all.

    You also may want to keep camera some constant above terrain itself (to give illusion of being physical character).

    You also may want to put all height in one array. And assume that x and y change by const interval, to simplify your terrain generation. And you get height-map!

  • The best way to make sure you're above a triangle is to use barycentric coordinates. In short, this is a coordinate system local to a triangle that allows you to get the height of a point somewhere in the interior of the triangle.

    So your algorithm would go something like:

    • Get the camera/player position
    • Figure out what triangle that player is on (assuming you have a regular grid of triangles in the (x,y) plane this should be trivial).
    • Using the 3 points of that triangle convert the camera point to triangle space and use the barycentric coordinates to solve for the height.
    • Unproject it from triangle space back to world space and you'll have the point on the triangle where the camera should be. You'll probably want to move this up so that it appears the camera is on the terrain proper.

Tags
c++ c opengl 3d
Related questions and answers
  • ); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i... NEHE'S TUT glBegin (GL_TRIANGLES); // Tell OpenGL What We Want To Draw for(int i = 0; i &lt; numTriangles; i++) { MD2Triangle* triangle..., 0.0f, 1.0f); glScalef(scale, scale, scale); // Loop through the objects for (int i = 0; i &lt; numObjects; i++) { // Enable texture coordiantes, normals

  • (zbd)); zbd.Width = SCREEN_WIDTH; // set the width of the depth buffer zbd.Height = SCREEN_HEIGHT; // set the height of the depth buffer zbd.ArraySize = 1; // we are only creating one texture... be on the depth buffer is 0.0 viewport.MaxDepth = 1; // the farthest an object can be on the depth buffer is 1.0 device-&gt;RSSetViewports(1, &viewport); // set the viewport } // this is the function used to render a single frame void render_frame() { // clear the window to a deep blue and clear the depth buffer to 1.0f device-&gt;ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f

  • (GL_LIGHT0); glEnable(GL_LIGHT1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0f, 0.0f, 0.0f); int vertexIndex = 0, normalIndex; glRotatef(90, 0, 1, 0); glPushMatrix(); for(int a = 0; a &lt; (int)groups.size(); a++) { if(groups[a].type == "prop") { //Code for rotation glPopMatrix(); glPushMatrix(); float x,y,z; x = y = z = 0; int... /= Count; y /= Count; glTranslatef(0, -y, -z); glRotatef(angle, 1, 0, 0); glTranslatef(0, y, z); } for(int b = 0; b &lt; (int)groups[a].faces.size(); b

  • : float m_Position[3]; // x, y, z // offset 0, size = 3*sizeof(float) float m_TexCoords[2]; // u, v // offset 3*sizeof(float), size = 2*sizeof(float) float m_Normal[3... ); glDisableClientState( GL_COLOR_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); } Back to the Vertex Array Object though. My code for creating the Vertex... ); glEnableClientState( GL_NORMAL_ARRAY ); glEnableClientState( GL_COLOR_ARRAY ); glVertexPointer( 3, GL_FLOAT, sizeof(CustomVertex), ((char*)NULL + 0) ); glTexCoordPointer(3, GL_FLOAT, sizeof

  • char index_normal; }; typedef short md2_textcoord_t[2]; struct md2_frame_t { float scale[3]; vec3_t translate; char name[16]; md2_vertex_t vertices[1]; // First vertex of this frame...; float current_time, old_time, interpol; int type, current_frame, next_frame; }; struct md2_glcmd_t { float s; float t; int index; }; #define MD2_IDENT (('2'&lt;&lt;24) + ('P'&lt;&lt;16...; int* m_Normals; unsigned int m_TextureID; float m_Scale; void Interpolate(vec3_t* Vertices); }; #include &lt;helpers/md2.h&gt; #include &lt;iostream> #include &lt

  • wrong in my frame buffer set up code, or elsewhere. But I can't see what. The FBO is set up through the following function: unsigned int fbo_id; unsigned int depth_buffer; int m_FBOWidth, m_FBOHeight; unsigned int m_TextureID; void initFBO() { m_FBOWidth = screen_width; m_FBOHeight = screen_height; glGenRenderbuffers(1, &depth_buffer); glBindRenderbuffer(GL_RENDERBUFFER...(); ///////////////////////////////////////// // Render to Window glClearColor(0, 0, 0, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, screen_width, screen_height); glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT

  • Camera rotation (OpenGL) paintstripper

    it should. Here is some code from my render method: camera-&gt;Place(); ofSetColor(255, 255, 255, 255); //draw axis lines //x-axis glBegin(GL_LINES); glColor3f(1.0f,0.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(100.0f, 0.0f,0.0f); glEnd(); //y-axis glBegin(GL_LINES); glColor3f(0.0f,1.0f,0.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f, 100.0f,0.0f); glEnd(); //z-axis glBegin(GL_LINES); glColor3f(0.0f,0.0f,1.0f); glVertex3f(0.0f,0.0f,0.0f); glVertex3f(0.0f, 0.0f,100.0f); glEnd(); glColor3f(1,1,1); terrain-&gt;Draw(); And the rotate and place methods

  • _FILTER, GL_LINEAR ); ...and the drawing code itself. glBindTexture(GL_TEXTURE_2D,thisIsMyTexture); glBegin( GL_QUADS ); glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 1.0f); glTexCoord2f(1.0f...); glClearDepth(1.0f); glViewport(0, 0, windowWidth, windowHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, windowWidth, windowHeight, 0, 1, -1); glMatrixMode(GL_MODELVIEW); glEnable(GL_TEXTURE_2D... = SDL_DisplayFormat(zoomedImage); SDL_FreeSurface(zoomedImage); } return optimizedImage; } ...here is the code I actually use to create the GL texture... GLuint thisIsMyTexture

  • if we have it enabled if(postFlag&&fboSetup) { glFlush(); glBindFramebuffer(GL_FRAMEBUFFER, 0); glUseProgram(fboProgram); glClear(GL_COLOR_BUFFER_BIT); glBindTexture(GL...); //start rendering scene one way or another glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glUniformMatrix4fv(mats.projHandle,1,GL_FALSE, glm::value_ptr(mats.projMatrix)); glUniformMatrix4fv... is the init code for the FBO and it's texture: glGenTextures(1,&fboimg); glBindTexture(GL_TEXTURE_2D,fboimg); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf

Data information