![](/img/trans.png)
[英]OpenGl ES 2.0 and GLKit : From GLKBaseEffect shaders to OpenGl
[英]OpenGL ES 2.0 GLKit with transparent background
有沒有辦法讓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。
嗯,我不認為這很重要,因為alpha已經是0.0f,但是當我改變時
glClearColor(0.65f, 0.65f, 0.65f, 0.0f);
至
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
它使背景完全透明。
感謝大家的建議和幫助!
對我來說,它有助於使視圖不透明:
view.opaque = NO
除了glClearColor調用當然還有透明度。
0.5f的透明度將背景與另一個具有半透明度的UIImageview完美地融合在一起。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.