Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8474 was 8471, checked in by amaechler, 18 years ago

atmospheric: fog - weird fade problems, not solved

File size: 8.3 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    this->setClassID(CL_RAIN_EFFECT, "RainEffect");
51
52    this->init();
53
54    if (root != NULL)
55        this->loadParams(root);
56
57    //load rain sound
58    if (this->rainBuffer != NULL)
59        ResourceManager::getInstance()->unload(this->rainBuffer);
60    this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
61
62    //load wind sound
63    if (this->rainWindForce != 0) {
64        if (this->windBuffer != NULL)
65            ResourceManager::getInstance()->unload(this->windBuffer);
66        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
67    }
68
69    if(rainActivate) {
70        this->activate();
71        RainEffect::rainParticles->precache((int)this->rainLife);
72    }
73}
74
75RainEffect::~RainEffect() {
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    WeatherEffect::loadParams(root);
87
88    LoadParam(root, "coord", this, RainEffect, setRainCoord);
89    LoadParam(root, "size", this, RainEffect, setRainSize);
90    LoadParam(root, "rate", this, RainEffect, setRainRate);
91    LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
92    LoadParam(root, "life", this, RainEffect, setRainLife);
93    LoadParam(root, "wind", this, RainEffect, setRainWind);
94    LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
95    LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
96
97    LOAD_PARAM_START_CYCLE(root, element);
98    {
99        LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
100    }
101    LOAD_PARAM_END_CYCLE(element);
102
103}
104
105
106void RainEffect::init() {
107    //Default values
108    this->rainActivate = false;
109    this->rainMove = false;
110    this->rainCoord = Vector(500, 500, 500);
111    this->rainSize = Vector2D(1000, 1000);
112    this->rainRate = 4000;
113    this->rainVelocity = -300;
114    this->rainLife = 4;
115    this->rainMaxParticles = this->rainRate * this->rainLife;
116    this->rainWindForce  = 0;
117    this->rainFadeInDuration = 0;
118    this->rainFadeOutDuration = 0;
119    this->localTimer = 0;
120    this->soundRainVolume = 0.8f;
121
122    this->emitter = new PlaneEmitter(this->rainSize);
123
124    lightMan = LightManager::getInstance();
125    this->rainAmbient = lightMan->getAmbientColor();
126}
127
128
129SparkParticles* RainEffect::rainParticles = NULL;
130
131void RainEffect::activate() {
132    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" );
133
134    this->rainActivate = true;
135
136    if (unlikely(RainEffect::rainParticles == NULL)) {
137        RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
138        RainEffect::rainParticles->setName("RainParticles");
139        RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
140        RainEffect::rainParticles->setRadius(0, 0.03);
141        RainEffect::rainParticles->setRadius(0.2, 0.02);
142        RainEffect::rainParticles->setRadius(1, 0.01);
143        RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
144        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
145        RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
146    }
147
148    this->emitter->setSystem(RainEffect::rainParticles);
149
150    this->emitter->setRelCoor(this->rainCoord);
151
152    this->emitter->setEmissionRate(this->rainRate);
153    this->emitter->setEmissionVelocity(this->rainVelocity);
154
155    this->emitter->setSpread(this->rainWindForce / 50, 0.2);
156
157    this->soundSource.loop(this->rainBuffer, this->soundRainVolume);
158    if (this->rainWindForce != 0)
159        this->soundSource.loop(this->windBuffer, 0.1f * this->rainWindForce);
160
161    if (this->rainFadeInDuration == 0)
162        lightMan->setAmbientColor(.1,.1,.1);
163
164}
165
166
167void RainEffect::deactivate() {
168    PRINTF(0)("Deactivating RainEffect\n");
169
170    this->rainActivate = false;
171    this->emitter->setSystem(NULL);
172
173    // Stop Sound
174    this->soundSource.stop();
175
176    // Restore Light Ambient
177    lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
178
179}
180
181void RainEffect::tick (float dt) {
182    if (!this->rainActivate)
183        return;
184
185    if (this->rainMove) {
186        this->rainCoord = State::getCameraNode()->getAbsCoor();
187        this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
188    }
189
190    if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration) {
191        this->localTimer += dt;
192        float progress = this->localTimer / this->rainFadeInDuration;
193
194        // Dim Light
195        lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
196
197        // use alpha in color to fade in
198        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
199        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
200        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
201
202        // increase radius for more "heavy" rain
203        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
204        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
205        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
206
207        // increase sound volume
208        // this->soundSource.fadein(this->rainBuffer, 10);
209    } else if ( this->rainFadeOutDuration != 0 ) {
210        if ( this->localTimer < this->rainFadeOutDuration ) {
211            this->localTimer += dt;
212            float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
213
214            // Fade In Light
215            lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
216
217            // use alpha in color to fade out
218            RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
219            RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
220            RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
221
222            // decrease radius
223            RainEffect::rainParticles->setRadius(0, 0.03 * progress);
224            RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
225            RainEffect::rainParticles->setRadius(1, 0.01 * progress);
226
227            // decrease sound volume
228            // this->soundSource.fadeout(this->rainBuffer, 10);
229        } else
230            this->deactivate();
231    }
232
233}
234
235void RainEffect::startRaining() {
236
237    if (this->rainActivate)
238        this->deactivate();
239
240    if (!this->rainFadeInDuration > 0)
241        this->rainFadeInDuration = 20;
242
243    this->localTimer = 0;
244
245    this->activate();
246
247}
248
249void RainEffect::stopRaining() {
250
251    if (!this->rainActivate)
252        this->activate();
253
254    if (!this->rainFadeOutDuration > 0)
255        this->rainFadeOutDuration = 20;
256
257    this->localTimer = 0;
258
259}
Note: See TracBrowser for help on using the repository browser.