[work 68] wave of boxes

[work 68] wave of boxes

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


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

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

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

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

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

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


class Box {
public:
    Box();
    ~Box();
    void setup();
    void update();
    void display();
    
private:
    void drawBox(ofVec2f c);
    
    std::vector<ofVec2f> box;
    std::vector<ofVec2f> pos;
    
    int startPos;
};
#endif /* Box_hpp */
#include "Box.hpp"


Box::Box()
{
    
}


Box::~Box()
{
    
}


void Box::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);
    }
    
    pos.push_back(ofVec2f(ofGetWidth() / 2 + 200, 150));
    for (int i = 0; i < 12; i++) {
        ofVec2f p = pos.back() + box.at(2);
        pos.push_back(p);
    }
    
    startPos = 0;
}


void Box::update()
{
    if (ofGetFrameNum() % 3 == 0) {
        int anglePos = startPos;
        for (ofVec2f &p : pos) {
            float deg = 360.0 / (float)pos.size() * anglePos;
            float rad = TWO_PI * (deg / 360.0);
            p.y += std::sin(rad) * 50;
            anglePos = (++anglePos) % (int)pos.size();
        }
    
        startPos = (++startPos) % (int)pos.size();
    }
}


void Box::display()
{
    for (ofVec2f p : pos) {
        drawBox(p);
    }
}


void Box::drawBox(ofVec2f c)
{
    ofFill();
    ofSetColor(3, 91, 114);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(1));
    ofVertex(c + box.at(2));
    ofVertex(c + box.at(3));
    ofEndShape();
    
    ofSetColor(153, 196, 222);
    ofBeginShape();
    ofVertex(c + box.at(0));
    ofVertex(c + box.at(3));
    ofVertex(c + box.at(4));
    ofVertex(c + box.at(5));
    ofEndShape();
    
    ofSetColor(225, 225, 217);
    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();
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofVertex
c++std::vector

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode