-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgkinect.cpp
243 lines (206 loc) · 5.78 KB
/
gkinect.cpp
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
// Just some sample code from a tutorial
// This is a module I am creating for the Godot game engine
// so that it supports the microsoft kinect.
#include "gkinect.h"
#include <Windows.h>
#include <Ole2.h>
#include <Kinect.h>
#include <vector>
#include "core/os/os.h"
#include "core/image.h"
IKinectSensor* sensor = NULL; // Kinect sensor
IMultiSourceFrameReader* reader = NULL; // Kinect data source
ICoordinateMapper* mapper = NULL;
BOOLEAN tracked;
Joint bodies[BODY_COUNT][JointType_Count];
GKinect* GKinect::singleton;
static bool already_initialized = false;
static unsigned char* image;
size_t imageSize = 1920 * 1080 * 4;
GKinect::GKinect() {
singleton = this;
}
GKinect* GKinect::get_singleton()
{
return singleton;
}
void GKinect::initialize()
{
if (!already_initialized)
{
if (FAILED(GetDefaultKinectSensor(&sensor)))
{
return;
}
if (sensor)
{
sensor->get_CoordinateMapper(&mapper);
sensor->Open();
sensor->OpenMultiSourceFrameReader(FrameSourceTypes::FrameSourceTypes_Body,
&reader);
}
else
{
return;
}
already_initialized = true;
}
}
inline Vector3 JointToVec(Joint j)
{
return Vector3(j.Position.X, j.Position.Y, j.Position.Z);
}
Vector3 GKinect::get_joint(int p_body_id, int p_joint_id)
{
if (p_joint_id >= JointType_Count || p_joint_id < 0)
{
return Vector3(0, 0, 0);
}
else
{
return JointToVec(bodies[p_body_id][p_joint_id]);
}
}
inline std::string JointToName(int j)
{
switch (j)
{
case JointType_SpineBase:
return "spine_base";
case JointType_SpineMid:
return "spine_mid";
case JointType_Neck:
return "neck";
case JointType_Head:
return "head";
case JointType_ShoulderLeft:
return "shoulder_left";
case JointType_ElbowLeft:
return "elbow_left";
case JointType_WristLeft:
return "wrist_left";
case JointType_HandLeft:
return "hand_left";
case JointType_ShoulderRight:
return "shoulder_right";
case JointType_ElbowRight:
return "elbow_right";
case JointType_WristRight:
return "wrist_right";
case JointType_HandRight:
return "hand_right";
case JointType_HipLeft:
return "hip_left";
case JointType_KneeLeft:
return "knee_left";
case JointType_AnkleLeft:
return "ankle_left";
case JointType_FootLeft:
return "foot_left";
case JointType_HipRight:
return "hip_right";
case JointType_KneeRight:
return "knee_right";
case JointType_AnkleRight:
return "ankle_right";
case JointType_FootRight:
return "foot_right";
case JointType_SpineShoulder:
return "spine_shoulder";
case JointType_HandTipLeft:
return "hand_tip_left";
case JointType_ThumbLeft:
return "thumb_left";
case JointType_HandTipRight:
return "hand_tip_right";
case JointType_ThumbRight:
return "thumb_right";
default:
return "null";
}
}
Dictionary GKinect::get_skeleton()
{
Dictionary skeleton;
int body_id = 0;
for (int i = 0; i < JointType_Count; i++)
{
const char* name = JointToName(i).c_str();
skeleton[name] = JointToVec(bodies[body_id][i]);
}
return skeleton;
}
void GKinect::update_joints()
{
IMultiSourceFrame* frame = NULL;
if (SUCCEEDED(reader->AcquireLatestFrame(&frame)))
{
IBodyFrame* bodyframe;
IBodyFrameReference* bodyframeref = NULL;
frame->get_BodyFrameReference(&bodyframeref);
bodyframeref->AcquireFrame(&bodyframe);
if (bodyframeref) bodyframeref->Release();
if (bodyframe)
{
IBody* body[BODY_COUNT] = { 0 };
bodyframe->GetAndRefreshBodyData(BODY_COUNT, body);
int tracked_bodies = 0;
for (int i = 0; i < BODY_COUNT; i++)
{
body[i]->get_IsTracked(&tracked);
if (tracked)
{
tracked_bodies += 1;
body[i]->GetJoints(JointType_Count, &bodies[i][0]);
}
}
if (bodyframe) bodyframe->Release();
}
if (frame)
{
frame->Release();
}
}
}
void GKinect::_bind_methods()
{
ClassDB::bind_method(D_METHOD("update_joints"), &GKinect::update_joints);
ClassDB::bind_method(D_METHOD("get_skeleton"), &GKinect::get_skeleton);
ClassDB::bind_method(D_METHOD("initialize"), &GKinect::initialize);
ClassDB::bind_method(D_METHOD("get_joint", "body_id", "joint_id"), &GKinect::get_joint);
}
GKinectEditor *GKinectEditor::singleton = NULL;
GKinectEditor::GKinectEditor(EditorNode *p_editor)
{
editor = p_editor;
singleton = this;
OS::get_singleton()->print("Hello!");
}
void GKinectEditorPlugin::make_visible(bool p_visible)
{
if (p_visible)
{
g_kinect_editor->show();
}
else
{
g_kinect_editor->hide();
}
}
GKinectEditorPlugin::GKinectEditorPlugin(EditorNode *p_node)
{
editor = p_node;
g_kinect_editor = memnew(GKinectEditor(editor));
editor->get_viewport()->add_child(g_kinect_editor);
g_kinect_editor->hide();
GKinect* g_singleton = GKinect::get_singleton();
g_singleton->initialize();
memalloc(imageSize);
image = (unsigned char*)malloc(imageSize);
memset(image, 255, imageSize);
Image a;
a.create(1920, 1080, true, Image::FORMAT_RGBA8, image);
}
GKinectEditorPlugin::~GKinectEditorPlugin()
{
}