This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
imguizmo-wrapper.c
133 lines (119 loc) · 4.21 KB
/
imguizmo-wrapper.c
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
#include "rizz/imgui-extra.h"
#include "imguizmo/ImGuizmo.h"
#include "imgui-internal.h"
#include "sx/math-vec.h"
// gizmo
static void imgui__gizmo_set_rect(const sx_rect rc)
{
ImGuizmo_SetRect(rc.xmin, rc.ymin, rc.xmax - rc.xmin, rc.ymax - rc.ymin);
}
static void imgui__gizmo_decompose_mat4(const sx_mat4* mat, sx_vec3* translation, sx_vec3* rotation,
sx_vec3* scale)
{
sx_mat4 transpose = sx_mat4_transpose(mat);
ImGuizmo_DecomposeMatrixToComponents(transpose.f, translation->f, rotation->f, scale->f);
}
static void imgui__gizmo_compose_mat4(sx_mat4* mat, const sx_vec3 translation,
const sx_vec3 rotation, const sx_vec3 scale)
{
ImGuizmo_RecomposeMatrixFromComponents(translation.f, rotation.f, scale.f, mat->f);
sx_mat4_transpose(mat);
}
static inline void imgui__mat4_to_gizmo(float dest[16], const sx_mat4* src)
{
dest[0] = src->m11;
dest[1] = src->m21;
dest[2] = src->m31;
dest[3] = src->m41;
dest[4] = src->m12;
dest[5] = src->m22;
dest[6] = src->m32;
dest[7] = src->m42;
dest[8] = src->m13;
dest[9] = src->m23;
dest[10] = src->m33;
dest[11] = src->m43;
dest[12] = src->m14;
dest[13] = src->m24;
dest[14] = src->m34;
dest[15] = src->m44;
}
static inline sx_mat4 imgui__mat4_from_gizmo(const float src[16])
{
return sx_mat4v(
sx_vec4f(src[0], src[1], src[2], src[3]), sx_vec4f(src[4], src[5], src[6], src[7]),
sx_vec4f(src[8], src[9], src[10], src[11]), sx_vec4f(src[12], src[13], src[14], src[15]));
}
static void imgui__gizmo_translate(sx_mat4* mat, const sx_mat4* view, const sx_mat4* proj,
imgui_gizmo_mode mode, sx_mat4* delta_mat, sx_vec3* snap)
{
float _mat[16];
float tview[16];
float tproj[16];
float _delta[16];
imgui__mat4_to_gizmo(_mat, mat);
imgui__mat4_to_gizmo(tview, view);
imgui__mat4_to_gizmo(tproj, proj);
ImGuizmo_Manipulate(tview, tproj, TRANSLATE, (enum MODE)mode, _mat, delta_mat ? _delta : NULL,
snap ? snap->f : NULL, NULL, NULL);
if (delta_mat)
*delta_mat = imgui__mat4_from_gizmo(_delta);
*mat = imgui__mat4_from_gizmo(_mat);
}
static void imgui__gizmo_rotation(sx_mat4* mat, const sx_mat4* view, const sx_mat4* proj,
imgui_gizmo_mode mode, sx_mat4* delta_mat, float* snap)
{
float _mat[16];
float tview[16];
float tproj[16];
float _delta[16];
imgui__mat4_to_gizmo(_mat, mat);
imgui__mat4_to_gizmo(tview, view);
imgui__mat4_to_gizmo(tproj, proj);
ImGuizmo_Manipulate(tview, tproj, ROTATE, (enum MODE)mode, _mat, delta_mat ? _delta : NULL,
snap, NULL, NULL);
if (delta_mat)
*delta_mat = imgui__mat4_from_gizmo(_delta);
*mat = imgui__mat4_from_gizmo(_mat);
}
static void imgui__gizmo_scale(sx_mat4* mat, const sx_mat4* view, const sx_mat4* proj,
imgui_gizmo_mode mode, sx_mat4* delta_mat, float* snap)
{
float _mat[16];
float tview[16];
float tproj[16];
float _delta[16];
imgui__mat4_to_gizmo(_mat, mat);
imgui__mat4_to_gizmo(tview, view);
imgui__mat4_to_gizmo(tproj, proj);
ImGuizmo_Manipulate(tview, tproj, SCALE, (enum MODE)mode, _mat, delta_mat ? _delta : NULL, snap,
NULL, NULL);
if (delta_mat)
*delta_mat = imgui__mat4_from_gizmo(_delta);
*mat = imgui__mat4_from_gizmo(_mat);
}
void imgui__imguizmo_begin(void)
{
ImGuizmo_BeginFrame();
}
void imgui__imguizmo_setrect(float x, float y, float width, float height)
{
ImGuizmo_SetRect(x, y, width, height);
}
void imgui__get_imguizmo_api(rizz_api_imguizmo* api)
{
sx_assert(api);
*api = (rizz_api_imguizmo) {
.hover = ImGuizmo_IsOver,
.is_using = ImGuizmo_IsUsing,
.set_current_window = ImGuizmo_SetDrawlist,
.set_ortho = ImGuizmo_SetOrthographic,
.enable = ImGuizmo_Enable,
.set_rect = imgui__gizmo_set_rect,
.decompose_mat4 = imgui__gizmo_decompose_mat4,
.compose_mat4 = imgui__gizmo_compose_mat4,
.translate = imgui__gizmo_translate,
.rotation = imgui__gizmo_rotation,
.scale = imgui__gizmo_scale
};
}