Skip to content

Commit

Permalink
[wip] Primitives: update to use the new MeshData.
Browse files Browse the repository at this point in the history
Just two primitives converted and it's already ~200 kB saved in Debug?
Wha?

TODO: do I need to update docs (types, attribs)? what about
  backwards-compat includes?
  • Loading branch information
mosra committed Dec 2, 2019
1 parent cb2da19 commit 2813e49
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 87 deletions.
154 changes: 84 additions & 70 deletions src/Magnum/Primitives/Axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,83 +27,97 @@

#include "Magnum/Mesh.h"
#include "Magnum/Math/Color.h"
#include "Magnum/Trade/MeshData2D.h"
#include "Magnum/Trade/MeshData3D.h"
#include "Magnum/Trade/MeshData.h"

namespace Magnum { namespace Primitives {

using namespace Math::Literals;

Trade::MeshData2D axis2D() {
return Trade::MeshData2D{MeshPrimitive::Lines,
{0, 1,
1, 2, /* X axis */
1, 3,

4, 5,
5, 6, /* Y axis */
5, 7},
{{{ 0.0f, 0.0f},
{ 1.0f, 0.0f}, /* X axis */
{ 0.9f, 0.1f},
{ 0.9f, -0.1f},

{ 0.0f, 0.0f},
{ 0.0f, 1.0f}, /* Y axis */
{ 0.1f, 0.9f},
{-0.1f, 0.9f}}}, {},
{{0xff0000_rgbf,
0xff0000_rgbf, /* X axis */
0xff0000_rgbf,
0xff0000_rgbf,

0x00ff00_rgbf,
0x00ff00_rgbf, /* Y axis */
0x00ff00_rgbf,
0x00ff00_rgbf}}};
namespace {

/* not 8-bit because GPUs (and Vulkan) don't like it nowadays */
constexpr UnsignedShort Indices2D[]{
0, 1,
1, 2, /* X axis */
1, 3,

4, 5,
5, 6, /* Y axis */
5, 7
};
constexpr UnsignedShort Indices3D[]{
0, 1,
1, 2, /* X axis */
1, 3,

4, 5,
5, 6, /* Y axis */
5, 7,

8, 9,
9, 10, /* Z axis */
9, 11
};

constexpr struct Vertex2D {
Vector2 position;
Color3 color;
} Vertices2D[]{
{{ 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{ 1.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, /* X axis */
{{ 0.9f, 0.1f}, {1.0f, 0.0f, 0.0f}},
{{ 0.9f, -0.1f}, {1.0f, 0.0f, 0.0f}},

{{ 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{ 0.0f, 1.0f}, {0.0f, 1.0f, 0.0f}}, /* Y axis */
{{ 0.1f, 0.9f}, {0.0f, 1.0f, 0.0f}},
{{-0.1f, 0.9f}, {0.0f, 1.0f, 0.0f}}
};
constexpr struct Vertex3D {
Vector3 position;
Color3 color;
} Vertices3D[]{
{{ 0.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{ 1.0f, 0.0f, 0.0f}, {1.0f, 0.0f, 0.0f}}, /* X axis */
{{ 0.9f, 0.1f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{ 0.9f, -0.1f, 0.0f}, {1.0f, 0.0f, 0.0f}},

{{ 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{ 0.0f, 1.0f, 0.0f}, {0.0f, 1.0f, 0.0f}}, /* Y axis */
{{ 0.1f, 0.9f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{-0.1f, 0.9f, 0.0f}, {0.0f, 1.0f, 0.0f}},

{{ 0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{ 0.0f, 0.0f, 1.0f}, {0.0f, 0.0f, 1.0f}}, /* Z axis */
{{ 0.1f, 0.0f, 0.9f}, {0.0f, 0.0f, 1.0f}},
{{-0.1f, 0.0f, 0.9f}, {0.0f, 0.0f, 1.0f}}
};

constexpr Trade::MeshAttributeData Attributes2D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::StridedArrayView1D<const Vector2>{Vertices2D, &Vertices2D[0].position, Containers::arraySize(Vertices2D), sizeof(Vertex2D)}},
Trade::MeshAttributeData{Trade::MeshAttributeName::Color,
Containers::StridedArrayView1D<const Color3>{Vertices2D, &Vertices2D[0].color, Containers::arraySize(Vertices2D), sizeof(Vertex2D)}}
};
constexpr Trade::MeshAttributeData Attributes3D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position,
Containers::StridedArrayView1D<const Vector3>{Vertices3D, &Vertices3D[0].position, Containers::arraySize(Vertices3D), sizeof(Vertex3D)}},
Trade::MeshAttributeData{Trade::MeshAttributeName::Color,
Containers::StridedArrayView1D<const Color3>{Vertices3D, &Vertices3D[0].color, Containers::arraySize(Vertices3D), sizeof(Vertex3D)}}
};

}

Trade::MeshData axis2D() {
return Trade::MeshData{MeshPrimitive::Lines,
{}, Indices2D, Trade::MeshIndexData{Indices2D},
{}, Vertices2D, Trade::meshAttributeDataNonOwningArray(Attributes2D)};
}

Trade::MeshData3D axis3D() {
return Trade::MeshData3D{MeshPrimitive::Lines,
{0, 1,
1, 2, /* X axis */
1, 3,

4, 5,
5, 6, /* Y axis */
5, 7,

8, 9,
9, 10, /* Z axis */
9, 11},
{{{ 0.0f, 0.0f, 0.0f},
{ 1.0f, 0.0f, 0.0f}, /* X axis */
{ 0.9f, 0.1f, 0.0f},
{ 0.9f, -0.1f, 0.0f},

{ 0.0f, 0.0f, 0.0f},
{ 0.0f, 1.0f, 0.0f}, /* Y axis */
{ 0.1f, 0.9f, 0.0f},
{-0.1f, 0.9f, 0.0f},

{ 0.0f, 0.0f, 0.0f},
{ 0.0f, 0.0f, 1.0f}, /* Z axis */
{ 0.1f, 0.0f, 0.9f},
{-0.1f, 0.0f, 0.9f}}}, {}, {},
{{0xff0000_rgbf,
0xff0000_rgbf, /* X axis */
0xff0000_rgbf,
0xff0000_rgbf,

0x00ff00_rgbf,
0x00ff00_rgbf, /* Y axis */
0x00ff00_rgbf,
0x00ff00_rgbf,

0x0000ff_rgbf,
0x0000ff_rgbf, /* Z axis */
0x0000ff_rgbf,
0x0000ff_rgbf}}};
Trade::MeshData axis3D() {
return Trade::MeshData{MeshPrimitive::Lines,
{}, Indices3D, Trade::MeshIndexData{Indices3D},
{}, Vertices3D, Trade::meshAttributeDataNonOwningArray(Attributes3D)};
}

}}
4 changes: 2 additions & 2 deletions src/Magnum/Primitives/Axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Two color-coded arrows for visualizing orientation (XY is RG). Indexed
@see @ref axis3D(), @ref crosshair2D(), @ref line2D()
*/
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData2D axis2D();
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData axis2D();

/**
@brief 3D axis
Expand All @@ -56,7 +56,7 @@ Three color-coded arrows for visualizing orientation (XYZ is RGB). Indexed
@see @ref axis2D(), @ref crosshair3D(), @ref line3D()
*/
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData3D axis3D();
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData axis3D();

}}

Expand Down
41 changes: 28 additions & 13 deletions src/Magnum/Primitives/Crosshair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,39 @@

#include "Magnum/Mesh.h"
#include "Magnum/Math/Color.h"
#include "Magnum/Trade/MeshData2D.h"
#include "Magnum/Trade/MeshData3D.h"
#include "Magnum/Trade/MeshData.h"

namespace Magnum { namespace Primitives {

Trade::MeshData2D crosshair2D() {
return Trade::MeshData2D{MeshPrimitive::Lines, {}, {{
{-1.0f, 0.0f}, {1.0f, 0.0f},
{ 0.0f, -1.0f}, {0.0f, 1.0f}
}}, {}, {}, nullptr};
namespace {

constexpr Vector2 Positions2D[]{
{-1.0f, 0.0f}, {1.0f, 0.0f},
{ 0.0f, -1.0f}, {0.0f, 1.0f}
};
constexpr Vector3 Positions3D[]{
{-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}
};

constexpr Trade::MeshAttributeData Attributes2D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position, Containers::arrayView(Positions2D)}
};
constexpr Trade::MeshAttributeData Attributes3D[]{
Trade::MeshAttributeData{Trade::MeshAttributeName::Position, Containers::arrayView(Positions3D)}
};

}

Trade::MeshData crosshair2D() {
return Trade::MeshData{MeshPrimitive::Lines, {}, Positions2D,
Trade::meshAttributeDataNonOwningArray(Attributes2D)};
}

Trade::MeshData3D crosshair3D() {
return Trade::MeshData3D{MeshPrimitive::Lines, {}, {{
{-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}
}}, {}, {}, {}, nullptr};
Trade::MeshData crosshair3D() {
return Trade::MeshData{MeshPrimitive::Lines, {}, Positions3D,
Trade::meshAttributeDataNonOwningArray(Attributes3D)};
}

}}
4 changes: 2 additions & 2 deletions src/Magnum/Primitives/Crosshair.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Magnum { namespace Primitives {
@see @ref crosshair3D(), @ref axis2D(), @ref line2D()
*/
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData2D crosshair2D();
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData crosshair2D();

/**
@brief 3D crosshair
Expand All @@ -54,7 +54,7 @@ MAGNUM_PRIMITIVES_EXPORT Trade::MeshData2D crosshair2D();
@see @ref crosshair2D(), @ref axis2D(), @ref line3D()
*/
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData3D crosshair3D();
MAGNUM_PRIMITIVES_EXPORT Trade::MeshData crosshair3D();

}}

Expand Down

0 comments on commit 2813e49

Please sign in to comment.