From 9c34c00bb6f921e338e296d861fc74827d0b0d7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 21 Aug 2013 00:47:27 +0200 Subject: [PATCH] Primitives: use half length also in Cylinder primitive. Everything is now consistent. --- src/Primitives/Cylinder.cpp | 24 +++++++++++++----------- src/Primitives/Cylinder.h | 8 ++++---- src/Primitives/Test/CylinderTest.cpp | 6 +++--- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/Primitives/Cylinder.cpp b/src/Primitives/Cylinder.cpp index b1af5e6a8..da6b28b81 100644 --- a/src/Primitives/Cylinder.cpp +++ b/src/Primitives/Cylinder.cpp @@ -31,27 +31,27 @@ namespace Magnum { namespace Primitives { -Trade::MeshData3D Cylinder::solid(UnsignedInt rings, UnsignedInt segments, Float length, Cylinder::Flags flags) { +Trade::MeshData3D Cylinder::solid(const UnsignedInt rings, const UnsignedInt segments, const Float halfLength, const Flags flags) { CORRADE_ASSERT(rings >= 1 && segments >= 3, "Primitives::Cylinder::solid(): cylinder must have at least one ring and three segments", Trade::MeshData3D(Mesh::Primitive::Triangles, {}, {}, {}, {})); Implementation::Spheroid cylinder(segments, flags & Flag::GenerateTextureCoords ? Implementation::Spheroid::TextureCoords::Generate : Implementation::Spheroid::TextureCoords::DontGenerate); - Float y = length*0.5f; - Float textureCoordsV = flags & Flag::CapEnds ? 1.0f/(length+2.0f) : 0.0f; + const Float length = 2.0f*halfLength; + const Float textureCoordsV = flags & Flag::CapEnds ? 1.0f/(length+2.0f) : 0.0f; /* Bottom cap */ if(flags & Flag::CapEnds) { - cylinder.capVertex(-y, -1.0f, 0.0f); - cylinder.capVertexRing(-y, textureCoordsV, Vector3::yAxis(-1.0f)); + cylinder.capVertex(-halfLength, -1.0f, 0.0f); + cylinder.capVertexRing(-halfLength, textureCoordsV, Vector3::yAxis(-1.0f)); } /* Vertex rings */ - cylinder.cylinderVertexRings(rings+1, -y, length/rings, textureCoordsV, length/(rings*(flags & Flag::CapEnds ? length + 2.0f : length))); + cylinder.cylinderVertexRings(rings+1, -halfLength, length/rings, textureCoordsV, length/(rings*(flags & Flag::CapEnds ? length + 2.0f : length))); /* Top cap */ if(flags & Flag::CapEnds) { - cylinder.capVertexRing(y, 1.0f - textureCoordsV, Vector3::yAxis(1.0f)); - cylinder.capVertex(y, 1.0f, 1.0f); + cylinder.capVertexRing(halfLength, 1.0f - textureCoordsV, Vector3::yAxis(1.0f)); + cylinder.capVertex(halfLength, 1.0f, 1.0f); } /* Faces */ @@ -62,16 +62,18 @@ Trade::MeshData3D Cylinder::solid(UnsignedInt rings, UnsignedInt segments, Float return cylinder.finalize(); } -Trade::MeshData3D Cylinder::wireframe(const UnsignedInt rings, const UnsignedInt segments, const Float length) { +Trade::MeshData3D Cylinder::wireframe(const UnsignedInt rings, const UnsignedInt segments, const Float halfLength) { CORRADE_ASSERT(rings >= 1 && segments >= 4 && segments%4 == 0, "Primitives::Cylinder::wireframe(): improper parameters", Trade::MeshData3D(Mesh::Primitive::Lines, {}, {}, {}, {})); Implementation::WireframeSpheroid cylinder(segments/4); + const Float increment = 2*halfLength/rings; + /* Rings */ - cylinder.ring(-length/2); + cylinder.ring(-halfLength); for(UnsignedInt i = 0; i != rings; ++i) { cylinder.cylinder(); - cylinder.ring(-length/2 + (i+1)*(length/rings)); + cylinder.ring(-halfLength + (i+1)*increment); } return cylinder.finalize(); diff --git a/src/Primitives/Cylinder.h b/src/Primitives/Cylinder.h index 8b8d92bfb..e00946d63 100644 --- a/src/Primitives/Cylinder.h +++ b/src/Primitives/Cylinder.h @@ -62,7 +62,7 @@ class MAGNUM_PRIMITIVES_EXPORT Cylinder { * equal to 1. * @param segments Number of (face) segments. Must be larger or * equal to 3. - * @param length Cylinder length + * @param halfLength Half the cylinder length * @param flags Flags * * Indexed @ref Mesh::Primitive "Triangles" with normals, optional 2D @@ -70,7 +70,7 @@ class MAGNUM_PRIMITIVES_EXPORT Cylinder { * are generated, vertices of one segment are duplicated for texture * wrapping. */ - static Trade::MeshData3D solid(UnsignedInt rings, UnsignedInt segments, Float length, Flags flags = Flags()); + static Trade::MeshData3D solid(UnsignedInt rings, UnsignedInt segments, Float halfLength, Flags flags = Flags()); /** * @brief Wireframe cylinder @@ -78,11 +78,11 @@ class MAGNUM_PRIMITIVES_EXPORT Cylinder { * to 1. * @param segments Number of (line) segments. Must be larger or * equal to 4 and multiple of 4. - * @param length Cylinder length + * @param halfLength Half the cylinder length * * Indexed @ref Mesh::Primitive "Lines". */ - static Trade::MeshData3D wireframe(UnsignedInt rings, UnsignedInt segments, Float length); + static Trade::MeshData3D wireframe(UnsignedInt rings, UnsignedInt segments, Float halfLength); }; CORRADE_ENUMSET_OPERATORS(Cylinder::Flags) diff --git a/src/Primitives/Test/CylinderTest.cpp b/src/Primitives/Test/CylinderTest.cpp index 7aa532bf6..10bc34add 100644 --- a/src/Primitives/Test/CylinderTest.cpp +++ b/src/Primitives/Test/CylinderTest.cpp @@ -47,7 +47,7 @@ CylinderTest::CylinderTest() { } void CylinderTest::solidWithoutAnything() { - Trade::MeshData3D cylinder = Cylinder::solid(2, 3, 3.0f); + Trade::MeshData3D cylinder = Cylinder::solid(2, 3, 1.5f); CORRADE_COMPARE_AS(cylinder.positions(0), (std::vector{ {0.0f, -1.5f, 1.0f}, @@ -84,7 +84,7 @@ void CylinderTest::solidWithoutAnything() { } void CylinderTest::solidWithTextureCoordsAndCaps() { - Trade::MeshData3D cylinder = Cylinder::solid(2, 3, 3.0f, Cylinder::Flag::GenerateTextureCoords|Cylinder::Flag::CapEnds); + Trade::MeshData3D cylinder = Cylinder::solid(2, 3, 1.5f, Cylinder::Flag::GenerateTextureCoords|Cylinder::Flag::CapEnds); CORRADE_COMPARE_AS(cylinder.positions(0), (std::vector{ {0.0f, -1.5f, 0.0f}, @@ -188,7 +188,7 @@ void CylinderTest::solidWithTextureCoordsAndCaps() { } void CylinderTest::wireframe() { - Trade::MeshData3D cylinder = Cylinder::wireframe(2, 8, 1.0f); + Trade::MeshData3D cylinder = Cylinder::wireframe(2, 8, 0.5f); CORRADE_COMPARE_AS(cylinder.positions(0), (std::vector{ {0.0f, -0.5f, 1.0f},