I am currently struggling with my framebuffer in my 2D C++/OpenGL Game and failing to see my issues. It will not store some of my textures and can’t be updated properly and I can’t seem to find the reason.
To make texture picking easier, I portray all my textures in a framebuffer texture and store the ID and color values of each texture there. Therefore I created this framebuffer class:
void Framebuffer::Awake()
{
glm::vec2 windowSize = GlobalHelper::GetCurrentWindowSize();
glm::vec4 viewportSize = GlobalHelper::GetViewportSize();
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
// color buffer
glCreateTextures(GL_TEXTURE_2D, 1, &colorAttachment);
glBindTexture(GL_TEXTURE_2D, colorAttachment);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, viewportSize.z, viewportSize.w, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorAttachment, 0);
//ID buffer
glCreateTextures(GL_TEXTURE_2D, 1, &IDAttachment);
glBindTexture(GL_TEXTURE_2D, IDAttachment);
glTexImage2D(GL_TEXTURE_2D, 0, GL_R32I, viewportSize.x, viewportSize.y, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, IDAttachment, 0);
GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, drawBuffers);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Framebuffer::Bind()
{
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClearTexImage(IDAttachment, 0, GL_RED_INTEGER, GL_INT, &defaultValue);
}
void Framebuffer::Unbind()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
I use these functions in my render loop to be able to move textures around, add and delete them and still have the actual values.
colorBuffer = Framebuffer();
colorBuffer.Awake();
while(!glfwWindowShouldClose(window)) {
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
colorBuffer.Bind();
BatchRenderer::ResetStats();
BatchRenderer::BeginBatch();
GlobalHelper::GetCurrentScene()->OnlyGameUpdate();
BatchRenderer::EndBatch();
BatchRenderer::Flush();
GetTexturePerMouseClick(&startMenuScene);
colorBuffer.Unbind();
void Scene::OnlyGameUpdate()
{
for (const auto& x : sceneTextures)
{
BatchRenderer::DrawQuad({x.position.x, x.position.y, 0.0f}, 0.0f + x.rotation,
{x.scale * (float)x.imageWidth, x.scale * (float)x.imageHeight}, x.ID, x.color, 0.0f, x.ID);
}
}
I am now having several issues with that.
-
issue: If I am trying to create another texture, such as a background texture, outside of the
OnlyGameUpdate()
function, the framebuffer will not store it and it can’t be seen in the framebuffer texture, but I actually need it to be able to store a background texture. If I load the image outside the function but then add it to thesceneTextures
in the scene class, it works just fine. -
issue: once I change the scene or the viewport and need to clear the framebuffer textures with:
glClearTexImage(IDAttachment, 0, GL_RED_INTEGER, GL_INT, &defaultValue);
glClearTexImage(colorAttachment, 0, GL_RGBA, GL_FLOAT, &defaultColor);
it remains the same defaultValues forever and can’t be filled anew.
I have no idea what is happening, does anybody see a logical error or something obviously wrong here?