[ Video ]
[ About ]
文字の上を移動するエージェント。ofFboに文字を描写、エージェントの座標情報でピクセルへアクセスして境界判定
Agents are moving on a character. I draw character to ofFbo. Pixels access, and boundary determination from coordinate of agent.
[ 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 |
#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; 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(0); ofSetColor(0); this->fbo.allocate(ofGetWidth(), ofGetHeight()); this->fbo.begin(); ofClear(0); ofTrueTypeFont font; font.loadFont("fonts/Kazesawa-bold.ttf", 300, true, true, true); string word = "CO"; font.drawString(word, ofGetWidth() * 0.5 - font.stringWidth(word) * 0.5, ofGetHeight() * 0.5 - font.stringHeight(word) * 0.1); word = "DE"; font.drawString(word, ofGetWidth() * 0.5 - font.stringWidth(word) * 0.5, ofGetHeight() * 0.5 + font.stringHeight(word) * 1.1); this->fbo.end(); this->fbo.readToPixels(this->pixels); this->number_of_agent = 150; this->len_of_tail = 18; 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, 230); this->colors.push_back(color); } } ofSetLineWidth(1.5); } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->number_of_agent; i++) { glm::vec2 future = this->logs[i].front() + this->directions[i] * 5; int x = future.x; int y = future.y; while (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() { for (int i = 0; i < this->number_of_agent; i++) { for (int l = 0; l < this->logs[i].size() - 1; l++) { ofSetColor(255, ofMap(l, 0, this->len_of_tail, 255, 0)); ofDrawLine(this->logs[i][l], this->logs[i][l + 1]); } ofSetColor(255); ofDrawCircle(this->logs[i].front(), 3); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |