OpenGL draw transparent sphere over solid sphere

Viewed 118

I have a small sphere with an Earth texture and I want to have a slightly bigger sphere with clouds texture over it, that is transparent.

I draw my objects like this.

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();

  glDisable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glPushMatrix();
  auto sphere = gluNewQuadric();
  gluQuadricDrawStyle(sphere, GLU_FILL);
  gluQuadricTexture(sphere, true);
  gluQuadricNormals(sphere, GLU_SMOOTH);
  th4->bind(); // Cloud texture
  gluSphere(sphere, 0.42, 32, 32);
  glPopMatrix();
  glDisable(GL_BLEND);

  glEnable(GL_DEPTH_TEST);
  glPushMatrix();
  sphere = gluNewQuadric();
  gluQuadricDrawStyle(sphere, GLU_FILL);
  gluQuadricTexture(sphere, true);
  gluQuadricNormals(sphere, GLU_SMOOTH);
  th2->bind(); // Earth texture
  gluSphere(sphere, 0.4, 32, 32);
  glPopMatrix();

  glutSwapBuffers();

I initialize glut like this

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

The problem is, if I draw them in this order, my Earth sphere appears in front of the cloud texture sphere, and if I swap the drawing order I can no longer see my Earth sphere through my clouds sphere.

Updated Code:

  glCullFace(GL_BACK);
  glEnable(GL_CULL_FACE);
  glEnable(GL_DEPTH_TEST);
  glDisable(GL_BLEND);
  glPushMatrix();
  auto sphere = gluNewQuadric();
  gluQuadricDrawStyle(sphere, GLU_FILL);
  gluQuadricTexture(sphere, true);
  gluQuadricNormals(sphere, GLU_SMOOTH);
  th2->bind();
  gluSphere(sphere, 0.4, 32, 32);
  glPopMatrix();


  glDisable(GL_DEPTH_TEST);
  glEnable(GL_BLEND);
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glPushMatrix();
  sphere = gluNewQuadric();
  gluQuadricDrawStyle(sphere, GLU_FILL);
  gluQuadricTexture(sphere, true);
  gluQuadricNormals(sphere, GLU_SMOOTH);
  th4->bind();
  gluSphere(sphere, 0.42, 32, 32);
  glPopMatrix();
  glDisable(GL_BLEND);
1 Answers
Related