开发者

Displaying an EAGLView with transparent background on a UIImageView

I have an EAGLView displaying an OpenGL 3D model, and a UIImageView displayi开发者_运维技巧ng an image.

What I want is to display EAGLView above that UIImageView with transparent background.

Is there any way to display this 3D model with transparent background, on top of the UIImageView.

Any solution would be appreciated.


For reference, most of the accepted answer is unnecessary. The only key part is this (in the UIViewController - no need to edit or subclass EAGLView / GLKView):

self.view.opaque = NO; // NB: Apple DELETES THIS VALUE FROM NIB
self.view.backgroundColor = [UIColor clearColor]; // Optional: you can do this in NIB instead

You can set the background Color in the NIB - but you CANNOT set the opacity (Apple seems to overwrite this var for GL views at initWithCoder :( )

Plus, of course, in your OpenGL code you need to clear to alpha=0.0. So, wherever your glClear / glClearColor calls are already, replace the clearColor with:

glClearColor( 0, 0, 0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


Here is the solution I found:

In EAGLView.m initialization

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);


UIView is the base class from which both EAGLView and UIImageView derive. As you are using EAGLView and UIImageView, you are using UIView.

UIViews have subviews. Probably you'll have a UIView for which both the UIImageView and the EAGLView are subviews. The normal rule is that subviews are drawn in the order in which they are listed. So if your UIImageView is being drawn on top of your EAGLView then it is later in the list.

If you're creating them and adding them as subviews programmatically, then do so in a different order, or switch from using addSubview: to e.g. insertSubview:belowSubview: for the UIImageView.

If you're laying things out in Interface Builder then just ensure the EAGLView is listed after the UIImageView.


If u use GLKView all that need to do is

    view.opaque = NO;
    view.layer.opaque = NO;
    glClearColor(0.f,0.f,0.f,0.f);

Also for prepare displaying transparent texture

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

and if needed

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


Neither of above answers worked for me, I used this code modification to CCDirector.m and it worked ok:

-(void) setGLDefaultValues
{
    // This method SHOULD be called only after openGLView_ was initialized
    NSAssert( openGLView_, @"openGLView_ must be initialized");

    [self setAlphaBlending: YES];
    [self setDepthTest: YES];
    [self setProjection: projection_];

    // ***** NEW CODE: make open gl view transparent
    openGLView_.opaque = NO;
    glClearColor(0.0, 0.0, 0.0, 0.0);
    // ***** NEW CODE end

    // set other opengl default values
    //glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

#if CC_DIRECTOR_FAST_FPS
    if (!FPSLabel_) {
        CCTexture2DPixelFormat currentFormat = [CCTexture2D defaultAlphaPixelFormat];
        [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA4444];
        FPSLabel_ = [[CCLabelAtlas labelWithString:@"00.0" charMapFile:@"fps_images.png" itemWidth:16 itemHeight:24 startCharMap:'.'] retain];
        [CCTexture2D setDefaultAlphaPixelFormat:currentFormat];
    }
#endif  // CC_DIRECTOR_FAST_FPS

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜