Browse Source

Primitives: using strongly-typed angles.

pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
d1a1b954dc
  1. 29
      src/Primitives/Capsule.cpp
  2. 2
      src/Primitives/Capsule.h
  3. 7
      src/Primitives/Cylinder.cpp
  4. 6
      src/Primitives/UVSphere.cpp

29
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});

2
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);

7
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)

6
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);

Loading…
Cancel
Save