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