Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/graphics/effects/rain_effect.cc @ 9744

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

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

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