Search code examples
c++opengl3dglutperspectivecamera

Display 3D point using opengl


Simply, I am just trying to show a 3d point but nothing show on!

How to show it? what is wrong in the code below, anybody can help?

init:

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glOrtho(-200, 0.0, -50, 100, 70, 300);
}

display:

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glPointSize(2);
    glBegin(GL_POINTS);
    glVertex3f(-120.0, 25.0, 0.0);
    glEnd();

    glutSwapBuffers();
}

reshape:

  void reshape(int w, int h)
    {
        glViewport(0, 0, (GLsizei)w, (GLsizei)h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(-120, 25, 0.0, -120, 25, 150, 0, 1, 0);
    }

Solution

  • In the reshape function you set a near and far plane of 1.0, respectively 20.0:

    gluPerspective(65.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
    

    All the geometry which is not in between the near and the far plane is clipped.

    This means that z distance from the point to the point of view (eye coordinate) has to be in the range from 1.0 to 20.0.

    The z-coordinate of the point is 0.0:

    glVertex3f(-120.0, 25.0, 0.0);
    

    The z coordinate of the point of view (camera) is 0.0 too (3rd parameter of gluLookAt):

    gluLookAt(-120, 25, 0.0, -120, 25, 150, 0, 1, 0);
    

    This causes that the distance between the point of view (eye) and the point is 0-0 = 0 and the point is clipped.


    To solve the issue, either you have to change the z coordinate of the point:

    glVertex3f(-120.0, 25.0, 5.0);
    

    or the point of view:

    gluLookAt(-120, 25, -5.0, -120, 25, 150, 0, 1, 0);