Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/weather_effects/rain_effect.cc @ 9760

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

moved around the weather effecs

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