possibility to load gltf in a single layer

This commit is contained in:
alemuntoni 2021-07-01 11:24:03 +02:00
parent b506d83422
commit 1d588e2aaa
4 changed files with 79 additions and 8 deletions

View File

@ -66,6 +66,7 @@ void loadMeshes(
const std::list<MeshModel*>& meshModelList,
std::list<int>& 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<int>::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)

View File

@ -38,6 +38,7 @@ void loadMeshes(
const std::list<MeshModel*>& meshModelList,
std::list<int>& maskList,
const tinygltf::Model& model,
bool loadInSingleLayer,
vcg::CallBackPos* cb = nullptr);
namespace internal {
@ -55,6 +56,7 @@ void loadMeshesWhileTraversingNodes(
std::list<int>::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);

View File

@ -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);

View File

@ -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,