-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenNI-multi-device-ogl.cpp
More file actions
326 lines (272 loc) · 6.42 KB
/
OpenNI-multi-device-ogl.cpp
File metadata and controls
326 lines (272 loc) · 6.42 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
// Example to display multiple OpenNI device in point cloud with OpenGL
//
// by Heresy
// http://kheresy.wordpress.com
//
// version 1.00 @2014/11/18
// Standard Header
#include <iostream>
#include <vector>
// glut Header
#include <GL/glut.h>
// OpenNI Header
#include <OpenNI.h>
// helper header
#include "../../common/OpenGLCamera.h"
// namespace
using namespace std;
using namespace openni;
// class mapping to one device
class CDevice
{
public:
Device mDevice;
VideoStream mDepthStream;
VideoStream mColorStream;
double aTransformation[16];
CDevice(const char* uri)
{
// initial matrix
for (int y = 0; y < 4; ++y)
for (int x = 0; x < 4; ++x)
{
int idx = x + y * 4;
if (x == y)
aTransformation[idx] = 1;
else
aTransformation[idx] = 0;
}
// initial OpenNI device
if (mDevice.open(uri) == STATUS_OK)
{
if (mDepthStream.create(mDevice, SENSOR_DEPTH) == STATUS_OK)
{
// start
mDepthStream.start();
}
else
{
cerr << "Can't create depth stream of device: " << uri << endl;
}
if (mColorStream.create(mDevice, SENSOR_COLOR) == STATUS_OK)
{
// start
mColorStream.start();
}
else
{
cerr << "Can't create color stream of device: " << uri << endl;
}
}
else
{
cerr << "Can't open device at: " << uri << endl;
}
}
~CDevice()
{
if (mDepthStream.isValid())
{
mDepthStream.stop();
mDepthStream.destroy();
}
if (mColorStream.isValid())
{
mColorStream.stop();
mColorStream.destroy();
}
if (mDevice.isValid())
{
mDevice.close();
}
}
};
// global OpenNI object
vector<CDevice*> g_vDevices;
// global object
SimpleCamera g_Camera;
// shutdown OpenNI when exit program
void exit_program()
{
for (vector<CDevice*>::iterator itDevice = g_vDevices.begin(); itDevice != g_vDevices.end(); ++itDevice)
{
delete *itDevice;
}
g_vDevices.clear();
OpenNI::shutdown();
}
// glut display function(draw)
void display()
{
// clear previous screen
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// draw points
glPointSize(1.0f);
// for each device
for (vector<CDevice*>::iterator itDevice = g_vDevices.begin(); itDevice != g_vDevices.end(); ++itDevice)
{
CDevice& rDevice = **itDevice;
// get depth frame
VideoFrameRef vfDepthFrame;
if (rDevice.mDepthStream.readFrame(&vfDepthFrame) == STATUS_OK)
{
const DepthPixel* pDepthArray = static_cast<const DepthPixel*>(vfDepthFrame.getData());
// get color frame
VideoFrameRef vfColorFrame;
if (rDevice.mColorStream.readFrame(&vfColorFrame) == STATUS_OK)
{
const RGB888Pixel* pColorArray = static_cast<const RGB888Pixel*>(vfColorFrame.getData());
// apply transform matrix
glPushMatrix();
glMultMatrixd(rDevice.aTransformation);
// draw points with color
glBegin(GL_POINTS);
int w = vfDepthFrame.getWidth(),
h = vfDepthFrame.getHeight();
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
int idx = x + y * w;
const DepthPixel& rDepth = pDepthArray[idx];
if (rDepth != 0)
{
const RGB888Pixel& rColor = pColorArray[idx];
glColor3ub(rColor.r, rColor.g, rColor.b);
float fX, fY, fZ;
CoordinateConverter::convertDepthToWorld(rDevice.mDepthStream, x, y, rDepth, &fX, &fY, &fZ);
glVertex3f(fX, fY, fZ);
}
}
}
glEnd();
glPopMatrix();
vfColorFrame.release();
}
}
vfDepthFrame.release();
}
// Coordinate
glLineWidth( 5.0f );
glBegin( GL_LINES );
glColor3ub( 255, 0, 0 );
glVertex3f( 0, 0, 0 );
glVertex3f( 100, 0, 0 );
glColor3ub( 0, 255, 0 );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 100, 0 );
glColor3ub( 0, 0, 255 );
glVertex3f( 0, 0, 0 );
glVertex3f( 0, 0, 100 );
glEnd();
// swap buffer
glutSwapBuffers();
}
// glut idle function
void idle()
{
glutPostRedisplay();
}
// glut keyboard function
void keyboard( unsigned char key, int x, int y )
{
float fSpeed = 10.0f;
switch( key )
{
case 'q':
exit(0);
break;
case 's':
g_Camera.MoveForward( -fSpeed );
break;
case 'w':
g_Camera.MoveForward( fSpeed );
break;
case 'a':
g_Camera.MoveSide( -fSpeed );
break;
case 'd':
g_Camera.MoveSide( fSpeed );
break;
case 'z':
g_Camera.MoveUp( -fSpeed );
break;
case 'x':
g_Camera.MoveUp( fSpeed );
break;
}
}
// glut special keyboard function
void specialKey( int key, int x, int y )
{
float fRotateScale = 0.01f;
switch( key )
{
case GLUT_KEY_DOWN:
g_Camera.RotateUp( -fRotateScale );
break;
case GLUT_KEY_UP:
g_Camera.RotateUp( fRotateScale );
break;
case GLUT_KEY_RIGHT:
g_Camera.RotateSide( fRotateScale );
break;
case GLUT_KEY_LEFT:
g_Camera.RotateSide( -fRotateScale );
break;
}
}
int main( int argc, char** argv )
{
// Initial OpenNI
if( OpenNI::initialize() != STATUS_OK )
{
cerr << "OpenNI Initial Error: " << OpenNI::getExtendedError() << endl;
return -1;
}
// enumerate devices
Array<DeviceInfo> aDeviceList;
OpenNI::enumerateDevices(&aDeviceList);
// output information
cout << "There are " << aDeviceList.getSize() << " devices on this system." << endl;
for (int i = 0; i < aDeviceList.getSize(); ++i)
{
cout << "Device " << i << "\n";
const DeviceInfo& rDevInfo = aDeviceList[i];
cout << " " << rDevInfo.getName() << " by " << rDevInfo.getVendor() << "\n";
cout << " PID: " << rDevInfo.getUsbProductId() << "\n";
cout << " VID: " << rDevInfo.getUsbVendorId() << "\n";
cout << " URI: " << rDevInfo.getUri() << endl;
// initialize
CDevice* pDevWin = new CDevice(aDeviceList[i].getUri());
if (pDevWin->mColorStream.isValid() && pDevWin->mDepthStream.isValid())
{
// appleytest matrix
pDevWin->aTransformation[12] = i * -1000;
g_vDevices.push_back(pDevWin);
}
}
// initial glut
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
// create glut window
glutInitWindowSize( 640, 480 );
glutCreateWindow( "OpenNI 3D OpenGL 3D Point" );
glEnable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
glMatrixMode( GL_PROJECTION );
gluPerspective( 40.0, 1.0, 300.0, 20000.0 ); // FOV, aspect ration, near, far
g_Camera.vCenter = Vector3( 0.0, 0.0, 1000.0 );
g_Camera.vPosition = Vector3( 0.0, 0.0, -2000.0 );
g_Camera.vUpper = Vector3( 0.0, 1.0, 0.0 );
g_Camera.SetCamera();
// register glut callback functions
glutDisplayFunc( display );
glutIdleFunc( idle );
glutKeyboardFunc( keyboard );
glutSpecialFunc( specialKey );
std::atexit(exit_program);
// start
glutMainLoop();
return 0;
}