Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8171 was 8119, checked in by amaechler, 19 years ago

branches/atmospheric_engine: added function to slowly start rain

File size: 5.2 KB
Line 
1/*
2orxonox - the future of 3D-vertical-scrollers
3
4Copyright (C) 2004 orx
5
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.
10
11### File Specific:
12main-programmer: hdavid, amaechler
13*/
14
15#include "rain_effect.h"
16
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
19#include "util/loading/resource_manager.h"
20
21#include "glincl.h"
22#include "p_node.h"
23#include "state.h"
24#include "spark_particles.h"
25#include "plane_emitter.h"
26#include "shell_command.h"
27#include "light.h"
28
29#include "parser/tinyxml/tinyxml.h"
30
31SHELL_COMMAND(activate, RainEffect, activateRain);
32SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
33
34SHELL_COMMAND(startRaining, RainEffect, startRaining);
35
36using namespace std;
37
38CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
39
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
42RainEffect::RainEffect(const TiXmlElement* root)
43{
44        this->setClassID(CL_RAIN_EFFECT, "RainEffect");
45
46        this->init();
47
48        if (root != NULL)
49                this->loadParams(root);
50
51        //load rain sound
52        if (this->rainBuffer != NULL)
53                ResourceManager::getInstance()->unload(this->rainBuffer);
54        this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
55
56        //load wind sound
57        if (this->rainWindForce > 0) {
58                if (this->windBuffer != NULL)
59                        ResourceManager::getInstance()->unload(this->windBuffer);
60                this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV);
61        }
62
63        if(rainActivate)
64                this->activate();
65}
66
67RainEffect::~RainEffect()
68{
69        this->deactivate();
70
71        if (this->rainBuffer != NULL)
72                ResourceManager::getInstance()->unload(this->rainBuffer);
73
74        if (this->windBuffer != NULL)
75                ResourceManager::getInstance()->unload(this->windBuffer);
76}
77
78void RainEffect::loadParams(const TiXmlElement* root)
79{
80        WeatherEffect::loadParams(root);
81
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);
86        LoadParam(root, "life", this, RainEffect, setRainLife);
87        LoadParam(root, "wind", this, RainEffect, setRainWind);
88
89        LOAD_PARAM_START_CYCLE(root, element);
90        {
91                LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
92        }
93        LOAD_PARAM_END_CYCLE(element);
94
95}
96
97
98bool RainEffect::init()
99{
100        //Default values
101        this->rainActivate = false;
102        this->rainMove = false;
103        this->rainCoord = Vector(500, 500, 500);
104        this->rainSize = Vector2D(1000, 1000);
105        this->rainRate = 3000;
106        this->rainVelocity = -300;
107        this->rainLife = 4;
108        this->rainMaxParticles = this->rainRate * this->rainLife;
109        this->rainWindForce  = 0;
110
111        this->emitter = new PlaneEmitter(this->rainSize);
112
113        lightMan = LightManager::getInstance();
114}
115
116
117SparkParticles* RainEffect::rainParticles = NULL;
118
119bool RainEffect::activate()
120{
121        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" );
122
123        if (unlikely(RainEffect::rainParticles == NULL))
124        {
125                RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
126                RainEffect::rainParticles->setName("RainParticles");
127                RainEffect::rainParticles->precache((int)this->rainLife);
128                RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
129                RainEffect::rainParticles->setRadius(0, 0.03);
130                RainEffect::rainParticles->setRadius(0.2, 0.02);
131                RainEffect::rainParticles->setRadius(1, 0.01);
132                RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
133                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
134                RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
135        }
136
137        this->emitter->setSystem(RainEffect::rainParticles);
138
139        this->emitter->setRelCoor(this->rainCoord);
140
141        this->emitter->setEmissionRate(this->rainRate);
142        this->emitter->setEmissionVelocity(this->rainVelocity);
143
144        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
145       
146        this->soundSource.loop(this->rainBuffer, 0.8f);
147        if (this->rainWindForce > 0)
148                this->soundSource.loop(this->windBuffer, 0.5f);
149
150        lightMan->setAmbientColor(.1,.1,.1);
151}
152
153
154bool RainEffect::deactivate()
155{
156        PRINTF(0)("Deactivating RainEffect\n");
157        this->emitter->setSystem(NULL);
158
159        this->soundSource.stop();
160
161        lightMan->setAmbientColor(1,1,1);
162}
163
164void RainEffect::tick (float dt)
165{
166        if (this->rainMove) {
167                this->rainCoord = State::getCameraNode()->getAbsCoor();
168                this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
169        }
170}
171
172/**
173*  Slowly starts rain
174*  @param duration duration of the fade in process in seconds
175*/
176void RainEffect::startRaining(float duration)
177{
178        // use alpha in color to fade in
179        // increase radius for more "heavy" rain
180        // increase sound volume
181}
Note: See TracBrowser for help on using the repository browser.