[ Video ]
[ About ]
Instagramへの連続投稿365日達成しました(たぶん)
365 day continuous uploaded.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); ~Particle() {}; void apply_force(ofVec2f force); void update(); void draw(vector<shared_ptr<Particle>> particles); void set_color(ofColor body_color); ofVec2f get_location(); ofColor get_body_color(); private: ofVec2f location; ofVec2f velocity; ofVec2f acceleration; ofColor body_color; }; |
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 "Particle.h" Particle::Particle() { this->location = ofVec2f(ofRandom(ofGetWidth()), ofRandom(ofGetHeight())); this->velocity = ofVec2f(ofRandom(-3, 3), ofRandom(-3, 3)); this->acceleration = ofVec2f(); } void Particle::apply_force(ofVec2f force) { this->acceleration += force; } void Particle::update() { this->acceleration += this->velocity; this->location += this->acceleration; this->acceleration *= 0; if (this->location.x < 0) { this->location.x = 0; this->velocity.x *= -1; } else if (this->location.x > ofGetWidth()) { this->location.x = ofGetWidth(); this->velocity.x *= -1; } if (this->location.y < 0) { this->location.y = 0; this->velocity.y *= -1; } else if (this->location.y > ofGetHeight()) { this->location.y = ofGetHeight(); this->velocity.y *= -1; } } void Particle::draw(vector<shared_ptr<Particle>> particles) { if (this->body_color != ofColor(0, 0, 0, 0)) { ofSetColor(this->body_color, 128); float distance; vector<ofVec2f> near_point; for (int i = 0; i < particles.size(); i++) { distance = this->location.squareDistance(particles[i]->get_location()); if (distance > 0 && distance < 1000) { if (particles[i]->get_body_color() != ofColor(0, 0, 0)) { near_point.push_back(particles[i]->get_location()); } } } if (near_point.size() < 2) { return; } ofBeginShape(); for (int i = 0; i < near_point.size(); i++) { ofVertex(near_point[i].x, near_point[i].y); } ofEndShape(true); } } void Particle::set_color(ofColor body_color) { this->body_color = body_color; } ofVec2f Particle::get_location() { return this->location; } ofColor Particle::get_body_color() { return this->body_color; } |
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 "ofFbo.h" #include "Particle.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) {}; ofImage photo; ofFbo fbo; vector<shared_ptr<Particle>> particles; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); this->photo.loadImage("photo2.png"); for (int i = 0; i < 2048; i++) { this->particles.push_back(shared_ptr<Particle>(new Particle())); } this->fbo.allocate(720, 720); } //-------------------------------------------------------------- void ofApp::update() { ofPixels pixels = this->photo.getPixelsRef(); ofColor pixel_color; for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->update(); } this->fbo.begin(); ofClear(0); for (int i = 0; i < this->particles.size(); i++) { pixel_color = pixels.getColor(this->particles[i]->get_location().x, this->particles[i]->get_location().y); this->particles[i]->set_color(pixel_color); if (pixel_color != ofColor(0, 0, 0, 0)) { this->particles[i]->draw(this->particles); } } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |