Skip to content

Instantly share code, notes, and snippets.

@aaronsherwood
Last active December 14, 2015 19:39
Show Gist options
  • Save aaronsherwood/5138487 to your computer and use it in GitHub Desktop.
Save aaronsherwood/5138487 to your computer and use it in GitHub Desktop.
optical flow test with a fragment shader in openframeworks
#include "testApp.h"
using namespace ofxCv;
using namespace cv;
//--------------------------------------------------------------
void testApp::setup(){
ofSetVerticalSync(true);
ofBackground(0);
loadShader();
curFlow = &farneback;
cam.initGrabber(320, 240);
dampenedflow=ofVec2f(0,0);
prev=ofVec2f(ofGetWidth()/2,ofGetHeight()/2);
ofHideCursor();
}
//--------------------------------------------------------------
void testApp::update(){
cam.update();
if(cam.isFrameNew()) {
curFlow->calcOpticalFlow(cam);
flow=farneback.getAverageFlow()*20;
flow=ofVec2f(-flow.y,flow.x);
dampenedflow+=(flow-dampenedflow)*.05;
prev+=dampenedflow;
ofVec2f middle=ofVec2f(ofGetWidth()/2,ofGetHeight()/2);
ofVec2f getBack = middle-prev;
ofVec2f norm =getBack;
norm.normalize();
if(prev.distance(middle)>3){
prev+=norm;
}
}
}
//--------------------------------------------------------------
void testApp::draw(){
shader.begin();
shader.setUniform1f("time", ofGetElapsedTimef());
shader.setUniform2f("resolution", ofGetWidth()/2, ofGetHeight()/2);
int i =ofClamp(prev.x,30,ofGetWidth()-30);
int j =ofClamp(prev.y,30,ofGetHeight()-30);
shader.setUniform2f("mouse", 1+ofMap(i- ofGetWidth()/2,-ofGetWidth()/2,ofGetWidth()/2,-1.,1.,true ), 1+ofMap(ofGetHeight()/2-j,-ofGetHeight()/2,ofGetHeight()/2,-1.,1.,true ));
ofFill();
ofRect(0,0,ofGetWidth(),ofGetHeight());
shader.end();
cam.draw(1440, 0,-320,240);
curFlow->draw(1440, 0,-320,240);
}
void testApp::loadShader(){
shader.load("shaders/testGLES");
}
//--------------------------------------------------------------
void testApp::keyPressed(int key){
if (key == 'r'){
loadShader();
prev=ofVec2f(ofGetWidth()/2,ofGetHeight()/2);
}
}
#pragma once
#include "ofMain.h"
#include "ofxCv.h"
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void loadShader();
ofShader shader;
ofxCv::FlowFarneback farneback;
ofxCv::Flow* curFlow;
ofVideoGrabber cam;
ofVec2f flow;
ofVec2f dampenedflow;
ofVec2f prev;
};
uniform float time;
uniform vec2 mouse;
uniform vec2 resolution;
vec2 last_mouse;
float rand(vec2 co){
return fract(tan(dot(co.xy ,vec2(12.9898,78.233))) * 758.5453);
}
void main( void )
{
float scale = (last_mouse.x - mouse.x)* (last_mouse.y - mouse.y) * 1000.;
last_mouse = mouse;
float sum = 0.0;
float size = resolution.x / 1.5;
float g = .95;
int num = 350;
for (int i = 0; i < 50; ++i) {
vec2 position = mouse * resolution;
position.x += sin(time / 3.0 + 1.0 * float(i)) * 0.25 * scale;
position.y += cos(time / 3.0 + 1.0 * float(i)) * 0.25 * scale;
float dist = length(gl_FragCoord.xy - position);
sum += size / pow(dist, g);
}
vec4 color = vec4(0,0,0,1);
float val = sum / float(num);
color = vec4(val*0.5, val*0.8, val, 1);
gl_FragColor = vec4(color);
}
void main() {
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
verpos=gl_Position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment