title: Creating and blending a dynamic texture in OpenGL
body: <p>I need to render a sphere to a texture (done using a Framebuffer Object (FBO)), and then alpha blend that texture with the back buffer. So far I'm not doing any processing with the texture except clearing it at the beginning of every frame.</p>
  
  <p>I should say that my scene consists of nothing but a planet in empty space, the sphere should appear next to or around the planet (kind of like a moon for now). When I render the sphere directly to the back buffer, it displays correctly; but when I do the intermediary step of rendering it to a texture and then blending that texture with the back buffer, the sphere only shows up when it is in front of the planet, the part that isn't in front is just "cut off":</p>
  
  <p><img src="https://i.stack.imgur.com/y5NO2.jpg" alt="Sphere that is cut off by the edge of the planet"></p>
  
  <p>I render the sphere using <code>glutSolidSphere</code> to a RGBA8 fullscreen texture that's bound to an FBO, making sure that every sphere pixel receives an alpha value of 1.0. I then pass the texture to a fragment shader program, and use this code to render a fullscreen quad - with the texture mapped onto it - to the backbuffer while alpha blending:</p>
  
  <pre><code>glBindFramebuffer(GL_FRAMEBUFFER, 0);
  glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_BLEND);
  
  glDisable(GL_DEPTH_TEST);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glLoadIdentity();
  glMatrixMode(GL_PROJECTION);
  glPushMatrix();
  glLoadIdentity();
  glBegin(GL_QUADS);
  glTexCoord2i(0, 1);
  glVertex3i(-1,  1, -1);   // TOP LEFT
  glTexCoord2i(0, 0);
  glVertex3i(-1, -1, -1);   // BOTTOM LEFT
  glTexCoord2i(1, 0);   
  glVertex3i( 1, -1, -1);   // BOTTOM RIGHT
  glTexCoord2i(1, 1);   
  glVertex3i( 1,  1, -1);   // TOP RIGHT
  glEnd();
  glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix();
  glEnable(GL_DEPTH_TEST);
  
  glDisable(GL_BLEND);
  </code></pre>
  
  <p>This is the shader code (taken from an FX file written in Cg):</p>
  
  <pre><code>sampler2D BlitSamp = sampler_state
  {
      MinFilter = LINEAR;
      MagFilter = LINEAR;
      MipFilter = LINEAR;
      AddressU = Clamp;
      AddressV = Clamp;
  };
  
  float4 blendPS(float2 texcoords : TEXCOORD0) : COLOR
  {
      float4 outColor = tex2D(BlitSamp, texcoords);
      return outColor;
  }
  </code></pre>
  
  <p>I don't even know whether this is a problem with the depth buffer or with alpha blending, I've tried a lot of combinations of enabling and disabling depth testing (with a depth buffer attached to the FBO) and alpha blending.</p>
  
  <p>EDIT: I tried just rendering a blank fullscreen quad straight to the back buffer and even that was cropped around the planet's edges. For some reason, <em>enabling</em> depth testing for rendering the quad (that is, removing the lines <code>glDisable(GL_DEPTH_TEST)</code> and <code>glEnable(GL_DEPTH_TEST) in the code above</code>) got rid of the problem, but now everything but the planet and the sphere appears white:</p>
  
  <p><img src="https://i.stack.imgur.com/ARbrf.png" alt="Sphere with white incorrect white background"></p>
  
  <p>I made sure (and could confirm) that the alpha channel of the texture is 0 at every pixel but the sphere's, so I don't understand where the whiteness could be introduced. (Would also still be interested in an explanation why enabling depth testing has this effect.)</p>