Browse Source

Primitives: using half length for Capsule primitive.

pull/23/head
Vladimír Vondruš 13 years ago
parent
commit
ba72a56309
  1. 20
      src/Primitives/Capsule.cpp
  2. 8
      src/Primitives/Capsule.h

20
src/Primitives/Capsule.cpp

@ -31,14 +31,14 @@
namespace Magnum { namespace Primitives { namespace Magnum { namespace Primitives {
Trade::MeshData3D Capsule::solid(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float length, TextureCoords textureCoords) { Trade::MeshData3D Capsule::solid(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float halfLength, TextureCoords textureCoords) {
CORRADE_ASSERT(hemisphereRings >= 1 && cylinderRings >= 1 && segments >= 3, "Capsule must have at least one hemisphere ring, one cylinder ring and three segments", Trade::MeshData3D(Mesh::Primitive::Triangles, {}, {}, {}, {})); CORRADE_ASSERT(hemisphereRings >= 1 && cylinderRings >= 1 && segments >= 3, "Capsule must have at least one hemisphere ring, one cylinder ring and three segments", Trade::MeshData3D(Mesh::Primitive::Triangles, {}, {}, {}, {}));
Implementation::Spheroid capsule(segments, textureCoords == TextureCoords::Generate ? Implementation::Spheroid capsule(segments, textureCoords == TextureCoords::Generate ?
Implementation::Spheroid::TextureCoords::Generate : Implementation::Spheroid::TextureCoords::Generate :
Implementation::Spheroid::TextureCoords::DontGenerate); Implementation::Spheroid::TextureCoords::DontGenerate);
Float height = 2.0f+length; Float height = 2.0f+2.0f*halfLength;
Float hemisphereTextureCoordsVIncrement = 1.0f/(hemisphereRings*height); Float hemisphereTextureCoordsVIncrement = 1.0f/(hemisphereRings*height);
Rad hemisphereRingAngleIncrement(Constants::pi()/(2*hemisphereRings)); Rad hemisphereRingAngleIncrement(Constants::pi()/(2*hemisphereRings));
@ -46,13 +46,13 @@ Trade::MeshData3D Capsule::solid(UnsignedInt hemisphereRings, UnsignedInt cylind
capsule.capVertex(-height/2, -1.0f, 0.0f); capsule.capVertex(-height/2, -1.0f, 0.0f);
/* Rings of bottom hemisphere */ /* Rings of bottom hemisphere */
capsule.hemisphereVertexRings(hemisphereRings-1, -length/2, -Rad(Constants::pi())/2+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement); capsule.hemisphereVertexRings(hemisphereRings-1, -halfLength, -Rad(Constants::pi())/2+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement);
/* Rings of cylinder */ /* Rings of cylinder */
capsule.cylinderVertexRings(cylinderRings+1, -length/2, length/cylinderRings, 1.0f/height, length/(cylinderRings*height)); capsule.cylinderVertexRings(cylinderRings+1, -halfLength, 2.0f*halfLength/cylinderRings, 1.0f/height, 2.0f*halfLength/(cylinderRings*height));
/* Rings of top hemisphere */ /* Rings of top hemisphere */
capsule.hemisphereVertexRings(hemisphereRings-1, length/2, hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, (1.0f + length)/height+hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement); capsule.hemisphereVertexRings(hemisphereRings-1, halfLength, hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, (1.0f + 2.0f*halfLength)/height+hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement);
/* Top cap vertex */ /* Top cap vertex */
capsule.capVertex(height/2, 1.0f, 1.0f); capsule.capVertex(height/2, 1.0f, 1.0f);
@ -65,23 +65,23 @@ Trade::MeshData3D Capsule::solid(UnsignedInt hemisphereRings, UnsignedInt cylind
return capsule.finalize(); return capsule.finalize();
} }
Trade::MeshData3D Capsule::wireframe(const UnsignedInt hemisphereRings, const UnsignedInt cylinderRings, const UnsignedInt segments, const Float length) { Trade::MeshData3D Capsule::wireframe(const UnsignedInt hemisphereRings, const UnsignedInt cylinderRings, const UnsignedInt segments, const Float halfLength) {
CORRADE_ASSERT(hemisphereRings >= 1 && cylinderRings >= 1 && segments >= 4 && segments%4 == 0, "Primitives::Capsule::wireframe(): improper parameters", Trade::MeshData3D(Mesh::Primitive::Lines, {}, {}, {}, {})); CORRADE_ASSERT(hemisphereRings >= 1 && cylinderRings >= 1 && segments >= 4 && segments%4 == 0, "Primitives::Capsule::wireframe(): improper parameters", Trade::MeshData3D(Mesh::Primitive::Lines, {}, {}, {}, {}));
Implementation::WireframeSpheroid capsule(segments/4); Implementation::WireframeSpheroid capsule(segments/4);
/* Bottom hemisphere */ /* Bottom hemisphere */
capsule.bottomHemisphere(-length/2, hemisphereRings); capsule.bottomHemisphere(-halfLength, hemisphereRings);
/* Cylinder */ /* Cylinder */
capsule.ring(-length/2); capsule.ring(-halfLength);
for(UnsignedInt i = 0; i != cylinderRings; ++i) { for(UnsignedInt i = 0; i != cylinderRings; ++i) {
capsule.cylinder(); capsule.cylinder();
capsule.ring(-length/2 + (i+1)*(length/cylinderRings)); capsule.ring(-halfLength + (i+1)*(2.0f*halfLength/cylinderRings));
} }
/* Top hemisphere */ /* Top hemisphere */
capsule.topHemisphere(length/2, hemisphereRings); capsule.topHemisphere(halfLength, hemisphereRings);
return capsule.finalize(); return capsule.finalize();
} }

8
src/Primitives/Capsule.h

@ -54,14 +54,14 @@ class MAGNUM_PRIMITIVES_EXPORT Capsule {
* larger or equal to 1. * larger or equal to 1.
* @param segments Number of (face) segments. Must be larger or * @param segments Number of (face) segments. Must be larger or
* equal to 3. * equal to 3.
* @param length Length of the capsule, excluding hemispheres. * @param halfLength Half the length of cylinder part
* @param textureCoords Whether to generate texture coordinates. * @param textureCoords Whether to generate texture coordinates.
* *
* Indexed @ref Mesh::Primitive "Triangles" with normals and optional * Indexed @ref Mesh::Primitive "Triangles" with normals and optional
* 2D texture coordinates. If texture coordinates are generated, * 2D texture coordinates. If texture coordinates are generated,
* vertices of one segment are duplicated for texture wrapping. * vertices of one segment are duplicated for texture wrapping.
*/ */
static Trade::MeshData3D solid(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float length, TextureCoords textureCoords = TextureCoords::DontGenerate); static Trade::MeshData3D solid(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float halfLength, TextureCoords textureCoords = TextureCoords::DontGenerate);
/** /**
* @brief Wireframe capsule * @brief Wireframe capsule
@ -71,11 +71,11 @@ class MAGNUM_PRIMITIVES_EXPORT Capsule {
* larger or equal to 1. * larger or equal to 1.
* @param segments Number of line segments. Must be larger or * @param segments Number of line segments. Must be larger or
* equal to 4 and multiple of 4. * equal to 4 and multiple of 4.
* @param length Length of the capsule, excluding hemispheres. * @param halfLength Half the length of cylinder part
* *
* Indexed @ref Mesh::Primitive "Lines". * Indexed @ref Mesh::Primitive "Lines".
*/ */
static Trade::MeshData3D wireframe(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float length); static Trade::MeshData3D wireframe(UnsignedInt hemisphereRings, UnsignedInt cylinderRings, UnsignedInt segments, Float halfLength);
}; };
}} }}

Loading…
Cancel
Save