Opengl Basic立方体绘图,但立方体绘制为单色

时间:2010-09-27 22:43:01

标签: opengl

我正在尝试用六种不同的面部颜色绘制尺寸为5 * 5 * 5的立方体。但是,当我这样做时,它使用单一颜色绘制,下面是立方体的代码。

void init(void)
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
}

void DrawCube(void)
{
glLoadIdentity();
gluLookAt(10, 10, 10, 0, 0, 0, 0, 1, 0);
glBegin(GL_POLYGON);

//face in xy plane
glColor3f(0.82, 0.41, 0.12);//this the color with which complete cube is drawn. 
glVertex3f(0,0 ,0 );
glVertex3f(5, 0, 0);
glVertex3f(5, 5, 0);
glVertex3f(0, 5, 0);

//face in yz plane
glColor3f(1, 0, 0);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 5);
glVertex3f(0, 5, 0);
glVertex3f(0, 5, 5);

//face in zx plance
glColor3f(0, 1, 0);
glVertex3f(0, 0, 0  );
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 0, 0);

//|| to xy plane.
glColor3f(0, 0, 1);
glVertex3f(0, 0, 5);
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to yz plane
glColor3f(0.73, 0.58, 0.58);
glVertex3f(0,0 ,5 );
glVertex3f(5, 0, 5);
glVertex3f(5, 5, 5);
glVertex3f(0, 5, 5);

//|| to zx plane
glVertex3f(0.58, 0, 0.82);
glVertex3f(0, 5, 0  );
glVertex3f(0, 5, 5);
glVertex3f(5, 5, 5);
glVertex3f(5, 5, 0);
glEnd();
glFlush();
}


void reshape(int w,int h){

glViewport(0, 0, (GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1, 1, -1, 1, 1.5, 20);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv){

glutInit(&argc, argv);//we initizlilze the glut. functions
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(DrawCube);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}

2 个答案:

答案 0 :(得分:3)

问题是

glBegin(GL_POLYGON);

这意味着所有后续顶点都属于相同的凸多边形。

更改为

glBegin(GL_QUADS);

应该解决你的问题。

一般情况下,你绝不应该使用GL_POLYGON,因为它通常很慢。

答案 1 :(得分:1)

使用GL_QUADS,如果我没有弄错,如果所有顶点不在同一平面上,GL_POLYGON可能会有未定义的行为。另外,使用GL_SMOOTH作为阴影模型,我认为GL_FLAT使用整个面部的第一种颜色,再次,如果我没有弄错,它已经有一段时间了。