Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. //ofapp.h
  2.  
  3. #pragma once
  4.  
  5. #include "ofMain.h"
  6. #include "oscThread.h"
  7. #include "ofxGui.h"
  8.  
  9. class ofApp : public ofBaseApp{
  10.    
  11. public:
  12.     void setup();
  13.     void update();
  14.     void draw();
  15.    
  16.     void keyPressed(int key);
  17.     void keyReleased(int key);
  18.     void dragEvent(ofDragInfo dragInfo);
  19.     void gotMessage(ofMessage msg);
  20.    
  21.     int numMesh;
  22.    
  23.     ofEasyCam cam;
  24.     ofMesh mesh;
  25.     vector< vector<oscThread> > braid;
  26.    
  27.     ofParameter<float> amp, f;
  28.     ofParameter<int> res, speed;
  29.    
  30.     ofxPanel gui;
  31. };
  32.  
  33.  
  34.  
  35.  
  36.  
  37. #include "ofApp.h"
  38.  
  39. //--------------------------------------------------------------
  40. void ofApp::setup(){
  41.     ofSetVerticalSync(true);
  42.     numMesh = 3;
  43.    
  44.     /*for(int i=0;i<numMesh;i++){
  45.      mesh.addVertex(ofPoint(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()),ofRandom(600)));
  46.      mesh.addColor(ofColor(ofRandom(255)));
  47.      
  48.      offsets.push_back(ofVec3f(ofRandom(0,10000),ofRandom(0,10000),ofRandom(0,10000)));
  49.      mesh.setupIndicesAuto();
  50.      }*/
  51.    
  52.     mesh.addVertex(ofVec3f(-150,0));
  53.     mesh.addColor(ofColor(0));
  54.     mesh.addVertex(ofVec3f(150,0));
  55.     mesh.addColor(ofColor(0));
  56.     mesh.addVertex(ofVec3f(-150,100,50));
  57.     mesh.addColor(ofColor(0));
  58.    
  59.     mesh.setMode(OF_PRIMITIVE_POINTS);
  60.    
  61.     //GUI
  62.     gui.setup();
  63.     gui.add(res.set("Number of Vertices",200,10,200));
  64.     gui.add(amp.set("Amplitude",10,1,100));
  65.     gui.add(f.set("Frequency",10,1,100));
  66.     gui.add(speed.set("Speed",5,1,50));
  67.    
  68.     glEnable(GL_POINT_SMOOTH);
  69.     glPointSize(15);
  70. }
  71.  
  72. //--------------------------------------------------------------
  73. void ofApp::update(){
  74.     if(braid.size()>0){
  75.         for(int i=0;i<braid.size();i++){
  76.             for(int j=0;j<braid[i].size();j++){
  77.                 braid[i][j].amp = amp;
  78.                 braid[i][j].speed = speed;
  79.                 braid[i][j].f = f;
  80.                 braid[i][j].update();
  81.             }
  82.         }
  83.     }
  84.    
  85.     //Point Jitter
  86.     /*
  87.      for(int i=0;i<numMesh; i++){
  88.      ofVec3f vert = mesh.getVertex(i);
  89.      
  90.      float time = ofGetElapsedTimef();
  91.      float timeScale = 5.0;
  92.      float displacementScale = 0.75;
  93.      ofVec3f timeOffsets = offsets[i];
  94.      
  95.      vert.x += (ofSignedNoise(time*timeScale+timeOffsets.x)) * displacementScale;
  96.      vert.y += (ofSignedNoise(time*timeScale+timeOffsets.y)) * displacementScale;
  97.      vert.z += (ofSignedNoise(time*timeScale+timeOffsets.z)) * displacementScale;
  98.      
  99.      mesh.setVertex(i,vert);
  100.      }
  101.      */
  102. }
  103.  
  104. //--------------------------------------------------------------
  105. void ofApp::draw(){
  106.     ofEnableDepthTest();
  107.     //ofBackgroundGradient(ofColor(50), ofColor(0));
  108.    
  109.     cam.begin();
  110.     mesh.draw();
  111.     if(braid.size()>0){
  112.         for(int i=0;i<braid.size();i++){
  113.             for(int j=0;j<braid[i].size();j++){
  114.                 braid[i][j].draw();
  115.             }
  116.         }
  117.     }
  118.     cam.end();
  119.    
  120.     ofDrawBitmapString("Number of Braids: " + ofToString(braid.size()),10,10);
  121.     gui.draw();
  122. }
  123.  
  124. //--------------------------------------------------------------
  125. void ofApp::keyPressed(int key){
  126.     if(key=='f'){
  127.         vector<oscThread> tempBraid;    //Inner vector
  128.         ofVec2f tempOrigin;             //Origin/dest vector
  129.         tempOrigin.set((int)ofRandom(numMesh),(int)ofRandom(numMesh));
  130.         //tempOrigin.set(1,2);
  131.         while (tempOrigin.x == tempOrigin.y){
  132.             tempOrigin.set((int)ofRandom(numMesh),(int)ofRandom(numMesh));
  133.         }
  134.        
  135.         ofLog(OF_LOG_NOTICE,"TEMPORIGIN: " + ofToString(tempOrigin.x) + ", " + ofToString(tempOrigin.y));
  136.        
  137.         for(int i=0;i<1;i++){ // 1 being the size of "burst"
  138.             tempBraid.push_back(oscThread(tempOrigin,mesh));
  139.             tempBraid.back().res = res;
  140.             tempBraid.back().setup();   //Setup new threads
  141.         }
  142.        
  143.         braid.push_back(tempBraid);     //Finally insert tempBraid into outer vector
  144.     }
  145. }
  146.  
  147. //--------------------------------------------------------------
  148. void ofApp::keyReleased(int key){
  149.    
  150. }
  151.  
  152. //--------------------------------------------------------------
  153. void ofApp::gotMessage(ofMessage msg){
  154.    
  155. }
  156.  
  157. //--------------------------------------------------------------
  158. void ofApp::dragEvent(ofDragInfo dragInfo){
  159.    
  160. }
  161.  
  162.  
  163.  
  164. //oscThread.h
  165.  
  166. #ifndef _OSC_THREAD
  167. #define _OSC_THREAD
  168.  
  169. #include "ofMain.h"
  170.  
  171. class oscThread : of3dPrimitive {
  172.    
  173. public:
  174.     void setup();
  175.     void draw();
  176.     void update();
  177.    
  178.     ofVec3f originP, destP;
  179.     ofVec3f tempO, tempD; //temp origin/dest
  180.     float length;
  181.     float angleRotX, angleRotY, angleRotZ;
  182.     ofQuaternion xRot, yRot, zRot, curRot;
  183.    
  184.     ofColor color;
  185.    
  186.     int   res ;    //Resolution
  187.     int   speed;  //Speed (lower=faster)
  188.     float amp;    //Amplitude
  189.     float f;      //Frequency
  190.    
  191.     int numVerts;
  192.    
  193.     oscThread(ofVec2f origin, ofMesh mesh);
  194. };
  195.  
  196. #endif
  197.  
  198.  
  199.  
  200. //oscThread.cpp
  201.  
  202. #include "oscThread.h"
  203.  
  204. oscThread::oscThread(ofVec2f origin, ofMesh mesh)
  205. {
  206.     originP = mesh.getVertex(origin.x);
  207.     destP = mesh.getVertex(origin.y);
  208.    
  209.     length = sqrt(pow(destP.x-originP.x,2)+pow(destP.y-originP.y,2)+pow(destP.z-originP.z,2));
  210.    
  211.     //consider defining in update, to allow for point jitter!
  212.     //someone has suggested this to be more efficient
  213.     //sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  214.     angleRotX = atan2((destP.y-originP.y),(destP.z-originP.z))*180/PI;
  215.     angleRotY = atan2((destP.z-originP.z),(destP.x-originP.x))*180/PI;
  216.     angleRotZ = atan2((destP.y-originP.y),(destP.x-originP.x))*180/PI;
  217.    
  218.     xRot.makeRotate(angleRotX, ofVec3f(1,0,0));
  219.     yRot.makeRotate(angleRotY, ofVec3f(0,1,0));
  220.     zRot.makeRotate(angleRotZ, ofVec3f(0,0,1));
  221.     curRot *= xRot*yRot*zRot;
  222.    
  223.     tempO.set(-length/2,0);
  224.     tempD.set(length/2,0);
  225.    
  226.     //Rethink these variables, and in what context do they REALLY have influence?
  227.     color = ofColor(ofRandom(200));
  228.     res   = 200;
  229.     speed = 5;
  230.     amp   = 10;
  231.     f     = 3;
  232.    
  233.     numVerts  = 0;
  234. }
  235.  
  236. void oscThread::setup()
  237. {
  238.     numVerts = this->getMesh().getNumVertices();
  239.     float time = ofGetElapsedTimef()/speed;
  240.    
  241.     for (float x=0;x<res;x++){
  242.         float delta = x/res;
  243.         float p = x;
  244.         float oscillation = amp * sin((2 * pi * f * time + p)/20);
  245.        
  246.         ofVec3f signal = tempO.getInterpolated(tempD, delta);
  247.         signal.set(signal);
  248.        
  249.         ofPushMatrix();
  250.         signal.z = oscillation;
  251.         ofPopMatrix();
  252.        
  253.         this->getMesh().addVertex(signal);
  254.         this->getMesh().addColor(color);
  255.        
  256.         numVerts++;
  257.     }
  258.    
  259.     this->getMesh().setMode(OF_PRIMITIVE_LINE_STRIP);
  260.     glLineWidth(2);
  261. }
  262.  
  263. void oscThread::draw()
  264. {
  265.     ofPushMatrix();
  266.         ofTranslate((originP.x+destP.x)/2,(originP.y+destP.y)/2,(originP.z+destP.z)/2);
  267.         ofVec3f axis;
  268.         float angle;
  269.         curRot.getRotate(angle, axis);
  270.         ofRotate(angle, axis.x, axis.y, axis.z);
  271.         transformGL();
  272.         this->getMesh().draw();
  273.         restoreTransformGL();
  274.     ofPopMatrix();
  275. }
  276.  
  277. void oscThread::update()
  278. {
  279.     float time = ofGetElapsedTimef()/speed;
  280.    
  281.     for (int x=0;x<res;x++){
  282.         ofVec3f oscTemp = this->getMesh().getVertex(x);
  283.         float p = x;
  284.         float oscillation = amp * sin(2 * pi * f * time + p)/20;
  285.         oscTemp.rotate(x/f,ofVec3f(1,0,0));
  286.         this->getMesh().setVertex(x,oscTemp);
  287.     }
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement