Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ofapp.h
- #pragma once
- #include "ofMain.h"
- #include "oscThread.h"
- #include "ofxGui.h"
- class ofApp : public ofBaseApp{
- public:
- void setup();
- void update();
- void draw();
- void keyPressed(int key);
- void keyReleased(int key);
- void dragEvent(ofDragInfo dragInfo);
- void gotMessage(ofMessage msg);
- int numMesh;
- ofEasyCam cam;
- ofMesh mesh;
- vector< vector<oscThread> > braid;
- ofParameter<float> amp, f;
- ofParameter<int> res, speed;
- ofxPanel gui;
- };
- #include "ofApp.h"
- //--------------------------------------------------------------
- void ofApp::setup(){
- ofSetVerticalSync(true);
- numMesh = 3;
- /*for(int i=0;i<numMesh;i++){
- mesh.addVertex(ofPoint(ofRandom(ofGetWidth()),ofRandom(ofGetHeight()),ofRandom(600)));
- mesh.addColor(ofColor(ofRandom(255)));
- offsets.push_back(ofVec3f(ofRandom(0,10000),ofRandom(0,10000),ofRandom(0,10000)));
- mesh.setupIndicesAuto();
- }*/
- mesh.addVertex(ofVec3f(-150,0));
- mesh.addColor(ofColor(0));
- mesh.addVertex(ofVec3f(150,0));
- mesh.addColor(ofColor(0));
- mesh.addVertex(ofVec3f(-150,100,50));
- mesh.addColor(ofColor(0));
- mesh.setMode(OF_PRIMITIVE_POINTS);
- //GUI
- gui.setup();
- gui.add(res.set("Number of Vertices",200,10,200));
- gui.add(amp.set("Amplitude",10,1,100));
- gui.add(f.set("Frequency",10,1,100));
- gui.add(speed.set("Speed",5,1,50));
- glEnable(GL_POINT_SMOOTH);
- glPointSize(15);
- }
- //--------------------------------------------------------------
- void ofApp::update(){
- if(braid.size()>0){
- for(int i=0;i<braid.size();i++){
- for(int j=0;j<braid[i].size();j++){
- braid[i][j].amp = amp;
- braid[i][j].speed = speed;
- braid[i][j].f = f;
- braid[i][j].update();
- }
- }
- }
- //Point Jitter
- /*
- for(int i=0;i<numMesh; i++){
- ofVec3f vert = mesh.getVertex(i);
- float time = ofGetElapsedTimef();
- float timeScale = 5.0;
- float displacementScale = 0.75;
- ofVec3f timeOffsets = offsets[i];
- vert.x += (ofSignedNoise(time*timeScale+timeOffsets.x)) * displacementScale;
- vert.y += (ofSignedNoise(time*timeScale+timeOffsets.y)) * displacementScale;
- vert.z += (ofSignedNoise(time*timeScale+timeOffsets.z)) * displacementScale;
- mesh.setVertex(i,vert);
- }
- */
- }
- //--------------------------------------------------------------
- void ofApp::draw(){
- ofEnableDepthTest();
- //ofBackgroundGradient(ofColor(50), ofColor(0));
- cam.begin();
- mesh.draw();
- if(braid.size()>0){
- for(int i=0;i<braid.size();i++){
- for(int j=0;j<braid[i].size();j++){
- braid[i][j].draw();
- }
- }
- }
- cam.end();
- ofDrawBitmapString("Number of Braids: " + ofToString(braid.size()),10,10);
- gui.draw();
- }
- //--------------------------------------------------------------
- void ofApp::keyPressed(int key){
- if(key=='f'){
- vector<oscThread> tempBraid; //Inner vector
- ofVec2f tempOrigin; //Origin/dest vector
- tempOrigin.set((int)ofRandom(numMesh),(int)ofRandom(numMesh));
- //tempOrigin.set(1,2);
- while (tempOrigin.x == tempOrigin.y){
- tempOrigin.set((int)ofRandom(numMesh),(int)ofRandom(numMesh));
- }
- ofLog(OF_LOG_NOTICE,"TEMPORIGIN: " + ofToString(tempOrigin.x) + ", " + ofToString(tempOrigin.y));
- for(int i=0;i<1;i++){ // 1 being the size of "burst"
- tempBraid.push_back(oscThread(tempOrigin,mesh));
- tempBraid.back().res = res;
- tempBraid.back().setup(); //Setup new threads
- }
- braid.push_back(tempBraid); //Finally insert tempBraid into outer vector
- }
- }
- //--------------------------------------------------------------
- void ofApp::keyReleased(int key){
- }
- //--------------------------------------------------------------
- void ofApp::gotMessage(ofMessage msg){
- }
- //--------------------------------------------------------------
- void ofApp::dragEvent(ofDragInfo dragInfo){
- }
- //oscThread.h
- #ifndef _OSC_THREAD
- #define _OSC_THREAD
- #include "ofMain.h"
- class oscThread : of3dPrimitive {
- public:
- void setup();
- void draw();
- void update();
- ofVec3f originP, destP;
- ofVec3f tempO, tempD; //temp origin/dest
- float length;
- float angleRotX, angleRotY, angleRotZ;
- ofQuaternion xRot, yRot, zRot, curRot;
- ofColor color;
- int res ; //Resolution
- int speed; //Speed (lower=faster)
- float amp; //Amplitude
- float f; //Frequency
- int numVerts;
- oscThread(ofVec2f origin, ofMesh mesh);
- };
- #endif
- //oscThread.cpp
- #include "oscThread.h"
- oscThread::oscThread(ofVec2f origin, ofMesh mesh)
- {
- originP = mesh.getVertex(origin.x);
- destP = mesh.getVertex(origin.y);
- length = sqrt(pow(destP.x-originP.x,2)+pow(destP.y-originP.y,2)+pow(destP.z-originP.z,2));
- //consider defining in update, to allow for point jitter!
- //someone has suggested this to be more efficient
- //sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- angleRotX = atan2((destP.y-originP.y),(destP.z-originP.z))*180/PI;
- angleRotY = atan2((destP.z-originP.z),(destP.x-originP.x))*180/PI;
- angleRotZ = atan2((destP.y-originP.y),(destP.x-originP.x))*180/PI;
- xRot.makeRotate(angleRotX, ofVec3f(1,0,0));
- yRot.makeRotate(angleRotY, ofVec3f(0,1,0));
- zRot.makeRotate(angleRotZ, ofVec3f(0,0,1));
- curRot *= xRot*yRot*zRot;
- tempO.set(-length/2,0);
- tempD.set(length/2,0);
- //Rethink these variables, and in what context do they REALLY have influence?
- color = ofColor(ofRandom(200));
- res = 200;
- speed = 5;
- amp = 10;
- f = 3;
- numVerts = 0;
- }
- void oscThread::setup()
- {
- numVerts = this->getMesh().getNumVertices();
- float time = ofGetElapsedTimef()/speed;
- for (float x=0;x<res;x++){
- float delta = x/res;
- float p = x;
- float oscillation = amp * sin((2 * pi * f * time + p)/20);
- ofVec3f signal = tempO.getInterpolated(tempD, delta);
- signal.set(signal);
- ofPushMatrix();
- signal.z = oscillation;
- ofPopMatrix();
- this->getMesh().addVertex(signal);
- this->getMesh().addColor(color);
- numVerts++;
- }
- this->getMesh().setMode(OF_PRIMITIVE_LINE_STRIP);
- glLineWidth(2);
- }
- void oscThread::draw()
- {
- ofPushMatrix();
- ofTranslate((originP.x+destP.x)/2,(originP.y+destP.y)/2,(originP.z+destP.z)/2);
- ofVec3f axis;
- float angle;
- curRot.getRotate(angle, axis);
- ofRotate(angle, axis.x, axis.y, axis.z);
- transformGL();
- this->getMesh().draw();
- restoreTransformGL();
- ofPopMatrix();
- }
- void oscThread::update()
- {
- float time = ofGetElapsedTimef()/speed;
- for (int x=0;x<res;x++){
- ofVec3f oscTemp = this->getMesh().getVertex(x);
- float p = x;
- float oscillation = amp * sin(2 * pi * f * time + p)/20;
- oscTemp.rotate(x/f,ofVec3f(1,0,0));
- this->getMesh().setVertex(x,oscTemp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement