渲染到纹理或离屏帧缓冲区

发布于 2024-08-31 20:19:03 字数 1490 浏览 6 评论 0原文

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

替代文本
(来源:imagehost.org
替代文本
(来源:imagehost.org

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

我创建帧缓冲区的代码:

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

终难遇 2024-09-07 20:19:03

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

附加到 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);

希望有帮助

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文