-2

I am trying to render a 3d pyramid inside a frame buffer to display it in ImGui window. But the depth testing fails and I get this:

enter image description here

Here's what I have:

glEnable(GL_DEPTH_TEST);
while (!glfwWindowShouldClose(mainwin))
{
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    ImGui_ImplOpenGL3_NewFrame();
    ImGui_ImplGlfw_NewFrame();
    ImGui::NewFrame();

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   

    fbo.bind();//framebufferobject bind

    glClearColor(0.8f, 0.8f, 0.8f, 1.0f);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* Render here */       
    defaultshader.Activate();

    // Handles camera inputs
    camera1.Inputs(mainwin);
    // Updates and exports the camera matrix to the Vertex Shader
    camera1.Matrix(45.0f, 0.1f, 100.0f, defaultshader, "camMatrix");

    tex1.Bind();
    VAO1.Bind();
    // Draw primitives, number of indices, datatype of indices, index of indices
    glDrawElements(GL_TRIANGLES, sizeof(indices) / sizeof(int), GL_UNSIGNED_INT, 0);
    //---------------------------------------------------------------------------------------------------------------------------
    fbo.unbind();

    ImGui::Begin("Viewport");

    AppImguiAddImage(fbtex.FBtexID);//adds image to imgui drawlist

    ImGui::End();

    AppImguiFrameEnd();

    /* Swap front and back buffers */
    glfwSwapBuffers(mainwin);

    /* Poll for and process events */
    glfwPollEvents();

}

Note: I got AppImguiAddImage() and Clear() originally from: https://gamedev.stackexchange.com/questions/150214/render-in-a-imgui-window

I have tried different combinations of color clear and depth clear for the two Clear() funtions in the while loop to get the same result.

What is going wrong?

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220

1 Answers1

1

Does your FBO have a depth buffer attachment?

no, ...

That's your problem. If your FBO doesn't have a GL_DEPTH_ATTACHMENT then the depth test will be disabled when you render to that FBO.

To set a GL_DEPTH_ATTACHMENT on an FBO, you can attach either a renderbuffer (using glFramebufferRenderbuffer) or a texture (using glFramebufferTexture). These renderbuffer/texture would need to be in one of the depth formats.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Thank you for responding. AppImguiAddImage() only accepts texture references so I cant use RBOs directly to draw image on imgui. Is there some way to use depth attachment on texture? – Subham Swostik Pradhan Sep 28 '22 at 18:47
  • 1
    @SubhamSwostikPradhan Please see [this link](https://learnopengl.com/Advanced-OpenGL/Framebuffers) regarding how to use a texture as a depth buffer with FBO. – G.M. Sep 28 '22 at 19:05
  • 2
    @SubhamSwostikPradhan you can use RBO for the depth while you're using a texture for the color buffer. Since imgui needs only the color, it's not a problem. If you insist on using a texture for depth too, the process of creating that is basically the same. – Yakov Galka Sep 28 '22 at 19:08
  • Thank you all! It works now. I can now attach depth to FBO :) – Subham Swostik Pradhan Sep 28 '22 at 19:15