OpenGL ES 2.0, 2D Alpha Transparency Artifacts

Member
Posts: 31
Joined: 2010.02
Post: #1
Hi,

At the moment I'm rendering 2D sprites with an orthographic projection on top of my 3D scene.

The problem is if I draw two sprites on top of each other the alpha channel color does not blend with the sprite drawn beneath it.

Here is a screenshot as an example:
[Image: screenshot20100328at721.png]

The same problem is happening with my 3D particle system which uses point sprites.

This is the current blending function I'm using, which works with my purely 2D Open GL ES 1.0 projects:
Code:
glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

Also, I'm loading my PNG's like Apple does in a few samples:
Code:
    CGImageRef spriteImage = [UIImage imageNamed:nsstringpath].CGImage;
    
    unsigned width = CGImageGetWidth( spriteImage );
    unsigned height = CGImageGetHeight( spriteImage );
    
    GLubyte *spriteData = ( GLubyte * )calloc( width * height * 4, 1 ); //Must use calloc, so alpha does not contain garbage.
    CGContextRef spriteContext = CGBitmapContextCreate( spriteData, width, height, 8, width * 4, CGImageGetColorSpace( spriteImage ), kCGImageAlphaPremultipliedLast );
    CGContextDrawImage( spriteContext, CGRectMake( 0.0f, 0.0f, (CGFloat)width, (CGFloat)height), spriteImage );
    CGContextRelease( spriteContext );

Any help would be greatly appreciated.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #2
A couple ideas:

Try glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );

That screenshot suggests to me that you might have depth testing on. If that is the case, you will need to sort and draw your translucent sprites from back to front, after you've drawn your opaque objects. Otherwise, depending on what you're doing (e.g. for your 2D sprites over your 3D scene), you could probably just glDisable(GL_DEPTH_TEST) and not worry about sorting.
Quote this message in a reply
Member
Posts: 31
Joined: 2010.02
Post: #3
AnotherJake Wrote:A couple ideas:

Try glBlendFunc( GL_ONE, GL_ONE_MINUS_SRC_ALPHA );

That screenshot suggests to me that you might have depth testing on. If that is the case, you will need to sort and draw your translucent sprites from back to front, after you've drawn your opaque objects. Otherwise, depending on what you're doing (e.g. for your 2D sprites over your 3D scene), you could probably just glDisable(GL_DEPTH_TEST) and not worry about sorting.

Thank you so much! It wasn't the blending function but you were spot on with the depth testing. All of my 2D sprites are sorted but they all lie at the same "z-distance" from my camera so depth testing would cause some of them not to draw.

All, I had to do was glDisable(GL_DEPTH_TEST) before entering my orthographic rendering.

Thanks again.
Quote this message in a reply
Moderator
Posts: 3,570
Joined: 2003.06
Post: #4
Aha! Right on. Always glad to help. Grin
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 2,694 Jan 19, 2013 08:25 AM
Last Post: sealfin
  Sprite transparency in OpenGL? Guest! 26 28,128 Feb 17, 2012 09:24 AM
Last Post: Skorche
  Multisample AA Artifacts metacollin 4 4,777 Aug 14, 2009 07:11 PM
Last Post: metacollin
  Shadow Mapping - Self-Shadowing Z-Fighting Artifacts Bachus 16 20,166 Feb 11, 2009 12:24 PM
Last Post: arekkusu
  Opengl alpha premultiplication issue Najdorf 5 5,040 Nov 13, 2008 09:49 AM
Last Post: Najdorf