-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
246 lines (193 loc) · 6.2 KB
/
main.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
244
245
246
#include "main.h"
#include "glut.h"
#include <cmath>
#include <cstdio>
#include <Windows.h>
#include <Ole2.h>
#include <Kinect.h>
// We'll be using buffer objects to store the kinect point cloud
GLuint vboId;
GLuint cboId;
// Intermediate Buffers
unsigned char rgbimage[colorwidth*colorheight * 4]; // Stores RGB color image
ColorSpacePoint depth2rgb[width*height]; // Maps depth pixels to rgb pixels
CameraSpacePoint depth2xyz[width*height]; // Maps depth pixels to 3d coordinates
// Many skeletal tracking variables
BOOLEAN trackedMany[BODY_COUNT];
Joint jointsMany[BODY_COUNT][JointType_Count];
float depthMany[BODY_COUNT];
// Kinect Variables
IKinectSensor* sensor; // Kinect sensor
IMultiSourceFrameReader* reader; // Kinect data source
ICoordinateMapper* mapper; // Converts between depth, color, and 3d coordinates
bool initKinect() {
if (FAILED(GetDefaultKinectSensor(&sensor))) {
return false;
}
if (sensor) {
sensor->get_CoordinateMapper(&mapper);
sensor->Open();
sensor->OpenMultiSourceFrameReader(
FrameSourceTypes::FrameSourceTypes_Depth | FrameSourceTypes::FrameSourceTypes_Color | FrameSourceTypes::FrameSourceTypes_Body,
&reader);
return reader;
}
else {
return false;
}
}
void getDepthData(IMultiSourceFrame* frame, GLubyte* dest) {
IDepthFrame* depthframe;
IDepthFrameReference* frameref = NULL;
frame->get_DepthFrameReference(&frameref);
frameref->AcquireFrame(&depthframe);
if (frameref) frameref->Release();
if (!depthframe) return;
// Get data from frame
unsigned int sz;
unsigned short* buf;
depthframe->AccessUnderlyingBuffer(&sz, &buf);
// Write vertex coordinates
mapper->MapDepthFrameToCameraSpace(width*height, buf, width*height, depth2xyz);
float* fdest = (float*)dest;
for (int i = 0; i < sz; i++) {
*fdest++ = depth2xyz[i].X;
*fdest++ = depth2xyz[i].Y;
*fdest++ = depth2xyz[i].Z;
}
// Fill in depth2rgb map
mapper->MapDepthFrameToColorSpace(width*height, buf, width*height, depth2rgb);
if (depthframe) depthframe->Release();
}
void getRgbData(IMultiSourceFrame* frame, GLubyte* dest) {
IColorFrame* colorframe;
IColorFrameReference* frameref = NULL;
frame->get_ColorFrameReference(&frameref);
frameref->AcquireFrame(&colorframe);
if (frameref) frameref->Release();
if (!colorframe) return;
// Get data from frame
colorframe->CopyConvertedFrameDataToArray(colorwidth*colorheight * 4, rgbimage, ColorImageFormat_Rgba);
// Write color array for vertices
float* fdest = (float*)dest;
for (int i = 0; i < width*height; i++) {
ColorSpacePoint p = depth2rgb[i];
// Check if color pixel coordinates are in bounds
if (p.X < 0 || p.Y < 0 || p.X > colorwidth || p.Y > colorheight) {
*fdest++ = 0;
*fdest++ = 0;
*fdest++ = 0;
}
else {
int idx = (int)p.X + colorwidth * (int)p.Y;
*fdest++ = rgbimage[4 * idx + 0] / 255.;
*fdest++ = rgbimage[4 * idx + 1] / 255.;
*fdest++ = rgbimage[4 * idx + 2] / 255.;
}
}
if (colorframe) colorframe->Release();
}
void getAllBodyData(IMultiSourceFrame* frame)
{
IBodyFrame* bodyframe;
IBodyFrameReference* frameref = NULL;
frame->get_BodyFrameReference(&frameref);
frameref->AcquireFrame(&bodyframe);
if (frameref) frameref->Release();
if (!bodyframe) return;
IBody* body[BODY_COUNT] = { 0 };
BOOLEAN tracked_body[BODY_COUNT];
bodyframe->GetAndRefreshBodyData(BODY_COUNT, body);
for (int i = 0; i < BODY_COUNT; i++) {
body[i]->get_IsTracked(&trackedMany[i]);
if (trackedMany[i]) {
body[i]->GetJoints(JointType_Count, jointsMany[i]);
}
}
if (bodyframe) bodyframe->Release();
}
void getKinectData() {
IMultiSourceFrame* frame = NULL;
if (SUCCEEDED(reader->AcquireLatestFrame(&frame))) {
GLubyte* ptr;
glBindBuffer(GL_ARRAY_BUFFER, vboId);
ptr = (GLubyte*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getDepthData(frame, ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
ptr = (GLubyte*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
if (ptr) {
getRgbData(frame, ptr);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
// getBodyData(frame);
getAllBodyData(frame);
}
if (frame) frame->Release();
}
void drawKinectData() {
getKinectData();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glVertexPointer(3, GL_FLOAT, 0, NULL);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glColorPointer(3, GL_FLOAT, 0, NULL);
glPointSize(1.f);
glDrawArrays(GL_POINTS, 0, width*height);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
for (int i = 0; i < BODY_COUNT; i++)
{
if (trackedMany[i]) {
// Draw some arms
const CameraSpacePoint& lh = jointsMany[i][JointType_FootLeft].Position;
const CameraSpacePoint& le = jointsMany[i][JointType_KneeLeft].Position;
const CameraSpacePoint& m = jointsMany[i][JointType_SpineBase].Position;
const CameraSpacePoint& rh = jointsMany[i][JointType_FootRight].Position;
const CameraSpacePoint& re = jointsMany[i][JointType_KneeRight].Position;
depthMany[i] = m.Z;
printf("depth of %d th person : %f\n", i+1, depthMany[i]);
glBegin(GL_LINES);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(lh.X, lh.Y, lh.Z);
glVertex3f(le.X, le.Y, le.Z);
glVertex3f(le.X, le.Y, le.Z);
glVertex3f(m.X, m.Y, m.Z);
glVertex3f(rh.X, rh.Y, rh.Z);
glVertex3f(re.X, re.Y, re.Z);
glVertex3f(re.X, re.Y, re.Z);
glVertex3f(m.X, m.Y, m.Z);
glEnd();
}
}
}
int main(int argv,char ** argc) {
if (!init(argv, argc)) return 1;
if (!initKinect()) return 1;
// OpenGL setup
glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);
// Set up array buffers
const int dataSize = width * height * 3 * 4;
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
glGenBuffers(1, &cboId);
glBindBuffer(GL_ARRAY_BUFFER, cboId);
glBufferData(GL_ARRAY_BUFFER, dataSize, 0, GL_DYNAMIC_DRAW);
// Camera setup
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, width / (GLdouble)height, 0.1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, 1, 0, 1, 0);
// Main loop
execute();
return 0;
}