Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/rain_effect.cc @ 8453

Last change on this file since 8453 was 8443, checked in by amaechler, 18 years ago

branches/atmospheric_engine: fade work

File size: 7.8 KB
Line 
1/*
2orxonox - the future of 3D-vertical-scrollers
3
4Copyright (C) 2004 orx
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11### File Specific:
12main-programmer: hdavid, amaechler
13*/
14
15#include "rain_effect.h"
16
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
19#include "util/loading/resource_manager.h"
20
21#include "glincl.h"
22#include "p_node.h"
23#include "state.h"
24#include "spark_particles.h"
25#include "plane_emitter.h"
26#include "shell_command.h"
27#include "light.h"
28
29#include "parser/tinyxml/tinyxml.h"
30
31SHELL_COMMAND(activate, RainEffect, activateRain);
32SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
33SHELL_COMMAND(startraining, RainEffect, startRaining);
34SHELL_COMMAND(stopraining, RainEffect, stopRaining);
35
36using namespace std;
37
38CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
39
40/* TODO:
41        - preCaching
42        - test multiple rain emitters
43        - Think about what happens with building poss. to hang movewithcam off
44        - Possible to activate lightening
45        - turn off visibility when in a building
46        - variable emitter size depending on playable
47*/
48
49RainEffect::RainEffect(const TiXmlElement* root)
50{
51        this->setClassID(CL_RAIN_EFFECT, "RainEffect");
52
53        this->init();
54
55        if (root != NULL)
56                this->loadParams(root);
57
58        //load rain sound
59        if (this->rainBuffer != NULL)
60                ResourceManager::getInstance()->unload(this->rainBuffer);
61        this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
62
63        //load wind sound
64        if (this->rainWindForce != 0) {
65                if (this->windBuffer != NULL)
66                        ResourceManager::getInstance()->unload(this->windBuffer);
67                this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
68        }
69
70        if(rainActivate)
71                this->activate();
72}
73
74RainEffect::~RainEffect()
75{
76        this->deactivate();
77
78        if (this->rainBuffer != NULL)
79                ResourceManager::getInstance()->unload(this->rainBuffer);
80
81        if (this->windBuffer != NULL)
82                ResourceManager::getInstance()->unload(this->windBuffer);
83}
84
85void RainEffect::loadParams(const TiXmlElement* root)
86{
87        WeatherEffect::loadParams(root);
88
89        LoadParam(root, "coord", this, RainEffect, setRainCoord);
90        LoadParam(root, "size", this, RainEffect, setRainSize);
91        LoadParam(root, "rate", this, RainEffect, setRainRate);
92        LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
93        LoadParam(root, "life", this, RainEffect, setRainLife);
94        LoadParam(root, "wind", this, RainEffect, setRainWind);
95        LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
96        LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
97
98        LOAD_PARAM_START_CYCLE(root, element);
99        {
100                LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
101        }
102        LOAD_PARAM_END_CYCLE(element);
103
104}
105
106
107bool RainEffect::init()
108{
109        //Default values
110        this->rainActivate = false;
111        this->rainMove = false;
112        this->rainCoord = Vector(500, 500, 500);
113        this->rainSize = Vector2D(1000, 1000);
114        this->rainRate = 4000;
115        this->rainVelocity = -300;
116        this->rainLife = 4;
117        this->rainMaxParticles = this->rainRate * this->rainLife;
118        this->rainWindForce  = 0;
119        this->rainFadeInDuration = 0;
120        this->rainFadeOutDuration = 0;
121        this->localTimer = 0;
122        this->soundRainVolume = 0.8f;
123
124        this->emitter = new PlaneEmitter(this->rainSize);
125
126        lightMan = LightManager::getInstance();
127        this->rainAmbient = lightMan->getAmbientColor();
128
129        return 0;
130}
131
132
133SparkParticles* RainEffect::rainParticles = NULL;
134
135bool RainEffect::activate()
136{
137        PRINTF(0)( "Activating RainEffect, coord: %f, %f, %f, size: %f, %f, rate: %f, velocity: %f, moveRain: %s\n", this->rainCoord.x, this->rainCoord.y, this->rainCoord.z, this->rainSize.x, this-> rainSize.y, this->rainRate, this->rainVelocity, this->rainMove ? "true" : "false" );
138
139        this->rainActivate = true;
140
141        if (unlikely(RainEffect::rainParticles == NULL))
142        {
143                RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
144                RainEffect::rainParticles->setName("RainParticles");
145                RainEffect::rainParticles->precache((int)this->rainLife, 30); // TODO: Why doesnt this work??
146                RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
147                RainEffect::rainParticles->setRadius(0, 0.03);
148                RainEffect::rainParticles->setRadius(0.2, 0.02);
149                RainEffect::rainParticles->setRadius(1, 0.01);
150                RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2);     // grey blue 1
151                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2);   // grey blue 2
152                RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2);     // light grey
153        }
154
155        this->emitter->setSystem(RainEffect::rainParticles);
156
157        this->emitter->setRelCoor(this->rainCoord);
158
159        this->emitter->setEmissionRate(this->rainRate);
160        this->emitter->setEmissionVelocity(this->rainVelocity);
161
162        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
163       
164        this->soundSource.loop(this->rainBuffer, this->soundRainVolume);
165        if (this->rainWindForce != 0) this->soundSource.loop(this->windBuffer, 0.1f * this->rainWindForce);
166
167        if (this->rainFadeInDuration == 0)
168                lightMan->setAmbientColor(.1,.1,.1);
169
170        return 0;
171}
172
173
174bool RainEffect::deactivate()
175{
176        PRINTF(0)("Deactivating RainEffect\n");
177       
178        this->rainActivate = false;
179        this->emitter->setSystem(NULL);
180
181        // Stop Sound
182        this->soundSource.stop();
183
184        // Restore Light Ambient
185        lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
186
187        return 0;
188}
189
190void RainEffect::tick (float dt)
191{
192        if (!this->rainActivate)
193                return;
194               
195        if (this->rainMove) {
196                this->rainCoord = State::getCameraNode()->getAbsCoor();
197                this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
198        }
199
200        if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration) {
201                this->localTimer += dt;
202                float progress = this->localTimer / this->rainFadeInDuration;
203
204                // Dim Light
205                lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
206
207                // use alpha in color to fade in
208                RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
209                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
210                RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
211
212                // increase radius for more "heavy" rain
213                RainEffect::rainParticles->setRadius(0, 0.03 * progress);
214                RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
215                RainEffect::rainParticles->setRadius(1, 0.01 * progress);
216
217                // increase sound volume
218                // this->soundSource.fadein(this->rainBuffer, 10);
219        }
220        else if ( this->rainFadeOutDuration != 0 ) {
221                if ( this->localTimer < this->rainFadeOutDuration ) {
222                        this->localTimer += dt;
223                        float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
224       
225                        // Fade In Light
226                        lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
227
228                        // use alpha in color to fade out
229                        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
230                        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
231                        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
232       
233                        // decrease radius
234                        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
235                        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
236                        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
237       
238                        // decrease sound volume
239                        // this->soundSource.fadeout(this->rainBuffer, 10);
240                }
241                else
242                        this->deactivate();
243        }
244       
245}
246
247void RainEffect::startRaining() {
248
249        if (this->rainActivate)
250                this->deactivate();
251
252        if (!this->rainFadeInDuration > 0)
253                this->rainFadeInDuration = 20;
254
255        this->localTimer = 0;
256
257        this->activate();
258
259}
260
261void RainEffect::stopRaining() {
262
263        if (!this->rainActivate)
264                this->activate();
265
266        if (!this->rainFadeOutDuration > 0)
267                this->rainFadeOutDuration = 20;
268
269        this->localTimer = 0;
270
271}
Note: See TracBrowser for help on using the repository browser.