Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/shaders/src/orxonox/Scene.cc @ 9407

Last change on this file since 9407 was 9407, checked in by davidsa, 12 years ago

orxonox::RenderQueueListener: Implemented a rudimentary RenderQueueListener to enable the use of stencil buffer for elaborate alpha blending shaders without creating artifacts from overlapping faces. Also added a XML Port to assign a Model to a certain RenderQueueGroup. Needs to be improved to allow the use of strings for choosing the group instead of a static int which may change in the feature.

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