读书人

一个旋转的正方体为什么红色的面没有

发布时间: 2012-05-01 12:48:58 作者: rapoo

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

C/C++ code
#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();    glutSwapBuffers();}void reshape(int w, int h){    glViewport(0, 0, w, h);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    glOrtho(-4.0, 4.0, -4.0, 4.0, -4.0, 4.0);}int main(int argc, char **argv){    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);    glutInitWindowSize(400, 400);    glutInitWindowPosition(200, 200);    glutCreateWindow("OpenGL");    glutDisplayFunc(display);    glutReshapeFunc(reshape);    glutIdleFunc(spinCube);    glutMouseFunc(mouse);    init();    glutMainLoop();}


[解决办法]
组成每个面的polygon的四个顶点顺序必须从外向内看是逆时针吧
[解决办法]
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[0]);
//glColor3fv(colors[4]);
polygon(4, 5, 6, 7);

glColor3fv(colors[5]);
polygon(5, 4, 0, 1);
}

读书人网 >C++

热点推荐