Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weather_effects/rain_effect.cc @ 10626

Last change on this file since 10626 was 10415, checked in by patrick, 18 years ago

changed the path names to reflect the new data repos

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