gpt4 book ai didi

opengl - FBO Blitting 不工作

转载 作者:行者123 更新时间:2023-12-04 15:25:49 28 4
gpt4 key购买 nike

我正在尝试将多采样场景渲染为纹理,这是我正在使用的代码。我得到一个黑屏。我在 init 结束时检查了 fbo 的完整性,他们报告两个 fbo 都是完整的。

void init_rendered_FBO() {
glGenFramebuffers(1,&fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glGenTextures(1,&fbo_tex);
glBindTexture(GL_TEXTURE_2D, fbo_tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width_screen, height_screen, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, width_screen, height_screen);
glBindTexture (GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fbo_tex, 0);
int objectType;
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,&objectType);
ASSERT(objectType == GL_TEXTURE);
int objectName;
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,&objectName);
ASSERT(glIsTexture(objectName) == GL_TRUE);
int wid, hei, fmt;
glBindTexture(GL_TEXTURE_2D, objectName);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &wid);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &hei);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
&fmt);
glBindTexture(GL_TEXTURE_2D, 0);
std::string format = convertInternalFormatToString(fmt);
std::cout << "Color attachment 0: " << objectName << " " << wid << "x" << hei << ", " << format << std::endl;
ASSERT(checkFramebufferStatus());
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}

// this is the init function that gets called.
void init_rendered_multisample_FBO() {
init_rendered_FBO();
// now I'm going to set up the additional component necessary to perform multisampling which is a new fbo
// that has a multisampled color buffer attached. I won't need a multisample depth buffer.
glGenFramebuffers(1,&multi_fbo);
glBindFramebuffer(GL_FRAMEBUFFER,multi_fbo);
glGenRenderbuffers(1,&renderbuffer_multi);
glBindRenderbuffer(GL_RENDERBUFFER,renderbuffer_multi);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA, width_screen,height_screen);
glBindRenderbuffer(GL_RENDERBUFFER,0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer_multi);
int objectType;
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,&objectType);
ASSERT(objectType == GL_RENDERBUFFER);
int objectName;
glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,&objectName);
ASSERT(glIsRenderbuffer(objectName) == GL_TRUE);
glBindRenderbuffer(GL_RENDERBUFFER,objectName);
int wid,hei,fmt,sam;
glGetRenderbufferParameteriv(GL_RENDERBUFFER,GL_RENDERBUFFER_WIDTH,&wid);
glGetRenderbufferParameteriv(GL_RENDERBUFFER,GL_RENDERBUFFER_HEIGHT,&hei);
glGetRenderbufferParameteriv(GL_RENDERBUFFER,GL_RENDERBUFFER_INTERNAL_FORMAT,&fmt);
glGetRenderbufferParameteriv(GL_RENDERBUFFER,GL_RENDERBUFFER_SAMPLES,&sam);
glBindRenderbuffer(GL_RENDERBUFFER,0);
printf("Renderbuffer: %dx%d, fmt=%d, samples=%d\n",wid,hei,fmt,sam);
ASSERT(checkFramebufferStatus());
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
// this is called after rendering to multi_fbo
void resolve_multisample_FBO() {
glBindFramebuffer(GL_READ_FRAMEBUFFER, multi_fbo);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
GLenum drawbuf = GL_COLOR_ATTACHMENT0;
glDrawBuffers(1,&drawbuf);
glBlitFramebuffer(0,0,width_screen,height_screen,0,0,width_screen,height_screen,GL_COLOR_BUFFER_BIT,GL_NEAREST);
}

你能看出我遗漏了什么吗?我认为问题可能出在 glFramebufferRenderbuffer称呼。我尝试将第一个参数切换为 GL_READ_FRAMEBUFFER但它没有解决。

我查看 glGetError并且没有错误。如果我设置错误,肯定会失败并给我 INVALID_ENUMINVALID_OPERATION帮助我缩小问题的范围。

我使用此代码的方式是,为了启用多重采样,我必须更改的是在绘制时绑定(bind) multi_fbo,然后调用将绑定(bind)和 blit 的解析函数。之后,我的 fbo 纹理(当我直接渲染到它时效果很好)现在应该包含多重采样渲染。但它只是黑色的。我也打过 glEnable(GL_MULTISAMPLE)在初始化代码的另一部分。

我现在将尝试将非多重采样纹理blit 到另一个纹理以确保它有效。希望这将帮助我缩小我搞砸的地方。

更新:事实证明,将常规 FBO 复制到另一个相同的 FBO(两者都具有附加到颜色附件 0 的纹理)会产生相同的黑屏。 blit 根本行不通。

同样奇怪的是,一旦我尝试进行 blit,然后绘制 SOURCE fbo 的纹理,它仍然是全黑的。这就像试图破坏一切一样。

有人知道或有任何 FBO blitting 代码吗?我找不到任何东西,但我知道人们已经得到了多重采样 FBO 的工作。

您可以通过以下方式提供帮助:如果您有调用 glBlitFramebuffer 的代码在任何时候,我都希望看到其他要求设置该操作的电话。一旦我调用它,我似乎什么也得不到,除了黑色缓冲区和纹理。

更新:Blit to backbuffer 工作,即使是多重采样!这当然不需要任何设置,因为我只是绑定(bind)帧缓冲区 0。所以问题似乎是我设置了 FBO,它有一个应该被 blitted 的纹理附件。

最佳答案

事实证明,除了未能重置帧缓冲区绑定(bind)之外,我没有做错任何事情。

void resolve_multisample_FBO() 结尾我只需要一个glBindFramebuffer(GL_FRAMEBUFFER, 0);
我向你保证,这是浪费 3 个小时的可怕方式。但是多重采样看起来很棒,而且弥补了它的不足。

关于opengl - FBO Blitting 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9248223/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com