透明的OpenGL CAEAGLLayer

时间:2012-10-18 10:47:21

标签: ios opengl-es eaglview caeagllayer

我试图将opengl视图(CAEAGLLayer)渲染为透明,因此我可以在其后面放置一个UIView。我正在使用以下设置来实现透明的opengl背景。

CAEAGLLayer* layer = (CAEAGLLayer*)eaglLayer;
layer.opaque = NO;
layer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
                            kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,
                            nil
                            ];

我的问题是现在我的opengl场景中的对象现在是透明的,我可以透过它们看到(大约50%的透明度)。

如何让场景中的对象保持透明,同时让场景中的对象不透明。

请注意,所有图层,UIViews和UIWindows都具有1.0的alpha值,并且场景中的片段着色器不会导致问题,因为当所有图像都是纯色时 layer.opaque设置为YES。我还应该补充一点,如果这对任何事都有帮助,这是一个统一项目。

编辑:

统一的一些代码设置。

int OpenEAGL_UnityCallback(UIWindow** window, int* screenWidth, int* screenHeight,  int* openglesVersion)
{
    CGRect rect = [[UIScreen mainScreen] bounds];

    // Create a full-screen window
    _window = [[UIWindow alloc] initWithFrame:rect];
    EAGLView* view = [[EAGLView alloc] initWithFrame:rect];
    UnityViewController *controller = [[UnityViewController alloc] init];
    sGLViewController = controller;

#if defined(__IPHONE_3_0)
    if( _ios30orNewer )
        controller.wantsFullScreenLayout = TRUE;
#endif

    controller.view = view;

    CreateSplashView( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? (UIView*)_window : (UIView*)view );
    CreateActivityIndicator(_splashView);

    // add only now so controller have chance to reorient *all* added views
    [_window addSubview:view];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
        [_window bringSubviewToFront:_splashView];

    if( !UnityUseOSAutorotation() )
    {
        _autorotEnableHandling = true;
        [[NSNotificationCenter defaultCenter] postNotificationName: UIDeviceOrientationDidChangeNotification object: [UIDevice currentDevice]];
    }

    // reposition activity indicator after we rotated views
    if (_activityIndicator)
        _activityIndicator.center = CGPointMake([_splashView bounds].size.width/2, [_splashView bounds].size.height/2);

    int openglesApi =
#if defined(__IPHONE_3_0) && USE_OPENGLES20_IF_AVAILABLE
    kEAGLRenderingAPIOpenGLES2;
#else
    kEAGLRenderingAPIOpenGLES1;
#endif

    for (; openglesApi >= kEAGLRenderingAPIOpenGLES1 && !_context; --openglesApi)
    {
        if (!UnityIsRenderingAPISupported(openglesApi))
            continue;

        _context = [[EAGLContext alloc] initWithAPI:openglesApi];
    }

    if (!_context)
        return false;

    if (![EAGLContext setCurrentContext:_context]) {
        _context = 0;
        return false;
    }

    const GLuint colorFormat = UnityUse32bitDisplayBuffer() ? GL_RGBA8_OES : GL_RGB565_OES;

    if (!CreateWindowSurface(view, colorFormat, GL_DEPTH_COMPONENT16_OES, UnityGetDesiredMSAASampleCount(MSAA_DEFAULT_SAMPLE_COUNT), NO, &_surface)) {
        return false;
    }

    glViewport(0, 0, _surface.w, _surface.h);
    [_window makeKeyAndVisible];
    [view release];

    *window = _window;
    *screenWidth = _surface.w;
    *screenHeight = _surface.h;
    *openglesVersion = _context.API;

    _glesContextCreated = true;

    return true;
}

void PresentSurface(EAGLSurfaceDesc* surface)
{
    if(_skipPresent)
    {
        UNITY_DBG_LOG ("SKIP PresentSurface:\n");
        return;
    }
    UNITY_DBG_LOG ("PresentSurface:\n");

    EAGLContext *oldContext = [EAGLContext currentContext];
    if (oldContext != _context)
        [EAGLContext setCurrentContext:_context];

    PreparePresentSurfaceGLES(surface);

    // presentRenderbuffer presents currently bound RB, so make sure we have the correct one bound
    GLES_CHK( glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->renderbuffer) );
    if(![_context presentRenderbuffer:GL_RENDERBUFFER_OES])
        printf_console("failed to present renderbuffer (%s:%i)\n", __FILE__, __LINE__ );

    AfterPresentSurfaceGLES(surface);

    if(oldContext != _context)
        [EAGLContext setCurrentContext:oldContext];
}


bool CreateSurface(EAGLView *view, EAGLSurfaceDesc* surface)
{
    CAEAGLLayer* eaglLayer = (CAEAGLLayer*)surface->eaglLayer;
    assert(eaglLayer == [view layer]);

    CGSize newSize = [eaglLayer bounds].size;
    newSize.width  = roundf(newSize.width);
    newSize.height = roundf(newSize.height);

#ifdef __IPHONE_4_0
    int resolution = UnityGetTargetResolution();

    if (    (resolution == kTargetResolutionNative || resolution == kTargetResolutionHD)
         && [view respondsToSelector:@selector(setContentScaleFactor:)]
         && [[UIScreen mainScreen] respondsToSelector:@selector(scale)]
       )
    {
            CGFloat scaleFactor = [UIScreen mainScreen].scale;
            [view setContentScaleFactor:scaleFactor];
            newSize.width = roundf(newSize.width * scaleFactor);
            newSize.height = roundf(newSize.height * scaleFactor);
            UnitySetInputScaleFactor(scaleFactor);
    }
#endif

    surface->w = newSize.width;
    surface->h = newSize.height;

    UNITY_DBG_LOG ("CreateWindowSurface: FBO\n");
    CreateSurfaceGLES(surface);
    GLES_CHK( glBindRenderbufferOES(GL_RENDERBUFFER_OES, surface->renderbuffer) );

    return true;
}

extern "C" bool AllocateRenderBufferStorageFromEAGLLayer(void* eaglLayer)
{
    return [_context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:(CAEAGLLayer*)eaglLayer];
}

extern "C" void InitEAGLLayer(void* eaglLayer, bool use32bitColor)
{
    CAEAGLLayer* layer = (CAEAGLLayer*)eaglLayer;

    const NSString* colorFormat = use32bitColor ? kEAGLColorFormatRGBA8 : kEAGLColorFormatRGB565;

    layer.opaque = NO;
    layer.drawableProperties =  [NSDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking,
                                    colorFormat, kEAGLDrawablePropertyColorFormat,
                                    nil
                                ];
}

2 个答案:

答案 0 :(得分:0)

用alpha<清除帧缓冲区。 1,即

glClearColor(r, g, b, alpha); // where alpha < 1
glClear(GL_COLOR_BUFFER_BIT | … );

要获得完全透明度,您需要alpha = 0。

答案 1 :(得分:0)

好的问题似乎与PSD有关。 Unity允许你加载psd的纹理。他们正在制造奇怪的透明度问题。我把它换成了jpegs,问题得到解决。