Roger Tello
Roger Tello

Reputation: 33

Rendering viewport to texture produces overlaps viewports?

I'm creating a 2D Engine and I want to implement docking, so I need to create a viewport and render the screen to a texture.

To render the viewport I'm saving the framebuffer into a FrameBufferObject and drawing as normally, I used this technique time ago and it worked with no problems, here is the Draw code:

    glBindFramebuffer(GL_FRAMEBUFFER, fbo_msaa_id);
    glViewport(0, 0, width, height);

    DrawRoomObjects();

    glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo_msaa_id);
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo_id);
    glBlitFramebuffer(0, 0, width, height,  // src rect
        0, 0, width, height,  // dst rect
        GL_COLOR_BUFFER_BIT, // buffer mask
        GL_LINEAR); // scale filter

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, App->moduleWindow->screen_surface->w, App->moduleWindow->screen_surface->h);

I've made shure the DrawRoomsObjects() function is working correctly, and FBO is initialized correctly.

Here is the code to render the texture created using ImGui library:

glEnable(GL_TEXTURE_2D);


if (ImGui::Begin("Game Viewport", &visible, ImGuiWindowFlags_MenuBar)) {
        ImGui::Image(viewportTexture->GetTextureID());
}

Before this chunk I make some calculations to fit the image to the dock, I'm not using viewportTexture any more on the code.

The problem comes when I get this weird artifact at the time of moving the quad, which I don't know how to call, click this link to see a gif of the bug.

It seems the texture is not cleaning the data correctly...?

Upvotes: 3

Views: 615

Answers (1)

Rabbid76
Rabbid76

Reputation: 210948

You've to clear the framebuffer, before you render the objects to the framebuffer:

glBindFramebuffer(GL_FRAMEBUFFER, fbo_msaa_id);
glViewport(0, 0, width, height);

glClear(GL_COLOR_BUFFER_BIT);
DrawRoomObjects();

Upvotes: 1

Related Questions