Browse Source

Trade: got rid of Point2D/3D in MeshData in favor of Vector2/Vector3.

Positions were originally done using Point2D/3D to simplify their
transformation using matrices and to some extent simplify their usage in
shaders. But now the disadvantages exceeded the advantages:
 * They take 50% more for 2D positions and 33% more for 3D positions, as
   last coordinate is always equal to 1, on the other hand when last
   coordinate is errorneously not equal to 1 they have crazy behavior.
 * Normalizing them or transforming them with anything else than with
   matrices is PITA, as we need to strip the last component, do the
   transformation, and then add the component back.
 * All transformation handling classes (Complex, DualComplex,
   Quaternion, DualQuaternion, Matrix3, Matrix4) now have convenience
   functions for transforming points specified directly as
   Vector2/Vector3 (and also for transforming vectors).
 * When someone wants to use homogeneous coordinates with crazy last
   component values, they can do so with plain Vector3 for 2D and
   Vector4 for 3D and it will be less confusing than using Point2D/3D
   which no important detail hidden.
pull/7/head
Vladimír Vondruš 13 years ago
parent
commit
89c76e2267
  1. 6
      src/Primitives/Capsule.cpp
  2. 7
      src/Primitives/Crosshair.cpp
  3. 6
      src/Primitives/Cube.cpp
  4. 2
      src/Primitives/Cylinder.cpp
  5. 7
      src/Primitives/Icosphere.cpp
  6. 4
      src/Primitives/Icosphere.h
  7. 6
      src/Primitives/Plane.cpp
  8. 6
      src/Primitives/Square.cpp
  9. 6
      src/Primitives/Test/CapsuleTest.cpp
  10. 6
      src/Primitives/Test/CylinderTest.cpp
  11. 6
      src/Primitives/Test/UVSphereTest.cpp
  12. 2
      src/Trade/MeshData2D.cpp
  13. 8
      src/Trade/MeshData2D.h
  14. 2
      src/Trade/MeshData3D.cpp
  15. 8
      src/Trade/MeshData3D.h

6
src/Primitives/Capsule.cpp

@ -16,11 +16,11 @@
#include "Capsule.h"
#include "Math/Functions.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
namespace Magnum { namespace Primitives {
Capsule::Capsule(std::uint32_t hemisphereRings, std::uint32_t cylinderRings, std::uint32_t segments, GLfloat length, TextureCoords textureCoords): MeshData3D(Mesh::Primitive::Triangles, new std::vector<std::uint32_t>, {new std::vector<Point3D>()}, {new std::vector<Vector3>()}, textureCoords == TextureCoords::Generate ? std::vector<std::vector<Vector2>*>{new std::vector<Vector2>()} : std::vector<std::vector<Vector2>*>()), segments(segments), textureCoords(textureCoords) {
Capsule::Capsule(std::uint32_t hemisphereRings, std::uint32_t cylinderRings, std::uint32_t segments, GLfloat length, TextureCoords textureCoords): MeshData3D(Mesh::Primitive::Triangles, new std::vector<std::uint32_t>, {new std::vector<Vector3>()}, {new std::vector<Vector3>()}, textureCoords == TextureCoords::Generate ? std::vector<std::vector<Vector2>*>{new std::vector<Vector2>()} : std::vector<std::vector<Vector2>*>()), segments(segments), textureCoords(textureCoords) {
CORRADE_ASSERT(hemisphereRings >= 1 && cylinderRings >= 1 && segments >= 3, "Capsule must have at least one hemisphere ring, one cylinder ring and three segments", );
GLfloat height = 2.0f+length;
@ -48,7 +48,7 @@ Capsule::Capsule(std::uint32_t hemisphereRings, std::uint32_t cylinderRings, std
topFaceRing();
}
Capsule::Capsule(std::uint32_t segments, TextureCoords textureCoords): MeshData3D(Mesh::Primitive::Triangles, new std::vector<std::uint32_t>, {new std::vector<Point3D>()}, {new std::vector<Vector3>()}, textureCoords == TextureCoords::Generate ? std::vector<std::vector<Vector2>*>{new std::vector<Vector2>()} : std::vector<std::vector<Vector2>*>()), segments(segments), textureCoords(textureCoords) {}
Capsule::Capsule(std::uint32_t segments, TextureCoords textureCoords): MeshData3D(Mesh::Primitive::Triangles, new std::vector<std::uint32_t>, {new std::vector<Vector3>()}, {new std::vector<Vector3>()}, textureCoords == TextureCoords::Generate ? std::vector<std::vector<Vector2>*>{new std::vector<Vector2>()} : std::vector<std::vector<Vector2>*>()), segments(segments), textureCoords(textureCoords) {}
void Capsule::capVertex(GLfloat y, GLfloat normalY, GLfloat textureCoordsV) {
positions(0)->push_back({0.0f, y, 0.0f});

7
src/Primitives/Crosshair.cpp

@ -15,22 +15,21 @@
#include "Crosshair.h"
#include "Math/Point2D.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Trade/MeshData2D.h"
#include "Trade/MeshData3D.h"
namespace Magnum { namespace Primitives {
Trade::MeshData2D Crosshair2D::wireframe() {
return Trade::MeshData2D(Mesh::Primitive::Lines, nullptr, {new std::vector<Point2D>{
return Trade::MeshData2D(Mesh::Primitive::Lines, nullptr, {new std::vector<Vector2>{
{-1.0f, 0.0f}, {1.0f, 0.0f},
{ 0.0f, -1.0f}, {0.0f, 1.0f}
}}, {});
}
Trade::MeshData3D Crosshair3D::wireframe() {
return Trade::MeshData3D(Mesh::Primitive::Lines, nullptr, {new std::vector<Point3D>{
return Trade::MeshData3D(Mesh::Primitive::Lines, nullptr, {new std::vector<Vector3>{
{-1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f},
{ 0.0f, -1.0f, 0.0f}, {0.0f, 1.0f, 0.0f},
{ 0.0f, 0.0f, -1.0f}, {0.0f, 0.0f, 1.0f}

6
src/Primitives/Cube.cpp

@ -15,7 +15,7 @@
#include "Cube.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Trade/MeshData3D.h"
namespace Magnum { namespace Primitives {
@ -28,7 +28,7 @@ Trade::MeshData3D Cube::solid() {
12, 13, 14, 12, 14, 15, /* -Z */
16, 17, 18, 16, 18, 19, /* -Y */
20, 21, 22, 20, 22, 23 /* -X */
}, {new std::vector<Point3D>{
}, {new std::vector<Vector3>{
{-1.0f, -1.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f}, /* +Z */
@ -97,7 +97,7 @@ Trade::MeshData3D Cube::wireframe() {
4, 5, 5, 6, 6, 7, 7, 4, /* -Z */
1, 5, 2, 6, /* +X */
0, 4, 3, 7 /* -X */
}, {new std::vector<Point3D>{
}, {new std::vector<Vector3>{
{-1.0f, -1.0f, 1.0f},
{ 1.0f, -1.0f, 1.0f},
{ 1.0f, 1.0f, 1.0f},

2
src/Primitives/Cylinder.cpp

@ -16,7 +16,7 @@
#include "Cylinder.h"
#include "Math/Functions.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
namespace Magnum { namespace Primitives {

7
src/Primitives/Icosphere.cpp

@ -15,7 +15,7 @@
#include "Icosphere.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
namespace Magnum { namespace Primitives {
@ -40,7 +40,7 @@ Icosphere<0>::Icosphere(): MeshData3D(Mesh::Primitive::Triangles, new std::vecto
7, 1, 0,
3, 9, 8,
4, 8, 0
}, {new std::vector<Point3D>}, {new std::vector<Vector3>{
}, {new std::vector<Vector3>}, {new std::vector<Vector3>{
{0.0f, -0.525731f, 0.850651f},
{0.850651f, 0.0f, 0.525731f},
{0.850651f, 0.0f, -0.525731f},
@ -54,8 +54,7 @@ Icosphere<0>::Icosphere(): MeshData3D(Mesh::Primitive::Triangles, new std::vecto
{0.0f, 0.525731f, -0.850651f},
{0.0f, 0.525731f, 0.850651f}
}}, {}) {
positions(0)->reserve(normals(0)->size());
for(auto i: *normals(0)) positions(0)->push_back(Point3D(i));
positions(0)->assign(normals(0)->begin(), normals(0)->end());
}
}}

4
src/Primitives/Icosphere.h

@ -64,9 +64,7 @@ template<std::size_t subdivisions> class Icosphere {
});
MeshTools::clean(*indices(), *normals(0));
positions(0)->clear();
positions(0)->reserve(normals(0)->size());
for(auto i: *normals(0)) positions(0)->push_back(Point3D(i));
positions(0)->assign(normals(0)->begin(), normals(0)->end());
}
};

6
src/Primitives/Plane.cpp

@ -15,13 +15,13 @@
#include "Plane.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Trade/MeshData3D.h"
namespace Magnum { namespace Primitives {
Trade::MeshData3D Plane::solid() {
return Trade::MeshData3D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector<Point3D>{
return Trade::MeshData3D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector<Vector3>{
{1.0f, -1.0f, 0.0f},
{1.0f, 1.0f, 0.0f},
{-1.0f, -1.0f, 0.0f},
@ -35,7 +35,7 @@ Trade::MeshData3D Plane::solid() {
}
Trade::MeshData3D Plane::wireframe() {
return Trade::MeshData3D(Mesh::Primitive::LineLoop, nullptr, {new std::vector<Point3D>{
return Trade::MeshData3D(Mesh::Primitive::LineLoop, nullptr, {new std::vector<Vector3>{
{-1.0f, -1.0f, 0.0f},
{1.0f, -1.0f, 0.0f},
{1.0f, 1.0f, 0.0f},

6
src/Primitives/Square.cpp

@ -15,13 +15,13 @@
#include "Square.h"
#include "Math/Point2D.h"
#include "Math/Vector2.h"
#include "Trade/MeshData2D.h"
namespace Magnum { namespace Primitives {
Trade::MeshData2D Square::solid() {
return Trade::MeshData2D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector<Point2D>{
return Trade::MeshData2D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector<Vector2>{
{1.0f, -1.0f},
{1.0f, 1.0f},
{-1.0f, -1.0f},
@ -30,7 +30,7 @@ Trade::MeshData2D Square::solid() {
}
Trade::MeshData2D Square::wireframe() {
return Trade::MeshData2D(Mesh::Primitive::LineLoop, nullptr, {new std::vector<Point2D>{
return Trade::MeshData2D(Mesh::Primitive::LineLoop, nullptr, {new std::vector<Vector2>{
{-1.0f, -1.0f},
{1.0f, -1.0f},
{1.0f, 1.0f},

6
src/Primitives/Test/CapsuleTest.cpp

@ -19,7 +19,7 @@
#include <TestSuite/Tester.h>
#include <TestSuite/Compare/Container.h>
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Primitives/Capsule.h"
using Corrade::TestSuite::Compare::Container;
@ -42,7 +42,7 @@ CapsuleTest::CapsuleTest() {
void CapsuleTest::withoutTextureCoords() {
Capsule capsule(2, 2, 3, 1.0f);
CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector<Vector3>{
{0.0f, -1.5f, 0.0f},
{0.0f, -1.20711f, 0.707107f},
@ -107,7 +107,7 @@ void CapsuleTest::withoutTextureCoords() {
void CapsuleTest::withTextureCoords() {
Capsule capsule(2, 2, 3, 1.0f, Capsule::TextureCoords::Generate);
CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector<Vector3>{
{0.0f, -1.5f, 0.0f},
{0.0f, -1.20711f, 0.707107f},

6
src/Primitives/Test/CylinderTest.cpp

@ -16,7 +16,7 @@
#include <TestSuite/Tester.h>
#include <TestSuite/Compare/Container.h>
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Primitives/Cylinder.h"
using Corrade::TestSuite::Compare::Container;
@ -39,7 +39,7 @@ CylinderTest::CylinderTest() {
void CylinderTest::withoutAnything() {
Cylinder cylinder(2, 3, 3.0f);
CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector<Vector3>{
{0.0f, -1.5f, 1.0f},
{0.866025f, -1.5f, -0.5f},
{-0.866025f, -1.5f, -0.5f},
@ -76,7 +76,7 @@ void CylinderTest::withoutAnything() {
void CylinderTest::withTextureCoordsAndCaps() {
Cylinder cylinder(2, 3, 3.0f, Cylinder::Flag::GenerateTextureCoords|Cylinder::Flag::CapEnds);
CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector<Vector3>{
{0.0f, -1.5f, 0.0f},
{0.0f, -1.5f, 1.0f},

6
src/Primitives/Test/UVSphereTest.cpp

@ -16,7 +16,7 @@
#include <TestSuite/Tester.h>
#include <TestSuite/Compare/Container.h>
#include "Math/Point3D.h"
#include "Math/Vector3.h"
#include "Primitives/UVSphere.h"
using Corrade::TestSuite::Compare::Container;
@ -39,7 +39,7 @@ UVSphereTest::UVSphereTest() {
void UVSphereTest::withoutTextureCoords() {
UVSphere sphere(3, 3);
CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector<Vector3>{
{0.0f, -1.0f, 0.0f},
{0.0f, -0.5f, 0.866025f},
@ -77,7 +77,7 @@ void UVSphereTest::withoutTextureCoords() {
void UVSphereTest::withTextureCoords() {
UVSphere sphere(3, 3, UVSphere::TextureCoords::Generate);
CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector<Point3D>{
CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector<Vector3>{
{0.0f, -1.0f, 0.0f},
{0.0f, -0.5f, 0.866025f},

2
src/Trade/MeshData2D.cpp

@ -15,7 +15,7 @@
#include "MeshData2D.h"
#include "Math/Point2D.h"
#include "Math/Vector2.h"
namespace Magnum { namespace Trade {

8
src/Trade/MeshData2D.h

@ -47,7 +47,7 @@ class MAGNUM_EXPORT MeshData2D {
* @param textureCoords2D Array with two-dimensional texture
* coordinate arrays or empty array
*/
inline MeshData2D(Mesh::Primitive primitive, std::vector<std::uint32_t>* indices, std::vector<std::vector<Point2D>*> positions, std::vector<std::vector<Vector2>*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _textureCoords2D(textureCoords2D) {}
inline MeshData2D(Mesh::Primitive primitive, std::vector<std::uint32_t>* indices, std::vector<std::vector<Vector2>*> positions, std::vector<std::vector<Vector2>*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _textureCoords2D(textureCoords2D) {}
/** @brief Move constructor */
MeshData2D(MeshData2D&&) = default;
@ -77,8 +77,8 @@ class MAGNUM_EXPORT MeshData2D {
* @return Positions or nullptr if there is no vertex array with given
* ID.
*/
inline std::vector<Point2D>* positions(std::uint32_t id) { return _positions[id]; }
inline const std::vector<Point2D>* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */
inline std::vector<Vector2>* positions(std::uint32_t id) { return _positions[id]; }
inline const std::vector<Vector2>* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */
/** @brief Count of 2D texture coordinate arrays */
inline std::uint32_t textureCoords2DArrayCount() const { return _textureCoords2D.size(); }
@ -95,7 +95,7 @@ class MAGNUM_EXPORT MeshData2D {
private:
Mesh::Primitive _primitive;
std::vector<std::uint32_t>* _indices;
std::vector<std::vector<Point2D>*> _positions;
std::vector<std::vector<Vector2>*> _positions;
std::vector<std::vector<Vector2>*> _textureCoords2D;
};

2
src/Trade/MeshData3D.cpp

@ -15,7 +15,7 @@
#include "MeshData3D.h"
#include "Math/Point3D.h"
#include "Math/Vector3.h"
namespace Magnum { namespace Trade {

8
src/Trade/MeshData3D.h

@ -48,7 +48,7 @@ class MAGNUM_EXPORT MeshData3D {
* @param textureCoords2D Array with two-dimensional texture
* coordinate arrays or empty array
*/
inline MeshData3D(Mesh::Primitive primitive, std::vector<std::uint32_t>* indices, std::vector<std::vector<Point3D>*> positions, std::vector<std::vector<Vector3>*> normals, std::vector<std::vector<Vector2>*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _normals(normals), _textureCoords2D(textureCoords2D) {}
inline MeshData3D(Mesh::Primitive primitive, std::vector<std::uint32_t>* indices, std::vector<std::vector<Vector3>*> positions, std::vector<std::vector<Vector3>*> normals, std::vector<std::vector<Vector2>*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _normals(normals), _textureCoords2D(textureCoords2D) {}
/** @brief Move constructor */
MeshData3D(MeshData3D&&) = default;
@ -78,8 +78,8 @@ class MAGNUM_EXPORT MeshData3D {
* @return Positions or nullptr if there is no vertex array with given
* ID.
*/
inline std::vector<Point3D>* positions(std::uint32_t id) { return _positions[id]; }
inline const std::vector<Point3D>* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */
inline std::vector<Vector3>* positions(std::uint32_t id) { return _positions[id]; }
inline const std::vector<Vector3>* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */
/** @brief Count of normal arrays */
inline std::uint32_t normalArrayCount() const { return _normals.size(); }
@ -108,7 +108,7 @@ class MAGNUM_EXPORT MeshData3D {
private:
Mesh::Primitive _primitive;
std::vector<std::uint32_t>* _indices;
std::vector<std::vector<Point3D>*> _positions;
std::vector<std::vector<Vector3>*> _positions;
std::vector<std::vector<Vector3>*> _normals;
std::vector<std::vector<Vector2>*> _textureCoords2D;
};

Loading…
Cancel
Save