Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/Scene.cc @ 10255

Last change on this file since 10255 was 10216, checked in by landauf, 10 years ago

merged branch presentationFS14 back to trunk

  • Property svn:eol-style set to native
File size: 15.4 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
[2662]24 *      Reto Grieder (physics)
[2072]25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "Scene.h"
31
32#include <OgreRoot.h>
[3196]33#include <OgreSceneManager.h>
[2072]34#include <OgreSceneManagerEnumerator.h>
35#include <OgreSceneNode.h>
36
[3196]37#include <BulletCollision/BroadphaseCollision/btAxisSweep3.h>
38#include <BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h>
39#include <BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h>
40#include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
[2662]41
[2072]42#include "core/CoreIncludes.h"
[2896]43#include "core/GameMode.h"
[5929]44#include "core/GUIManager.h"
[2072]45#include "core/XMLPort.h"
[10196]46#include "core/command/ConsoleCommand.h"
[2662]47#include "tools/BulletConversions.h"
[10196]48#include "tools/BulletDebugDrawer.h"
49#include "tools/DebugDrawer.h"
[5929]50#include "Radar.h"
[5735]51#include "worldentities/WorldEntity.h"
[7163]52#include "Level.h"
[2072]53
54namespace orxonox
55{
[9667]56    RegisterClass(Scene);
[2072]57
[10196]58    SetConsoleCommand("Scene", "debugDrawPhysics", &Scene::consoleCommand_debugDrawPhysics).addShortcut().defaultValue(1, true).defaultValue(2, 0.5f);
59
[9667]60    Scene::Scene(Context* context) : BaseObject(context), Synchronisable(context), Context(context)
[2072]61    {
62        RegisterObject(Scene);
63
[5929]64        this->setScene(SmartPtr<Scene>(this, false), OBJECTID_UNKNOWN);
[2662]65        this->bShadows_ = true;
[10196]66        this->bDebugDrawPhysics_ = false;
67        this->debugDrawer_ = NULL;
[6417]68        this->soundReferenceDistance_ = 20.0;
[2072]69
[2896]70        if (GameMode::showsGraphics())
[2072]71        {
[3196]72            assert(Ogre::Root::getSingletonPtr());
73            this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC);
74            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
[5929]75
76            this->radar_ = new Radar();
[2072]77        }
78        else
79        {
80            // create a dummy SceneManager of our own since we don't have Ogre::Root.
81            this->sceneManager_ = new Ogre::DefaultSceneManager("");
82            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
[5929]83
84            this->radar_ = 0;
[2072]85        }
86
[2662]87        // No physics yet, XMLPort will do that.
88        const int defaultMaxWorldSize = 100000;
89        this->negativeWorldRange_ = Vector3::UNIT_SCALE * -defaultMaxWorldSize;
90        this->positiveWorldRange_ = Vector3::UNIT_SCALE *  defaultMaxWorldSize;
91        this->gravity_ = Vector3::ZERO;
92        this->physicalWorld_   = 0;
93        this->solver_          = 0;
94        this->dispatcher_      = 0;
95        this->collisionConfig_ = 0;
96        this->broadphase_      = 0;
[2072]97
98        this->registerVariables();
99    }
100
101    Scene::~Scene()
102    {
103        if (this->isInitialized())
104        {
[10192]105            this->setPhysicalWorld(false);
106
107            if (this->radar_)
108                this->radar_->destroy();
109
[3196]110            if (GameMode::showsGraphics())
[2072]111                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
[3196]112            else
[2072]113                delete this->sceneManager_;
114        }
115    }
116
117    void Scene::XMLPort(Element& xmlelement, XMLPort::Mode mode)
118    {
119        SUPER(Scene, XMLPort, xmlelement, mode);
120
121        XMLPortParam(Scene, "skybox", setSkybox, getSkybox, xmlelement, mode);
[3196]122        XMLPortParam(Scene, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2f, 0.2f, 0.2f, 1.0f));
[2072]123        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
[6417]124        XMLPortParam(Scene, "soundReferenceDistance", setSoundReferenceDistance, getSoundReferenceDistance, xmlelement, mode);
[2072]125
[2662]126        XMLPortParam(Scene, "gravity", setGravity, getGravity, xmlelement, mode);
127        XMLPortParam(Scene, "negativeWorldRange", setNegativeWorldRange, getNegativeWorldRange, xmlelement, mode);
128        XMLPortParam(Scene, "positiveWorldRange", setPositiveWorldRange, getPositiveWorldRange, xmlelement, mode);
129        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValues(true);
130
[2072]131        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
132    }
133
134    void Scene::registerVariables()
135    {
[3280]136        registerVariable(this->skybox_,             VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
137        registerVariable(this->ambientLight_,       VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
138        registerVariable(this->negativeWorldRange_, VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_negativeWorldRange));
139        registerVariable(this->positiveWorldRange_, VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_positiveWorldRange));
140        registerVariable(this->gravity_,            VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_gravity));
141        registerVariable(this->bHasPhysics_,        VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_hasPhysics));
142        registerVariable(this->bShadows_,           VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyShadows));
[7163]143        registerVariable(this->getLevel(),          VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::changedLevel));
[2072]144    }
145
[2662]146    void Scene::setNegativeWorldRange(const Vector3& range)
147    {
148        if (range.length() < 10.0f)
149        {
[8858]150            orxout(internal_warning) << "Setting the negative world range to a very small value: "
151                                     << multi_cast<std::string>(range) << endl;
[2662]152        }
153        if (this->hasPhysics())
154        {
[8858]155            orxout(internal_warning) << "Attempting to set the physical world range at run time. "
156                                     << "This causes a complete physical reload which might take some time." << endl;
[2662]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        {
[8858]169            orxout(internal_warning) << "Setting the positive world range to a very small value: "
170                                     << multi_cast<std::string>(range) << endl;
[2662]171        }
172        if (this->hasPhysics())
173        {
[8858]174            orxout(internal_warning) << "Attempting to set the physical world range at run time. "
175                                     << "This causes a complete physical reload which might take some time." << endl;
[2662]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())
[3196]188            this->physicalWorld_->setGravity(multi_cast<btVector3>(this->gravity_));
[2662]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(
[3196]199                multi_cast<btVector3>(this->negativeWorldRange_), multi_cast<btVector3>(this->positiveWorldRange_));
[2662]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_);
[3196]205            this->physicalWorld_->setGravity(multi_cast<btVector3>(this->gravity_));
[2662]206
[10196]207            this->debugDrawer_ = new BulletDebugDrawer(this->sceneManager_);
208            this->physicalWorld_->setDebugDrawer(this->debugDrawer_);
209
[2662]210            // also set the collision callback variable.
211            // Note: This is a global variable which we assign a static function.
212            // TODO: Check whether this (or anything about Bullet) works with multiple physics engine instances.
213            gContactAddedCallback = &Scene::collisionCallback;
214        }
215        else if (!wantPhysics && hasPhysics())
216        {
217            // Remove all WorldEntities and shove them to the queue since they would still like to be in a physical world.
218            for (std::set<WorldEntity*>::const_iterator it = this->physicalObjects_.begin();
219                it != this->physicalObjects_.end(); ++it)
220            {
221                this->physicalWorld_->removeRigidBody((*it)->physicalBody_);
222                this->physicalObjectQueue_.insert(*it);
223            }
224            this->physicalObjects_.clear();
225
[10196]226            delete this->debugDrawer_;
[2662]227            delete this->physicalWorld_;
228            delete this->solver_;
229            delete this->dispatcher_;
230            delete this->collisionConfig_;
231            delete this->broadphase_;
232            this->physicalWorld_   = 0;
233            this->solver_          = 0;
234            this->dispatcher_      = 0;
235            this->collisionConfig_ = 0;
236            this->broadphase_      = 0;
237        }
238    }
239
240    void Scene::tick(float dt)
241    {
[2896]242        if (!GameMode::showsGraphics())
[2662]243        {
244            // We need to update the scene nodes if we don't render
245            this->rootSceneNode_->_update(true, false);
246        }
247        if (this->hasPhysics())
248        {
249            // TODO: This here is bad practice! It will slow down the first tick() by ages.
250            //       Rather have an initialise() method as well, called by the Level after everything has been loaded.
251            if (this->physicalObjectQueue_.size() > 0)
252            {
253                // Add all scheduled WorldEntities
254                for (std::set<WorldEntity*>::const_iterator it = this->physicalObjectQueue_.begin();
255                    it != this->physicalObjectQueue_.end(); ++it)
256                {
257                    this->physicalWorld_->addRigidBody((*it)->physicalBody_);
258                    this->physicalObjects_.insert(*it);
259                }
260                this->physicalObjectQueue_.clear();
261            }
262
263            // Note: 60 means that Bullet will do physics correctly down to 1 frames per seconds.
264            //       Under that mark, the simulation will "loose time" and get unusable.
265            physicalWorld_->stepSimulation(dt, 60);
[10196]266
267            if (this->bDebugDrawPhysics_)
268                physicalWorld_->debugDrawWorld();
[2662]269        }
270    }
271
[2072]272    void Scene::setSkybox(const std::string& skybox)
273    {
[9348]274        try
275        {
276            if (GameMode::showsGraphics() && this->sceneManager_)
277                this->sceneManager_->setSkyBox(true, skybox);
278        }
279        catch (const Ogre::Exception&)
280        {
281            orxout(internal_error) << "Could not load skybox '" << skybox << "':" << endl;
282            orxout(internal_error) << Exception::handleMessage() << endl;
283        }
[2072]284
285        this->skybox_ = skybox;
286    }
287
288    void Scene::setAmbientLight(const ColourValue& colour)
289    {
[2896]290        if (GameMode::showsGraphics() && this->sceneManager_)
[2072]291            this->sceneManager_->setAmbientLight(colour);
292
293        this->ambientLight_ = colour;
294    }
295
296    void Scene::setShadow(bool bShadow)
297    {
[2896]298        if (GameMode::showsGraphics() && this->sceneManager_)
[2072]299        {
300            if (bShadow)
301                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
302            else
303                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
304        }
305
306        this->bShadows_ = bShadow;
307    }
308
309    void Scene::addObject(BaseObject* object)
310    {
311        this->objects_.push_back(object);
[5929]312        object->setScene(this, this->getObjectID());
[2072]313    }
314
315    BaseObject* Scene::getObject(unsigned int index) const
316    {
317        unsigned int i = 0;
318        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
319        {
320            if (i == index)
321                return (*it);
322            ++i;
323        }
324        return 0;
325    }
[2171]326
[2662]327    void Scene::addPhysicalObject(WorldEntity* object)
[2171]328    {
[2662]329        if (object)
[2171]330        {
[2662]331            std::set<WorldEntity*>::iterator it = this->physicalObjects_.find(object);
332            if (it != this->physicalObjects_.end())
333                return;
334
335            this->physicalObjectQueue_.insert(object);
[2171]336        }
337    }
[2662]338
339    void Scene::removePhysicalObject(WorldEntity* object)
340    {
341        // check queue first
342        std::set<WorldEntity*>::iterator it = this->physicalObjectQueue_.find(object);
343        if (it != this->physicalObjectQueue_.end())
344        {
345            this->physicalObjectQueue_.erase(it);
346            return;
347        }
348
349        it = this->physicalObjects_.find(object);
350        if (it == this->physicalObjects_.end())
351            return;
352        this->physicalObjects_.erase(it);
353
354        if (this->hasPhysics())
355            this->physicalWorld_->removeRigidBody(object->physicalBody_);
356    }
357
358    /*static*/ bool Scene::collisionCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,
359                                             int index0, const btCollisionObject* colObj1, int partId1, int index1)
360    {
361        // get the WorldEntity pointers
[6417]362        SmartPtr<WorldEntity> object0 = static_cast<WorldEntity*>(colObj0->getUserPointer());
363        SmartPtr<WorldEntity> object1 = static_cast<WorldEntity*>(colObj1->getUserPointer());
[2662]364
[10216]365        // get the CollisionShape pointers
366        const btCollisionShape* cs0 = colObj0->getCollisionShape();
367        const btCollisionShape* cs1 = colObj1->getCollisionShape();
368
[2662]369        // false means that bullet will assume we didn't modify the contact
370        bool modified = false;
371        if (object0->isCollisionCallbackActive())
[10216]372            modified |= object0->collidesAgainst(object1, cs1, cp);
[6064]373        if (object1->isCollisionCallbackActive())
[10216]374            modified |= object1->collidesAgainst(object0, cs0, cp);
[2662]375
376        return modified;
377    }
[10196]378
379    void Scene::setDebugDrawPhysics(bool bDraw, bool bFill, float fillAlpha)
380    {
381        this->bDebugDrawPhysics_ = bDraw;
382        if (this->debugDrawer_)
383            this->debugDrawer_->configure(bFill, fillAlpha);
384    }
385
386    /*static*/ void Scene::consoleCommand_debugDrawPhysics(bool bDraw, bool bFill, float fillAlpha)
387    {
388        for (ObjectListIterator<Scene> it = ObjectList<Scene>::begin(); it != ObjectList<Scene>::end(); ++it)
389            it->setDebugDrawPhysics(bDraw, bFill, fillAlpha);
390    }
[2072]391}
Note: See TracBrowser for help on using the repository browser.