Browse Source

SceneGraph: removed fugly hack for detecting scene.

Previously the scene was its own parent, which was good sometimes back
then, but it became so ugly and complicated hack ("my friend has access
to my privates"-style) that it needed to be replaced with something less
ugly.

Now there is an function isScene() which is overloaded in Scene. Yes,
it's possible to overload it anywhere else, but you better shouldn't
even think about it.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
71f7e241fa
  1. 15
      src/SceneGraph/Object.cpp
  2. 8
      src/SceneGraph/Object.h
  3. 3
      src/SceneGraph/Scene.h
  4. 5
      src/SceneGraph/Test/SceneTest.cpp

15
src/SceneGraph/Object.cpp

@ -27,14 +27,14 @@ namespace Magnum { namespace SceneGraph {
template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> ObjectType* Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::setParent(ObjectType* parent) {
/* Skip if nothing to do or this is scene */
if(_parent == parent || _parent == this) return static_cast<ObjectType*>(this);
if(_parent == parent || isScene()) return static_cast<ObjectType*>(this);
/* Add the object to children list of new parent */
if(parent != nullptr) {
/* Only Fry can be his own grandfather */
ObjectType* p = parent;
while(p != nullptr && p->parent() != p) {
while(p != nullptr) {
if(p == this) return static_cast<ObjectType*>(this);
p = p->parent();
}
@ -64,7 +64,7 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
t = p->transformation()*t;
/* We got to the scene, multiply with camera matrix */
if(p->parent() == p) {
if(p->isScene()) {
if(camera) {
CORRADE_ASSERT(camera->scene() == scene(), "Object::absoluteTransformation(): the camera is not part of the same scene as object!", t);
t = camera->cameraMatrix()*t;
@ -95,7 +95,7 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
(that's the scene) */
ObjectType* p = parent();
while(p != nullptr) {
if(p->parent() == p) return static_cast<SceneType*>(p);
if(p->isScene()) return static_cast<SceneType*>(p);
p = p->parent();
}
@ -103,7 +103,8 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
}
template<class MatrixType, class VectorType, class ObjectType, class SceneType, class CameraType> ObjectType* Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>::setTransformation(const MatrixType& transformation) {
if(_parent == this) return static_cast<ObjectType*>(this);
/* Setting transformation is forbidden for the scene */
if(isScene()) return static_cast<ObjectType*>(this);
_transformation = transformation;
setDirty();
@ -131,8 +132,8 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
for(;;) {
objects.push(p);
/* Stop on root object / scene / clean object */
if(p->parent() == nullptr || p->parent() == p || !p->parent()->isDirty())
/* Stop on root object / clean object */
if(p->parent() == nullptr || !p->parent()->isDirty())
break;
p = p->parent();

8
src/SceneGraph/Object.h

@ -27,8 +27,6 @@
namespace Magnum { namespace SceneGraph {
template<class MatrixType, class VectorType, class ObjectType, class CameraType> class Scene;
/**
@todo User-specified Object implementation:
- for front-to-back sorting, LoD changes etc.
@ -53,8 +51,6 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>& operator=(Object<MatrixType, VectorType, ObjectType, SceneType, CameraType>&& other) = delete;
#endif
friend class Scene<MatrixType, VectorType, ObjectType, CameraType>;
public:
/**
* @brief Constructor
@ -76,6 +72,9 @@ template<class MatrixType, class VectorType, class ObjectType, class SceneType,
/** @{ @name Scene hierarchy */
/** @brief Whether this object is scene */
virtual inline bool isScene() const { return false; }
/**
* @brief %Scene
* @return If the object is not assigned to any scene, returns nullptr.
@ -245,6 +244,7 @@ class Camera2D;
class Camera3D;
class Object2D;
class Object3D;
template<class MatrixType, class VectorType, class ObjectType, class CameraType> class Scene;
typedef Scene<Matrix3, Vector2, Object2D, Camera2D> Scene2D;
typedef Scene<Matrix4, Vector3, Object3D, Camera3D> Scene3D;

3
src/SceneGraph/Scene.h

@ -26,8 +26,7 @@ namespace Magnum { namespace SceneGraph {
/** @brief %Scene */
template<class MatrixType, class VectorType, class ObjectType, class CameraType> class SCENEGRAPH_EXPORT Scene: public ObjectType {
public:
/** @brief Constructor */
inline Scene() { this->_parent = this; }
inline bool isScene() const { return true; }
#ifndef DOXYGEN_GENERATING_OUTPUT
void setParent(ObjectType* parent) = delete;

5
src/SceneGraph/Test/SceneTest.cpp

@ -36,14 +36,13 @@ void SceneTest::transformation() {
void SceneTest::parent() {
Scene3D scene;
CORRADE_VERIFY(scene.parent() == &scene);
CORRADE_VERIFY(scene.isScene());
/* Scene parent cannot be changed */
Object3D* scenePointer = &scene;
Object3D object;
scenePointer->setParent(&object);
CORRADE_VERIFY(scene.parent() == &scene);
CORRADE_VERIFY(scene.parent() == nullptr);
CORRADE_VERIFY(scene.children().empty());
CORRADE_VERIFY(object.children().empty());
}

Loading…
Cancel
Save