我正在尝试使用open-GL实现透视投影
但是当我应用gluPerspective(0,0.5,0.5,5)方法时,多边形未在透 View 中显示,而在正交 View 中显示
这是输出
任何人都可以帮助
我的代码:
#include<GL/glut.h>
float angle = 2;
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 5);
//glFrustum(-0.5, 2.4, -0.5, 0.5, -0.5, 0.5);
//glFrustum(-5.0, 5.0, -5.0, 5.0, 5, 100);
gluPerspective(0,0.5,0.5,5);
}
void polygon(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 1.0);
//glLineWidth(2);
//glRotatef(angle, 0.0, 0.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
//glVertex3f(0, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(1000, 1000);
glutCreateWindow("Open Gl 2D Geometric Transformation");
myinit();
glutDisplayFunc(polygon);
glutMainLoop();
return 0;
}
最佳答案
gluPerspective
的第一个参数是错误的:
gluPerspective
的第一个参数是垂直视角,以度为单位。该值必须大于0.0且小于180。0.0是无效的参数,会导致未定义的行为。
该指令可能根本没有设置矩阵。
无论如何,如果设置了正确的 Angular ,则几何将被裁剪。透视投影矩阵定义Viewing frustum。裁剪不在票价平面和票价平面之间的所有几何图形。在您的情况下,近平面为0.5,远平面为5.0。
设置 View 矩阵,并通过沿负z轴移动几何图形在近平面和远平面之间转换。例如(0,0,-2.5)。gluPerspective
的第二个参数是宽高比。由于窗口的大小为1000x1000,因此宽高比必须为1.0:
void myinit(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble fov = 90.0; // 90 degrees
GLdouble aspect = 1.0;
GLdouble near_dist = 0.5;
GLdouble far_dist = 5.0;
gluPerspective(fov, aspect, near_dist, far_dist);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -2.5f); // near_dist < 2.5 < far_dist
}