OpenGL:旋转立方体的错误投影

时间:2021-08-16 06:00:53

I have the following blocks of code.

我有以下代码块。

OpenGL initialization:

    glClearDepth(1.0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    glOrtho(-WIDTH / (float) HEIGHT, WIDTH / HEIGHT, -1.0f, 1.0f, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);

OpenGL loop:

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-1.0f);
    glRotatef(xrot,1.0f,0.0f,0.0f);     
    glRotatef(yrot,0.0f,1.0f,0.0f);     
    glRotatef(zrot,0.0f,0.0f,1.0f);     
    glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
    glBegin(GL_QUADS);
    {

        // The first of six sides
        glTexCoord2f(0.0f, 0.0f);
        glVertex3f(-0.75f, -0.75f, 0.75f);
        glTexCoord2f(0.75f, 0.0f);
        glVertex3f(0.75f, -0.75f, 0.75f);
        glTexCoord2f(0.75f, 0.75f);
        glVertex3f(0.75f, 0.75f, 0.75f); 
        glTexCoord2f(0.0f, 0.75f);
        glVertex3f(-0.75f, 0.75f, 0.75f);   
       ....

Width = 800, heigh = 600. I expect to have a perfectly rotating cube; however, at some angles it does not look correct.

宽度= 800,高度= 600.我希望有一个完美旋转的立方体;但是,从某些角度来看,它看起来并不正确。

OpenGL:旋转立方体的错误投影 I think that I have an issue in glOrtho as I don't know how to create the correct expression.

我认为我在glOrtho中有一个问题,因为我不知道如何创建正确的表达式。

2 个解决方案

#1


Your cube is clipped by the front and near planes. Your cube has an extent of of 1.5, and its center is 1 units away from your "camera", but your clipping range is just [-1,1] (relative to the camera), so that the farther edges will be out of the viewing frustum.

你的立方体被前面和近面的平面夹住。你的立方体的范围是1.5,它的中心距离你的“相机”1个单位,但是你的剪裁范围只有[-1,1](相对于相机),所以更远的边缘将会离开观看视锥体。

#2


glPerspective() is better approach for 3D rendering instead of glOrtho(). Use glPerspective() to create the viewing volume. When I replaced glOrtho() expression by
gluPerspective(45f, (float) WIDTH / (float) HEIGHT, 0.1f, 100f);

glPerspective()是更好的3D渲染方法,而不是glOrtho()。使用glPerspective()创建查看量。当我用gluPerspective(45f,(float)WIDTH /(float)HEIGHT,0.1f,100f)替换glOrtho()表达式时;

I got the perfect cube.

我得到了完美的立方体。

OpenGL:旋转立方体的错误投影

#1


Your cube is clipped by the front and near planes. Your cube has an extent of of 1.5, and its center is 1 units away from your "camera", but your clipping range is just [-1,1] (relative to the camera), so that the farther edges will be out of the viewing frustum.

你的立方体被前面和近面的平面夹住。你的立方体的范围是1.5,它的中心距离你的“相机”1个单位,但是你的剪裁范围只有[-1,1](相对于相机),所以更远的边缘将会离开观看视锥体。

#2


glPerspective() is better approach for 3D rendering instead of glOrtho(). Use glPerspective() to create the viewing volume. When I replaced glOrtho() expression by
gluPerspective(45f, (float) WIDTH / (float) HEIGHT, 0.1f, 100f);

glPerspective()是更好的3D渲染方法,而不是glOrtho()。使用glPerspective()创建查看量。当我用gluPerspective(45f,(float)WIDTH /(float)HEIGHT,0.1f,100f)替换glOrtho()表达式时;

I got the perfect cube.

我得到了完美的立方体。

OpenGL:旋转立方体的错误投影