Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/Scene.cc @ 2298

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

Clarified use of different physical bodies. The "collisionType" XML attribute of WE specifies the following:
"none": There is not physical body at all. Physics disabled.
"static": It is a StaticEntity with physics. Any other derived class of WE issues an exception by choosing this collision type.
"kinematic" or "dynamic": MovableEntity with physics. StaticEntity issues an exception when choosing one of these two.

Added two new Exceptions: ParseError and PhysicsViolation.

  • Property svn:eol-style set to native
File size: 7.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 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "Scene.h"
31
32#include <OgreRoot.h>
33#include <OgreSceneManagerEnumerator.h>
34#include <OgreSceneNode.h>
35#include <OgreLight.h>
36
37#include "core/CoreIncludes.h"
38#include "core/Core.h"
39#include "core/XMLPort.h"
40
41#include "BulletCollision/BroadphaseCollision/btAxisSweep3.h"
42#include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
43#include "BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h"
44
45namespace orxonox
46{
47    CreateFactory(Scene);
48
49    Scene::Scene(BaseObject* creator) : BaseObject(creator), network::Synchronisable(creator)
50    {
51        RegisterObject(Scene);
52
53        this->setScene(this);
54        this->bShadows_ = false;
55
56        if (Core::showsGraphics())
57        {
58            if (Ogre::Root::getSingletonPtr())
59            {
60                this->sceneManager_ = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC);
61                this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
62            }
63            else
64            {
65                this->sceneManager_ = 0;
66                this->rootSceneNode_ = 0;
67            }
68        }
69        else
70        {
71            // create a dummy SceneManager of our own since we don't have Ogre::Root.
72            this->sceneManager_ = new Ogre::DefaultSceneManager("");
73            this->rootSceneNode_ = this->sceneManager_->getRootSceneNode();
74        }
75
76        // No physics for default
77        this->physicalWorld_ = 0;
78
79        // test test test
80        if (Core::showsGraphics() && this->sceneManager_)
81        {
82            Ogre::Light* light;
83            light = this->sceneManager_->createLight("Light-1");
84            light->setType(Ogre::Light::LT_DIRECTIONAL);
85            light->setDiffuseColour(ColourValue(1.0, 0.9, 0.6, 1.0));
86            light->setSpecularColour(ColourValue(1.0, 0.9, 0.6, 1.0));
87            light->setDirection(1, -0.3, 0.3);
88        }
89        // test test test
90
91        this->registerVariables();
92    }
93
94    Scene::~Scene()
95    {
96        if (this->isInitialized())
97        {
98            if (Ogre::Root::getSingletonPtr())
99            {
100                Ogre::Root::getSingleton().destroySceneManager(this->sceneManager_);
101            }
102            else if (!Core::showsGraphics())
103            {
104                delete this->sceneManager_;
105            }
106        }
107    }
108
109    void Scene::XMLPort(Element& xmlelement, XMLPort::Mode mode)
110    {
111        SUPER(Scene, XMLPort, xmlelement, mode);
112
113        XMLPortParam(Scene, "skybox", setSkybox, getSkybox, xmlelement, mode);
114        XMLPortParam(Scene, "ambientlight", setAmbientLight, getAmbientLight, xmlelement, mode).defaultValues(ColourValue(0.2, 0.2, 0.2, 1));
115        XMLPortParam(Scene, "shadow", setShadow, getShadow, xmlelement, mode).defaultValues(true);
116
117        const int defaultMaxWorldSize = 100000;
118        Vector3 worldAabbMin(-defaultMaxWorldSize, -defaultMaxWorldSize, -defaultMaxWorldSize);
119        Vector3 worldAabbMax( defaultMaxWorldSize,  defaultMaxWorldSize,  defaultMaxWorldSize);
120        XMLPortParamVariable(Scene, "negativeWorldRange", worldAabbMin, xmlelement, mode);
121        XMLPortParamVariable(Scene, "positiveWorldRange", worldAabbMax, xmlelement, mode);
122        XMLPortParam(Scene, "hasPhysics", setPhysicalWorld, hasPhysics, xmlelement, mode).defaultValue(0, true).defaultValue(1, worldAabbMin).defaultValue(2, worldAabbMax);
123
124        XMLPortObjectExtended(Scene, BaseObject, "", addObject, getObject, xmlelement, mode, true, false);
125    }
126
127    void Scene::registerVariables()
128    {
129        REGISTERSTRING(this->skybox_,     network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applySkybox));
130        REGISTERDATA(this->ambientLight_, network::direction::toclient, new network::NetworkCallback<Scene>(this, &Scene::networkcallback_applyAmbientLight));
131    }
132
133    void Scene::setPhysicalWorld(bool wantPhysics, const Vector3& worldAabbMin, const Vector3& worldAabbMax)
134    {
135        if (wantPhysics && !hasPhysics())
136        {
137            btVector3 worldAabbMin(worldAabbMin.x, worldAabbMin.y, worldAabbMin.z);
138            btVector3 worldAabbMax(worldAabbMax.x, worldAabbMax.y, worldAabbMax.z);
139
140            btDefaultCollisionConfiguration*     collisionConfig = new btDefaultCollisionConfiguration();
141            btCollisionDispatcher*               dispatcher      = new btCollisionDispatcher(collisionConfig);
142            bt32BitAxisSweep3*                   broadphase      = new bt32BitAxisSweep3(worldAabbMin,worldAabbMax);
143            btSequentialImpulseConstraintSolver* solver          = new btSequentialImpulseConstraintSolver;
144
145            this->physicalWorld_ =  new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfig);
146
147            // Disable Gravity for space
148            this->physicalWorld_->setGravity(btVector3(0,0,0));
149        }
150        else
151        {
152            // TODO: Destroy Bullet physics
153        }
154    }
155
156    void Scene::tick(float dt)
157    {
158        if (physicalWorld_)
159            physicalWorld_->stepSimulation(dt,10);
160    }
161
162    void Scene::setSkybox(const std::string& skybox)
163    {
164        if (Core::showsGraphics() && this->sceneManager_)
165            this->sceneManager_->setSkyBox(true, skybox);
166
167        this->skybox_ = skybox;
168    }
169
170    void Scene::setAmbientLight(const ColourValue& colour)
171    {
172        if (Core::showsGraphics() && this->sceneManager_)
173            this->sceneManager_->setAmbientLight(colour);
174
175        this->ambientLight_ = colour;
176    }
177
178    void Scene::setShadow(bool bShadow)
179    {
180        if (Core::showsGraphics() && this->sceneManager_)
181        {
182            if (bShadow)
183                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
184            else
185                this->sceneManager_->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
186        }
187
188        this->bShadows_ = bShadow;
189    }
190
191    void Scene::addObject(BaseObject* object)
192    {
193        this->objects_.push_back(object);
194        object->setScene(this);
195    }
196
197    BaseObject* Scene::getObject(unsigned int index) const
198    {
199        unsigned int i = 0;
200        for (std::list<BaseObject*>::const_iterator it = this->objects_.begin(); it != this->objects_.end(); ++it)
201        {
202            if (i == index)
203                return (*it);
204            ++i;
205        }
206        return 0;
207    }
208}
Note: See TracBrowser for help on using the repository browser.