Guest User

oscThread.cpp

a guest
Dec 25th, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #include "oscThread.h"
  2.  
  3. //--------------------------------------------------------------
  4. oscThread::oscThread(ofVec2f origin, ofMesh mesh)
  5. {
  6. originP = mesh.getVertex(origin.x);
  7. destP = mesh.getVertex(origin.y);
  8.  
  9. length = sqrt(pow(destP.x-originP.x,2)+pow(destP.y-originP.y,2)+pow(destP.z-originP.z,2));
  10.  
  11. tempO.set(-length/2,0);
  12. tempD.set(length/2,0);
  13.  
  14. color = ofRandom(255);
  15. res = 200;
  16. speed = 5;
  17. amp = 10;
  18. f = 10;
  19.  
  20. numVerts = 0;
  21. tLength = 100;
  22. cSpeed = 40;
  23.  
  24. }
  25.  
  26. //--------------------------------------------------------------
  27. void oscThread::setup()
  28. {
  29. numVerts = this->getMesh().getNumVertices();
  30. time = ofGetElapsedTimef();
  31. mTime = time/speed;
  32.  
  33. for (float x=0;x<res;x++){
  34. float delta = x/res;
  35. float p = x;
  36. float oscillation = amp * sin((2 * pi * f * mTime + p)/20);
  37.  
  38. ofVec3f signal = tempO.getInterpolated(tempD, delta);
  39. signal.set(signal);
  40.  
  41. ofPushMatrix();
  42. signal.z = oscillation;
  43. ofPopMatrix();
  44.  
  45. this->getMesh().addVertex(signal);
  46. this->getMesh().addColor(color);
  47.  
  48. numVerts++;
  49. }
  50.  
  51. this->getMesh().setMode(OF_PRIMITIVE_LINE_STRIP);
  52. glLineWidth(1);
  53. glEnable(GL_LINE_SMOOTH);
  54. glHint(GL_LINE_SMOOTH_HINT,GL_NICEST);
  55. }
  56.  
  57. //--------------------------------------------------------------
  58. void oscThread::draw()
  59. {
  60. ofPushMatrix();
  61. ofTranslate((originP.x+destP.x)/2,(originP.y+destP.y)/2,(originP.z+destP.z)/2);
  62. direction = (destP - originP).getNormalized();
  63. axis.set(1, 0, 0);
  64. rotation.makeRotate(axis, direction);
  65. rotation.getRotate(rotationAmount, rotationAngle);
  66. ofRotate(rotationAmount, rotationAngle.x, rotationAngle.y, rotationAngle.z);
  67. transformGL();
  68. this->getMesh().draw();
  69. restoreTransformGL();
  70. ofPopMatrix();
  71. }
  72.  
  73. //--------------------------------------------------------------
  74. void oscThread::update()
  75. {
  76. time = ofGetElapsedTimef();
  77. mTime = time/speed;
  78. float dt = ofClamp(time-time0, 0, 0.1);
  79.  
  80. time0 = time;
  81.  
  82. if (leadVertex != res){
  83. lead += cSpeed * dt;
  84. }else{
  85. leadVertex = res;
  86. ttLength -= cSpeed * dt;
  87. }
  88.  
  89. if (lead > leadVertex){
  90. leadVertex = lead;
  91. }
  92.  
  93. if(leadVertex < tLength){
  94. ttLength = leadVertex;
  95. }else if(leadVertex != res){
  96. ttLength = tLength;
  97. }
  98.  
  99. if(ttLength == 0){
  100. dead = true;
  101. }
  102.  
  103. for (int x=0;x<res;x++){
  104. ofVec3f oscTemp = this->getMesh().getVertex(x);
  105. float p = x;
  106. float oscillation = amp * sin(2 * pi * f * mTime + p)/20;
  107. oscTemp.rotate(x/f,ofVec3f(1,0,0));
  108. this->getMesh().setVertex(x,oscTemp);
  109. this->getMesh().setColor(x,ofColor(ofColor(255),0));
  110. }
  111.  
  112. for(int i=0, tC=ttLength; i<ttLength; i++,tC--){
  113. this->getMesh().setColor(leadVertex-i,ofColor(color,color+i,color-i,ofMap(tC,0,50,0,255)));
  114. }
  115. }
Add Comment
Please, Sign In to add comment