写入 OpenGL 多个渲染缓冲区失败
Write to OpenGL multiple renderbuffer failed
不确定是什么问题,写入多个渲染缓冲区时无法读取像素。这是代码:
设置代码:
// gen framebuffer object
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// gen renderbuffer 0
glGenRenderbuffers(1, &rbo_color0);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_color0);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo_color0);
// gen renderbuffer 1
glGenRenderbuffers(1, &rbo_color1);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_color1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RED_INTEGER, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rbo_color1);
// gen depth buffer
glGenRenderbuffers(1, &rbo_depth);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_depth);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
渲染和读取像素代码:
// setup shader and uniforms...
// bind framebuffer and clear color/depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glEnable(GL_DEPTH_TEST);
glClearColor(0.f, 0.f, 0.f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLuint buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, buffers);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, (void*)0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, data);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(x, y, 1, 1, GL_RED_INTEGER, GL_INT, (int*)(data) + 4);
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
我在输出的着色器中使用了布局限定符,应该没有任何问题。所以我认为缓冲区设置或渲染代码可能有问题。
当只有一个渲染缓冲区(rbo_color0)时,我成功读取了像素。主要区别是:
- renderbuffer1 没有生成代码;
- 渲染时使用glDrawBuffer(rbo_color0);
- rbo_color0的格式是GL_RGBA(仔细检查了相应glRenderbufferStorage/glReadBuffer调用中的格式设置,这里没问题);
- rbo_color1没有读取像素调用。
有帮助吗?
GL_RED_INTEGER
不是正确的内部格式,因为它可用于 glRenderbufferStorage
的第二个参数。
内部格式的正确枚举器常量是 GL_R32I
.
参见 OpenGL 4.6 API Compatibility Profile Specification; 8.26. TEXTURE IMAGE LOADS AND STORES; page 334
如果您要检查 OpenGL 错误 (glGetError
),则会收到 INVALID_ENUM
错误。
更改渲染缓冲区存储规格,解决问题:
glRenderbufferStorage(GL_RENDERBUFFER, GL_R32I, w, h);
注意,您应该通过 glCheckFramebufferStatus
:
检查帧缓冲区的完整性
GLenum fb_status = glCheckFramebufferStatus( GL_FRAMEBUFFER );
if ( fb_status != GL_FRAMEBUFFER_COMPLETE )
{
// error handling
}
不确定是什么问题,写入多个渲染缓冲区时无法读取像素。这是代码:
设置代码:
// gen framebuffer object
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
// gen renderbuffer 0
glGenRenderbuffers(1, &rbo_color0);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_color0);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo_color0);
// gen renderbuffer 1
glGenRenderbuffers(1, &rbo_color1);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_color1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RED_INTEGER, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rbo_color1);
// gen depth buffer
glGenRenderbuffers(1, &rbo_depth);
glBindRenderbuffer(GL_RENDERBUFFER, rbo_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, w, h);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo_depth);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
渲染和读取像素代码:
// setup shader and uniforms...
// bind framebuffer and clear color/depth buffer
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glEnable(GL_DEPTH_TEST);
glClearColor(0.f, 0.f, 0.f, 0.1f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const GLuint buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, buffers);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, (void*)0);
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadPixels(x, y, 1, 1, GL_RGB, GL_FLOAT, data);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(x, y, 1, 1, GL_RED_INTEGER, GL_INT, (int*)(data) + 4);
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
我在输出的着色器中使用了布局限定符,应该没有任何问题。所以我认为缓冲区设置或渲染代码可能有问题。 当只有一个渲染缓冲区(rbo_color0)时,我成功读取了像素。主要区别是:
- renderbuffer1 没有生成代码;
- 渲染时使用glDrawBuffer(rbo_color0);
- rbo_color0的格式是GL_RGBA(仔细检查了相应glRenderbufferStorage/glReadBuffer调用中的格式设置,这里没问题);
- rbo_color1没有读取像素调用。
有帮助吗?
GL_RED_INTEGER
不是正确的内部格式,因为它可用于 glRenderbufferStorage
的第二个参数。
内部格式的正确枚举器常量是 GL_R32I
.
参见 OpenGL 4.6 API Compatibility Profile Specification; 8.26. TEXTURE IMAGE LOADS AND STORES; page 334
如果您要检查 OpenGL 错误 (glGetError
),则会收到 INVALID_ENUM
错误。
更改渲染缓冲区存储规格,解决问题:
glRenderbufferStorage(GL_RENDERBUFFER, GL_R32I, w, h);
注意,您应该通过 glCheckFramebufferStatus
:
GLenum fb_status = glCheckFramebufferStatus( GL_FRAMEBUFFER );
if ( fb_status != GL_FRAMEBUFFER_COMPLETE )
{
// error handling
}