[work 56] Flow field – sector –

[work 56] Flow field – sector –

Movie

Source code

about

  • Flow Field上に扇形を描く
  • Flowに従って透明度と扇の中心角度を変化させる

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"
#include "Shape.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:
    shared_ptr<FlowField> ff;
    int resolution;
    
    std::vector<shared_ptr<Shape>> shapes;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    resolution = 80;
    ff = make_shared<FlowField>(resolution);
    ff->setup();
    
    ofVec2f point;
    int cols = ofGetWidth() / resolution;
    int rows = ofGetHeight() / resolution;
    for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
            point.x = i * resolution + (resolution / 2);
            point.y = j * resolution + (resolution / 2);
            shapes.push_back(make_shared<Shape>(point, resolution, resolution));
        }
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    ff->update();
    
    for (shared_ptr<Shape> &s: shapes) {
        s->follow(ff);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for (shared_ptr<Shape> &s: shapes) {
        s->display();
    }
}
#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];
}
#ifndef Shape_hpp
#define Shape_hpp


#include <stdio.h>
#include "ofMain.h"
#include "FlowField.hpp"
class Shape {
public:
    Shape(ofVec2f pos, float w, float h);
    ~Shape();
    void display();
    void follow(shared_ptr<FlowField> &flowfield);
    
private:
    ofColor color1, color2;
    ofVec2f location;
    float angle;
    float force;
    float width;
    float height;
};
#endif /* Shape_hpp */
#include "Shape.hpp"



Shape::Shape(ofVec2f pos, float w, float h)
{
    color1 = ofColor(0);
    color2 = ofColor(128);
    
    location = pos;
    angle = 0;
    force = 0;
    width = w;
    height = h;
}


Shape::~Shape()
{
    
}


void Shape::display()
{
    ofPushMatrix();
    ofTranslate(location.x, location.y);
    
    ofSetColor(color1, ofMap(force, -1, 1, 0, 255));
    ofFill();
    
    ofBeginShape();
    ofSetPolyMode(OF_POLY_WINDING_NONZERO);
    
    ofVec2f orgDir = (ofVec2f(1, 0) * (width / 2)) * 0.85;
    int step = (int)(angle / std::abs(angle));
    ofVertex(0, 0);
    for (int i = 0; i < std::abs(angle); i++) {
        ofVertex(orgDir);
        orgDir.rotate(step);
    }
    ofEndShape();
    ofPopMatrix();
    
    ofPushMatrix();
    ofTranslate(location.x, location.y);
    ofSetRectMode(OF_RECTMODE_CENTER);
    ofNoFill();
    ofSetColor(color2);
    ofSetLineWidth(3);
    ofDrawCircle(0, 0, width / 2);
    ofPopMatrix();
}


void Shape::follow(shared_ptr<FlowField> &flowfield)
{
    Flow f = flowfield->lookup(location);
    ofVec2f dir = f.direction;
    ofVec2f org = ofVec2f(1, 0);
    angle = org.angle(dir);
    force = f.force;
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofSignedNoise
openframeworksofVec2f
c++std::vector

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode