附加RBO时FBO不完整

时间:2019-06-19 18:47:28

标签: c++ opengl fbo depth-buffer

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

...并且没有错误,但是深度测试根本没有用。

EDIT:这仅仅是因为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

1 个答案:

答案 0 :(得分:0)

已解决

我逐步按照本教程操作: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错误。

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