附加RBO时FBO不完整

qlckcl4x  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(233)

我尝试将RenderBuffer对象附加到FrameBuffer对象,但glCheckFramebufferStatus(GL_FRAMEBUFFER)一直向我返回错误代码:GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT

(我正在尝试附加DepthBuffer)

以下是我的代码(简化):

// Create and bind the new FBO (1920x1080)

GLuint gBuffer; 
glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);

// Create, initialize, bind and attach the new RBO (purpose : depth buffering)

GLuint rbo;
glGenRenderbuffers(1, &rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);

// Create, bind, and initialize, and attach a texture (the FBO's Color Buffer)

GLuint idMap;
glGenTextures(1, &idMap);
glBindTexture(GL_TEXTURE_2D, idMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, idMap, 0);

std::cout << std::to_string(glCheckFramebufferStatus(GL_FRAMEBUFFER)) << std::endl; // Gives me 36054 ( incomplete ???)

// Re-binding orignal FBO and RBO

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);

我做错了什么?

我注意到的事情

我不小心写了

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_COMPONENT16, GL_RENDERBUFFER, rbo);

而不是

glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo);

...没有错误,但深度测试根本不起作用。

编辑:很简单,因为GL_DEPTH_ATTACHMENT是附加缓冲区所必需的。它刚从帧缓冲区中出来...

在FBO中渲染时,我执行以下操作:

glBindFramebuffer(GL_FRAMEBUFFER, gBuffer); // Use secondary FBO
glBindRenderbuffer(GL_RENDERBUFFER, rbo);   // Use secondary DepthBuffer
glDepthMask(GL_TRUE);                       // Allow depth writing
glEnable(GL_DEPTH_TEST);                    // Enable Depth testing
glDepthFunc(GL_LESS);                       // Pass if fragment is nearer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color and depth buffers

Render(); 

glBindFramebuffer(GL_FRAMEBUFFER, 0);       // Go back to original FBO  
glBindRenderbuffer(GL_RENDERBUFFER, 0);     // to original Depth Buffer
lnxxn5zx

lnxxn5zx1#

我一步一步地遵循了本教程:http://www.opengl-tutorial.org/fr/intermediate-tutorials/tutorial-14-render-to-texture/

当以下代码块丢失时,会出现错误:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

它是纹理(颜色缓冲区)的参数。特别是,为了避免GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT错误,只需要第二行。

在不设置某些纹理过滤参数的情况下添加深度缓冲区似乎不起作用。(但是,当只有一个颜色缓冲区时,它可以工作,即冲印)

相关问题