[ Video ]
[ About ]
だいぶ前にOpenCVでやって影の生成をシェーダを使ってやってみました。はやいっ!
Generate shadows with shaders.
[ 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 |
#pragma once #include "ofMain.h" #include "Ripple.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) {}; vector<unique_ptr<Ripple>> ripples; ofFbo fbo; 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(239); ofSetLineWidth(3); this->fbo.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { // Make New Ripple if (ofGetFrameNum() % 3 == 0) { ofPoint point(ofRandom(this->fbo.getWidth()), ofRandom(this->fbo.getHeight()), ofRandom(500, 1000)); unique_ptr<Ripple> ripple(new Ripple(point)); this->ripples.push_back(move(ripple)); } // Draw Ripples this->fbo.begin(); ofClear(0); for (int i = this->ripples.size() - 1; i >= 0; i--) { if (this->ripples[i]->isLife()) { this->ripples[i]->update(); this->ripples[i]->draw(); } else { this->ripples.erase(this->ripples.begin() + i); } } this->fbo.end(); } //-------------------------------------------------------------- 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); ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight()); this->shader.end(); this->fbo.draw(0, 0); } //-------------------------------------------------------------- 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 13 14 15 16 17 18 19 20 21 |
#pragma once #include "ofMain.h" class Ripple { public: Ripple(); Ripple(ofPoint point); void update(); void draw(); bool isLife(); private: ofPoint point; float radius; float radius_max; float radius_span; ofColor body_color; bool life; }; |
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 |
#include "Ripple.h" Ripple::Ripple() : Ripple(ofPoint()) { } Ripple::Ripple(ofPoint point) { this->point = point; this->radius = 1; this->radius_max = point.z / 10; this->body_color.setHsb(ofRandom(255), 255, 255); this->life = true; } void Ripple::update() { if (this->life == false) { return; } if (this->point.z > 0) { this->point -= ofPoint(0, 0, 10); return; } if (this->radius > this->radius_max) { this->life = false; return; } else { this->radius += 1; } } void Ripple::draw() { ofPushMatrix(); ofTranslate(this->point); if (this->point.z > 0) { ofSetColor(this->body_color); ofDrawLine(ofPoint(), ofPoint(0, 0, 30)); } else { ofSetColor(this->body_color, ofMap(this->radius, 1, this->radius_max, 255, 10)); ofBeginShape(); for (int deg = 0; deg <= 360; deg += 10) { ofVertex(this->radius * cos(deg * DEG_TO_RAD), this->radius * sin(deg * DEG_TO_RAD)); } for (int deg = 360; deg >= 0; deg -= 10) { ofVertex(this->radius * 0.9 * cos(deg * DEG_TO_RAD), this->radius * 0.9 * sin(deg * DEG_TO_RAD)); } ofEndShape(true); } ofPopMatrix(); } bool Ripple::isLife() { return this->life; } |
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 |
#version 150 uniform float time; uniform vec2 resolution; uniform sampler2DRect tex; out vec4 outputColor; void main() { vec4 t = texture(tex, vec2(gl_FragCoord.x - 25, (resolution.y - gl_FragCoord.y) - 25)); float gray = dot(t.rgb, vec3(0.299, 0.587, 0.114)); outputColor = vec4(vec3(gray), t.a * 0.2); } |