PeriDyno 1.0.0
Loading...
Searching...
No Matches
imGuIZMOquat.h
Go to the documentation of this file.
1//------------------------------------------------------------------------------
2// Copyright (c) 2018-2020 Michele Morrone
3// All rights reserved.
4//
5// https://michelemorrone.eu - https://BrutPitt.com
6//
7// twitter: https://twitter.com/BrutPitt - github: https://github.com/BrutPitt
8//
9// mailto:brutpitt@gmail.com - mailto:me@michelemorrone.eu
10//
11// This software is distributed under the terms of the BSD 2-Clause license
12//------------------------------------------------------------------------------
13#pragma once
14#include <algorithm>
15
16
17#include "vGizmo.h"
18
19#if !defined(IMGUIZMO_IMGUI_FOLDER)
20 #define IMGUIZMO_IMGUI_FOLDER imgui/
21#endif
22
23#define GET_PATH(P) P
24#define INC_PATH(X) <GET_PATH(IMGUIZMO_IMGUI_FOLDER)X>
25
26#define IMGUI_DEFINE_MATH_OPERATORS
27
28#include INC_PATH(imgui.h)
29#include INC_PATH(imgui_internal.h)
30
32//
33// NEED TO BUILD :
34//
35// - Using glm (OpenGL Mathematics) include files: need version 0.9.9 or later
36// and GLM_ENABLE_EXPERIMENTAL compile option to enable "quaternion" class
37// - Using include virtualGizmo.h (my virtualGizmoClass) to get imput controls
38// and apply rotations
39// - use c++11 standard
40//
41// Use preprocessor define imguiGizmo_INTERPOLATE_NORMALS, to a phong color effect
42// for solid components, but there are more vertex memorized and more operations,
43// even if the visual effect is better... the computational difference is:
44// one normal for vertex VS one normal for plain figure (triangle or quad)
45//
46// comment/uncomment below or add as directive to compiler
48
49#define imguiGizmo_INTERPOLATE_NORMALS
50#define STARTING_ALPHA_PLANE .75f
51
53//
54// imguiGizmo 3D
55//
56// Buttons:
57// Left: drag to free rotate axes
58// Right: drag to free rotate spot (only dualmode!)
59// Right & Left: drag to rotate together
60// for ImGui features, right button drag is active only INSIDE the frame
61// Modifiers:
62// Shift: rotation only around X
63// Ctrl: rotation only around Y
64// Alt: rotation only around Z
66
67// The data structure that holds the orientation among other things
69{
70
71 quat qtV = quat(1.0f, vec3(0.0f)); // Quaternion value
72 quat qtV2 = quat(1.0f, vec3(0.0f)); // Quaternion value
73#ifndef IMGUIZMO_USE_ONLY_ROT
76#endif
77
78 enum { //0b0000'0000, //C++14 notation
79 mode3Axes = 0x0001, //0b0000'0001,
80 modeDirection = 0x0002, //0b0000'0010,
81 modeDirPlane = 0x0004, //0b0000'0100,
82 modeDual = 0x0008, //0b0000'1000,
83 modePanDolly = 0x0010, //0b0001'0000,
84 modeMask = 0x00ff,
85
86
87 cubeAtOrigin = 0x0100, //0b0000'0000,
88 sphereAtOrigin = 0x0200, //0b0001'0000,
89 noSolidAtOrigin = 0x0400, //0b0010'0000,
90 modeFullAxes = 0x0800,
91 axesModeMask = 0xff00
92 };
93
96 //enum { SOLID_SURF, SOLID_CAP }
97 //enum {
99
100 enum solidSides{ backSide, frontSide }; // or viceversa...
101 static ImVector<vec3> sphereVtx;
102 static ImVector<int> sphereTess;
103 static ImVector<vec3> cubeVtx;
104 static ImVector<vec3> cubeNorm;
105 static ImVector<vec3> planeVtx;
106 static ImVector<vec3> planeNorm;
107 static ImVector<vec3> arrowVtx[4];
108 static ImVector<vec3> arrowNorm[4];
109 static void buildPlane (const float size, const float thickness = planeThickness) {
110 buildPolygon(vec3(thickness,size,size), planeVtx, planeNorm);
111 }
112 static void buildCube (const float size) {
114 }
115 static void buildPolygon (const vec3& size,ImVector<vec3>& vtx,ImVector<vec3>& norm);
116 static void buildSphere (const float radius, const int tessFactor);
117 static void buildCone (const float x0, const float x1, const float radius, const int slices);
118 static void buildCylinder(const float x0, const float x1, const float radius, const int slices);
119
120
121 // helper functions
123 static void resizeAxesOf(const vec3 &newSize) {
127
128 static void resizeSolidOf(float newSize) {
132
133 static void setDirectionColor(ImU32 dColor, const ImU32 pColor) {
134 setDirectionColor(ImGui::ColorConvertU32ToFloat4(dColor), ImGui::ColorConvertU32ToFloat4(pColor)); }
135 static void setDirectionColor(const ImVec4 &dColor, const ImVec4 &pColor) {
137 directionColor = dColor; planeColor = pColor;
138 }
139 static void setDirectionColor(ImU32 color) { setDirectionColor(ImGui::ColorConvertU32ToFloat4(color)); }
140 static void setDirectionColor(const ImVec4& color) { setDirectionColor(color,ImVec4(color.x, color.y, color.z, STARTING_ALPHA_PLANE)); }
144
145 static void setSphereColors(const ImVec4& a, const ImVec4& b) {
146 setSphereColors( ImGui::ColorConvertFloat4ToU32(a), ImGui::ColorConvertFloat4ToU32(b)); }
147 static void setSphereColors(ImU32 a, ImU32 b) {
149 sphereColors[0] = a; sphereColors[1] = b; }
152
153
154 // gizmo mouse/key settings
156 // Call it once, to set all widgets... or if you need it
157 static void setGizmoFeelingRot(float f) { gizmoFeelingRot = f; } // default 1.0, >1 more mouse sensitivity, <1 less mouse sensitivity
158 static float getGizmoFeelingRot() { return gizmoFeelingRot; }
159
160#ifndef IMGUIZMO_USE_ONLY_ROT
161// available vgModifiers values:
162// evShiftModifier -> Shift
163// evControlModifier -> Ctrl
164// evAltModifier -> Alt
165// evSuperModifier -> Super
166 static void setPanModifier(vgModifiers v) { panMod = v; } // Change default assignment for Pan
167 static void setDollyModifier(vgModifiers v) { panMod = v; } // Change default assignment for Dolly
168
169 // Set the mouse response for the dolly operation... also wheel
170 static void setDollyScale(float scale) { dollyScale = scale; } // default 1.0, >1 more, <1 less
171 static float getDollyScale() { return dollyScale; }
172 // Set the mouse response for pan
173 static void setPanScale(float scale) { panScale = scale; } // default 1.0, >1 more, <1 less
174 static float getPanScale() { return panScale; }
175#endif
176
177
178 // internals
180 static bool solidAreBuilded;
181 static bool dragActivate;
182
185 bool showFullAxes = false;
186
187 bool drawFunc(const char* label, float size);
188
189 void modeSettings(int mode) {
190 drawMode = int(mode & modeMask); axesOriginType = int(mode & axesModeMask); showFullAxes = bool(modeFullAxes & mode); }
191
193
194 // vec3 -> quat -> trackbalTransforms -> quat -> vec3
196 bool getTransforms(quat& q, const char* label, vec3& dir, float size) {
197 float len = length(dir);
198
199 if(len<1.0 && len>= FLT_EPSILON) { normalize(dir); len = 1.0; }
200 else if(len< FLT_EPSILON) { dir = vec3(1.f, 0.f, 0.f); len = 1.0; }
201
202 q = angleAxis(acosf(dir.x/len), normalize(vec3(FLT_EPSILON, -dir.z, dir.y)));
203
204 bool ret = drawFunc(label, size);
205 if (ret) dir = (q * vec3(1.0f, 0.0f, 0.0f)) * len ; //return vector with original lenght
206
207 return ret;
208 }
209 // Vec4 (xyz axis, w angle) -> quat -> trackbalTransforms -> quat -> vec4
211 bool getTransforms(quat& q, const char* label, vec4& axis_angle, float size) {
212 q = angleAxis(axis_angle.w,vec3(axis_angle)); //g.ConvertFromAxisAngle();
213
214 bool ret = drawFunc(label, size);
215 if (ret) axis_angle = vec4(vec3(axis(q)),angle(q));
216
217 return ret;
218 }
219
220 //
221 // Settings
222 //
223 // axes/arrow are composed of cone (or pyramid) and cylinder
224 // (or parallelepiped): this solid are builded at first instance
225 // and will have same slices/radius/length for all controls in your
226 // applications but can be resized proportionally with a reductin
227 // factor: resizeAxesOf and resizeSolidOf (below).
228 // Also the colors of sphere tessellation are set at buil time,
229 // while colors of axes and cube are fixed
230 //
231 // if you want change solids attributes, change the static variables.
232 // If you need to resize solid and axes use resizeAxesOf and
233 // resizeSolidOf, they works like push/pop stack (without buffer!)
234 // with respective restoreAxesSize and restoreSolidSize.
235 // for example:
236 // // reDim axes ... same lenght,
237 // imguiGizmo::resizeAxesOf(vec3(imguiGizmo::axesResizeFactor.x, 2.0, 2.0));
238 // imguiGizmo::resizeSolidOf(1.25); // sphere bigger
239 // ImGui::gizmo3D("##RotB", b,sz);
240 // imguiGizmo::restoreSolidSize(); // restore at default
241 // imguiGizmo::restoreAxesSize();
243
244 //
245 // Build solid components
246 //
248
249 // arrow/axes components
251 static int coneSlices ;
252 static float coneRadius;
253 static float coneLength;
254
255 static int cylSlices ;
256 static float cylRadius ; // sizeCylLength ... defined in base to control size
257
258 // Sphere components
260 static float sphereRadius;
262
263 // Cube components
265 static float cubeSize;
266
267 // Plane components
269 static float planeSize;
270 static float planeThickness;
271
272
273 //
274 // Resizing and color settings
275 //
277
278
279 // Axes reduction
283
284 // solid reduction
286 static float solidResizeFactor;
288
289
290 static ImU32 sphereColors[2]; // Tessellation colors
291 static ImU32 savedSphereColors[2];
292 //ImU32 spherecolorA=0xff005cc0, spherecolorB=0xffc05c00;
293
294 static ImVec4 directionColor;
295 static ImVec4 savedDirectionColor;
296
297 static ImVec4 planeColor;
298 static ImVec4 savedPlaneColor;
299
300 // Gizmo mouse settings
302 static float gizmoFeelingRot; // >1 more mouse sensibility, <1 less mouse sensibility
303#ifndef IMGUIZMO_USE_ONLY_ROT
304 static float panScale, dollyScale;
306#endif
307
308 static const int imguiGizmoDefaultSize;
309
310};
311
312#define IMGUIZMO_DEF_SIZE (ImGui::GetFrameHeightWithSpacing()*4 - (ImGui::GetStyle().ItemSpacing.y*2))
313
314namespace ImGui
315{
316
319IMGUI_API bool gizmo3D(const char*, vec3&, float=IMGUIZMO_DEF_SIZE, const int=imguiGizmo::modeDirection);
320
324
325#ifndef IMGUIZMO_USE_ONLY_ROT
326
327//with Pan & Dolly feature
330IMGUI_API bool gizmo3D(const char*, vec3&, vec3&, float=IMGUIZMO_DEF_SIZE, const int=imguiGizmo::modeDirection);
331
335
336#endif
337};
338
339//#undef imguiGizmo_DEF_SIZE
#define IMGUI_API
Definition ImGuizmo.h:111
#define STARTING_ALPHA_PLANE
#define IMGUIZMO_DEF_SIZE
bool gizmo3D(const char *label, quat &q, float size, const int mode)
@ evLeftButton
Definition vGizmo.h:38
static void resizeAxesOf(const vec3 &newSize)
static void setPanModifier(vgModifiers v)
static ImVector< vec3 > planeNorm
static void setDirectionColor(const ImVec4 &color)
static ImVector< vec3 > cubeNorm
static float getGizmoFeelingRot()
static vgModifiers dollyMod
static void restoreAxesSize()
static void setDirectionColor(ImU32 color)
void setDualMode(const int mode)
static ImU32 sphereColors[2]
static vec3 axesResizeFactor
bool getTransforms(quat &q, const char *label, vec4 &axis_angle, float size)
bool getTransforms(quat &q, const char *label, vec3 &dir, float size)
bool drawFunc(const char *label, float size)
static ImVector< vec3 > planeVtx
static ImVector< int > sphereTess
vgButtons buttonPanDolly
static void setDirectionColor(const ImVec4 &dColor, const ImVec4 &pColor)
static float getDollyScale()
static ImVector< vec3 > cubeVtx
static ImVec4 planeColor
static ImVec4 savedPlaneColor
static float gizmoFeelingRot
static int cylSlices
static void setPanScale(float scale)
static float panScale
static ImVector< vec3 > arrowNorm[4]
static ImVec4 directionColor
static ImVector< vec3 > arrowVtx[4]
static float cubeSize
static float sphereRadius
static float solidResizeFactor
static vec3 savedAxesResizeFactor
static void restoreSphereColors()
static void setSphereColors(ImU32 a, ImU32 b)
static void setDollyModifier(vgModifiers v)
bool showFullAxes
static ImVector< vec3 > sphereVtx
static ImVec4 savedDirectionColor
static float coneLength
static float planeThickness
static bool dragActivate
static void restoreDirectionColor()
static float savedSolidResizeFactor
static void buildCone(const float x0, const float x1, const float radius, const int slices)
static void buildCylinder(const float x0, const float x1, const float radius, const int slices)
static void setDirectionColor(ImU32 dColor, const ImU32 pColor)
static void buildCube(const float size)
static void setGizmoFeelingRot(float f)
static ImU32 savedSphereColors[2]
static void buildSphere(const float radius, const int tessFactor)
static void buildPlane(const float size, const float thickness=planeThickness)
vec3 posPanDolly
static float cylRadius
static bool solidAreBuilded
static void setSphereColors(const ImVec4 &a, const ImVec4 &b)
static float getPanScale()
static const int imguiGizmoDefaultSize
static void setDollyScale(float scale)
static void resizeSolidOf(float newSize)
static int coneSlices
void modeSettings(int mode)
static float coneRadius
static void restoreSolidSize()
static vgModifiers panMod
static float dollyScale
static void buildPolygon(const vec3 &size, ImVector< vec3 > &vtx, ImVector< vec3 > &norm)
static float planeSize
static int sphereTessFactor
int vgButtons
Definition vGizmo.h:27
int vgModifiers
Definition vGizmo.h:28
vgm::Quat quat
Definition vgMath.h:633
vgm::Vec3 vec3
Definition vgMath.h:631
vgm::Vec4 vec4
Definition vgMath.h:632