[work 84] Wavy Cubes

[work 84] Wavy Cubes

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 "Cube.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:
    ofCamera cam;
    std::vector<std::shared_ptr<Cube>> cube;
    
    float size;
    int col;
    int row;
    
    int updetaCubes;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    ofEnableDepthTest();
    ofEnableSmoothing();
    
    cam.setGlobalPosition(ofVec3f(1500, 1500, 1500));
    cam.lookAt(glm::vec3(0, 0, 0));
    
    size = 150;
    row = 20;
    col = 10;
    updetaCubes = 0;
    
    for (int n = -row / 2; n < row / 2; n++) {
        float x = size * n;
        float y = 0;
        float z = size * -((col / 2) - 1);
        cube.push_back(make_shared<Cube>(ofVec3f(x, y, z), size));
        cube.back()->setup();
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    int count = ofGetFrameNum() % (int)ofGetTargetFrameRate();
    
    if ((count == 0) && (updetaCubes < cube.size())) {
        updetaCubes++;
    }
    
    for (int i = 0; i < updetaCubes; i++) {
        cube.at(i)->update();
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    ofSetRectMode(OF_RECTMODE_CENTER);
    for (int i = -row / 2; i < row / 2; i++) {
        for (int j = -col / 2; j < col / 2; j++) {
            ofPushMatrix();
            ofSetColor(240, 201, 194);
            ofNoFill();
            ofTranslate(ofVec3f(i * size, 0, j * size));
            ofRotateDeg(90, 1, 0, 0);
            ofDrawRectangle(0, 0, 0, size, size);
            ofPopMatrix();
        }
    }
    
    for (std::shared_ptr<Cube> c : cube) {
        c->display();
    }
    cam.end();
}

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

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


class Cube {
public:
    Cube();
    Cube(ofVec3f p, float s);
    ~Cube();
    void setup();
    void update();
    void display();
private:
    void move(int dir);
    void randMove();
    
    ofVec3f center;
    ofVec3f position;
    ofVec3f moveTo;
    float size;
    ofColor colFrame;
    ofColor colFace;
    
    bool pause;
    bool forward;
    int updateCount;
    
    ofVec3f rAxis;
    float rAngle;
};
#endif /* Cube_hpp */
#include "Cube.hpp"


Cube::Cube()
{
    position = ofVec3f(0, 0, 0);
    size = 150;
}


Cube::Cube(ofVec3f p, float s)
{
    position = p;
    size = s;
}


Cube::~Cube()
{
    
}


void Cube::setup()
{
    center = ofVec3f(0, size / 2, 0);
    moveTo = ofVec3f(0, 0, 0);
    rAxis = ofVec3f(0, 0, 0);
    rAngle = 0;
    colFrame = ofColor(0);
    colFace = ofColor(ofRandom(255), ofRandom(255), ofRandom(255));
    pause = false;
    forward = false;
    updateCount = 0;
}


void Cube::update()
{
    int loopMax = (int)ofGetTargetFrameRate();
    int loopCount = updateCount % loopMax;
    updateCount++;
    
    if (loopCount == 0) {
        rAngle = 0;
        position += moveTo;
        
        int n = updateCount / loopMax;
        if (n % 8 == 0) {
            forward = !forward;
        }
        
        if (forward) {
            move(0);
        } else {
            move(1);
        }
    } else {
        if (pause == false) {
            rAngle = (int)ofxeasing::map(loopCount, 0, loopMax - 1, 0, 90, ofxeasing::quart::easeInOut);
        }
    }
}


void Cube::display()
{
    ofPushMatrix();
    ofTranslate(position);
    ofTranslate(moveTo * 0.5);
    ofRotateDeg(rAngle, rAxis.x, rAxis.y, rAxis.z);
    ofTranslate(moveTo * -0.5);
    
    ofSetColor(colFace);
    ofFill();
    ofDrawBox(center, size * 0.9);
    
    ofSetColor(colFrame);
    ofNoFill();
    ofDrawBox(center, size);
    
    ofPopMatrix();
}


void Cube::move(int dir)
{
    pause = false;
    
    if (dir == 0) { // Forward(Z-axis(+))
        rAxis = ofVec3f(1, 0, 0);
        moveTo = ofVec3f(0, 0, 1) * size;
    } else if (dir == 1) { // Back(Z-axis(-))
        rAxis = ofVec3f(-1, 0, 0);
        moveTo = ofVec3f(0, 0, -1) * size;
    } else if (dir == 2) { // Right(X-axis(-))
        rAxis = ofVec3f(0, 0, 1);
        moveTo = ofVec3f(-1, 0, 0) * size;
    } else if (dir == 3) { // Left(X-axis(+))
        rAxis = ofVec3f(0, 0, -1);
        moveTo = ofVec3f(1, 0, 0) * size;
    } else { // Pause
        pause = true;
        rAxis = ofVec3f(0, 0, 0);
        moveTo = ofVec3f(0, 0, 0) * size;
    }
}


void Cube::randMove()
{
    int dir = (int)ofRandom(5);
    move(dir);
}

Link to the reference page

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

categoryAPI/Lib
openframeworksofTranslate
openframeworksofRotateDeg
openframeworksofEnableDepthTest
openframeworksofxeasing map

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode