[work 15] Wave

[work 15] Wave

Movie

Source code

about

  • 波を描く
  • sinとofNoiseを合成

file

  • 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
  • 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h"
#include "ofApp.h"

//================================
int main( ){

    // 4K:4096x2160
    // 2K:2048x1080
    // FullHD:1920x1080
    // HD:1440x1080
    // HD720p:1280x720
    // DVD:720x480
	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 "Wave.hpp"
#include "myColorLib.hpp"

#define WAVE_MAX (4)

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:
    Wave *w[WAVE_MAX];
    
    myColorLib *myColLib;
};
#include "ofApp.h"


ofApp::ofApp(){
    myColorLib m(MYCOL_SELECT);
    
    int offset = 35;
    for (int i = 0; i < WAVE_MAX; i++) {
        w[i] = new Wave(m.getColor(ofRandom(0, 1)), offset * i);
    }
}

ofApp::~ofApp(){
    for (int i = 0; i < WAVE_MAX; i++) {
        delete w[i];
    }
}

//--------------------------------------------------------------
void ofApp::setup(){
    double fps = 5;
    
    ofSetFrameRate(fps);
    ofBackground(255);
    ofSetBackgroundAuto(true);
    
    for (int i = 0; i < WAVE_MAX; i++) {
        w[i]->setup();
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    for (int i = 0; i < WAVE_MAX; i++) {
        w[i]->update();
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for (int i = 0; i < WAVE_MAX; i++) {
        w[i]->display();
    }
    
    ofPushMatrix();
    ofSetColor(0);
    ofNoFill();
    ofSetLineWidth(5);
    ofDrawRectangle(ofGetWidth() / 4, ofGetHeight() / 4, ofGetWidth() / 2, ofGetHeight() / 2);
    ofPopMatrix();
}
#ifndef Wave_hpp
#define Wave_hpp

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


class Wave {
public:
    Wave(ofColor c, float offset);
    ~Wave();
    void setup();
    void update();
    void display();
private:
    Wave();
    
    float angle;
    float angleVelocity;
    float time;
    float timeStep;
    float amp;
    float baseLine;
    float baseLineOffset;
    ofColor waveColor;
};
#endif /* Wave_hpp */
#include "Wave.hpp"



Wave::Wave(ofColor c, float offset)
{
    waveColor = c;
    baseLineOffset = offset;
}


Wave::~Wave()
{
    
}


void Wave::setup()
{
    angle = 0;
    angleVelocity = 0.015;
    time = ofRandom(10000);
    timeStep = 0.01;
    amp = ofRandom(100, 200);
    baseLine = (ofGetHeight() * 3 / 7) + baseLineOffset;
}


void Wave::update()
{
    angle = 0;
}


void Wave::display()
{
    ofVec2f location;
    ofVec2f preLocation;
    
    preLocation.x = ofGetWidth() / 4;
    preLocation.y = std::cos(angle) * ofSignedNoise(time) * amp + baseLine;
    
    ofPushMatrix();
    for (int x = preLocation.x; x < ofGetWidth() * 3 / 4; x++) {
        angle += angleVelocity;
        time += timeStep;
        location.x = x;
        location.y = std::cos(angle) * ofSignedNoise(time) * amp + baseLine;
        
        ofSetColor(255);
        ofSetLineWidth(5);
        ofFill();
        ofDrawLine(preLocation.x, preLocation.y, location.x, location.y);
        
        ofBeginShape();
        ofSetColor(waveColor);
        ofVertex(preLocation.x, preLocation.y);
        ofVertex(location.x, location.y);
        ofVertex(location.x, ofGetHeight() * 3 / 4);
        ofVertex(preLocation.x, ofGetHeight() * 3 / 4);
        ofEndShape();
        
        preLocation = location;
    }
    ofPopMatrix();
}

Link to the API reference page

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

categoryAPI
openframeworksofGraphics ofSetLineWidth
openframeworksofGraphics ofDrawLine
openframeworksofGraphics ofVertex

Development environment

  • openframeworks 0.10.1
  • c++
  • macOS
  • Xcode