Display EAGLView with transparent background on UIImageView

I have an EAGLView displaying an OpenGL 3D model, and a UIImageView displaying an image.



I want to show an EAGLView over this UIImageView with a transparent background.

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

Any solution would be appreciated.

+3
source share
5 answers

Here is the solution I found:

In the initialization of EAGLView.m

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);
+8
source

For reference, most of the accepted answer is not needed. The only key part is (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

NIB, (Apple, , var GL- initWithCoder:()

, , OpenGL = 0.0. , glClear/glClearColor, clearColor :

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

UIView - , EAGLView, UIImageView. EAGLView UIImageView, UIView.

UIViews . , UIView, UIImageView EAGLView . , , . , UIImageView EAGLView, .

, addSubview: insertSubview: belowSubview: UIImageView.

Interface Builder, , EAGLView UIImageView.

+1

GLKView, , ,

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

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

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+1

, CCDirector.m :

-(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

All Articles