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