Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 4436d34748c2eee5ef1dacf74e7b029df013bc18 @kylemcdonald committed Mar 28, 2011
@@ -0,0 +1,12 @@
+*.depend
+*.layout
+*.mode*v3
+*.pbxuser
+*.app*
+*.DS_*
+
+.svn/
+obj/
+bin/
+build/
+!data/

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.openFrameworks</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+</dict>
+</plist>
@@ -0,0 +1,8 @@
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+int main() {
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 1024, 768, OF_WINDOW);
+ ofRunApp(new testApp());
+}
@@ -0,0 +1,74 @@
+#pragma once
+
+/*
+ ofxAutoControlPanel makes ofxControlPanel a little easier to work with.
+
+ the control panel is autoamtically toggle-able using the tab key.
+ the panel is hooked up to mouse, draw, and update events automatically.
+ addPanel() does an addPanel() followed by a setWhichPanel() to avoid redundancy and mistakes.
+ the fps is autoamtically drawn at the lower right corner of the screen.
+*/
+
+#include "ofxControlPanel.h"
+#include "ofEvents.h"
+
+class ofxAutoControlPanel : public ofxControlPanel {
+public:
+ ofxAutoControlPanel() {
+ setXMLFilename("settings.xml");
+ ofAddListener(ofEvents.update, this, &ofxAutoControlPanel::update);
+ ofAddListener(ofEvents.draw, this, &ofxAutoControlPanel::draw);
+ ofAddListener(ofEvents.keyPressed, this, &ofxAutoControlPanel::keyPressed);
+ ofAddListener(ofEvents.mousePressed, this, &ofxAutoControlPanel::mousePressed);
+ ofAddListener(ofEvents.mouseReleased, this, &ofxAutoControlPanel::mouseReleased);
+ ofAddListener(ofEvents.mouseDragged, this, &ofxAutoControlPanel::mouseDragged);
+ }
+ void update(ofEventArgs& event) {
+ ofxControlPanel::update();
+ }
+ void draw(ofEventArgs& event) {
+ ofxControlPanel::draw();
+ if(!hidden) {
+ glColor4f(1, 1, 1, 1);
+ ofFill();
+ ofRect(ofGetWidth() - 45, ofGetHeight() - 25, 40, 20);
+ glColor4f(0, 0, 0, 1);
+ ofDrawBitmapString(ofToString((int) ofGetFrameRate()), ofGetWidth() - 40, ofGetHeight() - 10);
+ }
+ }
+ void keyPressed(ofKeyEventArgs& event) {
+ if(event.key == '\t') {
+ if(hidden) {
+ show();
+ } else {
+ hide();
+ }
+ }
+ }
+ void show() {
+ ofxControlPanel::show();
+ ofShowCursor();
+ }
+ void hide() {
+ ofxControlPanel::hide();
+ ofHideCursor();
+ }
+ void mousePressed(ofMouseEventArgs& event) {
+ ofxControlPanel::mousePressed(event.x, event.y, event.button);
+ }
+ void mouseReleased(ofMouseEventArgs& event) {
+ ofxControlPanel::mouseReleased();
+ }
+ void mouseDragged(ofMouseEventArgs& event) {
+ ofxControlPanel::mouseDragged(event.x, event.y, event.button);
+ }
+
+ // by default, make sliders float
+ void addSlider(string name, string xmlName, float value, float low, float high, bool isInt = false) {
+ ofxControlPanel::addSlider(name, xmlName, value, low, high, isInt);
+ }
+ void addPanel(string panelName, int columns = 1) {
+ ofxControlPanel::addPanel(panelName, columns);
+ ofxControlPanel::setWhichPanel(panelName);
+ }
+};
@@ -0,0 +1,258 @@
+#include "testApp.h"
+
+ofVec3f getNormal(Triangle& triangle) {
+ ofVec3f a = triangle.vert1 - triangle.vert2;
+ ofVec3f b = triangle.vert3 - triangle.vert2;
+ ofVec3f normal = b.cross(a);
+ normal.normalize();
+ return normal;
+}
+
+const float FovH=1.0144686707507438;
+const float FovV=0.78980943449644714;
+const float XtoZ = tanf(FovH/2)*2;
+const float YtoZ = tanf(FovV/2)*2;
+const unsigned int Xres = 640;
+const unsigned int Yres = 480;
+
+ofVec3f ConvertProjectiveToRealWorld(float x, float y, float z) {
+ return ofVec3f((x/Xres-.5f) * z * XtoZ,
+ (.5f-y/Yres) * z * YtoZ,
+ z);
+}
+
+void testApp::setup() {
+ ofSetVerticalSync(true);
+
+ panel.setup("Control Panel", 5, 5, 250, 600);
+ panel.addPanel("Settings");
+ panel.addSlider("zCutoff", "zCutoff", 100, 0, 200);
+ panel.addSlider("backOffset", "backOffset", 10, 0, 100);
+ panel.addToggle("exportStl", "exportStl", false);
+
+ kinect.init();
+ kinect.open();
+
+ light.setup();
+
+ surface.resize(Xres * Yres);
+ triangles.resize((Xres - 1) * (Yres - 1) * 2);
+ normals.resize(triangles.size() * 3);
+
+ backTriangles.resize(2 * (((Xres - 1) + (Yres - 1)) * 2 + 1));
+ backNormals.resize(triangles.size() * 3);
+
+ ofSetFrameRate(60);
+
+ kinect.setCameraTiltAngle(0);
+
+ offset.set(0, 0, -100);
+}
+
+void addTriangles(ofxSTLExporter& exporter, vector<Triangle>& triangles, vector<ofVec3f>& normals) {
+ for(int i = 0; i < triangles.size(); i++) {
+ exporter.addTriangle(triangles[i].vert1, triangles[i].vert2, triangles[i].vert3, normals[i * 3]);
+ }
+}
+
+#include "Poco/DateTimeFormatter.h"
+void testApp::update() {
+ zCutoff = panel.getValueF("zCutoff");
+
+ if(panel.getValueB("exportStl")) {
+ string pocoTime = Poco::DateTimeFormatter::format(Poco::LocalDateTime(), "%Y-%m-%d at %H.%M.%S");
+
+ ofxSTLExporter exporter;
+ exporter.beginModel("Kinect Export");
+ addTriangles(exporter, triangles, normals);
+ addTriangles(exporter, backTriangles, backNormals);
+ exporter.saveModel("Kinect Export " + pocoTime + ".stl");
+
+ panel.setValueB("exportStl", false);
+ }
+
+ kinect.update();
+ if(kinect.isFrameNew()) {
+ updateSurface();
+ updateTriangles();
+ updateBack();
+ }
+}
+
+void testApp::updateSurface() {
+ float* z = kinect.getDistancePixels();
+ int i = 0;
+ for(int y = 0; y < Yres; y++) {
+ for(int x = 0; x < Xres; x++) {
+ if(z[i] > zCutoff || z[i] < 10) {
+ z[i] = zCutoff;
+ }
+ surface[i] = ConvertProjectiveToRealWorld(x, y, z[i]);
+ i++;
+ }
+ }
+
+ // final pass to 0 the edges
+ for(int x = 0; x < Xres; x++) {
+ surface[x] = ConvertProjectiveToRealWorld(x, 0, zCutoff);
+ surface[(Yres - 1) * Xres + x] = ConvertProjectiveToRealWorld(x, Yres - 1, zCutoff);
+ }
+ for(int y = 0; y < Yres; y++) {
+ surface[y * Xres] = ConvertProjectiveToRealWorld(0, y, zCutoff);
+ surface[y * Xres + (Xres - 1)] = ConvertProjectiveToRealWorld(Xres - 1, y, zCutoff);
+ }
+}
+
+void calculateNormals(vector<Triangle>& triangles, vector<ofVec3f>& normals) {
+ int j = 0;
+ ofVec3f normal;
+ for(int i = 0; i < triangles.size(); i++) {
+ normal = getNormal(triangles[i]);
+ for(int m = 0; m < 3; m++) {
+ normals[j++] = normal;
+ }
+ }
+}
+
+void testApp::updateTriangles() {
+ int j = 0;
+ int k = 0;
+ for(int y = 0; y < Yres - 1; y++) {
+ for(int x = 0; x < Xres - 1; x++) {
+ int i = y * Xres + x;
+
+ int nw = i;
+ int ne = nw + 1;
+ int sw = i + Xres;
+ int se = sw + 1;
+
+ triangles[j].vert1 = surface[nw];
+ triangles[j].vert2 = surface[ne];
+ triangles[j].vert3 = surface[sw];
+ j++;
+
+ triangles[j].vert1 = surface[ne];
+ triangles[j].vert2 = surface[se];
+ triangles[j].vert3 = surface[sw];
+ j++;
+ }
+ }
+
+ calculateNormals(triangles, normals);
+}
+
+void testApp::updateBack() {
+ int j = 0;
+ Triangle* cur = &backTriangles[0];
+ ofVec3f offset(0, 0, panel.getValueF("backOffset"));
+
+ // top and bottom triangles
+ for(int x = 0; x < Xres - 1; x++) {
+ int i = 0 * Xres + x;
+ cur->vert1 = surface[i] + offset;
+ cur->vert2 = surface[i + 1];
+ cur->vert3 = surface[i];
+ cur++;
+
+ cur->vert1 = surface[i] + offset;
+ cur->vert2 = surface[i + 1] + offset;
+ cur->vert3 = surface[i + 1];
+ cur++;
+
+ i += (Yres - 1) * Xres;
+ cur->vert1 = surface[i];
+ cur->vert2 = surface[i + 1];
+ cur->vert3 = surface[i] + offset;
+ cur++;
+
+ cur->vert1 = surface[i + 1];
+ cur->vert2 = surface[i + 1] + offset;
+ cur->vert3 = surface[i] + offset;
+ cur++;
+ }
+
+ // left and right triangles
+ for(int y = 0; y < Yres - 1; y++) {
+ int i = y * Xres + 0;
+ cur->vert3 = surface[i] + offset;
+ cur->vert2 = surface[i + Xres];
+ cur->vert1 = surface[i];
+ cur++;
+
+ cur->vert3 = surface[i] + offset;
+ cur->vert2 = surface[i + Xres] + offset;
+ cur->vert1 = surface[i + Xres];
+ cur++;
+
+ i += Xres - 1;
+ cur->vert3 = surface[i];
+ cur->vert2 = surface[i + Xres];
+ cur->vert1 = surface[i] + offset;
+ cur++;
+
+ cur->vert3 = surface[i + Xres];
+ cur->vert2 = surface[i + Xres] + offset;
+ cur->vert1 = surface[i] + offset;
+ cur++;
+ }
+
+ int nw = 0;
+ int ne = nw + (Xres - 1);
+ int sw = (Yres - 1) * Xres;
+ int se = sw + (Xres - 1);
+
+ cur->vert1 = surface[sw] + offset;
+ cur->vert2 = surface[ne] + offset;
+ cur->vert3 = surface[nw] + offset;
+ cur++;
+
+ cur->vert1 = surface[sw] + offset;
+ cur->vert2 = surface[se] + offset;
+ cur->vert3 = surface[ne] + offset;
+
+ calculateNormals(backTriangles, backNormals);
+}
+
+void drawTriangleArray(vector<Triangle>& triangles, vector<ofVec3f>& normals) {
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glEnableClientState(GL_NORMAL_ARRAY);
+ glVertexPointer(3, GL_FLOAT, sizeof(ofVec3f), &(triangles[0].vert1.x));
+ glNormalPointer(GL_FLOAT, sizeof(ofVec3f), &(normals[0].x));
+ glDrawArrays(GL_TRIANGLES, 0, triangles.size() * 3);
+ glDisableClientState(GL_NORMAL_ARRAY);
+ glDisableClientState(GL_VERTEX_ARRAY);
+}
+
+void testApp::draw() {
+ ofBackground(0, 0, 0);
+ ofSetColor(255);
+
+ glEnable(GL_DEPTH_TEST);
+
+ cam.begin();
+
+ if(!surface.empty()) {
+ ofTranslate(offset);
+
+ /*
+ // draw point cloud
+ glEnableClientState(GL_VERTEX_ARRAY);
+ glVertexPointer(3, GL_FLOAT, sizeof(ofVec3f), &(surface[0].x));
+ glDrawArrays(GL_POINTS, 0, surface.size());
+ glDisableClientState(GL_VERTEX_ARRAY);
+ */
+
+ // draw triangles
+ drawTriangleArray(backTriangles, backNormals);
+ drawTriangleArray(triangles, normals);
+ }
+
+ cam.end();
+
+ glDisable(GL_DEPTH_TEST);
+}
+
+void testApp::exit() {
+ kinect.setCameraTiltAngle(0); // zero the tilt on exit
+ kinect.close();
+}
Oops, something went wrong.

0 comments on commit 4436d34

Please sign in to comment.