lundi 20 avril 2015

Colors disappear after drawing image

I am trying to draw a sphere and a cube at the same time on my screen. But the colors of my cube(which gets drawn first) dissapear. I don't understand why.

the sphere on the right is fine. But my cube on the left isn't. I added texture to both:

visualization

I can perfectly draw both of them seperately, but when I try to draw both of them on one widget something goes wrong. I tought the popping and pushing would solve this issue, but it doesn't.

code:

void MyGLWidget::drawCube()
{

    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); 
    glLoadIdentity();

    gluLookAt(position,0.5,-0.1,-0.5,-0.5,0,0,0,1);

    glTranslatef( 0.5, 0, 0.0);
    glRotatef(getCubeAngle(), 1.0f, 0.0f, 0.0f);
    glTranslatef(0, 0, 0);

    glDisable(GL_TEXTURE_GEN_S);
    glDisable(GL_TEXTURE_GEN_T);

    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
       //back
      glTexCoord2f(0.0, 1.0); glVertex3f(-0.1, 0.1,-0.1 );
      glTexCoord2f(1.0, 1.0); glVertex3f(0.1, 0.1,-0.1); 
      glTexCoord2f(1.0, 0.0); glVertex3f(0.1,-0.1,-0.1); 
      glTexCoord2f(0.0, 0.0); glVertex3f(-0.1,-0.1,-0.1);
    /*rest of cube gets drawn*/
      glEnd();
     glFlush();
     glPopMatrix();
}

void MyGLWidget::drawSun()
{
   glPushMatrix();
   glLoadIdentity();
   glBindTexture(GL_TEXTURE_2D, texturePlanet[0]);

   glPushMatrix();
   glScalef(1,1,1);
   glLoadIdentity();
   GLUquadricObj *quadric=gluNewQuadric();
   gluQuadricTexture(quadric, GLU_TRUE);
   gluQuadricNormals(quadric, GLU_SMOOTH);

   glEnable(GL_TEXTURE_2D); //
   glBindTexture(GL_TEXTURE_2D,texturePlanet[0]);//

   gluSphere(quadric, 0.25, 360,360);
   glDisable(GL_TEXTURE_2D);//

   gluDeleteQuadric(quadric);
  glPopMatrix();

}

void MyGLWidget::paintGL()
{
    drawCube();
    drawSun();
}

Aucun commentaire:

Enregistrer un commentaire