[ Video ]
[ About ]
ofFboに絵を描写してテキスチャとしてシェーダに送信、シェーダでピクセルずらし処理
Draw picture on ofFbo and send it to shader, pixel shift processing with shader.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#pragma once #include "ofMain.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void mouseEntered(int x, int y) {}; void mouseExited(int x, int y) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofFbo fbo; ofTrueTypeFont font; glm::vec3 x_noise; glm::vec3 y_noise; ofShader shader; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); this->fbo.allocate(ofGetWidth(), ofGetHeight()); this->font.loadFont("fonts/Kazesawa-bold.ttf", 180, true, true, true); this->x_noise = glm::vec3(0.f, 0.f, 0.f); this->y_noise = glm::vec3(0.f, 0.f, 0.f); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { this->fbo.begin(); ofClear(0); ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); ofSetColor(255); string word = "RGB"; this->font.drawString(word, this->font.stringWidth(word) * -0.5, ofGetHeight() * -0.25 + this->font.stringHeight(word) * 0.5); word = "DRIFT"; this->font.drawString(word, this->font.stringWidth(word) * -0.5, ofGetHeight() * 0.25 + this->font.stringHeight(word) * 0.5); this->fbo.end(); ofSeedRandom(39); this->x_noise.r = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); this->x_noise.g = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); this->x_noise.b = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); this->y_noise.r = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); this->y_noise.g = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); this->y_noise.b = ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.01), 0, 1, -1, 1); } //-------------------------------------------------------------- void ofApp::draw() { this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniformTexture("tex", this->fbo.getTexture(), 1); this->shader.setUniform3f("x_noise", this->x_noise); this->shader.setUniform3f("y_noise", this->y_noise); ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight()); this->shader.end(); } //-------------------------------------------------------------- int main() { ofGLWindowSettings settings; settings.setGLVersion(3, 2); settings.setSize(720, 720); ofCreateWindow(settings); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 |
#version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; void main(){ gl_Position = modelViewProjectionMatrix * position; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
#version 150 uniform float time; uniform vec2 resolution; uniform sampler2DRect tex; uniform vec3 x_noise; uniform vec3 y_noise; out vec4 outputColor; // 2D Random float random (in vec2 st) { return fract(sin(dot(st.xy, vec2(12.9898,78.233))) * 43758.5453123); } // 2D Noise based on Morgan McGuire @morgan3d // https://www.shadertoy.com/view/4dS3Wd float noise (in vec2 st) { vec2 i = floor(st); vec2 f = fract(st); // Four corners in 2D of a tile float a = random(i); float b = random(i + vec2(1.0, 0.0)); float c = random(i + vec2(0.0, 1.0)); float d = random(i + vec2(1.0, 1.0)); // Smooth Interpolation // Cubic Hermine Curve. Same as SmoothStep() vec2 u = f*f*(3.0-2.0*f); // u = smoothstep(0.,1.,f); // Mix 4 coorners percentages return mix(a, b, u.x) + (c - a)* u.y * (1.0 - u.x) + (d - b) * u.x * u.y; } void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); float drift = 60.0; float r = texture(tex, vec2(gl_FragCoord.x + drift * noise(vec2(p.x + x_noise.r, p.y + time)), (resolution.y - gl_FragCoord.y) + drift * noise(vec2(p.x + time, p.y + y_noise.r)))).r; float g = texture(tex, vec2(gl_FragCoord.x + drift * noise(vec2(p.x + x_noise.g, p.y + time)), (resolution.y - gl_FragCoord.y) + drift * noise(vec2(p.x + time, p.y + y_noise.g)))).g; float b = texture(tex, vec2(gl_FragCoord.x + drift * noise(vec2(p.x + x_noise.b, p.y + time)), (resolution.y - gl_FragCoord.y) + drift * noise(vec2(p.x + time, p.y + y_noise.b)))).b; outputColor = vec4(r, g, b, 1.0); } |