Display 3D point using opengl

余生长醉 提交于 2021-01-28 01:14:18

问题


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);
    }

回答1:


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


来源:https://stackoverflow.com/questions/52804807/display-3d-point-using-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!