上周做图像的离屏渲染,图像始终是黑色的。始终不可解,今天早上偶尔调试突然好了。找了下原因
原因是执行离屏渲染的顺序错误导致的。
执行顺序应该如下
-(void)offScreenRender:(void(^)(void)) performBlock{
glBindFramebuffer(GL_FRAMEBUFFER, self.mExtraFBOID
);
if (performBlock) {
performBlock();
}
glBindFramebuffer(GL_FRAMEBUFFER, self.mDefaultFBO
);
}
这个performBlock 里面执行的应该是清理屏幕和缓冲区,这样可以重新设置顶点属性。在执行相关操作。
大概是这样的
[self.frameBuffer offScreenRender:^{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
self.angleVertex = [[AGLKVertexAttribArrayBuffer alloc]initWithAttribStride:sizeof(GLfloat)*7 numberOfVertices:3 bytes:angleVertexs usage:GL_STATIC_DRAW];
[self.angleVertex prepareToDrawWithAttrib:GLKVertexAttribPosition numberOfCoordinates:2 attribOffset:0 shouldEnable:YES];
[self.angleVertex prepareToDrawWithAttrib:GLKVertexAttribColor numberOfCoordinates:3 attribOffset:sizeof(GLfloat)*2 shouldEnable:YES];
[self.angleVertex prepareToDrawWithAttrib:GLKVertexAttribTexCoord0 numberOfCoordinates:2 attribOffset:sizeof(GLfloat)*5 shouldEnable:YES];
[self.mExtraEffect prepareToDraw];
[self.angleVertex drawArrayWithMode:GL_TRIANGLES startVertexIndex:0 numberOfVertices:3];
}];
黑屏的主要原因是 我先
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
接着执行了
glBindFramebuffer(GL_FRAMEBUFFER, self.mExtraFBOID );
切换了frameBuffer 导致的