[ Video ]
[ About ]
TrueTypeFontから輪郭の座標を取得して逃げるParticleを生成。
Particle created from TrueTypeFont locations.
[ 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 |
#pragma once #include "ofMain.h" #include "ofFbo.h" #include "Particle.h" class ofApp : public ofBaseApp { public: ~ofApp(); 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) {}; ofFbo fbo; ofTrueTypeFont font; vector<vector<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 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#include "ofApp.h" //-------------------------------------------------------------- ofApp::~ofApp() { for (int i = this->particles.size() - 1; i > -1; i--) { for (int j = this->particles[i].size() - 1; j > -1; j--) { delete this->particles[i][j]; this->particles[i].erase(this->particles[i].begin() + j); } } } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofBackground(0); ofSetWindowTitle("Insta"); ofSetLineWidth(0.5); this->font.loadFont("fonts/Kazesawa-Bold.ttf", 150, true, false, true); vector<string> words; words.push_back("CODE"); words.push_back("+"); words.push_back("LIFE"); ofColor body_color; for (int word_index = 0; word_index < words.size(); word_index++) { ofVec2f word_size = ofVec2f(this->font.stringWidth(words[word_index]), -this->font.stringHeight(words[word_index])); vector<ofTTFCharacter> char_paths = this->font.getStringAsPoints(words[word_index]); vector<Particle*> tmp_particles; for (int path_index = 0; path_index < char_paths.size(); path_index++) { vector<ofPolyline> outline = char_paths[path_index].getOutline(); body_color.setHsb(ofRandom(255), 255, 255); for (int outline_index = 0; outline_index < outline.size(); outline_index++) { outline[outline_index] = outline[outline_index].getResampledBySpacing(5); tmp_particles.clear(); tmp_particles.shrink_to_fit(); vector<ofPoint> vertices = outline[outline_index].getVertices(); for (int vertices_index = 0; vertices_index < vertices.size(); vertices_index++) { ofVec2f location = vertices[vertices_index] - word_size / 2; location.y = location.y + 150 * (word_index - 1); tmp_particles.push_back(new Particle(location, body_color)); } this->particles.push_back(tmp_particles); } } } this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { this->fbo.begin(); ofClear(0); ofTranslate(ofGetWidth() / 2, ofGetHeight() / 2); ofVec2f mouse_point = ofVec2f(ofGetMouseX() - ofGetWidth() / 2, ofGetMouseY() - ofGetHeight() / 2); ofDrawCircle(mouse_point, 3); for (int i = 0; i < this->particles.size(); i++) { for (int j = 0; j < this->particles[i].size(); j++) { float distance = mouse_point.distance(this->particles[i][j]->get_location()); if (distance < 50) { this->particles[i][j]->seek_r(mouse_point); } else { this->particles[i][j]->seek(this->particles[i][j]->get_start_location()); } this->particles[i][j]->update(); } } for (int i = 0; i < this->particles.size(); i++) { //ofBeginShape(); for (int j = 0; j < this->particles[i].size(); j++) { this->particles[i][j]->draw(); //ofVertex(this->particles[i][j]->get_location().x, this->particles[i][j]->get_location().y); } //ofEndShape(true); } this->fbo.end(); } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); } //-------------------------------------------------------------- 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 22 23 24 25 26 27 28 29 30 |
#pragma once #include "ofMain.h" class Particle { public: Particle(); Particle(ofVec3f location, ofColor body_color); ~Particle(); void apply_force(ofVec3f force); void seek(ofVec3f target); void seek_r(ofVec3f target); void update(); void draw(); ofVec3f get_location(); ofVec3f get_start_location(); private: float size; ofVec3f location; ofVec3f start_location; ofVec3f velocity; ofVec3f acceleration; float max_force; float max_speed; float radius; 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 75 76 77 78 79 80 |
#include "Particle.h" Particle::Particle() : Particle(ofVec3f(0, 0, 0), ofColor(255)) { } Particle::Particle(ofVec3f location, ofColor body_color) { this->location = location; this->start_location = location; this->velocity = ofVec3f(); this->acceleration = ofVec3f(); this->size = 5; this->body_color = body_color; this->radius = radius; this->max_force = 10; this->max_speed = 300; this->radius = 5; } Particle::~Particle() { } void Particle::apply_force(ofVec3f force) { this->acceleration += force; } void Particle::seek(ofVec3f target) { ofVec3f desired = target - this->location; float distance = desired.length(); desired.normalize(); if (distance < this->radius) { if (distance < 1) { this->location = this->start_location; return; } else { distance *= ofMap(distance, 0, this->radius, 0, this->max_speed); } } else { distance *= max_speed; } ofVec3f steer = desired - this->velocity; steer.limit(this->max_force); apply_force(steer); } void Particle::seek_r(ofVec3f target) { ofVec3f desired = target - this->location; desired.normalize(); desired *= this->max_speed * 0.5; ofVec3f steer = desired - this->velocity; steer.limit(this->max_force * 0.5); steer *= -1; apply_force(steer); } void Particle::update() { this->velocity += this->acceleration; this->velocity.limit(this->max_speed); this->location += this->velocity; this->acceleration *= 0; this->velocity *= 0.98; } void Particle::draw() { ofSetColor(this->body_color); ofDrawCircle(this->location, this->size); } ofVec3f Particle::get_location() { return this->location; } ofVec3f Particle::get_start_location() { return this->start_location; } |