2012-10-18 3 views
1

내가 그 뒤에 UIView를 넣을 수 있도록 OpenGL보기 (CAEAGLLayer)를 투명하게 렌더링하려고합니다. 투명한 OpenGL 백그라운드를 만들기 위해 다음 설정을 사용하고 있습니다.투명 OpenGL CAEAGLLayer

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

내 문제는 이제 내 OpenGL 화면의 개체가 투명 해져서 (약 50 %의 투명도로) 볼 수 있습니다.

장면의 객체를 투명하게 유지하면서 OpenGL 장면의 배경을 투명하게 만들려면 어떻게해야합니까?

layer.opaque가 YES로 설정된 경우 모든 레이어, UIViews 및 UIWindows의 알파 값은 1.0이며 장면의 조각 쉐이더는 모든 것을 단색으로 처리하므로 문제가 발생하지 않습니다. 또한이 프로젝트가 도움이된다면 이것이 하나의 프로젝트라고 덧붙여 야합니다.

편집 :

화합의 일부 코드가 설정되었습니다.

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 
           ]; 
} 
+0

투명도는 픽셀 화 된 모양을 나타냅니다 – user346443

답변

0

지우기 알파 = 0

+0

배경은 OpenGL 장면에서 완전히 투명합니다. 문제는 OpenGL 씬의 객체 (삼각형)가 이제 반투명하다는 것입니다. 어떻게 장면 내에서 삼각형을 풀 컬러로 유지하면서 배경을 완전히 투명하게 만들 수 있습니까? – user346443

+0

아, 이제 너를 잡았다. 흠, 완전히 "작동하는", 깨진 최소 예제 소스 코드 또는 적어도 관련 부분, 즉 GLLayer 설치, 도면 코드 설정 등을 게시 할 수 있습니까? – datenwolf

+0

그것의 일부를 숨기지 만 병이 최선을 다하는 Unity 프로젝트에서 나온 것입니다. 고마워, 정말 고마워. – user346443

0

확인 문제가 PSD의 함께있을 듯 당신이 원하는 전체의 투명성를 들어 알파 < 1 프레임 버퍼, 즉

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

. Unity를 사용하면 psd에서 텍스처를로드 할 수 있습니다. 그들은 이상한 투명성 문제를 만들고있었습니다. 나는 그들을 jpeg로 바꾸었고 문제는 해결되었다.

관련 문제