Skip to content

Commit 9f018ff

Browse files
committed
Add an example drawing an image to the window draw list
1 parent a342e44 commit 9f018ff

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

imgui-lwjgl3/src/test/java/ImGuiGlfwExample.java

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import imgui.ImGui;
55
import imgui.ImGuiIO;
66
import imgui.ImString;
7+
import imgui.ImVec2;
78
import imgui.callbacks.ImStrConsumer;
89
import imgui.callbacks.ImStrSupplier;
910
import imgui.enums.ImGuiBackendFlags;
@@ -14,15 +15,20 @@
1415
import imgui.enums.ImGuiKey;
1516
import imgui.enums.ImGuiMouseCursor;
1617
import imgui.gl3.ImGuiImplGl3;
18+
import org.lwjgl.BufferUtils;
1719
import org.lwjgl.glfw.GLFWErrorCallback;
1820
import org.lwjgl.glfw.GLFWVidMode;
1921
import org.lwjgl.opengl.GL;
2022
import org.lwjgl.system.MemoryStack;
2123

24+
import javax.imageio.ImageIO;
25+
import java.awt.image.BufferedImage;
2226
import java.io.ByteArrayOutputStream;
27+
import java.io.File;
2328
import java.io.IOException;
2429
import java.io.InputStream;
2530
import java.io.UncheckedIOException;
31+
import java.nio.ByteBuffer;
2632
import java.nio.IntBuffer;
2733
import java.util.Objects;
2834

@@ -63,8 +69,11 @@ public final class ImGuiGlfwExample {
6369
private int clickCount = 0;
6470
private final ImString resizableStr = new ImString(5);
6571
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
6675

67-
public void run() {
76+
public void run() throws Exception {
6877
initGlfw();
6978
initImGui();
7079
loop();
@@ -271,7 +280,10 @@ public String get() {
271280
}
272281

273282
// 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+
275287
double time = 0; // to track our frame delta value
276288

277289
// Run the rendering loop until the user has attempted to close the window
@@ -326,7 +338,14 @@ private void showUi() {
326338

327339
ImGui.begin("Custom window"); // Start Custom window
328340

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
330349
ImGui.checkbox("Show demo window", showDemoWindow);
331350

332351
ImGui.separator();
@@ -430,7 +449,37 @@ private byte[] loadFromResources(final String fileName) {
430449
}
431450
}
432451

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 {
434483
new ImGuiGlfwExample().run();
435484
}
436485
}
11.4 KB
Loading

0 commit comments

Comments
 (0)