Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modal does not render correctly when using multiple begin #36

Closed
tlf30 opened this issue Jul 21, 2022 · 6 comments
Closed

Modal does not render correctly when using multiple begin #36

tlf30 opened this issue Jul 21, 2022 · 6 comments

Comments

@tlf30
Copy link

tlf30 commented Jul 21, 2022

Describe the bug
When using multiple windows, i.e. ImGui.begin... the offsets for rendering are incorrect and cause modals to not have the correct background, the background faded color becomes the windows foreground.

To Reproduce
Chapter 15, create multiple ImGui windows in the gui render.
Create a popup modal.

Expected behavior
The popup modal should render correctly.

See ocornut/imgui#4845 for more information.

I have a modified snippet from a vulkan test rendering engine I wrote that shows a working implementation using imgui offsets from the render data, I have modified it to be similar to the implementation here for ease of implementation.

            ImVec4 imVec4 = new ImVec4();
            VkRect2D.Buffer rect = VkRect2D.calloc(1, stack);

            int numCmdLists = imDrawData.getCmdListsCount();
            int offsetIdx = 0;
            int offsetVtx = 0;
            for (int i = 0; i < numCmdLists; i++) {

                int cmdBufferSize = imDrawData.getCmdListCmdBufferSize(i);
                for (int j = 0; j < cmdBufferSize; j++) {

                    imDrawData.getCmdListCmdBufferClipRect(i, j, imVec4);
                    rect.offset(it -> it
                            .x((int) Math.max(imVec4.x, 0))
                            .y((int) Math.max(imVec4.y, 1))
                    );
                    rect.extent(it -> it
                            .width((int) (imVec4.z - imVec4.x))
                            .height((int) (imVec4.w - imVec4.y))
                    );
                    vkCmdSetScissor(cmdHandle, 0, rect);

                    int numElements = imDrawData.getCmdListCmdBufferElemCount(i, j);

                    //Get texture info
                    int textureId = imDrawData.getCmdListCmdBufferTextureId(i, j);
                    if (textureId > 0) {
                        //Bind texture
                        //OVkTexture texture = pipeline.getBufferCache().getVkTexture(ImGuiUtil.getTexture(textureId));
                        //if (!texture.isTransitionRecorded()) {
                        //    continue; //We will wait until the texture is recorded
                        //}
                        //shaderPipeline.setParameter("textureSampler", texture); //TODO: We cannot set this here
                    } else {
                        //Set texture
                        //shaderPipeline.setParameter("textureSampler", fontsTexture); //TODO: We cannot set this here
                    }

                    OVkDebug.debugCommandBuffer(commandBuffer, "Draw Indexed Count=" + numElements + ". Size=" + rect.extent().width() + "x" + rect.extent().height());
                    vkCmdDrawIndexed(
                            cmdHandle, 
                            numElements,
                            1, 
                            offsetIdx + imDrawData.getCmdListCmdBufferIdxOffset(i, j), 
                            offsetVtx + imDrawData.getCmdListCmdBufferVtxOffset(i, j), 
                            0
                    );
                    OVkDebug.debugInsertLabel(commandBuffer, "Draw Indexed Count=" + numElements + ". Size=" + rect.extent().width() + "x" + rect.extent().height(), new Vector4f(.5f, .6f, 1f, 1f));
                }
                offsetIdx += imDrawData.getCmdListIdxBufferSize(i);
                offsetVtx += imDrawData.getCmdListVtxBufferSize(i);
            }
            OVkDebug.debugEndLabel(commandBuffer); //END Drawing ImGui
@lwjglgamedev
Copy link
Owner

Thanks for reporting. I will check.

@lwjglgamedev
Copy link
Owner

Applied the following fix (chapter 15, GuiRenderActivity.java ):

            ImDrawData imDrawData = ImGui.getDrawData();
            int numCmdLists = imDrawData.getCmdListsCount();
            int offsetIdx = 0;
            int offsetVtx = 0;
            for (int i = 0; i < numCmdLists; i++) {
                int cmdBufferSize = imDrawData.getCmdListCmdBufferSize(i);
                for (int j = 0; j < cmdBufferSize; j++) {
                    imDrawData.getCmdListCmdBufferClipRect(i, j, imVec4);
                    rect.offset(it -> it.x((int) Math.max(imVec4.x, 0)).y((int) Math.max(imVec4.y, 1)));
                    rect.extent(it -> it.width((int) (imVec4.z - imVec4.x)).height((int) (imVec4.w - imVec4.y)));
                    vkCmdSetScissor(cmdHandle, 0, rect);
                    int numElements = imDrawData.getCmdListCmdBufferElemCount(i, j);
                    vkCmdDrawIndexed(cmdHandle, numElements, 1,
                            offsetIdx + imDrawData.getCmdListCmdBufferIdxOffset(i, j),
                            offsetVtx + imDrawData.getCmdListCmdBufferVtxOffset(i, j), 0);
                }
                offsetIdx += imDrawData.getCmdListIdxBufferSize(i);
                offsetVtx += imDrawData.getCmdListVtxBufferSize(i);
            }

It sees that modal popups are shown ok. However, when showing a model window, a transparency is applied to the whole render area, any idea if this is normal?

@tlf30
Copy link
Author

tlf30 commented Jul 23, 2022

Off the top of my head that looks correct. Can you post a screenshot of what you are seeing. I have a 9 hour layover in Anchorage later today 😭 and will have lots of time to look at it. (Yes, that is 9 hours of being stuck in the airport...)

@lwjglgamedev
Copy link
Owner

Sure. Attached a screenshot.

img

Good luck , and lots of patience, in the airport!

@tlf30
Copy link
Author

tlf30 commented Jul 23, 2022

Yep, that looks correct for what a popup modal should do (graying out the background). Is there some other behavior you were expecting, or something that does not look correct?

@lwjglgamedev
Copy link
Owner

Nope. Not sure if graying out the background was correct or not. Ok, Fixed then. I will review the text and update next chapters. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants