pull/3958/head
Hill Ma 2021-06-15 13:20:12 -07:00
parent a9b29abee6
commit af748755e1
2 changed files with 18 additions and 8 deletions

View File

@ -41,6 +41,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include "EmbedTexturesProcess.h" #include "EmbedTexturesProcess.h"
#include <assimp/IOStream.hpp>
#include <assimp/IOSystem.hpp>
#include <assimp/ParsingUtils.h> #include <assimp/ParsingUtils.h>
#include "ProcessHelper.h" #include "ProcessHelper.h"
@ -62,6 +64,7 @@ bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const {
void EmbedTexturesProcess::SetupProperties(const Importer* pImp) { void EmbedTexturesProcess::SetupProperties(const Importer* pImp) {
mRootPath = pImp->GetPropertyString("sourceFilePath"); mRootPath = pImp->GetPropertyString("sourceFilePath");
mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u); mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u);
mIOHandler = pImp->GetIOHandler();
} }
void EmbedTexturesProcess::Execute(aiScene* pScene) { void EmbedTexturesProcess::Execute(aiScene* pScene) {
@ -101,27 +104,31 @@ bool EmbedTexturesProcess::addTexture(aiScene* pScene, std::string path) const {
std::string imagePath = path; std::string imagePath = path;
// Test path directly // Test path directly
std::ifstream file(imagePath, std::ios::binary | std::ios::ate); if (!mIOHandler->Exists(imagePath)) {
if ((imageSize = file.tellg()) == std::streampos(-1)) {
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder."); ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
// Test path in root path // Test path in root path
imagePath = mRootPath + path; imagePath = mRootPath + path;
file.open(imagePath, std::ios::binary | std::ios::ate); if (!mIOHandler->Exists(imagePath)) {
if ((imageSize = file.tellg()) == std::streampos(-1)) {
// Test path basename in root path // Test path basename in root path
imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u); imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u);
file.open(imagePath, std::ios::binary | std::ios::ate); if (!mIOHandler->Exists(imagePath)) {
if ((imageSize = file.tellg()) == std::streampos(-1)) {
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, "."); ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
return false; return false;
} }
} }
} }
IOStream* pFile = mIOHandler->Open(imagePath);
if (pFile == nullptr) {
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
return false;
}
imageSize = pFile->FileSize();
aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)]; aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)];
file.seekg(0, std::ios::beg); pFile->Seek(0, aiOrigin_SET);
file.read(reinterpret_cast<char*>(imageContent), imageSize); pFile->Read(reinterpret_cast<char*>(imageContent), imageSize, 1);
mIOHandler->Close(pFile);
// Enlarging the textures table // Enlarging the textures table
unsigned int textureId = pScene->mNumTextures++; unsigned int textureId = pScene->mNumTextures++;

View File

@ -48,6 +48,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
struct aiNode; struct aiNode;
class IOSystem;
namespace Assimp { namespace Assimp {
/** /**
@ -80,6 +82,7 @@ private:
private: private:
std::string mRootPath; std::string mRootPath;
IOSystem* mIOHandler = nullptr;
}; };
} // namespace Assimp } // namespace Assimp