Skip to content

Instantly share code, notes, and snippets.

@prisonerjohn
Last active October 21, 2019 01:24
Show Gist options
  • Save prisonerjohn/c0a4c3b5858a4237aa870aa96bdc0f1f to your computer and use it in GitHub Desktop.
Save prisonerjohn/c0a4c3b5858a4237aa870aa96bdc0f1f to your computer and use it in GitHub Desktop.
Depth World
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 360);
// Start the depth sensor.
rsContext.setup(true);
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
rsContext.update();
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
// Align the frames to the color viewport.
rsDevice->alignMode = ofxRealSense2::Device::Align::Color;
ofShortPixels rawDepthPix = rsDevice->getRawDepthPix();
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < rsDevice->getRawDepthTex().getHeight(); y += skipPoints)
{
for (int x = 0; x < rsDevice->getRawDepthTex().getWidth(); x += skipPoints)
{
int depth = rawDepthPix.getColor(x, y).r;
pointMesh.addVertex(ofDefaultVec3(x, y, depth));
pointMesh.addTexCoord(ofDefaultVec2(x, y));
}
}
}
}
void ofApp::draw()
{
// Try to get a pointer to a device.
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
// Begin rendering through the camera.
cam.begin();
ofEnableDepthTest();
if (rsDevice)
{
// Render the mesh.
rsDevice->getColorTex().bind();
pointMesh.draw();
rsDevice->getColorTex().unbind();
}
ofDisableDepthTest();
cam.end();
// Done rendering through the camera.
// Draw the gui.
guiPanel.draw();
}
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
grabber.setup(640, 480);
// Setup the parameters.
useCamera.set("Use Camera", false);
camPosition.set("Cam Position", glm::vec3(0, 0, 90), glm::vec3(-100), glm::vec3(100));
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(useCamera);
guiPanel.add(camPosition);
}
void ofApp::update()
{
grabber.update();
cam.setPosition(camPosition);
}
void ofApp::draw()
{
if (useCamera)
{
// Begin rendering through the camera.
cam.begin();
// Scale the drawing down into more manageable units.
ofScale(0.1f);
// Draw the grabber image anchored in the center.
grabber.draw(-grabber.getWidth() / 2, -grabber.getHeight() / 2);
cam.end();
// Done rendering through the camera.
}
else
{
// Draw the grabber in 2D.
grabber.draw(0, 0);
}
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofCamera cam;
ofParameter<glm::vec3> camPosition;
ofParameter<bool> useCamera;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
grabber.setup(640, 480);
// Setup the parameters.
useCamera.set("Use Camera", false);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(useCamera);
}
void ofApp::update()
{
grabber.update();
}
void ofApp::draw()
{
if (useCamera)
{
// Begin rendering through the camera.
cam.begin();
// Scale the drawing down into more manageable units.
ofScale(0.1f);
// Draw the grabber image anchored in the center.
grabber.draw(-grabber.getWidth() / 2, -grabber.getHeight() / 2);
cam.end();
// Done rendering through the camera.
}
else
{
// Draw the grabber in 2D.
grabber.draw(0, 0);
}
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofEasyCam cam;
ofParameter<bool> useCamera;
ofxPanel guiPanel;
};
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxKinect.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxKinect kinect;
ofMesh pointMesh;
ofParameter<int> skipPoints;
ofEasyCam cam;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start the depth sensor.
kinect.setRegistration(true);
kinect.init();
kinect.open();
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
kinect.update();
if (kinect.isFrameNew())
{
ofShortPixels rawDepthPix = kinect.getRawDepthPixels();
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < kinect.getHeight(); y += skipPoints)
{
for (int x = 0; x < kinect.getWidth(); x += skipPoints)
{
int depth = rawDepthPix.getColor(x, y).r;
pointMesh.addVertex(glm::vec3(x, y, depth));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
}
}
void ofApp::draw()
{
ofBackground(0);
// Begin rendering through the camera.
cam.begin();
ofEnableDepthTest();
// Render the mesh.
kinect.getTexture().bind();
pointMesh.draw();
kinect.getTexture().unbind();
ofDisableDepthTest();
cam.end();
// Done rendering through the camera.
// Draw the gui.
guiPanel.draw();
}
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start the depth sensor.
kinect.setRegistration(true);
kinect.init();
kinect.open();
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
kinect.update();
if (kinect.isFrameNew())
{
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < kinect.getHeight(); y += skipPoints)
{
for (int x = 0; x < kinect.getWidth(); x += skipPoints)
{
pointMesh.addVertex(kinect.getWorldCoordinateAt(x, y));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
}
}
void ofApp::draw()
{
ofBackground(0);
// Begin rendering through the camera.
cam.begin();
ofEnableDepthTest();
// Render the mesh.
kinect.getTexture().bind();
pointMesh.draw();
kinect.getTexture().unbind();
ofDisableDepthTest();
cam.end();
// Done rendering through the camera.
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofMesh quadMesh;
ofParameter<bool> drawWireframe;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
// Build the quad mesh.
quadMesh.setMode(OF_PRIMITIVE_TRIANGLES);
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 480, 0));
quadMesh.addColor(ofColor(200, 0, 0));
quadMesh.addColor(ofColor(0, 200, 0));
quadMesh.addColor(ofColor(0, 0, 200));
quadMesh.addColor(ofColor(200, 0, 0));
quadMesh.addColor(ofColor(0, 0, 200));
quadMesh.addColor(ofColor(200, 200, 0));
// Setup the parameters.
drawWireframe.set("Wireframe?", false);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(drawWireframe);
}
void ofApp::update()
{
}
void ofApp::draw()
{
// Render the mesh.
if (drawWireframe)
{
quadMesh.drawWireframe();
}
else
{
quadMesh.draw();
}
// Draw the gui.
guiPanel.draw();
}
#include "ofApp.h"
void ofApp::setup()
{
// Build the quad mesh.
quadMesh.setMode(OF_PRIMITIVE_TRIANGLES);
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 480, 0));
// Setup the parameters.
drawWireframe.set("Wireframe?", false);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(drawWireframe);
}
void ofApp::update()
{
}
void ofApp::draw()
{
// Render the mesh.
if (drawWireframe)
{
quadMesh.drawWireframe();
}
else
{
quadMesh.draw();
}
// Draw the gui.
guiPanel.draw();
}
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start the grabber.
grabber.setup(640, 480);
// Build the quad mesh.
quadMesh.setMode(OF_PRIMITIVE_TRIANGLES);
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 0, 0));
quadMesh.addVertex(glm::vec3(640, 480, 0));
quadMesh.addVertex(glm::vec3(0, 480, 0));
quadMesh.addTexCoord(glm::vec2(0, 0));
quadMesh.addTexCoord(glm::vec2(640, 0));
quadMesh.addTexCoord(glm::vec2(640, 480));
quadMesh.addTexCoord(glm::vec2(0, 0));
quadMesh.addTexCoord(glm::vec2(640, 480));
quadMesh.addTexCoord(glm::vec2(0, 480));
// Setup the parameters.
drawWireframe.set("Wireframe?", false);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(drawWireframe);
}
void ofApp::update()
{
grabber.update();
}
void ofApp::draw()
{
// Render the mesh.
grabber.bind();
if (drawWireframe)
{
quadMesh.drawWireframe();
}
else
{
quadMesh.draw();
}
grabber.unbind();
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofMesh quadMesh;
ofEasyCam cam;
ofParameter<bool> drawWireframe;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 360);
// Start the depth sensor.
rsContext.setup(true);
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
rsContext.update();
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
ofShortPixels rawDepthPix = rsDevice->getRawDepthPix();
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < rsDevice->getRawDepthTex().getHeight(); y += skipPoints)
{
for (int x = 0; x < rsDevice->getRawDepthTex().getWidth(); x += skipPoints)
{
int depth = rawDepthPix.getColor(x, y).r;
pointMesh.addVertex(glm::vec3(x, y, depth));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
}
}
void ofApp::draw()
{
// Try to get a pointer to a device.
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
// Begin rendering through the camera.
cam.begin();
ofEnableDepthTest();
if (rsDevice)
{
// Render the mesh.
rsDevice->getDepthTex().bind();
pointMesh.draw();
rsDevice->getDepthTex().unbind();
}
ofDisableDepthTest();
cam.end();
// Done rendering through the camera.
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
#include "ofxRealSense2.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofxRealSense2::Context rsContext;
ofMesh pointMesh;
ofParameter<int> skipPoints;
ofEasyCam cam;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start the grabber.
grabber.setup(640, 480);
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < grabber.getHeight(); y++)
{
for (int x = 0; x < grabber.getWidth(); x++)
{
pointMesh.addVertex(glm::vec3(x, y, 0));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
}
void ofApp::update()
{
grabber.update();
}
void ofApp::draw()
{
// Render the mesh.
grabber.bind();
pointMesh.draw();
grabber.unbind();
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofMesh pointMesh;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
// Start the grabber.
grabber.setup(640, 480);
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
grabber.update();
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < grabber.getHeight(); y += skipPoints)
{
for (int x = 0; x < grabber.getWidth(); x += skipPoints)
{
pointMesh.addVertex(glm::vec3(x, y, 0));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
}
void ofApp::draw()
{
// Render the mesh.
grabber.bind();
pointMesh.draw();
grabber.unbind();
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofMesh pointMesh;
ofParameter<int> skipPoints;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 480);
grabber.setup(640, 480);
// Setup the parameters.
useCamera.set("Use Camera", false);
camPosition.set("Cam Position", glm::vec3(0, 0, 90), glm::vec3(-100), glm::vec3(100));
camTruck.set("Truck", 0.0f, -100.0f, 100.0f);
camBoom.set("Boom", 0.0f, -100.0f, 100.0f);
camDolly.set("Dolly", 0.0f, -100.0f, 100.0f);
orientCamera.set("Orient Camera", true);
camLookAt.set("Cam Look At", ofDefaultVec3(0, 0, 0), ofDefaultVec3(-100), ofDefaultVec3(100));
camPan.set("Pan", 0.0f, -90.0f, 90.0f);
camTilt.set("Tilt", 0.0f, -90.0f, 90.0f);
camRoll.set("Roll", 0.0f, -90.0f, 90.0f);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(useCamera);
guiPanel.add(camPosition);
guiPanel.add(camTruck);
guiPanel.add(camBoom);
guiPanel.add(camDolly);
guiPanel.add(orientCamera);
guiPanel.add(camLookAt);
guiPanel.add(camPan);
guiPanel.add(camTilt);
guiPanel.add(camRoll);
}
void ofApp::update()
{
grabber.update();
// Reset everything each frame, otherwise the transform will be additive.
cam.resetTransform();
cam.setPosition(camPosition);
cam.truck(camTruck);
cam.boom(camBoom);
cam.dolly(camDolly);
if (orientCamera)
{
cam.lookAt(camLookAt);
cam.panDeg(camPan);
cam.tiltDeg(camTilt);
cam.rollDeg(camRoll);
}
}
void ofApp::draw()
{
if (useCamera)
{
// Begin rendering through the camera.
cam.begin();
// Scale the drawing down into more manageable units.
ofScale(0.1f);
// Draw the grabber image anchored in the center.
grabber.draw(-grabber.getWidth() / 2, -grabber.getHeight() / 2);
cam.end();
// Done rendering through the camera.
}
else
{
// Draw the grabber in 2D.
grabber.draw(0, 0);
}
// Draw the gui.
guiPanel.draw();
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class ofApp : public ofBaseApp
{
public:
void setup();
void update();
void draw();
ofVideoGrabber grabber;
ofCamera cam;
ofParameter<glm::vec3> camPosition;
ofParameter<float> camTruck;
ofParameter<float> camBoom;
ofParameter<float> camDolly;
ofParameter<bool> orientCamera;
ofParameter<glm::vec3> camLookAt;
ofParameter<float> camPan;
ofParameter<float> camTilt;
ofParameter<float> camRoll;
ofParameter<bool> useCamera;
ofxPanel guiPanel;
};
#include "ofApp.h"
void ofApp::setup()
{
ofSetWindowShape(640, 360);
// Start the depth sensor.
rsContext.setup(true);
// Setup the parameters.
skipPoints.set("Skip Points", 1, 1, 24);
// Setup the gui.
guiPanel.setup("3D World", "settings.json");
guiPanel.add(skipPoints);
}
void ofApp::update()
{
rsContext.update();
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
if (rsDevice)
{
// Align the frames to the color viewport.
rsDevice->alignMode = ofxRealSense2::Device::Align::Color;
ofShortPixels rawDepthPix = rsDevice->getRawDepthPix();
// Rebuild the mesh.
pointMesh.clear();
pointMesh.setMode(OF_PRIMITIVE_POINTS);
for (int y = 0; y < rsDevice->getRawDepthTex().getHeight(); y += skipPoints)
{
for (int x = 0; x < rsDevice->getRawDepthTex().getWidth(); x += skipPoints)
{
int depth = rawDepthPix.getColor(x, y).r;
pointMesh.addVertex(rsDevice->getWorldPosition(x, y));
pointMesh.addTexCoord(glm::vec2(x, y));
}
}
}
}
void ofApp::draw()
{
// Try to get a pointer to a device.
std::shared_ptr<ofxRealSense2::Device> rsDevice = rsContext.getDevice(0);
// Begin rendering through the camera.
cam.begin();
ofEnableDepthTest();
ofScale(100);
if (rsDevice)
{
// Render the mesh.
rsDevice->getColorTex().bind();
pointMesh.draw();
rsDevice->getColorTex().unbind();
}
ofDisableDepthTest();
cam.end();
// Done rendering through the camera.
// Draw the gui.
guiPanel.draw();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment