Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/bsp_model/src/lib/graphics/effects/snow_effect.cc @ 8372

Last change on this file since 8372 was 7810, checked in by bensch, 19 years ago

orxonox/trunk: merged the Weather effects back here

File size: 5.5 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 "snow_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 "debug.h"
23
24#include "p_node.h"
25#include "state.h"
26#include "sprite_particles.h"
27#include "plane_emitter.h"
28#include "shell_command.h"
29
30#include "parser/tinyxml/tinyxml.h"
31
32SHELL_COMMAND(activate, SnowEffect, activateSnow);
33SHELL_COMMAND(deactivate, SnowEffect, deactivateSnow);
34
35using namespace std;
36
37CREATE_FACTORY(SnowEffect, CL_SNOW_EFFECT);
38
39SnowEffect::SnowEffect(const TiXmlElement* root)
40{
41        this->setClassID(CL_SNOW_EFFECT, "SnowEffect");
42
43        this->init();
44
45        if (root != NULL)
46                this->loadParams(root);
47
48        //load wind sound
49        if (this->snowWindForce > 1) {
50                if (this->windBuffer != NULL)
51                        ResourceManager::getInstance()->unload(this->windBuffer);
52                        this->windBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/wind.wav", WAV);
53        }
54
55        this->activate();
56}
57
58
59SnowEffect::~SnowEffect()
60{
61        this->deactivate();
62}
63
64SpriteParticles* SnowEffect::snowParticles = NULL;
65
66void SnowEffect::loadParams(const TiXmlElement* root)
67{
68        WeatherEffect::loadParams(root);
69
70        LoadParam(root, "numParticles", this, SnowEffect, numParticles);
71        LoadParam(root, "materialTexture", this, SnowEffect, materialTexture);
72        LoadParam(root, "lifeSpans", this, SnowEffect, lifeSpan);
73        LoadParam(root, "radius", this, SnowEffect, radius);
74        LoadParam(root, "mass", this, SnowEffect, mass);
75        LoadParam(root, "emissionRate", this, SnowEffect, emissionRate);
76        LoadParam(root, "emissionVelocity", this, SnowEffect, emissionVelocity);
77  LoadParam(root, "wind", this, SnowEffect, wind);
78        LoadParam(root, "size", this, SnowEffect, size);
79        LoadParam(root, "coord", this, SnowEffect, coord);
80}
81
82bool SnowEffect::init()
83{
84        this->emitter = new PlaneEmitter();
85
86        // Default values
87        this->particles = 12000;
88        this->texture = "maps/snow_flake_01_32x32.png";
89        this->life = 8;
90        this->randomLife = 2;
91        this->snowRadius = 3.5;
92        this->randomRadius = 1;
93        this->snowMass = 1.0;
94        this->randomMass = 0.3;
95        this->rate = 900;
96        this->velocity = -100;
97        this->randomVelocity = 5;
98        this->angle = 0.5;
99        this->randomAngle = 0.2;
100        this->alpha = 0.5;
101        this->snowSize = Vector2D(2500, 2500);
102  this->snowCoord = Vector(100,450,400);
103        this->snowWindForce = 1;
104
105        this->activated = false;
106}
107
108bool SnowEffect::activate()
109{
110        PRINTF(0)("Activating SnowEffect\n");
111        activated = true;
112
113        SnowEffect::snowParticles = new SpriteParticles(particles);
114        SnowEffect::snowParticles->setName("SnowEffectTrailParticles");
115        SnowEffect::snowParticles->setMaterialTexture(texture);
116        SnowEffect::snowParticles->setLifeSpan(life, randomLife);
117        SnowEffect::snowParticles->setRadius(0.0, snowRadius, randomRadius);
118        SnowEffect::snowParticles->setRadius(0.2, snowRadius, randomRadius*0.8);
119        SnowEffect::snowParticles->setRadius(1.0, snowRadius, randomRadius*0.5);
120        SnowEffect::snowParticles->setMass(0, snowMass, randomMass);
121        SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
122        SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
123        SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
124
125        this->emitter->setSystem(SnowEffect::snowParticles);
126
127        this->emitter->setRelCoor(snowCoord);
128        this->emitter->setEmissionRate(rate);
129        this->emitter->setEmissionVelocity(velocity, randomVelocity);
130        this->emitter->setSpread(angle * this->snowWindForce , randomAngle * this->snowWindForce);
131        this->emitter->setSize(snowSize);
132
133  //SnowEffect::snowParticles->precache(8);
134
135        if (this->snowWindForce > 1)
136                this->soundSource.loop(this->windBuffer);
137}
138
139
140bool SnowEffect::deactivate()
141{
142        PRINTF(0)("Deactivating SnowEffect\n");
143        activated = false;
144
145        this->emitter->setSystem(NULL);
146
147        if (this->windBuffer != NULL)
148                ResourceManager::getInstance()->unload(this->windBuffer);
149
150}
151
152void SnowEffect::activateSnow()
153{
154        this->activate();
155}
156
157void SnowEffect::deactivateSnow()
158{
159        this->deactivate();
160}
161
162void SnowEffect::draw() const
163{
164}
165
166void SnowEffect::tick(float dt)
167{
168  float distance = (State::getCameraNode()->getAbsCoor() - Vector(snowCoord.x, State::getCameraNode()->getAbsCoor().y, snowCoord.z)).len();
169 
170  if(activated)
171  {
172    if(distance > 0.3*snowSize.x || distance > 0.3*snowSize.y)
173                  this->deactivate();
174    else if(distance > 0.25*snowSize.x || distance > 0.25*snowSize.y) 
175      this->alpha = 0.15;
176    else if(distance > 0.2*snowSize.x || distance > 0.2*snowSize.y) 
177      this->alpha = 0.25;
178    else if(distance > 0.1*snowSize.x || distance > 0.1*snowSize.y) 
179      this->alpha = 0.4;
180
181    SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
182    SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
183    SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
184  }
185  else
186  {
187  if(distance < 0.3*snowSize.x || distance < 0.3*snowSize.y )
188    this->activate();
189  if( distance < 0.25*snowSize.x || distance < 0.25*snowSize.y )
190    this->alpha = 0.25;
191  else if( distance < 0.2*snowSize.x || distance < 0.2*snowSize.y )
192    this->alpha = 0.4;
193  else if( distance < 0.1*snowSize.x || distance < 0.1*snowSize.y )
194    this->alpha = 0.5;
195
196    SnowEffect::snowParticles->setColor(0,1, 1, 1, alpha);
197    SnowEffect::snowParticles->setColor(.5, .6, .6, .6, alpha/2);
198    SnowEffect::snowParticles->setColor(1, .0, .0, .0, .0);
199  }
200}
201
Note: See TracBrowser for help on using the repository browser.