0

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:

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();
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • 2
    Without seeing the code, that's impossible to answer. My guess is that you have depth-testing enabled when drawing the lines. – BDL Feb 28 '22 at 13:31
  • 6
    When drawing transparency, the order matters. you usually draw polygons from back-to-front if you want correct blending. – Raildex Feb 28 '22 at 13:44
  • 1
    Please provide a [mre]. Order matters, as Raildex mentioned. – Yunnosch Feb 28 '22 at 13:48
  • The problem is that when I remove the depth test just before drawing the object, the colors become messed up. –  Feb 28 '22 at 15:02
  • 1
    Please read the previous comments carefully. You must sort the faces from back-to-front. See [Transparency Sorting](https://www.khronos.org/opengl/wiki/Transparency_Sorting). – Rabbid76 Feb 28 '22 at 15:06
  • Thank you so much. Well, I also made some researchs and appearently I can avoid the problem related to the colors using glEnable(GL_CULL_FACE). The second frame is visible, its axes intersect and the object colors are correct –  Feb 28 '22 at 15:53
  • see [How to create Order Independent transparency?](https://stackoverflow.com/a/37783085/2521214) – Spektre Mar 01 '22 at 09:26
  • Hello again, when you say that order matters and that I should draw the parallelepiped from back to front, you mean that I have to start drawing the back first and the last face should be the front ? back=>bottom=>left=>right=>top=>front ? –  Mar 02 '22 at 08:31
  • @Ing23 yes however you are combining the mesh with axises so they should be ordered within too ... after you add rotations then hardcoded order will be useless also for slightly more complex shapes is this no longer valid ... Its much easier,faster to 1. render back faces 2. render axises 3. render front faces. so no reordering of faces just 2 times rendering of the whole box just with switching the by: `glFrontFace(GL_CW/GL_CCW)` just like in the link in my previous comment (there I render 4 times as my shape has 2 layers) – Spektre Mar 04 '22 at 06:09

0 Answers0