Java lwjgl 与 ImGui 显示错误的图像

问题描述 投票:0回答:1

我正在开发一款游戏,并且还使用 ImGui 进行了基本的 lwgjl 设置,一切正常,但当我尝试向菜单添加图片时,我遇到了 Imgui 问题。我有以下代码,当我调用 ImGui.getWindowDrawList().addImage 时,它显示错误的图像,就好像 id 没有转换为 ImGui ID 一样。有什么帮助吗?我尝试寻找答案,但没有任何效果,大多数解决方案都是 C 语言,而不是 java 语言 这是完整的源代码https://github.com/freethemice/GameTest

    public void showUi() {
    if (!showMenu) return;
    ImGui.setNextWindowSize(600, 300, ImGuiCond.Once);
    int posX = testGame.getWindow().getWidth() / 2 - 600 / 2;
    int posY = testGame.getWindow().getHeight() / 2 - 300 / 2;
    ImGui.setNextWindowPos(posX, posY, ImGuiCond.Once);

    ImGui.begin("Main Menu");  // Start Custom window



    ImGui.sliderInt("Render Distance x/z", renderDistance, 10, 100 );

    ImGui.separator();
    float min = Math.min(30, ImGui.getWindowSize().y - 35);
    if (min > 10) {
        if (ImGui.button("Resume", ImGui.getWindowSize().x - 17, min)) {
            showMenu = false;
        }
    }
    min = Math.min(30, ImGui.getWindowSize().y - 65);
    if (min > 10) {
        if (ImGui.button("Exit", ImGui.getWindowSize().x - 17, Math.min(30, min))) {
            testGame.getWindow().shutDown();
        }
    }

    ImGui.getWindowSize(windowSize);
    ImGui.getWindowPos(windowPos);
    final float xPoint = windowPos.x + windowSize.x - 1000;
    final float yPoint = windowPos.y + windowSize.y;


    ImGui.getWindowDrawList().addImage(dukeTexture.getId(), xPoint, yPoint - 180, xPoint + 1000, yPoint);


    ImGui.end();  // End Custom window
}
    public int loadTexture(String filename) throws Exception {
    int width, height;
    ByteBuffer buffer;
    try(MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer c = stack.mallocInt(1);

        buffer = STBImage.stbi_load(filename, w, h, c, 4);
        if (buffer == null)
            throw new Exception("Image File " + filename + " not loaded " + STBImage.stbi_failure_reason());

        width = w.get();
        height = h.get();
    }


    int id = GL11.glGenTextures();
    textures.add(id);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

   
    GL30.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
    GL30.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
    STBImage.stbi_image_free(buffer);

    return id;

}

results Correct Picture

java lwjgl imgui
1个回答
0
投票

这是我正在使用的渲染函数

 public void render(SceneManager scene) {
    IGuiInstance guiInstance = scene.getGuiInstance();
    if (guiInstance == null || this.guiMesh == null) {
        return;
    }
    guiInstance.drawGui();
    shader.bind();

    glEnable(GL_BLEND);
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glBindVertexArray(guiMesh.getVaoId());

    glBindBuffer(GL_ARRAY_BUFFER, guiMesh.getVerticesVBO());
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, guiMesh.getIndicesVBO());

    ImGuiIO io = ImGui.getIO();
    scale.x = 2.0f / io.getDisplaySizeX();
    scale.y = -2.0f / io.getDisplaySizeY();
    shader.setUniforms("scale", scale);

    ImDrawData drawData = ImGui.getDrawData();
    int numLists = drawData.getCmdListsCount();
    for (int i = 0; i < numLists; i++) {
        glBufferData(GL_ARRAY_BUFFER, drawData.getCmdListVtxBufferData(i), GL_STREAM_DRAW);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, drawData.getCmdListIdxBufferData(i), GL_STREAM_DRAW);

        int numCmds = drawData.getCmdListCmdBufferSize(i);
        for (int j = 0; j < numCmds; j++) {
            final int elemCount = drawData.getCmdListCmdBufferElemCount(i, j);
            final int idxBufferOffset = drawData.getCmdListCmdBufferIdxOffset(i, j);
            final int indices = idxBufferOffset * ImDrawData.sizeOfImDrawIdx();

            glBindTexture(GL_TEXTURE_2D, texture.getId());
            glDrawElements(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices);
        }
    }


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDisable(GL_BLEND);
}

我必须将glBindTexture(GL_TEXTURE_2D,texture.getId())更改为glBindTexture(GL_TEXTURE_2D,(int)imGuiTextureID),当imGuiTextureID不为0时,它试图渲染图像,所以我将其更改为这个

    public void render(SceneManager scene) {
    IGuiInstance guiInstance = scene.getGuiInstance();
    if (guiInstance == null || this.guiMesh == null) {
        return;
    }
    guiInstance.drawGui();
    shader.bind();

    glEnable(GL_BLEND);
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);

    glBindVertexArray(guiMesh.getVaoId());

    glBindBuffer(GL_ARRAY_BUFFER, guiMesh.getVerticesVBO());
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, guiMesh.getIndicesVBO());

    ImGuiIO io = ImGui.getIO();
    scale.x = 2.0f / io.getDisplaySizeX();
    scale.y = -2.0f / io.getDisplaySizeY();
    shader.setUniforms("scale", scale);

    ImDrawData drawData = ImGui.getDrawData();
    int numLists = drawData.getCmdListsCount();
    for (int i = 0; i < numLists; i++) {
        glBufferData(GL_ARRAY_BUFFER, drawData.getCmdListVtxBufferData(i), GL_STREAM_DRAW);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, drawData.getCmdListIdxBufferData(i), GL_STREAM_DRAW);

        int numCmds = drawData.getCmdListCmdBufferSize(i);
        for (int j = 0; j < numCmds; j++) {
            long imGuiTextureID = drawData.getCmdListCmdBufferTextureId(i, j);
            final int elemCount = drawData.getCmdListCmdBufferElemCount(i, j);
            final int idxBufferOffset = drawData.getCmdListCmdBufferIdxOffset(i, j);
            final int indices = idxBufferOffset * ImDrawData.sizeOfImDrawIdx();
            if ((int) cmdListCmdBufferTextureId != 0) glBindTexture(GL_TEXTURE_2D, (int) imGuiTextureID);
            else glBindTexture(GL_TEXTURE_2D, texture.getId());
            glDrawElements(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices);
        }
    }


    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDisable(GL_BLEND);
}
© www.soinside.com 2019 - 2024. All rights reserved.