Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: subproject Particles looks good again

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