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