[ Video ]
[ About ]
Twitterのタイムラインで水の波紋を作っている方を見かけて「影を活かすならこれじゃん!」と、過去のヤツと昨日のヤツを組み合わせました。
影を足しただけですが、水面と底面が感じられて、透明感が当社比390% UP!
最近は、あえてクラスは起こさずに、ofSeedRandomを使って、生成された固定の乱数とフレーム番号の組み合わせで乗り切ってきたんですが、今回は断念してRippleクラスを作りました。
あとopenFrameworksの0.10.0が出たのでVisual Studio 2017で作りました。あんま自分が触ってる周りでは関係なさそうですが…
[ 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" #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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; vector<unique_ptr<Ripple>> ripples; ofFbo fbo; ofPixels pixels; cv::Mat pixels_mat; }; |
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 84 85 86 87 88 89 90 91 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(239); ofSetWindowTitle("Insta"); ofNoFill(); ofSetLineWidth(1.5); // Fbo Initialize (+ Shadow Gap) this->fbo.allocate(ofGetWidth() + 25, ofGetHeight() + 25); // Link pixels And pixels_mat this->fbo.readToPixels(this->pixels); this->pixels_mat = cv::Mat(this->pixels.getHeight(), this->pixels.getWidth(), CV_8UC4, this->pixels.getData()); } //-------------------------------------------------------------- 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(); // Copy Fbo To pixels this->fbo.readToPixels(this->pixels); // Gray cv::Mat gray_mat; cv::cvtColor(this->pixels_mat, gray_mat, cv::COLOR_RGBA2GRAY); for (int y = 0; y < this->pixels_mat.cols; y++) { for (int x = 0; x < this->pixels_mat.rows; x++) { if (gray_mat.at<unsigned char>(y, x) > 0) { this->pixels_mat.at<cv::Vec4b>(y, x) = cv::Vec4b(gray_mat.at<unsigned char>(y, x), gray_mat.at<unsigned char>(y, x), gray_mat.at<unsigned char>(y, x), this->pixels_mat.at<cv::Vec4b>(y, x)[3]); } } } // Blur Process cv::GaussianBlur(this->pixels_mat, this->pixels_mat, cv::Size(19, 19), 10, 10); } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(255); // Draw Shadow ofImage draw_image; draw_image.setFromPixels(this->pixels); draw_image.draw(-5, -5); // Draw Fbo Image this->fbo.draw(-25, -25); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); 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 |
#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, 30); 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++) { ofVertex(this->radius * cos(deg * DEG_TO_RAD), this->radius * sin(deg * DEG_TO_RAD)); } ofEndShape(true); } ofPopMatrix(); } bool Ripple::isLife() { return this->life; } |