title: OpenGL point sprites with depth testing - a blending issue?
body: <p>I am rendering point sprites (using OpenGL ES 2.0 on iOS) as a user's drawing strokes. I am storing these points in vertex buffer objects such that I need to perform depth testing in order for the sprites to appear in the correct order when they're submitted for drawing.</p>
  
  <p>I'm seeing an odd effect when rendering these drawing strokes, as shown by the following screenshot:</p>
  
  <p><img src="https://i.stack.imgur.com/4PAYw.png" alt="Point Sprite Blending Issue"></p>
  
  <p>Note the background-coloured 'border' around the edge of the blue stroke, where it is drawn over the green. The user drew the blue stroke after the green stroke, but when the VBOs are redrawn the blue stroke gets drawn first. When it comes to draw the green stroke, depth testing kicks in and sees that it should be behind the blue stroke, and so does this, with <em>some</em> success. It appears to me to be some kind of blending issue, or to do with incorrectly calculating the colour in the fragment shader? The edges of all strokes should be transparent, however it appears that the fragment shader combines it with the background texture when processing those fragments.</p>
  
  <p>In my app I have created a depth renderbuffer and called <code>glEnable(GL_DEPTH_TEST)</code> using <code>glDepthFunc(GL_LEQUAL)</code>. I have experimented with <code>glDepthMask()</code> to no avail. Blending is set to <code>glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)</code>, and the point sprite colour uses premultiplied alpha values. The drawing routine is very simple:</p>
  
  <ul>
  <li>Bind render-to-texture FBO.</li>
  <li>Draw background texture.</li>
  <li>Draw point sprites (from a number of VBOs).</li>
  <li>Draw this FBO's texture to the main framebuffer.</li>
  <li>Present the main framebuffer.</li>
  </ul>
  
  <p><br>
  <strong>EDIT</strong></p>
  
  <p>Here is some code from the drawing routine.</p>
  
  <p>Setup state prior to drawing:</p>
  
  <pre><code>glDisable(GL_DITHER);
  glEnable(GL_BLEND);
  glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  glEnable(GL_DEPTH_TEST);
  glDepthFunc(GL_LEQUAL);
  </code></pre>
  
  <p>Drawing routine:</p>
  
  <pre><code>[drawingView setFramebuffer:drawingView.scratchFramebuffer andClear:YES];
  glUseProgram(programs[PROGRAM_TEXTURE]);
  [self drawTexture:[self textureForBackgroundType:self.backgroundType]];
  
  glUseProgram(programs[PROGRAM_POINT_SPRITE]);
  
  // ...
  // Draw all VBOs containing point sprite data
  // ...
  
  [drawingView setFramebuffer:drawingView.defaultFramebuffer andClear:YES];
  glUseProgram(programs[PROGRAM_TEXTURE]);
  [self drawTexture:drawingView.scratchTexture];
  
  [drawingView presentFramebuffer:drawingView.defaultFramebuffer];
  </code></pre>
  
  <p>Thanks for any help.</p>