Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ofApp.h"
- float ofApp::fade(float t){
- return (float)(t * t * t * ( t * ( t * 6 - 15) + 10));
- }
- //--------------------------------------------------------------
- void ofApp::setup(){
- fog.allocate(ofGetWidth(),ofGetHeight(),1);
- grid.allocate(ofGetWidth(),ofGetHeight(),1);
- fogOut.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
- gridOut.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
- noise_gui.setup("Perlin Noise","",0,0);
- noise_gui.add(g_size.set("Grid Size",20,1,100));
- }
- //--------------------------------------------------------------
- void ofApp::update(){
- float noise = ofNoise(0,0);
- grid.setColor(ofColor(0));
- fog.setColor(ofColor(0));
- for(int x = 0; x < grid.getWidth(); x++){
- if(x % g_size == 0){
- for(int y = 0; y < grid.getHeight(); y++){
- if(y % g_size == 0){
- noise = ofNoise(x,y,ofGetElapsedTimef());
- grid.setColor(x,y,ofFloatColor(noise));
- }
- }
- }
- }
- ofVec2f g1, g2, g3, g4;
- float f1, f2, f3, f4, u, v, t_1, t_2;
- for(int x = 0; x < grid.getWidth(); x++){
- for(int y = 0; y < grid.getHeight(); y++){
- //find neigboring grid points
- g1 = ofVec2f(x-(x % g_size),y-(y % g_size));
- g2 = ofVec2f(g1.x + g_size,g1.y);
- g3 = ofVec2f(g1.x,g1.y + g_size);
- g4 = g1 + g_size;
- //get noise values from the grid neighbors
- f1 = grid.getColor(g1.x,g1.y).r;
- f2 = grid.getColor(g2.x,g2.y).r;
- f3 = grid.getColor(g3.x,g3.y).r;
- f4 = grid.getColor(g4.x,g4.y).r;
- u = (float)(x % g_size)/g_size;
- v = (float)(y % g_size)/g_size;
- // u = fade(u);
- // v = fade(v);
- t_1 = ofLerp(f1,f2,u);
- t_2 = ofLerp(f3,f4,u);
- ofColor average = ofLerp(t_1,t_2,v);
- fog.setColor(x,y,average);
- }
- }
- fogOut.setFromPixels(fog.getData(), ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
- fogOut.update();
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- fogOut.draw(0,0);
- noise_gui.draw();
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key){
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- }
- //--------------------------------------------------------------
- void ofApp::mouseMoved(int x, int y ){
- }
- //--------------------------------------------------------------
- void ofApp::mouseDragged(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mousePressed(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseReleased(int x, int y, int button){
- }
- //--------------------------------------------------------------
- void ofApp::mouseEntered(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::mouseExited(int x, int y){
- }
- //--------------------------------------------------------------
- void ofApp::windowResized(int w, int h){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement