渲染到纹理或屏幕外帧缓冲

时间:2022-06-24 03:20:19

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

我在iPhone上使用OpenGLES渲染纹理和屏幕外帧缓冲区时遇到问题。

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

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.

第一张图片显示直接渲染到CAEAGLLayer的麻将牌,这是正确的。第二个显示渲染到屏幕外帧缓冲区的切片,使用glCopyTexImage2D复制到纹理,并将纹理渲染到CAEAGLLayer。两者都使用白色不透明四边形作为背景。我也尝试直接渲染到纹理,但效果与屏幕外帧缓冲区相同。

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).

我使用VBO通过一次调用glDrawElements从纹理图集中绘制所有图块,以传递隔行扫描的顶点数据(坐标,纹理坐标,颜色)。我使用RGBA8888纹理格式,使用glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA)混合函数在两个三角形(四边形)上绘制每个图像。我不使用深度缓冲(在所有情况下)。

Can somebody tell me what could be the problem?

有人能告诉我可能是什么问题吗?

1 个解决方案

#1


0  

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.

似乎你还没有发布足够的代码,但是从发布的内容来看,似乎你将FBO中的纹理和渲染缓冲区的角色融合在一起。 (你的帖子标题说渲染到纹理)你也忘了定义你的绘制缓冲区。

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

附加到FBO的渲染缓冲区通常用作深度缓冲区。因此请尝试以下方法:

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

希望有所帮助

#1


0  

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.

似乎你还没有发布足够的代码,但是从发布的内容来看,似乎你将FBO中的纹理和渲染缓冲区的角色融合在一起。 (你的帖子标题说渲染到纹理)你也忘了定义你的绘制缓冲区。

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

附加到FBO的渲染缓冲区通常用作深度缓冲区。因此请尝试以下方法:

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

希望有所帮助