[ Video ]
[ About ]
文字を文字で埋め尽くす。
コレ、前からやってみたかったんですが、ofTrueTypeFontでSizeを変更するのに、Fontファイルを読み込み直しが発生する方法しか見つからず、ファイルIO処理になってしまうので、沢山のサイズの文字を描写するのは処理が重くて断念していました。
ふと、「一つの文字サイズで作成したofTrueTypeFontから取得したPath情報を操作して大きさを変えればファイルIOを回避できる…?」と閃いて、書いてみた所、旨く行きましたb
Fontの相性なのかQだけが画面からはみ出してしまいますが、とりあえずはOK。
[ 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" 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) {}; char charactor; char prev_charactor; float font_size; ofTrueTypeFont font; vector<ofPolyline> outline; ofPoint word_size; ofFbo fbo; ofPixels pix; }; |
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" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(239); ofSetWindowTitle("Insta"); this->font_size = 600; this->font.loadFont("fonts/NotoSansCJKjp-Bold.otf", this->font_size, true, true, true); this->prev_charactor = '$'; this->fbo.allocate(ofGetWidth(), ofGetHeight()); } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(39); this->charactor = ((ofGetFrameNum() / 60) % 26) + 'A'; if (this->charactor != this->prev_charactor) { string word = string{ this->charactor }; vector<ofPath> chara_path = this->font.getStringAsPoints(word, true, false); this->outline = chara_path[0].getOutline(); this->word_size = ofPoint(this->font.stringWidth(word), this->font.stringHeight(word)); this->fbo.begin(); ofClear(0); ofSetColor(0); ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); this->font.drawString(word, -this->word_size.x * 0.5, this->word_size.y * 0.5); this->fbo.end(); this->fbo.readToPixels(this->pix); this->prev_charactor = this->charactor; } } //-------------------------------------------------------------- void ofApp::draw() { int number_of_char = 400; int char_count = 0; while (char_count < number_of_char) { int x = ofRandom(ofGetWidth()); int y = ofRandom(ofGetHeight()); ofColor pix_color = this->pix.getColor(x, y); if(pix_color != ofColor(0)){ continue; } float x_increase = ofMap(ofNoise(ofRandom(number_of_char), ofGetFrameNum() * 0.005), 0, 1, -20, 20); float y_increase = ofMap(ofNoise(ofRandom(number_of_char), ofGetFrameNum() * 0.005), 0, 1, -20, 20); x += x_increase; y += y_increase; ofPushMatrix(); ofTranslate(x, y); ofRotate(ofRandom(360) + x_increase + y_increase); float size = ofRandom(3, 50); ofColor color; color.setHsb(ofRandom(255), 239, 239); ofSetColor(color); float magnification = size * (1 / this->font_size); ofBeginShape(); for (int line_index = 0; line_index < outline.size(); line_index++) { if (line_index != 0) { ofNextContour(true); } vector<glm::vec3> vertices = outline[line_index].getVertices(); for (int i = 0; i < vertices.size(); i++) { ofPoint point(vertices[i].x, vertices[i].y, vertices[i].z); point = point - ofPoint(this->word_size.x * 0.5, this->word_size.y * -0.5); ofVertex(point * magnification); } } ofEndShape(true); ofPopMatrix(); char_count++; } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |