I created a fixed frame then I drew the parallelepiped. Next, I created another frame that's supposed to be the object frame which is attached to the object. Before enabling the depth test, the object frame's axes intersect at the origin but the colors of the parallelepiped seems to interfere and everything is messed up. For example: the front face of the object is red, if I don't add the depth test just before drawing the object, the bottom becomes red as shown in the following picture.
When I add the depth test just before drawing the object, the colors are correct but the axes of the object's frame don't intersect as shown in the picture where the front face of the object is red. Here is a picture of my MainWindow:
Here is my code:
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(6.0, 6.0, 6.0, 3.0,3.5,0.0, 0.0,1.0,0.0);
glPushMatrix();
glTranslatef(1.1, 2.1, -3.1);
glScalef(0.8,0.8,0.8);
glLineWidth(3.0f);
glBegin(GL_LINES);
//X_axis
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
//Y_axis
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
//Z_axis
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();
// rotation about X axis
glRotatef(x,0.0,0.0,1.0);
// rotation about Y axis
glRotatef(y,0.0,1.0,0.0);
// rotation about Z axis
glRotatef(z,-1.0,0.0,0.0);
//glRotatef(90.0f,0.0f,0.1f,0.0f);
// Draw_Object
glEnable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
// Top (y=1.0)
glColor4f(0.0f, 1.0f, 0.0f,0.5f); // Green
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor4f(1.0f, 0.5f, 0.0f,0.5f); // Orange
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor4f(1.0f, 0.0f,0.0f,0.5f); // Red
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor4f(1.0f, 1.0f, 0.0f,0.5f); // Yellow
glVertex3f(2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor4f(0.0f, 0.0f, 1.0f,0.5f); // Blue
glVertex3f(-2.0f, 1.0f, 1.0f);
glVertex3f(-2.0f, 1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, -1.0f);
glVertex3f(-2.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor4f(1.0f, 0.0f, 1.0f,0.5f); // Purple
glVertex3f(2.0f, 1.0f, -1.0f);
glVertex3f(2.0f, 1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, 1.0f);
glVertex3f(2.0f, -1.0f, -1.0f);
glEnd();
glFlush();
// Object_frame
glRotated(-90,0.0,1.0,0.0);
glBegin(GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(4.0, 0.0, 0.0);
// arrow
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, 0.25f, 0.0f);
glVertex3f(4.0, 0.0f, 0.0f);
glVertex3f(3.75, -0.25f, 0.0f);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 4.0, 0.0);
// arrow
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(0.25, 3.75f, 0.0f);
glVertex3f(0.0, 4.0f, 0.0f);
glVertex3f(-0.25, 3.75f, 0.0f);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 4.0);
// arrow
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, 0.25f ,3.75f );
glVertex3f(0.0, 0.0f ,4.0f );
glVertex3f(0.0, -0.25f ,3.75f );
glEnd();