[work 32] Flow Field – Cherry Blossoms –

[work 32] Flow Field – Cherry Blossoms –

Movie

Source code

about

  • 読み込んだ画像のピクセルをパーティクル化。
  • 開始数秒後からFlowを適用して、画像が散っていくような効果を演出する。

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
main.cpp
ofApp.h
ofApp.cpp
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#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());
}
#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 "Particle.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::vector<unique_ptr<Particle>> particles;
unique_ptr<FlowField> ff;
ofTexture texture;
};
#pragma once #include "ofMain.h" #include "FlowField.hpp" #include "Particle.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::vector<unique_ptr<Particle>> particles; unique_ptr<FlowField> ff; ofTexture texture; };
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(0);
ofSetBackgroundAuto(true);
ff = make_unique<FlowField>(20);
ff->setup();
ofPixels pix;
ofImage img;
img.load("CherryBlossoms.jpg");
pix = img.getPixels();
pix.resize(ofGetWidth(), ofGetHeight());
for (int y = 0; y < ofGetHeight(); y++) {
for (int x = 0; x < ofGetWidth(); x++) {
int factor = 4;
ofColor color = pix.getColor(x, y);
particles.push_back(make_unique<Particle>(ofVec2f(x, y), color));
}
}
}
//--------------------------------------------------------------
void ofApp::update(){
if (ofGetFrameNum() > 90) {
ff->update();
for (unique_ptr<Particle> &p : particles) {
p->follow(ff);
p->update();
}
ofRemove(particles, Particle::shouldRemoveOffScreen);
}
}
//--------------------------------------------------------------
void ofApp::draw(){
for (unique_ptr<Particle> &p : particles) {
p->display();
}
}
#include "ofApp.h" ofApp::ofApp(){ } ofApp::~ofApp(){ } //-------------------------------------------------------------- void ofApp::setup(){ double fps = 30; ofSetFrameRate(fps); ofBackground(0); ofSetBackgroundAuto(true); ff = make_unique<FlowField>(20); ff->setup(); ofPixels pix; ofImage img; img.load("CherryBlossoms.jpg"); pix = img.getPixels(); pix.resize(ofGetWidth(), ofGetHeight()); for (int y = 0; y < ofGetHeight(); y++) { for (int x = 0; x < ofGetWidth(); x++) { int factor = 4; ofColor color = pix.getColor(x, y); particles.push_back(make_unique<Particle>(ofVec2f(x, y), color)); } } } //-------------------------------------------------------------- void ofApp::update(){ if (ofGetFrameNum() > 90) { ff->update(); for (unique_ptr<Particle> &p : particles) { p->follow(ff); p->update(); } ofRemove(particles, Particle::shouldRemoveOffScreen); } } //-------------------------------------------------------------- void ofApp::draw(){ for (unique_ptr<Particle> &p : particles) { p->display(); } }
#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 "Particle.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::vector<unique_ptr<Particle>> particles;
    unique_ptr<FlowField> ff;
    ofTexture texture;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(0);
    ofSetBackgroundAuto(true);
    
    ff = make_unique<FlowField>(20);
    ff->setup();
    
    ofPixels pix;
    ofImage img;
    img.load("CherryBlossoms.jpg");
    pix = img.getPixels();
    pix.resize(ofGetWidth(), ofGetHeight());
    
    for (int y = 0; y < ofGetHeight(); y++) {
        for (int x = 0; x < ofGetWidth(); x++) {
            int factor = 4;
            ofColor color = pix.getColor(x, y);
            particles.push_back(make_unique<Particle>(ofVec2f(x, y), color));
        }
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    if (ofGetFrameNum() > 90) {
        ff->update();
        
        for (unique_ptr<Particle> &p : particles) {
            p->follow(ff);
            p->update();
        }
        ofRemove(particles, Particle::shouldRemoveOffScreen);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for (unique_ptr<Particle> &p : particles) {
        p->display();
    }
}
FlowField.hpp
FlowField.cpp
Particle.hpp
Particle.cpp
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#ifndef FlowField_hpp
#define FlowField_hpp
#include <stdio.h>
#include "ofMain.h"
class FlowField {
public:
FlowField(int r);
~FlowField();
void setup();
void update();
void display();
ofVec2f lookup(ofVec2f pos);
private:
int cols, rows;
int resolution;
float xoff, yoff, zoff;
std::vector<std::vector<ofVec2f>> field;
};
#endif /* FlowField_hpp */
#ifndef FlowField_hpp #define FlowField_hpp #include <stdio.h> #include "ofMain.h" class FlowField { public: FlowField(int r); ~FlowField(); void setup(); void update(); void display(); ofVec2f lookup(ofVec2f pos); private: int cols, rows; int resolution; float xoff, yoff, zoff; std::vector<std::vector<ofVec2f>> field; }; #endif /* FlowField_hpp */
#include "FlowField.hpp"
FlowField::FlowField(int r)
{
resolution = r;
cols = ofGetWidth() / resolution;
rows = ofGetHeight() / resolution;
field.assign(cols, std::vector<ofVec2f>(rows, ofVec2f(0, 0)));
}
FlowField::~FlowField()
{
}
void FlowField::setup()
{
zoff = 0;
}
void FlowField::update()
{
xoff = 0;
for (int x = 0; x < cols; x++) {
yoff = 0;
for (int y = 0; y < rows; y++) {
float theta = ofMap(ofNoise(xoff, yoff, zoff), 0, 1, 0, TWO_PI);
ofVec2f dir = ofVec2f(std::cos(theta), std::sin(theta));
field[x][y] = dir.normalize();
yoff += 0.05;
}
xoff += 0.05;
}
zoff += 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 - (field[x][y] * len / 2);
ofVec2f t1 = c + (field[x][y] * len / 2);
ofSetColor(255, 255, 0);
ofDrawLine(t0.x, t0.y, t1.x, t1.y);
ofPopMatrix();
}
}
}
ofVec2f 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 field[col][row];
}
#include "FlowField.hpp" FlowField::FlowField(int r) { resolution = r; cols = ofGetWidth() / resolution; rows = ofGetHeight() / resolution; field.assign(cols, std::vector<ofVec2f>(rows, ofVec2f(0, 0))); } FlowField::~FlowField() { } void FlowField::setup() { zoff = 0; } void FlowField::update() { xoff = 0; for (int x = 0; x < cols; x++) { yoff = 0; for (int y = 0; y < rows; y++) { float theta = ofMap(ofNoise(xoff, yoff, zoff), 0, 1, 0, TWO_PI); ofVec2f dir = ofVec2f(std::cos(theta), std::sin(theta)); field[x][y] = dir.normalize(); yoff += 0.05; } xoff += 0.05; } zoff += 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 - (field[x][y] * len / 2); ofVec2f t1 = c + (field[x][y] * len / 2); ofSetColor(255, 255, 0); ofDrawLine(t0.x, t0.y, t1.x, t1.y); ofPopMatrix(); } } } ofVec2f 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 field[col][row]; }
#ifndef Particle_hpp
#define Particle_hpp
#include <stdio.h>
#include "ofMain.h"
#include "FlowField.hpp"
class Particle {
public:
Particle(ofVec2f l, ofColor c);
~Particle();
void update();
void display();
void follow(unique_ptr<FlowField> &flow);
void applyForce(ofVec2f force);
static bool shouldRemoveOffScreen(unique_ptr<Particle> &p);
private:
ofVec2f getLocation();
void setVelocity();
ofColor color;
ofVec2f location;
ofVec2f velocity;
ofVec2f acceleration;
float radius;
float maxforce;
float maxspeed;
float tx;
};
#endif /* Particle_hpp */
#ifndef Particle_hpp #define Particle_hpp #include <stdio.h> #include "ofMain.h" #include "FlowField.hpp" class Particle { public: Particle(ofVec2f l, ofColor c); ~Particle(); void update(); void display(); void follow(unique_ptr<FlowField> &flow); void applyForce(ofVec2f force); static bool shouldRemoveOffScreen(unique_ptr<Particle> &p); private: ofVec2f getLocation(); void setVelocity(); ofColor color; ofVec2f location; ofVec2f velocity; ofVec2f acceleration; float radius; float maxforce; float maxspeed; float tx; }; #endif /* Particle_hpp */
#include "Particle.hpp"
bool Particle::shouldRemoveOffScreen(unique_ptr<Particle> &p)
{
return !ofRectangle(0, 0, ofGetWidth(), ofGetHeight()).inside(p->getLocation());
}
Particle::Particle(ofVec2f l, ofColor c)
{
color = c;
maxspeed = 3;
maxforce = 0.3;
acceleration = ofVec2f(0, 0);
velocity = ofVec2f(0, 0);
location = l;
radius = 1;
tx = 0;
}
Particle::~Particle()
{
}
void Particle::update()
{
location += velocity;
acceleration *= 0;
}
void Particle::display()
{
ofPushMatrix();
ofFill();
ofSetColor(color);
ofTranslate(location.x, location.y);
ofDrawCircle(0, 0, radius);
ofPopMatrix();
}
void Particle::follow(unique_ptr<FlowField> &flow)
{
ofVec2f desired = flow->lookup(location);
desired *= maxspeed;
ofVec2f steer = desired - velocity;
steer.limit(maxforce);
applyForce(steer);
}
void Particle::applyForce(ofVec2f force)
{
acceleration += force;
setVelocity();
}
ofVec2f Particle::getLocation()
{
return location;
}
void Particle::setVelocity()
{
velocity += acceleration;
velocity.limit(maxspeed);
}
#include "Particle.hpp" bool Particle::shouldRemoveOffScreen(unique_ptr<Particle> &p) { return !ofRectangle(0, 0, ofGetWidth(), ofGetHeight()).inside(p->getLocation()); } Particle::Particle(ofVec2f l, ofColor c) { color = c; maxspeed = 3; maxforce = 0.3; acceleration = ofVec2f(0, 0); velocity = ofVec2f(0, 0); location = l; radius = 1; tx = 0; } Particle::~Particle() { } void Particle::update() { location += velocity; acceleration *= 0; } void Particle::display() { ofPushMatrix(); ofFill(); ofSetColor(color); ofTranslate(location.x, location.y); ofDrawCircle(0, 0, radius); ofPopMatrix(); } void Particle::follow(unique_ptr<FlowField> &flow) { ofVec2f desired = flow->lookup(location); desired *= maxspeed; ofVec2f steer = desired - velocity; steer.limit(maxforce); applyForce(steer); } void Particle::applyForce(ofVec2f force) { acceleration += force; setVelocity(); } ofVec2f Particle::getLocation() { return location; } void Particle::setVelocity() { velocity += acceleration; velocity.limit(maxspeed); }
#ifndef FlowField_hpp
#define FlowField_hpp

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


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


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


FlowField::~FlowField()
{
    
}


void FlowField::setup()
{
    zoff = 0;
}


void FlowField::update()
{
    xoff = 0;
    for (int x = 0; x < cols; x++) {
        yoff = 0;
        for (int y = 0; y < rows; y++) {
            float theta = ofMap(ofNoise(xoff, yoff, zoff), 0, 1, 0, TWO_PI);
            ofVec2f dir = ofVec2f(std::cos(theta), std::sin(theta));
            field[x][y] = dir.normalize();
            yoff += 0.05;
        }
        xoff += 0.05;
    }
    zoff += 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 - (field[x][y] * len / 2);
            ofVec2f t1 = c + (field[x][y] * len / 2);
            ofSetColor(255, 255, 0);
            ofDrawLine(t0.x, t0.y, t1.x, t1.y);
            ofPopMatrix();
        }
    }
}


ofVec2f 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 field[col][row];
}
#ifndef Particle_hpp
#define Particle_hpp

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


class Particle {
public:
    Particle(ofVec2f l, ofColor c);
    ~Particle();
    void update();
    void display();
    
    void follow(unique_ptr<FlowField> &flow);
    void applyForce(ofVec2f force);
    
    static bool shouldRemoveOffScreen(unique_ptr<Particle> &p);
private:
    ofVec2f getLocation();
    void setVelocity();
    
    ofColor color;
    ofVec2f location;
    ofVec2f velocity;
    ofVec2f acceleration;
    float radius;
    float maxforce;
    float maxspeed;
    float tx;
};
#endif /* Particle_hpp */
#include "Particle.hpp"


bool Particle::shouldRemoveOffScreen(unique_ptr<Particle> &p)
{
    return !ofRectangle(0, 0, ofGetWidth(), ofGetHeight()).inside(p->getLocation());
}

Particle::Particle(ofVec2f l, ofColor c)
{
    color = c;
    maxspeed = 3;
    maxforce = 0.3;
    acceleration = ofVec2f(0, 0);
    velocity = ofVec2f(0, 0);
    location = l;
    radius = 1;
    tx = 0;
}


Particle::~Particle()
{
    
}


void Particle::update()
{
    location += velocity;
    acceleration *= 0;
}


void Particle::display()
{
    ofPushMatrix();
    ofFill();
    ofSetColor(color);
    ofTranslate(location.x, location.y);
    ofDrawCircle(0, 0, radius);
    ofPopMatrix();
}


void Particle::follow(unique_ptr<FlowField> &flow)
{
    ofVec2f desired = flow->lookup(location);
    desired *= maxspeed;
    
    ofVec2f steer = desired - velocity;
    steer.limit(maxforce);
    applyForce(steer);
}


void Particle::applyForce(ofVec2f force)
{
    acceleration += force;
    setVelocity();
}


ofVec2f Particle::getLocation()
{
    return location;
}


void Particle::setVelocity()
{
    velocity += acceleration;
    velocity.limit(maxspeed);
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofPixels
openframeworksofTexture
openframeworksofImage

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode