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