Advertisement
Guest User

ofApp.cpp

a guest
Jun 9th, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include "ofApp.h"
  2.  
  3. //--------------------------------------------------------------
  4. void ofApp::setup(){
  5.     ofSetFrameRate(60);
  6.    
  7.     //origin and destination points
  8.     targets.addVertex(ofVec3f(-150,0));
  9.     targets.addColor(ofColor(255));
  10.     targets.addVertex(ofVec3f(150,50,50));
  11.     targets.addColor(ofColor(255));
  12.    
  13.     targets.setMode(OF_PRIMITIVE_POINTS);
  14.    
  15.     glEnable(GL_POINT_SMOOTH);
  16.     glPointSize(5);
  17.    
  18.     //line to be transformed
  19.     originP = targets.getVertex(0);
  20.     destP = targets.getVertex(1);
  21.    
  22.     length = sqrt(pow(destP.x-originP.x,2)+pow(destP.y-originP.y,2)+pow(destP.z-originP.z,2));
  23.    
  24.     tempO.set(-length/2,0);
  25.     tempD.set(length/2,0);
  26.    
  27.     float time = ofGetElapsedTimef()/5;
  28.    
  29.     for (float x=0;x<150;x++){
  30.         float delta = x/150;
  31.         ofVec3f signal = tempO.getInterpolated(tempD, delta);
  32.        
  33.         connections.addVertex(signal);
  34.         connections.addColor(0);
  35.     }
  36.    
  37.     connections.setMode(OF_PRIMITIVE_LINE_STRIP);
  38.     glLineWidth(1);
  39.     glEnable(GL_LINE_SMOOTH);
  40.     glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
  41.    
  42.     speed = 60; // Same as framerate
  43. }
  44.  
  45. //--------------------------------------------------------------
  46. void ofApp::update(){
  47.     int leadVertex = fmodf(ofGetElapsedTimef()*speed,150);
  48.    
  49.     for(int i=0; i<150; i++){
  50.         connections.setColor(i,ofColor(ofColor(255),0));
  51.     }
  52.    
  53.     for(int i=0, cc=50; i<50; i++,cc--){
  54.         connections.setColor(leadVertex-i,ofColor(ofColor(255),ofMap(cc,0,50,0,255)));
  55.     }
  56. }
  57.  
  58. //--------------------------------------------------------------
  59. void ofApp::draw(){
  60.     ofEnableDepthTest();
  61.     ofEnableAlphaBlending();
  62.     ofBackground(ofColor(0));
  63.    
  64.     ofDrawBitmapString("Speed = " + ofToString(speed),10,10);
  65.    
  66.     cam.begin();
  67.        
  68.         ofPushMatrix();
  69.        
  70.             ofTranslate((originP.x+destP.x)/2,(originP.y+destP.y)/2,(originP.z+destP.z)/2);
  71.            
  72.             ofVec3f direction = (destP - originP).getNormalized();
  73.             float rotationAmount;
  74.             ofVec3f rotationAngle;
  75.             ofQuaternion rotation;
  76.             ofVec3f axis(1, 0, 0);
  77.             rotation.makeRotate(axis, direction);
  78.             rotation.getRotate(rotationAmount, rotationAngle);
  79.             ofRotate(rotationAmount, rotationAngle.x, rotationAngle.y, rotationAngle.z);
  80.             connections.draw();
  81.        
  82.         ofPopMatrix();
  83.  
  84.     cam.end();
  85.    
  86. }
  87.  
  88. //--------------------------------------------------------------
  89. void ofApp::keyPressed(int key){
  90.     if(key=='f'){
  91.         for (float x=0;x<150;x++){
  92.             connections.setColor(x,ofColor(255));
  93.         }
  94.     }
  95.    
  96.     if(key=='-'){
  97.         speed--;
  98.     }
  99.    
  100.     if(key=='='){
  101.         speed++;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement