Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v3/src/orxonox/Scene.cc @ 11058

Last change on this file since 11058 was 11054, checked in by landauf, 9 years ago

merged branch cpp11_v2 into cpp11_v3

  • 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"
[10624]46#include "core/command/ConsoleCommandIncludes.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
[10624]64        this->setScene(WeakPtr<Scene>(this), this->getObjectID()); // store a weak-pointer to itself (a strong-pointer would create a recursive dependency)
65
[2662]66        this->bShadows_ = true;
[10196]67        this->bDebugDrawPhysics_ = false;
[11054]68        this->debugDrawer_ = nullptr;
[6417]69        this->soundReferenceDistance_ = 20.0;
[10624]70        this->bIsUpdatingPhysics_ = false;
[2072]71
[2896]72        if (GameMode::showsGraphics())
[2072]73        {
[3196]74            assert(Ogre::Root::getSingletonPtr());
75            this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC);
76            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
[5929]77
78            this->radar_ = new Radar();
[2072]79        }
80        else
81        {
82            // create a dummy SceneManager of our own since we don't have Ogre::Root.
83            this->sceneManager_ = new Ogre::DefaultSceneManager("");
84            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
[5929]85
[11054]86            this->radar_ = nullptr;
[2072]87        }
88
[2662]89        // No physics yet, XMLPort will do that.
[10727]90        const float defaultMaxWorldSize = 100000.0f;
[2662]91        this->negativeWorldRange_ = Vector3::UNIT_SCALE * -defaultMaxWorldSize;
92        this->positiveWorldRange_ = Vector3::UNIT_SCALE *  defaultMaxWorldSize;
93        this->gravity_ = Vector3::ZERO;
[11054]94        this->physicalWorld_   = nullptr;
95        this->solver_          = nullptr;
96        this->dispatcher_      = nullptr;
97        this->collisionConfig_ = nullptr;
98        this->broadphase_      = nullptr;
[2072]99
100        this->registerVariables();
101    }
102
103    Scene::~Scene()
104    {
105        if (this->isInitialized())
106        {
[10192]107            this->setPhysicalWorld(false);
108
109            if (this->radar_)
110                this->radar_->destroy();
111
[3196]112            if (GameMode::showsGraphics())
[2072]113                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
[3196]114            else
[2072]115                delete this->sceneManager_;
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);
[3196]124        XMLPortParam(Scene, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2f, 0.2f, 0.2f, 1.0f));
[2072]125        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
[6417]126        XMLPortParam(Scene, "soundReferenceDistance", setSoundReferenceDistance, getSoundReferenceDistance, xmlelement, mode);
[2072]127
[2662]128        XMLPortParam(Scene, "gravity", setGravity, getGravity, xmlelement, mode);
129        XMLPortParam(Scene, "negativeWorldRange", setNegativeWorldRange, getNegativeWorldRange, xmlelement, mode);
130        XMLPortParam(Scene, "positiveWorldRange", setPositiveWorldRange, getPositiveWorldRange, xmlelement, mode);
131        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValues(true);
132
[2072]133        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
134    }
135
136    void Scene::registerVariables()
137    {
[3280]138        registerVariable(this->skybox_,             VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
139        registerVariable(this->ambientLight_,       VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
140        registerVariable(this->negativeWorldRange_, VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_negativeWorldRange));
141        registerVariable(this->positiveWorldRange_, VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_positiveWorldRange));
142        registerVariable(this->gravity_,            VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_gravity));
143        registerVariable(this->bHasPhysics_,        VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_hasPhysics));
144        registerVariable(this->bShadows_,           VariableDirection::ToClient, new NetworkCallback<Scene>(this, &Scene::networkcallback_applyShadows));
[2072]145    }
146
[2662]147    void Scene::setNegativeWorldRange(const Vector3& range)
148    {
149        if (range.length() < 10.0f)
150        {
[8858]151            orxout(internal_warning) << "Setting the negative world range to a very small value: "
152                                     << multi_cast<std::string>(range) << endl;
[2662]153        }
154        if (this->hasPhysics())
155        {
[8858]156            orxout(internal_warning) << "Attempting to set the physical world range at run time. "
157                                     << "This causes a complete physical reload which might take some time." << endl;
[2662]158            this->setPhysicalWorld(false);
159            this->negativeWorldRange_ = range;
160            this->setPhysicalWorld(true);
161        }
162        else
163            this->negativeWorldRange_ = range;
164    }
165
166    void Scene::setPositiveWorldRange(const Vector3& range)
167    {
168        if (range.length() < 10.0f)
169        {
[8858]170            orxout(internal_warning) << "Setting the positive world range to a very small value: "
171                                     << multi_cast<std::string>(range) << endl;
[2662]172        }
173        if (this->hasPhysics())
174        {
[8858]175            orxout(internal_warning) << "Attempting to set the physical world range at run time. "
176                                     << "This causes a complete physical reload which might take some time." << endl;
[2662]177            this->setPhysicalWorld(false);
178            this->positiveWorldRange_ = range;
179            this->setPhysicalWorld(true);
180        }
181        else
182            this->positiveWorldRange_ = range;
183    }
184
185    void Scene::setGravity(const Vector3& gravity)
186    {
187        this->gravity_ = gravity;
188        if (this->hasPhysics())
[3196]189            this->physicalWorld_->setGravity(multi_cast<btVector3>(this->gravity_));
[2662]190    }
191
192    void Scene::setPhysicalWorld(bool wantPhysics)
193    {
194        this->bHasPhysics_ = wantPhysics;
195        if (wantPhysics && !hasPhysics())
196        {
197            // Note: These are all little known default classes and values.
198            //       It would require further investigation to properly dertermine the right choices.
199            this->broadphase_      = new bt32BitAxisSweep3(
[3196]200                multi_cast<btVector3>(this->negativeWorldRange_), multi_cast<btVector3>(this->positiveWorldRange_));
[2662]201            this->collisionConfig_ = new btDefaultCollisionConfiguration();
202            this->dispatcher_      = new btCollisionDispatcher(this->collisionConfig_);
203            this->solver_          = new btSequentialImpulseConstraintSolver();
204
205            this->physicalWorld_   = new btDiscreteDynamicsWorld(this->dispatcher_, this->broadphase_, this->solver_, this->collisionConfig_);
[3196]206            this->physicalWorld_->setGravity(multi_cast<btVector3>(this->gravity_));
[2662]207
[10316]208            if (GameMode::showsGraphics() && this->sceneManager_)
209            {
210                this->debugDrawer_ = new BulletDebugDrawer(this->sceneManager_);
211                this->physicalWorld_->setDebugDrawer(this->debugDrawer_);
212            }
[10196]213
[2662]214            // also set the collision callback variable.
215            // Note: This is a global variable which we assign a static function.
216            // TODO: Check whether this (or anything about Bullet) works with multiple physics engine instances.
217            gContactAddedCallback = &Scene::collisionCallback;
218        }
219        else if (!wantPhysics && hasPhysics())
220        {
221            // Remove all WorldEntities and shove them to the queue since they would still like to be in a physical world.
[11054]222            for (WorldEntity* object : this->physicalObjects_)
[2662]223            {
[11054]224                this->physicalWorld_->removeRigidBody(object->physicalBody_);
225                this->physicalObjectQueue_.insert(object);
[2662]226            }
227            this->physicalObjects_.clear();
228
[10196]229            delete this->debugDrawer_;
[2662]230            delete this->physicalWorld_;
231            delete this->solver_;
232            delete this->dispatcher_;
233            delete this->collisionConfig_;
234            delete this->broadphase_;
[11054]235            this->physicalWorld_   = nullptr;
236            this->solver_          = nullptr;
237            this->dispatcher_      = nullptr;
238            this->collisionConfig_ = nullptr;
239            this->broadphase_      = nullptr;
[2662]240        }
241    }
242
243    void Scene::tick(float dt)
244    {
[2896]245        if (!GameMode::showsGraphics())
[2662]246        {
247            // We need to update the scene nodes if we don't render
248            this->rootSceneNode_->_update(true, false);
249        }
250        if (this->hasPhysics())
251        {
252            // TODO: This here is bad practice! It will slow down the first tick() by ages.
253            //       Rather have an initialise() method as well, called by the Level after everything has been loaded.
254            if (this->physicalObjectQueue_.size() > 0)
255            {
256                // Add all scheduled WorldEntities
[11054]257                for (WorldEntity* object : this->physicalObjectQueue_)
[2662]258                {
[11054]259                    this->physicalWorld_->addRigidBody(object->physicalBody_);
260                    this->physicalObjects_.insert(object);
[2662]261                }
262                this->physicalObjectQueue_.clear();
263            }
264
265            // Note: 60 means that Bullet will do physics correctly down to 1 frames per seconds.
266            //       Under that mark, the simulation will "loose time" and get unusable.
[10624]267            this->bIsUpdatingPhysics_ = true;
268            this->physicalWorld_->stepSimulation(dt, 60);
269            this->bIsUpdatingPhysics_ = false;
[10196]270
271            if (this->bDebugDrawPhysics_)
[10624]272                this->physicalWorld_->debugDrawWorld();
[2662]273        }
274    }
275
[2072]276    void Scene::setSkybox(const std::string& skybox)
277    {
[9348]278        try
279        {
280            if (GameMode::showsGraphics() && this->sceneManager_)
281                this->sceneManager_->setSkyBox(true, skybox);
282        }
283        catch (const Ogre::Exception&)
284        {
285            orxout(internal_error) << "Could not load skybox '" << skybox << "':" << endl;
286            orxout(internal_error) << Exception::handleMessage() << endl;
287        }
[2072]288
289        this->skybox_ = skybox;
290    }
291
292    void Scene::setAmbientLight(const ColourValue& colour)
293    {
[2896]294        if (GameMode::showsGraphics() && this->sceneManager_)
[2072]295            this->sceneManager_->setAmbientLight(colour);
296
297        this->ambientLight_ = colour;
298    }
299
300    void Scene::setShadow(bool bShadow)
301    {
[2896]302        if (GameMode::showsGraphics() && this->sceneManager_)
[2072]303        {
304            if (bShadow)
305                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
306            else
307                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
308        }
309
310        this->bShadows_ = bShadow;
311    }
312
313    void Scene::addObject(BaseObject* object)
314    {
315        this->objects_.push_back(object);
316    }
317
318    BaseObject* Scene::getObject(unsigned int index) const
319    {
320        unsigned int i = 0;
[11054]321        for (BaseObject* object : this->objects_)
[2072]322        {
323            if (i == index)
[11054]324                return object;
[2072]325            ++i;
326        }
[11054]327        return nullptr;
[2072]328    }
[2171]329
[2662]330    void Scene::addPhysicalObject(WorldEntity* object)
[2171]331    {
[2662]332        if (object)
[2171]333        {
[2662]334            std::set<WorldEntity*>::iterator it = this->physicalObjects_.find(object);
335            if (it != this->physicalObjects_.end())
336                return;
337
338            this->physicalObjectQueue_.insert(object);
[2171]339        }
340    }
[2662]341
342    void Scene::removePhysicalObject(WorldEntity* object)
343    {
344        // check queue first
345        std::set<WorldEntity*>::iterator it = this->physicalObjectQueue_.find(object);
346        if (it != this->physicalObjectQueue_.end())
347        {
348            this->physicalObjectQueue_.erase(it);
349            return;
350        }
351
352        it = this->physicalObjects_.find(object);
353        if (it == this->physicalObjects_.end())
354            return;
355        this->physicalObjects_.erase(it);
356
357        if (this->hasPhysics())
358            this->physicalWorld_->removeRigidBody(object->physicalBody_);
359    }
360
361    /*static*/ bool Scene::collisionCallback(btManifoldPoint& cp, const btCollisionObject* colObj0, int partId0,
362                                             int index0, const btCollisionObject* colObj1, int partId1, int index1)
363    {
364        // get the WorldEntity pointers
[10624]365        StrongPtr<WorldEntity> object0 = static_cast<WorldEntity*>(colObj0->getUserPointer());
366        StrongPtr<WorldEntity> object1 = static_cast<WorldEntity*>(colObj1->getUserPointer());
[2662]367
[10216]368        // get the CollisionShape pointers
369        const btCollisionShape* cs0 = colObj0->getCollisionShape();
370        const btCollisionShape* cs1 = colObj1->getCollisionShape();
371
[2662]372        // false means that bullet will assume we didn't modify the contact
373        bool modified = false;
374        if (object0->isCollisionCallbackActive())
[10216]375            modified |= object0->collidesAgainst(object1, cs1, cp);
[6064]376        if (object1->isCollisionCallbackActive())
[10216]377            modified |= object1->collidesAgainst(object0, cs0, cp);
[2662]378
379        return modified;
380    }
[10196]381
382    void Scene::setDebugDrawPhysics(bool bDraw, bool bFill, float fillAlpha)
383    {
384        this->bDebugDrawPhysics_ = bDraw;
385        if (this->debugDrawer_)
386            this->debugDrawer_->configure(bFill, fillAlpha);
387    }
388
389    /*static*/ void Scene::consoleCommand_debugDrawPhysics(bool bDraw, bool bFill, float fillAlpha)
390    {
[11054]391        for (Scene* scene : ObjectList<Scene>())
392            scene->setDebugDrawPhysics(bDraw, bFill, fillAlpha);
[10196]393    }
[2072]394}
Note: See TracBrowser for help on using the repository browser.