使用opengl显示3D点 [英] Display 3D point using opengl
问题描述
简单地说,我只是想显示一个 3d
点,但没有显示!
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?
初始化:
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();
}
重塑:
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:
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.
这意味着从点到视点(眼睛坐标)的 z 距离必须在 1.0 到 20.0 的范围内.
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.
点的z坐标为0.0:
glVertex3f(-120.0, 25.0, 0.0);
视点(相机)的 z 坐标也是 0.0(gluLookAt
):
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);
这会导致视点(眼睛)与点之间的距离为0-0 = 0
并且该点被裁剪.
This causes that the distance between the point of view (eye) and the point is 0-0 = 0
and the point is clipped.
要解决这个问题,要么你必须改变点的z坐标:
To solve the issue, either you have to change the z coordinate of the point:
glVertex3f(-120.0, 25.0, 5.0);
或观点:
gluLookAt(-120, 25, -5.0, -120, 25, 150, 0, 1, 0);
这篇关于使用opengl显示3D点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!