Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: applying force works for particle-systems again

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