Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/new_class_id: SoundSource completely added as a Resource

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