Advertisement
Guest User

ofApp.cpp

a guest
Sep 29th, 2016
121
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "ofApp.h"
  2.  
  3. float ofApp::fade(float t){
  4.     return (float)(t * t * t * ( t * ( t * 6 - 15) + 10));
  5. }
  6.  
  7. //--------------------------------------------------------------
  8. void ofApp::setup(){
  9.     fog.allocate(ofGetWidth(),ofGetHeight(),1);
  10.     grid.allocate(ofGetWidth(),ofGetHeight(),1);
  11.     fogOut.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
  12.     gridOut.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
  13.    
  14.     noise_gui.setup("Perlin Noise","",0,0);
  15.     noise_gui.add(g_size.set("Grid Size",20,1,100));
  16. }
  17.  
  18. //--------------------------------------------------------------
  19. void ofApp::update(){
  20.     float noise = ofNoise(0,0);
  21.    
  22.     grid.setColor(ofColor(0));
  23.     fog.setColor(ofColor(0));
  24.    
  25.     for(int x = 0; x < grid.getWidth(); x++){
  26.         if(x % g_size == 0){
  27.             for(int y = 0; y < grid.getHeight(); y++){
  28.                 if(y % g_size == 0){
  29.                     noise = ofNoise(x,y,ofGetElapsedTimef());
  30.                     grid.setColor(x,y,ofFloatColor(noise));
  31.                 }
  32.             }
  33.         }
  34.     }
  35.    
  36.     ofVec2f g1, g2, g3, g4;
  37.     float f1, f2, f3, f4, u, v, t_1, t_2;
  38.    
  39.     for(int x = 0; x < grid.getWidth(); x++){
  40.         for(int y = 0; y < grid.getHeight(); y++){
  41.             //find neigboring grid points
  42.             g1 = ofVec2f(x-(x % g_size),y-(y % g_size));
  43.             g2 = ofVec2f(g1.x + g_size,g1.y);
  44.             g3 = ofVec2f(g1.x,g1.y + g_size);
  45.             g4 = g1 + g_size;
  46.  
  47.             //get noise values from the grid neighbors
  48.             f1 = grid.getColor(g1.x,g1.y).r;
  49.             f2 = grid.getColor(g2.x,g2.y).r;
  50.             f3 = grid.getColor(g3.x,g3.y).r;
  51.             f4 = grid.getColor(g4.x,g4.y).r;
  52.  
  53.             u = (float)(x % g_size)/g_size;
  54.             v = (float)(y % g_size)/g_size;
  55.            
  56. //            u = fade(u);
  57. //            v = fade(v);
  58.  
  59.             t_1 = ofLerp(f1,f2,u);
  60.             t_2 = ofLerp(f3,f4,u);
  61.            
  62.             ofColor average = ofLerp(t_1,t_2,v);
  63.            
  64.             fog.setColor(x,y,average);
  65.         }
  66.     }
  67.    
  68.     fogOut.setFromPixels(fog.getData(), ofGetWidth(), ofGetHeight(), OF_IMAGE_GRAYSCALE);
  69.     fogOut.update();
  70. }
  71.  
  72. //--------------------------------------------------------------
  73. void ofApp::draw(){
  74.     fogOut.draw(0,0);
  75.     noise_gui.draw();
  76. }
  77.  
  78. //--------------------------------------------------------------
  79. void ofApp::keyPressed(int key){
  80.  
  81. }
  82.  
  83. //--------------------------------------------------------------
  84. void ofApp::keyReleased(int key){
  85.  
  86. }
  87.  
  88. //--------------------------------------------------------------
  89. void ofApp::mouseMoved(int x, int y ){
  90.  
  91. }
  92.  
  93. //--------------------------------------------------------------
  94. void ofApp::mouseDragged(int x, int y, int button){
  95.  
  96. }
  97.  
  98. //--------------------------------------------------------------
  99. void ofApp::mousePressed(int x, int y, int button){
  100.  
  101. }
  102.  
  103. //--------------------------------------------------------------
  104. void ofApp::mouseReleased(int x, int y, int button){
  105.  
  106. }
  107.  
  108. //--------------------------------------------------------------
  109. void ofApp::mouseEntered(int x, int y){
  110.  
  111. }
  112.  
  113. //--------------------------------------------------------------
  114. void ofApp::mouseExited(int x, int y){
  115.  
  116. }
  117.  
  118. //--------------------------------------------------------------
  119. void ofApp::windowResized(int w, int h){
  120.  
  121. }
  122.  
  123. //--------------------------------------------------------------
  124. void ofApp::gotMessage(ofMessage msg){
  125.  
  126. }
  127.  
  128. //--------------------------------------------------------------
  129. void ofApp::dragEvent(ofDragInfo dragInfo){
  130.  
  131. }
Advertisement
RAW Paste Data Copied
Advertisement