title: OpenGL Fade in / fade out a texture2D
body: <p>I am writing an application that displays .jpg files that are stored as Texture2D (RGB) in OpenGL. I want to smoothly change from one texture2D to the next by fading to black, then fading into the next texture.</p>
  
  <p>After looking for some explanation I wrote something like this.</p>
  
  <pre><code>void renderTexture()
  {
    glMatrixMode(GL_MODELVIEW);
  
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, mTexture);
  
    gluSphere(mQuad, 1.0f, 50, 50);
  
    glBindTexture(GL_TEXTURE_2D, 0);
  }
  
  void fadeToBlack()
  {
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  
    for (GLfloat alpha = 1.0; alpha &gt; 0.0; alpha -= 0.05)
    {
      glColor4f(0.0, 0.0, 0.0, alpha);
      renderTexture();
  
      glFlush();
      glutSwapBuffers();
    }
  
    glDisable(GL_BLEND);
  }
  </code></pre>
  
  <p>Unfortunately, this does not fade to black but instead switches to black immediately. I must have some misunderstanding on how GL_BLEND is working here. Can somebody please point out what I am doing wrong?</p>
  
  <p>** EDIT: This did the trick. Thanks a lot j-p and Benjamin for the pointers **</p>
  
  <pre><code>void fadeToBlack()
  {
    for (GLfloat alpha = 1.0; alpha &gt; 0.0; alpha -= 0.001)
    {
      renderTexture();
      glColor4f(alpha, alpha, alpha, alpha);
  
      glFlush();
      glutSwapBuffers();
    }
    glColor4f(1.0, 1.0, 1.0, 1.0);
  }
  </code></pre>