[work 87] Flow Cubes
![[work 87] Flow Cubes](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/08/outFrameImg0090.png?fit=1280%2C720&ssl=1)
Movie
Source code
about
- FlowFieldに従ってCubeを動かす
- Cubeの上下動に合わせて輝度を変更する
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 "Cube.hpp"
#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:
std::vector<std::shared_ptr<Cube>> cube;
std::shared_ptr<FlowField> ff;
float size;
int col, row;
ofCamera cam;
int camYaw;
};
#include "ofApp.h"
ofApp::ofApp(){
}
ofApp::~ofApp(){
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 30;
ofSetFrameRate(fps);
ofBackground(0);
ofSetBackgroundAuto(true);
ofSetVerticalSync(true);
ofEnableDepthTest();
ofEnableSmoothing();
size = 80;
row = ofGetWidth() / size;
col = ofGetHeight() / size;
ff = make_shared<FlowField>(size);
ff->setup();
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cube.push_back(make_shared<Cube>(ofVec3f(i, 0, j), size));
cube.back()->setup();
}
}
camYaw = 0;
}
//--------------------------------------------------------------
void ofApp::update(){
float rad = ofDegToRad(camYaw) / 5;
cam.setGlobalPosition(glm::vec3(800 * std::cos(rad), 800, 800 * std::sin(rad)));
cam.lookAt(glm::vec3(0, 0, 0));
camYaw++;
int count = ofGetFrameNum() % (int)ofGetTargetFrameRate();
if (count == 0) {
ff->update();
for (std::shared_ptr<Cube> c : cube) {
c->follow(ff);
}
}
for (std::shared_ptr<Cube> c : cube) {
c->update();
}
}
//--------------------------------------------------------------
void ofApp::draw(){
cam.begin();
ofPushMatrix();
ofTranslate(glm::vec3((-(row / 2) * size), 0, -((col / 2) * size)));
for (std::shared_ptr<Cube> c : cube) {
c->display();
}
ofPopMatrix();
cam.end();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == 's') {
ofImage img;
img.grabScreen(0, 0, ofGetWidth(), ofGetHeight());
img.save("screenshot.png");
}
}
#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 Cube_hpp
#define Cube_hpp
#include <stdio.h>
#include "ofMain.h"
#include "ofxEasing.h"
#include "FlowField.hpp"
enum CUBE_MOVE {
CUBE_F = 0, // forward
CUBE_B, // back
CUBE_R, // right
CUBE_L, // left
CUBE_U, // up
CUBE_D, // down
CUBE_P, // pause
CUBE_MAX
};
class Cube {
public:
Cube();
Cube(ofVec3f p, float s);
~Cube();
void setup();
void update();
void display();
void setTarget(ofVec3f t);
void follow(std::shared_ptr<FlowField> f);
private:
void move(CUBE_MOVE dir);
void randMove();
void targetMove();
ofVec3f center;
ofVec3f position;
ofVec3f transToRAxis;
ofVec3f moveTo;
ofVec3f target;
float size;
ofColor colFrame;
ofColor colFace;
bool pause;
int updateCount;
ofVec3f rAxis;
float rAngle;
};
#endif /* Cube_hpp */
#include "Cube.hpp"
Cube::Cube()
{
size = 150;
position = ofVec3f(0, 0, 0);
}
Cube::Cube(ofVec3f p, float s)
{
size = s;
position = p;
}
Cube::~Cube()
{
}
void Cube::setup()
{
center = ofVec3f(0, 0, 0);
moveTo = ofVec3f(0, 0, 0);
rAxis = ofVec3f(0, 0, 0);
target = ofVec3f(0, 0, 0);
rAngle = 0;
colFrame = ofColor(255);
colFace = ofColor(255);
pause = true;
updateCount = 0;
}
void Cube::update()
{
int loopMax = (int)ofGetTargetFrameRate();
int loopCount = updateCount % loopMax;
updateCount++;
if (loopCount == 0) {
rAngle = 0;
position += moveTo;
targetMove();
} else {
if (pause == false) {
rAngle = (int)ofxeasing::map(loopCount, 0, loopMax - 1, 0, 90, ofxeasing::quart::easeInOut);
}
}
}
void Cube::display()
{
ofPushMatrix();
ofTranslate(position * size);
ofTranslate(transToRAxis * -1);
ofRotateDeg(rAngle, rAxis.x, rAxis.y, rAxis.z);
ofTranslate(transToRAxis);
ofSetColor(colFace);
ofFill();
ofDrawBox(center, size * 0.8);
ofSetColor(colFrame);
ofNoFill();
ofDrawBox(center, size);
ofPopMatrix();
}
void Cube::setTarget(ofVec3f t)
{
target = t;
}
void Cube::follow(std::shared_ptr<FlowField> f)
{
if (pause) {
ofVec2f p;
p.x = position.x * size;
p.y = position.z * size;
Flow flow = f->lookup(p);
float angle = flow.direction.angleRad(ofVec2f(1, 0));
ofVec3f t = position;
t.y = (int)ofMap(std::sin(angle), -1, 1, -5, 5);
colFace.setBrightness(ofMap(t.y, -5, 5, 0, 255));
setTarget(t);
pause = false;
}
}
void Cube::move(CUBE_MOVE dir)
{
pause = false;
if (dir == CUBE_F) { // Forward(Z-axis(+))
rAxis = ofVec3f(1, 0, 0);
transToRAxis = ofVec3f(0, 1, -1) * size * 0.5;
moveTo = ofVec3f(0, 0, 1);
} else if (dir == CUBE_B) { // Back(Z-axis(-))
rAxis = ofVec3f(-1, 0, 0);
transToRAxis = ofVec3f(0, 1, 1) * size * 0.5;
moveTo = ofVec3f(0, 0, -1);
} else if (dir == CUBE_R) { // Right(X-axis(-))
rAxis = ofVec3f(0, 0, 1);
transToRAxis = ofVec3f(1, 1, 0) * size * 0.5;
moveTo = ofVec3f(-1, 0, 0);
} else if (dir == CUBE_L) { // Left(X-axis(+))
rAxis = ofVec3f(0, 0, -1);
transToRAxis = ofVec3f(-1, 1, 0) * size * 0.5;
moveTo = ofVec3f(1, 0, 0);
} else if (dir == CUBE_U) {
rAxis = ofVec3f(1, 0, 0);
transToRAxis = ofVec3f(0, -1, -1) * size * 0.5;
moveTo = ofVec3f(0, 1, 0);
} else if (dir == CUBE_D) {
rAxis = ofVec3f(-1, 0, 0);
transToRAxis = ofVec3f(0, 1, -1) * size * 0.5;
moveTo = ofVec3f(0, -1, 0);
} else { // Pause
pause = true;
rAxis = ofVec3f(0, 0, 0);
transToRAxis = ofVec3f(0, 0, 0);
moveTo = ofVec3f(0, 0, 0);
}
}
void Cube::randMove()
{
int dir = (int)ofRandom(CUBE_MAX);
move((CUBE_MOVE)dir);
}
void Cube::targetMove()
{
ofVec3f t;
t.x = (int)target.x - (int)position.x;
t.y = (int)target.y - (int)position.y;
t.z = (int)target.z - (int)position.z;
if (t != ofVec3f(0, 0, 0)) {
float px = std::abs(t.x) / (std::abs(t.x) + std::abs(t.y) + std::abs(t.z));
float py = std::abs(t.y) / (std::abs(t.x) + std::abs(t.y) + std::abs(t.z));
float pz = std::abs(t.z) / (std::abs(t.x) + std::abs(t.y) + std::abs(t.z));
float p = ofRandom(1);
if (p < px) {
if (t.x > 0) {
move(CUBE_L);
} else {
move(CUBE_R);
}
} else if ((p >= px) && (p < px + py)) {
if (t.y > 0) {
move(CUBE_U);
} else {
move(CUBE_D);
}
} else {
if (t.z > 0) {
move(CUBE_F);
} else {
move(CUBE_B);
}
}
} else {
move(CUBE_P);
}
}
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API/Lib |
| openframeworks | ofCamera |
| openframeworks | ofTranslate |
| openframeworks | ofRotateDeg |
| openframeworks | ofEnableDepthTest |
| openframeworks | ofxeasing map |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode