From d1a1b954dcae6010e2082423d89630e2954ce6f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 21 Feb 2013 15:29:10 +0100 Subject: [PATCH] Primitives: using strongly-typed angles. --- src/Primitives/Capsule.cpp | 29 +++++++++++++++-------------- src/Primitives/Capsule.h | 2 +- src/Primitives/Cylinder.cpp | 7 ++++--- src/Primitives/UVSphere.cpp | 6 +++--- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/Primitives/Capsule.cpp b/src/Primitives/Capsule.cpp index 6a0c45b58..edd224a53 100644 --- a/src/Primitives/Capsule.cpp +++ b/src/Primitives/Capsule.cpp @@ -15,6 +15,7 @@ #include "Capsule.h" +#include "Math/Functions.h" #include "Math/Point3D.h" namespace Magnum { namespace Primitives { @@ -24,13 +25,13 @@ Capsule::Capsule(std::uint32_t hemisphereRings, std::uint32_t cylinderRings, std GLfloat height = 2.0f+length; GLfloat hemisphereTextureCoordsVIncrement = 1.0f/(hemisphereRings*height); - GLfloat hemisphereRingAngleIncrement = Constants::pi()/(2*hemisphereRings); + Rad hemisphereRingAngleIncrement = Rad(Constants::pi())/(2*hemisphereRings); /* Bottom cap vertex */ capVertex(-height/2, -1.0f, 0.0f); /* Rings of bottom hemisphere */ - hemisphereVertexRings(hemisphereRings-1, -length/2, -Constants::pi()/2+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement); + hemisphereVertexRings(hemisphereRings-1, -length/2, -Rad(Constants::pi())/2+hemisphereRingAngleIncrement, hemisphereRingAngleIncrement, hemisphereTextureCoordsVIncrement, hemisphereTextureCoordsVIncrement); /* Rings of cylinder */ cylinderVertexRings(cylinderRings+1, -length/2, length/cylinderRings, 1.0f/height, length/(cylinderRings*height)); @@ -57,18 +58,18 @@ void Capsule::capVertex(GLfloat y, GLfloat normalY, GLfloat textureCoordsV) { textureCoords2D(0)->push_back({0.5, textureCoordsV}); } -void Capsule::hemisphereVertexRings(std::uint32_t count, GLfloat centerY, GLfloat startRingAngle, GLfloat ringAngleIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement) { - GLfloat segmentAngleIncrement = 2*Constants::pi()/segments; +void Capsule::hemisphereVertexRings(std::uint32_t count, GLfloat centerY, Rad startRingAngle, Rad ringAngleIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement) { + Rad segmentAngleIncrement = 2*Rad(Constants::pi())/segments; GLfloat x, y, z; for(std::uint32_t i = 0; i != count; ++i) { - GLfloat ringAngle = startRingAngle + i*ringAngleIncrement; - x = z = std::cos(ringAngle); - y = std::sin(ringAngle); + Rad ringAngle = startRingAngle + i*ringAngleIncrement; + x = z = Math::cos(ringAngle); + y = Math::sin(ringAngle); for(std::uint32_t j = 0; j != segments; ++j) { - GLfloat segmentAngle = j*segmentAngleIncrement; - positions(0)->push_back({x*std::sin(segmentAngle), centerY+y, z*std::cos(segmentAngle)}); - normals(0)->push_back({x*std::sin(segmentAngle), y, z*std::cos(segmentAngle)}); + Rad segmentAngle = j*segmentAngleIncrement; + positions(0)->push_back({x*Math::sin(segmentAngle), centerY+y, z*Math::cos(segmentAngle)}); + normals(0)->push_back({x*Math::sin(segmentAngle), y, z*Math::cos(segmentAngle)}); if(textureCoords == TextureCoords::Generate) textureCoords2D(0)->push_back({j*1.0f/segments, startTextureCoordsV + i*textureCoordsVIncrement}); @@ -84,12 +85,12 @@ void Capsule::hemisphereVertexRings(std::uint32_t count, GLfloat centerY, GLfloa } void Capsule::cylinderVertexRings(std::uint32_t count, GLfloat startY, GLfloat yIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement) { - GLfloat segmentAngleIncrement = 2*Constants::pi()/segments; + Rad segmentAngleIncrement = 2*Rad(Constants::pi())/segments; for(std::uint32_t i = 0; i != count; ++i) { for(std::uint32_t j = 0; j != segments; ++j) { - GLfloat segmentAngle = j*segmentAngleIncrement; - positions(0)->push_back({std::sin(segmentAngle), startY, std::cos(segmentAngle)}); - normals(0)->push_back({std::sin(segmentAngle), 0.0f, std::cos(segmentAngle)}); + Rad segmentAngle = j*segmentAngleIncrement; + positions(0)->push_back({Math::sin(segmentAngle), startY, Math::cos(segmentAngle)}); + normals(0)->push_back({Math::sin(segmentAngle), 0.0f, Math::cos(segmentAngle)}); if(textureCoords == TextureCoords::Generate) textureCoords2D(0)->push_back({j*1.0f/segments, startTextureCoordsV + i*textureCoordsVIncrement}); diff --git a/src/Primitives/Capsule.h b/src/Primitives/Capsule.h index a9cd054da..1ae25f78b 100644 --- a/src/Primitives/Capsule.h +++ b/src/Primitives/Capsule.h @@ -61,7 +61,7 @@ class Capsule: public Trade::MeshData3D { Capsule(std::uint32_t segments, TextureCoords textureCoords); void capVertex(GLfloat y, GLfloat normalY, GLfloat textureCoordsV); - void hemisphereVertexRings(std::uint32_t count, GLfloat centerY, GLfloat startRingAngle, GLfloat ringAngleIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement); + void hemisphereVertexRings(std::uint32_t count, GLfloat centerY, Rad startRingAngle, Rad ringAngleIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement); void cylinderVertexRings(std::uint32_t count, GLfloat startY, GLfloat yIncrement, GLfloat startTextureCoordsV, GLfloat textureCoordsVIncrement); void bottomFaceRing(); void faceRings(std::uint32_t count, std::uint32_t offset = 1); diff --git a/src/Primitives/Cylinder.cpp b/src/Primitives/Cylinder.cpp index d62848917..406ecbd24 100644 --- a/src/Primitives/Cylinder.cpp +++ b/src/Primitives/Cylinder.cpp @@ -15,6 +15,7 @@ #include "Cylinder.h" +#include "Math/Functions.h" #include "Math/Point3D.h" namespace Magnum { namespace Primitives { @@ -47,11 +48,11 @@ Cylinder::Cylinder(std::uint32_t rings, std::uint32_t segments, GLfloat length, } void Cylinder::capVertexRing(GLfloat y, GLfloat textureCoordsV, const Vector3& normal) { - GLfloat segmentAngleIncrement = 2*Constants::pi()/segments; + Rad segmentAngleIncrement = 2*Rad(Constants::pi())/segments; for(std::uint32_t i = 0; i != segments; ++i) { - GLfloat segmentAngle = i*segmentAngleIncrement; - positions(0)->push_back({std::sin(segmentAngle), y, std::cos(segmentAngle)}); + Rad segmentAngle = i*segmentAngleIncrement; + positions(0)->push_back({Math::sin(segmentAngle), y, Math::cos(segmentAngle)}); normals(0)->push_back(normal); if(textureCoords == TextureCoords::Generate) diff --git a/src/Primitives/UVSphere.cpp b/src/Primitives/UVSphere.cpp index 5d181dd79..0746be437 100644 --- a/src/Primitives/UVSphere.cpp +++ b/src/Primitives/UVSphere.cpp @@ -15,7 +15,7 @@ #include "UVSphere.h" -#include "Math/Constants.h" +#include "Math/Angle.h" namespace Magnum { namespace Primitives { @@ -23,13 +23,13 @@ UVSphere::UVSphere(std::uint32_t rings, std::uint32_t segments, TextureCoords te CORRADE_ASSERT(rings >= 2 && segments >= 3, "UVSphere must have at least two rings and three segments", ); GLfloat textureCoordsVIncrement = 1.0f/rings; - GLfloat ringAngleIncrement = Constants::pi()/rings; + Rad ringAngleIncrement = Rad(Constants::pi())/rings; /* Bottom cap vertex */ capVertex(-1.0f, -1.0f, 0.0f); /* Vertex rings */ - hemisphereVertexRings(rings-1, 0.0f, -Constants::pi()/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); + hemisphereVertexRings(rings-1, 0.0f, -Rad(Constants::pi())/2+ringAngleIncrement, ringAngleIncrement, textureCoordsVIncrement, textureCoordsVIncrement); /* Top cap vertex */ capVertex(1.0f, 1.0f, 1.0f);