OpenGL ES 2.0, 2D Alpha Transparency Artifacts
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]](http://img188.imageshack.us/img188/8206/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:
Also, I'm loading my PNG's like Apple does in a few samples:
Any help would be greatly appreciated.
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]](http://img188.imageshack.us/img188/8206/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.
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.
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.
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.
Aha! Right on. Always glad to help.

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