绘制图像后颜色消失

时间:2015-04-20 13:31:18

标签: c++ opengl

我正在尝试在屏幕上同时绘制一个球体和一个立方体。但是我的立方体(首先被绘制)的颜色消失了。我不明白为什么。

右边的球体很好。但我左边的立方体不是。 我在两者中添加了纹理:

visualization

我可以完全同时绘制它们,但是当我试图在一个小部件上绘制它们时出错了。 我认为弹出和推动会解决这个问题,但它没有。

代码:

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();
}

1 个答案:

答案 0 :(得分:0)

这是因为在你的立方体绘图中,你没有启用纹理。

 glEnable(GL_TEXTURE_2D); //ADD THIS TO ENABLE TEXTURING
 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();

 glDisable(GL_TEXTURE_2D); // ADD THIS TO DISABLE TEXTURING