Last active
August 29, 2015 13:57
-
-
Save MartinBspheroid/9742374 to your computer and use it in GitHub Desktop.
Cinder-RuttEtra
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
#include "cinder/app/AppNative.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder\Capture.h" | |
#include "cinder\Surface.h" | |
#include "cinder/gl/Texture.h" | |
#include "cinder/MayaCamUI.h" | |
#include "cinder/Camera.h" | |
#include "cinder\Utilities.h" | |
#include "cinder/params/Params.h" | |
#include "cinder/ip/EdgeDetect.h" | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
class RuttEtraApp : public AppNative { | |
public: | |
void setup(); | |
void mouseDown( MouseEvent event ); | |
void mouseDrag( MouseEvent event ); | |
void update(); | |
void draw(); | |
Capture mInput; | |
gl::Texture webcam; | |
vector <Vec3f> pixData; | |
CameraPersp mCam; | |
MayaCamUI maya; | |
int stepX, stepY, numPoints; | |
int blackBreak; | |
float elevation; | |
params::InterfaceGl mParams; | |
bool enableSobel, enableWebcamPreview, enableGrayScale; | |
Surface8u edge; | |
}; | |
void RuttEtraApp::setup() | |
{ | |
elevation = 10; | |
enableWebcamPreview = enableSobel = false; | |
enableGrayScale = true; | |
mParams = params::InterfaceGl( "Rutt-Etra", Vec2i( 225, 200 ) ); | |
mCam.setPerspective( 45.0, getWindowAspectRatio(), 0.1f, 10000.0f ); | |
mCam.lookAt(Vec3f(0.0, 0.0, 500.0), Vec3f::zero(), Vec3f::yAxis()); | |
mCam.setCenterOfInterestPoint(Vec3f::zero()); | |
maya.setCurrentCam(mCam); | |
try { | |
mInput = Capture( 640, 480 ); | |
mInput.start(); | |
} | |
catch( ... ) { | |
console() << "Failed to initialize capture" << std::endl; | |
} | |
for (int i = 0; i < mInput.getWidth()*mInput.getHeight(); i++) | |
{ | |
pixData.push_back(Vec3f::zero()); | |
} | |
stepY= 5; | |
stepX = 5; | |
blackBreak = 64; | |
mParams.addParam("Elevation damping", &elevation, "min=1 max=10 step=0.01"); | |
mParams.addParam( "Steps X", &stepX, "min=1 max=10" ); | |
mParams.addParam( "StepsY", &stepY, "min=1 max=10" ); | |
mParams.addParam( "BlackBreak", &blackBreak, "min=0 max=255" ); | |
mParams.addSeparator(); | |
mParams.addParam("Enable preview", &enableWebcamPreview); | |
mParams.addParam("Enable sobel", &enableSobel); | |
mParams.addParam("Enable grayscale", &enableGrayScale); | |
edge = Surface8u(mInput.getWidth(), mInput.getHeight(), false, SurfaceChannelOrder::RGB); | |
} | |
void RuttEtraApp::mouseDown(MouseEvent event){ | |
maya.mouseDown( event.getPos() ); | |
} | |
void RuttEtraApp::mouseDrag(MouseEvent event){ | |
bool middle = event.isMiddleDown() || ( event.isMetaDown() && event.isLeftDown() ); | |
bool right = event.isRightDown() || ( event.isControlDown() && event.isLeftDown() ); | |
maya.mouseDrag( event.getPos(), event.isLeftDown() && !middle && !right, middle, right ); | |
} | |
void RuttEtraApp::update() | |
{ | |
if (mInput.isCapturing()) | |
{ | |
if (enableSobel) | |
{ | |
ci::ip::edgeDetectSobel(mInput.getSurface(), &edge); | |
if(enableWebcamPreview) webcam = gl::Texture(edge); | |
}else | |
{ | |
if(enableWebcamPreview) webcam = gl::Texture(mInput.getSurface()); | |
} | |
Surface::Iter surfIter = enableSobel ? surfIter = edge.getIter(edge.getBounds()) : surfIter = mInput.getSurface().getIter(mInput.getBounds()); | |
int p = 0; | |
while (surfIter.line()) | |
{ | |
while (surfIter.pixel()) | |
{ | |
float brightness = surfIter.r()+surfIter.g()+surfIter.b(); | |
if (surfIter.x() % stepX == 0 && surfIter.y() % stepY == 0) | |
{ | |
pixData[p].set(surfIter.x(), surfIter.mWidth-surfIter.y(), brightness/elevation); // !!!inversion | |
p++; | |
} | |
} | |
} | |
numPoints = p; | |
} | |
} | |
void RuttEtraApp::draw() | |
{ | |
gl::clear( Color::gray(0.5)); | |
gl::color(Color::white()); | |
gl::disableDepthRead(); | |
if(enableWebcamPreview) gl::draw(webcam, webcam.getBounds(), getWindowBounds()); | |
gl::enableDepthWrite(); | |
gl::enableDepthRead(true); | |
gl::pushMatrices(); | |
gl::setMatrices(maya.getCamera()); | |
gl::color(Color::black()); | |
int drawnVertices = 0; | |
gl::begin(GL_LINE_STRIP); // start drawing lines | |
int lb =0; | |
for (int x = 0; x < numPoints; x++) | |
{ | |
if (pixData[x].z < blackBreak/(elevation/3) || (pixData[x].x < pixData[x-1].x && x!=0) ) | |
{ | |
gl::end(); | |
gl::begin(GL_LINE_STRIP); | |
}else{ | |
gl::vertex(pixData[x]); | |
gl::color( enableGrayScale ? Color::gray( pixData[x].z / (255*3 ) * elevation ) : Color::white() ) ; | |
drawnVertices++; | |
} | |
} | |
gl::end(); // end drawing lines | |
gl::popMatrices(); | |
gl::enableAlphaBlending(); | |
gl::drawString("Points : " + toString(numPoints) +"\n Drawn Vertices:" + toString(drawnVertices) , Vec2f(getWindowWidth()-120, 20), Color::black()); | |
gl::disableAlphaBlending(); | |
mParams.draw(); | |
} | |
CINDER_APP_NATIVE( RuttEtraApp, RendererGl ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment