6
votes

I have a problem with rendering to texture and offscreen framebuffer with OpenGLES on iPhone.

alt text
(source: imagehost.org)
alt text
(source: imagehost.org)

First image shows mahjong tiles rendered to CAEAGLLayer directly and this is correct. Second one shows tiles rendered to offscreen framebuffer, copied to texture using glCopyTexImage2D and the texture rendered to CAEAGLLayer. Both use white opaque quad for background. I also have tried rendering directly to texture but effect was the same as with offscreen framebuffer.

My code for creating framebuffer:

    GLuint framebuffer;
    glGenFramebuffersOES(1, &framebuffer);
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

    GLuint renderbuffer;
    glGenRenderbuffersOES(1, &renderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_RGB8_OES,
            512, 512);

    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES,
            GL_RENDERBUFFER_OES, renderbuffer);

I draw all tiles from a texture atlas with one call to glDrawElements using VBO for passing interlaced vertex data (coordinates, texture coordinate, color). I use RGBA8888 texture format, drawing each image on two triangles (quad) with glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) blending function. I do not use depth buffer (in all cases).

Can somebody tell me what could be the problem?

1
What is incorrect about the images rendered? What do you expect them to look like?Frogblast
@Frogblast The first one, which is not rendered to offscreen framebuffer nor texture, is correct.sfider
Not enough code shown. The FBOs probably are not the problem.Razzupaltuff
probably a blendmode problem; check alpha channel and blendmode settings (or post code here)user431954

1 Answers

0
votes

It seems that you haven't posted enough of the code but from what is posted it seems that you fused together the roles of the texture and renderbuffer in your FBO. (your post title said render to texture) Also you forgot to define your draw buffers.

The render buffer attached to an FBO is usually used as a Depth Buffer. Therefore try the following:

GLuint framebuffer;
glGenFramebuffersOES(1, &framebuffer);
glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);

//set up the Depth Buffer
GLuint renderbuffer;
glGenRenderbuffersOES(1, &renderbuffer);
glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT24,
        512, 512);

glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT,
        GL_RENDERBUFFER_OES, renderbuffer);
//set up the texture that you will be rendering to
glActiveTexture(GL_TEXTURE0);
GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//add various other texture parameters here
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8_OES, 512,512);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
// define your draw buffers
GLenum drawBuffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, drawBuffers);

hope that helps