123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- #include "assimp.h"
- #include "aiMaterial.h"
- #include "assimp.hpp"
- #include "MaterialSystem.h"
- #include "../include/aiAssert.h"
- using namespace Assimp;
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
- const char* pKey,
- const aiMaterialProperty** pPropOut)
- {
- #if (defined DEBUG)
- ai_assert (pMat != NULL);
- ai_assert (pKey != NULL);
- ai_assert (pPropOut != NULL);
- #endif // ASSIMP_DEBUG
- for (unsigned int i = 0; i < pMat->mNumProperties;++i)
- {
- if (NULL != pMat->mProperties[i])
- {
- if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
- {
- *pPropOut = pMat->mProperties[i];
- return AI_SUCCESS;
- }
- }
- }
- *pPropOut = NULL;
- return AI_FAILURE;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
- const char* pKey,
- float* pOut,
- unsigned int* pMax)
- {
- #if (defined DEBUG)
- ai_assert (pMat != NULL);
- ai_assert (pKey != NULL);
- ai_assert (pOut != NULL);
- #endif // ASSIMP_DEBUG
- for (unsigned int i = 0; i < pMat->mNumProperties;++i)
- {
- if (NULL != pMat->mProperties[i])
- {
- if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
- {
- // data is given in floats, simply copy it
- if( aiPTI_Float == pMat->mProperties[i]->mType ||
- aiPTI_Buffer == pMat->mProperties[i]->mType)
- {
- unsigned int iWrite = pMat->mProperties[i]->
- mDataLength / sizeof(float);
- if (NULL != pMax)
- iWrite = *pMax < iWrite ? *pMax : iWrite;
- memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (float));
-
- if (NULL != pMax)
- *pMax = iWrite;
- }
- // data is given in ints, convert to float
- else if( aiPTI_Integer == pMat->mProperties[i]->mType)
- {
- unsigned int iWrite = pMat->mProperties[i]->
- mDataLength / sizeof(int);
- if (NULL != pMax)
- iWrite = *pMax < iWrite ? *pMax : iWrite;
- for (unsigned int a = 0; a < iWrite;++a)
- {
- pOut[a] = (float) ((int*)pMat->mProperties[i]->mData)[a];
- }
- if (NULL != pMax)
- *pMax = iWrite;
- }
- // it is a string ... no way to read something out of this
- else
- {
- if (NULL != pMax)
- *pMax = 0;
- return AI_FAILURE;
- }
- return AI_SUCCESS;
- }
- }
- }
- return AI_FAILURE;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
- const char* pKey,
- int* pOut,
- unsigned int* pMax)
- {
- #if (defined DEBUG)
- ai_assert (pMat != NULL);
- ai_assert (pKey != NULL);
- ai_assert (pOut != NULL);
- #endif // ASSIMP_DEBUG
- for (unsigned int i = 0; i < pMat->mNumProperties;++i)
- {
- if (NULL != pMat->mProperties[i])
- {
- if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
- {
- // data is given in ints, simply copy it
- if( aiPTI_Integer == pMat->mProperties[i]->mType ||
- aiPTI_Buffer == pMat->mProperties[i]->mType)
- {
- unsigned int iWrite = pMat->mProperties[i]->
- mDataLength / sizeof(int);
- if (NULL != pMax)
- iWrite = *pMax < iWrite ? *pMax : iWrite;
- memcpy (pOut, pMat->mProperties[i]->mData, iWrite * sizeof (int));
-
- if (NULL != pMax)
- *pMax = iWrite;
- }
- // data is given in floats convert to int (lossy!)
- else if( aiPTI_Float == pMat->mProperties[i]->mType)
- {
- unsigned int iWrite = pMat->mProperties[i]->
- mDataLength / sizeof(float);
- if (NULL != pMax)
- iWrite = *pMax < iWrite ? *pMax : iWrite;
- for (unsigned int a = 0; a < iWrite;++a)
- {
- pOut[a] = (int) ((float*)pMat->mProperties[i]->mData)[a];
- }
- if (NULL != pMax)
- *pMax = iWrite;
- }
- // it is a string ... no way to read something out of this
- else
- {
- if (NULL != pMax)
- *pMax = 0;
- return AI_FAILURE;
- }
- return AI_SUCCESS;
- }
- }
- }
- return AI_FAILURE;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn aiGetMaterialColor(const aiMaterial* pMat,
- const char* pKey,
- aiColor4D* pOut)
- {
- unsigned int iMax = 4;
- aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,(float*)pOut,&iMax);
- // if no alpha channel is provided set it to 1.0 by default
- if (3 == iMax)pOut->a = 1.0f;
- return eRet;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn aiGetMaterialString(const aiMaterial* pMat,
- const char* pKey,
- aiString* pOut)
- {
- #if (defined DEBUG)
- ai_assert (pMat != NULL);
- ai_assert (pKey != NULL);
- ai_assert (pOut != NULL);
- #endif // ASSIMP_DEBUG
- for (unsigned int i = 0; i < pMat->mNumProperties;++i)
- {
- if (NULL != pMat->mProperties[i])
- {
- if (0 == ASSIMP_stricmp( pMat->mProperties[i]->mKey->data, pKey ))
- {
- if( aiPTI_String == pMat->mProperties[i]->mType)
- {
- memcpy (pOut, pMat->mProperties[i]->mData,
- sizeof(aiString));
- }
- // wrong type
- else return AI_FAILURE;
- return AI_SUCCESS;
- }
- }
- }
- return AI_FAILURE;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn MaterialHelper::AddBinaryProperty (const void* pInput,
- const unsigned int pSizeInBytes,
- const char* pKey,
- aiPropertyTypeInfo pType)
- {
- #if (defined DEBUG)
- ai_assert (pInput != NULL);
- ai_assert (pKey != NULL);
- ai_assert (0 != pSizeInBytes);
- #endif // ASSIMP_DEBUG
- aiMaterialProperty* pcNew = new aiMaterialProperty();
- // fill this
- pcNew->mKey = new aiString();
- pcNew->mType = pType;
- pcNew->mDataLength = pSizeInBytes;
- pcNew->mData = new char[pSizeInBytes];
- memcpy (pcNew->mData,pInput,pSizeInBytes);
- pcNew->mKey->length = strlen(pKey);
- ai_assert ( MAXLEN > pcNew->mKey->length);
- strcpy( pcNew->mKey->data, pKey );
- // resize the array ... allocate
- // storage for 5 other properties
- if (this->mNumProperties == this->mNumAllocated)
- {
- unsigned int iOld = this->mNumAllocated;
- this->mNumAllocated += 5;
- aiMaterialProperty** ppTemp = new aiMaterialProperty*[this->mNumAllocated];
- if (NULL == ppTemp)return AI_OUTOFMEMORY;
- memcpy (ppTemp,this->mProperties,iOld * sizeof(void*));
- delete[] this->mProperties;
- this->mProperties = ppTemp;
- }
- // push back ...
- this->mProperties[this->mNumProperties++] = pcNew;
- return AI_SUCCESS;
- }
- // ---------------------------------------------------------------------------
- // ---------------------------------------------------------------------------
- aiReturn MaterialHelper::AddProperty (const aiString* pInput,
- const char* pKey)
- {
- return this->AddBinaryProperty(pInput,
- sizeof(aiString),pKey,aiPTI_String);
- }
|