0
votes

I'm currently drawing objects (images, rectangles) on iPhone with OpenGL ES 2.0.

There are two modes :

A) Without FBO :

  1. Draw objects
  2. Render to screen

B) With FBO

  1. Bind FBO
  2. Draw objects
  3. Render FBO to screen

And the scene draw order is :

  1. Draw background with red (or black) color (1, 0, 0, 1) with glClearColor
  2. Draw texture with transparency color (1, 1, 1, 0.5)

Here are the results (left without FBO, right with FBO) :

1) Image without transparency : both are same

image 1 image 1

2) Transparency set to 0.5, red background : both different

image 2 image 3

3) Transparency set to 0.5, black background : right same as 1) Without transparency

image 4 image 1

Here's how I create the 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));
1

1 Answers

1
votes

Found the problem, this line was the problem in my render-FBO-to-window function :

glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

I just removed it because I don't need alpha blending in this step.