2

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);
Paul
  • 776
  • 1
  • 5
  • 18
  • see [OpenGL - How to create Order Independent transparency?](https://stackoverflow.com/a/37783085/2521214) and [VEEERRRYYY Simplified version of Atmospheric scattering](https://stackoverflow.com/a/19659648/2521214) – Spektre Apr 06 '22 at 06:20

1 Answers1

2

Do the following:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174