[ Video ]
[ About ]
シェーダに前フレームをテキスチャとして送信、残像として重ねて描写。
Send the previous frame as a texture to the shader and overlay it as an afterimage.
[ 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) {}; int number_of_targets; vector<glm::vec4> targets; vector<glm::vec4> colors; 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->number_of_targets = 21; for (int i = 0; i < this->number_of_targets; i++) { this->targets.push_back(glm::vec4(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), 0.0, 0.0)); glm::vec4 color = glm::vec4(1.0, 1.0, 1.0, 0.0); int color_type = i % 3; switch (color_type) { case 0: color = glm::vec4(1.0, 0.0, 0.0, 0.0); break; case 1: color = glm::vec4(0.0, 1.0, 0.0, 0.0); break; case 2: color = glm::vec4(0.0, 0.0, 1.0, 0.0); break; } this->colors.push_back(color); } this->src.allocate(ofGetWidth(), ofGetHeight()); this->dst.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(this->number_of_targets); glm::vec4 rgb_point; for (int i = 0; i < this->number_of_targets; i++) { if (i % 3 == 0) { rgb_point = glm::vec4(ofNoise(ofRandom(this->number_of_targets), ofGetFrameNum() * 0.005) * ofGetWidth(), ofNoise(ofRandom(this->number_of_targets), ofGetFrameNum() * 0.005) * ofGetHeight(), 0.0, 0.0); } this->targets[i] = rgb_point + glm::vec4(ofMap(ofNoise(ofRandom(this->number_of_targets), ofGetFrameNum() * 0.01), 0, 1, -30, 30), ofMap(ofNoise(ofRandom(this->number_of_targets), ofGetFrameNum() * 0.01), 0, 1, -30, 30), 0.0, 0.0); } } //-------------------------------------------------------------- void ofApp::draw() { this->dst.begin(); ofClear(0); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniform4fv("targets", &this->targets[0].x, this->number_of_targets); this->shader.setUniform4fv("colors", &this->colors[0].x, this->number_of_targets); this->shader.setUniformTexture("tex0", this->src.getTexture(), 1); this->src.draw(0, 0); this->shader.end(); this->dst.end(); ofSetColor(255); this->dst.draw(0, 0); std::swap(this->src, this->dst); } //-------------------------------------------------------------- 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 9 10 11 12 |
#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 |
#version 150 const int number_of_targets = 21; uniform float time; uniform vec2 resolution; uniform vec4 targets[number_of_targets]; uniform vec4 colors[number_of_targets]; uniform sampler2DRect tex0; in vec2 texCoordVarying; out vec4 outputColor; void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); vec3 color = vec3(0.0); for(int i = 0; i < number_of_targets; i++){ vec2 t = vec2(targets[i].x, -targets[i].y) / min(resolution.x, resolution.y) * 2.0; t.xy += vec2(-resolution.x, resolution.y) / min(resolution.x, resolution.y); float r = 0.01 / length(p - t) * colors[i].x; float g = 0.01 / length(p - t) * colors[i].y; float b = 0.01 / length(p - t) * colors[i].z; vec3 c = vec3(smoothstep(0.003, 1.0, r), smoothstep(0.003, 1.0, g), smoothstep(0.003, 1.0, b)); color += c; } vec4 tex = texture(tex0, texCoordVarying); outputColor = vec4(color, 1.0) + tex * 0.9; } |