Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/tools/ParticleInterface.cc @ 2171

Last change on this file since 2171 was 2161, checked in by landauf, 16 years ago
  • added more exceptions to handle problems while loading a level or single objects
  • dedicated server runs and clients may join, but there are still some heavy problems
  • Property svn:eol-style set to native
File size: 8.0 KB
RevLine 
[1505]1/*
[2065]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/**
[2114]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>
[2019]40#include <cassert>
[1505]41
[1552]42#include "GraphicsEngine.h"
[2161]43#include "core/Core.h"
[1563]44#include "core/CoreIncludes.h"
[1552]45#include "util/Convert.h"
[1505]46
[1552]47namespace orxonox
48{
[2065]49    unsigned int ParticleInterface::counter_s = 0;
50    ParticleInterface* ParticleInterface::currentParticleInterface_s = 0;
[1505]51
[2065]52    ParticleInterface::ParticleInterface(Ogre::SceneManager* scenemanager, const std::string& templateName, LODParticle::LOD detaillevel)
53    {
54        RegisterRootObject(ParticleInterface);
[1563]55
[2065]56        assert(scenemanager);
[2019]57
[2065]58        this->scenemanager_ = scenemanager;
59        this->sceneNode_ = 0;
[2161]60        this->particleSystem_ = 0;
[1563]61
[2065]62        this->bEnabled_ = true;
63        this->bVisible_ = true;
64        this->bAllowedByLOD_ = true;
65
[2161]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        }
[2065]80
81        this->setDetailLevel((unsigned int)detaillevel);
82    }
83
84    ParticleInterface::~ParticleInterface()
[1563]85    {
[2161]86        if (this->particleSystem_)
87        {
88            this->particleSystem_->removeAllEmitters();
89            this->detachFromSceneNode();
90            this->scenemanager_->destroyParticleSystem(this->particleSystem_);
91        }
[1563]92    }
[2065]93
94    void ParticleInterface::addToSceneNode(Ogre::SceneNode* sceneNode)
[1563]95    {
[2065]96        if (this->sceneNode_)
97            this->detachFromSceneNode();
98
[2161]99        if (this->particleSystem_)
100        {
101            this->sceneNode_ = sceneNode;
102            this->sceneNode_->attachObject(this->particleSystem_);
103        }
[1563]104    }
[1505]105
[2065]106    void ParticleInterface::detachFromSceneNode()
107    {
108        if (this->sceneNode_)
109        {
[2161]110            if (this->particleSystem_)
111                this->sceneNode_->detachObject(this->particleSystem_);
[2065]112            this->sceneNode_ = 0;
113        }
114    }
[1505]115
[2065]116    Ogre::ParticleEmitter* ParticleInterface::createNewEmitter()
117    {
[2161]118        if (this->particleSystem_ && this->particleSystem_->getNumEmitters() > 0)
[2065]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    {
[2161]129        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
[2065]130            return this->particleSystem_->getEmitter(emitterNr);
131        else
132            return 0;
133    }
134    void ParticleInterface::removeEmitter(unsigned int emitterNr)
135    {
[2161]136        if (this->particleSystem_ && (emitterNr < this->particleSystem_->getNumEmitters()))
[2065]137            this->particleSystem_->removeEmitter(emitterNr);
138    }
139    void ParticleInterface::removeAllEmitters()
140    {
[2161]141        if (this->particleSystem_)
142            this->particleSystem_->removeAllEmitters();
[2065]143    }
144    unsigned int ParticleInterface::getNumEmitters() const
145    {
[2161]146        if (this->particleSystem_)
147            return this->particleSystem_->getNumEmitters();
148        else
149            return 0;
[2065]150    }
[1505]151
[2065]152    Ogre::ParticleAffector* ParticleInterface::addAffector(const std::string& name)
[1552]153    {
[2161]154        if (this->particleSystem_)
155            return this->particleSystem_->addAffector(name);
156        else
157            return 0;
[1552]158    }
[2065]159    Ogre::ParticleAffector* ParticleInterface::getAffector(unsigned int affectorNr) const
160    {
[2161]161        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
[2065]162            return this->particleSystem_->getAffector(affectorNr);
163        else
164            return 0;
165    }
166    void ParticleInterface::removeAffector(unsigned int affectorNr)
167    {
[2161]168        if (this->particleSystem_ && (affectorNr < this->particleSystem_->getNumAffectors()))
[2065]169            this->particleSystem_->removeAffector(affectorNr);
170    }
171    void ParticleInterface::removeAllAffectors()
172    {
[2161]173        if (this->particleSystem_)
174            this->particleSystem_->removeAllAffectors();
[2065]175    }
176    unsigned int ParticleInterface::getNumAffectors() const
177    {
[2161]178        if (this->particleSystem_)
179            return this->particleSystem_->getNumAffectors();
180        else
181            return 0;
[2065]182    }
[1505]183
[2065]184    void ParticleInterface::setEnabled(bool enable)
[1552]185    {
[2065]186        this->bEnabled_ = enable;
187
[2161]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
[2065]193    void ParticleInterface::setVisible(bool visible)
194    {
195        this->bVisible_ = visible;
[1505]196
[2161]197        if (this->particleSystem_)
198            this->particleSystem_->setVisible(this->bVisible_ && this->bAllowedByLOD_);
[2065]199    }
[1563]200
[2065]201    void ParticleInterface::setDetailLevel(unsigned int level)
202    {
203        this->detaillevel_ = level;
[2161]204        if (GraphicsEngine::getInstancePtr())
205            this->detailLevelChanged(GraphicsEngine::getInstance().getDetailLevelParticle());
[2065]206    }
[1563]207
[2065]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
[2065]215        this->updateVisibility();
216    }
[1505]217
[2065]218    void ParticleInterface::updateVisibility()
219    {
220        this->setEnabled(this->isEnabled());
221        this->setVisible(this->isVisible());
222    }
[1505]223
[2065]224    void ParticleInterface::setSpeedFactor(float factor)
225    {
[2161]226        if (this->particleSystem_)
227        {
228//            this->particleSystem_->setSpeedFactor(Orxonox::getInstance().getTimeFactor() * factor);
229            this->particleSystem_->setSpeedFactor(1.0f * factor);
230        }
[2065]231    }
232    float ParticleInterface::getSpeedFactor() const
233    {
[2161]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;
[2065]241    }
242
243    bool ParticleInterface::getKeepParticlesInLocalSpace() const
244    {
[2161]245        if (this->particleSystem_)
246            return this->particleSystem_->getKeepParticlesInLocalSpace();
247        else
248            return false;
[2065]249    }
250    void ParticleInterface::setKeepParticlesInLocalSpace(bool keep)
251    {
[2161]252        if (this->particleSystem_)
253            this->particleSystem_->setKeepParticlesInLocalSpace(keep);
[2065]254    }
[1505]255}
Note: See TracBrowser for help on using the repository browser.