[work 67] Trick

[work 67] Trick

Movie

Source code

about

トリックアート

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 "Trick.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<Trick> trick;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

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

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

//--------------------------------------------------------------
void ofApp::draw(){
    trick->display();
}

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

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

class Trick {
public:
    Trick();
    ~Trick();
    void setup();
    void update();
    void display();
    
private:
    void drawBox(ofVec2f c);
    void drawLastBox(ofVec2f c);
    std::vector<ofVec2f> box;
    ofVec2f pos;
    int step;
    ofFbo fbo;
    
};
#endif /* Trick_hpp */
#include "Trick.hpp"


Trick::Trick()
{
    
}


Trick::~Trick()
{
    
}


void Trick::setup()
{
    ofVec2f d = ofVec2f(0, 1);
    float len = 50;
    int num = 6;
    
    box.push_back(ofVec2f(0, 0));
    for (int i = 0; i < num; i++) {
        ofVec2f org = d;
        box.push_back(org.rotate((360 / num) * i) * len);
    }
    step = 0;
    pos = ofVec2f(ofGetWidth() / 2 + 100, 250);
    
    fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGBA, 4);
}


void Trick::update()
{
    if (ofGetFrameNum() % 30 == 0) {
        fbo.begin();
        if (step == 0) {
            drawBox(pos);
        } else if (step <= 4) {
            pos = pos + box.at(2);
            drawBox(pos);
        } else if (step <= 8) {
            pos = pos + box.at(6);
            drawBox(pos);
        } else if (step <= 10) {
            pos = pos + box.at(4);
            drawBox(pos);
        } else if (step == 11) {
            drawLastBox(pos + box.at(4));
        } else {
            
        }
        fbo.end();
    
        step++;
    }
}


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


void Trick::drawBox(ofVec2f c)
{
    ofFill();
    ofSetColor(255, 242, 124);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(1));
    ofVertex(c + box.at(2));
    ofVertex(c + box.at(3));
    ofEndShape();
    
    ofSetColor(217, 239, 151);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(3));
    ofVertex(c + box.at(4));
    ofVertex(c + box.at(5));
    ofEndShape();
    
    ofSetColor(8, 96, 168);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(5));
    ofVertex(c + box.at(6));
    ofVertex(c + box.at(1));
    ofEndShape();
    
    
    ofNoFill();
    ofSetLineWidth(3);
    ofSetColor(0);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(1));
    ofVertex(c + box.at(2));
    ofVertex(c + box.at(3));
    ofEndShape();
    
    ofSetColor(0);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(3));
    ofVertex(c + box.at(4));
    ofVertex(c + box.at(5));
    ofEndShape();
    
    ofSetColor(0);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(5));
    ofVertex(c + box.at(6));
    ofVertex(c + box.at(1));
    ofEndShape();
}


void Trick::drawLastBox(ofVec2f c)
{
    ofFill();
    ofSetColor(255, 242, 124);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(1));
    ofVertex(c + box.at(2));
    ofEndShape();
    
    ofSetColor(8, 96, 168);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(5));
    ofVertex(c + box.at(6));
    ofVertex(c + box.at(1));
    ofEndShape();
    
    ofNoFill();
    ofSetLineWidth(3);
    ofSetColor(0);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(1));
    ofVertex(c + box.at(2));
    ofEndShape();
    
    ofSetColor(0);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(5));
    ofVertex(c + box.at(6));
    ofVertex(c + box.at(1));
    ofEndShape();
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofGetFrameNum

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode