Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/Scene.cc @ 2518

Last change on this file since 2518 was 2491, checked in by rgrieder, 16 years ago
  • "gravity" xml parameter got missed in Scene
  • fixed issue with rotationrate and rotationaxis in MobileEntity
  • updated msvc files
  • Property svn:eol-style set to native
File size: 13.7 KB
RevLine 
[2072]1/*
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
[2459]24 *      Reto Grieder (physics)
[2072]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "OrxonoxStableHeaders.h"
31#include "Scene.h"
32
33#include <OgreRoot.h>
34#include <OgreSceneManagerEnumerator.h>
35#include <OgreSceneNode.h>
36
[2459]37#include "BulletCollision/BroadphaseCollision/btAxisSweep3.h"
38#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
39#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
40#include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
41
[2072]42#include "core/CoreIncludes.h"
43#include "core/Core.h"
44#include "core/XMLPort.h"
[2459]45#include "tools/BulletConversions.h"
46#include "objects/worldentities/WorldEntity.h"
[2072]47
48namespace orxonox
49{
50    CreateFactory(Scene);
51
[2171]52    Scene::Scene(BaseObject* creator) : BaseObject(creator), Synchronisable(creator)
[2072]53    {
54        RegisterObject(Scene);
55
56        this->setScene(this);
[2485]57        this->bShadows_ = true;
[2072]58
59        if (Core::showsGraphics())
60        {
61            if (Ogre::Root::getSingletonPtr())
62            {
63                this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC);
64                this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
65            }
66            else
67            {
68                this->sceneManager_ = 0;
69                this->rootSceneNode_ = 0;
70            }
71        }
72        else
73        {
74            // create a dummy SceneManager of our own since we don't have Ogre::Root.
[2468]75            this->sceneManagerDedicated_ = new Ogre::DefaultSceneManager("");
76            this->rootSceneNode_ = this->sceneManagerDedicated_->getRootSceneNode();
77            this->sceneManager_ = 0;
[2072]78        }
79
[2459]80        // No physics yet, XMLPort will do that.
81        const int defaultMaxWorldSize = 100000;
82        this->negativeWorldRange_ = Vector3::UNIT_SCALE * -defaultMaxWorldSize;
83        this->positiveWorldRange_ = Vector3::UNIT_SCALE *  defaultMaxWorldSize;
84        this->gravity_ = Vector3::ZERO;
85        this->physicalWorld_   = 0;
86        this->solver_          = 0;
87        this->dispatcher_      = 0;
88        this->collisionConfig_ = 0;
89        this->broadphase_      = 0;
90
[2072]91        this->registerVariables();
92    }
93
94    Scene::~Scene()
95    {
96        if (this->isInitialized())
97        {
98            if (Ogre::Root::getSingletonPtr())
99            {
100                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
101            }
102            else if (!Core::showsGraphics())
103            {
[2468]104                delete this->sceneManagerDedicated_;
[2072]105            }
[2459]106
107            this->setPhysicalWorld(false);
[2072]108        }
109    }
110
111    void Scene::XMLPort(Element& xmlelement, XMLPort::Mode mode)
112    {
113        SUPER(Scene, XMLPort, xmlelement, mode);
114
115        XMLPortParam(Scene, "skybox", setSkybox, getSkybox, xmlelement, mode);
116        XMLPortParam(Scene, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1));
117        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
118
[2491]119        XMLPortParam(Scene, "gravity", setGravity, getGravity, xmlelement, mode);
[2459]120        XMLPortParam(Scene, "negativeWorldRange", setNegativeWorldRange, getNegativeWorldRange, xmlelement, mode);
121        XMLPortParam(Scene, "positiveWorldRange", setPositiveWorldRange, getPositiveWorldRange, xmlelement, mode);
122        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValues(true);
123
[2072]124        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
125    }
126
127    void Scene::registerVariables()
128    {
[2459]129        registerVariable(this->skybox_,             variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
130        registerVariable(this->ambientLight_,       variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
131        registerVariable(this->negativeWorldRange_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_negativeWorldRange));
132        registerVariable(this->positiveWorldRange_, variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_positiveWorldRange));
133        registerVariable(this->gravity_,            variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_gravity));
134        registerVariable(this->bHasPhysics_,        variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_hasPhysics));
[2485]135        registerVariable(this->bShadows_,           variableDirection::toclient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyShadows));
[2072]136    }
137
[2459]138    void Scene::setNegativeWorldRange(const Vector3& range)
139    {
140        if (range.length() < 10.0f)
141        {
142            CCOUT(2) << "Warning: Setting the negative world range to a very small value: "
143                     << omni_cast<std::string>(range) << std::endl;
144        }
145        if (this->hasPhysics())
146        {
[2485]147            CCOUT(2) << "Warning: Attempting to set the physical world range at run time. "
[2459]148                     << "This causes a complete physical reload which might take some time." << std::endl;
149            this->setPhysicalWorld(false);
150            this->negativeWorldRange_ = range;
151            this->setPhysicalWorld(true);
152        }
153        else
154            this->negativeWorldRange_ = range;
155    }
156
157    void Scene::setPositiveWorldRange(const Vector3& range)
158    {
159        if (range.length() < 10.0f)
160        {
161            CCOUT(2) << "Warning: Setting the positive world range to a very small value: "
162                     << omni_cast<std::string>(range) << std::endl;
163        }
164        if (this->hasPhysics())
165        {
[2485]166            CCOUT(2) << "Warning: Attempting to set the physical world range at run time. "
[2459]167                     << "This causes a complete physical reload which might take some time." << std::endl;
168            this->setPhysicalWorld(false);
169            this->positiveWorldRange_ = range;
170            this->setPhysicalWorld(true);
171        }
172        else
173            this->positiveWorldRange_ = range;
174    }
175
176    void Scene::setGravity(const Vector3& gravity)
177    {
178        this->gravity_ = gravity;
179        if (this->hasPhysics())
180            this->physicalWorld_->setGravity(omni_cast<btVector3>(this->gravity_));
181    }
182
183    void Scene::setPhysicalWorld(bool wantPhysics)
184    {
185        this->bHasPhysics_ = wantPhysics;
186        if (wantPhysics && !hasPhysics())
187        {
188            // Note: These are all little known default classes and values.
189            //       It would require further investigation to properly dertermine the right choices.
190            this->broadphase_      = new bt32BitAxisSweep3(
191                omni_cast<btVector3>(this->negativeWorldRange_), omni_cast<btVector3>(this->positiveWorldRange_));
192            this->collisionConfig_ = new btDefaultCollisionConfiguration();
193            this->dispatcher_      = new btCollisionDispatcher(this->collisionConfig_);
[2491]194            this->solver_          = new btSequentialImpulseConstraintSolver();
[2459]195
196            this->physicalWorld_   = new btDiscreteDynamicsWorld(this->dispatcher_, this->broadphase_, this->solver_, this->collisionConfig_);
197            this->physicalWorld_->setGravity(omni_cast<btVector3>(this->gravity_));
[2466]198
199            // also set the collision callback variable.
200            // Note: This is a global variable which we assign a static function.
201            // TODO: Check whether this (or anything about Bullet) works with multiple physics engine instances.
202            gContactAddedCallback = &Scene::collisionCallback;
[2459]203        }
204        else if (!wantPhysics && hasPhysics())
205        {
206            // Remove all WorldEntities and shove them to the queue since they would still like to be in a physical world.
207            for (std::set<WorldEntity*>::const_iterator it = this->physicalObjects_.begin();
208                it != this->physicalObjects_.end(); ++it)
209            {
210                this->physicalWorld_->removeRigidBody((*it)->getPhysicalBody());
211                this->physicalObjectQueue_.insert(*it);
212            }
213            this->physicalObjects_.clear();
214
215            delete this->physicalWorld_;
216            delete this->solver_;
217            delete this->dispatcher_;
218            delete this->collisionConfig_;
219            delete this->broadphase_;
220            this->physicalWorld_   = 0;
221            this->solver_          = 0;
222            this->dispatcher_      = 0;
223            this->collisionConfig_ = 0;
224            this->broadphase_      = 0;
225        }
226    }
227
228    void Scene::tick(float dt)
229    {
230        if (!Core::showsGraphics())
231        {
232            // We need to update the scene nodes if we don't render
233            this->rootSceneNode_->_update(true, false);
234        }
235        if (this->hasPhysics())
236        {
237            // TODO: This here is bad practice! It will slow down the first tick() by ages.
238            //       Rather have an initialise() method as well, called by the Level after everything has been loaded.
239            if (this->physicalObjectQueue_.size() > 0)
240            {
241                // Add all scheduled WorldEntities
242                for (std::set<WorldEntity*>::const_iterator it = this->physicalObjectQueue_.begin();
243                    it != this->physicalObjectQueue_.end(); ++it)
244                {
245                    this->physicalWorld_->addRigidBody((*it)->getPhysicalBody());
[2463]246                    this->physicalObjects_.insert(*it);
[2459]247                }
248                this->physicalObjectQueue_.clear();
249            }
250
251            // Note: 60 means that Bullet will do physics correctly down to 1 frames per seconds.
252            //       Under that mark, the simulation will "loose time" and get unusable.
253            physicalWorld_->stepSimulation(dt, 60);
254        }
255    }
256
[2072]257    void Scene::setSkybox(const std::string& skybox)
258    {
259        if (Core::showsGraphics() && this->sceneManager_)
260            this->sceneManager_->setSkyBox(true, skybox);
261
262        this->skybox_ = skybox;
263    }
264
265    void Scene::setAmbientLight(const ColourValue& colour)
266    {
267        if (Core::showsGraphics() && this->sceneManager_)
268            this->sceneManager_->setAmbientLight(colour);
269
270        this->ambientLight_ = colour;
271    }
272
273    void Scene::setShadow(bool bShadow)
274    {
275        if (Core::showsGraphics() && this->sceneManager_)
276        {
277            if (bShadow)
278                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
279            else
280                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
281        }
282
283        this->bShadows_ = bShadow;
284    }
285
286    void Scene::addObject(BaseObject* object)
287    {
288        this->objects_.push_back(object);
289        object->setScene(this);
290    }
291
292    BaseObject* Scene::getObject(unsigned int index) const
293    {
294        unsigned int i = 0;
295        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
296        {
297            if (i == index)
298                return (*it);
299            ++i;
300        }
301        return 0;
302    }
[2171]303
[2459]304    void Scene::addPhysicalObject(WorldEntity* object)
[2171]305    {
[2459]306        if (object)
[2171]307        {
[2459]308            std::set<WorldEntity*>::iterator it = this->physicalObjects_.find(object);
309            if (it != this->physicalObjects_.end())
310                return;
311
312            this->physicalObjectQueue_.insert(object);
[2171]313        }
314    }
[2459]315
316    void Scene::removePhysicalObject(WorldEntity* object)
317    {
318        // check queue first
319        std::set<WorldEntity*>::iterator it = this->physicalObjectQueue_.find(object);
320        if (it != this->physicalObjectQueue_.end())
321        {
322            this->physicalObjectQueue_.erase(it);
323            return;
324        }
325
326        it = this->physicalObjects_.find(object);
327        if (it == this->physicalObjects_.end())
328            return;
329        this->physicalObjects_.erase(it);
330
331        if (this->hasPhysics())
332            this->physicalWorld_->removeRigidBody(object->getPhysicalBody());
333    }
[2466]334
335    /*static*/ bool Scene::collisionCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,
336                                             int index0, const btCollisionObject* colObj1, int partId1, int index1)
337    {
338        // get the WorldEntity pointers
339        WorldEntity* object0 = (WorldEntity*)colObj0->getUserPointer();
340        assert(dynamic_cast<WorldEntity*>(object0));
341        WorldEntity* object1 = (WorldEntity*)colObj1->getUserPointer();
342        assert(dynamic_cast<WorldEntity*>(object1));
343
344        // false means that bullet will assume we didn't modify the contact
345        bool modified = false;
346        if (object0->isCollisionCallbackActive())
347        {
348            modified |= object0->collidesAgainst(object1, cp);
349            if (object1->isCollisionCallbackActive())
350                modified |= object1->collidesAgainst(object0, cp);
351        }
352        else
353            modified |= object1->collidesAgainst(object0, cp);
354
355        return modified;
356    }
[2072]357}
Note: See TracBrowser for help on using the repository browser.