Render to texture or offscreen framebuffer

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

alt text http://j.imagehost.org/0923/IMG_0020.pngalt text http://j.imagehost.org/0725/IMG_0019.png

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?


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

链接地址: http://www.djcxy.com/p/33934.html

上一篇: 渲染到纹理和路径不消失

下一篇: 渲染到纹理或离屏帧缓冲区