Quelcode des Partikelsystems Boden
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ofApp.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #include "ofApp.h"
  2. //--------------------------------------------------------------
  3. void ofApp::setup() {
  4. ofSetVerticalSync(true);
  5. //Initialize 8 empty attractrs
  6. for (int i = 0; i < 8; i++)
  7. {
  8. attractors.push_back(new Attractor);
  9. }
  10. //OSC reciever port setup
  11. receiver.setup(PORT);
  12. varSystem = true;
  13. trails = true;
  14. history = 0.95;
  15. num = 1000;
  16. sys.assign(num, Particle());
  17. currentMode = PARTICLE_MODE_DEFAULT;
  18. currentModeDisp = "PARTICLE_MODE_DEFAULT";
  19. //Black background
  20. ofSetBackgroundColor(0, 0, 0);
  21. ofSetFrameRate(60);
  22. birthCount = 0;
  23. int w = ofGetWidth();
  24. int h = ofGetHeight();
  25. fbo.allocate(w, h, GL_RGB);
  26. //attractors.at(0)->setup(600, 600);
  27. attractors.at(1)->setup(200, 200);
  28. attractors.at(2)->setup(400, 400);
  29. attractors.at(3)->setup(500, 500);
  30. }
  31. //--------------------------------------------------------------
  32. void ofApp::update() {
  33. // *** OSC RECEIVER ***
  34. while (receiver.hasWaitingMessages()) {
  35. ofxOscMessage contourCentroids;
  36. receiver.getNextMessage(&contourCentroids);
  37. oscMsg = ofToString(contourCentroids);
  38. //Get active ammount of attractors (nBlobs) and their x & y coordinates
  39. //Exp. OSC Message: " /centroidsOfBlob ammount xValue, yValue"
  40. if (contourCentroids.getAddress() == "/centroidsOfBlob") {
  41. nBlobs = contourCentroids.getArgAsInt(0);
  42. for (int i = 1; i <= nBlobs; i++) {
  43. xOfCentroid = contourCentroids.getArgAsFloat(i * 2 - 1) * ofGetWindowWidth();
  44. yOfCentroid = contourCentroids.getArgAsFloat(i * 2) * ofGetWindowHeight();
  45. attractors.at(i - 1)->setup(xOfCentroid, yOfCentroid);
  46. }
  47. }
  48. //Delete all Attractors on command
  49. if (contourCentroids.getAddress() == "/checkin") {
  50. for (int i = 0; i < 8; i++) {
  51. attractors.at(i)->setup(0, 0);
  52. }
  53. }
  54. }
  55. // *** PARTICLE EMITTER ***
  56. //Capture time based on FrameTime
  57. double deltaT = ofGetLastFrameTime();
  58. birthCount += deltaT;
  59. if (varSystem == true) {
  60. //Birth control for new particles
  61. if (birthCount > 0.001) {
  62. for (int i = 0;i < 4;i++) {
  63. system.push_back(new Particle);
  64. system.back()->setup(currentMode);
  65. }
  66. birthCount = 0;
  67. }
  68. for (int p = 0; p < system.size();)
  69. {
  70. //Upate particle system /w all active attractors
  71. system.at(p)->update(deltaT, &attractors, system);
  72. //Delete particles, that reached max Age
  73. if (system.at(p)->getAgeNorm() > 4) {
  74. delete system.at(p);
  75. system.erase(system.begin() + p);
  76. }
  77. else {
  78. p++;
  79. }
  80. }
  81. }
  82. else {
  83. ofSeedRandom(39);
  84. }
  85. }
  86. //--------------------------------------------------------------
  87. void ofApp::reset() {
  88. for (int p = 0; p < system.size();)
  89. {
  90. system.erase(system.begin() + p);
  91. }
  92. }
  93. //--------------------------------------------------------------
  94. void ofApp::draw() {
  95. if (varSystem == false) {
  96. vector<ofVec2f> system;
  97. for (int i = 0; i < 1000; i++) {
  98. ofVec2f particle(ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.0005), 0, 1, 0, ofGetWidth()), ofMap(ofNoise(ofRandom(100), ofGetFrameNum() * 0.0005), 0, 1, 0, ofGetHeight()));
  99. system.push_back(particle);
  100. }
  101. for (int out = 0; out < system.size(); out++) {
  102. for (int in = out + 1; in < system.size(); in++) {
  103. if (system[out].distance(system[in]) < 60) {
  104. ofDrawLine(system[out], system[in]);
  105. }
  106. }
  107. }
  108. for (int i = 0; i < system.size(); i++) {
  109. ofDrawCircle(system[i], ofRandom(0.5, 3));
  110. }
  111. }
  112. if (trails == true) {
  113. fbo.begin();
  114. ofEnableAlphaBlending(); //Enable transparency
  115. float alpha = (1 - history) * 255;
  116. ofSetColor(0, 0, 0, alpha);
  117. ofFill();
  118. ofRect(0, 0, ofGetWidth(), ofGetHeight());
  119. ofDisableAlphaBlending(); //Disable transparency
  120. //Draw particle system
  121. for (int p = 0; p < system.size(); p++) {
  122. system.at(p)->draw();
  123. }
  124. fbo.end();
  125. }
  126. else {
  127. //Draw particle system
  128. for (int p = 0; p < system.size(); p++) {
  129. system.at(p)->draw();
  130. }
  131. }
  132. //Capture time based on FrameTime
  133. double deltaT = ofGetLastFrameTime();
  134. time += deltaT;
  135. // Delete inactive attractors after 4 seconds based on Frametime
  136. if (time > 2) {
  137. for (int i = 0; i < 8; i++) {
  138. //attractors.at(i)->setup(0, 0);
  139. time = 0;
  140. }
  141. }
  142. fbo.draw(0, 0);
  143. // *** DEBUG INFO ***
  144. ofSetColor(230);
  145. //All 8 Attractors with x | y coordinates
  146. for (int i = 0; i < 8; i++) {
  147. string x = ofToString(attractors.at(i)->getX());
  148. string y = ofToString(attractors.at(i)->getY());
  149. ofDrawBitmapString("x: " + x + " y: " + y, 100, 100 + i * 20);
  150. }
  151. //Recieved OSC messages
  152. ofDrawBitmapString("OSC: " + ofToString(oscMsg),100, 275);
  153. //Elapsed time since last clear of attractors
  154. ofDrawBitmapString("Time: " + ofToString(time),100, 300);
  155. //Current FPS
  156. ofDrawBitmapString("FPS: " + ofToString(ofGetFrameRate()), 100, 325);
  157. //Current Mode
  158. ofDrawBitmapString("Current Effect: " + currentModeDisp, 100, 350);
  159. }
  160. //--------------------------------------------------------------
  161. void ofApp::keyPressed(int key) {
  162. //Key press 1 to trigger default particle Mode
  163. if (key == '1') {
  164. currentMode = PARTICLE_MODE_DEFAULT;
  165. currentModeDisp = "PARTICLE_MODE_DEFAULT";
  166. varSystem = true;
  167. reset();
  168. }
  169. //Key press 2 to trigger attractor particle Mode
  170. if (key == '2') {
  171. currentMode = PARTICLE_MODE_RADIAL;
  172. currentModeDisp = "PARTICLE_MODE_RADIAL";
  173. varSystem = true;
  174. reset();
  175. }
  176. //Key press 2 to trigger rain particle Mode
  177. if (key == '3') {
  178. currentMode = PARTICLE_MODE_RAIN;
  179. currentModeDisp = "PARTICLE_MODE_RAIN";
  180. varSystem = false;
  181. reset();
  182. }
  183. //Key press 2 to trigger attractor particle Mode
  184. if (key == '4') {
  185. currentMode = PARTICLE_MODE_ATTRACTOR;
  186. currentModeDisp = "PARTICLE_MODE_ATTRACTOR";
  187. varSystem = true;
  188. reset();
  189. }
  190. //Key press 2 to trigger attractor particle Mode
  191. if (key == '5') {
  192. currentMode = PARTICLE_MODE_DETRACTOR;
  193. currentModeDisp = "PARTICLE_MODE_DETRACTOR";
  194. varSystem = true;
  195. reset();
  196. }
  197. //Key press 2 to trigger attractor particle Mode
  198. if (key == '6') {
  199. currentMode = PARTICLE_MODE_POLY;
  200. currentModeDisp = "PARTICLE_MODE_POLY";
  201. varSystem = false;
  202. reset();
  203. }
  204. if (key == '7') {
  205. trails = true;
  206. reset();
  207. }
  208. }
  209. //--------------------------------------------------------------
  210. void ofApp::keyReleased(int key) {
  211. }
  212. //--------------------------------------------------------------
  213. void ofApp::mouseMoved(int x, int y) {
  214. attractors.at(0)->setup(ofGetMouseX(), ofGetMouseY());
  215. }
  216. //--------------------------------------------------------------
  217. void ofApp::mousePressed(int x, int y, int button) {
  218. }
  219. //--------------------------------------------------------------
  220. void ofApp::mouseReleased(int x, int y, int button) {
  221. }