OpenGL 一个旋转的立方体,为什么红色的面没有显示

OpenGL 一个旋转的正方体,为什么红色的面没有显示
本帖最后由 a3250100907 于 2012-02-16 10:52:48 编辑

OpenGL 一个旋转的立方体,为什么红色的面没有显示
程序运行的时候,可以看到正方体由内向外旋转。让它就这样旋转,应该是4个面,但是感觉不对,因为只有3个颜色。

#include <gl/glut.h>

GLfloat vertices[][3] = {{-1.0, -1.0, 1.0}, {-1.0, 1.0, 1.0}, 
        {1.0, 1.0, 1.0}, {1.0, -1.0, 1.0}, {-1.0, -1.0, -1.0}, 
        {-1.0, 1.0, -1.0}, {1.0, 1.0, -1.0}, {1.0, -1.0, -1.0}};

GLfloat colors[][3] = {{1.0, 0.0, 0.0}, {0.0, 1.0, 1.0}, 
        {1.0, 1.0, 0.0}, {0.0, 1.0, 0.0}, 
        {0.0, 0.0, 1.0}, {1.0, 0.0, 1.0}};

int axis;
float theta[3];

void polygon(int a, int b, int c, int d)
{
    glBegin(GL_POLYGON);
    glVertex3fv(vertices[a]);
    glVertex3fv(vertices[b]);
    glVertex3fv(vertices[c]);
    glVertex3fv(vertices[d]);
    glEnd();
}

void colorcube()
{
    glColor3fv(colors[0]);
    polygon(0, 3, 2, 1);
    glColor3fv(colors[1]);
    polygon(2, 3, 7, 6);
    glColor3fv(colors[2]);
    polygon(3, 0, 4, 7);
    glColor3fv(colors[3]);
    polygon(1, 2, 6, 5);
    glColor3fv(colors[4]);
    polygon(4, 5, 6, 7);
    glColor3fv(colors[5]);
    polygon(5, 4, 0, 1);
}

void spinCube()
{
    theta[axis] += 1.0;
    if (theta[axis] > 360.0) theta[axis] -= 360.0;
    glutPostRedisplay();
}

void mouse(int btn, int state, int x, int y)
{
    if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)    axis = 0;
    if (btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)axis = 1;
    if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)    axis = 2;
}

void init()
{
    //glClearColor(0.0, 0.0, 0.0, 0.0);
    //glColor3f(1.0, 1.0, 1.0);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glLoadIdentity();
    glRotatef(theta[0], 1.0, 0.0, 0.0);
    glRotatef(theta[1], 0.0, 1.0, 0.0);
    glRotatef(theta[2], 0.0, 0.0, 1.0);
    colorcube();
    glFlush();