Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/Scene.cc @ 2506

Last change on this file since 2506 was 2448, checked in by rgrieder, 16 years ago

Spectator update with new physics stuff.

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