From e62afc1b6ce3d3467e18c55f333ea34c760f736e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 25 Jul 2013 12:08:43 +0200 Subject: [PATCH] Trade: ability to have either color or texture in PhongMaterialData. --- src/Trade/PhongMaterialData.cpp | 30 ++++++- src/Trade/PhongMaterialData.h | 137 ++++++++++++++++++++++++++++---- 2 files changed, 152 insertions(+), 15 deletions(-) diff --git a/src/Trade/PhongMaterialData.cpp b/src/Trade/PhongMaterialData.cpp index 339ced677..04c3bd749 100644 --- a/src/Trade/PhongMaterialData.cpp +++ b/src/Trade/PhongMaterialData.cpp @@ -26,6 +26,34 @@ namespace Magnum { namespace Trade { -PhongMaterialData::PhongMaterialData(const Vector3& ambientColor, const Vector3& diffuseColor, const Vector3& specularColor, Float shininess): AbstractMaterialData(Type::Phong), _ambientColor(ambientColor), _diffuseColor(diffuseColor), _specularColor(specularColor), _shininess(shininess) {} +Vector3& PhongMaterialData::ambientColor() { + CORRADE_ASSERT(!(_flags & Flag::AmbientTexture), "Trade::PhongMaterialData::ambientColor(): the material has ambient texture", _ambient.color); + return _ambient.color; +} + +UnsignedInt& PhongMaterialData::ambientTexture() { + CORRADE_ASSERT(_flags & Flag::AmbientTexture, "Trade::PhongMaterialData::ambientTexture(): the material doesn't have ambient texture", _ambient.texture); + return _ambient.texture; +} + +Vector3& PhongMaterialData::diffuseColor() { + CORRADE_ASSERT(!(_flags & Flag::DiffuseTexture), "Trade::PhongMaterialData::diffuseColor(): the material has diffuse texture", _diffuse.color); + return _diffuse.color; +} + +UnsignedInt& PhongMaterialData::diffuseTexture() { + CORRADE_ASSERT(_flags & Flag::DiffuseTexture, "Trade::PhongMaterialData::diffuseTexture(): the material doesn't have diffuse texture", _diffuse.texture); + return _diffuse.texture; +} + +Vector3& PhongMaterialData::specularColor() { + CORRADE_ASSERT(!(_flags & Flag::SpecularTexture), "Trade::PhongMaterialData::specularColor(): the material has specular texture", _specular.color); + return _specular.color; +} + +UnsignedInt& PhongMaterialData::specularTexture() { + CORRADE_ASSERT(_flags & Flag::SpecularTexture, "Trade::PhongMaterialData::specularTexture(): the material doesn't have specular texture", _specular.texture); + return _specular.texture; +} }} diff --git a/src/Trade/PhongMaterialData.h b/src/Trade/PhongMaterialData.h index d648c51ee..92ef09a49 100644 --- a/src/Trade/PhongMaterialData.h +++ b/src/Trade/PhongMaterialData.h @@ -39,34 +39,143 @@ namespace Magnum { namespace Trade { */ class MAGNUM_EXPORT PhongMaterialData: public AbstractMaterialData { public: + enum: UnsignedInt { + AmbientTextureID = 0, /**< Ambient texture ID for mapping with texture coordinates */ + DiffuseTextureID = 1, /**< Diffuse texture ID for mapping with texture coordinates */ + SpecularTextureID = 3 /**< Specular texture ID for mapping with texture coordinates */ + }; + + /** + * @brief Material flag + * + * @see @ref Flags, @ref flags() + */ + enum class Flag: UnsignedByte { + AmbientTexture = 1 << 0, /**< The material has ambient texture instead of color */ + DiffuseTexture = 1 << 1, /**< The material has diffuse texture instead of color */ + SpecularTexture = 1 << 2 /**< The material has specular texture instead of color */ + }; + + /** + * @brief Material flags + * + * @see @ref flags() + */ + typedef Containers::EnumSet Flags; + /** * @brief Constructor - * @param ambientColor Ambient color - * @param diffuseColor Diffuse color - * @param specularColor Specular color - * @param shininess Shininess + * @param flags Material flags + * @param shininess Shininess + * + * Colors and textures should be specified using member functions based + * on what flags are set. */ - explicit PhongMaterialData(const Vector3& ambientColor, const Vector3& diffuseColor, const Vector3& specularColor, Float shininess); + explicit PhongMaterialData(Flags flags, Float shininess): AbstractMaterialData(Type::Phong), _shininess(shininess), _flags(flags) {} - /** @brief Ambient color */ - Vector3 ambientColor() const { return _ambientColor; } + /** @brief Material flags */ + Flags flags() const { return _flags; } - /** @brief Diffuse color */ - Vector3 diffuseColor() const { return _diffuseColor; } + /** + * @brief Ambient color + * + * Available only if the material doesn't have @ref Flag "Flag::AmbientTexture". + * @see @ref flags() + */ + Vector3& ambientColor(); + Vector3 ambientColor() const; /**< @overload */ - /** @brief Specular color */ - Vector3 specularColor() const { return _specularColor; } + /** + * @brief Ambient texture ID + * + * Available only if the material has @ref Flag "Flag::AmbientTexture". + * @see @ref flags(), @ref AbstractImporter::texture() + */ + UnsignedInt& ambientTexture(); + UnsignedInt ambientTexture() const; /**< @overload */ + + /** + * @brief Diffuse color + * + * Available only if the material doesn't have @ref Flag "Flag::DiffuseTexture". + * @see @ref flags() + */ + Vector3& diffuseColor(); + Vector3 diffuseColor() const; /**< @overload */ + + /** + * @brief Diffuse texture ID + * + * Available only if the material has @ref Flag "Flag::DiffuseTexture". + * @see @ref flags(), @ref AbstractImporter::texture() + */ + UnsignedInt& diffuseTexture(); + UnsignedInt diffuseTexture() const; /**< @overload */ + + /** + * @brief Specular color + * + * Available only if the material doesn't have @ref Flag "Flag::SpecularTexture". + * @see @ref flags() + */ + Vector3& specularColor(); + Vector3 specularColor() const; /**< @overload */ + + /** + * @brief Specular texture ID + * + * Available only if the material has @ref Flag "Flag::SpecularTexture". + * @see @ref flags(), @ref AbstractImporter::texture() + */ + UnsignedInt& specularTexture(); + UnsignedInt specularTexture() const; /**< @overload */ /** @brief Shininess */ Float shininess() const { return _shininess; } private: - Vector3 _ambientColor, - _diffuseColor, - _specularColor; + union Source { + Source() {} + + Vector3 color; + UnsignedInt texture; + }; + + Source _ambient, + _diffuse, + _specular; Float _shininess; + Flags _flags; }; +CORRADE_ENUMSET_OPERATORS(PhongMaterialData::Flags) + +/* Ugly as hell. */ + +inline Vector3 PhongMaterialData::ambientColor() const { + return const_cast(this)->ambientColor(); +} + +inline UnsignedInt PhongMaterialData::ambientTexture() const { + return const_cast(this)->ambientTexture(); +} + +inline Vector3 PhongMaterialData::diffuseColor() const { + return const_cast(this)->diffuseColor(); +} + +inline UnsignedInt PhongMaterialData::diffuseTexture() const { + return const_cast(this)->diffuseTexture(); +} + +inline Vector3 PhongMaterialData::specularColor() const { + return const_cast(this)->specularColor(); +} + +inline UnsignedInt PhongMaterialData::specularTexture() const { + return const_cast(this)->specularTexture(); +} + }} #endif