Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void testApp::setup(){
- ofSetFrameRate(30);//find good framerate
- ofEnableAlphaBlending();
- vidPlayer.loadMovie("fingers.mov");
- vidPlayer.play();
- cam.initGrabber(640, 480);//set up videograbber
- contourFinder.setInvert(true);//FINDS BLACK, NOT BRIGHT SPOTS
- maskFbo.allocate(640,480);
- fbo.allocate(640,480);
- // There are 3 of ways of loading a shader:
- //
- // 1 - Using just the name of the shader and ledding ofShader look for .frag and .vert:
- // Ex.: shader.load( "myShader");
- //
- // 2 - Giving the right file names for each one:
- // Ex.: shader.load( "myShader.vert","myShader.frag");
- //
- // 3 - And the third one itÔøΩs passing the shader programa on a single string;
- //
- #ifdef TARGET_OPENGLES
- shader.load("shaders_gles/alphamask.vert","shaders_gles/alphamask.frag");
- #else
- if(ofGetGLProgrammableRenderer()){
- string vertex = "#version 150\n\
- \n\
- uniform mat4 projectionMatrix;\n\
- uniform mat4 modelViewMatrix;\n\
- uniform mat4 modelViewProjectionMatrix;\n\
- \n\
- \n\
- in vec4 position;\n\
- in vec2 texcoord;\n\
- \n\
- out vec2 texCoordVarying;\n\
- \n\
- void main()\n\
- {\n\
- texCoordVarying = texcoord;\
- gl_Position = modelViewProjectionMatrix * position;\n\
- }";
- string fragment = "#version 150\n\
- \n\
- uniform sampler2DRect tex0;\
- uniform sampler2DRect maskTex;\
- in vec2 texCoordVarying;\n\
- \
- out vec4 fragColor;\n\
- void main (void){\
- vec2 pos = texCoordVarying;\
- \
- vec3 src = texture(tex0, pos).rgb;\
- float mask = texture(maskTex, pos).r;\
- \
- fragColor = vec4( src , mask);\
- }";
- shader.setupShaderFromSource(GL_VERTEX_SHADER, vertex);
- shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragment);
- shader.bindDefaults();
- shader.linkProgram();
- }else{
- string shaderProgram = "#version 120\n \
- #extension GL_ARB_texture_rectangle : enable\n \
- \
- uniform sampler2DRect tex0;\
- uniform sampler2DRect maskTex;\
- \
- void main (void){\
- vec2 pos = gl_TexCoord[0].st;\
- \
- vec3 src = texture2DRect(tex0, pos).rgb;\
- float mask = texture2DRect(maskTex, pos).r;\
- \
- gl_FragColor = vec4( src , mask);\
- }";
- shader.setupShaderFromSource(GL_FRAGMENT_SHADER, shaderProgram);
- shader.linkProgram();
- }
- #endif
- maskFbo.begin();
- ofClear(0,0,0,255);
- maskFbo.end();
- fbo.begin();
- ofClear(0,0,0,255);
- fbo.end();
- }
- //--------------------------------------------------------------
- void testApp::update(){
- cam.update();
- vidPlayer.update();
- if(cam.isFrameNew()) {//if new frame
- contourFinder.setThreshold(80);
- contourFinder.findContours(cam);//find contour of current frame
- }
- // MASK (frame buffer object)
- maskFbo.begin();
- pathFromContour.draw(0,0);//GENERATED SHAPES FROM CONTOUR
- maskFbo.end();
- // HERE the shader-masking happens
- fbo.begin();
- ofClear(0, 0, 0, 0);
- shader.begin();
- shader.setUniformTexture("maskTex", maskFbo.getTextureReference(), 1 );
- vidPlayer.draw(0,0);//MY VIDEO HERE
- shader.end();
- fbo.end();
- }
- //--------------------------------------------------------------
- void testApp::draw(){
- ofSetColor(255);
- cam.draw(0, 0);
- contourFinder.draw();
- int n = contourFinder.size();//NUMBER OF SHAPES
- for(int i = 0; i < n; i++) {
- ofPoint center = toOf(contourFinder.getCenter(i));//center point of contour
- double area = contourFinder.getContourArea(i);//area of contour
- ofPushMatrix();
- ofTranslate(center.x, center.y);
- int label = contourFinder.getLabel(i);//gets ID
- string msg = "ID: " + ofToString(label);
- string areaMsg = "AREA: " + ofToString(area);
- ofSetColor(255,0,0);
- ofDrawBitmapString(msg, 0, 0);
- ofDrawBitmapString(areaMsg, 0, 10);
- ofScale(5, 5);
- ofPopMatrix();
- //MUST BE LINKED WITH AN IDENTIFIER.
- ofPolyline polyline = contourFinder.getPolyline(i);//to convert
- ofPath pathFromContour;//path to be built
- for(int j = 0; j < polyline.getVertices().size(); j++) {
- if(j == 0) {
- pathFromContour.newSubPath();
- pathFromContour.moveTo(polyline.getVertices()[j]);
- } else {
- pathFromContour.lineTo(polyline.getVertices()[j]);
- }
- }
- pathFromContour.close();
- pathFromContour.simplify();
- //WHY ARE THEY FILLING SO CHAOTICALLY?
- ofColor pathColor(255);
- pathFromContour.setFillColor(pathColor);
- }
- fbo.draw(0,0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement