mirror of https://github.com/mosra/magnum.git
Browse Source
The code is mostly copied and simplified from Capsule renderer. However, it is such simplification that any attempt to reduce duplicate code would result in much less maintainable thing.pull/23/head
7 changed files with 343 additions and 0 deletions
@ -0,0 +1,61 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "CylinderRenderer.h" |
||||
|
||||
#include "Mesh.h" |
||||
#include "DebugTools/ShapeRenderer.h" |
||||
#include "Shapes/Cylinder.h" |
||||
#include "Primitives/Cylinder.h" |
||||
#include "Primitives/Square.h" |
||||
#include "Shaders/Flat.h" |
||||
#include "Trade/MeshData2D.h" |
||||
#include "Trade/MeshData3D.h" |
||||
|
||||
#include "DebugTools/Implementation/CylinderRendererTransformation.h" |
||||
|
||||
namespace Magnum { namespace DebugTools { namespace Implementation { |
||||
|
||||
AbstractCylinderRenderer<2>::AbstractCylinderRenderer(): AbstractShapeRenderer<2>("cylinder2d", "cylinder2d-vertices", {}) { |
||||
if(!wireframeMesh) createResources(Primitives::Square::wireframe()); |
||||
} |
||||
|
||||
AbstractCylinderRenderer<3>::AbstractCylinderRenderer(): AbstractShapeRenderer<3>("cylinder3d", "cylinder3d-vertices", "cylinder3d-indices") { |
||||
if(!wireframeMesh) createResources(Primitives::Cylinder::wireframe(1, 40, 1.0f)); |
||||
} |
||||
|
||||
template<UnsignedInt dimensions> CylinderRenderer<dimensions>::CylinderRenderer(const Shapes::Implementation::AbstractShape<dimensions>& cylinder): cylinder(static_cast<const Shapes::Implementation::Shape<Shapes::Cylinder<dimensions>>&>(cylinder).shape) {} |
||||
|
||||
template<UnsignedInt dimensions> void CylinderRenderer<dimensions>::draw(Resource<ShapeRendererOptions>& options, const typename DimensionTraits<dimensions, Float>::MatrixType& projectionMatrix) { |
||||
AbstractShapeRenderer<dimensions>::wireframeShader->setTransformationProjectionMatrix(projectionMatrix* |
||||
Implementation::cylinderRendererTransformation<dimensions>(cylinder.a(), cylinder.b(), cylinder.radius())) |
||||
.setColor(options->color()) |
||||
.use(); |
||||
AbstractShapeRenderer<dimensions>::wireframeMesh->draw(); |
||||
} |
||||
|
||||
template class CylinderRenderer<2>; |
||||
template class CylinderRenderer<3>; |
||||
|
||||
}}} |
||||
@ -0,0 +1,60 @@
|
||||
#ifndef Magnum_DebugTools_Implementation_CylinderRenderer_h |
||||
#define Magnum_DebugTools_Implementation_CylinderRenderer_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "AbstractShapeRenderer.h" |
||||
|
||||
#include "Shapes/Shapes.h" |
||||
|
||||
#include "corradeCompatibility.h" |
||||
|
||||
namespace Magnum { namespace DebugTools { namespace Implementation { |
||||
|
||||
template<UnsignedInt dimensions> class AbstractCylinderRenderer; |
||||
|
||||
template<> class AbstractCylinderRenderer<2>: public AbstractShapeRenderer<2> { |
||||
public: |
||||
explicit AbstractCylinderRenderer(); |
||||
}; |
||||
|
||||
template<> class AbstractCylinderRenderer<3>: public AbstractShapeRenderer<3> { |
||||
public: |
||||
explicit AbstractCylinderRenderer(); |
||||
}; |
||||
|
||||
template<UnsignedInt dimensions> class CylinderRenderer: public AbstractCylinderRenderer<dimensions> { |
||||
public: |
||||
explicit CylinderRenderer(const Shapes::Implementation::AbstractShape<dimensions>& cylinder); |
||||
CylinderRenderer(const Shapes::Implementation::AbstractShape<dimensions>&&) = delete; |
||||
|
||||
void draw(Resource<ShapeRendererOptions>& options, const typename DimensionTraits<dimensions, Float>::MatrixType& projectionMatrix) override; |
||||
|
||||
private: |
||||
const Shapes::Cylinder<dimensions>& cylinder; |
||||
}; |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,88 @@
|
||||
#ifndef Magnum_DebugTools_Implementation_ForceRendererTransformation_h |
||||
#define Magnum_DebugTools_Implementation_ForceRendererTransformation_h |
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include "Math/Functions.h" |
||||
#include "Math/Matrix3.h" |
||||
#include "Math/Matrix4.h" |
||||
#include "Magnum.h" |
||||
#include "DimensionTraits.h" |
||||
|
||||
namespace Magnum { namespace DebugTools { namespace Implementation { |
||||
|
||||
template<UnsignedInt dimensions> typename DimensionTraits<dimensions, Float>::MatrixType cylinderRendererTransformation(const typename DimensionTraits<dimensions, Float>::VectorType& a, const typename DimensionTraits<dimensions, Float>::VectorType& b, Float radius); |
||||
|
||||
template<> Matrix3 cylinderRendererTransformation<2>(const Vector2& a, const Vector2& b, const Float radius) { |
||||
/* Vector from cylinder center to top hemisphere center */ |
||||
const Vector2 direction = 0.5f*(b - a); |
||||
const Float length = direction.length(); |
||||
|
||||
/* Capsule rotation and distance to caps after they are scaled to proper
|
||||
radius (if nonzero cylinder length) */ |
||||
Matrix3 rotation; |
||||
if(length >= Math::TypeTraits<Float>::epsilon()) { |
||||
rotation.up() = direction/length; |
||||
rotation.right() = rotation.up().perpendicular(); |
||||
CORRADE_INTERNAL_ASSERT(rotation.right().isNormalized()); |
||||
} |
||||
|
||||
/* Scaling and translation */ |
||||
return Matrix3::translation(0.5f*(a + b))*rotation*Matrix3::scaling({radius, length}); |
||||
} |
||||
|
||||
template<> Matrix4 cylinderRendererTransformation<3>(const Vector3& a, const Vector3& b, const Float radius) { |
||||
/* Vector from cylinder center to top hemisphere center */ |
||||
const Vector3 direction = 0.5f*(b - a); |
||||
const Float length = direction.length(); |
||||
|
||||
/* Capsule rotation and distance to caps after they are scaled to proper
|
||||
radius (if nonzero cylinder length) */ |
||||
Matrix4 rotation; |
||||
if(length >= Math::TypeTraits<Float>::epsilon()) { |
||||
const Vector3 directionNormalized = direction/length; |
||||
const Float dot = Vector3::dot(directionNormalized, Vector3::zAxis()); |
||||
|
||||
/* Direction is parallel to Z axis, special rotation case */ |
||||
if(Math::abs(dot) > 1.0f - Math::TypeTraits<Float>::epsilon()) { |
||||
rotation.up() = dot*Vector3::zAxis(); |
||||
rotation.right() = Vector3::xAxis(); |
||||
rotation.backward() = -dot*Vector3::yAxis(); |
||||
|
||||
/* Common case */ |
||||
} else { |
||||
rotation.up() = directionNormalized; |
||||
rotation.right() = Vector3::cross(rotation.up(), Vector3::zAxis()).normalized(); |
||||
rotation.backward() = Vector3::cross(rotation.right(), rotation.up()); |
||||
CORRADE_INTERNAL_ASSERT(rotation.up().isNormalized() && rotation.backward().isNormalized()); |
||||
} |
||||
} |
||||
|
||||
/* Scaling and translation */ |
||||
return Matrix4::translation(0.5f*(a + b))*rotation*Matrix4::scaling({radius, length, radius}); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
#endif |
||||
@ -0,0 +1,125 @@
|
||||
/*
|
||||
This file is part of Magnum. |
||||
|
||||
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a |
||||
copy of this software and associated documentation files (the "Software"), |
||||
to deal in the Software without restriction, including without limitation |
||||
the rights to use, copy, modify, merge, publish, distribute, sublicense, |
||||
and/or sell copies of the Software, and to permit persons to whom the |
||||
Software is furnished to do so, subject to the following conditions: |
||||
|
||||
The above copyright notice and this permission notice shall be included |
||||
in all copies or substantial portions of the Software. |
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
||||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
||||
DEALINGS IN THE SOFTWARE. |
||||
*/ |
||||
|
||||
#include <TestSuite/Tester.h> |
||||
|
||||
#include "DebugTools/Implementation/CylinderRendererTransformation.h" |
||||
|
||||
namespace Magnum { namespace DebugTools { namespace Test { |
||||
|
||||
class CylinderRendererTest: public TestSuite::Tester { |
||||
public: |
||||
explicit CylinderRendererTest(); |
||||
|
||||
void zeroLength2D(); |
||||
void common2D(); |
||||
|
||||
void zeroLength3D(); |
||||
void parallel3D(); |
||||
void antiParallel3D(); |
||||
void common3D(); |
||||
}; |
||||
|
||||
CylinderRendererTest::CylinderRendererTest() { |
||||
addTests({&CylinderRendererTest::zeroLength2D, |
||||
&CylinderRendererTest::common2D, |
||||
|
||||
&CylinderRendererTest::zeroLength3D, |
||||
&CylinderRendererTest::parallel3D, |
||||
&CylinderRendererTest::antiParallel3D, |
||||
&CylinderRendererTest::common3D}); |
||||
} |
||||
|
||||
void CylinderRendererTest::zeroLength2D() { |
||||
const Vector2 a(0.5f, 3.0f); |
||||
const Matrix3 transformation = Implementation::cylinderRendererTransformation<2>(a, a, 3.5f); |
||||
|
||||
CORRADE_COMPARE(transformation.rotationScaling(), (Math::Matrix<2, Float>::fromDiagonal({3.5f, 0.0f}))); |
||||
CORRADE_COMPARE(transformation.translation(), a); |
||||
} |
||||
|
||||
void CylinderRendererTest::common2D() { |
||||
const Vector2 a(0.5f, 3.0f); |
||||
const Vector2 b(7.5f, -1.0f); |
||||
const Matrix3 transformation = Implementation::cylinderRendererTransformation<2>(a, b, 3.5f); |
||||
|
||||
/* Rotation + scaling, test orthogonality */ |
||||
CORRADE_COMPARE(transformation.up(), Vector2(3.5f, -2.0f)); |
||||
CORRADE_COMPARE(transformation.right(), Vector2(4.0f, 7.0f).resized(3.5f)); |
||||
CORRADE_COMPARE(Vector2::dot(transformation.up(), transformation.right()), 0.0f); |
||||
|
||||
CORRADE_COMPARE(transformation.translation(), 0.5f*(a + b)); |
||||
} |
||||
|
||||
void CylinderRendererTest::zeroLength3D() { |
||||
const Vector3 a(0.5f, 3.0f, 7.0f); |
||||
const Matrix4 transformation = Implementation::cylinderRendererTransformation<3>(a, a, 3.5f); |
||||
|
||||
CORRADE_COMPARE(transformation.rotationScaling(), (Math::Matrix<3, Float>::fromDiagonal({3.5f, 0.0f, 3.5f}))); |
||||
CORRADE_COMPARE(transformation.translation(), a); |
||||
} |
||||
|
||||
void CylinderRendererTest::parallel3D() { |
||||
const Vector3 a(0.5f, 3.0f, 7.0f); |
||||
const Vector3 b(0.5f, 3.0f, 11.0f); |
||||
const Matrix4 transformation = Implementation::cylinderRendererTransformation<3>(a, b, 3.5f); |
||||
|
||||
CORRADE_COMPARE(transformation.rotationScaling(), |
||||
(Matrix4::rotationX(Deg(90.0f))*Matrix4::scaling({3.5f, 2.0f, 3.5f})).rotationScaling()); |
||||
|
||||
CORRADE_COMPARE(transformation.translation(), a+Vector3::zAxis(2.0f)); |
||||
} |
||||
|
||||
void CylinderRendererTest::antiParallel3D() { |
||||
const Vector3 a(0.5f, 3.0f, 7.0f); |
||||
const Vector3 b(0.5f, 3.0f, 3.0f); |
||||
const Matrix4 transformation = Implementation::cylinderRendererTransformation<3>(a, b, 3.5f); |
||||
|
||||
CORRADE_COMPARE(transformation.rotationScaling(), |
||||
(Matrix4::rotationX(-Deg(90.0f))*Matrix4::scaling({3.5f, 2.0f, 3.5f})).rotationScaling()); |
||||
|
||||
CORRADE_COMPARE(transformation.translation(), a+Vector3::zAxis(-2.0f)); |
||||
} |
||||
|
||||
void CylinderRendererTest::common3D() { |
||||
const Vector3 a(0.5f, 3.0f, 7.0f); |
||||
const Vector3 b(7.5f, -1.0f, 1.5f); |
||||
const Matrix4 transformation = Implementation::cylinderRendererTransformation<3>(a, b, 3.5f); |
||||
|
||||
/* Rotation + scaling */ |
||||
CORRADE_COMPARE(transformation.up(), Vector3(3.5f, -2.0f, -2.75f)); |
||||
CORRADE_COMPARE(transformation.right(), Vector3(-2.0f, -3.5f, 0.0f).resized(3.5f)); |
||||
CORRADE_COMPARE(transformation.backward(), Vector3(9.625f, -5.5f, 16.25f).resized(3.5f)); |
||||
|
||||
/* Orthogonality */ |
||||
CORRADE_COMPARE(Vector3::dot(transformation.up(), transformation.right()), 0.0f); |
||||
CORRADE_COMPARE(Vector3::dot(transformation.up(), transformation.backward()), 0.0f); |
||||
CORRADE_COMPARE(Vector3::dot(transformation.right(), transformation.backward()), 0.0f); |
||||
|
||||
CORRADE_COMPARE(transformation.translation(), 0.5f*(a + b)); |
||||
} |
||||
|
||||
}}} |
||||
|
||||
CORRADE_TEST_MAIN(Magnum::DebugTools::Test::CylinderRendererTest) |
||||
Loading…
Reference in new issue