Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/Scene.cc @ 5920

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