我目前正在使用OpenGL ES 2.0在iPhone上绘制对象(图像,矩形)。
有两种模式:
A)没有FBO:
B)使用FBO
场景绘制顺序是:
以下是结果(左边没有FBO,右边是FBO):
1)没有透明度的图像:两者都相同
2)透明度设置为0.5,红色背景:两者都不同
3)透明度设置为0.5,黑色背景:右侧与1相同)无透明度
以下是我创建FBO的方法:
GLint maxRenderBufferSize;
glGetIntegerv(GL_MAX_RENDERBUFFER_SIZE, &maxRenderBufferSize);
GLuint textureWidth = (GLuint)self.size.width;
GLuint textureHeight = (GLuint)self.size.height;
if(maxRenderBufferSize <= (GLint)textureWidth || maxRenderBufferSize <= (GLint)textureHeight)
@throw [NSException exceptionWithName:TAG
reason:@"FBO cannot allocate that much space"
userInfo:nil];
glGenFramebuffers(1, &fbo);
glGenRenderbuffers(1, &fboBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1, &fboTexture);
glBindTexture(GL_TEXTURE_2D, fboTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fboTexture, 0);
glBindRenderbuffer(GL_RENDERBUFFER, fboBuffer);
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if(status != GL_FRAMEBUFFER_COMPLETE)
@throw [NSException exceptionWithName:TAG
reason:@"Failed to initialize fbo"
userInfo:nil];
这是我的片段着色器:
gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));
答案 0 :(得分:1)
发现问题,这行是我的render-FBO-to-window函数中的问题:
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
我刚删除它,因为我不需要在此步骤中进行Alpha混合。