[work 96] Rotate shapes

[work 96] Rotate shapes

Movie

Source code

about

  • 3次元のランダムな位置にn 個の点を配置する
  • 各頂点を0 – (n-1)とし、順番に直線で結ぶ。
  • 図形Aの頂点0-1,1-2,2-3, …(n-1)-0の各線分を9:1に分割する点を頂点とする図形Bを描く
  • 図形Aから図形Bまでの変形の過程をアニメーション
  • これらを再帰的に実行
  • 最も短い線分が最小値に達したら、アニメーションを逆再生する

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 "Fractals.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:
    ofEasyCam cam;
    std::shared_ptr<Fractals> frac;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    frac = make_shared<Fractals>();
    frac->setup();
}

//--------------------------------------------------------------
void ofApp::update(){
    frac->update();
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    ofRotateYDeg(ofGetFrameNum());
    frac->display();
    cam.end();
}

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

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


struct myShape {
    std::vector<ofVec3f> shape;
    ofColor color;
    bool locus;
};

class  Fractals {
public:
    Fractals();
    ~Fractals();
    void setup();
    void update();
    void display();
    
private:
    std::shared_ptr<ofMesh> mesh, meshLocus;
    void setShape(std::shared_ptr<myShape> &r);
    void updateShape();
    
    std::vector<std::shared_ptr<myShape>> shapes;
    int index;
    bool reverse;
};
#endif /* Fractals_hpp */
#include "Fractals.hpp"


Fractals::Fractals()
{
    
}


Fractals::~Fractals()
{
    
}


void Fractals::setup()
{
    mesh = make_shared<ofMesh>();
    meshLocus = make_shared<ofMesh>();
    
    index = 0;
    
    auto ms = make_shared<myShape>();
    
    ofVec3f center = ofVec3f(0, 0, 0);
    int size = (int)ofRandom(6, 15);
    
    for (int i = 0; i < size; i++) {
        ofVec3f dir = ofVec3f(1, 0, 0);
        dir.rotate(ofRandom(0, 360), ofVec3f(1, 0, 0));
        dir.rotate(ofRandom(0, 360), ofVec3f(0, 1, 0));
        dir.rotate(ofRandom(0, 360), ofVec3f(0, 0, 1));
        dir *= ofRandom(200, 500);
        
        ms->shape.push_back(center + dir);
        
        std::cout << ms->shape.back() << std::endl;
    }
    ms->color = ofColor(0);
    ms->locus = false;
    
    setShape(ms);
}


void Fractals::update()
{
    updateShape();
}


void Fractals::display()
{
    meshLocus->drawWireframe();
    mesh->drawWireframe();
}


void Fractals::setShape(std::shared_ptr<myShape> &s)
{
    float size = (s->shape.at(0) - s->shape.at(1)).length();
    for (int i = 1; i < s->shape.size() - 1; i++) {
        size = std::min(size, (s->shape.at(i) - s->shape.at(i + 1)).length());
    }
    
    if (size > 30) {
        shapes.push_back(s);
        
        float step = 0.1;
        float stepNum = 5;
        ofVec3f p;
        
        for (int i = 1; i < stepNum; i++) {
            auto tmp_locus = make_shared<myShape>();
            float d = step / stepNum;
            
            p = s->shape.at(s->shape.size() - 1) * (d * i) + s->shape.at(0) * (1 - (d * i));
            tmp_locus->shape.push_back(p);
            for (int j = 0; j < (s->shape.size() - 1); j++) {
                p = s->shape.at(j) * (d * i) + s->shape.at(j + 1) * (1 - (d * i));
                tmp_locus->shape.push_back(p);
            }
            tmp_locus->color = ofColor(0);
            tmp_locus->locus = true;
            shapes.push_back(tmp_locus);
        }
        
        auto tmp_shape = make_shared<myShape>();
        p = s->shape.at(s->shape.size() - 1) * step + s->shape.at(0) * (1 - step);
        tmp_shape->shape.push_back(p);
        for (int j = 0; j < (s->shape.size() - 1); j++) {
            p = s->shape.at(j) * step + s->shape.at(j + 1) * (1 - step);
            tmp_shape->shape.push_back(p);
        }
        tmp_shape->color = ofColor(0);
        tmp_shape->locus = false;
        setShape(tmp_shape);
    }
}


void Fractals::updateShape()
{
    std::shared_ptr<ofMesh> m;
    
    if (index >= 0 && index < shapes.size()) {
        mesh->clear();
        meshLocus->clear();
        
        for (int i = 0; i <= index; i++) {
            if (shapes.at(i)->locus) {
                m = meshLocus;
                m->clear();
            } else {
                m = mesh;
            }
            
            m->setMode(OF_PRIMITIVE_LINE_LOOP);
            for (ofVec3f p : shapes.at(i)->shape) {
                m->addVertex(p);
                m->addColor(shapes.at(i)->color);
            }
        }
        if (reverse) {
            index--;
        } else {
            index++;
        }
    } else if (index < 0) {
        reverse = false;
        index = 0;
    } else {
        reverse = true;
        index = shapes.size() - 1;
    }
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofMesh
openframeworksofVec3f

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode