From 89c76e2267322eeebfe147eefa4565f8292f104e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 21 Feb 2013 23:42:18 +0100 Subject: [PATCH] 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. --- src/Primitives/Capsule.cpp | 6 +++--- src/Primitives/Crosshair.cpp | 7 +++---- src/Primitives/Cube.cpp | 6 +++--- src/Primitives/Cylinder.cpp | 2 +- src/Primitives/Icosphere.cpp | 7 +++---- src/Primitives/Icosphere.h | 4 +--- src/Primitives/Plane.cpp | 6 +++--- src/Primitives/Square.cpp | 6 +++--- src/Primitives/Test/CapsuleTest.cpp | 6 +++--- src/Primitives/Test/CylinderTest.cpp | 6 +++--- src/Primitives/Test/UVSphereTest.cpp | 6 +++--- src/Trade/MeshData2D.cpp | 2 +- src/Trade/MeshData2D.h | 8 ++++---- src/Trade/MeshData3D.cpp | 2 +- src/Trade/MeshData3D.h | 8 ++++---- 15 files changed, 39 insertions(+), 43 deletions(-) diff --git a/src/Primitives/Capsule.cpp b/src/Primitives/Capsule.cpp index edd224a53..94b6b8e5d 100644 --- a/src/Primitives/Capsule.cpp +++ b/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, {new std::vector()}, {new std::vector()}, textureCoords == TextureCoords::Generate ? std::vector*>{new std::vector()} : std::vector*>()), 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, {new std::vector()}, {new std::vector()}, textureCoords == TextureCoords::Generate ? std::vector*>{new std::vector()} : std::vector*>()), 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, {new std::vector()}, {new std::vector()}, textureCoords == TextureCoords::Generate ? std::vector*>{new std::vector()} : std::vector*>()), segments(segments), textureCoords(textureCoords) {} +Capsule::Capsule(std::uint32_t segments, TextureCoords textureCoords): MeshData3D(Mesh::Primitive::Triangles, new std::vector, {new std::vector()}, {new std::vector()}, textureCoords == TextureCoords::Generate ? std::vector*>{new std::vector()} : std::vector*>()), segments(segments), textureCoords(textureCoords) {} void Capsule::capVertex(GLfloat y, GLfloat normalY, GLfloat textureCoordsV) { positions(0)->push_back({0.0f, y, 0.0f}); diff --git a/src/Primitives/Crosshair.cpp b/src/Primitives/Crosshair.cpp index 26b323d65..e38084498 100644 --- a/src/Primitives/Crosshair.cpp +++ b/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{ + return Trade::MeshData2D(Mesh::Primitive::Lines, nullptr, {new std::vector{ {-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{ + return Trade::MeshData3D(Mesh::Primitive::Lines, nullptr, {new std::vector{ {-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} diff --git a/src/Primitives/Cube.cpp b/src/Primitives/Cube.cpp index 51d5a4c02..6c63afdc6 100644 --- a/src/Primitives/Cube.cpp +++ b/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{ + }, {new std::vector{ {-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{ + }, {new std::vector{ {-1.0f, -1.0f, 1.0f}, { 1.0f, -1.0f, 1.0f}, { 1.0f, 1.0f, 1.0f}, diff --git a/src/Primitives/Cylinder.cpp b/src/Primitives/Cylinder.cpp index 406ecbd24..8b47b67f4 100644 --- a/src/Primitives/Cylinder.cpp +++ b/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 { diff --git a/src/Primitives/Icosphere.cpp b/src/Primitives/Icosphere.cpp index 0be978c15..f7e003ba3 100644 --- a/src/Primitives/Icosphere.cpp +++ b/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}, {new std::vector{ +}, {new std::vector}, {new std::vector{ {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()); } }} diff --git a/src/Primitives/Icosphere.h b/src/Primitives/Icosphere.h index 4f24331d6..79f5583e3 100644 --- a/src/Primitives/Icosphere.h +++ b/src/Primitives/Icosphere.h @@ -64,9 +64,7 @@ template 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()); } }; diff --git a/src/Primitives/Plane.cpp b/src/Primitives/Plane.cpp index 8bcd58c4c..3bf69aca2 100644 --- a/src/Primitives/Plane.cpp +++ b/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{ + return Trade::MeshData3D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector{ {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{ + return Trade::MeshData3D(Mesh::Primitive::LineLoop, nullptr, {new std::vector{ {-1.0f, -1.0f, 0.0f}, {1.0f, -1.0f, 0.0f}, {1.0f, 1.0f, 0.0f}, diff --git a/src/Primitives/Square.cpp b/src/Primitives/Square.cpp index fe2dcd971..9a20cc4d0 100644 --- a/src/Primitives/Square.cpp +++ b/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{ + return Trade::MeshData2D(Mesh::Primitive::TriangleStrip, nullptr, {new std::vector{ {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{ + return Trade::MeshData2D(Mesh::Primitive::LineLoop, nullptr, {new std::vector{ {-1.0f, -1.0f}, {1.0f, -1.0f}, {1.0f, 1.0f}, diff --git a/src/Primitives/Test/CapsuleTest.cpp b/src/Primitives/Test/CapsuleTest.cpp index a39871d77..84177dc4a 100644 --- a/src/Primitives/Test/CapsuleTest.cpp +++ b/src/Primitives/Test/CapsuleTest.cpp @@ -19,7 +19,7 @@ #include #include -#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{ + CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector{ {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{ + CORRADE_COMPARE_AS(*capsule.positions(0), (std::vector{ {0.0f, -1.5f, 0.0f}, {0.0f, -1.20711f, 0.707107f}, diff --git a/src/Primitives/Test/CylinderTest.cpp b/src/Primitives/Test/CylinderTest.cpp index dd951d8e5..5e847a984 100644 --- a/src/Primitives/Test/CylinderTest.cpp +++ b/src/Primitives/Test/CylinderTest.cpp @@ -16,7 +16,7 @@ #include #include -#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{ + CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector{ {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{ + CORRADE_COMPARE_AS(*cylinder.positions(0), (std::vector{ {0.0f, -1.5f, 0.0f}, {0.0f, -1.5f, 1.0f}, diff --git a/src/Primitives/Test/UVSphereTest.cpp b/src/Primitives/Test/UVSphereTest.cpp index 20c88f625..494a6f4d0 100644 --- a/src/Primitives/Test/UVSphereTest.cpp +++ b/src/Primitives/Test/UVSphereTest.cpp @@ -16,7 +16,7 @@ #include #include -#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{ + CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector{ {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{ + CORRADE_COMPARE_AS(*sphere.positions(0), (std::vector{ {0.0f, -1.0f, 0.0f}, {0.0f, -0.5f, 0.866025f}, diff --git a/src/Trade/MeshData2D.cpp b/src/Trade/MeshData2D.cpp index b143396bb..810fc0c96 100644 --- a/src/Trade/MeshData2D.cpp +++ b/src/Trade/MeshData2D.cpp @@ -15,7 +15,7 @@ #include "MeshData2D.h" -#include "Math/Point2D.h" +#include "Math/Vector2.h" namespace Magnum { namespace Trade { diff --git a/src/Trade/MeshData2D.h b/src/Trade/MeshData2D.h index c41ae1697..85f6ea7d3 100644 --- a/src/Trade/MeshData2D.h +++ b/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* indices, std::vector*> positions, std::vector*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _textureCoords2D(textureCoords2D) {} + inline MeshData2D(Mesh::Primitive primitive, std::vector* indices, std::vector*> positions, std::vector*> 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* positions(std::uint32_t id) { return _positions[id]; } - inline const std::vector* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */ + inline std::vector* positions(std::uint32_t id) { return _positions[id]; } + inline const std::vector* 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* _indices; - std::vector*> _positions; + std::vector*> _positions; std::vector*> _textureCoords2D; }; diff --git a/src/Trade/MeshData3D.cpp b/src/Trade/MeshData3D.cpp index e76968720..bfca98c39 100644 --- a/src/Trade/MeshData3D.cpp +++ b/src/Trade/MeshData3D.cpp @@ -15,7 +15,7 @@ #include "MeshData3D.h" -#include "Math/Point3D.h" +#include "Math/Vector3.h" namespace Magnum { namespace Trade { diff --git a/src/Trade/MeshData3D.h b/src/Trade/MeshData3D.h index eed0c1650..17a1ac802 100644 --- a/src/Trade/MeshData3D.h +++ b/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* indices, std::vector*> positions, std::vector*> normals, std::vector*> textureCoords2D): _primitive(primitive), _indices(indices), _positions(positions), _normals(normals), _textureCoords2D(textureCoords2D) {} + inline MeshData3D(Mesh::Primitive primitive, std::vector* indices, std::vector*> positions, std::vector*> normals, std::vector*> 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* positions(std::uint32_t id) { return _positions[id]; } - inline const std::vector* positions(std::uint32_t id) const { return _positions[id]; } /**< @overload */ + inline std::vector* positions(std::uint32_t id) { return _positions[id]; } + inline const std::vector* 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* _indices; - std::vector*> _positions; + std::vector*> _positions; std::vector*> _normals; std::vector*> _textureCoords2D; };