简体   繁体   English

OpenGL ES-带有或不带有FBO的颜色都不同

[英]OpenGL ES - Colors are different with or without FBO

I'm currently drawing objects (images, rectangles) on iPhone with OpenGL ES 2.0. 我目前正在使用OpenGL ES 2.0在iPhone上绘制对象(图像,矩形)。

There are two modes : 有两种模式:

A) Without FBO : A)没有FBO:

  1. Draw objects 绘制对象
  2. Render to screen 渲染到屏幕

B) With FBO B)使用FBO

  1. Bind FBO 绑定FBO
  2. Draw objects 绘制对象
  3. Render FBO to screen 将FBO渲染到屏幕

And the scene draw order is : 场景绘制顺序为:

  1. Draw background with red (or black) color (1, 0, 0, 1) with glClearColor 使用glClearColor用红色(或黑色)(1、0、0、1)绘制背景
  2. Draw texture with transparency color (1, 1, 1, 0.5) 绘制具有透明颜色的纹理(1、1、1、0.5)

Here are the results (left without FBO, right with FBO) : 结果如下(左为无FBO,右为FBO):

1) Image without transparency : both are same 1)不透明的图像:两者相同

图片1 图片1

2) Transparency set to 0.5, red background : both different 2)透明度设为0.5,红色背景:两者都不同

图片2 图片3

3) Transparency set to 0.5, black background : right same as 1) Without transparency 3)透明度设为0.5,黑色背景:与1相同,无透明度

图片4 图片1

Here's how I create the FBO : 这是我创建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];

Here's my fragment shader : 这是我的片段着色器:

gl_FragColor = (v_Color * texture2D(u_Texture, v_TexCoordinate));

Found the problem, this line was the problem in my render-FBO-to-window function : 找到了问题,这行是我的render-FBO-to-window函数中的问题:

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

I just removed it because I don't need alpha blending in this step. 我刚刚删除了它,因为在此步骤中不需要alpha混合。

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

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