Hi,
I have the following situation: I have created a custom FBO and attached 2 color buffers to it:
//1st Color buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texId, 0);
//2nd Color buffer
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, width, height, 0, GL_RED_INTEGER, GL_INT, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, texId, 0);
And bound them with glDrawBuffers
GLenum buffers[]{ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, buffers)
I am using the 2nd color buffer write Ids to from one of the fragment shader (there are other shaders used in the render pass, but they don’t write to the second buffer):
//mesh_shader.frag
#version 330 core
layout(location = 0) out vec4 FragColor;
layout(location = 1) out int Id;
...
Id = -50 //Checking that I can write to it.
I am able to successfully write to the 2nd color buffer, however I am not able to clear it. I want to clear all of the values in that buffer to -1 so I can easily distinguish valid Ids.
I am using glClearBuffer
to do this:
//Clearing 1st Color buffer
static const float[] = {0.0f, 0.0f, 0.0f, 0.0f,};
glClearBufferfv(GL_COLOR, 0, clear_color)
//Clearing 2nd Color buffer
static const GLint clear_int = { -1, -1, -1, -1 };
glClearBufferiv(GL_COLOR, 1, &clear_int[0])
When I read from the 2nd Color buffer using the mouse position, I get -50 when I am over geometry rendered by mesh_shader.frag
(like I should), but random numbers ( like: 1057630335
, 1057423894
, etc) for the rest of the scene.
It seems that glClearBufferiv
hasn’t cleared the buffer.
All of my gl function calls are wrapped a macro which checks glGetError()
, but I am not getting any errors. Everything else is working appart from clearing the 2nd Color buffer.
Does anyone have any idea what is going wrong? Any help will be greatly appreciated!