[work 112] Undulation

[work 112] Undulation

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;
    
    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(200, 200, 200));
    cam.lookAt(glm::vec3(0, 0, 0));
    
    time = ofRandom(10000);
    float y = ofMap(ofSignedNoise(time, time), -1, 1, -100, 100);
    radius.assign(2, 0);
    
    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);
}

//--------------------------------------------------------------
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() - 1; 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);
            
            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), -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), -1, 1, -100, 100);
        radius.push_back(1);
        
        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);
    }
    
    if (radius.front() >= 500) {
        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.setHsb(ofMap(radius.at(i), 0, 500, 0, 255), 255, 255);
            c1.setHsb(ofMap(radius.at(i + 1), 0, 500, 0, 255), 255, 255);
            float a0 = ofMap(radius.at(i), 0, 500, 255, 0);
            float a1 = ofMap(radius.at(i + 1), 0, 500, 255, 0);
            
            m.addColor(ofColor(c0, a0));
            m.addColor(ofColor(c1, a1));
        }
        mesh.push_back(m);
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.begin();
    for (ofMesh m : mesh) {
        m.drawWireframe();
    }
    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