FBO 在 OSX 中无法正常工作

发布于 2024-11-27 12:13:37 字数 2314 浏览 2 评论 0原文

我遇到这个问题有一段时间了,我找不到解决方案。

这里我初始化帧缓冲区:

//Initialize buffers
    glGenBuffers(1, &primaryBuffer);
    glBindBuffer(GL_FRAMEBUFFER, primaryBuffer);

    glGenRenderbuffers(1, &depthBuffer);
    glBindBuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, Statics::ScreenWidth, Statics::ScreenHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glGenTextures(1, &colorTexture);
    glBindTexture(GL_TEXTURE_2D, colorTexture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glGenTextures(1, &depthTexture);
    glBindTexture(GL_TEXTURE_2D, depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);

    int res = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (res == GL_FRAMEBUFFER_COMPLETE)
    {
        printf("GOOD!\n");
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

我以这种方式在渲染函数中使用它:

glBindFramebuffer(GL_FRAMEBUFFER, primaryBuffer);
glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

SetCamera();
DrawScene1();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetCamera();
DrawScene2();

这里应该发生的是“scene1”在附加的帧缓冲区纹理中渲染。并且“scene2”应该出现在屏幕上。 “scene2”确实显示在屏幕中,但是当我使用渲染的 Framebuffer 纹理时,会出现以下内容: 看起来像是一些 GPU 内存垃圾!:)

谁能告诉我出了什么问题吗?

I had this problem for some time, and I couldn't find a solution.

Here I initialize the Framebuffer:

//Initialize buffers
    glGenBuffers(1, &primaryBuffer);
    glBindBuffer(GL_FRAMEBUFFER, primaryBuffer);

    glGenRenderbuffers(1, &depthBuffer);
    glBindBuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, Statics::ScreenWidth, Statics::ScreenHeight);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glGenTextures(1, &colorTexture);
    glBindTexture(GL_TEXTURE_2D, colorTexture);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);

    glGenTextures(1, &depthTexture);
    glBindTexture(GL_TEXTURE_2D, depthTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32,  Statics::ScreenWidth, Statics::ScreenHeight, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTexture, 0);

    int res = glCheckFramebufferStatus(GL_FRAMEBUFFER);
    if (res == GL_FRAMEBUFFER_COMPLETE)
    {
        printf("GOOD!\n");
    }

    glBindFramebuffer(GL_FRAMEBUFFER, 0);

I use it in the render function this way:

glBindFramebuffer(GL_FRAMEBUFFER, primaryBuffer);
glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);

SetCamera();
DrawScene1();

glBindFramebuffer(GL_FRAMEBUFFER, 0);

glViewport(0, 0, Statics::ScreenWidth, Statics::ScreenHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SetCamera();
DrawScene2();

What supposed to happen here is that "scene1" is rendered in the attached Framebuffer texture. And "scene2" should show up in the screen.
"scene2" does show up in the screen, but when I use the rendered Framebuffer texture, this is what appears:
Looks like some GPU memory garbage! :)

Can anyone tell me what's wrong?

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

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

发布评论

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

评论(1

成熟稳重的好男人 2024-12-04 12:13:37

从您所写的内容中,我了解到 DrawScene2() 应该渲染整个场景,而 DrawScene1() 应该渲染为 DrawScene2() 生成的部分内容。

由于您没有透露 DrawScene2() 的任何细节(或整个代码),我只能在这里做出有根据的猜测:问题将出在 DrawScene2 () 上。由于您在这里使用 DrawScene1() 的结果作为渲染源,您可能只是将整个 FBO 拍在渲染目标上,而不是将其视为常规纹理,即在它获取之前为其设置正确的世界和纹理坐标渲染(即由 DrawScene2() 渲染的门的边界,您想要用 DrawScene1() 生成的内容“绘制”)。

From what you wrote I have understood that DrawScene2() should render the entire scene, while DrawScene1() should render into a part of what DrawScene2() has produced.

Since you haven't given away any details (or the entire code) of DrawScene2(), I can only make an educated guess here: The problem will be DrawScene2 (). Since you are using the result of DrawScene1() as render source here, you probably just slapped the entire FBO on the render target, instead of treating it as a regular texture, i.e. setting up the proper world and texture coordinates for it before it gets rendered (i.e. the boundaries of the door rendered by DrawScene2() that you want to "paint" with what DrawScene1() has produced).

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