Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/orxonox/objects/Scene.cc @ 4725

Last change on this file since 4725 was 3325, checked in by rgrieder, 15 years ago

Merged orxonox_cast related revisions from core4 back to trunk.

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