[work 114] Undulation in Box

[work 114] Undulation in Box

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"


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:
    int step;
    int nofp;
    std::vector<ofMesh> mesh;
    std::vector<std::vector<ofVec3f>> points;
    std::vector<float> radius;
    float time;
    float dt;
    
    ofRectangle area;
    
    ofEasyCam cam;
};
#include "ofApp.h"


ofApp::ofApp(){
    
}

ofApp::~ofApp(){
    
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 30;
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    ofSetVerticalSync(true);
    
    ofEnableDepthTest();
    ofEnableSmoothing();

    cam.setPosition(glm::vec3(300, 300, 300));
    cam.lookAt(glm::vec3(0, 0, 0));
    
    time = ofRandom(10000);
    float y = ofMap(ofSignedNoise(time, time, time), -1, 1, -100, 100);
    
    step = 6;
    nofp = 360 / step;
    std::vector<ofVec3f> p;
    ofVec3f v = ofVec3f(1, y, 0);
    for (int i = 0; i < nofp; i++) {
        v.rotate(step, ofVec3f(0, 1, 0));
        p.push_back(v);
    }
    points.assign(2, p);
    radius.assign(2, 1);
    
    dt = 0;
    
    area.setFromCenter(glm::vec3(0, 0, 0), 500, 500);
}

//--------------------------------------------------------------
void ofApp::update(){
    for (int i = 0; i < radius.size() - 1; i++) {
        radius.at(i) += 1;
    }
    
    mesh.clear();
    for (int i = 0; i < points.size(); i++) {
        for (int j = 0; j < points.at(i).size(); j++) {
            points.at(i).at(j).y = 0;
            points.at(i).at(j).normalize();
            
            float x = points.at(i).at(j).x * radius.at(i);
            float z = points.at(i).at(j).z * radius.at(i);
            
            if (area.getLeft() > x) {
                float tmp = (z / x) * area.getLeft();
                x = area.getLeft();
                z = tmp;
            } else if (area.getRight() < x) {
                float tmp = (z / x) * area.getRight();
                x = area.getRight();
                z = tmp;
            }
            
            if (area.getTop() > z) {
                float tmp = (x / z) * area.getTop();
                z = area.getTop();
                x = tmp;
            } else if (area.getBottom() < z) {
                float tmp = (x / z) * area.getBottom();
                z = area.getBottom();
                x = tmp;
            }
            
            float dt0 = x * 0.003;
            float dt1 = z * 0.003;
            
            points.at(i).at(j).x = x;
            points.at(i).at(j).y = ofMap(ofSignedNoise(time + dt0, time + dt1, time + dt), -1, 1, -100, 100);
            points.at(i).at(j).z = z;
        }
    }
    
    auto itr = radius.rbegin();
    if (*(itr + 1) > 10) {
        float y = ofMap(ofSignedNoise(time, time, time + dt), -1, 1, -100, 100);
        
        step = 6;
        nofp = 360 / step;
        std::vector<ofVec3f> p;
        ofVec3f v = ofVec3f(1, y, 0);
        for (int i = 0; i < nofp; i++) {
            v.rotate(step, ofVec3f(0, 1, 0));
            p.push_back(v);
        }
        points.push_back(p);
        radius.push_back(1);
    }
    
    if (radius.front() >= glm::length(area.getTopLeft())) {
        auto rItr = radius.begin();
        radius.erase(rItr);
        auto pItr = points.begin();
        points.erase(pItr);
    }
    
    for (int i = 0; i < points.size() - 1; i++) {
        ofMesh m;
        m.setMode(OF_PRIMITIVE_TRIANGLE_STRIP);
        for (int j = 0; j <= points.at(i).size(); j++) {
            int index = j % points.at(i).size();
            
            m.addVertex(points.at(i).at(index));
            m.addVertex(points.at(i + 1).at(index));
            
            ofColor c0, c1;
            c0 = ofColor(0, 0, 255);
            c1 = ofColor(0, 0, 255);
            float a0 = ofMap(points.at(i).at(index).y, -100, 100, 255, 0);
            float a1 = ofMap(points.at(i + 1).at(index).y, -100, 100, 255, 0);
            
            m.addColor(ofColor(c0, a0));
            m.addColor(ofColor(c1, a1));
        }
        mesh.push_back(m);
    }
    
    dt += 0.003;
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    ofRotateYDeg(ofGetFrameNum() * 0.25);
    ofSetLineWidth(1);
    for (ofMesh m : mesh) {
        m.drawWireframe();
    }
    
    ofSetColor(0);
    ofNoFill();
    ofSetLineWidth(3);
    ofDrawBox(0, 0, 0, area.width, 200, area.height);
    cam.end();
}

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

Link to the reference page

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

categoryAPI/Lib
openframeworksofMesh
openframeworksofSignedNoise
openframeworksofMap
openframeworksofColor
c++std::vector

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode