Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8449 was 8449, checked in by hdavid, 18 years ago

branches/atmospheric_engine: new default values

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