Fehler beim Imgui mit Maven

Nico1999

Aktives Mitglied
ich versuche ImGui für ein graphisches menü zu benutzen, dass über meiner simulation liegen soll.
ich bekomme diesen Fehler
Assertion failed: GImGui != __null && "No current context. Did you call ImGui::CreateContext() and ImGui::SetCurrentContext() ?", file /tmp/imgui/jni/imgui.cpp, line 3478
ich glaube das hängt mit der Maven dependency zusammen bin mir aber net sicher.
hier meine maven dependency dazu
Java:
 <dependency>
            <groupId>io.github.spair</groupId>
            <artifactId>imgui-java-binding</artifactId>
            <version>1.82.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.spair</groupId>
            <artifactId>imgui-java-app</artifactId>
            <version>1.82.2</version>
        </dependency>
 
Frag doch mal ChatGPT 🙂

Der Fehler alleine weist Dich erst einmal auf die C++ Problematik hin:
This error message typically indicates that you are calling an ImGui function without having a current ImGui context. ImGui is a user interface library for C++, and it requires that you create a context and set it as the current context before you can use any of its functions.

To fix this error, you will need to make sure that you have called both ImGui::CreateContext() and ImGui::SetCurrentContext() before calling any other ImGui functions. These functions should be called once, at the beginning of your program, before you start using ImGui.

Here is an example of how you might do this:

Copy code
#include <imgui.h>

int main()
{
// Create an ImGui context.
ImGui::CreateContext();

// Set the current context.
ImGui::SetCurrentContext(ImGui::GetCurrentContext());

// Your program code here...

return 0;
}

If you are still having issues after making sure that you have called ImGui::CreateContext() and ImGui::SetCurrentContext(), it's possible that the context has been deleted or unset somehow. Make sure that you are not calling any functions that might delete the context or unset it as the current context.

Da Du aber die Java Bindings nutzt habe ich dann noch einmal java-imgui-bindings davor gesetzt:

It looks like you are using the java-imgui-bindings library, which is a Java binding for the ImGui C++ library. The error message you are seeing is similar to the one that you might see when using the C++ version of ImGui, but it is specific to the Java binding.

To fix this error, you will need to make sure that you have called both ImGui.createContext() and ImGui.setCurrentContext() before calling any other ImGui functions. These functions should be called once, at the beginning of your program, before you start using ImGui.

Here is an example of how you might do this:

Copy code
import org.iceburg.javaImGui.ImGui;

public class MyProgram {
public static void main(String[] args) {
// Create an ImGui context.
ImGui.createContext();

// Set the current context.
ImGui.setCurrentContext(ImGui.getCurrentContext());

// Your program code here...
}
}

If you are still having issues after making sure that you have called ImGui.createContext() and ImGui.setCurrentContext(), it's possible that the context has been deleted or unset somehow. Make sure that you are not calling any functions that might delete the context or unset it as the current context.

Ich habe imgui noch nie verwendet, aber das sieht bezüglich der Fehlermeldung erst einmal sehr einleuchtend aus.

Aber: Laut der Github Beschreibung muss man das nicht selbst aufrufen. Da hat man einen Aufbau wie:
Java:
import imgui.ImGui;
import imgui.app.Application;

public class Main extends Application {
    @Override
    protected void configure(Configuration config) {
        config.setTitle("Dear ImGui is Awesome!");
    }

    @Override
    public void process() {
        ImGui.text("Hello, World!");
    }

    public static void main(String[] args) {
        launch(new Main());
    }
}
Und da kümmert sich die launch Logik um alles:
Die Application class hat in der Dokumentation:

Life-cycle​

The entry point for ImGui applications is the Application class and launch(Application) method. It initializes application instance and starts the main application loop.

  1. configure(Configuration) It's called before window creation, so only basic application setups are expected.
  2. Window.initWindow(Configuration) Method creates application window.
  3. Window.initImGui(Configuration) Method initializes Dear ImGui context. Could be used to do Dear ImGui setup as well.
  4. preRun() Method called once, before application loop.
  5. Window.preProcess() Method called every frame, before Window.process().
  6. Window.process() Method is meant to be overridden with user application logic.
  7. Window.postProcess() Method called every frame, after Window.process().
  8. postRun() Method called once, after application loop.
  9. Window.disposeImGui() Destroys Dear ImGui context.
  10. Window.disposeWindow() Destroys application window.

Daher ist meine Frage: Hast Du denn launch aufgerufen? Oder hast Du Deine Applikation anders aufgebaut?
 
Frag doch mal ChatGPT 🙂
was ist damit gemeint ?

ich tue mir generell noch bissle schwer überhaupt mit maven

im tutorial steht auch das die dependency so aussehen sollen

Java:
<dependency>
   <groupId>io.github.spair</groupId>
   <artifactId>imgui-java-binding</artifactId>
   <version>${imgui-java.version}</version>
</dependency>
<dependency>
    <groupId>io.github.spair</groupId>
    <artifactId>imgui-java-${native.target}</artifactId>
    <version>${imgui-java.version}</version>
    <scope>runtime</scope>
</dependency>

allerdings funktiert dann maven nicht also ka was ich da tun soll.

also die klasse wo ich quasi Imgui benutze ist diese hier

Code:
package org.lwjglb.engine.graph;

import imgui.*;
import imgui.type.ImInt;
import org.joml.Vector2f;
import org.lwjglb.engine.*;
import org.lwjglb.engine.scene.Scene;

import java.nio.ByteBuffer;
import java.util.*;

import static org.lwjgl.opengl.GL32.*;

public class GuiRender {

    private GuiMesh guiMesh;
    private Vector2f scale;
    private ShaderProgram shaderProgram;
    private Texture texture;
    private UniformsMap uniformsMap;

    public GuiRender(Window window) {
        List<ShaderProgram.ShaderModuleData> shaderModuleDataList = new ArrayList<>();
        shaderModuleDataList.add(new ShaderProgram.ShaderModuleData("src/main/resources/shaders/gui.vert", GL_VERTEX_SHADER));
        shaderModuleDataList.add(new ShaderProgram.ShaderModuleData("src/main/resources/shaders/gui.frag", GL_FRAGMENT_SHADER));
        shaderProgram = new ShaderProgram(shaderModuleDataList);
        createUniforms();
        createUIResources(window);
    }

    public void cleanup() {
        shaderProgram.cleanup();
        texture.cleanup();
    }

    private void createUIResources(Window window) {
        ImGui.createContext();//hier ist auf jeden fall das createContext

        ImGuiIO imGuiIO = ImGui.getIO();
        imGuiIO.setIniFilename(null);
        imGuiIO.setDisplaySize(window.getWidth(), window.getHeight());

        ImFontAtlas fontAtlas = ImGui.getIO().getFonts();
        ImInt width = new ImInt();
        ImInt height = new ImInt();
        ByteBuffer buf = fontAtlas.getTexDataAsRGBA32(width, height);
        texture = new Texture(width.get(), height.get(), buf);

        guiMesh = new GuiMesh();
    }

    private void createUniforms() {
        uniformsMap = new UniformsMap(shaderProgram.getProgramId());
        uniformsMap.createUniform("scale");
        scale = new Vector2f();
    }

    public void render(Scene scene) {
        IGuiInstance guiInstance = scene.getGuiInstance();//interface mit zwei methoden drawGui und handleGuiInput
        if (guiInstance == null) {
            return;
        }
        guiInstance.drawGui();

        shaderProgram.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();
        uniformsMap.setUniform("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.SIZEOF_IM_DRAW_IDX;

                texture.bind();
                glDrawElements(GL_TRIANGLES, elemCount, GL_UNSIGNED_SHORT, indices);
            }
        }

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

    public void resize(int width, int height) {
        ImGuiIO imGuiIO = ImGui.getIO();
        imGuiIO.setDisplaySize(width, height);
    }
}

sry das dass jetzt ein bissle lang geworden ist
 
Also dieses ChatGPT findet sich unter https://chat.openai.com/chat - falls Du das auch etwas ausprobieren willst.

Das entscheidende ist nicht der Code dort sondern wie Du die Applikation Initialisierst. Eine Applikation muss richtig initialisiert werden!

Dazu dient der launch Aufruf bei Application - Hast Du beim Start genau so eine Konstellation?

Wenn nicht, dann wäre die nächste Frage: Stellst Du sicher, dass der Context initialisiert wurde? Die Aufrufe hat ja ChatGPT gezeigt, die da notwendig sind.

Ersteres wäre die typische Applikation mit der Library. Das mit der manuellen initialisierung ist nur, wenn die UI an eine bestehende Applikation dran kommen soll (wieso auch immer).
 
das mit der Applikation benutze ich nicht ich benutze auch nicht in irgenteiner Form extends Applikation

das hier ist meine init () methode in der Main (bin mir nicht sicher ob das gemeint ist)

Java:
 @Override
    public void init(Window window, Scene scene, Render render) {
        Model cubeModel = ModelLoader.loadModel("cube-model",
                "src/main/resources/models/cube/cube.obj", scene.getTextureCache());
        scene.addModel(cubeModel);

        cubeEntity = new Entity("cube-entity", cubeModel.getId());
        cubeEntity.setPosition(0,0,-2);
        scene.addEntity(cubeEntity);
    }
Es wäre ganz gut zu wissen ob ich das mit den Maven richtig gemacht habe weil, da denke ich liegt dann der Fehler.

Es stehen bei der Ai sache jetzt so dinge wie codecompletion und textcompletion, aber das macht doch eh schon die IDEA ?

und warum funktiert das was die letze Maven dependency nicht die oben gezeigt habe das mit den {} klammern
 
Zuletzt bearbeitet:
Dann fehlt Dir die Initialisierung von imgui und daher kommt der Fehler.

Da wäre der Versuch, vorab einmal die folgenden Zeilen auszuführen:
Java:
ImGui.createContext();
ImGui.setCurrentContext(ImGui.getCurrentContext());

Damit wird der Fehler hoffentlich verschwinden.

Es wäre ganz gut zu wissen ob ich das mit den Maven richtig gemacht habe weil, da denke ich liegt dann der Fehler.
Dann würdest Du beim bauen Fehler bekommen. Das ist aber doch wohl nicht der Fall. Bei dem Ausschnitt
XML:
<dependency>
   <groupId>io.github.spair</groupId>
   <artifactId>imgui-java-binding</artifactId>
   <version>${imgui-java.version}</version>
</dependency>
<dependency>
    <groupId>io.github.spair</groupId>
    <artifactId>imgui-java-${native.target}</artifactId>
    <version>${imgui-java.version}</version>
    <scope>runtime</scope>
</dependency>
musst Du auch eine Property imgui-java.version mit der zu nutzenden Version definieren. Evtl. hast Du das beim Eintragen nicht beachtet?

Aber Du scheinst das ja irgendwie gelöst zu haben, denn sonst wärst Du nicht auf den Laufzeitfehler gestoßen.

Es stehen bei der Ai sache jetzt so dinge wie codecompletion und textcompletion, aber das macht doch eh schon die IDEA ?
Da müsstest Du genauer sagen, wo das wie steht. Irgendwie sehe ich das nicht.
 
Dann würdest Du beim bauen Fehler bekommen. Das ist aber doch wohl nicht der Fall. Bei dem Ausschnitt
XML:
<dependency>
   <groupId>io.github.spair</groupId>
   <artifactId>imgui-java-binding</artifactId>
   <version>${imgui-java.version}</version>
</dependency>
<dependency>
    <groupId>io.github.spair</groupId>
    <artifactId>imgui-java-${native.target}</artifactId>
    <version>${imgui-java.version}</version>
    <scope>runtime</scope>
</dependency>
doch dann bekomme ich einen bauen fehler bei der implementation daher habe ich irgentwie im web gesucht und hab das dann so gemacht

hiermit bekomme ich keinen Fehler allerdings bin ich mir auch net sicher ob ich dann das richtige benutzt habe

Java:
<dependency>
            <groupId>io.github.spair</groupId>
            <artifactId>imgui-java-binding</artifactId>
            <version>1.82.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.spair</groupId>
            <artifactId>imgui-java-app</artifactId>
            <version>1.82.2</version>
        </dependency>
 

Neue Themen


Zurück
Oben