|
4 | 4 | import imgui.ImGui;
|
5 | 5 | import imgui.ImGuiIO;
|
6 | 6 | import imgui.ImString;
|
| 7 | +import imgui.ImVec2; |
7 | 8 | import imgui.callbacks.ImStrConsumer;
|
8 | 9 | import imgui.callbacks.ImStrSupplier;
|
9 | 10 | import imgui.enums.ImGuiBackendFlags;
|
|
14 | 15 | import imgui.enums.ImGuiKey;
|
15 | 16 | import imgui.enums.ImGuiMouseCursor;
|
16 | 17 | import imgui.gl3.ImGuiImplGl3;
|
| 18 | +import org.lwjgl.BufferUtils; |
17 | 19 | import org.lwjgl.glfw.GLFWErrorCallback;
|
18 | 20 | import org.lwjgl.glfw.GLFWVidMode;
|
19 | 21 | import org.lwjgl.opengl.GL;
|
20 | 22 | import org.lwjgl.system.MemoryStack;
|
21 | 23 |
|
| 24 | +import javax.imageio.ImageIO; |
| 25 | +import java.awt.image.BufferedImage; |
22 | 26 | import java.io.ByteArrayOutputStream;
|
| 27 | +import java.io.File; |
23 | 28 | import java.io.IOException;
|
24 | 29 | import java.io.InputStream;
|
25 | 30 | import java.io.UncheckedIOException;
|
| 31 | +import java.nio.ByteBuffer; |
26 | 32 | import java.nio.IntBuffer;
|
27 | 33 | import java.util.Objects;
|
28 | 34 |
|
@@ -63,8 +69,11 @@ public final class ImGuiGlfwExample {
|
63 | 69 | private int clickCount = 0;
|
64 | 70 | private final ImString resizableStr = new ImString(5);
|
65 | 71 | private final ImBool showDemoWindow = new ImBool();
|
| 72 | + private int dukeTexture; |
| 73 | + private ImVec2 windowSize = new ImVec2(); // Vector to store "Custom Window" size |
| 74 | + private ImVec2 windowPos = new ImVec2(); // Vector to store "Custom Window" position |
66 | 75 |
|
67 |
| - public void run() { |
| 76 | + public void run() throws Exception { |
68 | 77 | initGlfw();
|
69 | 78 | initImGui();
|
70 | 79 | loop();
|
@@ -271,7 +280,10 @@ public String get() {
|
271 | 280 | }
|
272 | 281 |
|
273 | 282 | // Main application loop
|
274 |
| - private void loop() { |
| 283 | + private void loop() throws Exception { |
| 284 | + // Load Duke image and convert it into OpenGL texture |
| 285 | + dukeTexture = loadTexture(ImageIO.read(new File("imgui-lwjgl3/src/test/resources/Duke_waving.png"))); |
| 286 | + |
275 | 287 | double time = 0; // to track our frame delta value
|
276 | 288 |
|
277 | 289 | // Run the rendering loop until the user has attempted to close the window
|
@@ -326,7 +338,14 @@ private void showUi() {
|
326 | 338 |
|
327 | 339 | ImGui.begin("Custom window"); // Start Custom window
|
328 | 340 |
|
329 |
| - // Simple text label |
| 341 | + // Example of how to draw an image in the bottom-right corner of the window |
| 342 | + ImGui.getWindowSize(windowSize); |
| 343 | + ImGui.getWindowPos(windowPos); |
| 344 | + float xPoint = windowPos.x + windowSize.x - 100; |
| 345 | + float yPoint = windowPos.y + windowSize.y; |
| 346 | + ImGui.getWindowDrawList().addImage(dukeTexture, xPoint, yPoint - 180, xPoint + 100, yPoint); |
| 347 | + |
| 348 | + // Simple checkbox to show demo window |
330 | 349 | ImGui.checkbox("Show demo window", showDemoWindow);
|
331 | 350 |
|
332 | 351 | ImGui.separator();
|
@@ -430,7 +449,37 @@ private byte[] loadFromResources(final String fileName) {
|
430 | 449 | }
|
431 | 450 | }
|
432 | 451 |
|
433 |
| - public static void main(final String[] args) { |
| 452 | + private int loadTexture(BufferedImage image) { |
| 453 | + int[] pixels = new int[image.getWidth() * image.getHeight()]; |
| 454 | + image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth()); |
| 455 | + |
| 456 | + ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); // 4 for RGBA, 3 for RGB |
| 457 | + for (int y = 0; y < image.getHeight(); y++) { |
| 458 | + for (int x = 0; x < image.getWidth(); x++) { |
| 459 | + int pixel = pixels[y * image.getWidth() + x]; |
| 460 | + buffer.put((byte) ((pixel >> 16) & 0xFF)); |
| 461 | + buffer.put((byte) ((pixel >> 8) & 0xFF)); |
| 462 | + buffer.put((byte) (pixel & 0xFF)); |
| 463 | + buffer.put((byte) ((pixel >> 24) & 0xFF)); |
| 464 | + } |
| 465 | + } |
| 466 | + buffer.flip(); |
| 467 | + |
| 468 | + int textureID = glGenTextures(); |
| 469 | + glBindTexture(GL_TEXTURE_2D, textureID); |
| 470 | + |
| 471 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 472 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 473 | + |
| 474 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 475 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 476 | + |
| 477 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); |
| 478 | + |
| 479 | + return textureID; |
| 480 | + } |
| 481 | + |
| 482 | + public static void main(final String[] args) throws Exception { |
434 | 483 | new ImGuiGlfwExample().run();
|
435 | 484 | }
|
436 | 485 | }
|
0 commit comments