Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: preparing to add fields to the particles-demo

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