[ Video ]
[ About ]
変化する文字の領域だけを動くエージェント
Agents are moving on a changing character.
[ 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 |
#pragma once #include "ofMain.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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; int number_of_agent; int len_of_tail; vector<deque<glm::vec2>> logs; vector<glm::vec2> directions; vector<ofColor> colors; ofTrueTypeFont font; ofFbo fbo; ofPixels pixels; }; |
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 108 109 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(1.5); this->number_of_agent = 100; this->len_of_tail = 10; this->font.loadFont("fonts/Kazesawa-Bold.ttf", 450, true, true, true); this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { vector<string> characters = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; this->fbo.begin(); ofClear(0); ofSetColor(39); string draw_character = characters[(int)(ofGetFrameNum() * 0.01) % characters.size()]; this->font.drawString(draw_character, ofGetWidth() * 0.5 - this->font.stringWidth(draw_character) * 0.5, ofGetHeight() * 0.5 + this->font.stringHeight(draw_character) * 0.5); this->fbo.end(); this->fbo.readToPixels(this->pixels); while (this->logs.size() < this->number_of_agent) { int x = ofRandom(ofGetWidth()); int y = ofRandom(ofGetHeight()); if (this->pixels.getColor(x, y) != ofColor(0, 0)) { deque<glm::vec2> log; log.push_front(glm::vec2(x, y)); this->logs.push_back(log); this->directions.push_back(glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1)))); ofColor color; color.setHsb(ofRandom(255), 255, 200); this->colors.push_back(color); } } for (int i = 0; i < this->number_of_agent; i++) { glm::vec2 future; if (this->pixels.getColor((int)this->logs[i].front().x, (int)this->logs[i].front().y) == ofColor(0, 0)) { future = this->logs[i].front(); } else { future = this->logs[i].front() + this->directions[i] * 5; int x = future.x; int y = future.y; while (x < 0 || x > ofGetWidth() || y < 0 || y > ofGetHeight() || this->pixels.getColor(x, y) == ofColor(0, 0)) { this->directions[i] = glm::normalize(glm::vec2(ofRandom(-1, 1), ofRandom(-1, 1))); future = this->logs[i].front() + this->directions[i] * 10; x = future.x; y = future.y; } } this->logs[i].push_front(future); while (this->logs[i].size() > this->len_of_tail) { this->logs[i].pop_back(); } } } //-------------------------------------------------------------- void ofApp::draw() { this->fbo.draw(0, 0); ofFill(); for (int i = 0; i < this->number_of_agent; i++) { for (int l = 0; l < this->logs[i].size() - 1; l++) { ofSetColor(this->colors[i], ofMap(l, 0, this->len_of_tail, 255, 64)); ofDrawLine(this->logs[i][l], this->logs[i][l + 1]); } ofSetColor(this->colors[i]); ofDrawCircle(this->logs[i].front(), 3); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |