[work 79] Chicken「鶏」 or the egg「卵」
Movie
Source code
about
- 鶏が先か卵が先か
- 漢字をモーフィング
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h" #include "ofApp.h" //================================ int main( ){ // 4K:4096x2160 // 2K:2048x1080 // FullHD:1920x1080 // HD:1440x1080 // HD720p:1280x720 // DVD:720x480 // setup the GL context ofSetupOpenGL(1280, 720, OF_WINDOW); // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp( new ofApp()); }
#pragma once #include "ofMain.h" #include "Morphing.hpp" class ofApp : public ofBaseApp{ public: ofApp(); ~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); private: std::shared_ptr<Morphing> morph; };
#include "ofApp.h" ofApp::ofApp(){ } ofApp::~ofApp(){ } //-------------------------------------------------------------- void ofApp::setup(){ double fps = 30; ofSetFrameRate(fps); ofBackground(255); ofSetBackgroundAuto(true); ofSetVerticalSync(true); morph = make_shared<Morphing>(); morph->setup(); } //-------------------------------------------------------------- void ofApp::update(){ morph->update(); } //-------------------------------------------------------------- void ofApp::draw(){ morph->display(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if (key == 's') { ofImage img; img.grabScreen(0, 0, ofGetWidth(), ofGetHeight()); img.save("screenshot.png"); } }
#ifndef Morphing_hpp #define Morphing_hpp #include <stdio.h> #include "ofMain.h" #include "ofxEasing.h" class Morphing { public: Morphing(); ~Morphing(); void setup(); void update(); void display(); private: void setTarget(std::string str); void arrangeVec(); std::shared_ptr<ofTrueTypeFont> font; std::vector<std::string> morphStr; int index; bool morphUpdate; std::vector<glm::vec3> position; float amp; std::vector<std::vector<glm::vec3>> drawPoint; std::vector<std::vector<glm::vec3>> target; std::vector<std::vector<glm::vec3>> org; int count; ofRectangle boundingBox; ofColor color; }; #endif /* Morphing_hpp */
#include "Morphing.hpp" Morphing::Morphing() { } Morphing::~Morphing() { } void Morphing::setup() { ofTrueTypeFontSettings settings("font/HiraginoProNW4.ttc", 60); settings.addRanges(ofAlphabet::Japanese); settings.contours = true; font = make_shared<ofTrueTypeFont>(); font->load(settings); amp = 5; count = 1; index = 0; morphUpdate = true; morphStr.push_back("鶏"); morphStr.push_back("鶏"); morphStr.push_back("卵"); morphStr.push_back("卵"); color = ofColor(0, 0, 0, 100); setTarget(morphStr.at(index)); position.push_back(glm::vec3(250, 500, 0)); position.push_back(glm::vec3(250, 500, 0)); position.push_back(glm::vec3(600, 500, 0)); position.push_back(glm::vec3(600, 500, 0)); for (int i = 0; i < target.size(); i++) { std::vector<glm::vec3> tmp; tmp.assign(target.at(i).size(), glm::vec3(0, 0, 0)); org.push_back(tmp); } } void Morphing::update() { int maxFrame = (int)ofGetTargetFrameRate(); if (morphUpdate) { if (count != 0) { drawPoint.clear(); for (int i = 0; i < target.size(); i++) { std::vector<glm::vec3> tmp; for (int j = 0; j < target.at(i).size(); j++) { float step = ofxeasing::map(count, 0, maxFrame - 1, 0, 1, ofxeasing::linear::easeOut); glm::vec3 pos; if (index == 0) { pos = position.at(morphStr.size() - 1) + (position.at(0) - position.at(morphStr.size() - 1)) * step; } else { pos = position.at(index - 1) + (position.at(index) - position.at(index - 1)) * step; } tmp.push_back(pos + org.at(i).at(j) + (target.at(i).at(j) - org.at(i).at(j)) * step); } drawPoint.push_back(tmp); } } else { if (index < morphStr.size()) { org.clear(); org = target; setTarget(morphStr.at(index)); arrangeVec(); } else { morphUpdate = false; } } count = (count + 1) % maxFrame; if (count == 0) { index = (index + 1) % morphStr.size(); } } } void Morphing::display() { ofSetPolyMode(OF_POLY_WINDING_NONZERO); ofPushMatrix(); ofSetColor(color); ofNoFill(); for (std::vector<glm::vec3> p : drawPoint) { ofBeginShape(); ofVertices(p); ofEndShape(); } ofPopMatrix(); } void Morphing::setTarget(std::string str) { std::vector<ofPath> paths = font->getStringAsPoints(str, true, false); float minX = FLT_MIN; float maxY = 0; float maxW = 0; float maxH = 0; target.clear(); for (ofPath path : paths) { std::vector<ofPolyline> lines = path.getOutline(); minX = std::min(minX, lines.at(0).getBoundingBox().x * amp); maxY = std::max(maxY, lines.at(0).getBoundingBox().y * amp); maxW = std::max(maxW, lines.at(0).getBoundingBox().width * amp); maxH = std::max(maxH, lines.at(0).getBoundingBox().height * amp); for (ofPolyline pl : lines) { std::vector<glm::vec3> points = pl.getVertices(); for (glm::vec3 &p : points) { p *= amp; } target.push_back(points); } } boundingBox.x = minX; boundingBox.y = maxY; boundingBox.width = maxW; boundingBox.height = maxH; } void Morphing::arrangeVec() { glm::vec3 c = boundingBox.getCenter(); c.y *= -1; // resize if (org.size() > target.size()) { // thin out while (org.size() != target.size()) { org.pop_back(); } } else { // packing while (org.size() != target.size()) { std::vector<glm::vec3> tmp(1, c); org.push_back(tmp); } } for (int i = 0; i < org.size(); i++) { if (org.at(i).size() > target.at(i).size()) { // thin out while (org.at(i).size() != target.at(i).size()) { org.at(i).pop_back(); } } else { // packing while (org.at(i).size() != target.at(i).size()) { org.at(i).push_back(c); } } } }
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
category | API/Lib |
openframeworks | ofTrueTypeFontSettings |
openframeworks | ofTrueTypeFont |
openframeworks | ofPath |
openframeworks | ofPolyLine |
openframeworks | ofxeasing map |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode