mirror of https://github.com/mosra/magnum.git
10 changed files with 385 additions and 3 deletions
@ -0,0 +1,56 @@
|
||||
/*
|
||||
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 "DebugDrawResourceManager.h" |
||||
|
||||
#include "AbstractShaderProgram.h" |
||||
#include "Mesh.h" |
||||
#include "AbstractShape.h" |
||||
#include "Box.h" |
||||
#include "ShapedObject.h" |
||||
#include "ShapeGroup.h" |
||||
#include "Implementation/AbstractDebugRenderer.h" |
||||
#include "Implementation/ShapeShader.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
template class ResourceManager<AbstractShaderProgram, Mesh, Physics::Implementation::Options>; |
||||
|
||||
namespace Physics { |
||||
|
||||
SceneGraph::Object2D* DebugDrawResourceManager::createDebugRenderer(AbstractShape2D* shape, ResourceKey options) { |
||||
return createDebugMesh(nullptr, shape, options); |
||||
} |
||||
|
||||
SceneGraph::Object2D* DebugDrawResourceManager::createDebugMesh(SceneGraph::Object2D* parent, AbstractShape2D* shape, ResourceKey options) { |
||||
switch(shape->type()) { |
||||
case AbstractShape2D::Type::ShapeGroup: { |
||||
if(!parent) parent = new SceneGraph::Object2D; |
||||
ShapeGroup2D* group = static_cast<ShapeGroup2D*>(shape); |
||||
if(group->first()) createDebugMesh(parent, group->first(), options); |
||||
if(group->second()) createDebugMesh(parent, group->second(), options); |
||||
return parent; |
||||
} default: return nullptr; |
||||
} |
||||
} |
||||
|
||||
DebugDrawResourceManager::DebugDrawResourceManager() { |
||||
setFallback<Options>(new Options); |
||||
set<AbstractShaderProgram>("shader2d", new Implementation::ShapeShader<2>, ResourceDataState::Final, ResourcePolicy::Resident); |
||||
} |
||||
|
||||
DebugDrawResourceManager::~DebugDrawResourceManager() {} |
||||
|
||||
}} |
||||
@ -0,0 +1,119 @@
|
||||
#ifndef Magnum_Physics_DebugDrawResourceManager_h |
||||
#define Magnum_Physics_DebugDrawResourceManager_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::DebugDrawResourceManager |
||||
*/ |
||||
|
||||
#include "Magnum.h" |
||||
#include "Color.h" |
||||
#include "ResourceManager.h" |
||||
|
||||
#include "magnumPhysicsVisibility.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
class AbstractShaderProgram; |
||||
class Mesh; |
||||
|
||||
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||
namespace Physics { namespace Implementation { |
||||
struct Options { |
||||
Color3<GLfloat> color; |
||||
}; |
||||
template<std::uint8_t> class AbstractDebugRenderer; |
||||
}} |
||||
|
||||
extern template class PHYSICS_EXPORT ResourceManager<AbstractShaderProgram, Mesh, Physics::Implementation::Options>; |
||||
#endif |
||||
|
||||
namespace SceneGraph { |
||||
class Object2D; |
||||
class Object3D; |
||||
} |
||||
|
||||
namespace Physics { |
||||
|
||||
template<std::uint8_t> class AbstractShape; |
||||
typedef AbstractShape<2> AbstractShape2D; |
||||
typedef AbstractShape<3> AbstractShape3D; |
||||
|
||||
/**
|
||||
@brief %Resource manager for physics debug draw |
||||
|
||||
Can create objects which draw object collision shape for debugging purposes. |
||||
|
||||
@section DebugDrawResourceManager-usage Basic usage |
||||
The manager must be instanced for the whole lifetime of debug draw objects. |
||||
To create debug draw objects, call createDebugRenderer() and add the resulting |
||||
object to the scene. You can specify options via Options struct - add it to |
||||
the manager and then create debug renderer with the same options key. This way |
||||
you can easily share the same options with more objects. If no options for |
||||
given key exist, default is used. |
||||
|
||||
Example code: |
||||
@code |
||||
// Instance the manager at first
|
||||
DebugDrawResourceManager manager; |
||||
|
||||
// Create some options
|
||||
auto o = new DebugDrawResourceManager::Options { |
||||
{1.0f, 0.0f, 0.0f} // Red color
|
||||
}; |
||||
manager->set<DebugDrawResourceManager::Options>("red", o, ResourceDataState::Final, ResourcePolicy::Persistent); |
||||
|
||||
// Add debug draw object for given shape, use "red" options for it, don't
|
||||
// forget to add it to the scene
|
||||
ShapedObject2D* object; |
||||
DebugDrawResourceManager::createDebugRenderer(object->shape(), "red") |
||||
->setParent(object); |
||||
@endcode |
||||
*/ |
||||
class PHYSICS_EXPORT DebugDrawResourceManager: public ResourceManager<AbstractShaderProgram, Mesh, Physics::Implementation::Options> { |
||||
public: |
||||
#ifdef DOXYGEN_GENERATING_OUTPUT |
||||
/** @brief %Options */ |
||||
struct Options { |
||||
Color3<GLfloat> color; /**< @brief Color */ |
||||
}; |
||||
#else |
||||
typedef Implementation::Options Options; |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief Create debug renderer for given shape |
||||
* @param shape Shape for which to create debug renderer |
||||
* @param options Options resource key. See |
||||
* @ref DebugDrawResourceManager-usage "class documentation" for |
||||
* more information. |
||||
* |
||||
* Returned object is not parented to anything, you have to add it to |
||||
* desired scene yourself. |
||||
*/ |
||||
static SceneGraph::Object2D* createDebugRenderer(AbstractShape2D* shape, ResourceKey options = ResourceKey()); |
||||
|
||||
DebugDrawResourceManager(); |
||||
|
||||
~DebugDrawResourceManager(); |
||||
|
||||
private: |
||||
static SceneGraph::Object2D* createDebugMesh(SceneGraph::Object2D* parent, AbstractShape2D* shape, ResourceKey options); |
||||
}; |
||||
|
||||
}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,30 @@
|
||||
/*
|
||||
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 "AbstractDebugRenderer.h" |
||||
|
||||
#include "AbstractShaderProgram.h" |
||||
#include "Mesh.h" |
||||
#include "Physics/DebugDrawResourceManager.h" |
||||
#include "ShapeShader.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> AbstractDebugRenderer<dimensions>::AbstractDebugRenderer(ResourceKey shader, ResourceKey mesh, ResourceKey options, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent): SceneGraph::AbstractObject<dimensions>::ObjectType(parent), shader(DebugDrawResourceManager::instance()->get<AbstractShaderProgram, ShapeShader<dimensions>>(shader)), mesh(DebugDrawResourceManager::instance()->get<Mesh>(mesh)), options(DebugDrawResourceManager::instance()->get<Options>(options)) {} |
||||
|
||||
template class AbstractDebugRenderer<2>; |
||||
template class AbstractDebugRenderer<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,48 @@
|
||||
#ifndef Magnum_Physics_Implementation_AbstractDebugRenderer_h |
||||
#define Magnum_Physics_Implementation_AbstractDebugRenderer_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 "Color.h" |
||||
#include "ResourceManager.h" |
||||
#include "SceneGraph/Camera.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
class AbstractShaderProgram; |
||||
class Mesh; |
||||
|
||||
namespace Physics { namespace Implementation { |
||||
|
||||
struct Options; |
||||
|
||||
template<std::uint8_t> class ShapeShader; |
||||
|
||||
template<std::uint8_t dimensions> class AbstractDebugRenderer: public SceneGraph::AbstractObject<dimensions>::ObjectType { |
||||
public: |
||||
AbstractDebugRenderer(ResourceKey shader, ResourceKey mesh, ResourceKey options, typename SceneGraph::AbstractObject<dimensions>::ObjectType* parent); |
||||
|
||||
protected: |
||||
Resource<AbstractShaderProgram, ShapeShader<dimensions>> shader; |
||||
Resource<Mesh> mesh; |
||||
Resource<Options> options; |
||||
}; |
||||
|
||||
extern template class AbstractDebugRenderer<2>; |
||||
extern template class AbstractDebugRenderer<3>; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,52 @@
|
||||
/*
|
||||
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 "ShapeShader.h" |
||||
|
||||
#include <Utility/Resource.h> |
||||
|
||||
#include "Shader.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
namespace { |
||||
template<std::uint8_t dimensions> struct ShaderName {}; |
||||
|
||||
template<> struct ShaderName<2> { |
||||
constexpr static const char* Vertex = "ShapeShader2D.vert"; |
||||
constexpr static const char* Fragment = "ShapeShader2D.frag"; |
||||
}; |
||||
|
||||
template<> struct ShaderName<3> { |
||||
constexpr static const char* Vertex = "ShapeShader3D.vert"; |
||||
constexpr static const char* Fragment = "ShapeShader3D.frag"; |
||||
}; |
||||
} |
||||
|
||||
template<std::uint8_t dimensions> ShapeShader<dimensions>::ShapeShader() { |
||||
Corrade::Utility::Resource resource("MagnumPhysics"); |
||||
attachShader(Shader::fromData(Version::GL330, Shader::Type::Vertex, resource.get(ShaderName<dimensions>::Vertex))); |
||||
attachShader(Shader::fromData(Version::GL330, Shader::Type::Fragment, resource.get(ShaderName<dimensions>::Fragment))); |
||||
|
||||
link(); |
||||
|
||||
transformationProjectionUniform = uniformLocation("transformationProjection"); |
||||
colorUniform = uniformLocation("color"); |
||||
} |
||||
|
||||
template class ShapeShader<2>; |
||||
template class ShapeShader<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,52 @@
|
||||
#ifndef Magnum_Physics_Implementation_ShapeShader_h |
||||
#define Magnum_Physics_Implementation_ShapeShader_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 "Math/Matrix3.h" |
||||
#include "Math/Matrix4.h" |
||||
#include "AbstractShaderProgram.h" |
||||
#include "Color.h" |
||||
#include "DimensionTraits.h" |
||||
|
||||
namespace Magnum { namespace Physics { namespace Implementation { |
||||
|
||||
template<std::uint8_t dimensions> class ShapeShader: public AbstractShaderProgram { |
||||
public: |
||||
typedef Attribute<0, typename DimensionTraits<dimensions, GLfloat>::PointType> Position; |
||||
|
||||
ShapeShader(); |
||||
|
||||
ShapeShader<dimensions>* setTransformationProjection(const typename DimensionTraits<dimensions, GLfloat>::MatrixType& matrix) { |
||||
setUniform(transformationProjectionUniform, matrix); |
||||
return this; |
||||
} |
||||
|
||||
ShapeShader<dimensions>* setColor(const Color3<GLfloat>& color) { |
||||
setUniform(colorUniform, color); |
||||
return this; |
||||
} |
||||
|
||||
private: |
||||
GLint transformationProjectionUniform, |
||||
colorUniform; |
||||
}; |
||||
|
||||
extern template class ShapeShader<2>; |
||||
extern template class ShapeShader<3>; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,7 @@
|
||||
uniform vec3 color; |
||||
|
||||
out vec4 fragmentColor; |
||||
|
||||
void main() { |
||||
fragmentColor = vec4(color, 1.0); |
||||
} |
||||
Loading…
Reference in new issue