Merge branch 'master' into develop3

pull/3546/head
Kim Kulling 2021-01-02 00:13:47 +01:00 committed by GitHub
commit fa24e4199c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 354 additions and 310 deletions

View File

@ -34,6 +34,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#---------------------------------------------------------------------- #----------------------------------------------------------------------
SET(CMAKE_POLICY_DEFAULT_CMP0012 NEW)
SET(CMAKE_POLICY_DEFAULT_CMP0074 NEW) SET(CMAKE_POLICY_DEFAULT_CMP0074 NEW)
SET(CMAKE_POLICY_DEFAULT_CMP0092 NEW) SET(CMAKE_POLICY_DEFAULT_CMP0092 NEW)

View File

@ -53,7 +53,7 @@ const MetaKeyPairVector MakeColladaAssimpMetaKeys() {
result.emplace_back("authoring_tool", AI_METADATA_SOURCE_GENERATOR); result.emplace_back("authoring_tool", AI_METADATA_SOURCE_GENERATOR);
result.emplace_back("copyright", AI_METADATA_SOURCE_COPYRIGHT); result.emplace_back("copyright", AI_METADATA_SOURCE_COPYRIGHT);
return result; return result;
}; }
const MetaKeyPairVector &GetColladaAssimpMetaKeys() { const MetaKeyPairVector &GetColladaAssimpMetaKeys() {
static const MetaKeyPairVector result = MakeColladaAssimpMetaKeys(); static const MetaKeyPairVector result = MakeColladaAssimpMetaKeys();
@ -66,7 +66,7 @@ const MetaKeyPairVector MakeColladaAssimpMetaKeysCamelCase() {
ToCamelCase(val.first); ToCamelCase(val.first);
} }
return result; return result;
}; }
const MetaKeyPairVector &GetColladaAssimpMetaKeysCamelCase() { const MetaKeyPairVector &GetColladaAssimpMetaKeysCamelCase() {
static const MetaKeyPairVector result = MakeColladaAssimpMetaKeysCamelCase(); static const MetaKeyPairVector result = MakeColladaAssimpMetaKeysCamelCase();

View File

@ -375,6 +375,11 @@ bool ReadScope(TokenList& output_tokens, const char* input, const char*& cursor,
// now come the individual properties // now come the individual properties
const char* begin_cursor = cursor; const char* begin_cursor = cursor;
if ((begin_cursor + prop_length) > end) {
TokenizeError("property length out of bounds reading length ", input, cursor);
}
for (unsigned int i = 0; i < prop_count; ++i) { for (unsigned int i = 0; i < prop_count; ++i) {
ReadData(sbeg, send, input, cursor, begin_cursor + prop_length); ReadData(sbeg, send, input, cursor, begin_cursor + prop_length);

View File

@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <assimp/StringUtils.h> #include <assimp/StringUtils.h>
#include <assimp/MemoryIOWrapper.h>
#include <iomanip> #include <iomanip>
// Header files, Assimp // Header files, Assimp
@ -331,7 +332,10 @@ inline void Buffer::Read(Value &obj, Asset &r) {
} }
} else { // Local file } else { // Local file
if (byteLength > 0) { if (byteLength > 0) {
std::string dir = !r.mCurrentAssetDir.empty() ? (r.mCurrentAssetDir) : ""; std::string dir = !r.mCurrentAssetDir.empty() ? (
r.mCurrentAssetDir.back() == '/' ?
r.mCurrentAssetDir : r.mCurrentAssetDir + '/'
) : "";
IOStream *file = r.OpenFile(dir + uri, "rb"); IOStream *file = r.OpenFile(dir + uri, "rb");
if (file) { if (file) {
@ -1276,7 +1280,9 @@ inline void Asset::Load(const std::string &pFile, bool isBinary) {
/*int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\'))); /*int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
if (pos != int(std::string::npos)) mCurrentAssetDir = pFile.substr(0, pos + 1);*/ if (pos != int(std::string::npos)) mCurrentAssetDir = pFile.substr(0, pos + 1);*/
mCurrentAssetDir = getCurrentAssetDir(pFile); if (0 != strncmp(pFile.c_str(), AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
mCurrentAssetDir = getCurrentAssetDir(pFile);
}
shared_ptr<IOStream> stream(OpenFile(pFile.c_str(), "rb", true)); shared_ptr<IOStream> stream(OpenFile(pFile.c_str(), "rb", true));
if (!stream) { if (!stream) {

View File

@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <assimp/StringUtils.h> #include <assimp/StringUtils.h>
#include <assimp/DefaultLogger.hpp> #include <assimp/DefaultLogger.hpp>
#include <assimp/MemoryIOWrapper.h>
using namespace Assimp; using namespace Assimp;
@ -400,7 +401,10 @@ inline void Buffer::Read(Value &obj, Asset &r) {
} }
} else { // Local file } else { // Local file
if (byteLength > 0) { if (byteLength > 0) {
std::string dir = !r.mCurrentAssetDir.empty() ? (r.mCurrentAssetDir) : ""; std::string dir = !r.mCurrentAssetDir.empty() ? (
r.mCurrentAssetDir.back() == '/' ?
r.mCurrentAssetDir : r.mCurrentAssetDir + '/'
) : "";
IOStream *file = r.OpenFile(dir + uri, "rb"); IOStream *file = r.OpenFile(dir + uri, "rb");
if (file) { if (file) {
@ -1590,8 +1594,10 @@ inline void Asset::Load(const std::string &pFile, bool isBinary) {
/*int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\'))); /*int pos = std::max(int(pFile.rfind('/')), int(pFile.rfind('\\')));
if (pos != int(std::string::npos)) */ if (pos != int(std::string::npos)) */
mCurrentAssetDir = glTFCommon::getCurrentAssetDir(pFile); if (0 != strncmp(pFile.c_str(), AI_MEMORYIO_MAGIC_FILENAME, AI_MEMORYIO_MAGIC_FILENAME_LENGTH)) {
mCurrentAssetDir = glTFCommon::getCurrentAssetDir(pFile);
}
shared_ptr<IOStream> stream(OpenFile(pFile.c_str(), "rb", true)); shared_ptr<IOStream> stream(OpenFile(pFile.c_str(), "rb", true));
if (!stream) { if (!stream) {
throw DeadlyImportError("GLTF: Could not open file for reading"); throw DeadlyImportError("GLTF: Could not open file for reading");

View File

@ -1258,9 +1258,6 @@ inline Ref<Accessor> GetSamplerInputRef(Asset& asset, std::string& animId, Ref<B
inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumPositionKeys; const unsigned int numKeyframes = nodeChannel->mNumPositionKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 3); std::vector<float> values(numKeyframes * 3);
@ -1281,9 +1278,6 @@ inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buf
inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumScalingKeys; const unsigned int numKeyframes = nodeChannel->mNumScalingKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 3); std::vector<float> values(numKeyframes * 3);
@ -1304,9 +1298,6 @@ inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>&
inline void ExtractRotationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractRotationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumRotationKeys; const unsigned int numKeyframes = nodeChannel->mNumRotationKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 4); std::vector<float> values(numKeyframes * 4);
@ -1347,29 +1338,36 @@ void glTF2Exporter::ExportAnimations()
if (anim->mName.length > 0) { if (anim->mName.length > 0) {
nameAnim = anim->mName.C_Str(); nameAnim = anim->mName.C_Str();
} }
Ref<Animation> animRef = mAsset->animations.Create(nameAnim);
for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) { for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) {
const aiNodeAnim* nodeChannel = anim->mChannels[channelIndex]; const aiNodeAnim* nodeChannel = anim->mChannels[channelIndex];
// It appears that assimp stores this type of animation as multiple animations.
// where each aiNodeAnim in mChannels animates a specific node.
std::string name = nameAnim + "_" + to_string(channelIndex); std::string name = nameAnim + "_" + to_string(channelIndex);
name = mAsset->FindUniqueID(name, "animation"); name = mAsset->FindUniqueID(name, "animation");
Ref<Animation> animRef = mAsset->animations.Create(name);
Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str()); Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
Animation::Sampler translationSampler; if (nodeChannel->mNumPositionKeys > 0)
ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler); {
AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION); Animation::Sampler translationSampler;
ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler);
AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION);
}
Animation::Sampler rotationSampler; if (nodeChannel->mNumRotationKeys > 0)
ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler); {
AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION); Animation::Sampler rotationSampler;
ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler);
AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION);
}
Animation::Sampler scaleSampler; if (nodeChannel->mNumScalingKeys > 0)
ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler); {
AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE); Animation::Sampler scaleSampler;
ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler);
AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE);
}
} }
// Assimp documentation staes this is not used (not implemented) // Assimp documentation staes this is not used (not implemented)

View File

@ -229,59 +229,64 @@ inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset
static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, Material &mat) { static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, Material &mat) {
aiMaterial *aimat = new aiMaterial(); aiMaterial *aimat = new aiMaterial();
if (!mat.name.empty()) { try {
aiString str(mat.name); if (!mat.name.empty()) {
aiString str(mat.name);
aimat->AddProperty(&str, AI_MATKEY_NAME); aimat->AddProperty(&str, AI_MATKEY_NAME);
}
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, aiTextureType_DIFFUSE);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.metallicRoughnessTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
aimat->AddProperty(&mat.pbrMetallicRoughness.metallicFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR);
aimat->AddProperty(&mat.pbrMetallicRoughness.roughnessFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR);
float roughnessAsShininess = 1 - mat.pbrMetallicRoughness.roughnessFactor;
roughnessAsShininess *= roughnessAsShininess * 1000;
aimat->AddProperty(&roughnessAsShininess, 1, AI_MATKEY_SHININESS);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.normalTexture, aimat, aiTextureType_NORMALS);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.occlusionTexture, aimat, aiTextureType_LIGHTMAP);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.emissiveTexture, aimat, aiTextureType_EMISSIVE);
SetMaterialColorProperty(r, mat.emissiveFactor, aimat, AI_MATKEY_COLOR_EMISSIVE);
aimat->AddProperty(&mat.doubleSided, 1, AI_MATKEY_TWOSIDED);
aiString alphaMode(mat.alphaMode);
aimat->AddProperty(&alphaMode, AI_MATKEY_GLTF_ALPHAMODE);
aimat->AddProperty(&mat.alphaCutoff, 1, AI_MATKEY_GLTF_ALPHACUTOFF);
//pbrSpecularGlossiness
if (mat.pbrSpecularGlossiness.isPresent) {
PbrSpecularGlossiness &pbrSG = mat.pbrSpecularGlossiness.value;
aimat->AddProperty(&mat.pbrSpecularGlossiness.isPresent, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS);
SetMaterialColorProperty(r, pbrSG.diffuseFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
SetMaterialColorProperty(r, pbrSG.specularFactor, aimat, AI_MATKEY_COLOR_SPECULAR);
float glossinessAsShininess = pbrSG.glossinessFactor * 1000.0f;
aimat->AddProperty(&glossinessAsShininess, 1, AI_MATKEY_SHININESS);
aimat->AddProperty(&pbrSG.glossinessFactor, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR);
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.diffuseTexture, aimat, aiTextureType_DIFFUSE);
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR);
}
if (mat.unlit) {
aimat->AddProperty(&mat.unlit, 1, AI_MATKEY_GLTF_UNLIT);
}
return aimat;
} catch (...) {
delete aimat;
throw;
} }
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, aiTextureType_DIFFUSE);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.metallicRoughnessTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
aimat->AddProperty(&mat.pbrMetallicRoughness.metallicFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR);
aimat->AddProperty(&mat.pbrMetallicRoughness.roughnessFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR);
float roughnessAsShininess = 1 - mat.pbrMetallicRoughness.roughnessFactor;
roughnessAsShininess *= roughnessAsShininess * 1000;
aimat->AddProperty(&roughnessAsShininess, 1, AI_MATKEY_SHININESS);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.normalTexture, aimat, aiTextureType_NORMALS);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.occlusionTexture, aimat, aiTextureType_LIGHTMAP);
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.emissiveTexture, aimat, aiTextureType_EMISSIVE);
SetMaterialColorProperty(r, mat.emissiveFactor, aimat, AI_MATKEY_COLOR_EMISSIVE);
aimat->AddProperty(&mat.doubleSided, 1, AI_MATKEY_TWOSIDED);
aiString alphaMode(mat.alphaMode);
aimat->AddProperty(&alphaMode, AI_MATKEY_GLTF_ALPHAMODE);
aimat->AddProperty(&mat.alphaCutoff, 1, AI_MATKEY_GLTF_ALPHACUTOFF);
//pbrSpecularGlossiness
if (mat.pbrSpecularGlossiness.isPresent) {
PbrSpecularGlossiness &pbrSG = mat.pbrSpecularGlossiness.value;
aimat->AddProperty(&mat.pbrSpecularGlossiness.isPresent, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS);
SetMaterialColorProperty(r, pbrSG.diffuseFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
SetMaterialColorProperty(r, pbrSG.specularFactor, aimat, AI_MATKEY_COLOR_SPECULAR);
float glossinessAsShininess = pbrSG.glossinessFactor * 1000.0f;
aimat->AddProperty(&glossinessAsShininess, 1, AI_MATKEY_SHININESS);
aimat->AddProperty(&pbrSG.glossinessFactor, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR);
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.diffuseTexture, aimat, aiTextureType_DIFFUSE);
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR);
}
if (mat.unlit) {
aimat->AddProperty(&mat.unlit, 1, AI_MATKEY_GLTF_UNLIT);
}
return aimat;
} }
void glTF2Importer::ImportMaterials(glTF2::Asset &r) { void glTF2Importer::ImportMaterials(glTF2::Asset &r) {
@ -291,6 +296,7 @@ void glTF2Importer::ImportMaterials(glTF2::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);
mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial); mScene->mMaterials[numImportedMaterials] = ImportMaterial(embeddedTexIdxs, r, defaultMaterial);
for (unsigned int i = 0; i < numImportedMaterials; ++i) { for (unsigned int i = 0; i < numImportedMaterials; ++i) {
@ -452,6 +458,7 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
if (targets.size() > 0) { if (targets.size() > 0) {
aim->mNumAnimMeshes = (unsigned int)targets.size(); aim->mNumAnimMeshes = (unsigned int)targets.size();
aim->mAnimMeshes = new aiAnimMesh *[aim->mNumAnimMeshes]; aim->mAnimMeshes = new aiAnimMesh *[aim->mNumAnimMeshes];
std::fill(aim->mAnimMeshes, aim->mAnimMeshes + aim->mNumAnimMeshes, nullptr);
for (size_t i = 0; i < targets.size(); i++) { for (size_t i = 0; i < targets.size(); i++) {
bool needPositions = targets[i].position.size() > 0; bool needPositions = targets[i].position.size() > 0;
bool needNormals = targets[i].normal.size() > 0; bool needNormals = targets[i].normal.size() > 0;
@ -510,7 +517,9 @@ void glTF2Importer::ImportMeshes(glTF2::Asset &r) {
size_t count = prim.indices->count; size_t count = prim.indices->count;
Accessor::Indexer data = prim.indices->GetIndexer(); Accessor::Indexer data = prim.indices->GetIndexer();
ai_assert(data.IsValid()); if (!data.IsValid()) {
throw DeadlyImportError("GLTF: Invalid accessor without data in mesh ", getContextForErrorMessages(mesh.id, mesh.name));
}
switch (prim.mode) { switch (prim.mode) {
case PrimitiveMode_POINTS: { case PrimitiveMode_POINTS: {
@ -699,6 +708,7 @@ void glTF2Importer::ImportCameras(glTF2::Asset &r) {
ASSIMP_LOG_DEBUG_F("Importing ", numCameras, " cameras"); ASSIMP_LOG_DEBUG_F("Importing ", numCameras, " cameras");
mScene->mNumCameras = numCameras; mScene->mNumCameras = numCameras;
mScene->mCameras = new aiCamera *[numCameras]; mScene->mCameras = new aiCamera *[numCameras];
std::fill(mScene->mCameras, mScene->mCameras + numCameras, nullptr);
for (size_t i = 0; i < numCameras; ++i) { for (size_t i = 0; i < numCameras; ++i) {
Camera &cam = r.cameras[i]; Camera &cam = r.cameras[i];
@ -735,6 +745,7 @@ void glTF2Importer::ImportLights(glTF2::Asset &r) {
ASSIMP_LOG_DEBUG_F("Importing ", numLights, " lights"); ASSIMP_LOG_DEBUG_F("Importing ", numLights, " lights");
mScene->mNumLights = numLights; mScene->mNumLights = numLights;
mScene->mLights = new aiLight *[numLights]; mScene->mLights = new aiLight *[numLights];
std::fill(mScene->mLights, mScene->mLights + numLights, nullptr);
for (size_t i = 0; i < numLights; ++i) { for (size_t i = 0; i < numLights; ++i) {
Light &light = r.lights[i]; Light &light = r.lights[i];
@ -898,129 +909,136 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector<unsigned int> &
aiNode *ainode = new aiNode(GetNodeName(node)); aiNode *ainode = new aiNode(GetNodeName(node));
if (!node.children.empty()) { try {
ainode->mNumChildren = unsigned(node.children.size()); if (!node.children.empty()) {
ainode->mChildren = new aiNode *[ainode->mNumChildren]; ainode->mNumChildren = unsigned(node.children.size());
ainode->mChildren = new aiNode *[ainode->mNumChildren];
std::fill(ainode->mChildren, ainode->mChildren + ainode->mNumChildren, nullptr);
for (unsigned int i = 0; i < ainode->mNumChildren; ++i) { for (unsigned int i = 0; i < ainode->mNumChildren; ++i) {
aiNode *child = ImportNode(pScene, r, meshOffsets, node.children[i]); aiNode *child = ImportNode(pScene, r, meshOffsets, node.children[i]);
child->mParent = ainode; child->mParent = ainode;
ainode->mChildren[i] = child; ainode->mChildren[i] = child;
}
} }
}
if (node.extensions) { if (node.extensions) {
ainode->mMetaData = new aiMetadata; ainode->mMetaData = new aiMetadata;
ParseExtensions(ainode->mMetaData, node.extensions); ParseExtensions(ainode->mMetaData, node.extensions);
}
GetNodeTransform(ainode->mTransformation, node);
if (!node.meshes.empty()) {
// GLTF files contain at most 1 mesh per node.
if (node.meshes.size() > 1)
{
throw DeadlyImportError("GLTF: Invalid input, found ", node.meshes.size(), " meshes in ", getContextForErrorMessages(node.id, node.name), ", but only 1 mesh per node allowed.");
} }
int mesh_idx = node.meshes[0].GetIndex();
int count = meshOffsets[mesh_idx + 1] - meshOffsets[mesh_idx];
ainode->mNumMeshes = count; GetNodeTransform(ainode->mTransformation, node);
ainode->mMeshes = new unsigned int[count];
if (node.skin) { if (!node.meshes.empty()) {
for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) { // GLTF files contain at most 1 mesh per node.
aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo]; if (node.meshes.size() > 1)
unsigned int numBones =static_cast<unsigned int>(node.skin->jointNames.size()); {
throw DeadlyImportError("GLTF: Invalid input, found ", node.meshes.size(), " meshes in ", getContextForErrorMessages(node.id, node.name), ", but only 1 mesh per node allowed.");
}
int mesh_idx = node.meshes[0].GetIndex();
int count = meshOffsets[mesh_idx + 1] - meshOffsets[mesh_idx];
std::vector<std::vector<aiVertexWeight>> weighting(numBones); ainode->mNumMeshes = count;
BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting); ainode->mMeshes = new unsigned int[count];
mesh->mNumBones = static_cast<unsigned int>(numBones); if (node.skin) {
mesh->mBones = new aiBone *[mesh->mNumBones]; for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) {
aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo];
unsigned int numBones =static_cast<unsigned int>(node.skin->jointNames.size());
// GLTF and Assimp choose to store bone weights differently. std::vector<std::vector<aiVertexWeight>> weighting(numBones);
// GLTF has each vertex specify which bones influence the vertex. BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting);
// Assimp has each bone specify which vertices it has influence over.
// To convert this data, we first read over the vertex data and pull
// out the bone-to-vertex mapping. Then, when creating the aiBones,
// we copy the bone-to-vertex mapping into the bone. This is unfortunate
// both because it's somewhat slow and because, for many applications,
// we then need to reconvert the data back into the vertex-to-bone
// mapping which makes things doubly-slow.
mat4 *pbindMatrices = nullptr; mesh->mNumBones = static_cast<unsigned int>(numBones);
node.skin->inverseBindMatrices->ExtractData(pbindMatrices); mesh->mBones = new aiBone *[mesh->mNumBones];
std::fill(mesh->mBones, mesh->mBones + mesh->mNumBones, nullptr);
for (uint32_t i = 0; i < numBones; ++i) { // GLTF and Assimp choose to store bone weights differently.
const std::vector<aiVertexWeight> &weights = weighting[i]; // GLTF has each vertex specify which bones influence the vertex.
aiBone *bone = new aiBone(); // Assimp has each bone specify which vertices it has influence over.
// To convert this data, we first read over the vertex data and pull
// out the bone-to-vertex mapping. Then, when creating the aiBones,
// we copy the bone-to-vertex mapping into the bone. This is unfortunate
// both because it's somewhat slow and because, for many applications,
// we then need to reconvert the data back into the vertex-to-bone
// mapping which makes things doubly-slow.
Ref<Node> joint = node.skin->jointNames[i]; mat4 *pbindMatrices = nullptr;
if (!joint->name.empty()) { node.skin->inverseBindMatrices->ExtractData(pbindMatrices);
bone->mName = joint->name;
} else { for (uint32_t i = 0; i < numBones; ++i) {
// Assimp expects each bone to have a unique name. const std::vector<aiVertexWeight> &weights = weighting[i];
static const std::string kDefaultName = "bone_"; aiBone *bone = new aiBone();
char postfix[10] = { 0 };
ASSIMP_itoa10(postfix, i); Ref<Node> joint = node.skin->jointNames[i];
bone->mName = (kDefaultName + postfix); if (!joint->name.empty()) {
bone->mName = joint->name;
} else {
// Assimp expects each bone to have a unique name.
static const std::string kDefaultName = "bone_";
char postfix[10] = { 0 };
ASSIMP_itoa10(postfix, i);
bone->mName = (kDefaultName + postfix);
}
GetNodeTransform(bone->mOffsetMatrix, *joint);
CopyValue(pbindMatrices[i], bone->mOffsetMatrix);
bone->mNumWeights = static_cast<uint32_t>(weights.size());
if (bone->mNumWeights > 0) {
bone->mWeights = new aiVertexWeight[bone->mNumWeights];
memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight));
} else {
// Assimp expects all bones to have at least 1 weight.
bone->mWeights = new aiVertexWeight[1];
bone->mNumWeights = 1;
bone->mWeights->mVertexId = 0;
bone->mWeights->mWeight = 0.f;
}
mesh->mBones[i] = bone;
} }
GetNodeTransform(bone->mOffsetMatrix, *joint);
CopyValue(pbindMatrices[i], bone->mOffsetMatrix);
bone->mNumWeights = static_cast<uint32_t>(weights.size());
if (bone->mNumWeights > 0) { if (pbindMatrices) {
bone->mWeights = new aiVertexWeight[bone->mNumWeights]; delete[] pbindMatrices;
memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight));
} else {
// Assimp expects all bones to have at least 1 weight.
bone->mWeights = new aiVertexWeight[1];
bone->mNumWeights = 1;
bone->mWeights->mVertexId = 0;
bone->mWeights->mWeight = 0.f;
} }
mesh->mBones[i] = bone;
} }
}
if (pbindMatrices) { int k = 0;
delete[] pbindMatrices; for (unsigned int j = meshOffsets[mesh_idx]; j < meshOffsets[mesh_idx + 1]; ++j, ++k) {
ainode->mMeshes[k] = j;
}
}
if (node.camera) {
pScene->mCameras[node.camera.GetIndex()]->mName = ainode->mName;
if (node.translation.isPresent) {
aiVector3D trans;
CopyValue(node.translation.value, trans);
pScene->mCameras[node.camera.GetIndex()]->mPosition = trans;
}
}
if (node.light) {
pScene->mLights[node.light.GetIndex()]->mName = ainode->mName;
//range is optional - see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
//it is added to meta data of parent node, because there is no other place to put it
if (node.light->range.isPresent) {
if (!ainode->mMetaData) {
ainode->mMetaData = aiMetadata::Alloc(1);
ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value);
}
else {
ainode->mMetaData->Add("PBR_LightRange", node.light->range.value);
} }
} }
} }
int k = 0; return ainode;
for (unsigned int j = meshOffsets[mesh_idx]; j < meshOffsets[mesh_idx + 1]; ++j, ++k) { } catch (...) {
ainode->mMeshes[k] = j; delete ainode;
} throw;
} }
if (node.camera) {
pScene->mCameras[node.camera.GetIndex()]->mName = ainode->mName;
if (node.translation.isPresent) {
aiVector3D trans;
CopyValue(node.translation.value, trans);
pScene->mCameras[node.camera.GetIndex()]->mPosition = trans;
}
}
if (node.light) {
pScene->mLights[node.light.GetIndex()]->mName = ainode->mName;
//range is optional - see https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
//it is added to meta data of parent node, because there is no other place to put it
if (node.light->range.isPresent) {
if (!ainode->mMetaData) {
ainode->mMetaData = aiMetadata::Alloc(1);
ainode->mMetaData->Set(0, "PBR_LightRange", node.light->range.value);
}
else {
ainode->mMetaData->Add("PBR_LightRange", node.light->range.value);
}
}
}
return ainode;
} }
void glTF2Importer::ImportNodes(glTF2::Asset &r) { void glTF2Importer::ImportNodes(glTF2::Asset &r) {
@ -1036,14 +1054,16 @@ void glTF2Importer::ImportNodes(glTF2::Asset &r) {
if (numRootNodes == 1) { // a single root node: use it if (numRootNodes == 1) { // a single root node: use it
mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]); mScene->mRootNode = ImportNode(mScene, r, meshOffsets, rootNodes[0]);
} else if (numRootNodes > 1) { // more than one root node: create a fake root } else if (numRootNodes > 1) { // more than one root node: create a fake root
aiNode *root = new aiNode("ROOT"); aiNode *root = mScene->mRootNode = new aiNode("ROOT");
root->mChildren = new aiNode *[numRootNodes]; root->mChildren = new aiNode *[numRootNodes];
std::fill(root->mChildren, root->mChildren + numRootNodes, nullptr);
for (unsigned int i = 0; i < numRootNodes; ++i) { for (unsigned int i = 0; i < numRootNodes; ++i) {
aiNode *node = ImportNode(mScene, r, meshOffsets, rootNodes[i]); aiNode *node = ImportNode(mScene, r, meshOffsets, rootNodes[i]);
node->mParent = root; node->mParent = root;
root->mChildren[root->mNumChildren++] = node; root->mChildren[root->mNumChildren++] = node;
} }
mScene->mRootNode = root;
} else { } else {
mScene->mRootNode = new aiNode("ROOT"); mScene->mRootNode = new aiNode("ROOT");
} }
@ -1066,133 +1086,145 @@ struct AnimationSamplers {
aiNodeAnim *CreateNodeAnim(glTF2::Asset&, Node &node, AnimationSamplers &samplers) { aiNodeAnim *CreateNodeAnim(glTF2::Asset&, Node &node, AnimationSamplers &samplers) {
aiNodeAnim *anim = new aiNodeAnim(); aiNodeAnim *anim = new aiNodeAnim();
anim->mNodeName = GetNodeName(node);
static const float kMillisecondsFromSeconds = 1000.f; try {
anim->mNodeName = GetNodeName(node);
if (samplers.translation) { static const float kMillisecondsFromSeconds = 1000.f;
float *times = nullptr;
samplers.translation->input->ExtractData(times); if (samplers.translation) {
aiVector3D *values = nullptr; float *times = nullptr;
samplers.translation->output->ExtractData(values); samplers.translation->input->ExtractData(times);
anim->mNumPositionKeys = static_cast<uint32_t>(samplers.translation->input->count); aiVector3D *values = nullptr;
anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys]; samplers.translation->output->ExtractData(values);
unsigned int ii = (samplers.translation->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0; anim->mNumPositionKeys = static_cast<uint32_t>(samplers.translation->input->count);
for (unsigned int i = 0; i < anim->mNumPositionKeys; ++i) { anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
anim->mPositionKeys[i].mTime = times[i] * kMillisecondsFromSeconds; unsigned int ii = (samplers.translation->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0;
anim->mPositionKeys[i].mValue = values[ii]; for (unsigned int i = 0; i < anim->mNumPositionKeys; ++i) {
ii += (samplers.translation->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1; anim->mPositionKeys[i].mTime = times[i] * kMillisecondsFromSeconds;
anim->mPositionKeys[i].mValue = values[ii];
ii += (samplers.translation->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1;
}
delete[] times;
delete[] values;
} else if (node.translation.isPresent) {
anim->mNumPositionKeys = 1;
anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
anim->mPositionKeys->mTime = 0.f;
anim->mPositionKeys->mValue.x = node.translation.value[0];
anim->mPositionKeys->mValue.y = node.translation.value[1];
anim->mPositionKeys->mValue.z = node.translation.value[2];
} }
delete[] times;
delete[] values;
} else if (node.translation.isPresent) {
anim->mNumPositionKeys = 1;
anim->mPositionKeys = new aiVectorKey[anim->mNumPositionKeys];
anim->mPositionKeys->mTime = 0.f;
anim->mPositionKeys->mValue.x = node.translation.value[0];
anim->mPositionKeys->mValue.y = node.translation.value[1];
anim->mPositionKeys->mValue.z = node.translation.value[2];
}
if (samplers.rotation) { if (samplers.rotation) {
float *times = nullptr; float *times = nullptr;
samplers.rotation->input->ExtractData(times); samplers.rotation->input->ExtractData(times);
aiQuaternion *values = nullptr; aiQuaternion *values = nullptr;
samplers.rotation->output->ExtractData(values); samplers.rotation->output->ExtractData(values);
anim->mNumRotationKeys = static_cast<uint32_t>(samplers.rotation->input->count); anim->mNumRotationKeys = static_cast<uint32_t>(samplers.rotation->input->count);
anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys]; anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys];
unsigned int ii = (samplers.rotation->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0; unsigned int ii = (samplers.rotation->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0;
for (unsigned int i = 0; i < anim->mNumRotationKeys; ++i) { for (unsigned int i = 0; i < anim->mNumRotationKeys; ++i) {
anim->mRotationKeys[i].mTime = times[i] * kMillisecondsFromSeconds; anim->mRotationKeys[i].mTime = times[i] * kMillisecondsFromSeconds;
anim->mRotationKeys[i].mValue.x = values[ii].w; anim->mRotationKeys[i].mValue.x = values[ii].w;
anim->mRotationKeys[i].mValue.y = values[ii].x; anim->mRotationKeys[i].mValue.y = values[ii].x;
anim->mRotationKeys[i].mValue.z = values[ii].y; anim->mRotationKeys[i].mValue.z = values[ii].y;
anim->mRotationKeys[i].mValue.w = values[ii].z; anim->mRotationKeys[i].mValue.w = values[ii].z;
ii += (samplers.rotation->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1; ii += (samplers.rotation->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1;
}
delete[] times;
delete[] values;
} else if (node.rotation.isPresent) {
anim->mNumRotationKeys = 1;
anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys];
anim->mRotationKeys->mTime = 0.f;
anim->mRotationKeys->mValue.x = node.rotation.value[0];
anim->mRotationKeys->mValue.y = node.rotation.value[1];
anim->mRotationKeys->mValue.z = node.rotation.value[2];
anim->mRotationKeys->mValue.w = node.rotation.value[3];
} }
delete[] times;
delete[] values;
} else if (node.rotation.isPresent) {
anim->mNumRotationKeys = 1;
anim->mRotationKeys = new aiQuatKey[anim->mNumRotationKeys];
anim->mRotationKeys->mTime = 0.f;
anim->mRotationKeys->mValue.x = node.rotation.value[0];
anim->mRotationKeys->mValue.y = node.rotation.value[1];
anim->mRotationKeys->mValue.z = node.rotation.value[2];
anim->mRotationKeys->mValue.w = node.rotation.value[3];
}
if (samplers.scale) { if (samplers.scale) {
float *times = nullptr; float *times = nullptr;
samplers.scale->input->ExtractData(times); samplers.scale->input->ExtractData(times);
aiVector3D *values = nullptr; aiVector3D *values = nullptr;
samplers.scale->output->ExtractData(values); samplers.scale->output->ExtractData(values);
anim->mNumScalingKeys = static_cast<uint32_t>(samplers.scale->input->count); anim->mNumScalingKeys = static_cast<uint32_t>(samplers.scale->input->count);
anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys]; anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys];
unsigned int ii = (samplers.scale->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0; unsigned int ii = (samplers.scale->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0;
for (unsigned int i = 0; i < anim->mNumScalingKeys; ++i) { for (unsigned int i = 0; i < anim->mNumScalingKeys; ++i) {
anim->mScalingKeys[i].mTime = times[i] * kMillisecondsFromSeconds; anim->mScalingKeys[i].mTime = times[i] * kMillisecondsFromSeconds;
anim->mScalingKeys[i].mValue = values[ii]; anim->mScalingKeys[i].mValue = values[ii];
ii += (samplers.scale->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1; ii += (samplers.scale->interpolation == Interpolation_CUBICSPLINE) ? 3 : 1;
}
delete[] times;
delete[] values;
} else if (node.scale.isPresent) {
anim->mNumScalingKeys = 1;
anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys];
anim->mScalingKeys->mTime = 0.f;
anim->mScalingKeys->mValue.x = node.scale.value[0];
anim->mScalingKeys->mValue.y = node.scale.value[1];
anim->mScalingKeys->mValue.z = node.scale.value[2];
} }
delete[] times;
delete[] values;
} else if (node.scale.isPresent) {
anim->mNumScalingKeys = 1;
anim->mScalingKeys = new aiVectorKey[anim->mNumScalingKeys];
anim->mScalingKeys->mTime = 0.f;
anim->mScalingKeys->mValue.x = node.scale.value[0];
anim->mScalingKeys->mValue.y = node.scale.value[1];
anim->mScalingKeys->mValue.z = node.scale.value[2];
}
return anim; return anim;
} catch (...) {
delete anim;
throw;
}
} }
aiMeshMorphAnim *CreateMeshMorphAnim(glTF2::Asset&, Node &node, AnimationSamplers &samplers) { aiMeshMorphAnim *CreateMeshMorphAnim(glTF2::Asset&, Node &node, AnimationSamplers &samplers) {
aiMeshMorphAnim *anim = new aiMeshMorphAnim(); aiMeshMorphAnim *anim = new aiMeshMorphAnim();
anim->mName = GetNodeName(node);
static const float kMillisecondsFromSeconds = 1000.f; try {
anim->mName = GetNodeName(node);
if (nullptr != samplers.weight) { static const float kMillisecondsFromSeconds = 1000.f;
float *times = nullptr;
samplers.weight->input->ExtractData(times);
float *values = nullptr;
samplers.weight->output->ExtractData(values);
anim->mNumKeys = static_cast<uint32_t>(samplers.weight->input->count);
// for Interpolation_CUBICSPLINE can have more outputs if (nullptr != samplers.weight) {
const unsigned int weightStride = (unsigned int)samplers.weight->output->count / anim->mNumKeys; float *times = nullptr;
const unsigned int numMorphs = (samplers.weight->interpolation == Interpolation_CUBICSPLINE) ? weightStride - 2 : weightStride; samplers.weight->input->ExtractData(times);
float *values = nullptr;
samplers.weight->output->ExtractData(values);
anim->mNumKeys = static_cast<uint32_t>(samplers.weight->input->count);
anim->mKeys = new aiMeshMorphKey[anim->mNumKeys]; // for Interpolation_CUBICSPLINE can have more outputs
unsigned int ii = (samplers.weight->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0; const unsigned int weightStride = (unsigned int)samplers.weight->output->count / anim->mNumKeys;
for (unsigned int i = 0u; i < anim->mNumKeys; ++i) { const unsigned int numMorphs = (samplers.weight->interpolation == Interpolation_CUBICSPLINE) ? weightStride - 2 : weightStride;
unsigned int k = weightStride * i + ii;
anim->mKeys[i].mTime = times[i] * kMillisecondsFromSeconds;
anim->mKeys[i].mNumValuesAndWeights = numMorphs;
anim->mKeys[i].mValues = new unsigned int[numMorphs];
anim->mKeys[i].mWeights = new double[numMorphs];
for (unsigned int j = 0u; j < numMorphs; ++j, ++k) { anim->mKeys = new aiMeshMorphKey[anim->mNumKeys];
anim->mKeys[i].mValues[j] = j; unsigned int ii = (samplers.weight->interpolation == Interpolation_CUBICSPLINE) ? 1 : 0;
anim->mKeys[i].mWeights[j] = (0.f > values[k]) ? 0.f : values[k]; for (unsigned int i = 0u; i < anim->mNumKeys; ++i) {
unsigned int k = weightStride * i + ii;
anim->mKeys[i].mTime = times[i] * kMillisecondsFromSeconds;
anim->mKeys[i].mNumValuesAndWeights = numMorphs;
anim->mKeys[i].mValues = new unsigned int[numMorphs];
anim->mKeys[i].mWeights = new double[numMorphs];
for (unsigned int j = 0u; j < numMorphs; ++j, ++k) {
anim->mKeys[i].mValues[j] = j;
anim->mKeys[i].mWeights[j] = (0.f > values[k]) ? 0.f : values[k];
}
} }
delete[] times;
delete[] values;
} }
delete[] times; return anim;
delete[] values; } catch (...) {
delete anim;
throw;
} }
return anim;
} }
std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation &anim) { std::unordered_map<unsigned int, AnimationSamplers> GatherSamplers(Animation &anim) {
std::unordered_map<unsigned int, AnimationSamplers> samplers; std::unordered_map<unsigned int, AnimationSamplers> samplers;
for (unsigned int c = 0; c < anim.channels.size(); ++c) { for (unsigned int c = 0; c < anim.channels.size(); ++c) {
Animation::Channel &channel = anim.channels[c]; Animation::Channel &channel = anim.channels[c];
if (channel.sampler >= static_cast<int>(anim.samplers.size())) { if (channel.sampler < 0 || channel.sampler >= static_cast<int>(anim.samplers.size())) {
continue; continue;
} }
@ -1224,10 +1256,13 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
} }
mScene->mAnimations = new aiAnimation *[numAnimations]; mScene->mAnimations = new aiAnimation *[numAnimations];
std::fill(mScene->mAnimations, mScene->mAnimations + numAnimations, nullptr);
for (unsigned int i = 0; i < numAnimations; ++i) { for (unsigned int i = 0; i < numAnimations; ++i) {
aiAnimation *ai_anim = mScene->mAnimations[i] = new aiAnimation();
Animation &anim = r.animations[i]; Animation &anim = r.animations[i];
aiAnimation *ai_anim = new aiAnimation();
ai_anim->mName = anim.name; ai_anim->mName = anim.name;
ai_anim->mDuration = 0; ai_anim->mDuration = 0;
ai_anim->mTicksPerSecond = 0; ai_anim->mTicksPerSecond = 0;
@ -1249,6 +1284,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
ai_anim->mNumChannels = numChannels; ai_anim->mNumChannels = numChannels;
if (ai_anim->mNumChannels > 0) { if (ai_anim->mNumChannels > 0) {
ai_anim->mChannels = new aiNodeAnim *[ai_anim->mNumChannels]; ai_anim->mChannels = new aiNodeAnim *[ai_anim->mNumChannels];
std::fill(ai_anim->mChannels, ai_anim->mChannels + ai_anim->mNumChannels, nullptr);
int j = 0; int j = 0;
for (auto &iter : samplers) { for (auto &iter : samplers) {
if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) { if ((nullptr != iter.second.rotation) || (nullptr != iter.second.scale) || (nullptr != iter.second.translation)) {
@ -1261,6 +1297,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
ai_anim->mNumMorphMeshChannels = numMorphMeshChannels; ai_anim->mNumMorphMeshChannels = numMorphMeshChannels;
if (ai_anim->mNumMorphMeshChannels > 0) { if (ai_anim->mNumMorphMeshChannels > 0) {
ai_anim->mMorphMeshChannels = new aiMeshMorphAnim *[ai_anim->mNumMorphMeshChannels]; ai_anim->mMorphMeshChannels = new aiMeshMorphAnim *[ai_anim->mNumMorphMeshChannels];
std::fill(ai_anim->mMorphMeshChannels, ai_anim->mMorphMeshChannels + ai_anim->mNumMorphMeshChannels, nullptr);
int j = 0; int j = 0;
for (auto &iter : samplers) { for (auto &iter : samplers) {
if (nullptr != iter.second.weight) { if (nullptr != iter.second.weight) {
@ -1312,8 +1349,6 @@ void glTF2Importer::ImportAnimations(glTF2::Asset &r) {
ai_anim->mDuration = maxDuration; ai_anim->mDuration = maxDuration;
ai_anim->mTicksPerSecond = 1000.0; ai_anim->mTicksPerSecond = 1000.0;
mScene->mAnimations[i] = ai_anim;
} }
} }
@ -1333,6 +1368,7 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
ASSIMP_LOG_DEBUG_F("Importing ", numEmbeddedTexs, " embedded textures"); ASSIMP_LOG_DEBUG_F("Importing ", numEmbeddedTexs, " embedded textures");
mScene->mTextures = new aiTexture *[numEmbeddedTexs]; mScene->mTextures = new aiTexture *[numEmbeddedTexs];
std::fill(mScene->mTextures, mScene->mTextures + numEmbeddedTexs, nullptr);
// Add the embedded textures // Add the embedded textures
for (size_t i = 0; i < r.images.Size(); ++i) { for (size_t i = 0; i < r.images.Size(); ++i) {

View File

@ -1015,7 +1015,7 @@ ENDIF()
# RT-extensions is used in "contrib/Open3DGC/o3dgcTimer.h" for collecting statistics. Pointed file # RT-extensions is used in "contrib/Open3DGC/o3dgcTimer.h" for collecting statistics. Pointed file
# has implementation for different platforms: WIN32, __MACH__ and other ("else" block). # has implementation for different platforms: WIN32, __MACH__ and other ("else" block).
FIND_PACKAGE(RT QUIET) FIND_PACKAGE(RT QUIET)
IF (NOT ASSIMP_HUNTER_ENABLED AND (RT_FOUND OR MSVC)) IF (NOT ASSIMP_HUNTER_ENABLED AND (RT_FOUND OR WIN32))
SET( ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC 1 ) SET( ASSIMP_IMPORTER_GLTF_USE_OPEN3DGC 1 )
ADD_DEFINITIONS( -DASSIMP_IMPORTER_GLTF_USE_OPEN3DGC=1 ) ADD_DEFINITIONS( -DASSIMP_IMPORTER_GLTF_USE_OPEN3DGC=1 )
ELSE () ELSE ()

View File

@ -183,9 +183,10 @@ void SceneCombiner::MergeScenes(aiScene **_dest, std::vector<aiScene *> &src, un
*_dest = src[0]; *_dest = src[0];
return; return;
} }
if (*_dest) if (*_dest) {
(*_dest)->~aiScene(); (*_dest)->~aiScene();
else new (*_dest) aiScene();
} else
*_dest = new aiScene(); *_dest = new aiScene();
// Create a dummy scene to serve as master for the others // Create a dummy scene to serve as master for the others

View File

@ -146,7 +146,7 @@ int IOSystem2Unzip::testerror(voidpf /*opaque*/, voidpf /*stream*/) {
zlib_filefunc_def IOSystem2Unzip::get(IOSystem *pIOHandler) { zlib_filefunc_def IOSystem2Unzip::get(IOSystem *pIOHandler) {
zlib_filefunc_def mapping; zlib_filefunc_def mapping;
#ifdef ASSIMP_USE_HUNTER #if defined (ASSIMP_USE_HUNTER) || defined (__MINGW32__) // GH#3144
mapping.zopen_file = (open_file_func)open; mapping.zopen_file = (open_file_func)open;
mapping.zread_file = (read_file_func)read; mapping.zread_file = (read_file_func)read;
mapping.zwrite_file = (write_file_func)write; mapping.zwrite_file = (write_file_func)write;
@ -335,7 +335,7 @@ ZipArchiveIOSystem::Implement::Implement(IOSystem *pIOHandler, const char *pFile
if (pFilename[0] == 0 || nullptr == pMode) { if (pFilename[0] == 0 || nullptr == pMode) {
return; return;
} }
zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler); zlib_filefunc_def mapping = IOSystem2Unzip::get(pIOHandler);
m_ZipFileHandle = unzOpen2(pFilename, &mapping); m_ZipFileHandle = unzOpen2(pFilename, &mapping);
} }

View File

@ -175,7 +175,7 @@ namespace o3dgc
m_data[2] = rhs.m_data[2]; m_data[2] = rhs.m_data[2];
} }
template <typename T> template <typename T>
inline Vec3<T>::~Vec3(void){}; inline Vec3<T>::~Vec3(void){}
template <typename T> template <typename T>
inline Vec3<T>::Vec3() {} inline Vec3<T>::Vec3() {}
@ -308,7 +308,7 @@ namespace o3dgc
m_data[1] = rhs.m_data[1]; m_data[1] = rhs.m_data[1];
} }
template <typename T> template <typename T>
inline Vec2<T>::~Vec2(void){}; inline Vec2<T>::~Vec2(void){}
template <typename T> template <typename T>
inline Vec2<T>::Vec2() {} inline Vec2<T>::Vec2() {}

View File

@ -254,25 +254,16 @@ bool IOStreamBuffer<T>::getNextDataLine( std::vector<T> &buffer, T continuationT
} }
} }
bool continuationFound( false );
size_t i = 0; size_t i = 0;
for( ;; ) { for( ;; ) {
if ( continuationToken == m_cache[ m_cachePos ] ) { if ( continuationToken == m_cache[ m_cachePos ] && IsLineEnd( m_cache[ m_cachePos + 1 ] ) ) {
continuationFound = true;
++m_cachePos; ++m_cachePos;
} while ( m_cache[ m_cachePos ] != '\n' ) {
if ( IsLineEnd( m_cache[ m_cachePos ] ) ) {
if ( !continuationFound ) {
// the end of the data line
break;
} else {
// skip line end
while ( m_cache[m_cachePos] != '\n') {
++m_cachePos;
}
++m_cachePos; ++m_cachePos;
continuationFound = false;
} }
++m_cachePos;
} else if ( IsLineEnd ( m_cache[ m_cachePos ] ) ) {
break;
} }
buffer[ i ] = m_cache[ m_cachePos ]; buffer[ i ] = m_cache[ m_cachePos ];

View File

@ -763,7 +763,7 @@ void cleanup()
if (g_hWnd) if (g_hWnd)
KillGLWindow(); KillGLWindow();
}; }
LRESULT CALLBACK WndProc(HWND hWnd, // Handles for this Window LRESULT CALLBACK WndProc(HWND hWnd, // Handles for this Window
UINT uMsg, // Message for this Window UINT uMsg, // Message for this Window

View File

@ -161,4 +161,4 @@ int CMeshRenderer::DrawSorted(unsigned int iIndex,const aiMatrix4x4& mWorld) {
return 1; return 1;
} }
}; }

View File

@ -2143,7 +2143,7 @@ INT_PTR CALLBACK AboutMessageProc(HWND hwndDlg,UINT uMsg,
} }
return FALSE; return FALSE;
} }
}; }
using namespace AssimpView; using namespace AssimpView;