SpaiR / imgui-java

JNI based binding for Dear ImGui
MIT License
551 stars 90 forks source link

Bug: Weird issue within glClear when trying to make the framebuffer transparent? #140

Closed ghost closed 1 year ago

ghost commented 1 year ago

Version

Latest one

What happened?

So, I'm trying to render ImGUI window onto the Minecraft window with the Minecraft's title screen not getting cleared? I have no idea how to explain this error so I'll try my best with examples. There's a weird rendering error, or probably my mistake. When i do this:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0f, 0f, 0f, 0f);

My ImGUI window receives a black background.

Without glClear, the imgui window leaves a weird windows-xp alike trail.

And this is the result i want to get, where Minecraft's title screen doesn't get cleared, and the ImGUI stays on top.

So far I am doing this:

 public static long windowPtr = MinecraftClient.getInstance().getWindow().getHandle();
        public static final ImGuiImplGlfw implGlfw = new ImGuiImplGlfw();
        public static final ImGuiImplGl3 implGl3 = new ImGuiImplGl3();
        private static final String glslVersion = "#version 150";

      //  Ignore this part of the imguiLayer, that's the class where im doing my ImGui stuff like ImGui.text etc.
      //  public static ImGuiLayer imguiLayer;

      //  public Window(ImGuiLayer layer) {
      //      imguiLayer = layer;
      //  }

        public static void init() {
            initWindow();
            initImGui();
            implGlfw.init(windowPtr, true);
            implGl3.init(glslVersion);
        }

        private static void initWindow() {
            System.out.println("InitWindow");
            GLFWErrorCallback.createPrint(System.err).set();

            // Initialize GLFW. Most GLFW functions will not work before doing this.
            if ( !glfwInit() ) {
                System.out.println("Unable to initialize GLFW");
                System.exit(-1);
            }

            glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
            glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
          //  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
            glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
            glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);

            if (windowPtr == NULL) {
                System.out.println("Unable to create window");
                System.exit(-1);
            }
           // glfwSwapInterval(0);
            glfwMakeContextCurrent(windowPtr);

            glEnable(GL_BLEND);
            glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
            glfwShowWindow(windowPtr);

            GL.createCapabilities();

        }

        private static void initImGui() {
            ImGui.createContext();
            ImGuiIO io = ImGui.getIO();

            io.getFonts().addFontFromFileTTF(String.valueOf(FontPath),18);
          //  io.addConfigFlags(ImGuiConfigFlags.ViewportsEnable);
        }
    }

    public static final ImGuiScreen INSTANCE = new ImGuiScreen();

    public ImGuiScreen() {
        super(new LiteralText("ImguiScreen"));
    }

        @Override
        public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
               //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
               // glClearColor(0f, 0f, 0f, 0f);
              // Uncomment this to get the window to start showing the weird black background 
              // Leave this uncommented if you want to see the windows-xp styled window trail

                implGlfw.newFrame();
                ImGui.newFrame();
                imguiLayer.imgui();
                ImGui.end();
                ImGui.render();
                //org.lwjgl.glfw.GLFW.glfwPollEvents();

                implGl3.renderDrawData(Objects.requireNonNull(ImGui.getDrawData()));
                //GLFW.glfwSwapBuffers(windowPtr);
                implGl3.renderDrawData(ImGui.getDrawData());
        }

}

Reproduction

Uncomment glClear()

Relevant log output

No response

ghost commented 1 year ago

I also thought about making the ImGUI window it's standalone window, because if i would call glClear() it wouldnt clear the Minecraft title screen, and somehow hooking it up to Minecraft. But i don't know if making the ImGUI window standalone and hooking it up is possible :sweat:

SpaiR commented 1 year ago

I have no experience with Minecraft in terms of integration with imgui-java. Yet, I know there are several repositories which already use it. Even ready to use solutions like https://github.com/mjwells2002/imgui-mc. Please, consider to use them as a reference (or use them instead in case of the last).

ghost commented 1 year ago

Aight, thank you!

ghost commented 1 year ago

I have decided to stick to my own implementation for now, because the implementation that mjwells2002 made uses some custom written libraries, and i don't want to have that for now.