Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8084 was 8074, checked in by hdavid, 19 years ago

branches/atmospheric_engine: rain influences the light

File size: 4.9 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#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
34using namespace std;
35
36CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
37
38// 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
39
40RainEffect::RainEffect(const TiXmlElement* root)
41{
42        this->setClassID(CL_RAIN_EFFECT, "RainEffect");
43
44        this->init();
45
46        if (root != NULL)
47                this->loadParams(root);
48
49        //load rain sound
50        if (this->rainBuffer != NULL)
51                ResourceManager::getInstance()->unload(this->rainBuffer);
52        this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/rain.wav", WAV);
53
54        //load wind sound
55        if (this->rainWindForce > 0) {
56                if (this->windBuffer != NULL)
57                        ResourceManager::getInstance()->unload(this->windBuffer);
58                this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV);
59        }
60
61  if(rainActivate)
62         this->activate();
63}
64
65RainEffect::~RainEffect()
66{
67        this->deactivate();
68
69        if (this->rainBuffer != NULL)
70                ResourceManager::getInstance()->unload(this->rainBuffer);
71
72        if (this->windBuffer != NULL)
73                ResourceManager::getInstance()->unload(this->windBuffer);
74}
75
76void RainEffect::loadParams(const TiXmlElement* root)
77{
78        WeatherEffect::loadParams(root);
79
80        LoadParam(root, "coord", this, RainEffect, setRainCoord);
81        LoadParam(root, "size", this, RainEffect, setRainSize);
82        LoadParam(root, "rate", this, RainEffect, setRainRate);
83        LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
84        LoadParam(root, "life", this, RainEffect, setRainLife);
85        LoadParam(root, "wind", this, RainEffect, setRainWind);
86 
87  LOAD_PARAM_START_CYCLE(root, element);
88  {
89    LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
90  }
91  LOAD_PARAM_END_CYCLE(element);
92 
93}
94
95
96bool RainEffect::init()
97{
98        //Default values
99  this->rainActivate = false;
100  this->rainMove = false;
101        this->rainCoord = Vector(500, 500, 500);
102        this->rainSize = Vector2D(1000, 1000);
103        this->rainRate = 3000;
104        this->rainVelocity = -300;
105        this->rainLife = 4;
106        this->rainMaxParticles = this->rainRate * this->rainLife;
107        this->rainWindForce  = 0;
108
109        this->emitter = new PlaneEmitter(this->rainSize);
110 
111  lightMan = LightManager::getInstance();
112}
113
114
115SparkParticles* RainEffect::rainParticles = NULL;
116
117bool RainEffect::activate()
118{
119        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" );
120
121        if (unlikely(RainEffect::rainParticles == NULL))
122        {
123                RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
124                RainEffect::rainParticles->setName("RainParticles");
125                RainEffect::rainParticles->precache((int)this->rainLife);
126                RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
127                RainEffect::rainParticles->setRadius(0, 0.03);
128                RainEffect::rainParticles->setRadius(0.2, 0.02);
129                RainEffect::rainParticles->setRadius(1, 0.01);
130                RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
131                RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
132                RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
133        }
134
135        this->emitter->setSystem(RainEffect::rainParticles);
136
137        this->emitter->setRelCoor(this->rainCoord);
138
139        this->emitter->setEmissionRate(this->rainRate);
140        this->emitter->setEmissionVelocity(this->rainVelocity);
141
142        this->emitter->setSpread(this->rainWindForce / 50, 0.2);
143       
144        this->soundSource.loop(this->rainBuffer, 0.8f);
145        if (this->rainWindForce > 0)
146                this->soundSource.loop(this->windBuffer, 0.5f);
147 
148  lightMan->setAmbientColor(.1,.1,.1);
149}
150
151
152bool RainEffect::deactivate()
153{
154        PRINTF(0)("Deactivating RainEffect\n");
155        this->emitter->setSystem(NULL);
156
157        this->soundSource.stop();
158 
159  lightMan->setAmbientColor(1,1,1);
160}
161
162void RainEffect::tick (float dt)
163{
164        if (this->rainMove) {
165                this->rainCoord = State::getCameraNode()->getAbsCoor();
166                this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
167        }
168}
Note: See TracBrowser for help on using the repository browser.