Permalink
Browse files

added cloning wrapper example

  • Loading branch information...
1 parent 694862a commit fe60496d5dbfa7733999c829b135fcd149883710 @kylemcdonald kylemcdonald committed Oct 14, 2011
@@ -0,0 +1,9 @@
+//THE PATH TO THE ROOT OF OUR OF PATH RELATIVE TO THIS PROJECT.
+//THIS NEEDS TO BE DEFINED BEFORE CoreOF.xcconfig IS INCLUDED
+OF_PATH = ../../..
+
+//THIS HAS ALL THE HEADER AND LIBS FOR OF CORE
+#include "../../../libs/openFrameworksCompiled/project/osx/CoreOF.xcconfig"
+
+OTHER_LDFLAGS = $(OF_CORE_LIBS)
+HEADER_SEARCH_PATHS = $(OF_CORE_HEADERS)
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.openFrameworks</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+</dict>
+</plist>
@@ -0,0 +1,109 @@
+#include "Clone.h"
+
+char maskBlurShaderSource[] =
+"uniform sampler2DRect tex, mask;\
+uniform vec2 direction;\
+uniform int k;\
+void main() {\
+ vec2 pos = gl_TexCoord[0].st;\
+ vec4 sum = texture2DRect(tex, pos);\
+ int i;\
+ for(i = 1; i < k; i++) {\
+ vec2 curOffset = float(i) * direction;\
+ vec4 leftMask = texture2DRect(mask, pos - curOffset);\
+ vec4 rightMask = texture2DRect(mask, pos + curOffset);\
+ bool valid = leftMask.r > 0. && rightMask.r > 0.;\
+ if(valid) {\
+ sum +=\
+ texture2DRect(tex, pos + curOffset) +\
+ texture2DRect(tex, pos - curOffset);\
+ } else {\
+ break;\
+ }\
+ }\
+ int samples = 1 + (i - 1) * 2;\
+ gl_FragColor = sum / float(samples);\
+}";
+
+char cloneShaderSource[] =
+"uniform sampler2DRect src, srcBlur, dstBlur;\
+void main() {\
+ vec2 pos = gl_TexCoord[0].st; \
+ vec4 srcColorBlur = texture2DRect(srcBlur, pos);\
+ if(srcColorBlur.a > 0.) {\
+ vec3 srcColor = texture2DRect(src, pos).rgb;\
+ vec4 dstColorBlur = texture2DRect(dstBlur, pos);\
+ vec3 offset = (dstColorBlur.rgb - srcColorBlur.rgb);\
+ gl_FragColor = vec4(srcColor + offset, 1.);\
+ } else {\
+ gl_FragColor = vec4(0.);\
+ }\
+}";
+
+void Clone::setup(int width, int height) {
+ ofFbo::Settings settings;
+ settings.width = width;
+ settings.height = height;
+
+ buffer.allocate(settings);
+ srcBlur.allocate(settings);
+ dstBlur.allocate(settings);
+
+ maskBlurShader.setupShaderFromSource(GL_FRAGMENT_SHADER, maskBlurShaderSource);
+ cloneShader.setupShaderFromSource(GL_FRAGMENT_SHADER, cloneShaderSource);
+ maskBlurShader.linkProgram();
+ cloneShader.linkProgram();
+
+ strength = 0;
+}
+
+void Clone::maskedBlur(ofTexture& tex, ofTexture& mask, ofFbo& result) {
+ int k = strength;
+
+ buffer.begin();
+ maskBlurShader.begin();
+ maskBlurShader.setUniformTexture("tex", tex, 1);
+ maskBlurShader.setUniformTexture("mask", mask, 2);
+ maskBlurShader.setUniform2f("direction", 1, 0);
+ maskBlurShader.setUniform1i("k", k);
+ tex.draw(0, 0);
+ maskBlurShader.end();
+ buffer.end();
+
+ result.begin();
+ maskBlurShader.begin();
+ maskBlurShader.setUniformTexture("tex", buffer, 1);
+ maskBlurShader.setUniformTexture("mask", mask, 2);
+ maskBlurShader.setUniform2f("direction", 0, 1);
+ maskBlurShader.setUniform1i("k", k);
+ buffer.draw(0, 0);
+ maskBlurShader.end();
+ result.end();
+}
+
+void Clone::setStrength(int strength) {
+ this->strength = strength;
+}
+
+void Clone::update(ofTexture& src, ofTexture& dst, ofTexture& mask) {
+ maskedBlur(src, mask, srcBlur);
+ maskedBlur(dst, mask, dstBlur);
+
+ buffer.begin();
+ ofPushStyle();
+ ofEnableAlphaBlending();
+ dst.draw(0, 0);
+ cloneShader.begin();
+ cloneShader.setUniformTexture("src", src, 1);
+ cloneShader.setUniformTexture("srcBlur", srcBlur, 2);
+ cloneShader.setUniformTexture("dstBlur", dstBlur, 3);
+ dst.draw(0, 0);
+ cloneShader.end();
+ ofDisableAlphaBlending();
+ ofPopStyle();
+ buffer.end();
+}
+
+void Clone::draw(float x, float y) {
+ buffer.draw(x, y);
+}
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "ofMain.h"
+
+class Clone {
+public:
+ void setup(int width, int height);
+ void setStrength(int strength);
+ void update(ofTexture& src, ofTexture& dst, ofTexture& mask);
+ void draw(float x, float y);
+
+protected:
+ void maskedBlur(ofTexture& tex, ofTexture& mask, ofFbo& result);
+ ofFbo buffer, srcBlur, dstBlur;
+ ofShader maskBlurShader, cloneShader;
+ int strength;
+};
@@ -0,0 +1,8 @@
+#include "testApp.h"
+#include "ofAppGlutWindow.h"
+
+int main() {
+ ofAppGlutWindow window;
+ ofSetupOpenGL(&window, 640, 480, OF_WINDOW);
+ ofRunApp(new testApp());
+}
@@ -0,0 +1,36 @@
+#include "testApp.h"
+
+void testApp::setup() {
+ ofSetVerticalSync(true);
+ w = 640, h = 480;
+ src.allocate(640, 480);
+ mask.allocate(640, 480);
+ dst.initGrabber(w, h);
+ clone.setup(w, h);
+}
+
+void testApp::update() {
+ dst.update();
+ if(dst.isFrameNew()) {
+ int x = mouseX, y = mouseY;
+
+ ofSetColor(255);
+
+ src.begin();
+ dst.draw(x - w / 2, y - h / 2);
+ src.end();
+
+ mask.begin();
+ ofClear(0, 255);
+ ofCircle(x, y, 32);
+ mask.end();
+
+ clone.setStrength(32);
+ clone.update(src.getTextureReference(), dst.getTextureReference(), mask.getTextureReference());
+ }
+}
+
+void testApp::draw() {
+ ofBackground(0);
+ clone.draw(0, 0);
+}
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "ofMain.h"
+#include "Clone.h"
+
+class testApp : public ofBaseApp {
+public:
+ void setup();
+ void update();
+ void draw();
+
+ int w, h;
+ ofFbo src, mask;
+ ofVideoGrabber dst;
+ Clone clone;
+};

0 comments on commit fe60496

Please sign in to comment.