diff --git a/include/assimp/material.inl b/include/assimp/material.inl index 1aec6d865..b299892b0 100644 --- a/include/assimp/material.inl +++ b/include/assimp/material.inl @@ -163,16 +163,21 @@ AI_FORCE_INLINE } break; case aiPTI_Float: - case aiPTI_Double: - case aiPTI_Integer: { - // Read as integer and cast to bool - int value = 0; - if (AI_SUCCESS == ::aiGetMaterialInteger(this, pKey, type, idx, &value)) { + 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; diff --git a/test/unit/utMaterialSystem.cpp b/test/unit/utMaterialSystem.cpp index 9df64ae3b..363a4b193 100644 --- a/test/unit/utMaterialSystem.cpp +++ b/test/unit/utMaterialSystem.cpp @@ -151,6 +151,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) { 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; @@ -158,6 +160,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) { 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; @@ -165,6 +169,8 @@ TEST_F(MaterialSystemTest, testCastIntProperty) { 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); } // ------------------------------------------------------------------------------------------------ @@ -188,3 +194,25 @@ TEST_F(MaterialSystemTest, testCastFloatProperty) { 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); +}