附加 RBO 时 FBO 不完整

Incomplete FBO when attaching RBO

我正在尝试将 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

已解决

我是按照这个教程一步一步来的: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 错误。

似乎在不设置某些纹理过滤参数的情况下添加深度缓冲区是行不通的。 (然而,当只有一个颜色缓冲区时,它起作用了,令人惊讶)