khanat-opennel-code/code/nel/tools/3d/mesh_utils/mesh_utils.cpp

274 lines
8.6 KiB
C++
Raw Normal View History

2015-09-19 15:14:35 +00:00
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
// Copyright (C) 2015 Winch Gate Property Limited
// Author: Jan Boon <jan.boon@kaetemi.be>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-09-19 16:08:25 +00:00
#include <nel/misc/types_nl.h>
#include "mesh_utils.h"
2015-09-19 17:35:57 +00:00
#include <nel/misc/debug.h>
2015-09-20 12:08:38 +00:00
#include <nel/misc/tool_logger.h>
#include <nel/misc/sstring.h>
2015-09-19 17:35:57 +00:00
#include "database_config.h"
2015-09-20 14:44:56 +00:00
#include "scene_meta.h"
2015-09-19 17:35:57 +00:00
#include <assimp/postprocess.h>
#include <assimp/scene.h>
#include <assimp/Importer.hpp>
2015-09-20 15:22:24 +00:00
#define NL_NODE_INTERNAL_TYPE aiNode
#define NL_SCENE_INTERNAL_TYPE aiScene
#include "scene_context.h"
#include "assimp_shape.h"
2015-09-19 17:35:57 +00:00
CMeshUtilsSettings::CMeshUtilsSettings()
{
/*ShapeDirectory = "shape";
2015-09-19 17:35:57 +00:00
IGDirectory = "ig";
SkelDirectory = "skel";*/
2015-09-19 17:35:57 +00:00
}
2015-09-20 15:22:24 +00:00
void importShapes(CMeshUtilsContext &context, const aiNode *node)
2015-09-19 17:35:57 +00:00
{
2015-09-20 15:22:24 +00:00
if (node != context.InternalScene->mRootNode)
2015-09-19 17:35:57 +00:00
{
2015-09-20 15:22:24 +00:00
CNodeContext &nodeContext = context.Nodes[node->mName.C_Str()];
CNodeMeta &nodeMeta = context.SceneMeta.Nodes[node->mName.C_Str()];
if (nodeMeta.ExportMesh == TMeshShape)
{
if (node->mNumMeshes)
{
nldebug("Shape '%s' found containing '%u' meshes", node->mName.C_Str(), node->mNumMeshes);
assimpShape(context, nodeContext);
}
}
2015-09-19 17:35:57 +00:00
}
for (unsigned int i = 0; i < node->mNumChildren; ++i)
2015-09-20 15:22:24 +00:00
importShapes(context, node->mChildren[i]);
2015-09-19 17:35:57 +00:00
}
2015-09-20 15:22:24 +00:00
void validateInternalNodeNames(CMeshUtilsContext &context, const aiNode *node)
2015-09-19 17:35:57 +00:00
{
2015-09-20 15:22:24 +00:00
if (!node->mParent || node == context.InternalScene->mRootNode)
2015-09-19 17:35:57 +00:00
{
// do nothing
}
else if (node->mName.length == 0)
{
2015-09-20 12:08:38 +00:00
tlwarning(context.ToolLogger, context.Settings.SourceFilePath.c_str(),
"Node has no name");
2015-09-19 17:35:57 +00:00
}
else
{
CNodeContext &nodeContext = context.Nodes[node->mName.C_Str()];
2015-09-20 15:22:24 +00:00
if (nodeContext.InternalNode && nodeContext.InternalNode != node)
2015-09-19 17:35:57 +00:00
{
2015-09-20 12:08:38 +00:00
tlerror(context.ToolLogger, context.Settings.SourceFilePath.c_str(),
"Node name '%s' appears multiple times", node->mName.C_Str());
2015-09-19 17:35:57 +00:00
}
else
{
2015-09-20 15:22:24 +00:00
nodeContext.InternalNode = node;
2015-09-19 17:35:57 +00:00
}
}
for (unsigned int i = 0; i < node->mNumChildren; ++i)
2015-09-20 15:22:24 +00:00
validateInternalNodeNames(context, node->mChildren[i]);
2015-09-19 17:35:57 +00:00
}
void flagAssimpBones(CMeshUtilsContext &context)
{
// Find out which nodes are bones by checking the mesh meta info
2015-09-20 15:22:24 +00:00
const aiScene *scene = context.InternalScene;
2015-09-19 17:35:57 +00:00
for (unsigned int i = 0; i < scene->mNumMeshes; ++i)
{
// nldebug("FOUND MESH '%s'\n", scene->mMeshes[i]->mName.C_Str());
const aiMesh *mesh = scene->mMeshes[i];
for (unsigned int j = 0; j < mesh->mNumBones; ++j)
{
CNodeContext &nodeContext = context.Nodes[mesh->mBones[j]->mName.C_Str()];
2015-09-20 15:22:24 +00:00
if (!nodeContext.InternalNode)
2015-09-19 17:35:57 +00:00
{
2015-09-20 12:08:38 +00:00
tlerror(context.ToolLogger, context.Settings.SourceFilePath.c_str(),
"Bone '%s' has no associated node", mesh->mBones[j]->mName.C_Str());
2015-09-19 17:35:57 +00:00
}
else
{
// Flag as bone
nodeContext.IsBone = true;
// Flag all parents as bones
2015-09-20 15:22:24 +00:00
/*const aiNode *parent = nodeContext.InternalNode;
2015-09-19 17:35:57 +00:00
while (parent = parent->mParent) if (parent->mName.length)
{
context.Nodes[parent->mName.C_Str()].IsBone = true;
2015-09-20 12:08:38 +00:00
}*/
2015-09-19 17:35:57 +00:00
}
}
}
2015-09-20 15:03:44 +00:00
// Find out which nodes are bones by checking the animation info
// TODO
2015-09-19 17:35:57 +00:00
}
2015-09-20 15:03:44 +00:00
void flagRecursiveBones(CMeshUtilsContext &context, CNodeContext &nodeContext, bool autoStop = false)
2015-09-20 14:44:56 +00:00
{
nodeContext.IsBone = true;
2015-09-20 15:22:24 +00:00
const aiNode *node = nodeContext.InternalNode;
2015-09-20 14:44:56 +00:00
nlassert(node);
for (unsigned int i = 0; i < node->mNumChildren; ++i)
2015-09-20 15:03:44 +00:00
{
CNodeContext &ctx = context.Nodes[node->mName.C_Str()];
if (autoStop && ctx.IsBone)
continue;
flagRecursiveBones(context, ctx);
}
2015-09-20 14:44:56 +00:00
}
void flagMetaBones(CMeshUtilsContext &context)
{
for (TNodeContextMap::iterator it(context.Nodes.begin()), end(context.Nodes.end()); it != end; ++it)
{
CNodeContext &ctx = it->second;
CNodeMeta &meta = context.SceneMeta.Nodes[it->first];
if (meta.ExportBone == TBoneForce)
ctx.IsBone = true;
else if (meta.ExportBone == TBoneRoot)
flagRecursiveBones(context, ctx);
}
}
void flagLocalParentBones(CMeshUtilsContext &context, CNodeContext &nodeContext)
{
2015-09-20 15:22:24 +00:00
const aiNode *node = nodeContext.InternalNode;
2015-09-20 14:44:56 +00:00
}
2015-09-20 15:03:44 +00:00
void flagAllParentBones(CMeshUtilsContext &context, CNodeContext &nodeContext, bool autoStop = false)
2015-09-20 14:44:56 +00:00
{
2015-09-20 15:22:24 +00:00
const aiNode *parent = nodeContext.InternalNode;
while (parent = parent->mParent) if (parent->mName.length && parent != context.InternalScene->mRootNode)
2015-09-20 15:03:44 +00:00
{
CNodeContext &ctx = context.Nodes[parent->mName.C_Str()];
if (autoStop && ctx.IsBone)
break;
ctx.IsBone = true;
}
}
bool hasIndirectParentBone(CMeshUtilsContext &context, CNodeContext &nodeContext)
{
2015-09-20 15:22:24 +00:00
const aiNode *parent = nodeContext.InternalNode;
while (parent = parent->mParent) if (parent->mName.length && parent != context.InternalScene->mRootNode)
2015-09-20 15:03:44 +00:00
if (context.Nodes[parent->mName.C_Str()].IsBone) return true;
return false;
2015-09-20 14:44:56 +00:00
}
void flagExpandedBones(CMeshUtilsContext &context)
{
switch (context.SceneMeta.SkeletonMode)
{
case TSkelLocal:
for (TNodeContextMap::iterator it(context.Nodes.begin()), end(context.Nodes.end()); it != end; ++it)
{
CNodeContext &nodeContext = it->second;
2015-09-20 15:03:44 +00:00
if (nodeContext.IsBone && hasIndirectParentBone(context, nodeContext))
flagAllParentBones(context, nodeContext, true);
2015-09-20 14:44:56 +00:00
}
break;
case TSkelRoot:
for (TNodeContextMap::iterator it(context.Nodes.begin()), end(context.Nodes.end()); it != end; ++it)
{
CNodeContext &nodeContext = it->second;
if (nodeContext.IsBone)
2015-09-20 15:03:44 +00:00
flagAllParentBones(context, nodeContext, true);
2015-09-20 14:44:56 +00:00
}
break;
case TSkelFull:
for (TNodeContextMap::iterator it(context.Nodes.begin()), end(context.Nodes.end()); it != end; ++it)
{
CNodeContext &nodeContext = it->second;
if (nodeContext.IsBone)
2015-09-20 15:03:44 +00:00
flagAllParentBones(context, nodeContext, true);
}
for (TNodeContextMap::iterator it(context.Nodes.begin()), end(context.Nodes.end()); it != end; ++it)
{
CNodeContext &nodeContext = it->second;
if (nodeContext.IsBone)
flagRecursiveBones(context, nodeContext, true);
2015-09-20 14:44:56 +00:00
}
break;
}
}
2015-09-19 17:35:57 +00:00
// TODO: Separate load scene and save scene functions
int exportScene(const CMeshUtilsSettings &settings)
{
2015-09-20 12:08:38 +00:00
CMeshUtilsContext context(settings);
NLMISC::CFile::createDirectoryTree(settings.DestinationDirectoryPath);
2015-09-20 12:08:38 +00:00
if (!settings.ToolDependLog.empty())
context.ToolLogger.initDepend(settings.ToolDependLog);
if (!settings.ToolErrorLog.empty())
context.ToolLogger.initError(settings.ToolErrorLog);
2015-09-20 12:08:38 +00:00
context.ToolLogger.writeDepend(NLMISC::BUILD, "*", context.Settings.SourceFilePath.c_str()); // Base input file
// Apply database configuration
CDatabaseConfig::init(settings.SourceFilePath);
CDatabaseConfig::initTextureSearchDirectories();
2015-09-20 12:08:38 +00:00
2015-09-19 17:35:57 +00:00
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(settings.SourceFilePath, aiProcess_Triangulate | aiProcess_ValidateDataStructure); // aiProcess_SplitLargeMeshes | aiProcess_LimitBoneWeights
2015-09-20 12:08:38 +00:00
if (!scene)
{
const char *errs = importer.GetErrorString();
if (errs) tlerror(context.ToolLogger, context.Settings.SourceFilePath.c_str(), "Assimp failed to load the scene: '%s'", errs);
2015-09-20 12:08:38 +00:00
else tlerror(context.ToolLogger, context.Settings.SourceFilePath.c_str(), "Unable to load scene");
return EXIT_FAILURE;
}
// aiProcess_Triangulate
2015-09-19 17:35:57 +00:00
// aiProcess_ValidateDataStructure: TODO: Catch Assimp error output stream
// aiProcess_RemoveRedundantMaterials: Not used because we may override materials with NeL Material from meta
// aiProcess_ImproveCacheLocality: TODO: Verify this does not modify vertex indices
//scene->mRootNode->mMetaData
2015-09-20 15:22:24 +00:00
context.InternalScene = scene;
2015-09-20 14:44:56 +00:00
if (context.SceneMeta.load(context.Settings.SourceFilePath))
context.ToolLogger.writeDepend(NLMISC::BUILD, "*", context.SceneMeta.metaFilePath().c_str()); // Meta input file
2015-09-19 17:35:57 +00:00
2015-09-20 15:22:24 +00:00
validateInternalNodeNames(context, context.InternalScene->mRootNode);
2015-09-20 15:03:44 +00:00
// -- SKEL FLAG --
2015-09-19 17:35:57 +00:00
flagAssimpBones(context);
2015-09-20 14:44:56 +00:00
flagMetaBones(context);
flagExpandedBones(context);
2015-09-20 15:03:44 +00:00
// TODO
// [
// Only necessary in TSkelLocal
// For each shape test if all the bones have the same root bones for their skeleton
// 1) Iterate each until a different is found
// 2) When a different root is found, connect the two to the nearest common bone
// ]
// -- SKEL FLAG --
2015-09-19 17:35:57 +00:00
2015-09-20 15:22:24 +00:00
importShapes(context, context.InternalScene->mRootNode);
2015-09-19 17:35:57 +00:00
2015-09-19 16:08:25 +00:00
return EXIT_SUCCESS;
2015-09-19 15:14:35 +00:00
}
/* end of file */