OpenGL ES 2.0 渲染到纹理

发布于 2024-12-20 13:42:48 字数 1604 浏览 2 评论 0原文

我正在尝试使用 OpenGL ES 2.0 渲染纹理,但似乎无法使其工作。

这就是我的处理方式:

    struct RenderTexture
    {
        GLuint framebuffer;
        GLuint tex;
        GLint old_fbo;


        RenderTexture(GLuint width, GLuint height)
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);

            glGenFramebuffers(1, &framebuffer);
            glGenTextures(1, &tex);

            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
            glBindTexture(GL_TEXTURE_2D, tex);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
                         width, height, 0, GL_RGBA, 
                         GL_UNSIGNED_BYTE, NULL);
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
                                   tex, 0);

            glClearColor(1, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);

            GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
            if (status != GL_FRAMEBUFFER_COMPLETE) {
                cout << status << endl; // this is not called
            }

            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }

        void begin()
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
        }

        void end()
        {
            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }
    };

但是当我尝试在其上绘图并使用生成的纹理时,纹理被绘制为全黑。

如果我不将绘图代码包装在 render_tex->begin();render_tex->end(); 中,则所有内容都会正确绘制,导致我相信问题与上面的代码无关。

I'm trying to render to a texture using OpenGL ES 2.0, but I can't seem to make it work.

This is how I proceed:

    struct RenderTexture
    {
        GLuint framebuffer;
        GLuint tex;
        GLint old_fbo;


        RenderTexture(GLuint width, GLuint height)
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);

            glGenFramebuffers(1, &framebuffer);
            glGenTextures(1, &tex);

            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
            glBindTexture(GL_TEXTURE_2D, tex);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 
                         width, height, 0, GL_RGBA, 
                         GL_UNSIGNED_BYTE, NULL);
            glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 
                                   tex, 0);

            glClearColor(1, 0, 0, 1);
            glClear(GL_COLOR_BUFFER_BIT);

            GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
            if (status != GL_FRAMEBUFFER_COMPLETE) {
                cout << status << endl; // this is not called
            }

            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }

        void begin()
        {
            glGetIntegerv(GL_FRAMEBUFFER_BINDING, &old_fbo);
            glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
        }

        void end()
        {
            glBindFramebuffer(GL_FRAMEBUFFER, old_fbo);
        }
    };

But when I try drawing on it and using the resulting texture, the texture is drawn as totally black.

If I just don't wrap the drawing code in render_tex->begin(); and render_tex->end();, everything draws correctly, leading me to believe that the problem is isolated to the code above.

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

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

发布评论

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

评论(4

谁许谁一生繁华 2024-12-27 13:42:48

在尝试渲染纹理之前,请确保纹理未绑定。即使根本不使用纹理,尝试渲染到当前绑定的纹理也可能会调用未定义的行为并且不起作用。

实际上,您应该在 RenderTexture 构造函数中的 glTexImage2D 之后调用 glBindTexture(GL_TEXTURE_2D, 0),或者像您一样恢复之前绑定的纹理与固定基地运营基地 (FBO) 一起。只需确保渲染到 FBO 时 tex 未绑定即可。

Make sure the texture is not bound before trying to render into it. Even if not using texturing at all, trying to render into a currently bound texture may invoke undefined behaviour and just not work.

You should actually call glBindTexture(GL_TEXTURE_2D, 0) after the glTexImage2D in your RenderTexture constructor, or maybe restore the previously bound texture, like you do with the FBO. Just make sure the tex is not bound when you render into the FBO.

森林迷了鹿 2024-12-27 13:42:48

这已经有一段时间了,但正如我在评论中所写,请确保您正在初始化 2 的幂纹理。

This has been a while, but as I wrote in the comments, be sure you're initializing a power of 2 texture.

全部不再 2024-12-27 13:42:48

我遇到了和你完全相同的问题。尝试

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

glBindTexture(GL_TEXTURE_2D, tex); 之后添加,黑色方块必须消失。

I had exact the same problem as u do. Try to add

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

right after glBindTexture(GL_TEXTURE_2D, tex); and black square have to disappear.

御守 2024-12-27 13:42:48

您似乎没有使用glActiveTexture。我建议您在每个 glBindTexture(tex); 之前调用 glActiveTexture(GL_TEXTURE0+tex);,这样当您使用多个纹理时,这将为您省去很多麻烦。我猜错误出在您用于在屏幕上绘制纹理的代码中。

You don't seem to make use of glActiveTexture. I recommend you to call glActiveTexture(GL_TEXTURE0+tex); before each glBindTexture(tex);, which will save you a lot of headaches when you use more than one texture. I guess the error is in the code you use for drawing the texture on the screen.

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