Browse Source

DebugTools: ShapeRenderer cleanup.

* Added missing forward declarations.
 * Added getters and setters to ShapeRendererOptions class.
 * Cube primitive is indexed, modified the mesh creation accordingly. It
   now uses MeshTools, link the library to DebugTools.
 * Simplified resource handling so it's now handled in one place. It
   still needs to be thought out better.
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
22d405809f
  1. 2
      src/DebugTools/CMakeLists.txt
  2. 5
      src/DebugTools/DebugTools.h
  3. 94
      src/DebugTools/Implementation/AbstractBoxRenderer.cpp
  4. 21
      src/DebugTools/Implementation/AbstractBoxRenderer.h
  5. 2
      src/DebugTools/Implementation/AbstractShapeRenderer.cpp
  6. 8
      src/DebugTools/Implementation/AbstractShapeRenderer.h
  7. 2
      src/DebugTools/Implementation/AxisAlignedBoxRenderer.cpp
  8. 2
      src/DebugTools/Implementation/BoxRenderer.cpp
  9. 4
      src/DebugTools/ResourceManager.cpp
  10. 43
      src/DebugTools/ShapeRenderer.h

2
src/DebugTools/CMakeLists.txt

@ -18,7 +18,7 @@ set(MagnumDebugTools_HEADERS
add_library(MagnumDebugTools SHARED ${MagnumDebugTools_SRCS})
target_link_libraries(MagnumDebugTools Magnum MagnumPhysics MagnumPrimitives MagnumSceneGraph MagnumShaders)
target_link_libraries(MagnumDebugTools Magnum MagnumMeshTools MagnumPhysics MagnumPrimitives MagnumSceneGraph MagnumShaders)
install(TARGETS MagnumDebugTools DESTINATION ${MAGNUM_LIBRARY_INSTALL_DIR})
install(FILES ${MagnumDebugTools_HEADERS} DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}/DebugTools)

5
src/DebugTools/DebugTools.h

@ -25,8 +25,11 @@ namespace Magnum { namespace DebugTools {
class Profiler;
class ResourceManager;
template<std::uint8_t> class ShapeRenderer;
struct ShapeRendererOptions;
typedef ShapeRenderer<2> ShapeRenderer2D;
typedef ShapeRenderer<3> ShapeRenderer3D;
class ShapeRendererOptions;
}}

94
src/DebugTools/Implementation/AbstractBoxRenderer.cpp

@ -16,55 +16,71 @@
#include "AbstractBoxRenderer.h"
#include "Buffer.h"
#include "IndexedMesh.h"
#include "DebugTools/ResourceManager.h"
#include "MeshTools/CompressIndices.h"
#include "Primitives/Cube.h"
#include "Primitives/Square.h"
#include "Shaders/FlatShader.h"
namespace Magnum { namespace DebugTools { namespace Implementation {
namespace {
template<std::uint8_t> struct BoxMesh {};
template<> struct BoxMesh<2> {
static ResourceKey shader() { return {"shader2d"}; }
static ResourceKey key() { return {"box2d"}; }
static Mesh* mesh(Buffer* buffer) {
Primitives::Square square;
Mesh* mesh = new Mesh;
buffer->setData(*square.positions(0), Buffer::Usage::StaticDraw);
return mesh->setPrimitive(square.primitive())
->setVertexCount(square.positions(0)->size())
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position());
}
};
template<> struct BoxMesh<3> {
static ResourceKey shader() { return {"shader3d"}; }
static ResourceKey key() { return {"box3d"}; }
static Mesh* mesh(Buffer* buffer) {
Primitives::Cube cube;
Mesh* mesh = new Mesh;
buffer->setData(*cube.positions(0), Buffer::Usage::StaticDraw);
return mesh->setPrimitive(cube.primitive())
->setVertexCount(cube.positions(0)->size())
->addVertexBuffer(buffer, Shaders::FlatShader<2>::Position());
}
};
}
AbstractBoxRenderer<2>::AbstractBoxRenderer() {
/* Shader */
shader = ResourceManager::instance()->get<AbstractShaderProgram, Shaders::FlatShader2D>("FlatShader2D");
if(!shader) ResourceManager::instance()->set<AbstractShaderProgram>(shader.key(),
new Shaders::FlatShader2D, ResourceDataState::Final, ResourcePolicy::Resident);
/* Mesh and vertex buffer */
mesh = ResourceManager::instance()->get<Mesh>("box2d");
buffer = ResourceManager::instance()->get<Buffer>("box2d");
if(mesh) return;
/* Create the mesh */
Primitives::Square square;
Buffer* buffer = new Buffer(Buffer::Target::Array);
Mesh* mesh = new Mesh;
template<std::uint8_t dimensions> AbstractBoxRenderer<dimensions>::AbstractBoxRenderer(): AbstractShapeRenderer<dimensions>(BoxMesh<dimensions>::shader(), BoxMesh<dimensions>::key()), buffer(ResourceManager::instance()->get<Buffer>(BoxMesh<dimensions>::key())) {
if(!this->mesh) {
ResourceManager::instance()->set(this->buffer.key(), new Buffer, ResourceDataState::Final, ResourcePolicy::Manual);
ResourceManager::instance()->set<Mesh>(this->mesh.key(), BoxMesh<dimensions>::mesh(buffer), ResourceDataState::Final, ResourcePolicy::Manual);
}
buffer->setData(*square.positions(0), Buffer::Usage::StaticDraw);
ResourceManager::instance()->set(this->buffer.key(), buffer, ResourceDataState::Final, ResourcePolicy::Manual);
mesh->setPrimitive(square.primitive())
->setVertexCount(square.positions(0)->size())
->addVertexBuffer(buffer, Shaders::FlatShader2D::Position());
ResourceManager::instance()->set(this->mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}
template<std::uint8_t dimensions> AbstractBoxRenderer<dimensions>::~AbstractBoxRenderer() {}
AbstractBoxRenderer<3>::AbstractBoxRenderer() {
/* Shader */
shader = ResourceManager::instance()->get<AbstractShaderProgram, Shaders::FlatShader3D>("FlatShader3D");
if(!shader) ResourceManager::instance()->set<AbstractShaderProgram>(shader.key(),
new Shaders::FlatShader3D, ResourceDataState::Final, ResourcePolicy::Resident);
/* Mesh and vertex buffer */
mesh = ResourceManager::instance()->get<Mesh>("box3d");
vertexBuffer = ResourceManager::instance()->get<Buffer>("box3d-vertices");
indexBuffer = ResourceManager::instance()->get<Buffer>("box3d-indices");
if(mesh) return;
/* Create the mesh */
Primitives::Cube cube;
Buffer* vertexBuffer = new Buffer(Buffer::Target::Array);
Buffer* indexBuffer = new Buffer(Buffer::Target::ElementArray);
IndexedMesh* mesh = new IndexedMesh;
vertexBuffer->setData(*cube.positions(0), Buffer::Usage::StaticDraw);
ResourceManager::instance()->set(this->vertexBuffer.key(), vertexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
MeshTools::compressIndices(mesh, indexBuffer, Buffer::Usage::StaticDraw, *cube.indices());
ResourceManager::instance()->set(this->indexBuffer.key(), indexBuffer, ResourceDataState::Final, ResourcePolicy::Manual);
mesh->setPrimitive(cube.primitive())
->setVertexCount(cube.positions(0)->size())
->addVertexBuffer(vertexBuffer, Shaders::FlatShader3D::Position());
ResourceManager::instance()->set<Mesh>(this->mesh.key(), mesh, ResourceDataState::Final, ResourcePolicy::Manual);
}
template class AbstractBoxRenderer<2>;
template class AbstractBoxRenderer<3>;
AbstractBoxRenderer<2>::~AbstractBoxRenderer() {}
AbstractBoxRenderer<3>::~AbstractBoxRenderer() {}
}}}

21
src/DebugTools/Implementation/AbstractBoxRenderer.h

@ -17,20 +17,39 @@
#include "AbstractShapeRenderer.h"
#include "Resource.h"
#include "Shaders/Shaders.h"
#include "corradeCompatibility.h"
namespace Magnum { namespace DebugTools { namespace Implementation {
template<std::uint8_t dimensions> class AbstractBoxRenderer: public AbstractShapeRenderer<dimensions> {
template<std::uint8_t dimensions> class AbstractBoxRenderer {};
template<> class AbstractBoxRenderer<2>: public AbstractShapeRenderer<2> {
public:
AbstractBoxRenderer();
~AbstractBoxRenderer();
protected:
Resource<Mesh> mesh;
Resource<AbstractShaderProgram, Shaders::FlatShader2D> shader;
Resource<Buffer> buffer;
};
template<> class AbstractBoxRenderer<3>: public AbstractShapeRenderer<3> {
public:
AbstractBoxRenderer();
~AbstractBoxRenderer();
protected:
Resource<Mesh> mesh;
Resource<AbstractShaderProgram, Shaders::FlatShader3D> shader;
Resource<Buffer> vertexBuffer, indexBuffer;
};
}}}
#endif

2
src/DebugTools/Implementation/AbstractShapeRenderer.cpp

@ -22,8 +22,6 @@
namespace Magnum { namespace DebugTools { namespace Implementation {
template<std::uint8_t dimensions> AbstractShapeRenderer<dimensions>::AbstractShapeRenderer(ResourceKey shader, ResourceKey mesh): shader(ResourceManager::instance()->get<AbstractShaderProgram, Shaders::FlatShader<dimensions>>(shader)), mesh(ResourceManager::instance()->get<Mesh>(mesh)) {}
template<std::uint8_t dimensions> AbstractShapeRenderer<dimensions>::~AbstractShapeRenderer() {}
template class AbstractShapeRenderer<2>;

8
src/DebugTools/Implementation/AbstractShapeRenderer.h

@ -16,24 +16,16 @@
*/
#include "DimensionTraits.h"
#include "ResourceManager.h"
#include "SceneGraph/SceneGraph.h"
#include "Shaders/Shaders.h"
#include "DebugTools/DebugTools.h"
namespace Magnum { namespace DebugTools { namespace Implementation {
template<std::uint8_t dimensions> class AbstractShapeRenderer {
public:
AbstractShapeRenderer(ResourceKey shader, ResourceKey mesh);
virtual ~AbstractShapeRenderer();
virtual void draw(Resource<ShapeRendererOptions>& options, const typename DimensionTraits<dimensions>::MatrixType& transformationMatrix, SceneGraph::AbstractCamera<dimensions>* camera) = 0;
protected:
Resource<AbstractShaderProgram, Shaders::FlatShader<dimensions>> shader;
Resource<Mesh> mesh;
};
}}}

2
src/DebugTools/Implementation/AxisAlignedBoxRenderer.cpp

@ -28,7 +28,7 @@ template<std::uint8_t dimensions> void AxisAlignedBoxRenderer<dimensions>::draw(
DimensionTraits<dimensions>::MatrixType::translation(axisAlignedBox.transformedPosition())*
DimensionTraits<dimensions>::MatrixType::scaling(axisAlignedBox.transformedSize());
this->shader->setTransformationProjectionMatrix(camera->projectionMatrix()*camera->cameraMatrix()*transformation)
->setColor(options->color)
->setColor(options->color())
->use();
this->mesh->draw();
}

2
src/DebugTools/Implementation/BoxRenderer.cpp

@ -25,7 +25,7 @@ namespace Magnum { namespace DebugTools { namespace Implementation {
template<std::uint8_t dimensions> void BoxRenderer<dimensions>::draw(Resource<ShapeRendererOptions>& options, const typename DimensionTraits<dimensions>::MatrixType&, typename SceneGraph::AbstractCamera<dimensions>* camera) {
this->shader->setTransformationProjectionMatrix(camera->projectionMatrix()*camera->cameraMatrix()*box.transformedTransformation())
->setColor(options->color)
->setColor(options->color())
->use();
this->mesh->draw();
}

4
src/DebugTools/ResourceManager.cpp

@ -19,8 +19,7 @@
#include "Buffer.h"
#include "Mesh.h"
#include "Shaders/FlatShader.h"
#include "ShapeRenderer.h"
#include "DebugTools/ShapeRenderer.h"
namespace Magnum {
@ -30,7 +29,6 @@ namespace DebugTools {
ResourceManager::ResourceManager() {
setFallback(new ShapeRendererOptions);
set<AbstractShaderProgram>("shader2d", new Shaders::FlatShader<2>, ResourceDataState::Final, ResourcePolicy::Resident);
}
ResourceManager::~ResourceManager() {}

43
src/DebugTools/ShapeRenderer.h

@ -39,22 +39,38 @@ namespace Implementation {
See ShapeRenderer documentation for more information.
*/
struct ShapeRendererOptions {
Color3<> color; /**< @brief Color */
class ShapeRendererOptions {
public:
/** @brief Color of rendered shape */
inline constexpr Color3<> color() const { return _color; }
/**
* @brief Set color of rendered shape
* @return Pointer to self (for method chaining)
*
* Default is black.
*/
inline ShapeRendererOptions* setColor(const Color3<>& color) {
_color = color;
return this;
}
private:
Color3<> _color;
};
/**
@brief Shape renderer
Creates renderers for object collision shape.
Visualizes collision shape.
@section ShapeRenderer-usage Basic usage
ResourceManager must be instanced for the whole lifetime of debug
renderers. 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 renderers. If no options for given key
exist, default is used.
renderers. You can specify options via ShapeRendererOptions 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 renderers. If no options for
given key exist, default is used.
Example code:
@code
@ -66,14 +82,13 @@ DebugTools::ResourceManager manager;
SceneGraph::DrawableGroup2D debugDrawables;
// Create some options
auto o = new DebugTools::ShapeRendererOptions {
{1.0f, 0.0f, 0.0f} // Red color
};
manager->set<DebugTools::ShapeRendererOptions>("red", o, ResourceDataState::Final, ResourcePolicy::Persistent);
DebugTools::ResourceManager::instance()->set<DebugTools::ShapeRendererOptions>("red",
(new DebugTools::ShapeRendererOptions())->setColor({1.0f, 0.0f, 0.0f}),
ResourceDataState::Final, ResourcePolicy::Persistent);
// Create debug renderer for given shape, use "red" options for it
Physics::ObjectShape2D* shape;
debugDrawables.add(new DebugTools::ShapeRenderer2D(shape, "red", debugDrawables));
new DebugTools::ShapeRenderer2D(shape, "red", debugDrawables);
@endcode
@see ShapeRenderer2D, ShapeRenderer3D
@ -92,8 +107,8 @@ template<std::uint8_t dimensions> class MAGNUM_DEBUGTOOLS_EXPORT ShapeRenderer:
* information.
* @param drawables Drawable group
*
* @attention @p shape must be available for the whole lifetime of
* this class
* The renderer is automatically added to shape's object features,
* @p shape must be available for the whole lifetime of the renderer.
*/
explicit ShapeRenderer(Physics::ObjectShape<dimensions>* shape, ResourceKey options = ResourceKey(), SceneGraph::DrawableGroup<dimensions>* drawables = nullptr);

Loading…
Cancel
Save