objectimport and object loader with kinect
in
Contributed Library Questions
•
2 years ago
hi everybody,
i want to create a model with my kinect and import it to processing again. Now i see that there's no MTL file. So i don't have the exported colours. I've allready looked at the model in Cinema 4d and it shows right.
Plus i have some problems with that the kinect hangs some times. It doesn't show any errors, but it just stops the video from the kinect or the point grid.
here's my code:
[UPDATE]
tried with nervoussystem OBJ import, but it only takes one eillpise
i want to create a model with my kinect and import it to processing again. Now i see that there's no MTL file. So i don't have the exported colours. I've allready looked at the model in Cinema 4d and it shows right.
Plus i have some problems with that the kinect hangs some times. It doesn't show any errors, but it just stops the video from the kinect or the point grid.
here's my code:
- // Daniel Shiffman
// Kinect Point Cloud example
// http://www.shiffman.net
// https://github.com/shiffman/libfreenect/tree/master/wrappers/java/processing
//import processing.opengl.*;
import org.openkinect.*;
import org.openkinect.processing.*;
import peasy.*;
import processing.dxf.*;
import superCAD.*;
// Kinect Library object
Kinect kinect;
float a = 0;
int gridsizex = 20;
int gridsizey = 20;
// space between boxes
int spacer = 0;
// Size of kinect image
int w = 640;
int h = 480;
int maxDepthVal = 0;
PImage img, depth;
boolean record= false;
// We'll use a lookup table so that we don't have to repeat the math over and over
float[] depthLookUp = new float[2048];
void setup() {
size(800,600,P3D);
kinect = new Kinect(this);
kinect.start();
kinect.enableDepth(true);
// We don't need the grayscale image in this example
// so this makes it more efficient
kinect.processDepthImage(false);
kinect.enableRGB(true);
img = kinect.getVideoImage();
depth = kinect.getDepthImage();
img = kinect.getVideoImage();
// Lookup table for all possible depth values (0 - 2047)
for (int i = 0; i < depthLookUp.length; i++) {
depthLookUp[i] = rawDepthToMeters(i);
}
}
void draw() {
background(0);
fill(255);
textMode(SCREEN);
// image(img,0,0,640,480);
text("Kinect FR: " + (int)kinect.getDepthFPS() + "\nProcessing FR: " + (int)frameRate,10,16);
hint(DISABLE_DEPTH_TEST);
stroke(255);
noFill();
smooth();
ellipseMode(CENTER);
noSmooth();
ellipse(width/2, height/2,150, 200);
rectMode(CENTER);
rect(width/2, height-100,200,300);
hint(ENABLE_DEPTH_TEST);
if (record == true) {
beginRaw("superCAD."+"ObjFile","Export"+"/OBJFILE"+".obj");
}
if(key=='s') {
save ("image.jpg");
}
// Get the raw depth as array of integers
int[] depth = kinect.getRawDepth();
// We're just going to calculate and draw every 4th pixel (equivalent of 160x120)
int skip = 4;
frameRate = kinect.getVideoFPS();
// Translate and rotate
translate(width/2,height/2,-50);
rotateY(a);
for(int x=0; x<w; x+=skip) {
for(int y=0; y<h; y+=skip) {
int offset = x+y*w;
color cp = img.get(x, y);
// Convert kinect data to world xyz coordinate
int rawDepth = depth[offset];
if (rawDepth < 900) {
PVector v = depthToWorld(x,y,rawDepth);
stroke(255);
pushMatrix();
// Scale up by 200
float factor = 400;
// println(rawDepth);
translate(v.x*factor,v.y*factor,factor-v.z*factor+200);
// Draw a point
fill(cp);
noStroke();
// box(3, 3,1 );
ellipse(5,5,5,5);
// point(gridsizex-spacer, gridsizey-spacer, int(cp));
popMatrix();
}
}
}
if (record == true) {
endRaw();
record= false;
}
// Rotate
// a += 0.015f;
}
// These functions come from: http://graphics.stanford.edu/~mdfisher/Kinect.html
float rawDepthToMeters(int depthValue) {
if (depthValue < 2047) {
return (float)(1.0 / ((double)(depthValue) * -0.0030711016 + 3.3309495161));
}
return 0.0f;
}
PVector depthToWorld(int x, int y, int depthValue) {
final double fx_d = 1.0 / 5.9421434211923247e+02;
final double fy_d = 1.0 / 5.9104053696870778e+02;
final double cx_d = 3.3930780975300314e+02;
final double cy_d = 2.4273913761751615e+02;
PVector result = new PVector();
double depth = depthLookUp[depthValue];//rawDepthToMeters(depthValue);
result.x = (float)((x - cx_d) * depth * fx_d);
result.y = (float)((y - cy_d) * depth * fy_d);
result.z = (float)(depth);
return result;
}
void stop() {
kinect.quit();
super.stop();
}
void keyPressed() {
if (key == 'r') {
record = true;
}
}
- /*
Load a scene from an .obj file as simple as it gets.
model is the free Sponza Atrium by Marko Dabrovic:
http://hdri.cgtechniques.com/~sponza/
Downloads: http://hdri.cgtechniques.com/~sponza/files/
*/
import javax.media.opengl.*;
import processing.opengl.*;
import objimp.*;
float aspectRatio = 1;
ObjImpScene scene2;
void setup()
{
size( 800, 600, OPENGL );
aspectRatio = width / (float)height;
hint( ENABLE_OPENGL_4X_SMOOTH );
smooth();
frameRate( 60 );
try
{
scene2 = new ObjImpScene( this );
scene2.load( dataPath("OBJFILE.obj"), 5 );
// scene2.load( dataPath("spheres.obj"), .5 );
} catch( Exception e )
{
println( e );
System.exit( 0 );
}
}
void draw()
{
float time = millis() * 0.001;
background( 0.4*255 );
perspective( PI*.25, aspectRatio, 1, 6000 );
camera( 0, 3, 0, -2*cos(time*.5), sin(time)*2+3, -2*sin(time*.5), 0, 1, 0 );
// setup light. to do correct lighting you need to set it before any world transformations
GL _gl = ((PGraphicsOpenGL)g).beginGL();
setupLight( _gl, new float[]{0, 15, 0}, 1 );
((PGraphicsOpenGL)g).endGL();
scale( 1, -1, 1 ); // make y axis points up
// translate( 0, -10, 0 );
rotateY( radians(mouseX) );
scene2.draw();
}
// val is 0 or 1. 0 = directional light, 1 = point light
void setupLight( GL g, float[] pos, float val )
{
float[] light_emissive = { 0.0f, 0.0f, 0.0f, 1 };
float[] light_ambient = { 0.0f, 0.0f, 0.0f, 1 };
float[] light_diffuse = { 1.0f, 1.0f, 1.0f, 1.0f };
float[] light_specular = { 1.0f, 1.0f, 1.0f, 1.0f };
float[] light_position = { pos[0], pos[1], pos[2], val };
g.glLightfv ( GL.GL_LIGHT1, GL.GL_AMBIENT, light_ambient, 0 );
g.glLightfv ( GL.GL_LIGHT1, GL.GL_DIFFUSE, light_diffuse, 0 );
g.glLightfv ( GL.GL_LIGHT1, GL.GL_SPECULAR, light_specular, 0 );
g.glLightfv ( GL.GL_LIGHT1, GL.GL_POSITION, light_position, 0 );
g.glEnable( GL.GL_LIGHT1 );
g.glEnable( GL.GL_LIGHTING );
g.glEnable( GL.GL_COLOR_MATERIAL );
}
void stop()
{
super.stop();
}
[UPDATE]
tried with nervoussystem OBJ import, but it only takes one eillpise
1