在 UIImageView 上显示具有透明背景的 EAGLView

发布于 2024-10-21 18:32:49 字数 193 浏览 1 评论 0原文

我有一个显示 OpenGL 3D 模型的 EAGLView 和一个显示图像的 UIImageView。



我想要的是在具有透明背景的 UIImageView 之上显示 EAGLView。

有没有办法在 UIImageView 之上显示具有透明背景的 3D 模型。

任何解决方案将不胜感激。

I have an EAGLView displaying an OpenGL 3D model, and a UIImageView displaying 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

东走西顾 2024-10-28 18:32:49

作为参考,大多数已接受的答案都是不必要的。唯一的关键部分是这个(在 UIViewController 中 - 无需编辑或子类 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 似乎在 initWithCoder 中覆盖了 GL 视图的此变量) :( )

另外,当然,在您的 OpenGL 代码中,您需要清除 alpha=0.0,因此,无论您的 glClear / glClearColor 调用已经在哪里,请将clearColor 替换为:

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

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);
不弃不离 2024-10-28 18:32:49

这是我找到的解决方案:

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

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);
来日方长 2024-10-28 18:32:49

UIView 是 EAGLView 和 UIImageView 派生的基类。当您使用 EAGLView 和 UIImageView 时,您正在使用 UIView。

UIViews 有子视图。也许您将拥有一个 UIView,其中 UIImageView 和 EAGLView 都是子视图。通常的规则是子视图按照它们列出的顺序绘制。因此,如果您的 UIImageView 绘制在 EAGLView 之上,那么它会位于列表的后面。

如果您要以编程方式创建它们并将它们添加为子视图,请以不同的顺序执行此操作,或者从使用 addSubview: 例如 insertSubview:belowSubview: 的UIImageView。

如果您在 Interface Builder 中进行布局,那么只需确保 EAGLView 列在 UIImageView 之后即可。

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.

东风软 2024-10-28 18:32:49

如果你使用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);

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);
如痴如狂 2024-10-28 18:32:49

以上两个答案都不适合我,我对 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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文