Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/graphics/effects/rain_effect.cc @ 9201

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

rain

File size: 11.1 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#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
16
17#include "rain_effect.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 "glincl.h"
24#include "p_node.h"
25#include "state.h"
26#include "spark_particles.h"
27#include "plane_emitter.h"
28#include "shell_command.h"
29#include "light.h"
30#include "cloud_effect.h"
31#include "script_class.h"
32
33#include "parser/tinyxml/tinyxml.h"
34
35// Define shell commands
36//SHELL_COMMAND(activate, RainEffect, activateRain);
37//SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
38SHELL_COMMAND(startraining, RainEffect, startRaining);
39SHELL_COMMAND(stopraining, RainEffect, stopRaining);
40
41using namespace std;
42
43CREATE_SCRIPTABLE_CLASS(RainEffect, CL_RAIN_EFFECT,
44                        addMethod("startRaining", ExecutorLua0<RainEffect>(&RainEffect::startRaining))
45                            ->addMethod("stopRaining", ExecutorLua0<RainEffect>(&RainEffect::stopRaining))
46                            ->addMethod("activate", ExecutorLua0<RainEffect>(&RainEffect::activate))
47                            ->addMethod("deactivate", ExecutorLua0<RainEffect>(&RainEffect::deactivate))
48                       );
49
50CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
51
52/**
53 * @brief standard constructor
54 */
55RainEffect::RainEffect(const TiXmlElement* root) {
56    this->setClassID(CL_RAIN_EFFECT, "RainEffect");
57
58    this->init();
59
60    if (root != NULL)
61        this->loadParams(root);
62
63    //load rain sound
64    if (this->rainBuffer != NULL)
65        ResourceManager::getInstance()->unload(this->rainBuffer);
66    this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
67
68    //load wind sound
69    if (this->rainWindForce != 0) {
70        if (this->windBuffer != NULL)
71            ResourceManager::getInstance()->unload(this->windBuffer);
72        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
73    }
74
75    if(rainActivate) {
76        this->activate();
77        RainEffect::rainParticles->precache((int)this->rainLife * 2);
78    }
79}
80
81/**
82 * @brief standard deconstructor
83 */
84RainEffect::~RainEffect() {
85    this->deactivate();
86
87    if (this->rainBuffer != NULL)
88        ResourceManager::getInstance()->unload(this->rainBuffer);
89
90    if (this->windBuffer != NULL)
91        ResourceManager::getInstance()->unload(this->windBuffer);
92}
93
94/**
95 * @brief initalizes the rain effect with default values
96 */
97void RainEffect::init() {
98    //Default values
99    this->rainActivate = false;
100    this->rainFadeInActivate = false;
101    this->rainFadeOutActivate = false;
102
103    this->rainMove = false;
104    this->rainCoord = Vector(500, 500, 500);
105    this->rainSize = Vector2D(1000, 1000);
106    this->rainRate = 4000;
107    this->rainVelocity = -300;
108    this->rainLife = 4;
109    this->rainWindForce  = 0;
110    this->rainFadeInDuration = 10;
111    this->rainFadeOutDuration = 10;
112
113    this->cloudColor = Vector(0.6f, 0.6f, 0.6f);
114    this->skyColor = Vector(0.0f, 0.0f, 0.0f);
115
116    this->rainMaxParticles = this->rainRate * this->rainLife;
117    this->localTimer = 0;
118    this->soundRainVolume = 0.3f;
119    this->emitter = new PlaneEmitter(this->rainSize);
120
121    lightMan = LightManager::getInstance();
122}
123
124/**
125 * @brief loads the rain effect parameters.
126 * @param root: the XML-Element to load the data from
127 */
128void RainEffect::loadParams(const TiXmlElement* root) {
129    WeatherEffect::loadParams(root);
130
131    LoadParam(root, "coord", this, RainEffect, setRainCoord);
132    LoadParam(root, "size", this, RainEffect, setRainSize);
133    LoadParam(root, "rate", this, RainEffect, setRainRate);
134    LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
135    LoadParam(root, "life", this, RainEffect, setRainLife);
136    LoadParam(root, "wind", this, RainEffect, setRainWind);
137    LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
138    LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
139    LoadParam(root, "cloudcolor", this, RainEffect, setCloudColor);
140    LoadParam(root, "skycolor", this, RainEffect, setSkyColor);
141
142    LOAD_PARAM_START_CYCLE(root, element);
143    {
144        LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
145    }
146    LOAD_PARAM_END_CYCLE(element);
147}
148
149SparkParticles* RainEffect::rainParticles = NULL;
150
151/**
152 * @brief activates the rain effect
153 */
154void RainEffect::activate() {
155    PRINTF(3)( "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" );
156
157    this->rainActivate = true;
158
159    if (unlikely(RainEffect::rainParticles == NULL)) {
160        RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
161        RainEffect::rainParticles->setName("RainParticles");
162        RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
163        RainEffect::rainParticles->setRadius(0, 0.03);
164        RainEffect::rainParticles->setRadius(0.2, 0.02);
165        RainEffect::rainParticles->setRadius(1, 0.01);
166        RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2);   // grey blue 1
167        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
168        RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2);   // light grey
169    }
170
171    this->emitter->setSystem(RainEffect::rainParticles);
172    this->emitter->setRelCoor(this->rainCoord);
173    this->emitter->setEmissionRate(this->rainRate);
174    this->emitter->setEmissionVelocity(this->rainVelocity);
175    this->emitter->setSpread(this->rainWindForce / 50, 0.2);
176
177    // play rain sound and loop it
178    this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
179
180    // if there's wind, play wind sound
181    if (this->rainWindForce > 0)
182        this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true);
183
184    // Store cloud- and sky color before the rain;
185    this->oldCloudColor = CloudEffect::cloudColor;
186    this->oldSkyColor   = CloudEffect::skyColor;
187
188    // If we're not fading, change color immediately
189    if (!this->rainFadeInActivate || !this->rainFadeOutActivate) {
190        CloudEffect::changeCloudColor(this->cloudColor, 0.2);
191        CloudEffect::changeSkyColor(this->skyColor, 0.2);
192    }
193   
194    //lightMan->setAmbientColor(.1,.1,.1);
195}
196
197/**
198 * @brief deactivates the rain effect
199 */
200void RainEffect::deactivate() {
201    PRINTF(3)("Deactivating RainEffect\n");
202
203    this->rainActivate = false;
204    this->rainFadeInActivate = false;
205    this->rainFadeOutActivate = false;
206
207    //if(this->emitter)
208    //  this->emitter->setSystem(NULL);
209    //this->hideRain();
210
211    // Stop Sound
212    this->soundSource.stop();
213
214    // Restore the old cloud- and sky color
215    CloudEffect::changeCloudColor(this->oldCloudColor, 0.2);
216    CloudEffect::changeSkyColor(this->oldSkyColor, 0.2);
217}
218
219/**
220 * @brief ticks the rain effect
221 * @param dt: tick float
222 */
223void RainEffect::tick (float dt) {
224    if (!this->rainActivate)
225        return;
226
227    if (this->rainMove) {
228        this->rainCoord = State::getCameraNode()->getAbsCoor();
229        this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
230    }
231
232    if (this->rainFadeInActivate) {
233        PRINTF(4)("tick fading IN RainEffect\n");
234
235        this->localTimer += dt;
236        float progress = this->localTimer / this->rainFadeInDuration;
237
238        // use alpha in color to fade in rain
239        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
240        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
241        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
242
243        // increase radius for more "heavy" rain
244        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
245        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
246        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
247
248        // increase sound volume
249        if (!this->soundSource.isPlaying())
250            this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
251        this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
252
253        if (progress >= 1)
254            this->rainFadeInActivate = false;
255       
256        lightMan->setAmbientColor(1-progress, 1-progress, 1-progress);
257    }
258
259    if (this->rainFadeOutActivate) {
260        PRINTF(4)("tick fading OUT RainEffect\n");
261
262        this->localTimer += dt;
263        float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
264
265        // use alpha in color to fade out
266        RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
267        RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
268        RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
269
270        // decrease radius
271        RainEffect::rainParticles->setRadius(0, 0.03 * progress);
272        RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
273        RainEffect::rainParticles->setRadius(1, 0.01 * progress);
274
275        // decrease sound volume
276        if (!this->soundSource.isPlaying())
277            this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
278        this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
279
280        if (progress <= 0) {
281            PRINTF(4)("tick fading OUT RainEffect COMPLETED! Deactivating...\n");
282            this->rainFadeOutActivate = false;
283            this->deactivate();
284        }
285        lightMan->setAmbientColor(1-progress, 1-progress, 1-progress);
286    }
287}
288
289/**
290 * @brief starts raining slowly
291*/
292void RainEffect::startRaining() {
293    PRINTF(4)("startRaining function;\n");
294
295    // If it is already raining, do nothing
296    if (this->rainActivate)
297        return;
298
299    this->localTimer = 0;
300    this->rainFadeInActivate = true;
301
302    PRINTF(4)("startRaining function complete; fadedur: %f\n", this->rainFadeInDuration);
303    this->activate();
304
305    CloudEffect::changeCloudColor(this->cloudColor, this->rainFadeInDuration);
306    CloudEffect::changeSkyColor(this->skyColor, this->rainFadeInDuration);
307}
308
309/**
310 * @brief stops raining slowly
311 */
312void RainEffect::stopRaining() {
313    PRINTF(4)("stopRaining function;\n");
314
315    // If it is not raining, do nothing
316    if (!this->rainActivate)
317        return;
318
319    this->localTimer = 0;
320    this->rainFadeOutActivate = true;
321
322    PRINTF(4)("stopRaining function completed; fadedur: %f\n", this->rainFadeOutDuration);
323
324    CloudEffect::changeCloudColor(this->oldCloudColor, this->rainFadeOutDuration);
325    CloudEffect::changeSkyColor(this->oldSkyColor, this->rainFadeOutDuration);
326}
327
328/**
329 * @brief hides the rain
330 */
331void RainEffect::hideRain() {
332    RainEffect::rainParticles->setColor(0, 0,0,0, 0);
333    RainEffect::rainParticles->setColor(0, 0,0,0, 0);
334}
Note: See TracBrowser for help on using the repository browser.