/
ofApp.cpp
277 lines (220 loc) · 7.65 KB
/
ofApp.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
267
268
269
270
271
272
273
274
275
276
277
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofSetLogLevel(OF_LOG_VERBOSE);
kinect.setRegistration(true);
kinect.init();
kinect.open(); // opens first available kinect
// print the intrinsic IR sensor values
if(kinect.isConnected()) {
ofLogNotice() << "sensor-emitter dist: " << kinect.getSensorEmitterDistance() << "cm";
ofLogNotice() << "sensor-camera dist: " << kinect.getSensorCameraDistance() << "cm";
ofLogNotice() << "zero plane pixel size: " << kinect.getZeroPlanePixelSize() << "mm";
ofLogNotice() << "zero plane dist: " << kinect.getZeroPlaneDistance() << "mm";
}
ofSetFrameRate(60);
// zero the tilt on startup
kinectAnglePitch = 0;
kinect.setCameraTiltAngle(kinectAnglePitch);
nearThreshold = 400;
farThreshold = 1000;
#ifdef TARGET_OPENGLES
//shader.load("shadersES2/shader");
#else
if(ofIsGLProgrammableRenderer()){
shader.load("shadersGL3/shader");
}else{
//shader.load("shadersGL2/shader");
}
#endif
easyCam.setGlobalPosition(ofVec3f(0.0, 100.0, 1500));
easyCam.setTarget(ofVec3f(0.0, 0.0, 0.0));
}
//--------------------------------------------------------------
void ofApp::update(){
ofBackground(0, 0, 0);
kinect.update();
if(kinect.isFrameNew()) {
}
}
//--------------------------------------------------------------
void ofApp::draw(){
ofEnableDepthTest();
drawPointCloud();
/*
ofSetColor(0, 255, 0);
stringstream reportStream;
reportStream
<< "-----------------------------------------------" << endl
<< "gerardobort@gmail.com - kinect demo - " << ofGetElapsedTimef() << endl
<< "-----------------------------------------------" << endl
<< "kinectAnglePitch " << kinectAnglePitch << endl
<< "-----------------------------------" << endl
<< "cameraZoom " << cameraZoom << endl
<< "cameraAngleYaw " << cameraAngleYaw << endl
<< "cameraAnglePitch " << cameraAnglePitch << endl
<< "-----------------------------------" << endl
<< "farThreshold " << farThreshold << endl
<< "nearThreshold " << nearThreshold << endl
<< "-----------------------------------" << endl;
ofDrawBitmapString(reportStream.str(), 20, 20);
*/
}
//--------------------------------------------------------------
void ofApp::drawPointCloud() {
int w = kinect.width;
int h = kinect.height;
ofMesh mesh;
mesh.setMode(OF_PRIMITIVE_TRIANGLES);
int step = 10;
for (int y = 0; y+2*step < h; y += step) {
for (int x = 0; x+2*step < w; x += step) {
if (kinect.getDistanceAt(x, y) > 0) {
ofVec3f v1 = kinect.getWorldCoordinateAt(x, y);
ofVec3f v2 = kinect.getWorldCoordinateAt(x+step, y);
ofVec3f v3 = kinect.getWorldCoordinateAt(x, y+step);
ofVec3f v4 = kinect.getWorldCoordinateAt(x+step, y+step);
ofColor c1 = kinect.getColorAt(x, y);
ofColor c2 = kinect.getColorAt(x+step, y);
ofColor c3 = kinect.getColorAt(x, y+step);
ofColor c4 = kinect.getColorAt(x+step, y+step);
if (farThreshold > v1.z && v1.z > nearThreshold) {
if (v1.distance(v2) > 10*step) continue;
if (v1.distance(v3) > 10*step) continue;
mesh.addColor(c1);
mesh.addVertex(v1);
mesh.addColor(c2);
mesh.addVertex(v2);
mesh.addColor(c3);
mesh.addVertex(v3);
}
if (farThreshold > v4.z && v4.z > nearThreshold) {
if (v4.distance(v2) > 10*step) continue;
if (v4.distance(v3) > 10*step) continue;
mesh.addColor(c2);
mesh.addVertex(v2);
mesh.addColor(c3);
mesh.addVertex(v3);
mesh.addColor(c4);
mesh.addVertex(v4);
}
}
}
}
ofMesh mesh2;
mesh2.setMode(OF_PRIMITIVE_POINTS);
step = 2;
for (int y = 0; y+2*step < h; y += step) {
for (int x = 0; x+2*step < w; x += step) {
if (kinect.getDistanceAt(x, y) > 0) {
ofVec3f v1 = kinect.getWorldCoordinateAt(x, y);
ofColor c1 = kinect.getColorAt(x, y);
mesh2.addColor(c1);
mesh2.addVertex(v1);
}
}
}
shader.begin();
shader.setUniform1i("u_worldWidth", ofGetWindowWidth());
shader.setUniform1i("u_worldHeight", ofGetWindowHeight());
shader.setUniform1i("u_mapWidth", kinect.width);
shader.setUniform1i("u_mapHeight", kinect.height);
shader.setUniform1i("u_farThreshold", farThreshold);
shader.setUniform1f("u_time", ofGetElapsedTimef());
shader.setUniformTexture("u_sampler2d", kinect.getTextureReference(), kinect.getTextureReference().getTextureData().textureID);
ofScale(1, 1, -1);
ofTranslate(500, 500, -1000);
glPointSize(1);
shader.setUniform1i("u_index", 1);
mesh.drawFaces();
shader.end();
glPointSize(6);
mesh2.drawVertices();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
switch (key) {
case 'w':
kinect.enableDepthNearValueWhite(!kinect.isDepthNearValueWhite());
break;
case 'e':
kinect.setCameraTiltAngle(kinectAnglePitch); // go back to prev tilt
kinect.open();
break;
case 'c':
kinect.setCameraTiltAngle(0); // zero the tilt
kinect.close();
break;
case '1':
kinect.setLed(ofxKinect::LED_GREEN);
break;
case '2':
kinect.setLed(ofxKinect::LED_YELLOW);
break;
case '3':
kinect.setLed(ofxKinect::LED_RED);
break;
case '4':
kinect.setLed(ofxKinect::LED_BLINK_GREEN);
break;
case '5':
kinect.setLed(ofxKinect::LED_BLINK_YELLOW_RED);
break;
case '0':
kinect.setLed(ofxKinect::LED_OFF);
break;
case OF_KEY_UP:
kinectAnglePitch++;
if(kinectAnglePitch>30) kinectAnglePitch=30;
kinect.setCameraTiltAngle(kinectAnglePitch);
break;
case OF_KEY_DOWN:
kinectAnglePitch--;
if(kinectAnglePitch<-30) kinectAnglePitch=-30;
kinect.setCameraTiltAngle(kinectAnglePitch);
break;
case OF_KEY_LEFT:
easyCam.setDistance(easyCam.getDistance() + 20);
break;
case OF_KEY_RIGHT:
easyCam.setDistance(easyCam.getDistance() - 20);
break;
case 'i':
farThreshold-=10;
nearThreshold-=10;
break;
case 'o':
farThreshold+=10;
nearThreshold+=10;
break;
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
//--------------------------------------------------------------
void ofApp::exit() {
kinect.setCameraTiltAngle(0); // zero the tilt on exit
kinect.close();
}