简体   繁体   English

具有透明背景的OpenGL ES 2.0 GLKit

[英]OpenGL ES 2.0 GLKit with transparent background

Is there a way to make the background of a GLKView transparent? 有没有办法让GLKView的背景透明? I've tried the solution here but that isn't working for me. 我在这里尝试了解决方案但这对我不起作用。

*EDIT: I need help making it completely transparent. *编辑:我需要帮助使它完全透明。 The background is mostly white and gray, but I just tested it with more vibrant colors, and sure enough you can vaguely see through to the background. 背景主要是白色和灰色,但我只是用更鲜艳的色彩测试它,当然你可以模糊地看到背景。 Any ideas why it would be partly transparent but not fully with the following code? 任何想法为什么它将部分透明但不完全使用以下代码?

Here's the code in my view controller: 这是我的视图控制器中的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2] autorelease];
    CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.view.layer;
    eaglLayer.opaque = NO;

    if (!self.context) {
       NSLog(@"Failed to create ES context");
    }

    GLKView *view = (GLKView *)self.view;
    view.context = self.context;

    view.backgroundColor = [UIColor clearColor];

    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    [self setupGL];
}

- (void)setupGL
{
    [EAGLContext setCurrentContext:self.context];

    self.effect = [[[GLKBaseEffect alloc] init] autorelease];
    self.effect.light0.enabled = GL_FALSE;
    self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 0.0f);
    self.effect.light0.ambientColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 0.0f);

    glDisable(GL_DEPTH_TEST);

    glGenVertexArraysOES(1, &_vertexArray);
    glBindVertexArrayOES(_vertexArray);

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_DYNAMIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, VERTEX_POS_DATA_SIZE, GL_FLOAT, GL_FALSE, VERTEX_DATA_SIZE * sizeof(GLfloat), BUFFER_OFFSET(0));

    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, VERTEX_COLOR_DATA_SIZE, GL_FLOAT, GL_FLOAT, VERTEX_DATA_SIZE * sizeof(GLfloat), BUFFER_OFFSET(VERTEX_POS_DATA_SIZE * sizeof(GLfloat)));    

    glLineWidth(10.0);
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    if (!needsRedraw) return;
    needsRedraw = NO;

    glClearColor(0.65f, 0.65f, 0.65f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    [self.effect prepareToDraw];

    glDrawArrays(GL_LINE_STRIP, 0, vertexCount);    
}

I've tried setting the backgroundColor of the to [UIColor clearColor] and setting the eaglLayer to not be opaque. 我已经尝试将backgroundColor设置为[UIColor clearColor]并将eaglLayer设置为不透明。 Heck, I even tried setting the backgroundColor of the eaglLayer to CGColorRef with 0 opacity. 哎呀,我甚至尝试将eaglLayer的backgroundColor设置为具有0不透明度的CGColorRef。

Well, I didn't think this would matter because the alpha was already at 0.0f, but when I changed 嗯,我不认为这很重要,因为alpha已经是0.0f,但是当我改变时

glClearColor(0.65f, 0.65f, 0.65f, 0.0f);

to

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

it made the background perfectly transparent. 它使背景完全透明。

Thanks everyone for your suggestions and help! 感谢大家的建议和帮助!

For me it helped to make the view not opaque: 对我来说,它有助于使视图不透明:

view.opaque = NO

in addition to the glClearColor call with transparancy of course. 除了glClearColor调用当然还有透明度。

A transparancy of 0.5f perfectly blends the background over another UIImageview with half opacity. 0.5f的透明度将背景与另一个具有半透明度的UIImageview完美地融合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM