Browse Source

Trade: add a MaterialTextureSwizzle enum.

pull/459/head
Vladimír Vondruš 6 years ago
parent
commit
d3b346eda5
  1. 3
      src/Magnum/MeshTools/sceneconverter.cpp
  2. 11
      src/Magnum/Trade/MaterialData.cpp
  3. 58
      src/Magnum/Trade/MaterialData.h
  4. 50
      src/Magnum/Trade/Test/MaterialDataTest.cpp
  5. 1
      src/Magnum/Trade/Trade.h

3
src/Magnum/MeshTools/sceneconverter.cpp

@ -499,6 +499,9 @@ save its output; if no --converter is specified, AnySceneConverter is used.)")
case Trade::MaterialAttributeType::String: case Trade::MaterialAttributeType::String:
d << info.data.attribute<Containers::StringView>(i, j); d << info.data.attribute<Containers::StringView>(i, j);
break; break;
case Trade::MaterialAttributeType::TextureSwizzle:
d << info.data.attribute<Trade::MaterialTextureSwizzle>(i, j);
break;
} }
} }
} }

11
src/Magnum/Trade/MaterialData.cpp

@ -69,6 +69,7 @@ std::size_t materialAttributeTypeSize(const MaterialAttributeType type) {
case MaterialAttributeType::Rad: case MaterialAttributeType::Rad:
case MaterialAttributeType::UnsignedInt: case MaterialAttributeType::UnsignedInt:
case MaterialAttributeType::Int: case MaterialAttributeType::Int:
case MaterialAttributeType::TextureSwizzle:
return 4; return 4;
case MaterialAttributeType::UnsignedLong: case MaterialAttributeType::UnsignedLong:
@ -590,6 +591,15 @@ Debug& operator<<(Debug& debug, const MaterialAttribute value) {
return debug << "::" << Debug::nospace << string; return debug << "::" << Debug::nospace << string;
} }
Debug& operator<<(Debug& debug, const MaterialTextureSwizzle value) {
/* The swizzle is encoded as a fourCC, so just print the numerical value as
a char. Worst case this will print nothing or four garbage letters.
Sorry in that case. GCC 4.8 doesn't understand just {}, wants to have
a full MaterialTextureSwizzle{} here. */
MaterialTextureSwizzle values[]{value, MaterialTextureSwizzle{}};
return debug << "Trade::MaterialTextureSwizzle::" << Debug::nospace << reinterpret_cast<const char*>(values);
}
Debug& operator<<(Debug& debug, const MaterialAttributeType value) { Debug& operator<<(Debug& debug, const MaterialAttributeType value) {
debug << "Trade::MaterialAttributeType" << Debug::nospace; debug << "Trade::MaterialAttributeType" << Debug::nospace;
@ -624,6 +634,7 @@ Debug& operator<<(Debug& debug, const MaterialAttributeType value) {
_c(Pointer) _c(Pointer)
_c(MutablePointer) _c(MutablePointer)
_c(String) _c(String)
_c(TextureSwizzle)
#undef _c #undef _c
/* LCOV_EXCL_STOP */ /* LCOV_EXCL_STOP */
} }

58
src/Magnum/Trade/MaterialData.h

@ -26,7 +26,7 @@
*/ */
/** @file /** @file
* @brief Class @ref Magnum::Trade::MaterialData, @ref Magnum::Trade::MaterialAttributeData, enum @ref Magnum::Trade::MaterialAttribute, @ref Magnum::Trade::MaterialAttributeType * @brief Class @ref Magnum::Trade::MaterialData, @ref Magnum::Trade::MaterialAttributeData, enum @ref Magnum::Trade::MaterialAttribute, @ref Magnum::Trade::MaterialTextureSwizzle, @ref Magnum::Trade::MaterialAttributeType
* @m_since_latest * @m_since_latest
*/ */
@ -34,6 +34,7 @@
#include <Corrade/Containers/EnumSet.h> #include <Corrade/Containers/EnumSet.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/StringView.h> #include <Corrade/Containers/StringView.h>
#include <Corrade/Utility/Endianness.h>
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/Math/RectangularMatrix.h" #include "Magnum/Math/RectangularMatrix.h"
@ -287,6 +288,50 @@ enum class MaterialAttribute: UnsignedInt {
*/ */
MAGNUM_TRADE_EXPORT Debug& operator<<(Debug& debug, MaterialAttribute value); MAGNUM_TRADE_EXPORT Debug& operator<<(Debug& debug, MaterialAttribute value);
/**
@brief Material texture swizzle
@m_since_latest
See @ref MaterialData for more information.
*/
enum class MaterialTextureSwizzle: UnsignedInt {
/** Red component */
R = Utility::Endianness::fourCC('R', '\0', '\0', '\0'),
/** Green component */
G = Utility::Endianness::fourCC('G', '\0', '\0', '\0'),
/** Blue component */
B = Utility::Endianness::fourCC('B', '\0', '\0', '\0'),
/** Alpha component */
A = Utility::Endianness::fourCC('A', '\0', '\0', '\0'),
/** Red and green component */
RG = Utility::Endianness::fourCC('R', 'G', '\0', '\0'),
/** Green and blue component */
GB = Utility::Endianness::fourCC('G', 'B', '\0', '\0'),
/** Blue and alpha component */
BA = Utility::Endianness::fourCC('B', 'A', '\0', '\0'),
/** RGB components */
RGB = Utility::Endianness::fourCC('R', 'G', 'B', '\0'),
/** GBA components */
GBA = Utility::Endianness::fourCC('G', 'B', 'A', '\0'),
/** RGBA components */
RGBA = Utility::Endianness::fourCC('R', 'G', 'B', 'A'),
};
/**
@debugoperatorenum{MaterialTextureSwizzle}
@m_since_latest
*/
MAGNUM_TRADE_EXPORT Debug& operator<<(Debug& debug, MaterialTextureSwizzle value);
/** /**
@brief Material attribute type @brief Material attribute type
@m_since_latest @m_since_latest
@ -352,7 +397,10 @@ enum class MaterialAttributeType: UnsignedByte {
* @ref Corrade::Containers::StringView, retrieval has to be done using * @ref Corrade::Containers::StringView, retrieval has to be done using
* @ref Corrade::Containers::StringView. * @ref Corrade::Containers::StringView.
*/ */
String String,
/** One of the values from @ref MaterialTextureSwizzle */
TextureSwizzle
}; };
/** /**
@ -575,6 +623,7 @@ class MAGNUM_TRADE_EXPORT MaterialAttributeData {
constexpr explicit ErasedScalar(Rad value): f{Float(value)} {} constexpr explicit ErasedScalar(Rad value): f{Float(value)} {}
constexpr explicit ErasedScalar(UnsignedInt value): u{value} {} constexpr explicit ErasedScalar(UnsignedInt value): u{value} {}
constexpr explicit ErasedScalar(Int value): i{value} {} constexpr explicit ErasedScalar(Int value): i{value} {}
constexpr explicit ErasedScalar(MaterialTextureSwizzle value): u{UnsignedInt(value)} {}
Float f; Float f;
UnsignedInt u; UnsignedInt u;
@ -1566,6 +1615,11 @@ namespace Implementation {
}; };
/* No specialization for StringView as this type trait should not be used /* No specialization for StringView as this type trait should not be used
in that case */ in that case */
template<> struct MaterialAttributeTypeFor<MaterialTextureSwizzle> {
constexpr static MaterialAttributeType type() {
return MaterialAttributeType::TextureSwizzle;
}
};
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
#define _c(type_) template<> struct MaterialAttributeTypeFor<type_> { \ #define _c(type_) template<> struct MaterialAttributeTypeFor<type_> { \
constexpr static MaterialAttributeType type() { \ constexpr static MaterialAttributeType type() { \

50
src/Magnum/Trade/Test/MaterialDataTest.cpp

@ -59,6 +59,7 @@ class MaterialDataTest: public TestSuite::Tester {
void constructAttributeMutablePointer(); void constructAttributeMutablePointer();
void constructAttributeStringNameStringValue(); void constructAttributeStringNameStringValue();
void constructAttributeNameStringValue(); void constructAttributeNameStringValue();
void constructAttributeTextureSwizzle();
void constructAttributeInvalidName(); void constructAttributeInvalidName();
void constructAttributeWrongTypeForName(); void constructAttributeWrongTypeForName();
@ -93,6 +94,7 @@ class MaterialDataTest: public TestSuite::Tester {
void access(); void access();
void accessPointer(); void accessPointer();
void accessString(); void accessString();
void accessTextureSwizzle();
void accessOptional(); void accessOptional();
void accessOutOfBounds(); void accessOutOfBounds();
void accessNotFound(); void accessNotFound();
@ -133,6 +135,7 @@ class MaterialDataTest: public TestSuite::Tester {
void phongAccessInvalidTextures(); void phongAccessInvalidTextures();
void debugAttribute(); void debugAttribute();
void debugTextureSwizzle();
void debugAttributeType(); void debugAttributeType();
void debugType(); void debugType();
@ -190,6 +193,7 @@ MaterialDataTest::MaterialDataTest() {
&MaterialDataTest::constructAttributeMutablePointer, &MaterialDataTest::constructAttributeMutablePointer,
&MaterialDataTest::constructAttributeStringNameStringValue, &MaterialDataTest::constructAttributeStringNameStringValue,
&MaterialDataTest::constructAttributeNameStringValue, &MaterialDataTest::constructAttributeNameStringValue,
&MaterialDataTest::constructAttributeTextureSwizzle,
&MaterialDataTest::constructAttributeInvalidName, &MaterialDataTest::constructAttributeInvalidName,
&MaterialDataTest::constructAttributeWrongTypeForName, &MaterialDataTest::constructAttributeWrongTypeForName,
@ -227,6 +231,7 @@ MaterialDataTest::MaterialDataTest() {
&MaterialDataTest::access, &MaterialDataTest::access,
&MaterialDataTest::accessPointer, &MaterialDataTest::accessPointer,
&MaterialDataTest::accessString, &MaterialDataTest::accessString,
&MaterialDataTest::accessTextureSwizzle,
&MaterialDataTest::accessOptional, &MaterialDataTest::accessOptional,
&MaterialDataTest::accessOutOfBounds, &MaterialDataTest::accessOutOfBounds,
&MaterialDataTest::accessNotFound, &MaterialDataTest::accessNotFound,
@ -267,6 +272,7 @@ MaterialDataTest::MaterialDataTest() {
&MaterialDataTest::phongAccessInvalidTextures, &MaterialDataTest::phongAccessInvalidTextures,
&MaterialDataTest::debugAttribute, &MaterialDataTest::debugAttribute,
&MaterialDataTest::debugTextureSwizzle,
&MaterialDataTest::debugAttributeType, &MaterialDataTest::debugAttributeType,
&MaterialDataTest::debugType, &MaterialDataTest::debugType,
@ -560,6 +566,27 @@ void MaterialDataTest::constructAttributeNameStringValue() {
CORRADE_COMPARE(typeErased.value<Containers::StringView>()[typeErased.value<Containers::StringView>().size()], '\0'); CORRADE_COMPARE(typeErased.value<Containers::StringView>()[typeErased.value<Containers::StringView>().size()], '\0');
} }
void MaterialDataTest::constructAttributeTextureSwizzle() {
MaterialAttributeData attribute{"swizzle", MaterialTextureSwizzle::GBA};
CORRADE_COMPARE(attribute.name(), "swizzle");
CORRADE_COMPARE(attribute.type(), MaterialAttributeType::TextureSwizzle);
CORRADE_COMPARE(*static_cast<const MaterialTextureSwizzle*>(attribute.value()), MaterialTextureSwizzle::GBA);
CORRADE_COMPARE(attribute.value<MaterialTextureSwizzle>(), MaterialTextureSwizzle::GBA);
constexpr MaterialAttributeData cattribute{"swizzle"_s, MaterialTextureSwizzle::GBA};
CORRADE_COMPARE(cattribute.name(), "swizzle");
CORRADE_COMPARE(cattribute.type(), MaterialAttributeType::TextureSwizzle);
CORRADE_COMPARE(*static_cast<const MaterialTextureSwizzle*>(cattribute.value()), MaterialTextureSwizzle::GBA);
CORRADE_COMPARE(cattribute.value<MaterialTextureSwizzle>(), MaterialTextureSwizzle::GBA);
/* Type-erased variant */
const MaterialTextureSwizzle swizzle = MaterialTextureSwizzle::GBA;
MaterialAttributeData typeErased{"swizzle", MaterialAttributeType::TextureSwizzle, &swizzle};
CORRADE_COMPARE(typeErased.name(), "swizzle");
CORRADE_COMPARE(typeErased.type(), MaterialAttributeType::TextureSwizzle);
CORRADE_COMPARE(typeErased.value<MaterialTextureSwizzle>(), MaterialTextureSwizzle::GBA);
}
void MaterialDataTest::constructAttributeInvalidName() { void MaterialDataTest::constructAttributeInvalidName() {
#ifdef CORRADE_NO_ASSERT #ifdef CORRADE_NO_ASSERT
CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions"); CORRADE_SKIP("CORRADE_NO_ASSERT defined, can't test assertions");
@ -1323,6 +1350,19 @@ void MaterialDataTest::accessString() {
CORRADE_COMPARE(data.attribute<Containers::StringView>(0)[data.attribute<Containers::StringView>(0).size()], '\0'); CORRADE_COMPARE(data.attribute<Containers::StringView>(0)[data.attribute<Containers::StringView>(0).size()], '\0');
} }
void MaterialDataTest::accessTextureSwizzle() {
MaterialData data{{}, {
{"normalSwizzle", MaterialTextureSwizzle::BA}
}};
CORRADE_COMPARE(data.attributeType("normalSwizzle"), MaterialAttributeType::TextureSwizzle);
/* Pointer access will stop at the first null byte, printing the string
value */
CORRADE_COMPARE(static_cast<const char*>(data.attribute(0)), "BA"_s);
CORRADE_COMPARE(*static_cast<const MaterialTextureSwizzle*>(data.attribute(0)), MaterialTextureSwizzle::BA);
CORRADE_COMPARE(data.attribute<MaterialTextureSwizzle>(0), MaterialTextureSwizzle::BA);
}
void MaterialDataTest::accessOptional() { void MaterialDataTest::accessOptional() {
MaterialData data{{}, { MaterialData data{{}, {
{MaterialAttribute::AlphaMask, 0.5f}, {MaterialAttribute::AlphaMask, 0.5f},
@ -2221,6 +2261,16 @@ void MaterialDataTest::debugAttribute() {
CORRADE_COMPARE(out.str(), "Trade::MaterialAttribute::DiffuseTextureCoordinates Trade::MaterialAttribute::LayerName Trade::MaterialAttribute(0xfefe) Trade::MaterialAttribute(0x0)\n"); CORRADE_COMPARE(out.str(), "Trade::MaterialAttribute::DiffuseTextureCoordinates Trade::MaterialAttribute::LayerName Trade::MaterialAttribute(0xfefe) Trade::MaterialAttribute(0x0)\n");
} }
void MaterialDataTest::debugTextureSwizzle() {
std::ostringstream out;
/* The swizzle is encoded as a fourCC, so it just prints the numerical
value as a char. Worst case this will print nothing or four garbage
letters. Sorry in that case. */
Debug{&out} << MaterialTextureSwizzle::BA << MaterialTextureSwizzle{};
CORRADE_COMPARE(out.str(), "Trade::MaterialTextureSwizzle::BA Trade::MaterialTextureSwizzle::\n");
}
void MaterialDataTest::debugAttributeType() { void MaterialDataTest::debugAttributeType() {
std::ostringstream out; std::ostringstream out;

1
src/Magnum/Trade/Trade.h

@ -49,6 +49,7 @@ typedef CORRADE_DEPRECATED("use InputFileCallbackPolicy instead") InputFileCallb
#endif #endif
enum class MaterialAttribute: UnsignedInt; enum class MaterialAttribute: UnsignedInt;
enum class MaterialTextureSwizzle: UnsignedInt;
enum class MaterialAttributeType: UnsignedByte; enum class MaterialAttributeType: UnsignedByte;
enum class MaterialType: UnsignedInt; enum class MaterialType: UnsignedInt;
enum class MaterialAlphaMode: UnsignedByte; enum class MaterialAlphaMode: UnsignedByte;

Loading…
Cancel
Save