Fix unittests + small refactorings

pull/4342/head
Kim Kulling 2022-01-18 21:23:43 +01:00
parent cbde086bfb
commit 5af79cac1d
2 changed files with 40 additions and 29 deletions

View File

@ -98,7 +98,7 @@ static const aiImporterDesc desc = {
glTF2Importer::glTF2Importer() : glTF2Importer::glTF2Importer() :
BaseImporter(), BaseImporter(),
meshOffsets(), meshOffsets(),
embeddedTexIdxs(), mEmbeddedTexIdxs(),
mScene(nullptr) { mScene(nullptr) {
// empty // empty
} }
@ -111,18 +111,21 @@ const aiImporterDesc *glTF2Importer::GetInfo() const {
return &desc; return &desc;
} }
bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { bool glTF2Importer::CanRead(const std::string &filename, IOSystem *pIOHandler, bool checkSig ) const {
try { const std::string extension = GetExtension(filename);
glTF2::Asset asset(pIOHandler); if (!checkSig && (extension != "gltf") && (extension != "glb")) {
asset.Load(pFile, GetExtension(pFile) == "glb");
std::string version = asset.asset.version;
return !version.empty() && version[0] == '2';
} catch(...) {
return false; return false;
} }
if (pIOHandler) {
glTF2::Asset asset(pIOHandler);
return asset.CanRead(filename, extension == "glb");
}
return false;
} }
static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) { static inline aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) {
switch (gltfWrapMode) { switch (gltfWrapMode) {
case SamplerWrap::Mirrored_Repeat: case SamplerWrap::Mirrored_Repeat:
return aiTextureMapMode_Mirror; return aiTextureMapMode_Mirror;
@ -137,21 +140,21 @@ static aiTextureMapMode ConvertWrappingMode(SamplerWrap gltfWrapMode) {
} }
} }
inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat, static inline void SetMaterialColorProperty(Asset & /*r*/, vec4 &prop, aiMaterial *mat,
const char *pKey, unsigned int type, unsigned int idx) { const char *pKey, unsigned int type, unsigned int idx) {
aiColor4D col; aiColor4D col;
CopyValue(prop, col); CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx); mat->AddProperty(&col, 1, pKey, type, idx);
} }
inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat, static inline void SetMaterialColorProperty(Asset & /*r*/, vec3 &prop, aiMaterial *mat,
const char *pKey, unsigned int type, unsigned int idx) { const char *pKey, unsigned int type, unsigned int idx) {
aiColor4D col; aiColor4D col;
glTFCommon::CopyValue(prop, col); glTFCommon::CopyValue(prop, col);
mat->AddProperty(&col, 1, pKey, type, idx); mat->AddProperty(&col, 1, pKey, type, idx);
} }
inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/, static void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset & /*r*/,
glTF2::TextureInfo prop, aiMaterial *mat, aiTextureType texType, glTF2::TextureInfo prop, aiMaterial *mat, aiTextureType texType,
unsigned int texSlot = 0) { unsigned int texSlot = 0) {
if (prop.texture && prop.texture->source) { if (prop.texture && prop.texture->source) {
@ -368,10 +371,10 @@ void glTF2Importer::ImportMaterials(Asset &r) {
mScene->mNumMaterials = numImportedMaterials + 1; mScene->mNumMaterials = numImportedMaterials + 1;
mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials]; mScene->mMaterials = new aiMaterial *[mScene->mNumMaterials];
std::fill(mScene->mMaterials, mScene->mMaterials + mScene->mNumMaterials, nullptr); std::fill(mScene->mMaterials, mScene->mMaterials + mScene->mNumMaterials, nullptr);
mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial); mScene->mMaterials[numImportedMaterials] = ImportMaterial(mEmbeddedTexIdxs, r, defaultMaterial);
for (unsigned int i = 0; i < numImportedMaterials; ++i) { for (unsigned int i = 0; i < numImportedMaterials; ++i) {
mScene->mMaterials[i] = ImportMaterial(embeddedTexIdxs, r, r.materials[i]); mScene->mMaterials[i] = ImportMaterial(mEmbeddedTexIdxs, r, r.materials[i]);
} }
} }
@ -799,8 +802,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
if (actualNumFaces < nFaces) { if (actualNumFaces < nFaces) {
ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped."); ASSIMP_LOG_WARN("Some faces had out-of-range indices. Those faces were dropped.");
} }
if (actualNumFaces == 0) if (actualNumFaces == 0) {
{
throw DeadlyImportError("Mesh \"", aim->mName.C_Str(), "\" has no faces"); throw DeadlyImportError("Mesh \"", aim->mName.C_Str(), "\" has no faces");
} }
aim->mNumFaces = actualNumFaces; aim->mNumFaces = actualNumFaces;
@ -840,7 +842,6 @@ void glTF2Importer::ImportCameras(glTF2::Asset &r) {
aicam->mLookAt = aiVector3D(0.f, 0.f, -1.f); aicam->mLookAt = aiVector3D(0.f, 0.f, -1.f);
if (cam.type == Camera::Perspective) { if (cam.type == Camera::Perspective) {
aicam->mAspect = cam.cameraProperties.perspective.aspectRatio; aicam->mAspect = cam.cameraProperties.perspective.aspectRatio;
aicam->mHorizontalFOV = cam.cameraProperties.perspective.yfov * ((aicam->mAspect == 0.f) ? 1.f : aicam->mAspect); aicam->mHorizontalFOV = cam.cameraProperties.perspective.yfov * ((aicam->mAspect == 0.f) ? 1.f : aicam->mAspect);
aicam->mClipPlaneFar = cam.cameraProperties.perspective.zfar; aicam->mClipPlaneFar = cam.cameraProperties.perspective.zfar;
@ -859,8 +860,9 @@ void glTF2Importer::ImportCameras(glTF2::Asset &r) {
} }
void glTF2Importer::ImportLights(glTF2::Asset &r) { void glTF2Importer::ImportLights(glTF2::Asset &r) {
if (!r.lights.Size()) if (!r.lights.Size()) {
return; return;
}
const unsigned int numLights = r.lights.Size(); const unsigned int numLights = r.lights.Size();
ASSIMP_LOG_DEBUG("Importing ", numLights, " lights"); ASSIMP_LOG_DEBUG("Importing ", numLights, " lights");
@ -1122,8 +1124,8 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
bone->mNumWeights = static_cast<uint32_t>(weights.size()); bone->mNumWeights = static_cast<uint32_t>(weights.size());
if (bone->mNumWeights > 0) { if (bone->mNumWeights > 0) {
bone->mWeights = new aiVertexWeight[bone->mNumWeights]; bone->mWeights = new aiVertexWeight[bone->mNumWeights];
memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight));
} else { } else {
// Assimp expects all bones to have at least 1 weight. // Assimp expects all bones to have at least 1 weight.
bone->mWeights = new aiVertexWeight[1]; bone->mWeights = new aiVertexWeight[1];
@ -1164,8 +1166,7 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
if (!ainode->mMetaData) { if (!ainode->mMetaData) {
ainode->mMetaData = aiMetadata::Alloc(1); ainode->mMetaData = aiMetadata::Alloc(1);
ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value); ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value);
} } else {
else {
ainode->mMetaData->Add("PBR_LightRange", node.light->range.value); ainode->mMetaData->Add("PBR_LightRange", node.light->range.value);
} }
} }
@ -1506,16 +1507,26 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
} }
} }
void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { static unsigned int countEmbeddedTextures(glTF2::Asset &r) {
embeddedTexIdxs.resize(r.images.Size(), -1); unsigned int numEmbeddedTexs = 0;
int numEmbeddedTexs = 0;
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {
if (r.images[i].HasData()) { if (r.images[i].HasData()) {
numEmbeddedTexs += 1; numEmbeddedTexs += 1;
} }
} }
return numEmbeddedTexs;
}
void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
mEmbeddedTexIdxs.resize(r.images.Size(), -1);
const unsigned int numEmbeddedTexs = countEmbeddedTextures(r);
/* for (size_t i = 0; i < r.images.Size(); ++i) {
if (r.images[i].HasData()) {
numEmbeddedTexs += 1;
}
}*/
if (numEmbeddedTexs == 0) { if (numEmbeddedTexs == 0) {
return; return;
} }
@ -1533,7 +1544,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
} }
int idx = mScene->mNumTextures++; int idx = mScene->mNumTextures++;
embeddedTexIdxs[i] = idx; mEmbeddedTexIdxs[i] = idx;
aiTexture *tex = mScene->mTextures[idx] = new aiTexture(); aiTexture *tex = mScene->mTextures[idx] = new aiTexture();
@ -1594,7 +1605,7 @@ void glTF2Importer::InternReadFile(const std::string &pFile, aiScene *pScene, IO
// clean all member arrays // clean all member arrays
meshOffsets.clear(); meshOffsets.clear();
embeddedTexIdxs.clear(); mEmbeddedTexIdxs.clear();
this->mScene = pScene; this->mScene = pScene;

View File

@ -79,7 +79,7 @@ private:
private: private:
std::vector<unsigned int> meshOffsets; std::vector<unsigned int> meshOffsets;
std::vector<int> embeddedTexIdxs; std::vector<int> mEmbeddedTexIdxs;
aiScene *mScene; aiScene *mScene;
/// An instance of rapidjson::IRemoteSchemaDocumentProvider /// An instance of rapidjson::IRemoteSchemaDocumentProvider