Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/tools/ParticleInterface.cc @ 2540

Last change on this file since 2540 was 2171, checked in by landauf, 16 years ago

merged revisions 2111-2170 from objecthierarchy branch back to trunk.

  • Property svn:eol-style set to native
File size: 8.0 KB
RevLine 
[1505]1/*
[2087]2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
[1505]28
29/**
[2171]30* @file
[1505]31* @brief class to control praticle effects
32*/
33
34#include "OrxonoxStableHeaders.h"
35#include "ParticleInterface.h"
36
[1552]37#include <OgreParticleSystem.h>
38#include <OgreParticleEmitter.h>
39#include <OgreSceneManager.h>
[2087]40#include <cassert>
[1505]41
[1552]42#include "GraphicsEngine.h"
[2171]43#include "core/Core.h"
[1563]44#include "core/CoreIncludes.h"
[1552]45#include "util/Convert.h"
[1505]46
[1552]47namespace orxonox
48{
[2087]49    unsigned int ParticleInterface::counter_s = 0;
50    ParticleInterface* ParticleInterface::currentParticleInterface_s = 0;
[1505]51
[2087]52    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::LOD detaillevel)
53    {
54        RegisterRootObject(ParticleInterface);
[1563]55
[2087]56        assert(scenemanager);
[1563]57
[2087]58        this->scenemanager_ = scenemanager;
59        this->sceneNode_ = 0;
[2171]60        this->particleSystem_ = 0;
[2087]61
62        this->bEnabled_ = true;
63        this->bVisible_ = true;
64        this->bAllowedByLOD_ = true;
65
[2171]66        if (Core::showsGraphics())
67        {
68            try
69            {
70                this->particleSystem_ = this->scenemanager_->createParticleSystem("particles" + getConvertedValue<unsigned int, std::string>(ParticleInterface::counter_s++), templateName);
71                this->particleSystem_->setSpeedFactor(1.0f);
72//                this->particleSystem_->setSpeedFactor(Orxonox::getInstance().getTimeFactor());
73            }
74            catch (...)
75            {
76                COUT(1) << "Error: Couln't load particle system \"" << templateName << "\"" << std::endl;
77                this->particleSystem_ = 0;
78            }
79        }
[2087]80
81        this->setDetailLevel((unsigned int)detaillevel);
82    }
83
84    ParticleInterface::~ParticleInterface()
[1563]85    {
[2171]86        if (this->particleSystem_)
87        {
88            this->particleSystem_->removeAllEmitters();
89            this->detachFromSceneNode();
90            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
91        }
[1563]92    }
[2087]93
94    void ParticleInterface::addToSceneNode(Ogre::SceneNode* sceneNode)
[1563]95    {
[2087]96        if (this->sceneNode_)
97            this->detachFromSceneNode();
98
[2171]99        if (this->particleSystem_)
100        {
101            this->sceneNode_ = sceneNode;
102            this->sceneNode_->attachObject(this->particleSystem_);
103        }
[1563]104    }
[1505]105
[2087]106    void ParticleInterface::detachFromSceneNode()
107    {
108        if (this->sceneNode_)
109        {
[2171]110            if (this->particleSystem_)
111                this->sceneNode_->detachObject(this->particleSystem_);
[2087]112            this->sceneNode_ = 0;
113        }
114    }
[1505]115
[2087]116    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
117    {
[2171]118        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
[2087]119        {
120            Ogre::ParticleEmitter* newemitter = this->particleSystem_->addEmitter(this->particleSystem_->getEmitter(0)->getType());
121            this->particleSystem_->getEmitter(0)->copyParametersTo(newemitter);
122            return newemitter;
123        }
124        else
125            return 0;
126    }
127    Ogre::ParticleEmitter* ParticleInterface::getEmitter(unsigned int emitterNr) const
128    {
[2171]129        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
[2087]130            return this->particleSystem_->getEmitter(emitterNr);
131        else
132            return 0;
133    }
134    void ParticleInterface::removeEmitter(unsigned int emitterNr)
135    {
[2171]136        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
[2087]137            this->particleSystem_->removeEmitter(emitterNr);
138    }
139    void ParticleInterface::removeAllEmitters()
140    {
[2171]141        if (this->particleSystem_)
142            this->particleSystem_->removeAllEmitters();
[2087]143    }
144    unsigned int ParticleInterface::getNumEmitters() const
145    {
[2171]146        if (this->particleSystem_)
147            return this->particleSystem_->getNumEmitters();
148        else
149            return 0;
[2087]150    }
[1505]151
[2087]152    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
[1552]153    {
[2171]154        if (this->particleSystem_)
155            return this->particleSystem_->addAffector(name);
156        else
157            return 0;
[1552]158    }
[2087]159    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr) const
160    {
[2171]161        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
[2087]162            return this->particleSystem_->getAffector(affectorNr);
163        else
164            return 0;
165    }
166    void ParticleInterface::removeAffector(unsigned int affectorNr)
167    {
[2171]168        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
[2087]169            this->particleSystem_->removeAffector(affectorNr);
170    }
171    void ParticleInterface::removeAllAffectors()
172    {
[2171]173        if (this->particleSystem_)
174            this->particleSystem_->removeAllAffectors();
[2087]175    }
176    unsigned int ParticleInterface::getNumAffectors() const
177    {
[2171]178        if (this->particleSystem_)
179            return this->particleSystem_->getNumAffectors();
180        else
181            return 0;
[2087]182    }
[1505]183
[2087]184    void ParticleInterface::setEnabled(bool enable)
[1552]185    {
[2087]186        this->bEnabled_ = enable;
187
[2171]188        if (this->particleSystem_)
189            for (unsigned int i = 0; i < this->particleSystem_->getNumEmitters(); i++)
190                this->particleSystem_->getEmitter(i)->setEnabled(this->bEnabled_ && this->bAllowedByLOD_);
[1505]191    }
192
[2087]193    void ParticleInterface::setVisible(bool visible)
194    {
195        this->bVisible_ = visible;
[1505]196
[2171]197        if (this->particleSystem_)
198            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
[2087]199    }
[1563]200
[2087]201    void ParticleInterface::setDetailLevel(unsigned int level)
202    {
203        this->detaillevel_ = level;
[2171]204        if (GraphicsEngine::getInstancePtr())
205            this->detailLevelChanged(GraphicsEngine::getInstance().getDetailLevelParticle());
[2087]206    }
[1563]207
[2087]208    void ParticleInterface::detailLevelChanged(unsigned int newlevel)
209    {
210        if (newlevel >= (unsigned int)this->detaillevel_)
211            this->bAllowedByLOD_ = true;
212        else
213            this->bAllowedByLOD_ = false;
[1563]214
[2087]215        this->updateVisibility();
216    }
[1505]217
[2087]218    void ParticleInterface::updateVisibility()
219    {
220        this->setEnabled(this->isEnabled());
221        this->setVisible(this->isVisible());
222    }
[1505]223
[2087]224    void ParticleInterface::setSpeedFactor(float factor)
225    {
[2171]226        if (this->particleSystem_)
227        {
228//            this->particleSystem_->setSpeedFactor(Orxonox::getInstance().getTimeFactor() * factor);
229            this->particleSystem_->setSpeedFactor(1.0f * factor);
230        }
[2087]231    }
232    float ParticleInterface::getSpeedFactor() const
233    {
[2171]234        if (this->particleSystem_)
235        {
236//            return (this->particleSystem_->getSpeedFactor() / Orxonox::getInstance().getTimeFactor());
237            return (this->particleSystem_->getSpeedFactor() / 1.0f);
238        }
239        else
240            return 1.0f;
[2087]241    }
242
243    bool ParticleInterface::getKeepParticlesInLocalSpace() const
244    {
[2171]245        if (this->particleSystem_)
246            return this->particleSystem_->getKeepParticlesInLocalSpace();
247        else
248            return false;
[2087]249    }
250    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
251    {
[2171]252        if (this->particleSystem_)
253            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
[2087]254    }
[1505]255}
Note: See TracBrowser for help on using the repository browser.