0

在这段代码中,我尝试绘制一个立方体。我尝试逆时针绘制所有面顶点。
问题是,如果我不旋转立方体,只绘制红色面,如果我将它旋转 5 度,我只会看到立方体的一部分。

#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>

int width=500, height=500, depth=500;

void init()
{
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(200, 200,-200, 200, 200, 0, 0, 1, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,width,0,height);
    gluPerspective(90, 1, -100, 100);
    glViewport(0, 0, width, height);
}

void drawCube()
{
    int vertices[8][3]= { {100,100,0} , {300,100,0}, {300,300,0}, {100,300,0}, {100,100,300} , {300,100,300}, {300,300,300}, {100,300,300} };
    glBegin(GL_QUADS);

    glColor4f(1, 0, 0, 0);
    glVertex3iv(vertices[0]);
    glVertex3iv(vertices[1]);
    glVertex3iv(vertices[2]);
    glVertex3iv(vertices[3]);

    glVertex3iv(vertices[4]);
    glVertex3iv(vertices[5]);
    glVertex3iv(vertices[6]);
    glVertex3iv(vertices[7]);

    glColor4f(0, 1, 0, 0);
    glVertex3iv(vertices[1]);
    glVertex3iv(vertices[5]);
    glVertex3iv(vertices[6]);
    glVertex3iv(vertices[4]);

    glVertex3iv(vertices[0]);
    glVertex3iv(vertices[4]);
    glVertex3iv(vertices[7]);
    glVertex3iv(vertices[3]);

    glColor4f(0, 0, 1, 0);
    glVertex3iv(vertices[3]);
    glVertex3iv(vertices[2]);
    glVertex3iv(vertices[6]);
    glVertex3iv(vertices[7]);

    glVertex3iv(vertices[0]);
    glVertex3iv(vertices[1]);
    glVertex3iv(vertices[5]);
    glVertex3iv(vertices[4]);

    glEnd();
}


void display()
{
    glClearColor(0.0, 0.0, 0.0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(200,200,150);
    glRotatef(5, 0, 1, 0);
    glTranslatef(-200,-200,-150);
    drawCube();
    glutSwapBuffers();
}

void idle(void)
{
}

int main(int argc, char * argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(width, height);
    glutCreateWindow("Test");
    glutDisplayFunc(display);
    glutIdleFunc(idle);
    init();
    glutMainLoop();
    return 0;
}

这就是我所看到的:

Window

但我应该看到一个旋转的立方体,所以我应该看到右边另一张脸的部分。我怀疑我按逆时针顺序绘制顶点或其他什么东西出错了。

PS:代码已经过时了,因为在我的大学我没有机会学习最新版本的OpenGL,我必须使用GLUT。

4

1 回答 1

1

几个问题:

  1. 您的投影矩阵设置不合理。

    首先,您应该决定是否需要正交投影或透视投影。

    If you want orthographic, use gluOrtho2d. If you want a perspective projection, use gluPerspective. Using both will generate a bizarre transformation that's certainly not what you want.

  2. gluPerspective can't have a negative near plane. The near plane should be greater than zero, perhaps something small like 1, with a far plane defining how far away from the camera you want the back clip plane to be. Since you seem to be using units in the hundreds, I might recommend a back plane of 1000 or so.

  3. You're calling gluLookAt, but erasing the view matrix by calling glLoadIdentity in display(). If you want a view matrix, don't erase it after you program it.

于 2012-11-13T17:46:30.753 回答