Advertisement
Guest User

FBO Test

a guest
Mar 18th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void testApp::setup(){
  2.     ofSetFrameRate(30);//find good framerate
  3.    
  4.     ofEnableAlphaBlending();
  5.     vidPlayer.loadMovie("fingers.mov");
  6.     vidPlayer.play();
  7.    
  8.     cam.initGrabber(640, 480);//set up videograbber
  9.     contourFinder.setInvert(true);//FINDS BLACK, NOT BRIGHT SPOTS
  10.    
  11.     maskFbo.allocate(640,480);
  12.     fbo.allocate(640,480);
  13.    
  14.     // There are 3 of ways of loading a shader:
  15.     //
  16.     //  1 - Using just the name of the shader and ledding ofShader look for .frag and .vert:
  17.     //      Ex.: shader.load( "myShader");
  18.     //
  19.     //  2 - Giving the right file names for each one:
  20.     //      Ex.: shader.load( "myShader.vert","myShader.frag");
  21.     //
  22.     //  3 - And the third one itÔøΩs passing the shader programa on a single string;
  23.     //
  24.    
  25.    
  26.     #ifdef TARGET_OPENGLES
  27.         shader.load("shaders_gles/alphamask.vert","shaders_gles/alphamask.frag");
  28.     #else
  29.         if(ofGetGLProgrammableRenderer()){
  30.             string vertex = "#version 150\n\
  31.            \n\
  32.            uniform mat4 projectionMatrix;\n\
  33.            uniform mat4 modelViewMatrix;\n\
  34.            uniform mat4 modelViewProjectionMatrix;\n\
  35.            \n\
  36.            \n\
  37.            in vec4  position;\n\
  38.            in vec2  texcoord;\n\
  39.            \n\
  40.            out vec2 texCoordVarying;\n\
  41.            \n\
  42.            void main()\n\
  43.            {\n\
  44.            texCoordVarying = texcoord;\
  45.            gl_Position = modelViewProjectionMatrix * position;\n\
  46.            }";
  47.             string fragment = "#version 150\n\
  48.            \n\
  49.            uniform sampler2DRect tex0;\
  50.            uniform sampler2DRect maskTex;\
  51.            in vec2 texCoordVarying;\n\
  52.            \
  53.            out vec4 fragColor;\n\
  54.            void main (void){\
  55.            vec2 pos = texCoordVarying;\
  56.            \
  57.            vec3 src = texture(tex0, pos).rgb;\
  58.            float mask = texture(maskTex, pos).r;\
  59.            \
  60.            fragColor = vec4( src , mask);\
  61.            }";
  62.             shader.setupShaderFromSource(GL_VERTEX_SHADER, vertex);
  63.             shader.setupShaderFromSource(GL_FRAGMENT_SHADER, fragment);
  64.             shader.bindDefaults();
  65.             shader.linkProgram();
  66.         }else{
  67.             string shaderProgram = "#version 120\n \
  68.            #extension GL_ARB_texture_rectangle : enable\n \
  69.            \
  70.            uniform sampler2DRect tex0;\
  71.            uniform sampler2DRect maskTex;\
  72.            \
  73.            void main (void){\
  74.            vec2 pos = gl_TexCoord[0].st;\
  75.            \
  76.            vec3 src = texture2DRect(tex0, pos).rgb;\
  77.            float mask = texture2DRect(maskTex, pos).r;\
  78.            \
  79.            gl_FragColor = vec4( src , mask);\
  80.            }";
  81.             shader.setupShaderFromSource(GL_FRAGMENT_SHADER, shaderProgram);
  82.             shader.linkProgram();
  83.         }
  84.     #endif
  85.        
  86.         maskFbo.begin();
  87.         ofClear(0,0,0,255);
  88.         maskFbo.end();
  89.        
  90.         fbo.begin();
  91.         ofClear(0,0,0,255);
  92.         fbo.end();
  93. }
  94.  
  95. //--------------------------------------------------------------
  96. void testApp::update(){
  97.     cam.update();
  98.     vidPlayer.update();
  99.    
  100.     if(cam.isFrameNew()) {//if new frame
  101.         contourFinder.setThreshold(80);
  102.         contourFinder.findContours(cam);//find contour of current frame
  103.     }
  104.  
  105.     // MASK (frame buffer object)
  106.     maskFbo.begin();
  107.         pathFromContour.draw(0,0);//GENERATED SHAPES FROM CONTOUR
  108.     maskFbo.end();
  109.    
  110.     // HERE the shader-masking happens
  111.     fbo.begin();
  112.         ofClear(0, 0, 0, 0);
  113.         shader.begin();
  114.             shader.setUniformTexture("maskTex", maskFbo.getTextureReference(), 1 );
  115.             vidPlayer.draw(0,0);//MY VIDEO HERE
  116.         shader.end();
  117.     fbo.end();
  118.    
  119. }
  120.  
  121. //--------------------------------------------------------------
  122. void testApp::draw(){
  123.    
  124.     ofSetColor(255);
  125.     cam.draw(0, 0);
  126.     contourFinder.draw();
  127.    
  128.     int n = contourFinder.size();//NUMBER OF SHAPES
  129.    
  130.     for(int i = 0; i < n; i++) {
  131.        
  132.         ofPoint center = toOf(contourFinder.getCenter(i));//center point of contour
  133.         double area = contourFinder.getContourArea(i);//area of contour
  134.        
  135.         ofPushMatrix();
  136.             ofTranslate(center.x, center.y);
  137.             int label = contourFinder.getLabel(i);//gets ID
  138.             string msg = "ID: " + ofToString(label);
  139.             string areaMsg = "AREA: " + ofToString(area);
  140.             ofSetColor(255,0,0);
  141.             ofDrawBitmapString(msg, 0, 0);
  142.             ofDrawBitmapString(areaMsg, 0, 10);
  143.             ofScale(5, 5);
  144.         ofPopMatrix();
  145.        
  146.         //MUST BE LINKED WITH AN IDENTIFIER.
  147.         ofPolyline polyline = contourFinder.getPolyline(i);//to convert
  148.        
  149.         ofPath pathFromContour;//path to be built
  150.        
  151.         for(int j = 0; j < polyline.getVertices().size(); j++) {
  152.             if(j == 0) {
  153.                 pathFromContour.newSubPath();
  154.                 pathFromContour.moveTo(polyline.getVertices()[j]);
  155.             } else {
  156.                 pathFromContour.lineTo(polyline.getVertices()[j]);
  157.             }
  158.         }
  159.        
  160.         pathFromContour.close();
  161.         pathFromContour.simplify();
  162.        
  163.         //WHY ARE THEY FILLING SO CHAOTICALLY?
  164.         ofColor pathColor(255);
  165.         pathFromContour.setFillColor(pathColor);
  166.        
  167.     }
  168.    
  169.     fbo.draw(0,0);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement