tags:

views:

37

answers:

2

Hi I am trying to draw a cube of size 5*5*5 with six diffrent face colors. How ever, when I am doing it, It draws with the single colors, Below is the code for cube. Hope to get some help , Thanks in advance

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;
}
+1  A: 

Use GL_QUADS, GL_POLYGON can have undefined behavior if all vertices are not on the same plane if I'm not mistaken. Also, use GL_SMOOTH for shade model, I think GL_FLAT uses the first color for the whole face, again, if I'm not mistaken, it has been a while.

Madman
You are right, but GL_FLAT should be no problem here since all vertices of the same quad have the same color. So there should be no difference between flat and smooth shading.
ChrisM
I think it's the GL_FLAT + GL_POLYGON is why the color of the first vertex is used for whole geometry. GL_POLYGON makes sure all vertices belong to the same "primitive", which in this case doesn't even lie on the same plane, and GL_FLAT makes sure the first glColor is used for all vertices. If you change any of the parameters, the output will look correct, but the GL_POLYGON is definitely wrong here, and best be avoided at all costs. Also, the output with GL_SMOOTH will differ depending on how the GL_QUAD/GL_POLYGON will get tessellated into triangles.
Madman
A: 

The problem is the

glBegin(GL_POLYGON);

This means that all following vertices are belonging to the same, convex polygon.

Changing to

glBegin(GL_QUADS);

should solve your problem.

In general, you should never use GL_POLYGON anyway because it is usually really slow.

ChrisM
Thanks Chris. I was trying since an hour. It worked. :)