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