Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/rain_effect.cc @ 8356

Last change on this file since 8356 was 8356, checked in by amaechler, 18 years ago

branches/atmospheric_engine: fadeins generalized

File size: 7.7 KB
RevLine 
[7561]1/*
[8255]2orxonox - the future of 3D-vertical-scrollers
[7561]3
[8255]4Copyright (C) 2004 orx
[7561]5
[8255]6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
[7561]10
11### File Specific:
[8255]12main-programmer: hdavid, amaechler
[7561]13*/
14
15#include "rain_effect.h"
16
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
[7646]19#include "util/loading/resource_manager.h"
[7561]20
21#include "glincl.h"
[7652]22#include "p_node.h"
[7562]23#include "state.h"
[7628]24#include "spark_particles.h"
25#include "plane_emitter.h"
[7696]26#include "shell_command.h"
[8255]27#include "light.h"
[7562]28
29#include "parser/tinyxml/tinyxml.h"
30
[7696]31SHELL_COMMAND(activate, RainEffect, activateRain);
32SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
[8356]33SHELL_COMMAND(startraining, RainEffect, startRaining);
34SHELL_COMMAND(stopraining, RainEffect, stopRaining);
[7577]35
[7561]36using namespace std;
37
38CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
39
[8255]40// TODO: Dim Light with Rain, Finish preCaching, check out if multiple rain emitters work,  Think about what happens with building poss. to hang movewithcam off, benchmark, possible to activate lightening, turn off visibility when in a building, variable emitter size depending on playable, also rain velocity
41
[7561]42RainEffect::RainEffect(const TiXmlElement* root)
43{
[7652]44        this->setClassID(CL_RAIN_EFFECT, "RainEffect");
[7561]45
[7652]46        this->init();
[7561]47
[7652]48        if (root != NULL)
49                this->loadParams(root);
[7646]50
[7696]51        //load rain sound
[7652]52        if (this->rainBuffer != NULL)
53                ResourceManager::getInstance()->unload(this->rainBuffer);
[8261]54        this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
[7646]55
[7696]56        //load wind sound
[8255]57        if (this->rainWindForce != 0) {
[7696]58                if (this->windBuffer != NULL)
59                        ResourceManager::getInstance()->unload(this->windBuffer);
[8261]60                this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
[7696]61        }
62
[8255]63        if(rainActivate)
64                this->activate();
[7561]65}
66
67RainEffect::~RainEffect()
68{
[7652]69        this->deactivate();
[7696]70
71        if (this->rainBuffer != NULL)
72                ResourceManager::getInstance()->unload(this->rainBuffer);
73
74        if (this->windBuffer != NULL)
75                ResourceManager::getInstance()->unload(this->windBuffer);
[7561]76}
77
78void RainEffect::loadParams(const TiXmlElement* root)
79{
[7628]80        WeatherEffect::loadParams(root);
[7561]81
[7628]82        LoadParam(root, "coord", this, RainEffect, setRainCoord);
83        LoadParam(root, "size", this, RainEffect, setRainSize);
84        LoadParam(root, "rate", this, RainEffect, setRainRate);
85        LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
[7652]86        LoadParam(root, "life", this, RainEffect, setRainLife);
[7682]87        LoadParam(root, "wind", this, RainEffect, setRainWind);
[8267]88        LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
89        LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
[8255]90
91        LOAD_PARAM_START_CYCLE(root, element);
92        {
93                LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
94        }
95        LOAD_PARAM_END_CYCLE(element);
96
[7628]97}
[7561]98
99
[7628]100bool RainEffect::init()
101{
[7652]102        //Default values
[8255]103        this->rainActivate = false;
104        this->rainMove = false;
[7652]105        this->rainCoord = Vector(500, 500, 500);
106        this->rainSize = Vector2D(1000, 1000);
[8327]107        this->rainRate = 4000;
[7652]108        this->rainVelocity = -300;
[7682]109        this->rainLife = 4;
110        this->rainMaxParticles = this->rainRate * this->rainLife;
[7685]111        this->rainWindForce  = 0;
[8267]112        this->rainFadeInDuration = 0;
113        this->rainFadeOutDuration = 0;
[8255]114        this->localTimer = 0;
115        this->soundRainVolume = 0.8f;
[7646]116
[7652]117        this->emitter = new PlaneEmitter(this->rainSize);
[8255]118
119        lightMan = LightManager::getInstance();
120        this->rainAmbient = lightMan->getAmbientColor();
[8267]121
122        return 0;
[7561]123}
124
125
[7577]126SparkParticles* RainEffect::rainParticles = NULL;
[7561]127
128bool RainEffect::activate()
129{
[7652]130        PRINTF(0)( "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]131
[8255]132        this->rainActivate = true;
133
[7577]134        if (unlikely(RainEffect::rainParticles == NULL))
135        {
[7682]136                RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
[7577]137                RainEffect::rainParticles->setName("RainParticles");
[7682]138                RainEffect::rainParticles->precache((int)this->rainLife);
[7652]139                RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
[7682]140                RainEffect::rainParticles->setRadius(0, 0.03);
141                RainEffect::rainParticles->setRadius(0.2, 0.02);
142                RainEffect::rainParticles->setRadius(1, 0.01);
[8255]143                RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2);     // grey blue 1
144                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2);   // grey blue 2
145                RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2);     // light grey
[7577]146        }
147
148        this->emitter->setSystem(RainEffect::rainParticles);
149
[7628]150        this->emitter->setRelCoor(this->rainCoord);
[7577]151
[7628]152        this->emitter->setEmissionRate(this->rainRate);
[7646]153        this->emitter->setEmissionVelocity(this->rainVelocity);
[7628]154
[7685]155        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
[7696]156       
[8255]157        this->soundSource.loop(this->rainBuffer, this->soundRainVolume);
158        if (this->rainWindForce != 0) this->soundSource.loop(this->windBuffer, 0.1f * this->rainWindForce);
159
160        lightMan->setAmbientColor(.1,.1,.1);
[8267]161
162        return 0;
[7561]163}
164
165
166bool RainEffect::deactivate()
167{
[7652]168        PRINTF(0)("Deactivating RainEffect\n");
[8255]169       
170        this->rainActivate = false;
[7696]171        this->emitter->setSystem(NULL);
[7577]172
[8255]173        // Stop Sound
[7696]174        this->soundSource.stop();
[7646]175
[8255]176        // Restore Light Ambient
177        lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
[8267]178
179        return 0;
[7696]180}
181
[7646]182void RainEffect::tick (float dt)
183{
[8255]184        if (!this->rainActivate)
185                return;
186               
[7685]187        if (this->rainMove) {
188                this->rainCoord = State::getCameraNode()->getAbsCoor();
[7691]189                this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
[7685]190        }
[8255]191
[8267]192        if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration) {
[8255]193                this->localTimer += dt;
[8267]194                float progress = this->localTimer / this->rainFadeInDuration;
[8255]195
196                // Dim Light
197                lightMan->setAmbientColor(1.1 - progress, 1.1 - progress, 1.1 - progress);
198
199                // use alpha in color to fade in
200                RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
201                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
202                RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
203
204                // increase radius for more "heavy" rain
205                RainEffect::rainParticles->setRadius(0, 0.03 * progress);
206                RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
207                RainEffect::rainParticles->setRadius(1, 0.01 * progress);
208
209                // increase sound volume
210                // this->soundSource.fadein(this->rainBuffer, 10);
211        }
[8356]212        else if (this->rainFadeOutDuration != 0 && this->localTimer < this->rainFadeOutDuration) {
213                this->localTimer += dt;
214                float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
215
216                // Dim Light
217                lightMan->setAmbientColor(1.1 - progress, 1.1 - progress, 1.1 - progress);
218
219                // use alpha in color to fade in
220                RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
221                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
222                RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
223
224                // increase radius for more "heavy" rain
225                RainEffect::rainParticles->setRadius(0, 0.03 * progress);
226                RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
227                RainEffect::rainParticles->setRadius(1, 0.01 * progress);
228
229                // increase sound volume
230                // this->soundSource.fadein(this->rainBuffer, 10);
231        }
[8255]232       
[7646]233}
[8255]234
235void RainEffect::startRaining() {
236
237        if (this->rainActivate)
238                this->deactivate();
239
[8267]240        if (!this->rainFadeInDuration > 0)
241                this->rainFadeInDuration = 20;
242
[8255]243        this->localTimer = 0;
244        this->activate();
245
246}
[8356]247
248void RainEffect::stopRaining() {
249
250        if (this->rainActivate)
251                this->deactivate();
252
253        if (!this->rainFadeOutDuration > 0)
254                this->rainFadeOutDuration = 20;
255
256        this->localTimer = 0;
257        this->activate();
258
259}
Note: See TracBrowser for help on using the repository browser.