[work 80] Wind「風」

[work 80] Wind「風」

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 "FlowField.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:
    ofFbo fboDraw;
    std::shared_ptr<FlowField> ff;
    std::shared_ptr<ofTrueTypeFont> font;
    ofPixels pix;
    std::vector<ofVec2f> points;
    std::vector<ofColor> colors;
    ofVec2f offset;
    bool flow;
    
    std::vector<ofColor> colSet = {{217,239,151},{8,96,168},{255,242,124},
                                    {81,86,165},{37,113,120},{105,100,173},
                                    {255,242,174},{254,242,236},{230,244,127},
                                    {230,240,103},{204,235,209},{241,225,209}};
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    fboDraw.allocate(ofGetWidth(), ofGetHeight(), GL_RGBA, 4);
    
    ff = make_shared<FlowField>(10);
    ff->setup();
    
    ofTrueTypeFontSettings settings("font/HiraginoProNW4.ttc", 200);
    settings.addRanges(ofAlphabet::Japanese);
    settings.contours = true;
    font = make_shared<ofTrueTypeFont>();
    font->load(settings);
    
    ofTexture tex = font->getStringTexture("風");
    ofFbo fbo;
    
    // For tex to pix
    fbo.allocate(tex.getWidth(), tex.getHeight());
    fbo.begin();
    ofClear(0);
    tex.draw(0, 0);
    fbo.end();
    
    fbo.readToPixels(pix);

    
    offset = ofVec2f((ofGetWidth() / 2) - (pix.getWidth() / 2), (ofGetHeight() / 2) - (pix.getHeight() / 2));
    for (int i = 0; i < pix.getWidth(); i++) {
        for (int j = 0; j < pix.getHeight(); j++) {
            if (pix.getColor(i, j) == ofColor(255)) {
                points.push_back(offset + ofVec2f(i, j));
                int index = (int)ofRandom(colSet.size());
                colors.push_back(colSet.at(index));
            }
        }
    }
    
    flow = false;
}

//--------------------------------------------------------------
void ofApp::update(){
    ff->update();
    
    if (flow) {
        for (int i = 0; i < points.size(); i++) {
            Flow f = ff->lookup(points.at(i));
            float speed = ofMap(f.force, -1, 1, 1, 5);
            points.at(i) += f.direction * speed;
            
            if ((int)ofRandom(3) == 0) {
                colors.at(i).a -= 1;
            }
        }
    }
    
    fboDraw.begin();
    ofClear(0);
    for (int i = 0; i < points.size(); i++) {
        ofSetColor(colors.at(i));
        ofDrawCircle(points.at(i), 1);
    }
    fboDraw.end();
}

//--------------------------------------------------------------
void ofApp::draw(){
    fboDraw.draw(0, 0);
}

//--------------------------------------------------------------
void ofApp::keyPressed(int key){
    if (key == 's') {
        ofImage img;
        img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
        img.save("screenshot.png");
    } else if (key == 'f') {
        flow = !flow;
    }
}
#ifndef FlowField_hpp
#define FlowField_hpp

#include <stdio.h>
#include "ofMain.h"

struct Flow {
    ofVec2f direction;
    float force;
};

class FlowField {
public:
    FlowField(int r);
    ~FlowField();
    void setup();
    void update();
    void display();
    Flow lookup(ofVec2f pos);
private:
    int cols, rows;
    int resolution;
    float xoff, yoff, zoff0, zoff1;
    std::vector<std::vector<Flow>> flow;
};
#endif /* FlowField_hpp */
#include "FlowField.hpp"


FlowField::FlowField(int r)
{
    resolution = r;
    cols = ofGetWidth() / resolution;
    rows = ofGetHeight() / resolution;
    Flow f;
    f.direction = ofVec2f(0, 0);
    f.force = 0;
    flow.assign(cols, std::vector<Flow>(rows, f));
}


FlowField::~FlowField()
{
    
}


void FlowField::setup()
{
    zoff0 = ofRandom(0, 1000);
    zoff1 = ofRandom(1000, 2000);
}


void FlowField::update()
{
    xoff = 0;
    for (int x = 0; x < cols; x++) {
        yoff = 0;
        for (int y = 0; y < rows; y++) {
            float theta = ofMap(ofSignedNoise(xoff, yoff, zoff0), -1, 1, -PI, PI);
            ofVec2f dir = ofVec2f(std::cos(theta), std::sin(theta));
            Flow f;
            f.direction = dir.normalize();
            f.force = ofSignedNoise(xoff, yoff, zoff1);
            flow[x][y] = f;
            yoff += 0.05;
        }
        xoff += 0.05;
    }
    zoff0 += 0.01;
}


void FlowField::display()
{
    for (int x = 0; x < cols; x++) {
        for (int y = 0; y < rows; y++) {
            ofPushMatrix();
            ofTranslate(x * resolution, y * resolution);
            ofNoFill();
            ofSetColor(255);
            ofDrawRectangle(0, 0, resolution, resolution);
            
            ofVec2f c = ofVec2f(resolution / 2, resolution / 2);
            int len = resolution * 3 / 4;
            ofVec2f t0 = c - (flow[x][y].direction * len / 2);
            ofVec2f t1 = c + (flow[x][y].direction * len / 2);
            ofSetColor(255, 255, 0);
            ofDrawLine(t0.x, t0.y, t1.x, t1.y);
            ofPopMatrix();
        }
    }
}


Flow FlowField::lookup(ofVec2f pos)
{
    int col = (int)ofClamp(pos.x / resolution, 0, cols - 1);
    int row = (int)ofClamp(pos.y / resolution, 0, rows - 1);
    return flow[col][row];
}

Link to the reference page

ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。

categoryAPI/Lib
openframeworksofTrueTypeFontSettings
openframeworksofTrueTypeFont
openframeworksofTexture
openframeworksofFbo
openframeworksofPixels

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode