mirror of https://github.com/mosra/magnum.git
Browse Source
Physics library now depends on SceneGraph, which must be explicitly linked too.vectorfields
10 changed files with 414 additions and 3 deletions
@ -0,0 +1,54 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "ShapedObject.h" |
||||||
|
|
||||||
|
#include <algorithm> |
||||||
|
|
||||||
|
#include "AbstractShaderProgram.h" |
||||||
|
#include "Mesh.h" |
||||||
|
#include "ResourceManager.h" |
||||||
|
#include "AbstractShape.h" |
||||||
|
#include "ShapedObjectGroup.h" |
||||||
|
|
||||||
|
using namespace std; |
||||||
|
|
||||||
|
namespace Magnum { namespace Physics { |
||||||
|
|
||||||
|
template<size_t dimensions> ShapedObject<dimensions>::ShapedObject(ShapedObjectGroup<dimensions>* group, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent): SceneGraph::AbstractObject<dimensions>::ObjectType(parent), group(group), _shape(nullptr) { |
||||||
|
group->objects.push_back(this); |
||||||
|
} |
||||||
|
|
||||||
|
template<size_t dimensions> ShapedObject<dimensions>::~ShapedObject() { |
||||||
|
group->objects.erase(find(group->objects.begin(), group->objects.end(), this)); |
||||||
|
delete _shape; |
||||||
|
} |
||||||
|
|
||||||
|
template<size_t dimensions> void ShapedObject<dimensions>::setDirty() { |
||||||
|
SceneGraph::AbstractObject<dimensions>::ObjectType::setDirty(); |
||||||
|
|
||||||
|
group->setDirty(); |
||||||
|
} |
||||||
|
|
||||||
|
template<size_t dimensions> void ShapedObject<dimensions>::clean(const typename SceneGraph::AbstractObject<dimensions>::MatrixType& absoluteTransformation) { |
||||||
|
SceneGraph::AbstractObject<dimensions>::ObjectType::clean(absoluteTransformation); |
||||||
|
|
||||||
|
if(_shape) _shape->applyTransformation(absoluteTransformation); |
||||||
|
} |
||||||
|
|
||||||
|
template class ShapedObject<2>; |
||||||
|
template class ShapedObject<3>; |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,95 @@ |
|||||||
|
#ifndef Magnum_Physics_ShapedObject_h |
||||||
|
#define Magnum_Physics_ShapedObject_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Physics::ShapedObject |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "SceneGraph/Object.h" |
||||||
|
|
||||||
|
#include "magnumPhysicsVisibility.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Physics { |
||||||
|
|
||||||
|
template<size_t> class ShapedObjectGroup; |
||||||
|
template<size_t> class AbstractShape; |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Object with assigned shape |
||||||
|
|
||||||
|
@see ShapedObject2D, ShapedObject3D |
||||||
|
*/ |
||||||
|
template<size_t dimensions> class PHYSICS_EXPORT ShapedObject: public SceneGraph::AbstractObject<dimensions>::ObjectType { |
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @brief Constructor |
||||||
|
* @param group Group this shaped object belongs to |
||||||
|
* @param parent Parent object |
||||||
|
* |
||||||
|
* Creates object with no shape. |
||||||
|
* @see setShape() |
||||||
|
*/ |
||||||
|
ShapedObject(ShapedObjectGroup<dimensions>* group, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent = nullptr); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destructor |
||||||
|
* |
||||||
|
* Deletes associated shape. |
||||||
|
*/ |
||||||
|
~ShapedObject(); |
||||||
|
|
||||||
|
/** @brief Object shape */ |
||||||
|
inline AbstractShape<dimensions>* shape() { return _shape; } |
||||||
|
inline const AbstractShape<dimensions>* shape() const { return _shape; } /**< @overload */ |
||||||
|
|
||||||
|
/** @brief Set object shape */ |
||||||
|
void setShape(AbstractShape<dimensions>* shape) { _shape = shape; } |
||||||
|
|
||||||
|
/**
|
||||||
|
* @copybrief SceneGraph::AbstractObject::setDirty() |
||||||
|
* |
||||||
|
* Marks shaped object group as dirty. |
||||||
|
*/ |
||||||
|
void setDirty(); |
||||||
|
|
||||||
|
protected: |
||||||
|
/**
|
||||||
|
* @copybrief SceneGraph::AbstractObject::clean() |
||||||
|
* |
||||||
|
* Applies transformation to associated shape. |
||||||
|
*/ |
||||||
|
void clean(const typename SceneGraph::AbstractObject<dimensions>::MatrixType& absoluteTransformation); |
||||||
|
|
||||||
|
private: |
||||||
|
ShapedObjectGroup<dimensions>* group; |
||||||
|
AbstractShape<dimensions>* _shape; |
||||||
|
}; |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
extern template class PHYSICS_EXPORT ShapedObject<2>; |
||||||
|
extern template class PHYSICS_EXPORT ShapedObject<3>; |
||||||
|
#endif |
||||||
|
|
||||||
|
/** @brief Two-dimensional shaped object */ |
||||||
|
typedef ShapedObject<2> ShapedObject2D; |
||||||
|
|
||||||
|
/** @brief Three-dimensional shaped object */ |
||||||
|
typedef ShapedObject<3> ShapedObject3D; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,32 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "ShapedObjectGroup.h" |
||||||
|
|
||||||
|
#include "ShapedObject.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Physics { |
||||||
|
|
||||||
|
template<size_t dimensions> void ShapedObjectGroup<dimensions>::setClean() { |
||||||
|
for(ShapedObject<dimensions>* object: objects) |
||||||
|
if(object->isDirty()) object->setClean(); |
||||||
|
|
||||||
|
dirty = false; |
||||||
|
} |
||||||
|
|
||||||
|
template class ShapedObjectGroup<2>; |
||||||
|
template class ShapedObjectGroup<3>; |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,134 @@ |
|||||||
|
#ifndef Magnum_Physics_ShapedObjectGroup_h |
||||||
|
#define Magnum_Physics_ShapedObjectGroup_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Physics::ShapedObjectGroup |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <cstddef> |
||||||
|
#include <vector> |
||||||
|
|
||||||
|
#include "magnumPhysicsVisibility.h" |
||||||
|
|
||||||
|
namespace Magnum { |
||||||
|
|
||||||
|
class AbstractShaderProgram; |
||||||
|
template<class T, class U> class Resource; |
||||||
|
|
||||||
|
namespace Physics { |
||||||
|
|
||||||
|
template<size_t> class ShapedObject; |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
namespace Implementation { |
||||||
|
enum class DebugMode { |
||||||
|
None, |
||||||
|
Wireframe, |
||||||
|
SolidWireframe |
||||||
|
}; |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Group of shaped objects |
||||||
|
|
||||||
|
@ref ShapedObject "ShapedObject*D" instances are added to the group by |
||||||
|
specifying it in the constructor. When the group is deleted, all objects |
||||||
|
belogning to it are deleted too. |
||||||
|
@see ShapedObjectGroup2D, ShapedObjectGroup3D |
||||||
|
*/ |
||||||
|
template<size_t dimensions> class PHYSICS_EXPORT ShapedObjectGroup { |
||||||
|
friend class ShapedObject<dimensions>; |
||||||
|
|
||||||
|
public: |
||||||
|
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||||
|
/**
|
||||||
|
* @brief Debug mode |
||||||
|
* |
||||||
|
* @see setDebugMode() |
||||||
|
*/ |
||||||
|
enum class DebugMode { |
||||||
|
None, /**< @brief Nothing is rendered */ |
||||||
|
Wireframe, /**< @brief Wireframe of the shape is rendered */ |
||||||
|
SolidWireframe /**< @brief Solid with wireframe is rendered */ |
||||||
|
}; |
||||||
|
#else |
||||||
|
typedef Implementation::DebugMode DebugMode; |
||||||
|
#endif |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Destructor |
||||||
|
* |
||||||
|
* Deletes all objects belogning to the group. |
||||||
|
*/ |
||||||
|
inline virtual ~ShapedObjectGroup() { |
||||||
|
for(auto i: objects) delete i; |
||||||
|
} |
||||||
|
|
||||||
|
/** @brief Debug mode */ |
||||||
|
inline DebugMode debugMode() const { return _debugMode; } |
||||||
|
|
||||||
|
/** @brief Set debug mode */ |
||||||
|
inline void setDebugMode(DebugMode mode) { _debugMode = mode; } |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Whether the group is dirty |
||||||
|
* @return True if any object in the group is dirty, false otherwise. |
||||||
|
*/ |
||||||
|
inline bool isDirty() const { return dirty; } |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the group as dirty |
||||||
|
* |
||||||
|
* If some body in the group changes its transformation, it sets dirty |
||||||
|
* status also on the group to indicate that the body and maybe also |
||||||
|
* group state needs to be cleaned before computing collisions. |
||||||
|
* |
||||||
|
* @see setClean() |
||||||
|
*/ |
||||||
|
inline void setDirty() { dirty = true; } |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set the group and all bodies as clean |
||||||
|
* |
||||||
|
* This function is called before computing any collisions to ensure |
||||||
|
* all objects are cleaned. |
||||||
|
*/ |
||||||
|
void setClean(); |
||||||
|
|
||||||
|
private: |
||||||
|
Resource<AbstractShaderProgram, AbstractShaderProgram>& shader(); |
||||||
|
|
||||||
|
DebugMode _debugMode; |
||||||
|
std::vector<ShapedObject<dimensions>*> objects; |
||||||
|
bool dirty; |
||||||
|
}; |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
extern template class PHYSICS_EXPORT ShapedObjectGroup<2>; |
||||||
|
extern template class PHYSICS_EXPORT ShapedObjectGroup<3>; |
||||||
|
#endif |
||||||
|
|
||||||
|
/** @brief Group of two-dimensional shaped objects */ |
||||||
|
typedef ShapedObjectGroup<2> ShapedObjectGroup2D; |
||||||
|
|
||||||
|
/** @brief Group of three-dimensional shaped objects */ |
||||||
|
typedef ShapedObjectGroup<3> ShapedObjectGroup3D; |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
@ -0,0 +1,59 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "ShapedObjectTest.h" |
||||||
|
|
||||||
|
#include "Physics/ShapedObjectGroup.h" |
||||||
|
#include "Physics/ShapedObject.h" |
||||||
|
|
||||||
|
CORRADE_TEST_MAIN(Magnum::Physics::Test::ShapedObjectTest) |
||||||
|
|
||||||
|
namespace Magnum { namespace Physics { namespace Test { |
||||||
|
|
||||||
|
ShapedObjectTest::ShapedObjectTest() { |
||||||
|
addTests(&ShapedObjectTest::clean); |
||||||
|
} |
||||||
|
|
||||||
|
void ShapedObjectTest::clean() { |
||||||
|
ShapedObjectGroup3D group; |
||||||
|
|
||||||
|
ShapedObject3D a(&group), b(&group); |
||||||
|
|
||||||
|
/* Everything is dirty at the beginning */ |
||||||
|
CORRADE_VERIFY(group.isDirty()); |
||||||
|
CORRADE_VERIFY(a.isDirty()); |
||||||
|
CORRADE_VERIFY(b.isDirty()); |
||||||
|
|
||||||
|
/* Cleaning object will not clean anything other */ |
||||||
|
a.setClean(); |
||||||
|
CORRADE_VERIFY(group.isDirty()); |
||||||
|
CORRADE_VERIFY(!a.isDirty()); |
||||||
|
CORRADE_VERIFY(b.isDirty()); |
||||||
|
|
||||||
|
/* Setting group clean will clean whole group */ |
||||||
|
a.setDirty(); |
||||||
|
group.setClean(); |
||||||
|
CORRADE_VERIFY(!group.isDirty()); |
||||||
|
CORRADE_VERIFY(!a.isDirty()); |
||||||
|
CORRADE_VERIFY(!b.isDirty()); |
||||||
|
|
||||||
|
/* Setting object dirty will set also the group, but not other objects */ |
||||||
|
b.setDirty(); |
||||||
|
CORRADE_VERIFY(group.isDirty()); |
||||||
|
CORRADE_VERIFY(!a.isDirty()); |
||||||
|
CORRADE_VERIFY(b.isDirty()); |
||||||
|
} |
||||||
|
|
||||||
|
}}} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
#ifndef Magnum_Physics_Test_ShapedObjectTest_h |
||||||
|
#define Magnum_Physics_Test_ShapedObjectTest_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <TestSuite/Tester.h> |
||||||
|
|
||||||
|
namespace Magnum { namespace Physics { namespace Test { |
||||||
|
|
||||||
|
class ShapedObjectTest: public Corrade::TestSuite::Tester<ShapedObjectTest> { |
||||||
|
public: |
||||||
|
ShapedObjectTest(); |
||||||
|
|
||||||
|
void clean(); |
||||||
|
}; |
||||||
|
|
||||||
|
}}} |
||||||
|
|
||||||
|
#endif |
||||||
Loading…
Reference in new issue