Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: lighting model is corrected now in the Particle-engine

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