[work 10] Perlin noise
![[work 10] Perlin noise](https://i0.wp.com/tsukuru.hayato-works.com/wp-content/uploads/2019/02/outFrameImg0000.png?fit=1024%2C576&ssl=1)
Movie
Source code
about
- 周波数と振幅を変えた複数のパーリンノイズ(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 <random>
#include "myColorLib.hpp"
#include "OctaveNoise.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:
OctaveNoise *oct;
float tx;
float ty;
float tz;
float dt;
ofTexture tex;
ofPixels pix;
unsigned char *color;
myColorLib *myColLib;
};
#include "ofApp.h"
ofApp::ofApp(){
color = new unsigned char[ofGetWidth() * ofGetHeight() * 3];
oct = new OctaveNoise(8);
myColLib = new myColorLib(MYCOL_ALL);
}
ofApp::~ofApp(){
delete myColLib;
delete oct;
delete color;
}
//--------------------------------------------------------------
void ofApp::setup(){
double fps = 1;
ofSetFrameRate(fps);
ofBackground(255,255,255);
ofSetBackgroundAuto(true);
tx = ty = tz = 0;
dt = 0.01;
pix.allocate(ofGetWidth(), ofGetHeight(), OF_PIXELS_RGBA);
tex.allocate(pix);
}
//--------------------------------------------------------------
void ofApp::update(){
ty = 0.0;
for (int y = 0; y < ofGetHeight(); y++) {
tx = 0.0;
for (int x = 0; x < ofGetWidth(); x++) {
//float gray = (unsigned char)ofMap(oct->Noise(tx, ty, tz), 0, 1, 0, 255, true);
//ofColor c = ofColor(gray);
ofColor c = myColLib->getColor(oct->Noise(tx, ty, tz));
pix.setColor(x, y, c);
tx += dt;
}
ty += dt;
}
tz += 0.1;
tex.loadData(pix);
}
//--------------------------------------------------------------
void ofApp::draw(){
tex.draw(0, 0);
}
#ifndef OctaveNoise_hpp
#define OctaveNoise_hpp
#include <stdio.h>
#include "ofMain.h"
class OctaveNoise {
public:
OctaveNoise(int oct);
~OctaveNoise();
float Noise(float x);
float Noise(float x, float y);
float Noise(float x, float y, float z);
private:
int octave = 8;
float persistence = 0.5;
};
#endif /* OctaveNoise_hpp */
#include "OctaveNoise.hpp"
OctaveNoise::OctaveNoise(int oct)
{
octave = oct;
}
OctaveNoise::~OctaveNoise()
{
}
float OctaveNoise::Noise(float x)
{
Noise(x, 0, 0);
}
float OctaveNoise::Noise(float x, float y)
{
Noise(x, y, 0);
}
float OctaveNoise::Noise(float x, float y, float z)
{
float value = 0;
float frequency = 1;
float amplitude = 1;
float maxValue = 0;
for (int i = 0; i < octave; i++) {
value += ofNoise(x * frequency, y * frequency, z * frequency) * amplitude;
maxValue += amplitude;
amplitude *= persistence;
frequency *= 2;
}
return value / maxValue;
}
Link to the API reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
| category | API |
| openframeworks | ofTexture |
| openframeworks | ofPixels |
| openframeworks | ofMath ofNoise |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode