Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: demo now with 3 different fields

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