有没有办法使 GLKView 的背景透明?我在这里尝试了解决方案,但这对我不起作用。
*编辑:我需要帮助使其完全透明。背景大多是白色和灰色,但我只是用更鲜艳的颜色测试了它,果然你可以隐约看到背景。任何想法为什么它会部分透明但不完全使用以下代码?
这是我的视图控制器中的代码:
- (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);
}
我尝试将 backgroundColor 设置为 [UIColor clearColor] 并将 eaglLayer 设置为不透明。哎呀,我什至尝试将 eaglLayer 的 backgroundColor 设置为不透明度为 0 的 CGColorRef。