Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/subprojects/particles/particle_fun.cc @ 4666

Last change on this file since 4666 was 4666, checked in by bensch, 19 years ago

orxonox/trunk: particle-demo now also shows models from the model-directory

File size: 14.7 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14
15   this file extends the framework file, so it renders what i want.
16*/
17
18#include "framework.h"
19
20#include "physics_engine.h"
21#include "particle_engine.h"
22#include "fields.h"
23#include "stdlibincl.h"
24#include "graphics_engine.h"
25
26#include <dirent.h>
27
28#define PINIT_EMISSION_RATE          50
29#define PINIT_EMISSION_VELOCITY      5.0
30#define PINIT_SPREAD_ANGLE           0.1
31#define PINIT_EMITTER_TYPE           EMITTER_DOT
32#define PINIT_EMITTER_SIZE           1.0
33#define PINIT_START_RADIUS           5.0
34#define PINIT_END_RADIUS             0.0
35#define PINIT_LIFESPAN               1.0
36#define PINIT_CONSERVE_FACTOR        1.0
37#define PINIT_PARTICLE_TYPE          PARTICLE_SPRITE
38#define PINIT_INHERIT_SPEED          0.0
39#define PINIT_PARTICLE_MASS          1.0
40#define PINIT_MODEL_DIRECTORY        "models/"
41
42
43Field* twirl;
44Field* gravity;
45Field* pointGravity;
46
47void Framework::moduleInit(int argc, char** argv)
48{
49  GraphicsEngine::getInstance()->setWindowName("ParticlesFun", "particles");
50
51  verbose = 5;
52  ParticleEngine::getInstance();
53  PhysicsEngine::getInstance();
54
55
56  // Creating a Test Particle System
57
58  ParticleSystem* system = new ParticleSystem(100000, PINIT_PARTICLE_TYPE);
59  system->setRadius(0, PINIT_START_RADIUS, 0 );
60  system->setRadius(1, PINIT_END_RADIUS, 0 );
61  system->setLifeSpan(PINIT_LIFESPAN);
62
63  system->setConserve(PINIT_CONSERVE_FACTOR);
64  //system->setMass(5,3,6);
65
66  // Creating a Test Particle Emitter
67  ParticleEmitter* emitter = new ParticleEmitter(Vector(0 , 1, 0));
68  emitter->setEmissionRate(PINIT_EMISSION_RATE);
69  emitter->setEmissionVelocity(PINIT_EMISSION_VELOCITY ,0);
70  emitter->setSpread(PINIT_SPREAD_ANGLE ,0);
71  emitter->setType(PINIT_EMITTER_TYPE);
72  emitter->setSize(PINIT_EMITTER_SIZE);
73  emitter->setAbsCoor(Vector(3,0,0));
74  // Add the Flow from the Emitter into the System
75  ParticleEngine::getInstance()->addConnection(emitter, system);
76
77
78  twirl = new Twirl();
79  twirl->setMagnitude(0);
80  gravity = new Gravity();
81  gravity->setMagnitude(0);
82  pointGravity = new PointGravity();
83  pointGravity->setMagnitude(0);
84
85  new PhysicsConnection(system, gravity);
86  new PhysicsConnection(system, twirl);
87  new PhysicsConnection(system, pointGravity);
88}
89
90void Framework::moduleEventHandler(SDL_Event* event)
91{
92  switch (event->type)
93    {
94    case SDL_KEYDOWN:
95      switch (event->key.keysym.sym)
96        {
97        case SDLK_i:
98          ParticleEngine::getInstance()->debug();
99          PhysicsEngine::getInstance()->debug();
100          break;
101        }
102    }
103}
104
105void Framework::moduleTick(float dt)
106{
107  PhysicsEngine::getInstance()->tick(dt);
108  ParticleEngine::getInstance()->tick(dt);
109}
110
111void Framework::moduleDraw() const
112{
113  ParticleEngine::getInstance()->draw();
114}
115
116
117void Framework::moduleHelp(void) const
118{
119  PRINT(0)("\n");
120  PRINT(0)("i - Particle-state Information\n\n");
121  PRINT(0)("\n");
122}
123
124int emitterChange(GtkWidget* nonInterest, void* widget)
125{
126  Option* option = (Option*) widget;
127  const char* name = option->getTitle();
128  char* value = option->save();
129
130  ParticleEmitter* tmpEmit = ParticleEngine::getInstance()->getEmitterByNumber(1);
131  if (tmpEmit)
132    {
133      if (!strcmp(name, "EmissionRate"))
134        {
135          tmpEmit->setEmissionRate(atof(value));
136          PRINT(4)("EmissionRate set to %f\n", atof(value));
137        }
138      else if (!strcmp(name, "Velocity"))
139        {
140          tmpEmit->setEmissionVelocity(atof(value));
141          PRINT(4)("Velocity set to %f\n", atof(value));
142        }
143      else if(!strcmp(name, "SpreadAngle"))
144        {
145          tmpEmit->setSpread(atof(value), 0);
146          PRINT(4)("SpreadAngle set to %f\n", atof(value));
147        }
148      else if(!strcmp(name, "EmitterType"))
149        {
150          if (!strcmp(value, "EMITTER_DOT"))
151            tmpEmit->setType(EMITTER_DOT);
152          else if (!strcmp(value, "EMITTER_PLANE"))
153            tmpEmit->setType(EMITTER_PLANE);
154          else if (!strcmp(value, "EMITTER_CUBE"))
155            tmpEmit->setType(EMITTER_CUBE);
156          PRINT(4)("EmitterType set to %s\n", value);
157        }
158      else if(!strcmp(name, "EmitterSize"))
159        {
160          tmpEmit->setSize(atof(value));
161          PRINT(4)("EmitterSize set to %f\n", atof(value));
162        }
163        else if (!strcmp(name, "InheritSpeed"))
164        {
165          tmpEmit->setInheritSpeed(atof(value));
166          PRINT(4)("ParticleInheritSpeed set to %f\n", atof(value));
167        }
168    }
169  delete value;
170}
171
172
173int systemChange(GtkWidget* nonInterest, void* widget)
174{
175  Option* option = (Option*) widget;
176  const char* name = option->getTitle();
177  char* value = option->save();
178
179  ParticleSystem* tmpSys = ParticleEngine::getInstance()->getSystemByNumber(1);
180  if (tmpSys)
181    {
182      if (!strcmp(name, "StartRadius"))
183        {
184          tmpSys->setRadius(0, atof(value));
185          PRINT(4)("ParticleStartRadius set to %f\n", atof(value));
186        }
187      else if (!strcmp(name, "EndRadius"))
188        {
189          tmpSys->setRadius( 1, atof(value));
190          PRINT(4)("ParticleEndRadius set to %f\n", atof(value));
191        }
192
193      else if (!strcmp(name, "LifeSpan"))
194        {
195          tmpSys->setLifeSpan(atof(value));
196          PRINT(4)("ParticleLifeSpan set to %f\n", atof(value));
197        }
198
199      else if (!strcmp(name, "Mass"))
200        {
201          tmpSys->setMass(0, atof(value));
202          PRINT(4)("ParticleMass set to %f\n", atof(value));
203        }
204
205      else if (!strcmp(name, "ConserveFactor"))
206        {
207          tmpSys->setConserve(atof(value));
208          PRINT(4)("ParticleConserveFactor set to %f\n", atof(value));
209        }
210
211      else if (!strcmp(name, "ParticleType"))
212        {
213          if (!strcmp(value, "PARTICLE_DOT"))
214            tmpSys->setType(PARTICLE_DOT);
215          else if (!strcmp(value, "PARTICLE_SPARK"))
216            tmpSys->setType(PARTICLE_SPARK);
217          else if (!strcmp(value, "PARTICLE_SPRITE"))
218            tmpSys->setType(PARTICLE_SPRITE);
219
220          PRINT(4)("ParticleType set to %s\n", value);
221        }
222        else if(!strcmp(name, "ParticleModel"))
223        {
224          char* modelName = new char[strlen(PINIT_MODEL_DIRECTORY) + strlen(value)+1];
225          sprintf(modelName, "%s%s", PINIT_MODEL_DIRECTORY, value);
226          tmpSys->setModel(ResourceManager::getFullName(modelName));
227          delete modelName;
228        }
229      else if (!strcmp(name, "RandomColor"))
230        {
231          tmpSys->setColor(0, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, 1);
232          tmpSys->setColor(.5, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, .5);
233          tmpSys->setColor(.5, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, (float)rand()/RAND_MAX, 0);
234        }
235    }
236  delete value;
237}
238
239int fieldsChange(GtkWidget* nonInterest, void* widget)
240{
241  Option* option = (Option*) widget;
242  const char* name = option->getTitle();
243  char* value = option->save();
244
245
246  if (!strcmp(name, "Gravity"))
247    {
248      gravity->setMagnitude(atof(value));
249    }
250
251  else if (!strcmp(name, "Twirl"))
252    {
253      twirl->setMagnitude(atof(value));
254    }
255
256  else if (!strcmp(name, "PointGravity"))
257    {
258      pointGravity->setMagnitude(atof(value));
259    }
260
261}
262
263void Framework::moduleInitGui(int argc, char** argv)
264{
265  Window* guiMainWindow = NULL;
266
267  initGUI(0, NULL);
268
269  guiMainWindow = new Window("ParticlesFUN");
270  {
271    Box* windowBox = new Box('h');
272    {
273      Frame* emitterFrame = new Frame("emitter-settings");
274      {
275        Box* emitterBox = new Box('v');
276        {
277          emitterBox->fill(new Label("EmissionRate"));
278          Slider* EmissionRate = new Slider("EmissionRate", 0, 1000);
279          EmissionRate->connectSignal("value_changed", (void*)EmissionRate, emitterChange );
280          EmissionRate->setValue(PINIT_EMISSION_RATE);
281          EmissionRate->redraw();
282          emitterBox->fill(EmissionRate);
283
284          emitterBox->fill(new Label("Velocity"));
285          Slider* velocity = new Slider("Velocity", 0, 20);
286          velocity->setExactness(2);
287          velocity->connectSignal("value_changed", (void*)velocity, emitterChange );
288          velocity->setValue(PINIT_EMISSION_VELOCITY);
289          velocity->redraw();
290          emitterBox->fill(velocity);
291
292          emitterBox->fill(new Label("SpreadAngle"));
293          Slider* SpreadAngle = new Slider("SpreadAngle", 0, M_PI);
294          SpreadAngle->setExactness(3);
295          SpreadAngle->connectSignal("value_changed", (void*)SpreadAngle, emitterChange );
296          SpreadAngle->setValue(PINIT_SPREAD_ANGLE);
297          SpreadAngle->redraw();
298          emitterBox->fill(SpreadAngle);
299
300          emitterBox->fill(new Label("EmitterType"));
301          Menu* EmitterType = new Menu("EmitterType");
302          EmitterType->addItem("EMITTER_DOT");
303          EmitterType->addItem("EMITTER_PLANE");
304          EmitterType->addItem("EMITTER_CUBE");
305          EmitterType->connectSignal("changed", (void*)EmitterType, emitterChange );
306          EmitterType->load("EMITTER_DOT");
307          emitterBox->fill(EmitterType);
308
309          emitterBox->fill(new Label("EmitterSize"));
310          Slider* EmitterSize = new Slider("EmitterSize", 0, 100);
311          EmitterSize->setExactness(1);
312          EmitterSize->connectSignal("value_changed", (void*)EmitterSize, emitterChange );
313          EmitterSize->setValue(PINIT_EMITTER_SIZE);
314          EmitterSize->redraw();
315          emitterBox->fill(EmitterSize);
316
317          emitterBox->fill(new Label("InheritSpeed"));
318          Slider* InheritSpeed = new Slider("InheritSpeed", 0, 1);
319          InheritSpeed->setExactness(3);
320          InheritSpeed->connectSignal("value_changed", (void*)InheritSpeed, emitterChange );
321          emitterBox->fill(InheritSpeed);
322        }
323        emitterFrame->fill(emitterBox);
324      }
325      windowBox->fill(emitterFrame);
326
327      Frame* systemFrame = new Frame("system-settings");
328      {
329        Box* systemBox = new Box('v');
330        {
331          systemBox->fill(new Label("StartRadius"));
332          Slider* StartRadius = new Slider("StartRadius", 0, 10);
333          StartRadius->setExactness(3);
334          StartRadius->connectSignal("value_changed", (void*)StartRadius, systemChange );
335          StartRadius->setValue(PINIT_START_RADIUS);
336          StartRadius->redraw();
337          systemBox->fill(StartRadius);
338
339          systemBox->fill(new Label("EndRadius"));
340          Slider* EndRadius = new Slider("EndRadius", 0, 10);
341          EndRadius->setExactness(3);
342          EndRadius->connectSignal("value_changed", (void*)EndRadius, systemChange );
343          EndRadius->setValue(PINIT_END_RADIUS);
344          EndRadius->redraw();
345          systemBox->fill(EndRadius);
346
347          systemBox->fill(new Label("ParticleMass"));
348          Slider* Mass = new Slider("Mass", 0, 10);
349          Mass->setExactness(2);
350          Mass->connectSignal("value_changed", (void*)Mass, systemChange );
351          Mass->setValue(PINIT_PARTICLE_MASS);
352          Mass->redraw();
353          systemBox->fill(Mass);
354
355
356          systemBox->fill(new Label("LifeSpan"));
357          Slider* LifeSpan = new Slider("LifeSpan", 0, 10);
358          LifeSpan->setExactness(3);
359          LifeSpan->connectSignal("value_changed", (void*)LifeSpan, systemChange );
360          LifeSpan->setValue(PINIT_LIFESPAN);
361          LifeSpan->redraw();
362          systemBox->fill(LifeSpan);
363
364          systemBox->fill(new Label("ConserveFactor"));
365          Slider* ConserveFactor = new Slider("ConserveFactor", 0, 1);
366          ConserveFactor->setExactness(3);
367          ConserveFactor->connectSignal("value_changed", (void*)ConserveFactor, systemChange );
368          ConserveFactor->setValue(PINIT_CONSERVE_FACTOR);
369          ConserveFactor->redraw();
370          systemBox->fill(ConserveFactor);
371
372          systemBox->fill(new Label("ParticleType"));
373          Menu* ParticleType = new Menu("ParticleType");
374          ParticleType->addItem("PARTICLE_DOT");
375          ParticleType->addItem("PARTICLE_SPARK");
376          ParticleType->addItem("PARTICLE_SPRITE");
377          ParticleType->connectSignal("changed", (void*)ParticleType, systemChange );
378          ParticleType->load("PARTICLE_SPRITE");
379          systemBox->fill(ParticleType);
380
381          systemBox->fill(new Label("ParticleModel"));
382          Menu* ParticleModel = new Menu("ParticleModel");
383          {
384            DIR* directory;
385            directory = opendir(ResourceManager::getFullName(PINIT_MODEL_DIRECTORY));
386            dirent* file;
387            while(file = readdir(directory))
388            {
389              printf("%s\n", file->d_name);
390              if(strstr(file->d_name, ".obj"))
391                 ParticleModel->addItem(file->d_name);
392            }
393          }
394          ParticleModel->connectSignal("changed", (void*)ParticleModel, systemChange );
395          systemBox->fill(ParticleModel);
396
397
398          Button* RandomColor = new Button("RandomColor");
399          RandomColor->connectSignal("released", (void*)RandomColor, systemChange);
400          systemBox->fill(RandomColor);
401
402        }
403        systemFrame->fill(systemBox);
404      }
405      windowBox->fill(systemFrame);
406
407      Frame* fieldsFrame = new Frame("Field-Settings");
408      {
409        Box* fieldsBox = new Box('v');
410        {
411          fieldsBox->fill(new Label("Gravity"));
412          Slider* Gravity = new Slider("Gravity", 0, 10);
413          Gravity->setExactness(1);
414          Gravity->connectSignal("value_changed", (void*)Gravity, fieldsChange );
415          Gravity->setValue(0);
416          Gravity->redraw();
417          fieldsBox->fill(Gravity);
418
419
420          fieldsBox->fill(new Label("Twirl"));
421          Slider* Twirl = new Slider("Twirl", 0, 10);
422          Twirl->setExactness(1);
423          Twirl->connectSignal("value_changed", (void*)Twirl, fieldsChange );
424          Twirl->setValue(0);
425          Twirl->redraw();
426          fieldsBox->fill(Twirl);
427
428
429          fieldsBox->fill(new Label("PointGravity"));
430          Slider* PointGravity = new Slider("PointGravity", 0, 10);
431          PointGravity->setExactness(1);
432          PointGravity->connectSignal("value_changed", (void*)PointGravity, fieldsChange );
433          PointGravity->setValue(0);
434          PointGravity->redraw();
435          fieldsBox->fill(PointGravity);
436
437        }
438        fieldsFrame->fill(fieldsBox);
439      }
440      windowBox->fill(fieldsFrame);
441
442      Button* quitButton = new Button("quit");
443
444      quitButton->connectSignal("clicked", NULL, quitGui);
445      //  Window::mainWindow->connectSignal("remove", this, GuiExec::quitGui);
446      Window::mainWindow->connectSignal("destroy", NULL, quitGui);
447      windowBox->fill(quitButton);
448
449    }
450    guiMainWindow->fill(windowBox);
451  }
452  Window::mainWindow->showall();
453  Window::mainWindow->setSize(300, 500);
454}
Note: See TracBrowser for help on using the repository browser.