From dfa92c2998931cb04de2fa7a708c6e8d9376acc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 17 Oct 2012 13:38:22 +0200 Subject: [PATCH] Physics: Box2D debug renderer. --- src/Physics/CMakeLists.txt | 1 + src/Physics/DebugDrawResourceManager.cpp | 3 + src/Physics/Implementation/BoxRenderer.cpp | 81 ++++++++++++++++++++++ src/Physics/Implementation/BoxRenderer.h | 41 +++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/Physics/Implementation/BoxRenderer.cpp create mode 100644 src/Physics/Implementation/BoxRenderer.h diff --git a/src/Physics/CMakeLists.txt b/src/Physics/CMakeLists.txt index a9709f340..837b068a4 100644 --- a/src/Physics/CMakeLists.txt +++ b/src/Physics/CMakeLists.txt @@ -17,6 +17,7 @@ set(MagnumPhysics_SRCS Sphere.cpp Implementation/AbstractDebugRenderer.cpp + Implementation/BoxRenderer.cpp Implementation/ShapeShader.cpp ${MagnumPhysics_RCS}) set(MagnumPhysics_HEADERS diff --git a/src/Physics/DebugDrawResourceManager.cpp b/src/Physics/DebugDrawResourceManager.cpp index a19cb3152..92f27730f 100644 --- a/src/Physics/DebugDrawResourceManager.cpp +++ b/src/Physics/DebugDrawResourceManager.cpp @@ -22,6 +22,7 @@ #include "ShapedObject.h" #include "ShapeGroup.h" #include "Implementation/AbstractDebugRenderer.h" +#include "Implementation/BoxRenderer.h" #include "Implementation/ShapeShader.h" namespace Magnum { @@ -36,6 +37,8 @@ SceneGraph::Object2D* DebugDrawResourceManager::createDebugRenderer(AbstractShap SceneGraph::Object2D* DebugDrawResourceManager::createDebugMesh(SceneGraph::Object2D* parent, AbstractShape2D* shape, ResourceKey options) { switch(shape->type()) { + case AbstractShape2D::Type::Box: + return new Implementation::BoxRenderer<2>(*static_cast(shape), options, parent); case AbstractShape2D::Type::ShapeGroup: { if(!parent) parent = new SceneGraph::Object2D; ShapeGroup2D* group = static_cast(shape); diff --git a/src/Physics/Implementation/BoxRenderer.cpp b/src/Physics/Implementation/BoxRenderer.cpp new file mode 100644 index 000000000..e93e2e6ae --- /dev/null +++ b/src/Physics/Implementation/BoxRenderer.cpp @@ -0,0 +1,81 @@ +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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 "BoxRenderer.h" + +#include "Buffer.h" +#include "Physics/Box.h" +#include "Physics/DebugDrawResourceManager.h" +#include "Primitives/Cube.h" +#include "Primitives/Square.h" +#include "ShapeShader.h" + +namespace Magnum { namespace Physics { namespace Implementation { + +namespace { + template struct BoxMesh {}; + + template<> struct BoxMesh<2> { + constexpr static char shader[] = "shader2d"; + constexpr static char key[] = "box2d"; + + static Mesh* mesh() { + Primitives::Square square; + Mesh* mesh = new Mesh; + Buffer* buffer = mesh->addBuffer(Mesh::BufferType::NonInterleaved); + buffer->setData(*square.positions(0), Buffer::Usage::StaticDraw); + return mesh->setPrimitive(square.primitive()) + ->bindAttribute::Position>(buffer) + ->setVertexCount(square.positions(0)->size()); + } + }; + + template<> struct BoxMesh<3> { + constexpr static char shader[] = "shader3d"; + constexpr static char key[] = "box3d"; + + static Mesh* mesh() { + Primitives::Cube cube; + Mesh* mesh = new Mesh; + Buffer* buffer = mesh->addBuffer(Mesh::BufferType::NonInterleaved); + buffer->setData(*cube.positions(0), Buffer::Usage::StaticDraw); + return mesh->setPrimitive(cube.primitive()) + ->bindAttribute::Position>(buffer) + ->setVertexCount(cube.positions(0)->size()); + } + }; +} + +constexpr char BoxMesh<2>::shader[]; +constexpr char BoxMesh<2>::key[]; +constexpr char BoxMesh<3>::shader[]; +constexpr char BoxMesh<3>::key[]; + +template BoxRenderer::BoxRenderer(Box& box, ResourceKey options, typename SceneGraph::AbstractObject::ObjectType* parent): AbstractDebugRenderer(BoxMesh::shader, BoxMesh::key, options, parent), box(box) { + if(!this->mesh) + DebugDrawResourceManager::instance()->set(this->mesh.key(), BoxMesh::mesh(), ResourceDataState::Final, ResourcePolicy::Manual); +} + +template void BoxRenderer::draw(const typename DimensionTraits::MatrixType&, typename SceneGraph::AbstractObject::CameraType* camera) { + this->shader->setTransformationProjection(camera->projectionMatrix()*box.transformedTransformation()) + ->setColor(this->options->color) + ->use(); + this->mesh->draw(); +} + +template class BoxRenderer<2>; +template class BoxRenderer<3>; + +}}} diff --git a/src/Physics/Implementation/BoxRenderer.h b/src/Physics/Implementation/BoxRenderer.h new file mode 100644 index 000000000..1540c8347 --- /dev/null +++ b/src/Physics/Implementation/BoxRenderer.h @@ -0,0 +1,41 @@ +#ifndef Magnum_Physics_Implementation_BoxRenderer_h +#define Magnum_Physics_Implementation_BoxRenderer_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + 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" + +namespace Magnum { namespace Physics { + +template class Box; + +namespace Implementation { + +template class BoxRenderer: public AbstractDebugRenderer { + public: + BoxRenderer(Box& box, ResourceKey options, typename SceneGraph::AbstractObject::ObjectType* parent); + + void draw(const typename DimensionTraits::MatrixType& transformation, typename SceneGraph::AbstractObject::CameraType* camera); + + private: + Box& box; +}; + +extern template class BoxRenderer<2>; +extern template class BoxRenderer<3>; + +}}} + +#endif