Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jun 26, 2006, 3:36:16 PM (19 years ago)
Author:
patrick
Message:

trunk: merged the weather engine branche to the trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/graphics/effects/rain_effect.cc

    r8495 r8793  
    11/*
    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 
     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 
    1111### File Specific:
    12 main-programmer: hdavid, amaechler
     12  main-programmer: hdavid, amaechler
    1313*/
    1414
     
    2626#include "shell_command.h"
    2727#include "light.h"
     28#include "cloud_effect.h"
    2829
    2930#include "parser/tinyxml/tinyxml.h"
    3031
     32// Define shell commands
    3133SHELL_COMMAND(activate, RainEffect, activateRain);
    3234SHELL_COMMAND(deactivate, RainEffect, deactivateRain);
     
    3840CREATE_FACTORY(RainEffect, CL_RAIN_EFFECT);
    3941
    40 /* TODO:
    41   - test multiple rain emitters
    42   - Think about what happens with building poss. to hang movewithcam off
    43   - Possible to activate lightening
    44   - turn off visibility when in a building
    45   - variable emitter size depending on playable
     42/**
     43 * @brief standard constructor
     44 */
     45RainEffect::RainEffect(const TiXmlElement* root)
     46{
     47  this->setClassID(CL_RAIN_EFFECT, "RainEffect");
     48
     49  this->init();
     50
     51  if (root != NULL)
     52    this->loadParams(root);
     53
     54  //load rain sound
     55  if (this->rainBuffer != NULL)
     56    ResourceManager::getInstance()->unload(this->rainBuffer);
     57  this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
     58
     59  //load wind sound
     60  if (this->rainWindForce != 0)
     61  {
     62    if (this->windBuffer != NULL)
     63      ResourceManager::getInstance()->unload(this->windBuffer);
     64    this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
     65  }
     66
     67  if(rainActivate)
     68  {
     69    this->activate();
     70    RainEffect::rainParticles->precache((int)this->rainLife * 2);
     71  }
     72}
     73
     74/**
     75 * @brief standard deconstructor
     76 */
     77RainEffect::~RainEffect()
     78{
     79  this->deactivate();
     80
     81  if (this->rainBuffer != NULL)
     82    ResourceManager::getInstance()->unload(this->rainBuffer);
     83
     84  if (this->windBuffer != NULL)
     85    ResourceManager::getInstance()->unload(this->windBuffer);
     86}
     87
     88/**
     89 * @brief initalizes the rain effect with default values
     90 */
     91void RainEffect::init()
     92{
     93  //Default values
     94  this->rainActivate = false;
     95  this->rainMove = false;
     96  this->rainCoord = Vector(500, 500, 500);
     97  this->rainSize = Vector2D(1000, 1000);
     98  this->rainRate = 4000;
     99  this->rainVelocity = -300;
     100  this->rainLife = 4;
     101  this->rainWindForce  = 0;
     102  this->rainFadeInDuration = 0;
     103  this->rainFadeOutDuration = 0;
     104 
     105  this->cloudColor = Vector(0.8f, 0.8f, 0.8f);
     106  this->skyColor = Vector(0.0f, 0.0f, 0.0f);
     107
     108  this->rainMaxParticles = this->rainRate * this->rainLife;
     109  this->localTimer = 0;
     110  this->soundRainVolume = 0.3f;
     111  this->emitter = new PlaneEmitter(this->rainSize);
     112
     113  lightMan = LightManager::getInstance();
     114  this->rainAmbient = lightMan->getAmbientColor();
     115}
     116
     117/**
     118 * @brief loads the rain effect parameters.
     119 * @param root: the XML-Element to load the data from
     120 */
     121void RainEffect::loadParams(const TiXmlElement* root)
     122{
     123  WeatherEffect::loadParams(root);
     124
     125  LoadParam(root, "coord", this, RainEffect, setRainCoord);
     126  LoadParam(root, "size", this, RainEffect, setRainSize);
     127  LoadParam(root, "rate", this, RainEffect, setRainRate);
     128  LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
     129  LoadParam(root, "life", this, RainEffect, setRainLife);
     130  LoadParam(root, "wind", this, RainEffect, setRainWind);
     131  LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
     132  LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
     133  LoadParam(root, "cloudcolor", this, RainEffect, setCloudColor);
     134  LoadParam(root, "skycolor", this, RainEffect, setSkyColor);
     135
     136  LOAD_PARAM_START_CYCLE(root, element);
     137  {
     138    LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
     139  }
     140  LOAD_PARAM_END_CYCLE(element);
     141}
     142
     143SparkParticles* RainEffect::rainParticles = NULL;
     144
     145/**
     146 * @brief activates the rain effect
     147 */
     148void RainEffect::activate()
     149{
     150  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" );
     151
     152  this->rainActivate = true;
     153
     154  if (unlikely(RainEffect::rainParticles == NULL))
     155  {
     156    RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
     157    RainEffect::rainParticles->setName("RainParticles");
     158    RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
     159    RainEffect::rainParticles->setRadius(0, 0.03);
     160    RainEffect::rainParticles->setRadius(0.2, 0.02);
     161    RainEffect::rainParticles->setRadius(1, 0.01);
     162    RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
     163    RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
     164    RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
     165  }
     166
     167  this->emitter->setSystem(RainEffect::rainParticles);
     168
     169  this->emitter->setRelCoor(this->rainCoord);
     170
     171  this->emitter->setEmissionRate(this->rainRate);
     172  this->emitter->setEmissionVelocity(this->rainVelocity);
     173
     174  this->emitter->setSpread(this->rainWindForce / 50, 0.2);
     175
     176  // plays the rain sound and loops it
     177  this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     178
     179  // if there is wind, play the wind sound
     180  if (this->rainWindForce != 0)
     181    this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true);
     182
     183  if (this->rainFadeInDuration == 0)
     184    lightMan->setAmbientColor(.1,.1,.1);
     185 
     186  // Change the cloudcolor,skycolor
     187  this->oldCloudColor = CloudEffect::cloudColor;
     188  this->oldSkyColor = CloudEffect::skyColor;
     189  CloudEffect::changeCloudColor(this->cloudColor);
     190  CloudEffect::changeSkyColor(this->skyColor);
     191}
     192
     193/**
     194 * @brief deactivates the rain effect
     195 */
     196void RainEffect::deactivate()
     197{
     198  PRINTF(0)("Deactivating RainEffect\n");
     199
     200  this->rainActivate = false;
     201  this->emitter->setSystem(NULL);
     202
     203  // Stop Sound
     204  this->soundSource.stop();
     205
     206  // Restore Light Ambient
     207  lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
     208 
     209  CloudEffect::changeCloudColor(this->oldCloudColor);
     210  CloudEffect::changeSkyColor(this->oldSkyColor);
     211}
     212
     213/**
     214 * @brief ticks the rain effect
     215 * @param dt: tick float
     216 */
     217void RainEffect::tick (float dt)
     218{
     219  if (!this->rainActivate)
     220    return;
     221
     222  if (this->rainMove)
     223  {
     224    this->rainCoord = State::getCameraNode()->getAbsCoor();
     225    this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
     226  }
     227
     228  if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration)
     229  {
     230    this->localTimer += dt;
     231    float progress = this->localTimer / this->rainFadeInDuration;
     232
     233    // Dim Light
     234    lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
     235
     236    // use alpha in color to fade in
     237    RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
     238    RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
     239    RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
     240
     241    // increase radius for more "heavy" rain
     242    RainEffect::rainParticles->setRadius(0, 0.03 * progress);
     243    RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
     244    RainEffect::rainParticles->setRadius(1, 0.01 * progress);
     245
     246    // increase sound volume
     247    if (!this->soundSource.isPlaying())
     248      this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     249
     250    this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
     251  }
     252  else if ( this->rainFadeOutDuration != 0 )
     253  {
     254    if ( this->localTimer < this->rainFadeOutDuration )
     255    {
     256      this->localTimer += dt;
     257      float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
     258
     259      // Fade In Light
     260      lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
     261
     262      // use alpha in color to fade out
     263      RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
     264      RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
     265      RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
     266
     267      // decrease radius
     268      RainEffect::rainParticles->setRadius(0, 0.03 * progress);
     269      RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
     270      RainEffect::rainParticles->setRadius(1, 0.01 * progress);
     271
     272      // decrease sound volume
     273      if (!this->soundSource.isPlaying())
     274        this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
     275
     276      this->soundSource.gain(this->rainBuffer, this->soundRainVolume * progress);
     277    }
     278    else
     279      this->deactivate();
     280  }
     281}
     282
     283/**
     284 * @brief starts raining slowly
    46285*/
    47 
    48 RainEffect::RainEffect(const TiXmlElement* root) {
    49     this->setClassID(CL_RAIN_EFFECT, "RainEffect");
    50 
    51     this->init();
    52 
    53     if (root != NULL)
    54         this->loadParams(root);
    55 
    56     //load rain sound
    57     if (this->rainBuffer != NULL)
    58         ResourceManager::getInstance()->unload(this->rainBuffer);
    59     this->rainBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/rain.wav", WAV);
    60 
    61     //load wind sound
    62     if (this->rainWindForce != 0) {
    63         if (this->windBuffer != NULL)
    64             ResourceManager::getInstance()->unload(this->windBuffer);
    65         this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/atmosphere/wind.wav", WAV);
    66     }
    67 
    68     if(rainActivate) {
    69         this->activate();
    70         RainEffect::rainParticles->precache((int)this->rainLife * 5); // TODO: Figure out the correct value
    71     }
    72 }
    73 
    74 RainEffect::~RainEffect() {
     286void RainEffect::startRaining()
     287{
     288
     289  if (this->rainActivate)
    75290    this->deactivate();
    76291
    77     if (this->rainBuffer != NULL)
    78         ResourceManager::getInstance()->unload(this->rainBuffer);
    79 
    80     if (this->windBuffer != NULL)
    81         ResourceManager::getInstance()->unload(this->windBuffer);
    82 }
    83 
    84 void RainEffect::loadParams(const TiXmlElement* root) {
    85     WeatherEffect::loadParams(root);
    86 
    87     LoadParam(root, "coord", this, RainEffect, setRainCoord);
    88     LoadParam(root, "size", this, RainEffect, setRainSize);
    89     LoadParam(root, "rate", this, RainEffect, setRainRate);
    90     LoadParam(root, "velocity", this, RainEffect, setRainVelocity);
    91     LoadParam(root, "life", this, RainEffect, setRainLife);
    92     LoadParam(root, "wind", this, RainEffect, setRainWind);
    93     LoadParam(root, "fadeinduration", this, RainEffect, setRainFadeIn);
    94     LoadParam(root, "fadeoutduration", this, RainEffect, setRainFadeOut);
    95 
    96     LOAD_PARAM_START_CYCLE(root, element);
    97     {
    98         LoadParam_CYCLE(element, "option", this, RainEffect, setRainOption);
    99     }
    100     LOAD_PARAM_END_CYCLE(element);
    101 
    102 }
    103 
    104 
    105 void RainEffect::init() {
    106     //Default values
    107     this->rainActivate = false;
    108     this->rainMove = false;
    109     this->rainCoord = Vector(500, 500, 500);
    110     this->rainSize = Vector2D(1000, 1000);
    111     this->rainRate = 4000;
    112     this->rainVelocity = -300;
    113     this->rainLife = 4;
    114     this->rainMaxParticles = this->rainRate * this->rainLife;
    115     this->rainWindForce  = 0;
    116     this->rainFadeInDuration = 0;
    117     this->rainFadeOutDuration = 0;
    118     this->localTimer = 0;
    119     this->soundRainVolume = 0.8f;
    120 
    121     this->emitter = new PlaneEmitter(this->rainSize);
    122 
    123     lightMan = LightManager::getInstance();
    124     this->rainAmbient = lightMan->getAmbientColor();
    125 }
    126 
    127 
    128 SparkParticles* RainEffect::rainParticles = NULL;
    129 
    130 void RainEffect::activate() {
    131     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" );
    132 
    133     this->rainActivate = true;
    134 
    135     if (unlikely(RainEffect::rainParticles == NULL)) {
    136         RainEffect::rainParticles = new SparkParticles((int) this->rainMaxParticles);
    137         RainEffect::rainParticles->setName("RainParticles");
    138         RainEffect::rainParticles->setLifeSpan(this->rainLife, 2);
    139         RainEffect::rainParticles->setRadius(0, 0.03);
    140         RainEffect::rainParticles->setRadius(0.2, 0.02);
    141         RainEffect::rainParticles->setRadius(1, 0.01);
    142         RainEffect::rainParticles->setColor(0, 0.3, 0.3, 0.5, 0.2); // grey blue 1
    143         RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2); // grey blue 2
    144         RainEffect::rainParticles->setColor(1, 0.7, 0.7, 0.7, 0.2); // light grey
    145     }
    146 
    147     this->emitter->setSystem(RainEffect::rainParticles);
    148 
    149     this->emitter->setRelCoor(this->rainCoord);
    150 
    151     this->emitter->setEmissionRate(this->rainRate);
    152     this->emitter->setEmissionVelocity(this->rainVelocity);
    153 
    154     this->emitter->setSpread(this->rainWindForce / 50, 0.2);
    155 
    156     this->soundSource.play(this->rainBuffer, this->soundRainVolume, true);
    157 
    158     if (this->rainWindForce != 0)
    159         this->soundSource.play(this->windBuffer, 0.1f * this->rainWindForce, true);
    160 
    161     if (this->rainFadeInDuration == 0)
    162         lightMan->setAmbientColor(.1,.1,.1);
    163 
    164 }
    165 
    166 
    167 void RainEffect::deactivate() {
    168     PRINTF(0)("Deactivating RainEffect\n");
    169 
    170     this->rainActivate = false;
    171     this->emitter->setSystem(NULL);
    172 
    173     // Stop Sound
    174     this->soundSource.stop();
    175 
    176     // Restore Light Ambient
    177     lightMan->setAmbientColor(this->rainAmbient, this->rainAmbient, this->rainAmbient);
    178 
    179 }
    180 
    181 void RainEffect::tick (float dt) {
    182     if (!this->rainActivate)
    183         return;
    184 
    185     if (this->rainMove) {
    186         this->rainCoord = State::getCameraNode()->getAbsCoor();
    187         this->emitter->setRelCoor(this->rainCoord.x , this->rainCoord.y+800, this->rainCoord.z);
    188     }
    189 
    190     if (this->rainFadeInDuration != 0 && this->localTimer < this->rainFadeInDuration) {
    191         this->localTimer += dt;
    192         float progress = this->localTimer / this->rainFadeInDuration;
    193 
    194         // Dim Light
    195         lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
    196 
    197         // use alpha in color to fade in
    198         RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
    199         RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
    200         RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
    201 
    202         // increase radius for more "heavy" rain
    203         RainEffect::rainParticles->setRadius(0, 0.03 * progress);
    204         RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
    205         RainEffect::rainParticles->setRadius(1, 0.01 * progress);
    206 
    207         // increase sound volume
    208         // this->soundSource.fadein(this->rainBuffer, 10);
    209     } else if ( this->rainFadeOutDuration != 0 ) {
    210         if ( this->localTimer < this->rainFadeOutDuration ) {
    211             this->localTimer += dt;
    212             float progress = 1 - (this->localTimer / this->rainFadeOutDuration);
    213 
    214             // Fade In Light
    215             lightMan->setAmbientColor(1 - progress * 0.9, 1 - progress * 0.9, 1 - progress * 0.9);
    216 
    217             // use alpha in color to fade out
    218             RainEffect::rainParticles->setColor(0,   0.3, 0.3, 0.5, 0.2 * progress); // grey blue 1
    219             RainEffect::rainParticles->setColor(0.5, 0.4, 0.4, 0.5, 0.2 * progress); // grey blue 2
    220             RainEffect::rainParticles->setColor(1,   0.7, 0.7, 0.7, 0.2 * progress); // light grey
    221 
    222             // decrease radius
    223             RainEffect::rainParticles->setRadius(0, 0.03 * progress);
    224             RainEffect::rainParticles->setRadius(0.2, 0.02 * progress);
    225             RainEffect::rainParticles->setRadius(1, 0.01 * progress);
    226 
    227             // decrease sound volume
    228             // this->soundSource.fadeout(this->rainBuffer, 10);
    229         } else
    230             this->deactivate();
    231     }
    232 
    233 }
    234 
    235 void RainEffect::startRaining() {
    236 
    237     if (this->rainActivate)
    238         this->deactivate();
    239 
    240     if (!this->rainFadeInDuration > 0)
    241         this->rainFadeInDuration = 20;
    242 
    243     this->localTimer = 0;
    244 
     292  if (!this->rainFadeInDuration > 0)
     293    this->rainFadeInDuration = 20;
     294
     295  this->localTimer = 0;
     296
     297  this->activate();
     298}
     299
     300/**
     301 * @brief stops raining slowly
     302 */
     303void RainEffect::stopRaining()
     304{
     305
     306  if (!this->rainActivate)
    245307    this->activate();
    246308
    247 }
    248 
    249 void RainEffect::stopRaining() {
    250 
    251     if (!this->rainActivate)
    252         this->activate();
    253 
    254     if (!this->rainFadeOutDuration > 0)
    255         this->rainFadeOutDuration = 20;
    256 
    257     this->localTimer = 0;
    258 
    259 }
     309  if (!this->rainFadeOutDuration > 0)
     310    this->rainFadeOutDuration = 20;
     311
     312  this->localTimer = 0;
     313}
     314
     315/**
     316 * @brief hides the rain
     317 */
     318void RainEffect::hideRain()
     319{
     320  RainEffect::rainParticles->setColor(0, 0,0,0, 0);
     321  RainEffect::rainParticles->setColor(0, 0,0,0, 0);
     322}
Note: See TracChangeset for help on using the changeset viewer.