diff --git a/include/assimp/material.inl b/include/assimp/material.inl index a10ba4de3..0b30a6839 100644 --- a/include/assimp/material.inl +++ b/include/assimp/material.inl @@ -130,7 +130,54 @@ AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type, } // --------------------------------------------------------------------------- -AI_FORCE_INLINE aiReturn aiMaterial::Get(const char* pKey,unsigned int type, +// Specialisation for a single bool. +// Casts floating point and integer to bool +template <> +AI_FORCE_INLINE + aiReturn + aiMaterial::Get(const char *pKey, unsigned int type, + unsigned int idx, bool &pOut) const { + const aiMaterialProperty *prop; + const aiReturn ret = ::aiGetMaterialProperty(this, pKey, type, idx, + (const aiMaterialProperty **)&prop); + if (AI_SUCCESS == ret) { + + switch (prop->mType) { + // Type cannot be converted + default: return AI_FAILURE; + + case aiPTI_Buffer: { + // Native bool value storage + if (prop->mDataLength < sizeof(bool)) { + return AI_FAILURE; + } + ::memcpy(&pOut, prop->mData, sizeof(bool)); + } break; + + case aiPTI_Float: + case aiPTI_Double: { + // Read as float and cast to bool + float value = 0.0f; + if (AI_SUCCESS == ::aiGetMaterialFloat(this, pKey, type, idx, &value)) { + pOut = static_cast(value); + return AI_SUCCESS; + } + return AI_FAILURE; + } + case aiPTI_Integer: { + // Cast to bool + const int value = static_cast(*prop->mData); + pOut = static_cast(value); + return AI_SUCCESS; + } + } + } + return ret; +} + +// --------------------------------------------------------------------------- +AI_FORCE_INLINE +aiReturn aiMaterial::Get(const char* pKey,unsigned int type, unsigned int idx,ai_real* pOut, unsigned int* pMax) const { return ::aiGetMaterialFloatArray(this,pKey,type,idx,pOut,pMax); diff --git a/include/assimp/pbrmaterial.h b/include/assimp/pbrmaterial.h index 06932ced2..3bdf151a3 100644 --- a/include/assimp/pbrmaterial.h +++ b/include/assimp/pbrmaterial.h @@ -61,7 +61,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #define AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR AI_MATKEY_METALLIC_FACTOR #define AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR AI_MATKEY_ROUGHNESS_FACTOR -//#define AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS "$mat.gltf.pbrSpecularGlossiness", 0, 0 +#define AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS AI_MATKEY_GLOSSINESS_FACTOR #define AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR AI_MATKEY_GLOSSINESS_FACTOR // Use AI_MATKEY_SHADING_MODEL == aiShadingMode_Unlit instead diff --git a/test/unit/utMaterialSystem.cpp b/test/unit/utMaterialSystem.cpp index 7b4560443..39ef60679 100644 --- a/test/unit/utMaterialSystem.cpp +++ b/test/unit/utMaterialSystem.cpp @@ -125,13 +125,94 @@ TEST_F(MaterialSystemTest, testStringProperty) { } // ------------------------------------------------------------------------------------------------ -TEST_F(MaterialSystemTest, testMaterialNameAccess) { - aiMaterial *mat = new aiMaterial(); - EXPECT_NE(nullptr, mat); - - aiString name = mat->GetName(); +TEST_F(MaterialSystemTest, testDefaultMaterialName) { + aiString name = pcMat->GetName(); const int retValue(strncmp(name.C_Str(), AI_DEFAULT_MATERIAL_NAME, name.length)); EXPECT_EQ(0, retValue); - - delete mat; +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testBoolProperty) { + const bool valTrue = true; + const bool valFalse = false; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&valTrue, 1, "bool_true")); + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&valFalse, 1, "bool_false")); + + bool read = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("bool_true", 0, 0, read)); + EXPECT_TRUE(read) << "read true bool"; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("bool_false", 0, 0, read)); + EXPECT_FALSE(read) << "read false bool"; +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testCastIntProperty) { + int value = 10; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "integer")); + value = 0; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero")); + value = -1; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "negative")); + + // To float + float valFloat = 0.0f; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("integer", 0, 0, valFloat)); + EXPECT_EQ(10.0f, valFloat); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valFloat)); + EXPECT_EQ(0.0f, valFloat); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("negative", 0, 0, valFloat)); + EXPECT_EQ(-1.0f, valFloat); + + // To bool + bool valBool = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("integer", 0, 0, valBool)); + EXPECT_EQ(true, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool)); + EXPECT_EQ(false, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("negative", 0, 0, valBool)); + EXPECT_EQ(true, valBool); +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testCastFloatProperty) { + float value = 150392.63f; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "float")); + value = 0; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero")); + + // To int + int valInt = 0.0f; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valInt)); + EXPECT_EQ(150392, valInt); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valInt)); + EXPECT_EQ(0, valInt); + + // To bool + bool valBool = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valBool)); + EXPECT_EQ(true, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool)); + EXPECT_EQ(false, valBool); +} + +// ------------------------------------------------------------------------------------------------ +TEST_F(MaterialSystemTest, testCastSmallFloatProperty) { + float value = 0.0078125f; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "float")); + value = 0; + EXPECT_EQ(AI_SUCCESS, pcMat->AddProperty(&value, 1, "zero")); + + // To int + int valInt = 0.0f; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valInt)); + EXPECT_EQ(0, valInt); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valInt)); + EXPECT_EQ(0, valInt); + + // To bool + bool valBool = false; + EXPECT_EQ(AI_SUCCESS, pcMat->Get("float", 0, 0, valBool)); + EXPECT_EQ(true, valBool); + EXPECT_EQ(AI_SUCCESS, pcMat->Get("zero", 0, 0, valBool)); + EXPECT_EQ(false, valBool); }