[work 51] Rotate Stars

[work 51] Rotate Stars

Movie

Source code

about

  • 星Aの凸部分の頂点を時計回りに番号を0,3,1,4,2とする。
  • 0-1,1-2,2-3,3-4,4-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:
    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(){
    frac->display();
}
#ifndef Fractals_hpp
#define Fractals_hpp

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


struct myShape {
    std::array<ofVec2f, 5> shape;
    bool locus;
};

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


Fractals::Fractals()
{
    
}


Fractals::~Fractals()
{
    
}


void Fractals::setup()
{
    fbo.allocate(ofGetWidth(), ofGetHeight());
    fbolocus.allocate(ofGetWidth(), ofGetHeight());
    
    index = 0;
    
    myShape ms;
    ofVec2f center = ofVec2f(ofGetWidth() / 2, ofGetHeight() / 2);
    float radius = 350;
    ofVec2f point = ofVec2f(0, -1) * radius;
    ms.shape.at(0) = center + point;
    ms.shape.at(1) = center + point.rotate(144);
    ms.shape.at(2) = center + point.rotate(144);
    ms.shape.at(3) = center + point.rotate(144);
    ms.shape.at(4) = center + point.rotate(144);
    ms.locus = false;
    
    setShape(ms);
}


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


void Fractals::display()
{
    fbolocus.draw(0, 0);
    fbo.draw(0, 0);
}


void Fractals::setShape(myShape s)
{
    float size = (s.shape.at(0) - s.shape.at(1)).length();
    
    if (size > 10) {
        shapes.push_back(s);
        
        float step = 0.1;
        float stepNum = 10;
        myShape tmp;
        for (int i = 1; i < stepNum; i++) {
            float d = step / stepNum;
            tmp.shape.at(0) = s.shape.at(4) * (d * i) + s.shape.at(0) * (1 - (d * i));
            tmp.shape.at(1) = s.shape.at(0) * (d * i) + s.shape.at(1) * (1 - (d * i));
            tmp.shape.at(2) = s.shape.at(1) * (d * i) + s.shape.at(2) * (1 - (d * i));
            tmp.shape.at(3) = s.shape.at(2) * (d * i) + s.shape.at(3) * (1 - (d * i));
            tmp.shape.at(4) = s.shape.at(3) * (d * i) + s.shape.at(4) * (1 - (d * i));
            tmp.locus = true;
            shapes.push_back(tmp);
        }
        
        
        tmp.shape.at(0) = s.shape.at(4) * step + s.shape.at(0) * (1 - step);
        tmp.shape.at(1) = s.shape.at(0) * step + s.shape.at(1) * (1 - step);
        tmp.shape.at(2) = s.shape.at(1) * step + s.shape.at(2) * (1 - step);
        tmp.shape.at(3) = s.shape.at(2) * step + s.shape.at(3) * (1 - step);
        tmp.shape.at(4) = s.shape.at(3) * step + s.shape.at(4) * (1 - step);
        tmp.locus = false;
        setShape(tmp);
    }
}


void Fractals::updateShape()
{
    if (index >= 0 && index < shapes.size()) {
        fbolocus.begin();
        ofClear(0);
        fbolocus.end();
        fbo.begin();
        ofClear(0);
        fbo.end();
        
        for (int i = 0; i <= index; i++) {
            if (shapes.at(i).locus) {
                fbolocus.begin();
                ofClear(0);
            } else {
                fbo.begin();
            }
            
            ofEnableAntiAliasing();
            ofSetPolyMode(OF_POLY_WINDING_NONZERO);
            ofSetColor(0);
            ofNoFill();
            ofSetLineWidth(1);
            ofBeginShape();
            ofVertex(shapes.at(i).shape.at(0));
            ofVertex(shapes.at(i).shape.at(1));
            ofVertex(shapes.at(i).shape.at(2));
            ofVertex(shapes.at(i).shape.at(3));
            ofVertex(shapes.at(i).shape.at(4));
            ofEndShape();
            
            if (shapes.at(i).locus) {
                fbolocus.end();
            } else {
                fbo.end();
            }
        }
        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
openframeworksofVertex
openframeworksofFbo

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode