c++ - 使用opengl显示3D点

标签 c++ opengl 3d glut perspectivecamera

简单地说,我只是想显示一个 3d 点,但没有任何显示!

如何显示?下面的代码有什么问题,有人可以帮忙吗?

初始化:

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

显示:

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

最佳答案

reshape 函数中,您将近平面和远平面分别设置为 1.0 和 20.0:

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

所有不在近平面和远平面之间的几何体都被剪裁。

这意味着从点到视点(眼睛坐标)的 z 距离必须在 1.0 到 20.0 的范围内。

点的z坐标为0.0:

glVertex3f(-120.0, 25.0, 0.0);

视点(相机)的 z 坐标也是 0.0(gluLookAt 的第三个参数):

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

这导致视点(眼睛)与点之间的距离为 0-0 = 0 并且点被剪裁。


要解决这个问题,要么你必须改变点的 z 坐标:

glVertex3f(-120.0, 25.0, 5.0);

或观点:

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

关于c++ - 使用opengl显示3D点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52804807/

相关文章:

c++ - Qt 5.1 使用 QSerialPort 进行串口通信

opengl - 如何获得自动唯一的原子计数器绑定(bind)点(无硬编码绑定(bind)=)?

python - 使用 python-matplotlib 连续 3D 绘图(即图形更新)?

python - Python 中的 3D 多边形

c++ - 为什么我要在不调试的情况下启动调试构建?

c++ - asin 使用 Clang 在不同的平台上产生不同的答案

c++ - if(a == b) 不适用于 for 循环中的 double

opengl - glGetString 和 glGetShaderInfoLog 等函数使用什么编码

c++ - Vertex Shader 和 fragment 编译失败,与 main 冲突

靠近相机时,Python 光线追踪会扭曲物体形状