Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/graphics/effects/rain_effect.cc @ 9716

Last change on this file since 9716 was 9716, checked in by bensch, 18 years ago

more renamings

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