10

I am working on iPhone app and I have an openGL view rendering on top of a regular UIView. It's working, but I can't seem to get the openGL view to have a transparent background that shows the UIView underneath. Instead, I get a big black box.

I have tried setting the background color to UIColor clearColor, I've set opaque to NO, I've set glClearColor to 0.0,0.0,0.0,0.0.

I think I'm misunderstanding/misusing something related to blend modes, but I'm not sure. Can anyone give me a bit of sample code which shows how to do this?

Thanks in advance.

1
  • Did you ever get this working? I am having similar problem. I am using kEAGLColorFormatRGBA8 in drawableProperties.
    – westsider
    Jul 31, 2010 at 19:58

3 Answers 3

12

Here's what I had to do to get this to work:

eaglLayer.opaque = NO;      
eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                                kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

And incase you're doing what I was doing and are trying to test it with the GLGravity demo, there's another call to glClearColor(0.0f, 0.0f, 0.0f, 1.0f); every time drawView is called -- so make sure you change that one to glClearColor(0.0f, 0.0f, 0.0f, 0.0f); too! :)

2
  • 1
    I found the "drawableProperties" was unnecessary in iOS 5 and above
    – Adam
    May 10, 2012 at 12:37
  • HOWEVER ... I want to point out that you MUST call "opaque = NO" in "viewDidLoad" or similar - if you set it in the NIB file, Apple silently overwrites it at runtime (evil!)
    – Adam
    May 10, 2012 at 12:38
4

Add those in EAGLView.m

-(id)initWithCoder:(NSCoder*)coder

self.opaque = NO;
self.backgroundColor = [UIColor clearColor];

CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = NO;

CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
const CGFloat myColor[] = {0.0, 0.0, 0.0, 0.0};
eaglLayer.backgroundColor = CGColorCreate(rgb, myColor);
CGColorSpaceRelease(rgb);
2

Check the color format of your view. If you’ve set it to kEAGLColorFormatRGB565 via the drawableProperties dictionary, the alpha channel of the view’s contents will be implicitly treated as 1.0, regardless of what you draw into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.