Advertisement
Guest User

oscThread

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