From 1d588e2aaa862c9fc668d8eac1f5834e27e96619 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Thu, 1 Jul 2021 11:24:03 +0200 Subject: [PATCH] possibility to load gltf in a single layer --- src/meshlabplugins/io_gltf/gltf_loader.cpp | 55 +++++++++++++++++++--- src/meshlabplugins/io_gltf/gltf_loader.h | 6 +++ src/meshlabplugins/io_gltf/io_gltf.cpp | 23 ++++++++- src/meshlabplugins/io_gltf/io_gltf.h | 3 ++ 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/meshlabplugins/io_gltf/gltf_loader.cpp b/src/meshlabplugins/io_gltf/gltf_loader.cpp index ecc2be994..f81d2de59 100644 --- a/src/meshlabplugins/io_gltf/gltf_loader.cpp +++ b/src/meshlabplugins/io_gltf/gltf_loader.cpp @@ -66,6 +66,7 @@ void loadMeshes( const std::list& meshModelList, std::list& maskList, const tinygltf::Model& model, + bool loadInSingleLayer, vcg::CallBackPos* cb) { CallBackProgress progress(100.0/meshModelList.size()); @@ -81,6 +82,7 @@ void loadMeshes( maskit, Matrix44m::Identity(), scene.nodes[n], + loadInSingleLayer, cb, progress); } @@ -135,6 +137,7 @@ void loadMeshesWhileTraversingNodes( std::list::iterator& currentMask, Matrix44m currentMatrix, unsigned int currentNode, + bool loadInSingleLayer, vcg::CallBackPos* cb, CallBackProgress& progress) { @@ -142,10 +145,20 @@ void loadMeshesWhileTraversingNodes( if (model.nodes[currentNode].mesh >= 0) { int meshid = model.nodes[currentNode].mesh; - loadMesh(**currentMesh, *currentMask, model.meshes[meshid], model, cb, progress); - (*currentMesh)->cm.Tr = currentMatrix; - ++currentMesh; - ++currentMask; + loadMesh( + **currentMesh, + *currentMask, + model.meshes[meshid], + model, + loadInSingleLayer, + currentMatrix, + cb, + progress); + if (!loadInSingleLayer) { + (*currentMesh)->cm.Tr = currentMatrix; + ++currentMesh; + ++currentMask; + } } //for each child @@ -158,6 +171,7 @@ void loadMeshesWhileTraversingNodes( currentMask, currentMatrix, c, + loadInSingleLayer, cb, progress); } @@ -238,6 +252,8 @@ void loadMesh( int& mask, const tinygltf::Mesh& tm, const tinygltf::Model& model, + bool loadInSingleLayer, + const Matrix44m& transf, vcg::CallBackPos* cb, CallBackProgress& progress) { @@ -249,7 +265,15 @@ void loadMesh( //for each primitive, load it into the mesh for (const tinygltf::Primitive& p : tm.primitives){ - internal::loadMeshPrimitive(m, mask, model, p, cb, progress); + internal::loadMeshPrimitive( + m, + mask, + model, + p, + loadInSingleLayer, + transf, + cb, + progress); } if (cb) cb(progress.progress(), "Loaded all primitives for current mesh."); @@ -267,6 +291,8 @@ void loadMeshPrimitive( int& mask, const tinygltf::Model& model, const tinygltf::Primitive& p, + bool loadInSingleLayer, + const Matrix44m& transf, vcg::CallBackPos* cb, CallBackProgress& progress) { @@ -312,6 +338,14 @@ void loadMeshPrimitive( loadAttribute(m, ivp, model, p, POSITION); progress.increment(); + //if all the meshes are loaded in a single layer, I need to apply + //the transformation matrix to the loaded coordinates + if (loadInSingleLayer){ + for (CMeshO::VertexPointer p : ivp){ + p->P() = transf * p->P(); + } + } + //if the mesh has a base color, set it to vertex colors if (vCol) { mask |= vcg::tri::io::Mask::IOM_VERTCOLOR; @@ -324,8 +358,17 @@ void loadMeshPrimitive( cb(progress.progress(), "Loading vertex normals"); //load all the other vertex attributes (ivp is not modified by these calls) bool res = loadAttribute(m, ivp, model, p, NORMAL); - if (res) + if (res) { mask |= vcg::tri::io::Mask::IOM_VERTNORMAL; + //if all the meshes are loaded in a single layer, I need to apply + //the transformation matrix to the loaded coordinates + if (loadInSingleLayer){ + Matrix33m mat33(transf,3); + for (CMeshO::VertexPointer p : ivp){ + p->N() = mat33 * p->N(); + } + } + } progress.increment(); if (cb) diff --git a/src/meshlabplugins/io_gltf/gltf_loader.h b/src/meshlabplugins/io_gltf/gltf_loader.h index 809220256..b61fde324 100644 --- a/src/meshlabplugins/io_gltf/gltf_loader.h +++ b/src/meshlabplugins/io_gltf/gltf_loader.h @@ -38,6 +38,7 @@ void loadMeshes( const std::list& meshModelList, std::list& maskList, const tinygltf::Model& model, + bool loadInSingleLayer, vcg::CallBackPos* cb = nullptr); namespace internal { @@ -55,6 +56,7 @@ void loadMeshesWhileTraversingNodes( std::list::iterator& currentMask, Matrix44m currentMatrix, unsigned int currentNode, + bool loadInSingleLayer, vcg::CallBackPos* cb, CallBackProgress& progress); @@ -67,6 +69,8 @@ void loadMesh( int& mask, const tinygltf::Mesh& tm, const tinygltf::Model& model, + bool loadInSingleLayer, + const Matrix44m& transf, vcg::CallBackPos* cb, CallBackProgress& progress); @@ -75,6 +79,8 @@ void loadMeshPrimitive( int& mask, const tinygltf::Model& model, const tinygltf::Primitive& p, + bool loadInSingleLayer, + const Matrix44m& transf, vcg::CallBackPos* cb, CallBackProgress& progress); diff --git a/src/meshlabplugins/io_gltf/io_gltf.cpp b/src/meshlabplugins/io_gltf/io_gltf.cpp index aa7148828..389e6e099 100644 --- a/src/meshlabplugins/io_gltf/io_gltf.cpp +++ b/src/meshlabplugins/io_gltf/io_gltf.cpp @@ -58,12 +58,29 @@ void IOglTFPlugin::exportMaskCapability( return; } +RichParameterList IOglTFPlugin::initPreOpenParameter( + const QString& format) const +{ + RichParameterList parameters; + if(format.toUpper() == tr("GLTF")) + parameters.addParam(RichBool( + "load_in_a_single_layer", false, "Load in a single layer", + "GLTF files may contain more than one mesh. If this parameter is " + "set to false, all the meshes contained in the file will be " + "merged in a single mesh.")); + return parameters; +} + unsigned int IOglTFPlugin::numberMeshesContainedInFile( const QString& format, const QString& fileName, - const RichParameterList&) const + const RichParameterList& parameters) const { if (format.toUpper() == "GLTF"){ + if (parameters.getBool("load_in_a_single_layer")){ + //all the meshes loaded from the file must be placed in a single layer + return 1; + } tinygltf::Model model; tinygltf::TinyGLTF loader; std::string err; @@ -91,6 +108,8 @@ void IOglTFPlugin::open( vcg::CallBackPos* cb) { if (fileFormat.toUpper() == "GLTF"){ + bool loadInSingleLayer = params.getBool("load_in_a_single_layer"); + tinygltf::Model model; tinygltf::TinyGLTF loader; std::string err; @@ -102,7 +121,7 @@ void IOglTFPlugin::open( if (!warn.empty()) reportWarning(QString::fromStdString(warn)); - gltf::loadMeshes(meshModelList, maskList, model, cb); + gltf::loadMeshes(meshModelList, maskList, model, loadInSingleLayer, cb); } else { wrongOpenFormat(fileFormat); diff --git a/src/meshlabplugins/io_gltf/io_gltf.h b/src/meshlabplugins/io_gltf/io_gltf.h index d16e62ecc..94d033299 100644 --- a/src/meshlabplugins/io_gltf/io_gltf.h +++ b/src/meshlabplugins/io_gltf/io_gltf.h @@ -44,6 +44,9 @@ public: int& capability, int& defaultBits) const; + RichParameterList initPreOpenParameter( + const QString& format) const; + unsigned int numberMeshesContainedInFile( const QString& format, const QString& fileName,