在OpenGL中使用gluPerspective

yun*_*eek 2 opengl

我正在做我的OpenGL程序,我碰巧有问题,你能找到它的问题吗?

当我使用glOrtho它时,它以y轴为中心旋转一个平面工作正常,但gluPerspective事实并非如此.

这有一些问题,gluPerspective因为当我将角度更改为0时,我只能看到一些东西而不是整个东西.但是当我把它改为45时,屏幕上什么也没有出现,我也没有得到关于远近值的线索.

 #include iostream  
 #include "Xlib.h"  
 #include "gl.h"  
 #include "glu.h"  
 #include "glut.h"  
 #include math.h  
void setupRC()  
{  
    glClearColor(1,0,0,1);  
    glColor3f(0,0,0);  
}  
void timerfunc(int value)  
{  
    glutPostRedisplay();  
    glutTimerFunc(1, timerfunc ,1);  
}  
void RenderScene()   
{  
    glColor3f(0,0,0);  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
    static GLfloat rot = 0.0f,x =0.0f , y=1.0f , z=0.0f;  
    rot++;  
    glPushMatrix();  

    glRotatef(rot,0.0,1.0,0.0);  
    glBegin(GL_POLYGON);  
        glVertex3i(1,-1,0);  
        glVertex3i(1,1,0);  
        glVertex3i(-1,1,0);  
        glVertex3i(-1,-1,0);  
        glVertex3i(1,-1,0);  

    glEnd();  
    glPopMatrix();  
    if (rot == 360)  
        rot = 0;  
    glutSwapBuffers();  
}  
void ChangeSize(GLint w, GLint h)  
{  
    if(h==0)  
        h = 1;  
    GLfloat aspectratio = (GLfloat)w/(GLfloat)h;  

    glViewport(0,0,w,h);  
    glMatrixMode(GL_PROJECTION);  
    glLoadIdentity();  
    gluPerspective(45.0f, aspectratio, 1,1000);  
    /*if(w <= h)  
        glOrtho(-100,100,-100/aspectratio, 100/aspectratio, 1,-1);  
    else   
        glOrtho(-100*aspectratio, 100*aspectratio , -100,100,1,-1);*/  
    glMatrixMode(GL_MODELVIEW);  
    glLoadIdentity();  

}  

int main(int argc , char **argv)  
{  
    glutInit(&argc, argv);  
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);  
    glutInitWindowSize(800,600);  
    glutInitWindowPosition(0,0);  
    glutCreateWindow("chelsea");  
    glutTimerFunc(1, timerfunc , 1);  
    setupRC();  
    glutDisplayFunc(RenderScene);  
    glutReshapeFunc(ChangeSize);  
    glutMainLoop();  

    return 0;  
}  
Run Code Online (Sandbox Code Playgroud)

Jul*_*n-L 9

您的问题是:您的多边形和您的相机处于相同的坐标.(多边形不在相机前面,它它上面.)

  1. 当多边形处于其初始位置时,它与相机无法正确对齐;
  2. 当它开始旋转时,它的一部分会进入相机前面,但它太靠近而无法显示.

然而,fovy在构建透视矩阵时将其设置为0°会产生一个(真正的)广角相机,它能够看到(真正)近距离的物体 - 解释为什么你可以看到某些东西,"但不是整个事物".

如果您希望能够看到"整个事物" fovy=45,请通过沿Z轴应用负平移来移动多边形远离相机:

void RenderScene()
{
    ...
    glPushMatrix();
    glTranslatef(0.,0.,-10.); // Move away from camera
    glRotatef(rot,0.0,1.0,0.0);
    glBegin(GL_POLYGON);
        ...
    glEnd();
    glPopMatrix();
    ...
}
Run Code Online (Sandbox Code Playgroud)