pthom / imgui_bundle

Dear ImGui Bundle: an extensive set of Ready-to-use widgets and libraries, based on ImGui. Start your first app in 5 lines of code, or less. Whether you prefer Python or C++, this pack has your back!
https://pthom.github.io/imgui_bundle/
MIT License
592 stars 63 forks source link

Fix modal window rendering in pyopengl backend (Fix #156) #157

Closed Aman-Anas closed 6 months ago

Aman-Anas commented 6 months ago

To address issue #156 .

pthom commented 6 months ago

The weird thing is, I tried it like (idx_buffer_offset + command.idx_offset * imgui.INDEX_SIZE) and it seems to break rendering all over the place. The link does mention that some backends try and calculate the offsets manually, which may be what's happening here with idx_buffer_offset being incorrect.

Based on info here https://github.com/ocornut/imgui/issues/4845 apparently the start offsets are no longer simply the sum of the ElemCount's, and that's essentially how idx_buffer_offset is calculated right now. Still need to test, but I think idx_buffer_offset may not be needed. It could also be that since idx_buffer is already initialized to 0 in the ProgrammablePipeline it doesn't affect anything, and maybe the FixedPipeline is different.

I opened a pull request for the version using command.idx_offset * imgui.INDEX_SIZE

This is strange. I would need more info in order to make sure the correction is OK Could you provide an minimal reproducible example where the issue occurs?

Aman-Anas commented 6 months ago

Here's a minimal reproducible example for the modal issue, just modified the example glfw3 backend script a little. The modal window is grayed on the inside before the renderer fix, and grayed on the outside afterwards.

from imgui_bundle.python_backends.glfw_backend import GlfwRenderer
import OpenGL.GL as gl  # type: ignore
from imgui_bundle import imgui
import glfw  # type: ignore
import sys

def main():
    imgui.create_context()
    window = impl_glfw_init()
    impl = GlfwRenderer(window)

    show_custom_window = True

    while not glfw.window_should_close(window):
        glfw.poll_events()
        impl.process_inputs()

        imgui.new_frame()

        if show_custom_window:
            is_expand, show_custom_window = imgui.begin("Custom window", True)
            if is_expand:
                if imgui.button("Open modal"):
                    imgui.open_popup("Test Modal")

                modal_expand, _ = imgui.begin_popup_modal("Test Modal", True)
                if modal_expand:
                    imgui.text("hello there")
                    imgui.text("with the bug, this window is grayed out")
                    imgui.end_popup()

            imgui.end()

        gl.glClearColor(1.0, 1.0, 1.0, 1)
        gl.glClear(gl.GL_COLOR_BUFFER_BIT)

        imgui.render()
        impl.render(imgui.get_draw_data())
        glfw.swap_buffers(window)

    impl.shutdown()
    glfw.terminate()

def impl_glfw_init():
    width, height = 1280, 720
    window_name = "minimal ImGui/GLFW3 example"

    if not glfw.init():
        print("Could not initialize OpenGL context")
        sys.exit(1)

    # OS X supports only forward-compatible core profiles from 3.2
    glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
    glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 3)
    glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)

    glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, gl.GL_TRUE)

    # Create a windowed mode window and its OpenGL context
    window = glfw.create_window(
        int(width), int(height), window_name, None, None)
    glfw.make_context_current(window)

    if not window:
        glfw.terminate()
        print("Could not initialize Window")
        sys.exit(1)

    return window

if __name__ == "__main__":
    main()
pthom commented 6 months ago

I merged your PR, thanks for the repro

pthom commented 6 months ago

And congrats for the fix!