Java lwjgl mit ImGui zeigt falsches Bild
Posted: 21 Dec 2024, 02:06
Ich arbeite an einem Spiel und habe auch ein grundlegendes LWGJL-Setup mit ImGui. Alles funktioniert, aber ich habe Probleme mit Imgui, wenn ich versuche, dem Menü ein Bild hinzuzufügen. Ich habe den folgenden Code und wenn ich ImGui.getWindowDrawList().addImage aufrufe, wird das falsche Bild angezeigt, als ob die ID nicht in die ImGui-ID übersetzt würde. Hilfe? Ich habe versucht, nach der Antwort zu suchen, aber nichts funktioniert. Die meisten Lösungen sind in C, nicht in Java
Hier ist der vollständige Quellcode 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;
}


Hier ist der vollständige Quellcode 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;
}

