多维数据集绘制opengl,但绘制了一个混乱的立方体图像

时间:2010-09-27 23:32:39

标签: opengl

您好 我试图绘制一个尺寸为5 * 5 * 5的立方体,具有六种不同的面部颜色。然而,我看不到所有的脸都是不同颜色的,我看到的只是一个立方体,颜色格式混乱。 有些面清晰可见,而与zx平面平行的顶面不可见。

提前致谢

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_QUADS);

//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 //this face is not visible. I am not understanding why.
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;
}

1 个答案:

答案 0 :(得分:1)

//|| to zx plane //this face is not visible. I am not understanding why.
glVertex3f(0.58, 0, 0.82);

你想要glColor3f。我猜这是一个错字。

两件无关的事情:

  • 你的||到xy plane quad与你的||相同到yz plane quad。
  • 在绘图之前,您没有清除颜色缓冲区。您可能希望在glClear(GL_COLOR_BUFFER_BIT)
  • 的开头DrawCube