简体   繁体   English

使用 OpenGL 的多边形的透视投影

[英]Perspective projection for a polygon Using OpenGL

I'm trying to implement a perspective projection using open-GL But when I apply gluPerspective(0,0.5,0.5,5) method, the polygon is not shown in a perspective view and shown in orthogonal view instead here is the output我正在尝试使用 open-GL 实现透视投影但是当我应用 gluPerspective(0,0.5,0.5,5) 方法时,多边形不会在透视图中显示,而是在正交视图中显示,这里是 output 在此处输入图像描述 Anyone can help my code:任何人都可以帮助我的代码:

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

the first argument to to gluPerspective is wrong: gluPerspective的第一个参数是错误的:

 gluPerspective(0,0.5,0.5,5);

The first argument to gluPerspective is the vertical field of view angle in degrees. gluPerspective的第一个参数是以度为单位的垂直视场角。 The value has to be greater than 0.0 and less than 180. 0.0 is an invalid argument and causes undefined behavior.该值必须大于 0.0 且小于 180。0.0 是无效参数并导致未定义的行为。 Probably the instruction doesn't set the matrix at all.可能该指令根本没有设置矩阵。

Anyway, if you set a correct angle, then the geometry will be clipped.无论如何,如果你设置了一个正确的角度,那么几何图形就会被剪裁。 The perspective projection matrix defines a Viewing frustum .透视投影矩阵定义了一个视锥体 All the geometry which is not in between the near and fare plane is clipped.所有不在 near 平面和 fare 平面之间的几何图形都将被剪裁。 In your case the near plane is 0.5 and the far plane 5.0.在您的情况下,近平面为 0.5,远平面为 5.0。

Set a view matrix and translate the geometry in between the near and far plane, by shifting the geometry along the negative z axis.通过沿负 z 轴移动几何图形,设置视图矩阵并在近平面和远平面之间转换几何图形。 For instance (0, 0, -2.5).例如 (0, 0, -2.5)。

The 2nd argument of gluPerspective is the aspect ration. gluPerspective的第二个参数是纵横比。 Since the size of the window is 1000x1000, the aspect ratio has to be 1.0:由于 window 的大小为 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM