-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathExampleImGuizmo.java
More file actions
339 lines (291 loc) · 12.3 KB
/
ExampleImGuizmo.java
File metadata and controls
339 lines (291 loc) · 12.3 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import imgui.ImGui;
import imgui.extension.imguizmo.ImGuizmo;
import imgui.extension.imguizmo.flag.Mode;
import imgui.extension.imguizmo.flag.Operation;
import imgui.flag.ImGuiCond;
import imgui.flag.ImGuiInputTextFlags;
import imgui.flag.ImGuiWindowFlags;
import imgui.type.ImBoolean;
import imgui.type.ImFloat;
import java.awt.Desktop;
import java.net.URI;
import java.util.Arrays;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_R;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_S;
import static org.lwjgl.glfw.GLFW.GLFW_KEY_T;
public class ExampleImGuizmo {
private static final String URL = "https://github.com/CedricGuillemet/ImGuizmo/tree/f7bbbe";
private static final int CAM_DISTANCE = 8;
private static final float CAM_Y_ANGLE = 165.f / 180.f * (float) Math.PI;
private static final float CAM_X_ANGLE = 32.f / 180.f * (float) Math.PI;
private static final float FLT_EPSILON = 1.19209290E-07f;
private static final float[][] OBJECT_MATRICES = {
{
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f
},
{
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
2.f, 0.f, 0.f, 1.f
},
{
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
2.f, 0.f, 2.f, 1.f
},
{
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 2.f, 1.f
}
};
private static final float[] IDENTITY_MATRIX = {
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f
};
private static final float[] EMPTY = new float[]{0};
private static final float[] INPUT_CAMERA_VIEW = {
1.f, 0.f, 0.f, 0.f,
0.f, 1.f, 0.f, 0.f,
0.f, 0.f, 1.f, 0.f,
0.f, 0.f, 0.f, 1.f
};
private static final float[] INPUT_BOUNDS = new float[]{-0.5f, -0.5f, -0.5f, 0.5f, 0.5f, 0.5f};
private static final float[] INPUT_BOUNDS_SNAP = new float[]{1f, 1f, 1f};
private static final float[] INPUT_SNAP_VALUE = new float[]{1f, 1f, 1f};
private static final float[] INPUT_MATRIX_TRANSLATION = new float[3];
private static final float[] INPUT_MATRIX_SCALE = new float[3];
private static final float[] INPUT_MATRIX_ROTATION = new float[3];
private static final ImFloat INPUT_FLOAT = new ImFloat();
private static final ImBoolean BOUNDING_SIZE = new ImBoolean(false);
private static final ImBoolean USE_SNAP = new ImBoolean(false);
private static int currentMode = Mode.LOCAL;
private static int currentGizmoOperation;
private static boolean boundSizingSnap = false;
private static boolean firstFrame = true;
public static void show(final ImBoolean showImGuizmoWindow) {
ImGuizmo.beginFrame();
if (ImGui.begin("ImGuizmo Demo", showImGuizmoWindow)) {
ImGui.text("This a demo for ImGuizmo");
ImGui.alignTextToFramePadding();
ImGui.text("Repo:");
ImGui.sameLine();
if (ImGui.button(URL)) {
try {
Desktop.getDesktop().browse(new URI(URL));
} catch (Exception e) {
e.printStackTrace();
}
}
ImGui.separator();
if (firstFrame) {
float[] eye = new float[]{
(float) (Math.cos(CAM_Y_ANGLE) * Math.cos(CAM_X_ANGLE) * CAM_DISTANCE),
(float) (Math.sin(CAM_X_ANGLE) * CAM_DISTANCE),
(float) (Math.sin(CAM_Y_ANGLE) * Math.cos(CAM_X_ANGLE) * CAM_DISTANCE)
};
float[] at = new float[]{0.f, 0.f, 0.f};
float[] up = new float[]{0.f, 1.f, 0.f};
lookAt(eye, at, up, INPUT_CAMERA_VIEW);
firstFrame = false;
}
ImGui.text("Keybindings:");
ImGui.text("T - Translate");
ImGui.text("R - Rotate");
ImGui.text("S - Scale");
ImGui.separator();
if (ImGuizmo.isUsing()) {
ImGui.text("Using gizmo");
if (ImGuizmo.isOver()) {
ImGui.text("Over a gizmo");
}
if (ImGuizmo.isOver(Operation.TRANSLATE)) {
ImGui.text("Over translate gizmo");
} else if (ImGuizmo.isOver(Operation.ROTATE)) {
ImGui.text("Over rotate gizmo");
} else if (ImGuizmo.isOver(Operation.SCALE)) {
ImGui.text("Over scale gizmo");
}
} else {
ImGui.text("Not using gizmo");
}
editTransform(showImGuizmoWindow);
ImGui.end();
}
}
private static void editTransform(final ImBoolean showImGuizmoWindow) {
if (ImGui.isKeyPressed(GLFW_KEY_T)) {
currentGizmoOperation = Operation.TRANSLATE;
} else if (ImGui.isKeyPressed(GLFW_KEY_R)) {
currentGizmoOperation = Operation.ROTATE;
} else if (ImGui.isKeyPressed(GLFW_KEY_S)) {
currentGizmoOperation = Operation.SCALE;
} else if (ImGui.isKeyPressed(GLFW_KEY_LEFT_SHIFT)) {
USE_SNAP.set(!USE_SNAP.get());
}
if (ImGuizmo.isUsing()) {
ImGuizmo.decomposeMatrixToComponents(OBJECT_MATRICES[0], INPUT_MATRIX_TRANSLATION, INPUT_MATRIX_ROTATION, INPUT_MATRIX_SCALE);
}
ImGui.inputFloat3("Tr", INPUT_MATRIX_TRANSLATION, "%.3f", ImGuiInputTextFlags.ReadOnly);
ImGui.inputFloat3("Rt", INPUT_MATRIX_ROTATION, "%.3f", ImGuiInputTextFlags.ReadOnly);
ImGui.inputFloat3("Sc", INPUT_MATRIX_SCALE, "%.3f", ImGuiInputTextFlags.ReadOnly);
if (ImGuizmo.isUsing()) {
ImGuizmo.recomposeMatrixFromComponents(INPUT_MATRIX_TRANSLATION, INPUT_MATRIX_ROTATION, INPUT_MATRIX_SCALE, OBJECT_MATRICES[0]);
}
if (currentGizmoOperation != Operation.SCALE) {
if (ImGui.radioButton("Local", currentMode == Mode.LOCAL)) {
currentMode = Mode.LOCAL;
}
ImGui.sameLine();
if (ImGui.radioButton("World", currentMode == Mode.WORLD)) {
currentMode = Mode.WORLD;
}
}
ImGui.checkbox("Snap Checkbox", USE_SNAP);
INPUT_FLOAT.set(INPUT_SNAP_VALUE[0]);
switch (currentGizmoOperation) {
case Operation.TRANSLATE:
ImGui.inputFloat3("Snap Value", INPUT_SNAP_VALUE);
break;
case Operation.ROTATE:
ImGui.inputFloat("Angle Value", INPUT_FLOAT);
float rotateValue = INPUT_FLOAT.get();
Arrays.fill(INPUT_SNAP_VALUE, rotateValue); //avoiding allocation
break;
case Operation.SCALE:
ImGui.inputFloat("Scale Value", INPUT_FLOAT);
float scaleValue = INPUT_FLOAT.get();
Arrays.fill(INPUT_SNAP_VALUE, scaleValue);
break;
}
ImGui.checkbox("Show Bound Sizing", BOUNDING_SIZE);
if (BOUNDING_SIZE.get()) {
if (ImGui.checkbox("BoundSizingSnap", boundSizingSnap)) {
boundSizingSnap = !boundSizingSnap;
}
ImGui.sameLine();
ImGui.inputFloat3("Snap", INPUT_BOUNDS_SNAP);
}
ImGui.setNextWindowPos(ImGui.getMainViewport().getPosX() + 100, ImGui.getMainViewport().getPosY() + 100, ImGuiCond.Once);
ImGui.setNextWindowSize(800, 400, ImGuiCond.Once);
ImGui.begin("Gizmo", showImGuizmoWindow);
ImGui.beginChild("prevent_window_from_moving_by_drag", 0, 0, false, ImGuiWindowFlags.NoMove);
float aspect = ImGui.getWindowWidth() / ImGui.getWindowHeight();
float[] cameraProjection = perspective(27, aspect, 0.1f, 100f);
ImGuizmo.setOrthographic(false);
ImGuizmo.enable(true);
ImGuizmo.setDrawList();
float windowWidth = ImGui.getWindowWidth();
float windowHeight = ImGui.getWindowHeight();
ImGuizmo.setRect(ImGui.getWindowPosX(), ImGui.getWindowPosY(), windowWidth, windowHeight);
ImGuizmo.drawGrid(INPUT_CAMERA_VIEW, cameraProjection, IDENTITY_MATRIX, 100);
ImGuizmo.setID(0);
ImGuizmo.drawCubes(INPUT_CAMERA_VIEW, cameraProjection, OBJECT_MATRICES[0]);
if (USE_SNAP.get() && BOUNDING_SIZE.get() && boundSizingSnap) {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0], null, INPUT_SNAP_VALUE, INPUT_BOUNDS, INPUT_BOUNDS_SNAP);
} else if (USE_SNAP.get() && BOUNDING_SIZE.get()) {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0], null, INPUT_SNAP_VALUE, INPUT_BOUNDS);
} else if (BOUNDING_SIZE.get() && boundSizingSnap) {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0], null, EMPTY, INPUT_BOUNDS, INPUT_BOUNDS_SNAP);
} else if (BOUNDING_SIZE.get()) {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0], null, EMPTY, INPUT_BOUNDS);
} else if (USE_SNAP.get()) {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0], null, INPUT_SNAP_VALUE);
} else {
ImGuizmo.manipulate(INPUT_CAMERA_VIEW, cameraProjection, currentGizmoOperation, currentMode, OBJECT_MATRICES[0]);
}
float viewManipulateRight = ImGui.getWindowPosX() + windowWidth;
float viewManipulateTop = ImGui.getWindowPosY();
ImGuizmo.viewManipulate(INPUT_CAMERA_VIEW, CAM_DISTANCE, viewManipulateRight - 128, viewManipulateTop, 128, 128, 0x10101010);
ImGui.endChild();
ImGui.end();
}
private static float[] perspective(float fovY, float aspect, float near, float far) {
float ymax, xmax;
ymax = (float) (near * Math.tan(fovY * Math.PI / 180.0f));
xmax = ymax * aspect;
return frustum(-xmax, xmax, -ymax, ymax, near, far);
}
private static float[] frustum(float left, float right, float bottom, float top, float near, float far) {
float[] r = new float[16];
float temp = 2.0f * near;
float temp2 = right - left;
float temp3 = top - bottom;
float temp4 = far - near;
r[0] = temp / temp2;
r[1] = 0.0f;
r[2] = 0.0f;
r[3] = 0.0f;
r[4] = 0.0f;
r[5] = temp / temp3;
r[6] = 0.0f;
r[7] = 0.0f;
r[8] = (right + left) / temp2;
r[9] = (top + bottom) / temp3;
r[10] = (-far - near) / temp4;
r[11] = -1.0f;
r[12] = 0.0f;
r[13] = 0.0f;
r[14] = (-temp * far) / temp4;
r[15] = 0.0f;
return r;
}
private static float[] cross(float[] a, float[] b) {
float[] r = new float[3];
r[0] = a[1] * b[2] - a[2] * b[1];
r[1] = a[2] * b[0] - a[0] * b[2];
r[2] = a[0] * b[1] - a[1] * b[0];
return r;
}
private static float dot(float[] a, float[] b) {
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
private static float[] normalize(float[] a) {
float[] r = new float[3];
float il = (float) (1.f / (Math.sqrt(dot(a, a)) + FLT_EPSILON));
r[0] = a[0] * il;
r[1] = a[1] * il;
r[2] = a[2] * il;
return r;
}
private static void lookAt(float[] eye, float[] at, float[] up, float[] m16) {
float[] x;
float[] y;
float[] z;
float[] tmp = new float[3];
tmp[0] = eye[0] - at[0];
tmp[1] = eye[1] - at[1];
tmp[2] = eye[2] - at[2];
z = normalize(tmp);
y = normalize(up);
tmp = cross(y, z);
x = normalize(tmp);
tmp = cross(z, x);
y = normalize(tmp);
m16[0] = x[0];
m16[1] = y[0];
m16[2] = z[0];
m16[3] = 0.0f;
m16[4] = x[1];
m16[5] = y[1];
m16[6] = z[1];
m16[7] = 0.0f;
m16[8] = x[2];
m16[9] = y[2];
m16[10] = z[2];
m16[11] = 0.0f;
m16[12] = -dot(x, eye);
m16[13] = -dot(y, eye);
m16[14] = -dot(z, eye);
m16[15] = 1.0f;
}
}