Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/lightening_effect.cc @ 8704

Last change on this file since 8704 was 8702, checked in by amaechler, 19 years ago

branches/atmospheric_engine: cleanup

File size: 8.5 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: hdavid, amaechler
13*/
14
15#include "lightening_effect.h"
16
17#include "state.h"
18
19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
21#include "util/loading/resource_manager.h"
22
23#include "effects/billboard.h"
24
25#include "glincl.h"
26#include "parser/tinyxml/tinyxml.h"
27
28#include "shell_command.h"
29#include "light.h"
30
31SHELL_COMMAND(activate, LighteningEffect, activateLightening);
32SHELL_COMMAND(deactivate, LighteningEffect, deactivateLightening);
33
34using namespace std;
35
36CREATE_FACTORY(LighteningEffect, CL_LIGHTENING_EFFECT);
37
38LighteningEffect::LighteningEffect(const TiXmlElement* root)
39{
40  this->setClassID(CL_LIGHTENING_EFFECT, "LighteningEffect");
41
42  this->init();
43
44  if (root != NULL)
45    this->loadParams(root);
46
47  if(this->lighteningActivate)
48    this->activate();
49}
50
51LighteningEffect::~LighteningEffect()
52{
53  this->deactivate();
54}
55
56void LighteningEffect::loadParams(const TiXmlElement* root)
57{
58  WeatherEffect::loadParams(root);
59
60  LoadParam(root, "coord", this, LighteningEffect, coord);
61  LoadParam(root, "frequency", this, LighteningEffect, setFlashFrequency);
62  LoadParam(root, "const-time", this, LighteningEffect, setFlashConstTime);
63  LoadParam(root, "rising-time", this, LighteningEffect, setFlashRisingTime);
64  LoadParam(root, "size", this, LighteningEffect, setFlashSize);
65  LoadParam(root, "seed", this, LighteningEffect, setFlashSeed);
66
67  LOAD_PARAM_START_CYCLE(root, element);
68  {
69    LoadParam_CYCLE(element, "option", this, LighteningEffect, setLighteningOption);
70  }
71  LOAD_PARAM_END_CYCLE(element);
72}
73
74
75void LighteningEffect::init()
76{
77  //default values
78  this->lighteningActivate = false;
79
80  this->flashFrequencyBase = 2.0f;
81  this->flashFrequencySeed = 4.0f;
82
83  this->flashHoldTime = 0.1f;
84  this->flashRisingTime = 0.03f;
85
86  this->seedX = 500.f;
87  this->seedZ = 1000.0f;
88
89  this->width = 700.0f;
90  this->height = 250.0f;
91  this->seedWidth = 50.0f;
92  this->seedHeight = 50.0f;
93
94  this->lighteningMove = false;
95
96  this->mainPosX = 2000;
97  this->mainPosY = 900;
98  this->mainPosZ = 0;
99
100  this->time = 0.0;
101
102  // initialize lightening textures
103  this->billboard[0] = new Billboard(NULL);
104  this->billboard[0]->setTexture("maps/lightning_bolt1.png");
105  this->billboard[0]->setSize(this->width, this->height);
106  this->billboard[0]->setVisibiliy(false);
107
108  this->billboard[1] = new Billboard(NULL);
109  this->billboard[1]->setTexture("maps/lightning_bolt2.png");
110  this->billboard[1]->setSize(this->width, this->height);
111  this->billboard[1]->setVisibiliy(false);
112
113  this->billboard[2] = new Billboard(NULL);
114  this->billboard[2]->setTexture("maps/lightning_bolt3.png");
115  this->billboard[2]->setSize(this->width, this->height);
116  this->billboard[2]->setVisibiliy(false);
117
118  this->billboard[3] = new Billboard(NULL);
119  this->billboard[3]->setTexture("maps/lightning_bolt4.png");
120  this->billboard[3]->setSize(this->width, this->height);
121  this->billboard[3]->setVisibiliy(false);
122
123  if (this->lighteningMove)
124  {
125    this->cameraCoor = State::getCameraNode()->getAbsCoor();
126    this->billboard[0]->setAbsCoor(this->cameraCoor.x+3000,900,this->cameraCoor.z+0);
127    this->billboard[1]->setAbsCoor(this->cameraCoor.x+3000,900,this->cameraCoor.z+0);
128    this->billboard[2]->setAbsCoor(this->cameraCoor.x+3000,900,this->cameraCoor.z+0);
129    this->billboard[3]->setAbsCoor(this->cameraCoor.x+3000,900,this->cameraCoor.z+0);
130  }
131  else
132  {
133    this->billboard[0]->setAbsCoor(3000,900,0);
134    this->billboard[1]->setAbsCoor(3000,900,0);
135    this->billboard[2]->setAbsCoor(3000,900,0);
136    this->billboard[3]->setAbsCoor(3000,900,0);
137  }
138
139  this->flashLight = new Light();
140  this->flashLight->setDiffuseColor(0,0,0);
141  this->flashLight->setSpecularColor(0,0,0);
142
143  //load sound
144  if (this->thunderBuffer != NULL)
145    ResourceManager::getInstance()->unload(this->thunderBuffer);
146  this->thunderBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/thunder.ogg", OGG);
147
148}
149
150void LighteningEffect::coord(float x, float y, float z)
151{
152  if (this->lighteningMove)
153  {
154    this->cameraCoor = State::getCameraNode()->getAbsCoor();
155    this->billboard[0]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
156    this->billboard[1]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
157    this->billboard[2]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
158    this->billboard[3]->setAbsCoor(this->cameraCoor.x+x, y, this->cameraCoor.z+z);
159  }
160  else
161  {
162    this->billboard[0]->setAbsCoor(x, y, z);
163    this->billboard[1]->setAbsCoor(x, y, z);
164    this->billboard[2]->setAbsCoor(x, y, z);
165    this->billboard[3]->setAbsCoor(x, y, z);
166  }
167
168  this->mainPosX = x;
169  this->mainPosY = y;
170  this->mainPosZ = z;
171}
172
173
174void LighteningEffect::setFlashSize(float width, float height, float seedWidth, float seedHeight)
175{
176  this->width = width;
177  this->height = height;
178  this->seedWidth = seedWidth;
179  this->seedHeight = seedHeight;
180
181  this->billboard[0]->setSize(this->width, this->height);
182  this->billboard[1]->setSize(this->width, this->height);
183  this->billboard[2]->setSize(this->width, this->height);
184  this->billboard[3]->setSize(this->width, this->height);
185}
186
187
188void LighteningEffect::activate()
189{
190  PRINTF(0)( "Activating LighteningEffect\n" );
191  this->lighteningActivate = true;
192
193  this->time = 0;
194}
195
196
197void LighteningEffect::deactivate()
198{
199  PRINTF(0)("Deactivating LighteningEffect\n");
200  this->lighteningActivate = false;
201
202  this->billboard[0]->setVisibiliy(false);
203  this->billboard[1]->setVisibiliy(false);
204  this->billboard[2]->setVisibiliy(false);
205  this->billboard[3]->setVisibiliy(false);
206}
207
208void LighteningEffect::tick (float dt)
209{
210  if(!lighteningActivate)
211    return;
212
213  this->time += dt;
214
215  float x;
216  x = (float)rand()/(float)RAND_MAX;
217
218  // TODO: Make random flashing with short light dingsda:)
219
220  if( this->time > this->flashFrequency)
221  {
222    // Reset timer
223    this->time = 0.0f;
224
225    // Move billboard to start position
226    this->flashLight->setAbsCoor(this->billboard[0]->getAbsCoor().x, this->billboard[0]->getAbsCoor().y, this->billboard[0]->getAbsCoor().z);
227
228    // Start a flash & lightening cycle
229    this->billboard[0]->setVisibiliy(true);
230
231    // Lighten up environment
232    this->flashLight->setDiffuseColor(1,1,1);
233    this->flashLight->setSpecularColor(1,1,1);
234
235    // Play thunder sound
236    this->soundSource.play(this->thunderBuffer);
237  }
238
239  if( this->billboard[0]->isVisible() && this->time > this->flashRisingTime*1/3 )
240  {
241    this->billboard[0]->setVisibiliy(false);
242    this->billboard[1]->setVisibiliy(true);
243  }
244  else if( this->billboard[1]->isVisible() && this->time > this->flashRisingTime*2/3 )
245  {
246    this->billboard[1]->setVisibiliy(false);
247    this->billboard[2]->setVisibiliy(true);
248
249  }
250  else if( this->billboard[2]->isVisible() && this->time > this->flashRisingTime)
251  {
252    this->billboard[2]->setVisibiliy(false);
253    this->billboard[3]->setVisibiliy(true);
254  }
255
256  if( this->billboard[3]->isVisible() && this->time > this->flashHoldTime)
257  {
258    this->billboard[3]->setVisibiliy(false);
259    this->time = 0.0f;
260    this->flashLight->setDiffuseColor(0,0,0);
261    this->flashLight->setSpecularColor(0,0,0);
262
263    this->newCoordinates();
264  }
265}
266
267void LighteningEffect::newCoordinates() {
268
269    float posX, posZ;
270
271    if(this->lighteningMove)
272    {
273
274      this->cameraCoor = State::getCameraNode()->getAbsCoor();
275      posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX + this->cameraCoor.x;
276      posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX + this->cameraCoor.z;
277
278    }
279    else
280    {
281
282      posX = this->mainPosX - this->seedX * (float)rand()/(float)RAND_MAX;
283      posZ = this->mainPosZ + this->seedZ * (float)rand()/(float)RAND_MAX;
284
285    }
286
287    this->billboard[0]->setAbsCoor(posX, this->mainPosY, posZ);
288    this->billboard[1]->setAbsCoor(posX, this->mainPosY, posZ);
289    this->billboard[2]->setAbsCoor(posX, this->mainPosY, posZ);
290    this->billboard[3]->setAbsCoor(posX, this->mainPosY, posZ);
291
292    this->flashFrequency = this->flashFrequencyBase + this->flashFrequencySeed * (float)rand()/(float)RAND_MAX;
293
294    float w = this->width + this->seedWidth * (float)rand()/(float)RAND_MAX;
295    float h = this->height + this->seedHeight * (float)rand()/(float)RAND_MAX;
296
297    this->billboard[0]->setSize(w, h);
298    this->billboard[1]->setSize(w, h);
299    this->billboard[2]->setSize(w, h);
300    this->billboard[3]->setSize(w, h);
301}
Note: See TracBrowser for help on using the repository browser.