-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathOpenGL3.java
More file actions
190 lines (154 loc) · 7.84 KB
/
OpenGL3.java
File metadata and controls
190 lines (154 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package imgui.examples;
import glm_.vec2.Vec2;
import glm_.vec2.Vec2i;
import glm_.vec4.Vec4;
import gln.cap.Caps;
import imgui.Cond;
import imgui.Flag;
import imgui.ImGui;
import imgui.MutableReference;
import imgui.classes.Context;
import imgui.classes.IO;
import imgui.impl.gl.ImplGL3;
import imgui.impl.glfw.ImplGlfw;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.system.Platform;
import uno.gl.GlWindow;
import uno.glfw.GlfwWindow;
import uno.glfw.Hints;
import uno.glfw.VSync;
import static gln.GlnKt.glClearColor;
import static gln.GlnKt.glViewport;
import static imgui.ImguiKt.DEBUG;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
public class OpenGL3 {
// The window handle
private GlWindow window;
private Context ctx;
private uno.glfw.glfw glfw = uno.glfw.glfw.INSTANCE;
private ImGui imgui = ImGui.INSTANCE;
private IO io;
private float[] f = {0f};
private Vec4 clearColor = new Vec4(0.45f, 0.55f, 0.6f, 1f);
// Java users can use both a MutableReference or a Boolean Array
private MutableReference<Boolean> showAnotherWindow = new MutableReference<>(false);
private boolean[] showDemo = {true};
private int[] counter = {0};
private ImplGlfw implGlfw;
private ImplGL3 implGl3;
public static void main(String[] args) {
new OpenGL3();
}
private OpenGL3() {
// Setup window
GLFW.glfwSetErrorCallback((error, description) -> System.out.println("Glfw Error " + error + ": " + description));
glfw.init();
Hints.Context hintCtx = glfw.getHints().getContext();
hintCtx.setDebug(DEBUG);
// Decide GL+GLSL versions
if (Platform.get() == Platform.MACOSX) { // GL 3.2 + GLSL 150
ImplGL3.Companion.getData().setGlslVersion(150);
hintCtx.setVersion("3.2");
hintCtx.setProfile(Hints.Context.Profile.Core); // 3.2+ only
hintCtx.setForwardComp(true); // Required on Mac
} else { // GL 3.0 + GLSL 130
ImplGL3.Companion.getData().setGlslVersion(130);
hintCtx.setVersion("3.0");
//profile = core // 3.2+ only
//forwardComp = true // 3.0+ only
}
// Create window with graphics context
GlfwWindow glfwWindow = GlfwWindow.create(1280, 720, "Dear ImGui GLFW+OpenGL3 OpenGL example", MemoryUtil.NULL, null, new Vec2i(30));
window = new GlWindow(glfwWindow, Caps.Profile.COMPATIBILITY, true);
window.makeCurrent(true);
glfw.setSwapInterval(VSync.ON); // Enable vsync
// Initialize OpenGL loader
GL.createCapabilities();
// Setup Dear ImGui context
ctx = new Context(null);
//io.configFlags = io.configFlags or ConfigFlag.NavEnableKeyboard // Enable Keyboard Controls
//io.configFlags = io.configFlags or ConfigFlag.NavEnableGamepad // Enable Gamepad Controls
// Setup Dear ImGui style
imgui.styleColorsDark(null);
// imgui.styleColorsClassic(null)
// Setup Platform/Renderer backends
implGlfw = new ImplGlfw(window, true, null);
implGl3 = new ImplGL3();
io = imgui.getIo();
// Load Fonts
/* - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use
pushFont()/popFont() to select them.
- addFontFromFileTTF() will return the Font so you can store it if you need to select the font among multiple.
- If the file cannot be loaded, the function will return null. Please handle those errors in your application
(e.g. use an assertion, or display an error and quit).
- The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling
FontAtlas.build()/getTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call.
- Read 'docs/FONTS.txt' for more instructions and details.
- Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write
a double backslash \\ ! */
//ImGuiIO& io = ImGui::GetIO();
//io.Fonts->AddFontDefault();
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
// Font font = io.getFonts().addFontFromFileTTF("misc/fonts/ArialUni.ttf", 18f, new FontConfig(), io.getFonts().getGlyphRangesJapanese());
// assert (font != null);
/* Main loop
This automatically also polls events, swaps buffers and resets the appBuffer
Poll and handle events (inputs, window resize, etc.)
You can read the io.wantCaptureMouse, io.wantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
- When io.wantCaptureMouse is true, do not dispatch mouse input data to your main application.
- When io.wantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. */
window.loop((MemoryStack stack) -> mainLoop());
implGlfw.shutdown();
implGl3.shutdown();
ctx.destroy();
window.destroy();
glfw.terminate();
}
private void mainLoop() {
// Start the Dear ImGui frame
implGl3.newFrame();
implGlfw.newFrame();
imgui.newFrame();
imgui.text("Hello, world!"); // Display some text (you can use a format string too)
// TODO
// imgui.sliderFloat("float", f, 0, 0f, 1f, "%.3f", 1f); // Edit 1 float using a slider from 0.0f to 1.0f
imgui.colorEdit3("clear color", clearColor, Flag.none()); // Edit 3 floats representing a color
imgui.checkbox("Demo Window", showDemo); // Edit bools storing our windows open/close state
imgui.checkbox("Another Window", showAnotherWindow);
if (imgui.button("Button", new Vec2())) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
counter[0]++;
imgui.sameLine(0f, -1f);
imgui.text("counter = " + counter[0]);
imgui.text("Application average %.3f ms/frame (%.1f FPS)", 1_000f / io.getFramerate(), io.getFramerate());
// 2. Show another simple window. In most cases you will use an explicit begin/end pair to name the window.
if (showAnotherWindow.get()) {
imgui.begin("Another Window", showAnotherWindow, Flag.none());
imgui.text("Hello from another window!");
if (imgui.button("Close Me", new Vec2()))
showAnotherWindow.set(false);
imgui.end();
}
/* 3. Show the ImGui demo window. Most of the sample code is in imgui.showDemoWindow().
Read its code to learn more about Dear ImGui! */
if (showDemo[0]) {
/* Normally user code doesn't need/want to call this because positions are saved in .ini file anyway.
Here we just want to make the demo initial state a bit more friendly! */
imgui.setNextWindowPos(new Vec2(650, 20), Cond.FirstUseEver.INSTANCE, new Vec2());
imgui.showDemoWindow(showDemo);
}
// Rendering
imgui.render();
glViewport(window.getFramebufferSize());
glClearColor(clearColor);
glClear(GL_COLOR_BUFFER_BIT);
implGl3.renderDrawData(imgui.getDrawData());
}
}