summaryrefslogtreecommitdiff
path: root/TRRSS_01_rec/src/testApp.cpp
blob: 82affcb7ca24824c60bd473ac5bb9c0b82215a1c (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "testApp.h"
//texture binding with normalised coords
void bindTexture(ofBaseHasTexture &t) {
    ofTexture &tex = t.getTextureReference();
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTexture(ofBaseHasTexture &t) {
    t.getTextureReference().unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
void bindTex(ofTexture &tex) {
    tex.bind();

    glMatrixMode(GL_TEXTURE);
    glPushMatrix();
    glLoadIdentity();

    ofTextureData texData = tex.getTextureData();
    if(texData.textureTarget == GL_TEXTURE_RECTANGLE_ARB) {
        glScalef(tex.getWidth(), tex.getHeight(), 1.0f);
    } else {
        glScalef(tex.getWidth() / texData.tex_w, tex.getHeight() / texData.tex_h, 1.0f);
    }

    glMatrixMode(GL_MODELVIEW);
}
void unbindTex(ofTexture &tex) {
    tex.unbind();

    glMatrixMode(GL_TEXTURE);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}
//--------------------------------------------------------------
void testApp::setup(){
	isLive			= true;
	isRecording		= false;

#ifdef NEWAPI
	openNIDevice.setup(false);//FromXML("openni/config/ofxopenni_config.xml");
    openNIDevice.setLogLevel(OF_LOG_VERBOSE);
    openNIDevice.addDepthGenerator();
    openNIDevice.addImageGenerator();   // comment this out
    openNIDevice.addUserGenerator();
    openNIDevice.setRegister(true);
    openNIDevice.setMirror(true);
    openNIDevice.start();

    openNIDevice.setUseDepthRawPixels(true);

    //openNIDevice.setSafeThreading(true); ?slower and still crashes?

    openNIDevice.setMaxNumUsers(1);
    ofAddListener(openNIDevice.userEvent, this, &testApp::userEvent);

    ofxOpenNIUser user;
    user.setUseMaskTexture(true);
    user.setUsePointCloud(true);
    user.setPointCloudDrawSize(1); // this is the size of the glPoint that will be drawn for the point cloud
    user.setPointCloudResolution(1); // this is the step size between points for the cloud -> eg., this sets it to every second point
    openNIDevice.setBaseUserClass(user);
    
    
#else
    /*
	recordContext.setup();
	recordDepth.setup(&recordContext);
	recordImage.setup(&recordContext);
	recordUser.setup(&recordContext);
	recordUser.setUseCloudPoints(true);
	recordUser.setSmoothing(10.0f);
	recordContext.toggleRegisterViewport();
	*/
#endif

	setupRecording();
	whichUser=&recordUser;
	whichImage=&recordImage;

	guiWin=new guiWindow();
	ofxFenster* win=ofxFensterManager::get()->createFenster(0, 0,  200, 400, OF_WINDOW);
	win->setWindowTitle("config");
	win->addListener(guiWin);
	guiWin->setup();
}

void testApp::setupRecording(string _filename) {

	bool isCloud=true;

#if defined (TARGET_OSX) //|| defined(TARGET_LINUX) // only working on Mac/Linux at the moment (but on Linux you need to run as sudo...)
	hardware.setup();				// libusb direct control of motor, LED and accelerometers
	hardware.setLedOption(LED_OFF); // turn off the led just for yacks (or for live installation/performances ;-)
#endif

	recordContext.setup();	// all nodes created by code -> NOT using the xml config file at all
	//recordContext.setupUsingXMLFile();
	recordDepth.setup(&recordContext);
	recordImage.setup(&recordContext);

	recordUser.setup(&recordContext);
	//recordUser.setSmoothing(filterFactor);				// built in openni skeleton smoothing...
	//recordUser.setUseMaskPixels(isMasking);
	recordUser.setUseCloudPoints(isCloud);
	recordUser.setMaxNumberOfUsers(2);					// use this to set dynamic max number of users (NB: that a hard upper limit is defined by MAX_NUMBER_USERS in ofxUserGenerator)

	recordContext.toggleRegisterViewport();
	recordContext.toggleMirror();

	oniRecorder.setup(&recordContext, ONI_STREAMING);
	//oniRecorder.setup(&recordContext, ONI_CYCLIC, 60);
	//read the warning in ofxOpenNIRecorder about memory usage with ONI_CYCLIC recording!!!

}

void testApp::setupPlayback(string _filename) {

	bool isCloud=true;

	playContext.shutdown();
	playContext.setupUsingRecording(ofToDataPath(_filename));
	playDepth.setup(&playContext);
	playImage.setup(&playContext);

	playUser.setup(&playContext);
	//playUser.setSmoothing(filterFactor);				// built in openni skeleton smoothing...
	//playUser.setUseMaskPixels(isMasking);
	playUser.setUseCloudPoints(isCloud);

	playContext.toggleRegisterViewport();
	playContext.toggleMirror();

}

//--------------------------------------------------------------
void testApp::update(){
	if (isLive) {
#ifdef NEWAPI
		openNIDevice.update();
#else
		recordContext.update();
		recordDepth.update();
		recordImage.update();
		recordUser.update();
#endif
	}
	else {

		playContext.update();
		playDepth.update();
		playImage.update();
		playUser.update();
	}
	if (isRecording) oniRecorder.update();
}

//--------------------------------------------------------------
void testApp::draw(){
#ifdef NEWAPI
	ofSetColor(255, 255, 255);



    //openNIDevice.drawDebug(); // draws all generators
    //openNIDevice.drawDepth(0, 0);
    
    if (guiWin->drawPoints) {

/*
    	glEnable( GL_POINT_SMOOTH );
	    glEnable( GL_BLEND );
	    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
	    glPointSize( guiWin->pointSize );

		glBegin(GL_POINTS);
		glColor3f( 1.0f, 1.0f, 1.0f );

		
		//uint16_t *depth=openNIDevice.getDepthRawPixels().getPixels();
		ofPoint p;
    	for (int i=0;i<640;i+=guiWin->drawStep){
			for (int j=0;j<480;j+=guiWin->drawStep){
				p=openNIDevice.cameraToWorld(ofVec2f(i,j));
				glVertex3f(p.x,p.y,p.z);
			}
		}
*/

    	
    	ofPushMatrix();
	    ofEnableBlendMode(OF_BLENDMODE_ALPHA);
	    int numUsers = openNIDevice.getNumTrackedUsers();
	    for (int nID = 0; nID < numUsers; nID++){
	        ofxOpenNIUser & user = openNIDevice.getTrackedUser(nID);
	        user.drawMask();
	        ofPushMatrix();
	        ofTranslate((1024/2)-(640/2),(768/2)-(480/2),-500);
	        user.drawPointCloud();
	        ofPopMatrix();

	    }
	    ofDisableBlendMode();
	    ofPopMatrix();
	    
    
    }
    else {
    	openNIDevice.drawImage(0, 0,ofGetWidth(),ofGetHeight());
    }
#else

	//cam.begin();
	//bind texture recordImage
	//get point data from recordDepth
	//draw textured polys and allow manipulation
	float cloudWidth=ofGetWidth();
	float cloudHeight=ofGetHeight();

	ofPushMatrix();
		ofTranslate((ofGetWidth()/2)-(640/2),(ofGetHeight()/2)-(480/2),800);
		ofRotate(0.1,1,0.05,30);
		//ofRotate(0,0,1,180);
		
		int step = 1;
		
		if (guiWin->drawPoints) {
			// http://stackoverflow.com/questions/1513811/getting-smooth-big-points-in-opengl
			glEnable( GL_POINT_SMOOTH );
		    glEnable( GL_BLEND );
		    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		    glPointSize( guiWin->pointSize );

			glBegin(GL_POINTS);
			glColor3f( 1.0f, 1.0f, 1.0f );

			float spread=guiWin->distMax-guiWin->distMin;

			for(int y = step; y < 480; y += guiWin->drawStep) {

				for(int x = step; x < 640; x += guiWin->drawStep) {
					ofPoint pos4 = whichUser->getWorldCoordinateAt(x, y, 0); //userID);
					
					if (guiWin->distMin<pos4.z&&pos4.z<guiWin->distMax) {
						float l=(pos4.z-guiWin->distMin)/spread;
						glColor3f( l, l, l );

						glVertex3f(pos4.x, pos4.y,-pos4.z);
					}
				}
				
			}
			glEnd();
		}
		else {
			bindTex(whichImage->getTexture());
			for(int y = step; y < 480; y += guiWin->drawStep) {
				glBegin(GL_QUADS);
				for(int x = step; x < 640; x += guiWin->drawStep) {
					ofPoint pos1 = whichUser->getWorldCoordinateAt(x-step, y-step, 0); //userID);
					ofPoint pos2 = whichUser->getWorldCoordinateAt(x, y-step, 0); //userID);
					ofPoint pos3 = whichUser->getWorldCoordinateAt(x-step, y, 0); //userID);
					ofPoint pos4 = whichUser->getWorldCoordinateAt(x, y, 0); //userID);
					
					if ((guiWin->distMin<pos1.z&&pos1.z<guiWin->distMax)&&
						(guiWin->distMin<pos2.z&&pos2.z<guiWin->distMax)&&
						(guiWin->distMin<pos3.z&&pos3.z<guiWin->distMax)&&
						(guiWin->distMin<pos4.z&&pos4.z<guiWin->distMax)) {
					
						glTexCoord2f(((float)x-step)/640.0f,((float)y-step)/480.0f);
						glVertex3f(pos1.x, pos1.y,-pos1.z);
						glTexCoord2f(((float)x)/640.0f,((float)y-step)/480.0f);
						glVertex3f(pos2.x, pos2.y,-pos2.z);
						glTexCoord2f(((float)x)/640.0f,((float)y)/480.0f);
						glVertex3f(pos4.x, pos4.y,-pos4.z);
						glTexCoord2f(((float)x-step)/640.0f,((float)y)/480.0f);
						glVertex3f(pos3.x, pos3.y,-pos3.z);
					}
				}
				glEnd();
			}

			unbindTex(whichImage->getTexture());
		}
	ofPopMatrix();
	
	
	
	//recordImage.draw(0, 0, ofGetWidth(),ofGetHeight());
	
	//cam.end();
#endif
	
}
#ifdef NEWAPI
void testApp::userEvent(ofxOpenNIUserEvent & event){
    ofLogNotice() << getUserStatusAsString(event.userStatus) << "for user" << event.id << "from device" << event.deviceID;
}
#endif
void testApp::exit(){
#ifdef NEWAPI
    openNIDevice.stop();
#endif
}

string testApp::generateFileName() {

	string _root = "kinectRecord";

	string _timestamp = ofToString(ofGetDay()) +
	ofToString(ofGetMonth()) +
	ofToString(ofGetYear()) +
	ofToString(ofGetHours()) +
	ofToString(ofGetMinutes()) +
	ofToString(ofGetSeconds());

	string _filename = (_root + _timestamp + ".oni");

	return _filename;

}

//--------------------------------------------------------------
void testApp::keyPressed(int key){
	switch (key) {
		case 's':
		case 'S':
			if (isRecording) {
				oniRecorder.stopRecord();
				isRecording = false;
				break;
			} else {
				oniRecorder.startRecord(generateFileName());
				isRecording = true;
				break;
			}
			break;
		case 'p':
		case 'P':
			if (oniRecorder.getCurrentFileName() != "" && !isRecording && isLive) {
				setupPlayback(oniRecorder.getCurrentFileName());
				isLive = false;
				whichUser=&playUser;
				whichImage=&playImage;
			} else {
				isLive = true;
				whichUser=&recordUser;
				whichImage=&recordImage;
			}
			break;
	}
}

//--------------------------------------------------------------
void testApp::keyReleased(int key){

}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){

}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::mouseReleased(int x, int y, int button){

}

//--------------------------------------------------------------
void testApp::windowResized(int w, int h){

}

//--------------------------------------------------------------
void testApp::gotMessage(ofMessage msg){

}

//--------------------------------------------------------------
void testApp::dragEvent(ofDragInfo dragInfo){

}