diff options
Diffstat (limited to 'src/3rdparty/assimp/code/XFileImporter.cpp')
-rw-r--r-- | src/3rdparty/assimp/code/XFileImporter.cpp | 79 |
1 files changed, 46 insertions, 33 deletions
diff --git a/src/3rdparty/assimp/code/XFileImporter.cpp b/src/3rdparty/assimp/code/XFileImporter.cpp index d444135aa..d100692d3 100644 --- a/src/3rdparty/assimp/code/XFileImporter.cpp +++ b/src/3rdparty/assimp/code/XFileImporter.cpp @@ -52,7 +52,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace Assimp; static const aiImporterDesc desc = { - "Collada Importer", + "Direct3D XFile Importer", "", "", "", @@ -110,10 +110,6 @@ void XFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, I if( fileSize < 16) throw DeadlyImportError( "XFile is too small."); - // need to clear members - this method might be called multiple - // times on a single XFileImporter instance. - mImportedMats.clear(); - // in the hope that binary files will never start with a BOM ... mBuffer.resize( fileSize + 1); file->Read( &mBuffer.front(), 1, fileSize); @@ -132,7 +128,7 @@ void XFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, I // ------------------------------------------------------------------------------------------------ // Constructs the return data structure out of the imported data. -void XFileImporter::CreateDataRepresentationFromImport( aiScene* pScene, const XFile::Scene* pData) +void XFileImporter::CreateDataRepresentationFromImport( aiScene* pScene, XFile::Scene* pData) { // Read the global materials first so that meshes referring to them can find them later ConvertMaterials( pScene, pData->mGlobalMaterials); @@ -233,8 +229,8 @@ void XFileImporter::CreateMeshes( aiScene* pScene, aiNode* pNode, const std::vec std::vector<aiMesh*> meshes; for( unsigned int a = 0; a < pMeshes.size(); a++) { - const XFile::Mesh* sourceMesh = pMeshes[a]; - // first convert its materials so that we can find them when searching by name afterwards + XFile::Mesh* sourceMesh = pMeshes[a]; + // first convert its materials so that we can find them with their index afterwards ConvertMaterials( pScene, sourceMesh->mMaterials); unsigned int numMaterials = std::max( (unsigned int)sourceMesh->mMaterials.size(), 1u); @@ -272,15 +268,11 @@ void XFileImporter::CreateMeshes( aiScene* pScene, aiNode* pNode, const std::vec aiMesh* mesh = new aiMesh; meshes.push_back( mesh); - // find the material by name in the scene's material list. Either own material - // or referenced material, it should already be found there + // find the material in the scene's material list. Either own material + // or referenced material, it should already have a valid index if( sourceMesh->mFaceMaterials.size() > 0) { - std::map<std::string, unsigned int>::const_iterator matIt = mImportedMats.find( sourceMesh->mMaterials[b].mName); - if( matIt == mImportedMats.end()) - mesh->mMaterialIndex = 0; - else - mesh->mMaterialIndex = matIt->second; + mesh->mMaterialIndex = sourceMesh->mMaterials[b].sceneIndex; } else { mesh->mMaterialIndex = 0; @@ -554,32 +546,52 @@ void XFileImporter::CreateAnimations( aiScene* pScene, const XFile::Scene* pData // ------------------------------------------------------------------------------------------------ // Converts all materials in the given array and stores them in the scene's material list. -void XFileImporter::ConvertMaterials( aiScene* pScene, const std::vector<XFile::Material>& pMaterials) +void XFileImporter::ConvertMaterials( aiScene* pScene, std::vector<XFile::Material>& pMaterials) { // count the non-referrer materials in the array - unsigned int numMaterials = 0; + unsigned int numNewMaterials = 0; for( unsigned int a = 0; a < pMaterials.size(); a++) if( !pMaterials[a].mIsReference) - numMaterials++; - - if( numMaterials == 0) - return; + numNewMaterials++; // resize the scene's material list to offer enough space for the new materials - aiMaterial** prevMats = pScene->mMaterials; - pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials + numMaterials]; - if( prevMats) - { - memcpy( pScene->mMaterials, prevMats, pScene->mNumMaterials * sizeof( aiMaterial*)); - delete [] prevMats; - } + if( numNewMaterials > 0 ) + { + aiMaterial** prevMats = pScene->mMaterials; + pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials + numNewMaterials]; + if( prevMats) + { + memcpy( pScene->mMaterials, prevMats, pScene->mNumMaterials * sizeof( aiMaterial*)); + delete [] prevMats; + } + } // convert all the materials given in the array for( unsigned int a = 0; a < pMaterials.size(); a++) { - const XFile::Material& oldMat = pMaterials[a]; + XFile::Material& oldMat = pMaterials[a]; if( oldMat.mIsReference) - continue; + { + // find the material it refers to by name, and store its index + for( size_t a = 0; a < pScene->mNumMaterials; ++a ) + { + aiString name; + pScene->mMaterials[a]->Get( AI_MATKEY_NAME, name); + if( strcmp( name.C_Str(), oldMat.mName.data()) == 0 ) + { + oldMat.sceneIndex = a; + break; + } + } + + if( oldMat.sceneIndex == SIZE_MAX ) + { + DefaultLogger::get()->warn( boost::str( boost::format( "Could not resolve global material reference \"%s\"") % oldMat.mName)); + oldMat.sceneIndex = 0; + } + + continue; + } aiMaterial* mat = new aiMaterial; aiString name; @@ -594,8 +606,9 @@ void XFileImporter::ConvertMaterials( aiScene* pScene, const std::vector<XFile:: mat->AddProperty<int>( &shadeMode, 1, AI_MATKEY_SHADING_MODEL); // material colours - // FIX: Setup this as ambient not as emissive color - mat->AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_AMBIENT); + // Unclear: there's no ambient colour, but emissive. What to put for ambient? + // Probably nothing at all, let the user select a suitable default. + mat->AddProperty( &oldMat.mEmissive, 1, AI_MATKEY_COLOR_EMISSIVE); mat->AddProperty( &oldMat.mDiffuse, 1, AI_MATKEY_COLOR_DIFFUSE); mat->AddProperty( &oldMat.mSpecular, 1, AI_MATKEY_COLOR_SPECULAR); mat->AddProperty( &oldMat.mSpecularExponent, 1, AI_MATKEY_SHININESS); @@ -677,7 +690,7 @@ void XFileImporter::ConvertMaterials( aiScene* pScene, const std::vector<XFile:: } pScene->mMaterials[pScene->mNumMaterials] = mat; - mImportedMats[oldMat.mName] = pScene->mNumMaterials; + oldMat.sceneIndex = pScene->mNumMaterials; pScene->mNumMaterials++; } } |