[ Video ]
[ About ]
動画の10フレーム分をテキスチャとしてシェーダに送信、ピクセルごとにノイズで描写フレームを操作。OpenCVでも何度か似た処理をやってきたけど、こちらの方が早くて素敵。
Send 10 frames of video to shader. Pixel drawing frame is determinated by noise.
[ 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 31 32 33 |
#pragma once #include "ofMain.h" #include <opencv2/opencv.hpp> 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) {}; cv::VideoCapture cap; cv::Size cap_size; vector<cv::Mat> frames; vector<ofImage> images; ofFbo src; ofFbo dst; 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openframeworks"); this->cap.open("image1.mp4"); this->cap_size = cv::Size(1280, 720); cv::Mat cap_frame; while(true){ this->cap >> cap_frame; if (cap_frame.empty()) { break; } ofImage image; image.allocate(this->cap_size.width, this->cap_size.height, OF_IMAGE_COLOR); cv::Mat frame = cv::Mat(cv::Size(image.getWidth(), image.getHeight()), CV_MAKETYPE(CV_8UC3, image.getPixels().getNumChannels()), image.getPixels().getData(), 0); cv::resize(cap_frame, cap_frame, this->cap_size); cv::cvtColor(cap_frame, cap_frame, CV_BGR2RGB); cap_frame.copyTo(frame); this->frames.push_back(frame); this->images.push_back(image); } this->src.allocate(ofGetWidth(), ofGetHeight()); this->dst.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { } //-------------------------------------------------------------- void ofApp::draw() { int frame_number = ofGetFrameNum() % this->images.size(); this->dst.begin(); ofClear(0); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniformTexture("tex0", this->images[(frame_number + 0) % this->images.size()], 1); this->shader.setUniformTexture("tex1", this->images[(frame_number + 2) % this->images.size()], 2); this->shader.setUniformTexture("tex2", this->images[(frame_number + 4) % this->images.size()], 3); this->shader.setUniformTexture("tex3", this->images[(frame_number + 6) % this->images.size()], 4); this->shader.setUniformTexture("tex4", this->images[(frame_number + 8) % this->images.size()], 5); this->shader.setUniformTexture("tex5", this->images[(frame_number + 10) % this->images.size()], 5); this->shader.setUniformTexture("tex6", this->images[(frame_number + 12) % this->images.size()], 5); this->shader.setUniformTexture("tex7", this->images[(frame_number + 14) % this->images.size()], 5); this->shader.setUniformTexture("tex8", this->images[(frame_number + 16) % this->images.size()], 5); this->shader.setUniformTexture("tex9", this->images[(frame_number + 18) % this->images.size()], 5); this->src.draw(0, 0); this->shader.end(); this->dst.end(); ofSetColor(255); this->dst.draw(0, 0); } //-------------------------------------------------------------- int main() { ofGLWindowSettings settings; settings.setGLVersion(3, 2); settings.setSize(1280, 720); ofCreateWindow(settings); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; in vec2 texcoord; out vec2 texCoordVarying; void main(){ texCoordVarying = texcoord; 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
#version 150 uniform float time; uniform vec2 resolution; uniform sampler2DRect tex0; uniform sampler2DRect tex1; uniform sampler2DRect tex2; uniform sampler2DRect tex3; uniform sampler2DRect tex4; uniform sampler2DRect tex5; uniform sampler2DRect tex6; uniform sampler2DRect tex7; uniform sampler2DRect tex8; uniform sampler2DRect tex9; in vec2 texCoordVarying; out vec4 outputColor; float mod289(float x){return x - floor(x * (1.0 / 289.0)) * 289.0;} vec4 mod289(vec4 x){return x - floor(x * (1.0 / 289.0)) * 289.0;} vec4 perm(vec4 x){return mod289(((x * 34.0) + 1.0) * x);} float noise(vec3 p){ vec3 a = floor(p); vec3 d = p - a; d = d * d * (3.0 - 2.0 * d); vec4 b = a.xxyy + vec4(0.0, 1.0, 0.0, 1.0); vec4 k1 = perm(b.xyxy); vec4 k2 = perm(k1.xyxy + b.zzww); vec4 c = k2 + a.zzzz; vec4 k3 = perm(c); vec4 k4 = perm(c + 1.0); vec4 o1 = fract(k3 * (1.0 / 41.0)); vec4 o2 = fract(k4 * (1.0 / 41.0)); vec4 o3 = o2 * d.z + o1 * (1.0 - d.z); vec2 o4 = o3.yw * d.x + o3.xz * (1.0 - d.x); return o4.y * d.y + o4.x * (1.0 - d.y); } void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); vec4 color = vec4(0.0); float v = noise(vec3(p.x * 13.0, p.y * 13.0, time)); if(v < 0.1){ color += texture(tex0, texCoordVarying); }else if(v < 0.2){ color += texture(tex1, texCoordVarying); }else if(v < 0.3){ color += texture(tex2, texCoordVarying); }else if(v < 0.4){ color += texture(tex3, texCoordVarying); }else if(v < 0.5){ color += texture(tex4, texCoordVarying); }else if(v < 0.6){ color += texture(tex5, texCoordVarying); }else if(v < 0.7){ color += texture(tex6, texCoordVarying); }else if(v < 0.8){ color += texture(tex7, texCoordVarying); }else if(v < 0.9){ color += texture(tex8, texCoordVarying); }else { color += texture(tex9, texCoordVarying); } outputColor = color; } |