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.6KB

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