Created
January 31, 2012 05:21
-
-
Save tatsuro-ueda/1709011 to your computer and use it in GitHub Desktop.
simple-OpenNI sample DepthMap3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 深度カメラの映像を3次元で描画する | |
import SimpleOpenNI.*; | |
SimpleOpenNI context; | |
float zoomF =0.3f; | |
float rotX = radians(180); // デフォルトでは視界をx軸に対して180度回転させる | |
// OpenNIのデータは反対から来る | |
float rotY = radians(0); | |
void setup() | |
{ | |
frameRate(300); | |
size(1024,768,P3D); // strange, get drawing error in the cameraFrustum if i use P3D, in opengl there is no problem | |
//context = new SimpleOpenNI(this,SimpleOpenNI.RUN_MODE_SINGLE_THREADED); | |
context = new SimpleOpenNI(this); | |
// 鏡像にならないようにする | |
context.setMirror(false); | |
// 深度カメラを有効にする | |
if(context.enableDepth() == false) | |
{ | |
println("Can't open the depthMap, maybe the camera is not connected!"); | |
exit(); | |
return; | |
} | |
stroke(255,255,255); | |
smooth(); | |
perspective(radians(45), | |
float(width)/float(height), | |
10,150000); | |
} | |
void draw() | |
{ | |
// カメラ情報を更新する | |
context.update(); | |
background(0,0,0); | |
translate(width/2, height/2, 0); | |
rotateX(rotX); | |
rotateY(rotY); | |
scale(zoomF); | |
int[] depthMap = context.depthMap(); | |
int steps = 3; // to speed up the drawing, draw every third point | |
int index; | |
PVector realWorldPoint; | |
translate(0,0,-1000); // set the rotation center of the scene 1000 infront of the camera | |
stroke(255); | |
PVector[] realWorldMap = context.depthMapRealWorld(); | |
for(int y=0;y < context.depthHeight();y+=steps) | |
{ | |
for(int x=0;x < context.depthWidth();x+=steps) | |
{ | |
index = x + y * context.depthWidth(); | |
if(depthMap[index] > 0) | |
{ | |
// 投射された点を描画する | |
// realWorldPoint = context.depthMapRealWorld()[index]; | |
realWorldPoint = realWorldMap[index]; | |
point(realWorldPoint.x,realWorldPoint.y,realWorldPoint.z); // make realworld z negative, | |
// in the 3d drawing coordsystem +z points in the direction of the eye | |
} | |
//println("x: " + x + " y: " + y); | |
} | |
} | |
// Kinectカメラを描画する | |
context.drawCamFrustum(); | |
} | |
void keyPressed() | |
{ | |
switch(key) | |
{ | |
case ' ': | |
context.setMirror(!context.mirror()); | |
break; | |
} | |
switch(keyCode) | |
{ | |
case LEFT: | |
rotY += 0.1f; | |
break; | |
case RIGHT: | |
// zoom out | |
rotY -= 0.1f; | |
break; | |
case UP: | |
if(keyEvent.isShiftDown()) | |
zoomF += 0.02f; | |
else | |
rotX += 0.1f; | |
break; | |
case DOWN: | |
if(keyEvent.isShiftDown()) | |
{ | |
zoomF -= 0.02f; | |
if(zoomF < 0.01) | |
zoomF = 0.01; | |
} | |
else | |
rotX -= 0.1f; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment