This repository has been archived by the owner on Nov 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
266 lines (223 loc) · 6.73 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//added for OSX compilation
#define USE_GLUT
/****************************************************************************
* *
* Nite 1.3 - Point Viewer Sample with TUIO output *
* *
* Author: Oz Magal *
* TUIO support added by Matt Cook ([email protected]) *
* *
****************************************************************************/
/****************************************************************************
* *
* Nite 1.3 *
* Copyright (C) 2006 PrimeSense Ltd. All Rights Reserved. *
* *
* This file has been provided pursuant to a License Agreement containing *
* restrictions on its use. This data contains valuable trade secrets *
* and proprietary information of PrimeSense Ltd. and is protected by law. *
* *
****************************************************************************/
// Headers for OpenNI
#include <XnOpenNI.h>
#include <XnCppWrapper.h>
#include <XnHash.h>
#include <XnLog.h>
// Header for NITE
#include "XnVNite.h"
// local header
#include "PointDrawerTUIO.h"
#define CHECK_RC(rc, what) \
if (rc != XN_STATUS_OK) \
{ \
printf("%s failed: %s\n", what, xnGetStatusString(rc)); \
return rc; \
}
#define CHECK_ERRORS(rc, errors, what) \
if (rc == XN_STATUS_NO_NODE_PRESENT) \
{ \
XnChar strError[1024]; \
errors.ToString(strError, 1024); \
printf("%s\n", strError); \
return (rc); \
}
#ifdef USE_GLUT
#if (XN_PLATFORM == XN_PLATFORM_MACOSX)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#endif
// OpenNI objects
xn::Context g_Context;
xn::DepthGenerator g_DepthGenerator;
xn::HandsGenerator g_HandsGenerator;
// NITE objects
XnVSessionManager* g_pSessionManager;
XnVFlowRouter* g_pFlowRouter;
// the drawer
PointDrawerTUIO* g_pDrawer;
#define GL_WIN_SIZE_X 720
#define GL_WIN_SIZE_Y 480
// Draw the depth map?
XnBool g_bDrawDepthMap = true;
XnBool g_bPrintFrameID = false;
// Use smoothing?
XnFloat g_fSmoothing = 0.0f;
XnBool g_bPause = false;
XnBool g_bQuit = false;
SessionState g_SessionState = NOT_IN_SESSION;
void CleanupExit()
{
//delete tuioServer;
g_Context.Shutdown();
exit (1);
}
// Callback for when the focus is in progress
void XN_CALLBACK_TYPE FocusProgress(const XnChar* strFocus, const XnPoint3D& ptPosition, XnFloat fProgress, void* UserCxt)
{
// printf("Focus progress: %s @(%f,%f,%f): %f\n", strFocus, ptPosition.X, ptPosition.Y, ptPosition.Z, fProgress);
}
// callback for session start
void XN_CALLBACK_TYPE SessionStarting(const XnPoint3D& ptPosition, void* UserCxt)
{
printf("Session start: (%f,%f,%f)\n", ptPosition.X, ptPosition.Y, ptPosition.Z);
g_SessionState = IN_SESSION;
}
// Callback for session end
void XN_CALLBACK_TYPE SessionEnding(void* UserCxt)
{
printf("Session end\n");
g_SessionState = NOT_IN_SESSION;
}
void XN_CALLBACK_TYPE NoHands(void* UserCxt)
{
if (g_SessionState != NOT_IN_SESSION)
{
printf("Quick refocus\n");
g_SessionState = QUICK_REFOCUS;
}
}
// this function is called each frame
void glutDisplay (void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Setup the OpenGL viewpoint
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
XnMapOutputMode mode;
g_DepthGenerator.GetMapOutputMode(mode);
#ifdef USE_GLUT
glOrtho(0, mode.nXRes, mode.nYRes, 0, -1.0, 1.0);
#else
glOrthof(0, mode.nXRes, mode.nYRes, 0, -1.0, 1.0);
#endif
glDisable(GL_TEXTURE_2D);
if (!g_bPause)
{
// Read next available data
g_Context.WaitAndUpdateAll();
// Update NITE tree
g_pSessionManager->Update(&g_Context);
PrintSessionState(g_SessionState);
}
#ifdef USE_GLUT
glutSwapBuffers();
#endif
}
#ifdef USE_GLUT
void glutIdle (void)
{
if (g_bQuit) {
CleanupExit();
}
// Display the frame
glutPostRedisplay();
}
void glutKeyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 27:
// Exit
CleanupExit();
case'p':
// Toggle pause
g_bPause = !g_bPause;
break;
case 'd':
// Toggle drawing of the depth map
g_bDrawDepthMap = !g_bDrawDepthMap;
g_pDrawer->SetDepthMap(g_bDrawDepthMap);
break;
case 'f':
g_bPrintFrameID = !g_bPrintFrameID;
g_pDrawer->SetFrameID(g_bPrintFrameID);
break;
case 's':
// Toggle smoothing
if (g_fSmoothing == 0)
g_fSmoothing = 0.1;
else
g_fSmoothing = 0;
g_HandsGenerator.SetSmoothing(g_fSmoothing);
break;
case 'e':
// end current session
g_pSessionManager->EndSession();
break;
}
}
void glInit (int * pargc, char ** argv)
{
glutInit(pargc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
glutCreateWindow ("PrimeSense Nite Point Viewer");
//glutFullScreen();
glutSetCursor(GLUT_CURSOR_NONE);
glutKeyboardFunc(glutKeyboard);
glutDisplayFunc(glutDisplay);
glutIdleFunc(glutIdle);
glDisable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
#endif
// xml to initialize OpenNI
#define SAMPLE_XML_PATH "Sample-Tracking.xml"
int main(int argc, char ** argv)
{
XnStatus rc = XN_STATUS_OK;
xn::EnumerationErrors errors;
// Initialize OpenNI
rc = g_Context.InitFromXmlFile(SAMPLE_XML_PATH, &errors);
CHECK_ERRORS(rc, errors, "InitFromXmlFile");
CHECK_RC(rc, "InitFromXmlFile");
rc = g_Context.FindExistingNode(XN_NODE_TYPE_DEPTH, g_DepthGenerator);
CHECK_RC(rc, "Find depth generator");
rc = g_Context.FindExistingNode(XN_NODE_TYPE_HANDS, g_HandsGenerator);
CHECK_RC(rc, "Find hands generator");
// Create NITE objects
g_pSessionManager = new XnVSessionManager;
rc = g_pSessionManager->Initialize(&g_Context, "Click,Wave", "RaiseHand");
CHECK_RC(rc, "SessionManager::Initialize");
g_pSessionManager->RegisterSession(NULL, SessionStarting, SessionEnding, FocusProgress);
g_pDrawer = new PointDrawerTUIO(20, g_DepthGenerator);
g_pFlowRouter = new XnVFlowRouter;
g_pFlowRouter->SetActive(g_pDrawer);
g_pSessionManager->AddListener(g_pFlowRouter);
g_pDrawer->RegisterNoPoints(NULL, NoHands);
g_pDrawer->SetDepthMap(g_bDrawDepthMap);
// Initialization done. Start generating
rc = g_Context.StartGeneratingAll();
CHECK_RC(rc, "StartGenerating");
// Mainloop
#ifdef USE_GLUT
glInit(&argc, argv);
glutMainLoop();
#else
#endif
}