Merge branch 'master' into collada_modeller_metadata

pull/2820/head
Kim Kulling 2019-12-13 08:28:50 +01:00 committed by GitHub
commit 9ef2d7fb7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 1004 additions and 827 deletions

View File

@ -2089,6 +2089,13 @@ namespace Assimp {
TrySetTextureProperties(out_mat, textures, "Maya|TEX_metallic_map|file", aiTextureType_METALNESS, mesh); TrySetTextureProperties(out_mat, textures, "Maya|TEX_metallic_map|file", aiTextureType_METALNESS, mesh);
TrySetTextureProperties(out_mat, textures, "Maya|TEX_roughness_map|file", aiTextureType_DIFFUSE_ROUGHNESS, mesh); TrySetTextureProperties(out_mat, textures, "Maya|TEX_roughness_map|file", aiTextureType_DIFFUSE_ROUGHNESS, mesh);
TrySetTextureProperties(out_mat, textures, "Maya|TEX_ao_map|file", aiTextureType_AMBIENT_OCCLUSION, mesh); TrySetTextureProperties(out_mat, textures, "Maya|TEX_ao_map|file", aiTextureType_AMBIENT_OCCLUSION, mesh);
// 3DSMax PBR
TrySetTextureProperties(out_mat, textures, "3dsMax|Parameters|base_color_map", aiTextureType_BASE_COLOR, mesh);
TrySetTextureProperties(out_mat, textures, "3dsMax|Parameters|bump_map", aiTextureType_NORMAL_CAMERA, mesh);
TrySetTextureProperties(out_mat, textures, "3dsMax|Parameters|emission_map", aiTextureType_EMISSION_COLOR, mesh);
TrySetTextureProperties(out_mat, textures, "3dsMax|Parameters|metalness_map", aiTextureType_METALNESS, mesh);
TrySetTextureProperties(out_mat, textures, "3dsMax|Parameters|roughness_map", aiTextureType_DIFFUSE_ROUGHNESS, mesh);
} }
void FBXConverter::SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const MeshGeometry* const mesh) void FBXConverter::SetTextureProperties(aiMaterial* out_mat, const LayeredTextureMap& layeredTextures, const MeshGeometry* const mesh)

View File

@ -137,8 +137,9 @@ public:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
void Execute( aiScene* pScene); void Execute( aiScene* pScene);
protected: public:
void ProcessMesh( aiMesh* pMesh); /** Some other types of post-processing require winding order flips */
static void ProcessMesh( aiMesh* pMesh);
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View File

@ -43,13 +43,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @brief Implementation of the aiProcess_OptimizGraph step * @brief Implementation of the aiProcess_OptimizGraph step
*/ */
#ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS #ifndef ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS
#include "OptimizeGraph.h" #include "OptimizeGraph.h"
#include "ProcessHelper.h" #include "ProcessHelper.h"
#include <assimp/SceneCombiner.h> #include "ConvertToLHProcess.h"
#include <assimp/Exceptional.h> #include <assimp/Exceptional.h>
#include <assimp/SceneCombiner.h>
#include <stdio.h> #include <stdio.h>
using namespace Assimp; using namespace Assimp;
@ -60,21 +60,21 @@ using namespace Assimp;
* The unhashed variant should be faster, except for *very* large data sets * The unhashed variant should be faster, except for *very* large data sets
*/ */
#ifdef AI_OG_USE_HASHING #ifdef AI_OG_USE_HASHING
// Use our standard hashing function to compute the hash // Use our standard hashing function to compute the hash
# define AI_OG_GETKEY(str) SuperFastHash(str.data,str.length) #define AI_OG_GETKEY(str) SuperFastHash(str.data, str.length)
#else #else
// Otherwise hope that std::string will utilize a static buffer // Otherwise hope that std::string will utilize a static buffer
// for shorter node names. This would avoid endless heap copying. // for shorter node names. This would avoid endless heap copying.
# define AI_OG_GETKEY(str) std::string(str.data) #define AI_OG_GETKEY(str) std::string(str.data)
#endif #endif
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer // Constructor to be privately used by Importer
OptimizeGraphProcess::OptimizeGraphProcess() OptimizeGraphProcess::OptimizeGraphProcess() :
: mScene() mScene(),
, nodes_in() nodes_in(),
, nodes_out() nodes_out(),
, count_merged() { count_merged() {
// empty // empty
} }
@ -86,33 +86,33 @@ OptimizeGraphProcess::~OptimizeGraphProcess() {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns whether the processing step is present in the given flag field. // Returns whether the processing step is present in the given flag field.
bool OptimizeGraphProcess::IsActive( unsigned int pFlags) const { bool OptimizeGraphProcess::IsActive(unsigned int pFlags) const {
return (0 != (pFlags & aiProcess_OptimizeGraph)); return (0 != (pFlags & aiProcess_OptimizeGraph));
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Setup properties for the post-processing step // Setup properties for the post-processing step
void OptimizeGraphProcess::SetupProperties(const Importer* pImp) { void OptimizeGraphProcess::SetupProperties(const Importer *pImp) {
// Get value of AI_CONFIG_PP_OG_EXCLUDE_LIST // Get value of AI_CONFIG_PP_OG_EXCLUDE_LIST
std::string tmp = pImp->GetPropertyString(AI_CONFIG_PP_OG_EXCLUDE_LIST,""); std::string tmp = pImp->GetPropertyString(AI_CONFIG_PP_OG_EXCLUDE_LIST, "");
AddLockedNodeList(tmp); AddLockedNodeList(tmp);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Collect new children // Collect new children
void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& nodes) { void OptimizeGraphProcess::CollectNewChildren(aiNode *nd, std::list<aiNode *> &nodes) {
nodes_in += nd->mNumChildren; nodes_in += nd->mNumChildren;
// Process children // Process children
std::list<aiNode*> child_nodes; std::list<aiNode *> child_nodes;
for (unsigned int i = 0; i < nd->mNumChildren; ++i) { for (unsigned int i = 0; i < nd->mNumChildren; ++i) {
CollectNewChildren(nd->mChildren[i],child_nodes); CollectNewChildren(nd->mChildren[i], child_nodes);
nd->mChildren[i] = nullptr; nd->mChildren[i] = nullptr;
} }
// Check whether we need this node; if not we can replace it by our own children (warn, danger of incest). // Check whether we need this node; if not we can replace it by our own children (warn, danger of incest).
if (locked.find(AI_OG_GETKEY(nd->mName)) == locked.end() ) { if (locked.find(AI_OG_GETKEY(nd->mName)) == locked.end()) {
for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) { for (std::list<aiNode *>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
if (locked.find(AI_OG_GETKEY((*it)->mName)) == locked.end()) { if (locked.find(AI_OG_GETKEY((*it)->mName)) == locked.end()) {
(*it)->mTransformation = nd->mTransformation * (*it)->mTransformation; (*it)->mTransformation = nd->mTransformation * (*it)->mTransformation;
@ -136,19 +136,19 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
nodes.push_back(nd); nodes.push_back(nd);
// Now check for possible optimizations in our list of child nodes. join as many as possible // Now check for possible optimizations in our list of child nodes. join as many as possible
aiNode* join_master = NULL; aiNode *join_master = nullptr;
aiMatrix4x4 inv; aiMatrix4x4 inv;
const LockedSetType::const_iterator end = locked.end(); const LockedSetType::const_iterator end = locked.end();
std::list<aiNode*> join; std::list<aiNode *> join;
for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end();) { for (std::list<aiNode *>::iterator it = child_nodes.begin(); it != child_nodes.end();) {
aiNode* child = *it; aiNode *child = *it;
if (child->mNumChildren == 0 && locked.find(AI_OG_GETKEY(child->mName)) == end) { if (child->mNumChildren == 0 && locked.find(AI_OG_GETKEY(child->mName)) == end) {
// There may be no instanced meshes // There may be no instanced meshes
unsigned int n = 0; unsigned int n = 0;
for (; n < child->mNumMeshes;++n) { for (; n < child->mNumMeshes; ++n) {
if (meshes[child->mMeshes[n]] > 1) { if (meshes[child->mMeshes[n]] > 1) {
break; break;
} }
@ -159,7 +159,7 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
inv = join_master->mTransformation; inv = join_master->mTransformation;
inv.Inverse(); inv.Inverse();
} else { } else {
child->mTransformation = inv * child->mTransformation ; child->mTransformation = inv * child->mTransformation;
join.push_back(child); join.push_back(child);
it = child_nodes.erase(it); it = child_nodes.erase(it);
@ -170,31 +170,40 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
++it; ++it;
} }
if (join_master && !join.empty()) { if (join_master && !join.empty()) {
join_master->mName.length = ::ai_snprintf(join_master->mName.data, MAXLEN, "$MergedNode_%i",count_merged++); join_master->mName.length = ::ai_snprintf(join_master->mName.data, MAXLEN, "$MergedNode_%i", count_merged++);
unsigned int out_meshes = 0; unsigned int out_meshes = 0;
for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) { for (std::list<aiNode *>::const_iterator it = join.cbegin(); it != join.cend(); ++it) {
out_meshes += (*it)->mNumMeshes; out_meshes += (*it)->mNumMeshes;
} }
// copy all mesh references in one array // copy all mesh references in one array
if (out_meshes) { if (out_meshes) {
unsigned int* meshes = new unsigned int[out_meshes+join_master->mNumMeshes], *tmp = meshes; unsigned int *meshes = new unsigned int[out_meshes + join_master->mNumMeshes], *tmp = meshes;
for (unsigned int n = 0; n < join_master->mNumMeshes;++n) { for (unsigned int n = 0; n < join_master->mNumMeshes; ++n) {
*tmp++ = join_master->mMeshes[n]; *tmp++ = join_master->mMeshes[n];
} }
for (std::list<aiNode*>::iterator it = join.begin(); it != join.end(); ++it) { for (const aiNode *join_node : join) {
for (unsigned int n = 0; n < (*it)->mNumMeshes; ++n) { for (unsigned int n = 0; n < join_node->mNumMeshes; ++n) {
*tmp = (*it)->mMeshes[n]; *tmp = join_node->mMeshes[n];
aiMesh* mesh = mScene->mMeshes[*tmp++]; aiMesh *mesh = mScene->mMeshes[*tmp++];
// Assume the transformation is affine
// manually move the mesh into the right coordinate system // manually move the mesh into the right coordinate system
const aiMatrix3x3 IT = aiMatrix3x3( (*it)->mTransformation ).Inverse().Transpose();
// Check for odd negative scale (mirror)
if (join_node->mTransformation.Determinant() < 0) {
// Reverse the mesh face winding order
FlipWindingOrderProcess::ProcessMesh(mesh);
}
// Update positions, normals and tangents
const aiMatrix3x3 IT = aiMatrix3x3(join_node->mTransformation).Inverse().Transpose();
for (unsigned int a = 0; a < mesh->mNumVertices; ++a) { for (unsigned int a = 0; a < mesh->mNumVertices; ++a) {
mesh->mVertices[a] *= (*it)->mTransformation; mesh->mVertices[a] *= join_node->mTransformation;
if (mesh->HasNormals()) if (mesh->HasNormals())
mesh->mNormals[a] *= IT; mesh->mNormals[a] *= IT;
@ -205,7 +214,7 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
} }
} }
} }
delete *it; // bye, node delete join_node; // bye, node
} }
delete[] join_master->mMeshes; delete[] join_master->mMeshes;
join_master->mMeshes = meshes; join_master->mMeshes = meshes;
@ -219,17 +228,17 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
delete[] nd->mChildren; delete[] nd->mChildren;
if (!child_nodes.empty()) { if (!child_nodes.empty()) {
nd->mChildren = new aiNode*[child_nodes.size()]; nd->mChildren = new aiNode *[child_nodes.size()];
} } else
else nd->mChildren = nullptr; nd->mChildren = nullptr;
} }
nd->mNumChildren = static_cast<unsigned int>(child_nodes.size()); nd->mNumChildren = static_cast<unsigned int>(child_nodes.size());
if (nd->mChildren) { if (nd->mChildren) {
aiNode** tmp = nd->mChildren; aiNode **tmp = nd->mChildren;
for (std::list<aiNode*>::iterator it = child_nodes.begin(); it != child_nodes.end(); ++it) { for (std::list<aiNode *>::iterator it = child_nodes.begin(); it != child_nodes.end(); ++it) {
aiNode* node = *tmp++ = *it; aiNode *node = *tmp++ = *it;
node->mParent = nd; node->mParent = nd;
} }
} }
@ -239,12 +248,12 @@ void OptimizeGraphProcess::CollectNewChildren(aiNode* nd, std::list<aiNode*>& no
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Execute the post-processing step on the given scene // Execute the post-processing step on the given scene
void OptimizeGraphProcess::Execute( aiScene* pScene) { void OptimizeGraphProcess::Execute(aiScene *pScene) {
ASSIMP_LOG_DEBUG("OptimizeGraphProcess begin"); ASSIMP_LOG_DEBUG("OptimizeGraphProcess begin");
nodes_in = nodes_out = count_merged = 0; nodes_in = nodes_out = count_merged = 0;
mScene = pScene; mScene = pScene;
meshes.resize(pScene->mNumMeshes,0); meshes.resize(pScene->mNumMeshes, 0);
FindInstancedMeshes(pScene->mRootNode); FindInstancedMeshes(pScene->mRootNode);
// build a blacklist of identifiers. If the name of a node matches one of these, we won't touch it // build a blacklist of identifiers. If the name of a node matches one of these, we won't touch it
@ -259,7 +268,7 @@ void OptimizeGraphProcess::Execute( aiScene* pScene) {
for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) { for (unsigned int i = 0; i < pScene->mNumAnimations; ++i) {
for (unsigned int a = 0; a < pScene->mAnimations[i]->mNumChannels; ++a) { for (unsigned int a = 0; a < pScene->mAnimations[i]->mNumChannels; ++a) {
aiNodeAnim* anim = pScene->mAnimations[i]->mChannels[a]; aiNodeAnim *anim = pScene->mAnimations[i]->mChannels[a];
locked.insert(AI_OG_GETKEY(anim->mNodeName)); locked.insert(AI_OG_GETKEY(anim->mNodeName));
} }
} }
@ -267,7 +276,7 @@ void OptimizeGraphProcess::Execute( aiScene* pScene) {
for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
for (unsigned int a = 0; a < pScene->mMeshes[i]->mNumBones; ++a) { for (unsigned int a = 0; a < pScene->mMeshes[i]->mNumBones; ++a) {
aiBone* bone = pScene->mMeshes[i]->mBones[a]; aiBone *bone = pScene->mMeshes[i]->mBones[a];
locked.insert(AI_OG_GETKEY(bone->mName)); locked.insert(AI_OG_GETKEY(bone->mName));
// HACK: Meshes referencing bones may not be transformed; we need to look them. // HACK: Meshes referencing bones may not be transformed; we need to look them.
@ -277,35 +286,35 @@ void OptimizeGraphProcess::Execute( aiScene* pScene) {
} }
for (unsigned int i = 0; i < pScene->mNumCameras; ++i) { for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
aiCamera* cam = pScene->mCameras[i]; aiCamera *cam = pScene->mCameras[i];
locked.insert(AI_OG_GETKEY(cam->mName)); locked.insert(AI_OG_GETKEY(cam->mName));
} }
for (unsigned int i = 0; i < pScene->mNumLights; ++i) { for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
aiLight* lgh = pScene->mLights[i]; aiLight *lgh = pScene->mLights[i];
locked.insert(AI_OG_GETKEY(lgh->mName)); locked.insert(AI_OG_GETKEY(lgh->mName));
} }
// Insert a dummy master node and make it read-only // Insert a dummy master node and make it read-only
aiNode* dummy_root = new aiNode(AI_RESERVED_NODE_NAME); aiNode *dummy_root = new aiNode(AI_RESERVED_NODE_NAME);
locked.insert(AI_OG_GETKEY(dummy_root->mName)); locked.insert(AI_OG_GETKEY(dummy_root->mName));
const aiString prev = pScene->mRootNode->mName; const aiString prev = pScene->mRootNode->mName;
pScene->mRootNode->mParent = dummy_root; pScene->mRootNode->mParent = dummy_root;
dummy_root->mChildren = new aiNode*[dummy_root->mNumChildren = 1]; dummy_root->mChildren = new aiNode *[dummy_root->mNumChildren = 1];
dummy_root->mChildren[0] = pScene->mRootNode; dummy_root->mChildren[0] = pScene->mRootNode;
// Do our recursive processing of scenegraph nodes. For each node collect // Do our recursive processing of scenegraph nodes. For each node collect
// a fully new list of children and allow their children to place themselves // a fully new list of children and allow their children to place themselves
// on the same hierarchy layer as their parents. // on the same hierarchy layer as their parents.
std::list<aiNode*> nodes; std::list<aiNode *> nodes;
CollectNewChildren (dummy_root,nodes); CollectNewChildren(dummy_root, nodes);
ai_assert(nodes.size() == 1); ai_assert(nodes.size() == 1);
if (dummy_root->mNumChildren == 0) { if (dummy_root->mNumChildren == 0) {
pScene->mRootNode = NULL; pScene->mRootNode = nullptr;
throw DeadlyImportError("After optimizing the scene graph, no data remains"); throw DeadlyImportError("After optimizing the scene graph, no data remains");
} }
@ -314,19 +323,18 @@ void OptimizeGraphProcess::Execute( aiScene* pScene) {
// Keep the dummy node but assign the name of the old root node to it // Keep the dummy node but assign the name of the old root node to it
pScene->mRootNode->mName = prev; pScene->mRootNode->mName = prev;
} } else {
else {
// Remove the dummy root node again. // Remove the dummy root node again.
pScene->mRootNode = dummy_root->mChildren[0]; pScene->mRootNode = dummy_root->mChildren[0];
dummy_root->mChildren[0] = NULL; dummy_root->mChildren[0] = nullptr;
delete dummy_root; delete dummy_root;
} }
pScene->mRootNode->mParent = NULL; pScene->mRootNode->mParent = nullptr;
if (!DefaultLogger::isNullLogger()) { if (!DefaultLogger::isNullLogger()) {
if ( nodes_in != nodes_out) { if (nodes_in != nodes_out) {
ASSIMP_LOG_INFO_F("OptimizeGraphProcess finished; Input nodes: ", nodes_in, ", Output nodes: ", nodes_out); ASSIMP_LOG_INFO_F("OptimizeGraphProcess finished; Input nodes: ", nodes_in, ", Output nodes: ", nodes_out);
} else { } else {
ASSIMP_LOG_DEBUG("OptimizeGraphProcess finished"); ASSIMP_LOG_DEBUG("OptimizeGraphProcess finished");
@ -338,9 +346,8 @@ void OptimizeGraphProcess::Execute( aiScene* pScene) {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Build a LUT of all instanced meshes // Build a LUT of all instanced meshes
void OptimizeGraphProcess::FindInstancedMeshes (aiNode* pNode) void OptimizeGraphProcess::FindInstancedMeshes(aiNode *pNode) {
{ for (unsigned int i = 0; i < pNode->mNumMeshes; ++i) {
for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
++meshes[pNode->mMeshes[i]]; ++meshes[pNode->mMeshes[i]];
} }

View File

@ -75,13 +75,13 @@ public:
~OptimizeGraphProcess(); ~OptimizeGraphProcess();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
bool IsActive( unsigned int pFlags) const; bool IsActive( unsigned int pFlags) const override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
void Execute( aiScene* pScene); void Execute( aiScene* pScene) override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
void SetupProperties(const Importer* pImp); void SetupProperties(const Importer* pImp) override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** @brief Add a list of node names to be locked and not modified. /** @brief Add a list of node names to be locked and not modified.

View File

@ -45,11 +45,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @brief Implementation of the "PretransformVertices" post processing step * @brief Implementation of the "PretransformVertices" post processing step
*/ */
#include "PretransformVertices.h" #include "PretransformVertices.h"
#include "ConvertToLHProcess.h"
#include "ProcessHelper.h" #include "ProcessHelper.h"
#include <assimp/SceneCombiner.h>
#include <assimp/Exceptional.h> #include <assimp/Exceptional.h>
#include <assimp/SceneCombiner.h>
using namespace Assimp; using namespace Assimp;
@ -59,12 +59,12 @@ using namespace Assimp;
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Constructor to be privately used by Importer // Constructor to be privately used by Importer
PretransformVertices::PretransformVertices() PretransformVertices::PretransformVertices() :
: configKeepHierarchy (false) configKeepHierarchy(false),
, configNormalize(false) configNormalize(false),
, configTransform(false) configTransform(false),
, configTransformation() configTransformation(),
, mConfigPointCloud( false ) { mConfigPointCloud(false) {
// empty // empty
} }
@ -76,20 +76,18 @@ PretransformVertices::~PretransformVertices() {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Returns whether the processing step is present in the given flag field. // Returns whether the processing step is present in the given flag field.
bool PretransformVertices::IsActive( unsigned int pFlags) const bool PretransformVertices::IsActive(unsigned int pFlags) const {
{
return (pFlags & aiProcess_PreTransformVertices) != 0; return (pFlags & aiProcess_PreTransformVertices) != 0;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Setup import configuration // Setup import configuration
void PretransformVertices::SetupProperties(const Importer* pImp) void PretransformVertices::SetupProperties(const Importer *pImp) {
{
// Get the current value of AI_CONFIG_PP_PTV_KEEP_HIERARCHY, AI_CONFIG_PP_PTV_NORMALIZE, // Get the current value of AI_CONFIG_PP_PTV_KEEP_HIERARCHY, AI_CONFIG_PP_PTV_NORMALIZE,
// AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION and AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION // AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION and AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION
configKeepHierarchy = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_KEEP_HIERARCHY,0)); configKeepHierarchy = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_KEEP_HIERARCHY, 0));
configNormalize = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_NORMALIZE,0)); configNormalize = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_NORMALIZE, 0));
configTransform = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION,0)); configTransform = (0 != pImp->GetPropertyInteger(AI_CONFIG_PP_PTV_ADD_ROOT_TRANSFORMATION, 0));
configTransformation = pImp->GetPropertyMatrix(AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION, aiMatrix4x4()); configTransformation = pImp->GetPropertyMatrix(AI_CONFIG_PP_PTV_ROOT_TRANSFORMATION, aiMatrix4x4());
@ -98,11 +96,9 @@ void PretransformVertices::SetupProperties(const Importer* pImp)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Count the number of nodes // Count the number of nodes
unsigned int PretransformVertices::CountNodes( aiNode* pcNode ) unsigned int PretransformVertices::CountNodes(const aiNode *pcNode) const {
{
unsigned int iRet = 1; unsigned int iRet = 1;
for (unsigned int i = 0;i < pcNode->mNumChildren;++i) for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
{
iRet += CountNodes(pcNode->mChildren[i]); iRet += CountNodes(pcNode->mChildren[i]);
} }
return iRet; return iRet;
@ -110,8 +106,7 @@ unsigned int PretransformVertices::CountNodes( aiNode* pcNode )
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Get a bitwise combination identifying the vertex format of a mesh // Get a bitwise combination identifying the vertex format of a mesh
unsigned int PretransformVertices::GetMeshVFormat( aiMesh* pcMesh ) unsigned int PretransformVertices::GetMeshVFormat(aiMesh *pcMesh) const {
{
// the vertex format is stored in aiMesh::mBones for later retrieval. // the vertex format is stored in aiMesh::mBones for later retrieval.
// there isn't a good reason to compute it a few hundred times // there isn't a good reason to compute it a few hundred times
// from scratch. The pointer is unused as animations are lost // from scratch. The pointer is unused as animations are lost
@ -119,56 +114,47 @@ unsigned int PretransformVertices::GetMeshVFormat( aiMesh* pcMesh )
if (pcMesh->mBones) if (pcMesh->mBones)
return (unsigned int)(uint64_t)pcMesh->mBones; return (unsigned int)(uint64_t)pcMesh->mBones;
const unsigned int iRet = GetMeshVFormatUnique(pcMesh); const unsigned int iRet = GetMeshVFormatUnique(pcMesh);
// store the value for later use // store the value for later use
pcMesh->mBones = (aiBone**)(uint64_t)iRet; pcMesh->mBones = (aiBone **)(uint64_t)iRet;
return iRet; return iRet;
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Count the number of vertices in the whole scene and a given // Count the number of vertices in the whole scene and a given
// material index // material index
void PretransformVertices::CountVerticesAndFaces( aiScene* pcScene, aiNode* pcNode, unsigned int iMat, void PretransformVertices::CountVerticesAndFaces(const aiScene *pcScene, const aiNode *pcNode, unsigned int iMat,
unsigned int iVFormat, unsigned int* piFaces, unsigned int* piVertices) unsigned int iVFormat, unsigned int *piFaces, unsigned int *piVertices) const {
{ for (unsigned int i = 0; i < pcNode->mNumMeshes; ++i) {
for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) aiMesh *pcMesh = pcScene->mMeshes[pcNode->mMeshes[i]];
{ if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh)) {
aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ];
if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
{
*piVertices += pcMesh->mNumVertices; *piVertices += pcMesh->mNumVertices;
*piFaces += pcMesh->mNumFaces; *piFaces += pcMesh->mNumFaces;
} }
} }
for (unsigned int i = 0;i < pcNode->mNumChildren;++i) for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
{ CountVerticesAndFaces(pcScene, pcNode->mChildren[i], iMat,
CountVerticesAndFaces(pcScene,pcNode->mChildren[i],iMat, iVFormat, piFaces, piVertices);
iVFormat,piFaces,piVertices);
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Collect vertex/face data // Collect vertex/face data
void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsigned int iMat, void PretransformVertices::CollectData(const aiScene *pcScene, const aiNode *pcNode, unsigned int iMat,
unsigned int iVFormat, aiMesh* pcMeshOut, unsigned int iVFormat, aiMesh *pcMeshOut,
unsigned int aiCurrent[2], unsigned int* num_refs) unsigned int aiCurrent[2], unsigned int *num_refs) const {
{
// No need to multiply if there's no transformation // No need to multiply if there's no transformation
const bool identity = pcNode->mTransformation.IsIdentity(); const bool identity = pcNode->mTransformation.IsIdentity();
for (unsigned int i = 0; i < pcNode->mNumMeshes;++i) for (unsigned int i = 0; i < pcNode->mNumMeshes; ++i) {
{ aiMesh *pcMesh = pcScene->mMeshes[pcNode->mMeshes[i]];
aiMesh* pcMesh = pcScene->mMeshes[ pcNode->mMeshes[i] ]; if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh)) {
if (iMat == pcMesh->mMaterialIndex && iVFormat == GetMeshVFormat(pcMesh))
{
// Decrement mesh reference counter // Decrement mesh reference counter
unsigned int& num_ref = num_refs[pcNode->mMeshes[i]]; unsigned int &num_ref = num_refs[pcNode->mMeshes[i]];
ai_assert(0 != num_ref); ai_assert(0 != num_ref);
--num_ref; --num_ref;
// Save the name of the last mesh // Save the name of the last mesh
if (num_ref==0) if (num_ref == 0) {
{
pcMeshOut->mName = pcMesh->mName; pcMeshOut->mName = pcMesh->mName;
} }
@ -184,8 +170,7 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
pcMesh->mNormals, pcMesh->mNormals,
pcMesh->mNumVertices * sizeof(aiVector3D)); pcMesh->mNumVertices * sizeof(aiVector3D));
} }
if (iVFormat & 0x4) if (iVFormat & 0x4) {
{
// copy tangents without modifying them // copy tangents without modifying them
::memcpy(pcMeshOut->mTangents + aiCurrent[AI_PTVS_VERTEX], ::memcpy(pcMeshOut->mTangents + aiCurrent[AI_PTVS_VERTEX],
pcMesh->mTangents, pcMesh->mTangents,
@ -195,12 +180,10 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
pcMesh->mBitangents, pcMesh->mBitangents,
pcMesh->mNumVertices * sizeof(aiVector3D)); pcMesh->mNumVertices * sizeof(aiVector3D));
} }
} } else {
else
{
// copy positions, transform them to worldspace // copy positions, transform them to worldspace
for (unsigned int n = 0; n < pcMesh->mNumVertices;++n) { for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
pcMeshOut->mVertices[aiCurrent[AI_PTVS_VERTEX]+n] = pcNode->mTransformation * pcMesh->mVertices[n]; pcMeshOut->mVertices[aiCurrent[AI_PTVS_VERTEX] + n] = pcNode->mTransformation * pcMesh->mVertices[n];
} }
aiMatrix4x4 mWorldIT = pcNode->mTransformation; aiMatrix4x4 mWorldIT = pcNode->mTransformation;
mWorldIT.Inverse().Transpose(); mWorldIT.Inverse().Transpose();
@ -208,26 +191,23 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
// TODO: implement Inverse() for aiMatrix3x3 // TODO: implement Inverse() for aiMatrix3x3
aiMatrix3x3 m = aiMatrix3x3(mWorldIT); aiMatrix3x3 m = aiMatrix3x3(mWorldIT);
if (iVFormat & 0x2) if (iVFormat & 0x2) {
{
// copy normals, transform them to worldspace // copy normals, transform them to worldspace
for (unsigned int n = 0; n < pcMesh->mNumVertices;++n) { for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
pcMeshOut->mNormals[aiCurrent[AI_PTVS_VERTEX]+n] = pcMeshOut->mNormals[aiCurrent[AI_PTVS_VERTEX] + n] =
(m * pcMesh->mNormals[n]).Normalize(); (m * pcMesh->mNormals[n]).Normalize();
} }
} }
if (iVFormat & 0x4) if (iVFormat & 0x4) {
{
// copy tangents and bitangents, transform them to worldspace // copy tangents and bitangents, transform them to worldspace
for (unsigned int n = 0; n < pcMesh->mNumVertices;++n) { for (unsigned int n = 0; n < pcMesh->mNumVertices; ++n) {
pcMeshOut->mTangents [aiCurrent[AI_PTVS_VERTEX]+n] = (m * pcMesh->mTangents[n]).Normalize(); pcMeshOut->mTangents[aiCurrent[AI_PTVS_VERTEX] + n] = (m * pcMesh->mTangents[n]).Normalize();
pcMeshOut->mBitangents[aiCurrent[AI_PTVS_VERTEX]+n] = (m * pcMesh->mBitangents[n]).Normalize(); pcMeshOut->mBitangents[aiCurrent[AI_PTVS_VERTEX] + n] = (m * pcMesh->mBitangents[n]).Normalize();
} }
} }
} }
unsigned int p = 0; unsigned int p = 0;
while (iVFormat & (0x100 << p)) while (iVFormat & (0x100 << p)) {
{
// copy texture coordinates // copy texture coordinates
memcpy(pcMeshOut->mTextureCoords[p] + aiCurrent[AI_PTVS_VERTEX], memcpy(pcMeshOut->mTextureCoords[p] + aiCurrent[AI_PTVS_VERTEX],
pcMesh->mTextureCoords[p], pcMesh->mTextureCoords[p],
@ -235,8 +215,7 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
++p; ++p;
} }
p = 0; p = 0;
while (iVFormat & (0x1000000 << p)) while (iVFormat & (0x1000000 << p)) {
{
// copy vertex colors // copy vertex colors
memcpy(pcMeshOut->mColors[p] + aiCurrent[AI_PTVS_VERTEX], memcpy(pcMeshOut->mColors[p] + aiCurrent[AI_PTVS_VERTEX],
pcMesh->mColors[p], pcMesh->mColors[p],
@ -246,36 +225,33 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
// now we need to copy all faces. since we will delete the source mesh afterwards, // now we need to copy all faces. since we will delete the source mesh afterwards,
// we don't need to reallocate the array of indices except if this mesh is // we don't need to reallocate the array of indices except if this mesh is
// referenced multiple times. // referenced multiple times.
for (unsigned int planck = 0;planck < pcMesh->mNumFaces;++planck) for (unsigned int planck = 0; planck < pcMesh->mNumFaces; ++planck) {
{ aiFace &f_src = pcMesh->mFaces[planck];
aiFace& f_src = pcMesh->mFaces[planck]; aiFace &f_dst = pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE] + planck];
aiFace& f_dst = pcMeshOut->mFaces[aiCurrent[AI_PTVS_FACE]+planck];
const unsigned int num_idx = f_src.mNumIndices; const unsigned int num_idx = f_src.mNumIndices;
f_dst.mNumIndices = num_idx; f_dst.mNumIndices = num_idx;
unsigned int* pi; unsigned int *pi;
if (!num_ref) { /* if last time the mesh is referenced -> no reallocation */ if (!num_ref) { /* if last time the mesh is referenced -> no reallocation */
pi = f_dst.mIndices = f_src.mIndices; pi = f_dst.mIndices = f_src.mIndices;
// offset all vertex indices // offset all vertex indices
for (unsigned int hahn = 0; hahn < num_idx;++hahn){ for (unsigned int hahn = 0; hahn < num_idx; ++hahn) {
pi[hahn] += aiCurrent[AI_PTVS_VERTEX]; pi[hahn] += aiCurrent[AI_PTVS_VERTEX];
} }
} } else {
else {
pi = f_dst.mIndices = new unsigned int[num_idx]; pi = f_dst.mIndices = new unsigned int[num_idx];
// copy and offset all vertex indices // copy and offset all vertex indices
for (unsigned int hahn = 0; hahn < num_idx;++hahn){ for (unsigned int hahn = 0; hahn < num_idx; ++hahn) {
pi[hahn] = f_src.mIndices[hahn] + aiCurrent[AI_PTVS_VERTEX]; pi[hahn] = f_src.mIndices[hahn] + aiCurrent[AI_PTVS_VERTEX];
} }
} }
// Update the mPrimitiveTypes member of the mesh // Update the mPrimitiveTypes member of the mesh
switch (pcMesh->mFaces[planck].mNumIndices) switch (pcMesh->mFaces[planck].mNumIndices) {
{
case 0x1: case 0x1:
pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POINT; pcMeshOut->mPrimitiveTypes |= aiPrimitiveType_POINT;
break; break;
@ -296,21 +272,19 @@ void PretransformVertices::CollectData( aiScene* pcScene, aiNode* pcNode, unsign
} }
// append all children of us // append all children of us
for (unsigned int i = 0;i < pcNode->mNumChildren;++i) { for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
CollectData(pcScene,pcNode->mChildren[i],iMat, CollectData(pcScene, pcNode->mChildren[i], iMat,
iVFormat,pcMeshOut,aiCurrent,num_refs); iVFormat, pcMeshOut, aiCurrent, num_refs);
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Get a list of all vertex formats that occur for a given material index // Get a list of all vertex formats that occur for a given material index
// The output list contains duplicate elements // The output list contains duplicate elements
void PretransformVertices::GetVFormatList( aiScene* pcScene, unsigned int iMat, void PretransformVertices::GetVFormatList(const aiScene *pcScene, unsigned int iMat,
std::list<unsigned int>& aiOut) std::list<unsigned int> &aiOut) const {
{ for (unsigned int i = 0; i < pcScene->mNumMeshes; ++i) {
for (unsigned int i = 0; i < pcScene->mNumMeshes;++i) aiMesh *pcMesh = pcScene->mMeshes[i];
{
aiMesh* pcMesh = pcScene->mMeshes[ i ];
if (iMat == pcMesh->mMaterialIndex) { if (iMat == pcMesh->mMaterialIndex) {
aiOut.push_back(GetMeshVFormat(pcMesh)); aiOut.push_back(GetMeshVFormat(pcMesh));
} }
@ -319,35 +293,38 @@ void PretransformVertices::GetVFormatList( aiScene* pcScene, unsigned int iMat,
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Compute the absolute transformation matrices of each node // Compute the absolute transformation matrices of each node
void PretransformVertices::ComputeAbsoluteTransform( aiNode* pcNode ) void PretransformVertices::ComputeAbsoluteTransform(aiNode *pcNode) {
{
if (pcNode->mParent) { if (pcNode->mParent) {
pcNode->mTransformation = pcNode->mParent->mTransformation*pcNode->mTransformation; pcNode->mTransformation = pcNode->mParent->mTransformation * pcNode->mTransformation;
} }
for (unsigned int i = 0;i < pcNode->mNumChildren;++i) { for (unsigned int i = 0; i < pcNode->mNumChildren; ++i) {
ComputeAbsoluteTransform(pcNode->mChildren[i]); ComputeAbsoluteTransform(pcNode->mChildren[i]);
} }
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Apply the node transformation to a mesh // Apply the node transformation to a mesh
void PretransformVertices::ApplyTransform(aiMesh* mesh, const aiMatrix4x4& mat) void PretransformVertices::ApplyTransform(aiMesh *mesh, const aiMatrix4x4 &mat) const {
{
// Check whether we need to transform the coordinates at all // Check whether we need to transform the coordinates at all
if (!mat.IsIdentity()) { if (!mat.IsIdentity()) {
// Check for odd negative scale (mirror)
if (mesh->HasFaces() && mat.Determinant() < 0) {
// Reverse the mesh face winding order
FlipWindingOrderProcess::ProcessMesh(mesh);
}
// Update positions
if (mesh->HasPositions()) { if (mesh->HasPositions()) {
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) { for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
mesh->mVertices[i] = mat * mesh->mVertices[i]; mesh->mVertices[i] = mat * mesh->mVertices[i];
} }
} }
if (mesh->HasNormals() || mesh->HasTangentsAndBitangents()) {
aiMatrix4x4 mWorldIT = mat;
mWorldIT.Inverse().Transpose();
// TODO: implement Inverse() for aiMatrix3x3 // Update normals and tangents
aiMatrix3x3 m = aiMatrix3x3(mWorldIT); if (mesh->HasNormals() || mesh->HasTangentsAndBitangents()) {
const aiMatrix3x3 m = aiMatrix3x3(mat).Inverse().Transpose();
if (mesh->HasNormals()) { if (mesh->HasNormals()) {
for (unsigned int i = 0; i < mesh->mNumVertices; ++i) { for (unsigned int i = 0; i < mesh->mNumVertices; ++i) {
@ -366,29 +343,27 @@ void PretransformVertices::ApplyTransform(aiMesh* mesh, const aiMatrix4x4& mat)
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Simple routine to build meshes in worldspace, no further optimization // Simple routine to build meshes in worldspace, no further optimization
void PretransformVertices::BuildWCSMeshes(std::vector<aiMesh*>& out, aiMesh** in, void PretransformVertices::BuildWCSMeshes(std::vector<aiMesh *> &out, aiMesh **in,
unsigned int numIn, aiNode* node) unsigned int numIn, aiNode *node) const {
{
// NOTE: // NOTE:
// aiMesh::mNumBones store original source mesh, or UINT_MAX if not a copy // aiMesh::mNumBones store original source mesh, or UINT_MAX if not a copy
// aiMesh::mBones store reference to abs. transform we multiplied with // aiMesh::mBones store reference to abs. transform we multiplied with
// process meshes // process meshes
for (unsigned int i = 0; i < node->mNumMeshes;++i) { for (unsigned int i = 0; i < node->mNumMeshes; ++i) {
aiMesh* mesh = in[node->mMeshes[i]]; aiMesh *mesh = in[node->mMeshes[i]];
// check whether we can operate on this mesh // check whether we can operate on this mesh
if (!mesh->mBones || *reinterpret_cast<aiMatrix4x4*>(mesh->mBones) == node->mTransformation) { if (!mesh->mBones || *reinterpret_cast<aiMatrix4x4 *>(mesh->mBones) == node->mTransformation) {
// yes, we can. // yes, we can.
mesh->mBones = reinterpret_cast<aiBone**> (&node->mTransformation); mesh->mBones = reinterpret_cast<aiBone **>(&node->mTransformation);
mesh->mNumBones = UINT_MAX; mesh->mNumBones = UINT_MAX;
} } else {
else {
// try to find us in the list of newly created meshes // try to find us in the list of newly created meshes
for (unsigned int n = 0; n < out.size(); ++n) { for (unsigned int n = 0; n < out.size(); ++n) {
aiMesh* ctz = out[n]; aiMesh *ctz = out[n];
if (ctz->mNumBones == node->mMeshes[i] && *reinterpret_cast<aiMatrix4x4*>(ctz->mBones) == node->mTransformation) { if (ctz->mNumBones == node->mMeshes[i] && *reinterpret_cast<aiMatrix4x4 *>(ctz->mBones) == node->mTransformation) {
// ok, use this one. Update node mesh index // ok, use this one. Update node mesh index
node->mMeshes[i] = numIn + n; node->mMeshes[i] = numIn + n;
@ -397,15 +372,15 @@ void PretransformVertices::BuildWCSMeshes(std::vector<aiMesh*>& out, aiMesh** in
if (node->mMeshes[i] < numIn) { if (node->mMeshes[i] < numIn) {
// Worst case. Need to operate on a full copy of the mesh // Worst case. Need to operate on a full copy of the mesh
ASSIMP_LOG_INFO("PretransformVertices: Copying mesh due to mismatching transforms"); ASSIMP_LOG_INFO("PretransformVertices: Copying mesh due to mismatching transforms");
aiMesh* ntz; aiMesh *ntz;
const unsigned int tmp = mesh->mNumBones; // const unsigned int tmp = mesh->mNumBones; //
mesh->mNumBones = 0; mesh->mNumBones = 0;
SceneCombiner::Copy(&ntz,mesh); SceneCombiner::Copy(&ntz, mesh);
mesh->mNumBones = tmp; mesh->mNumBones = tmp;
ntz->mNumBones = node->mMeshes[i]; ntz->mNumBones = node->mMeshes[i];
ntz->mBones = reinterpret_cast<aiBone**> (&node->mTransformation); ntz->mBones = reinterpret_cast<aiBone **>(&node->mTransformation);
out.push_back(ntz); out.push_back(ntz);
@ -415,37 +390,34 @@ void PretransformVertices::BuildWCSMeshes(std::vector<aiMesh*>& out, aiMesh** in
} }
// call children // call children
for (unsigned int i = 0; i < node->mNumChildren;++i) for (unsigned int i = 0; i < node->mNumChildren; ++i)
BuildWCSMeshes(out,in,numIn,node->mChildren[i]); BuildWCSMeshes(out, in, numIn, node->mChildren[i]);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Reset transformation matrices to identity // Reset transformation matrices to identity
void PretransformVertices::MakeIdentityTransform(aiNode* nd) void PretransformVertices::MakeIdentityTransform(aiNode *nd) const {
{
nd->mTransformation = aiMatrix4x4(); nd->mTransformation = aiMatrix4x4();
// call children // call children
for (unsigned int i = 0; i < nd->mNumChildren;++i) for (unsigned int i = 0; i < nd->mNumChildren; ++i)
MakeIdentityTransform(nd->mChildren[i]); MakeIdentityTransform(nd->mChildren[i]);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Build reference counters for all meshes // Build reference counters for all meshes
void PretransformVertices::BuildMeshRefCountArray(aiNode* nd, unsigned int * refs) void PretransformVertices::BuildMeshRefCountArray(const aiNode *nd, unsigned int *refs) const {
{ for (unsigned int i = 0; i < nd->mNumMeshes; ++i)
for (unsigned int i = 0; i< nd->mNumMeshes;++i)
refs[nd->mMeshes[i]]++; refs[nd->mMeshes[i]]++;
// call children // call children
for (unsigned int i = 0; i < nd->mNumChildren;++i) for (unsigned int i = 0; i < nd->mNumChildren; ++i)
BuildMeshRefCountArray(nd->mChildren[i],refs); BuildMeshRefCountArray(nd->mChildren[i], refs);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Executes the post processing step on the given imported data. // Executes the post processing step on the given imported data.
void PretransformVertices::Execute( aiScene* pScene) void PretransformVertices::Execute(aiScene *pScene) {
{
ASSIMP_LOG_DEBUG("PretransformVerticesProcess begin"); ASSIMP_LOG_DEBUG("PretransformVerticesProcess begin");
// Return immediately if we have no meshes // Return immediately if we have no meshes
@ -456,7 +428,7 @@ void PretransformVertices::Execute( aiScene* pScene)
const unsigned int iOldAnimationChannels = pScene->mNumAnimations; const unsigned int iOldAnimationChannels = pScene->mNumAnimations;
const unsigned int iOldNodes = CountNodes(pScene->mRootNode); const unsigned int iOldNodes = CountNodes(pScene->mRootNode);
if(configTransform) { if (configTransform) {
pScene->mRootNode->mTransformation = configTransformation; pScene->mRootNode->mTransformation = configTransformation;
} }
@ -466,10 +438,10 @@ void PretransformVertices::Execute( aiScene* pScene)
// Delete aiMesh::mBones for all meshes. The bones are // Delete aiMesh::mBones for all meshes. The bones are
// removed during this step and we need the pointer as // removed during this step and we need the pointer as
// temporary storage // temporary storage
for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
aiMesh* mesh = pScene->mMeshes[i]; aiMesh *mesh = pScene->mMeshes[i];
for (unsigned int a = 0; a < mesh->mNumBones;++a) for (unsigned int a = 0; a < mesh->mNumBones; ++a)
delete mesh->mBones[a]; delete mesh->mBones[a];
delete[] mesh->mBones; delete[] mesh->mBones;
@ -477,74 +449,74 @@ void PretransformVertices::Execute( aiScene* pScene)
} }
// now build a list of output meshes // now build a list of output meshes
std::vector<aiMesh*> apcOutMeshes; std::vector<aiMesh *> apcOutMeshes;
// Keep scene hierarchy? It's an easy job in this case ... // Keep scene hierarchy? It's an easy job in this case ...
// we go on and transform all meshes, if one is referenced by nodes // we go on and transform all meshes, if one is referenced by nodes
// with different absolute transformations a depth copy of the mesh // with different absolute transformations a depth copy of the mesh
// is required. // is required.
if( configKeepHierarchy ) { if (configKeepHierarchy) {
// Hack: store the matrix we're transforming a mesh with in aiMesh::mBones // Hack: store the matrix we're transforming a mesh with in aiMesh::mBones
BuildWCSMeshes(apcOutMeshes,pScene->mMeshes,pScene->mNumMeshes, pScene->mRootNode); BuildWCSMeshes(apcOutMeshes, pScene->mMeshes, pScene->mNumMeshes, pScene->mRootNode);
// ... if new meshes have been generated, append them to the end of the scene // ... if new meshes have been generated, append them to the end of the scene
if (apcOutMeshes.size() > 0) { if (apcOutMeshes.size() > 0) {
aiMesh** npp = new aiMesh*[pScene->mNumMeshes + apcOutMeshes.size()]; aiMesh **npp = new aiMesh *[pScene->mNumMeshes + apcOutMeshes.size()];
memcpy(npp,pScene->mMeshes,sizeof(aiMesh*)*pScene->mNumMeshes); memcpy(npp, pScene->mMeshes, sizeof(aiMesh *) * pScene->mNumMeshes);
memcpy(npp+pScene->mNumMeshes,&apcOutMeshes[0],sizeof(aiMesh*)*apcOutMeshes.size()); memcpy(npp + pScene->mNumMeshes, &apcOutMeshes[0], sizeof(aiMesh *) * apcOutMeshes.size());
pScene->mNumMeshes += static_cast<unsigned int>(apcOutMeshes.size()); pScene->mNumMeshes += static_cast<unsigned int>(apcOutMeshes.size());
delete[] pScene->mMeshes; pScene->mMeshes = npp; delete[] pScene->mMeshes;
pScene->mMeshes = npp;
} }
// now iterate through all meshes and transform them to worldspace // now iterate through all meshes and transform them to worldspace
for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) { for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
ApplyTransform(pScene->mMeshes[i],*reinterpret_cast<aiMatrix4x4*>( pScene->mMeshes[i]->mBones )); ApplyTransform(pScene->mMeshes[i], *reinterpret_cast<aiMatrix4x4 *>(pScene->mMeshes[i]->mBones));
// prevent improper destruction // prevent improper destruction
pScene->mMeshes[i]->mBones = NULL; pScene->mMeshes[i]->mBones = NULL;
pScene->mMeshes[i]->mNumBones = 0; pScene->mMeshes[i]->mNumBones = 0;
} }
} else { } else {
apcOutMeshes.reserve(pScene->mNumMaterials<<1u); apcOutMeshes.reserve(pScene->mNumMaterials << 1u);
std::list<unsigned int> aiVFormats; std::list<unsigned int> aiVFormats;
std::vector<unsigned int> s(pScene->mNumMeshes,0); std::vector<unsigned int> s(pScene->mNumMeshes, 0);
BuildMeshRefCountArray(pScene->mRootNode,&s[0]); BuildMeshRefCountArray(pScene->mRootNode, &s[0]);
for (unsigned int i = 0; i < pScene->mNumMaterials;++i) { for (unsigned int i = 0; i < pScene->mNumMaterials; ++i) {
// get the list of all vertex formats for this material // get the list of all vertex formats for this material
aiVFormats.clear(); aiVFormats.clear();
GetVFormatList(pScene,i,aiVFormats); GetVFormatList(pScene, i, aiVFormats);
aiVFormats.sort(); aiVFormats.sort();
aiVFormats.unique(); aiVFormats.unique();
for (std::list<unsigned int>::const_iterator j = aiVFormats.begin();j != aiVFormats.end();++j) { for (std::list<unsigned int>::const_iterator j = aiVFormats.begin(); j != aiVFormats.end(); ++j) {
unsigned int iVertices = 0; unsigned int iVertices = 0;
unsigned int iFaces = 0; unsigned int iFaces = 0;
CountVerticesAndFaces(pScene,pScene->mRootNode,i,*j,&iFaces,&iVertices); CountVerticesAndFaces(pScene, pScene->mRootNode, i, *j, &iFaces, &iVertices);
if (0 != iFaces && 0 != iVertices) if (0 != iFaces && 0 != iVertices) {
{
apcOutMeshes.push_back(new aiMesh()); apcOutMeshes.push_back(new aiMesh());
aiMesh* pcMesh = apcOutMeshes.back(); aiMesh *pcMesh = apcOutMeshes.back();
pcMesh->mNumFaces = iFaces; pcMesh->mNumFaces = iFaces;
pcMesh->mNumVertices = iVertices; pcMesh->mNumVertices = iVertices;
pcMesh->mFaces = new aiFace[iFaces]; pcMesh->mFaces = new aiFace[iFaces];
pcMesh->mVertices = new aiVector3D[iVertices]; pcMesh->mVertices = new aiVector3D[iVertices];
pcMesh->mMaterialIndex = i; pcMesh->mMaterialIndex = i;
if ((*j) & 0x2)pcMesh->mNormals = new aiVector3D[iVertices]; if ((*j) & 0x2) pcMesh->mNormals = new aiVector3D[iVertices];
if ((*j) & 0x4) if ((*j) & 0x4) {
{
pcMesh->mTangents = new aiVector3D[iVertices]; pcMesh->mTangents = new aiVector3D[iVertices];
pcMesh->mBitangents = new aiVector3D[iVertices]; pcMesh->mBitangents = new aiVector3D[iVertices];
} }
iFaces = 0; iFaces = 0;
while ((*j) & (0x100 << iFaces)) while ((*j) & (0x100 << iFaces)) {
{
pcMesh->mTextureCoords[iFaces] = new aiVector3D[iVertices]; pcMesh->mTextureCoords[iFaces] = new aiVector3D[iVertices];
if ((*j) & (0x10000 << iFaces))pcMesh->mNumUVComponents[iFaces] = 3; if ((*j) & (0x10000 << iFaces))
else pcMesh->mNumUVComponents[iFaces] = 2; pcMesh->mNumUVComponents[iFaces] = 3;
else
pcMesh->mNumUVComponents[iFaces] = 2;
iFaces++; iFaces++;
} }
iFaces = 0; iFaces = 0;
@ -552,8 +524,8 @@ void PretransformVertices::Execute( aiScene* pScene)
pcMesh->mColors[iFaces++] = new aiColor4D[iVertices]; pcMesh->mColors[iFaces++] = new aiColor4D[iVertices];
// fill the mesh ... // fill the mesh ...
unsigned int aiTemp[2] = {0,0}; unsigned int aiTemp[2] = { 0, 0 };
CollectData(pScene,pScene->mRootNode,i,*j,pcMesh,aiTemp,&s[0]); CollectData(pScene, pScene->mRootNode, i, *j, pcMesh, aiTemp, &s[0]);
} }
} }
} }
@ -562,13 +534,10 @@ void PretransformVertices::Execute( aiScene* pScene)
if (apcOutMeshes.empty()) { if (apcOutMeshes.empty()) {
throw DeadlyImportError("No output meshes: all meshes are orphaned and are not referenced by any nodes"); throw DeadlyImportError("No output meshes: all meshes are orphaned and are not referenced by any nodes");
} } else {
else
{
// now delete all meshes in the scene and build a new mesh list // now delete all meshes in the scene and build a new mesh list
for (unsigned int i = 0; i < pScene->mNumMeshes;++i) for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
{ aiMesh *mesh = pScene->mMeshes[i];
aiMesh* mesh = pScene->mMeshes[i];
mesh->mNumBones = 0; mesh->mNumBones = 0;
mesh->mBones = NULL; mesh->mBones = NULL;
@ -591,14 +560,14 @@ void PretransformVertices::Execute( aiScene* pScene)
// It is impossible that we have more output meshes than // It is impossible that we have more output meshes than
// input meshes, so we can easily reuse the old mesh array // input meshes, so we can easily reuse the old mesh array
pScene->mNumMeshes = (unsigned int)apcOutMeshes.size(); pScene->mNumMeshes = (unsigned int)apcOutMeshes.size();
for (unsigned int i = 0; i < pScene->mNumMeshes;++i) { for (unsigned int i = 0; i < pScene->mNumMeshes; ++i) {
pScene->mMeshes[i] = apcOutMeshes[i]; pScene->mMeshes[i] = apcOutMeshes[i];
} }
} }
} }
// remove all animations from the scene // remove all animations from the scene
for (unsigned int i = 0; i < pScene->mNumAnimations;++i) for (unsigned int i = 0; i < pScene->mNumAnimations; ++i)
delete pScene->mAnimations[i]; delete pScene->mAnimations[i];
delete[] pScene->mAnimations; delete[] pScene->mAnimations;
@ -606,56 +575,50 @@ void PretransformVertices::Execute( aiScene* pScene)
pScene->mNumAnimations = 0; pScene->mNumAnimations = 0;
// --- we need to keep all cameras and lights // --- we need to keep all cameras and lights
for (unsigned int i = 0; i < pScene->mNumCameras;++i) for (unsigned int i = 0; i < pScene->mNumCameras; ++i) {
{ aiCamera *cam = pScene->mCameras[i];
aiCamera* cam = pScene->mCameras[i]; const aiNode *nd = pScene->mRootNode->FindNode(cam->mName);
const aiNode* nd = pScene->mRootNode->FindNode(cam->mName);
ai_assert(NULL != nd); ai_assert(NULL != nd);
// multiply all properties of the camera with the absolute // multiply all properties of the camera with the absolute
// transformation of the corresponding node // transformation of the corresponding node
cam->mPosition = nd->mTransformation * cam->mPosition; cam->mPosition = nd->mTransformation * cam->mPosition;
cam->mLookAt = aiMatrix3x3( nd->mTransformation ) * cam->mLookAt; cam->mLookAt = aiMatrix3x3(nd->mTransformation) * cam->mLookAt;
cam->mUp = aiMatrix3x3( nd->mTransformation ) * cam->mUp; cam->mUp = aiMatrix3x3(nd->mTransformation) * cam->mUp;
} }
for (unsigned int i = 0; i < pScene->mNumLights;++i) for (unsigned int i = 0; i < pScene->mNumLights; ++i) {
{ aiLight *l = pScene->mLights[i];
aiLight* l = pScene->mLights[i]; const aiNode *nd = pScene->mRootNode->FindNode(l->mName);
const aiNode* nd = pScene->mRootNode->FindNode(l->mName);
ai_assert(NULL != nd); ai_assert(NULL != nd);
// multiply all properties of the camera with the absolute // multiply all properties of the camera with the absolute
// transformation of the corresponding node // transformation of the corresponding node
l->mPosition = nd->mTransformation * l->mPosition; l->mPosition = nd->mTransformation * l->mPosition;
l->mDirection = aiMatrix3x3( nd->mTransformation ) * l->mDirection; l->mDirection = aiMatrix3x3(nd->mTransformation) * l->mDirection;
l->mUp = aiMatrix3x3( nd->mTransformation ) * l->mUp; l->mUp = aiMatrix3x3(nd->mTransformation) * l->mUp;
} }
if( !configKeepHierarchy ) { if (!configKeepHierarchy) {
// now delete all nodes in the scene and build a new // now delete all nodes in the scene and build a new
// flat node graph with a root node and some level 1 children // flat node graph with a root node and some level 1 children
aiNode* newRoot = new aiNode(); aiNode *newRoot = new aiNode();
newRoot->mName = pScene->mRootNode->mName; newRoot->mName = pScene->mRootNode->mName;
delete pScene->mRootNode; delete pScene->mRootNode;
pScene->mRootNode = newRoot; pScene->mRootNode = newRoot;
if (1 == pScene->mNumMeshes && !pScene->mNumLights && !pScene->mNumCameras) if (1 == pScene->mNumMeshes && !pScene->mNumLights && !pScene->mNumCameras) {
{
pScene->mRootNode->mNumMeshes = 1; pScene->mRootNode->mNumMeshes = 1;
pScene->mRootNode->mMeshes = new unsigned int[1]; pScene->mRootNode->mMeshes = new unsigned int[1];
pScene->mRootNode->mMeshes[0] = 0; pScene->mRootNode->mMeshes[0] = 0;
} } else {
else pScene->mRootNode->mNumChildren = pScene->mNumMeshes + pScene->mNumLights + pScene->mNumCameras;
{ aiNode **nodes = pScene->mRootNode->mChildren = new aiNode *[pScene->mRootNode->mNumChildren];
pScene->mRootNode->mNumChildren = pScene->mNumMeshes+pScene->mNumLights+pScene->mNumCameras;
aiNode** nodes = pScene->mRootNode->mChildren = new aiNode*[pScene->mRootNode->mNumChildren];
// generate mesh nodes // generate mesh nodes
for (unsigned int i = 0; i < pScene->mNumMeshes;++i,++nodes) for (unsigned int i = 0; i < pScene->mNumMeshes; ++i, ++nodes) {
{ aiNode *pcNode = new aiNode();
aiNode* pcNode = new aiNode();
*nodes = pcNode; *nodes = pcNode;
pcNode->mParent = pScene->mRootNode; pcNode->mParent = pScene->mRootNode;
pcNode->mName = pScene->mMeshes[i]->mName; pcNode->mName = pScene->mMeshes[i]->mName;
@ -666,52 +629,49 @@ void PretransformVertices::Execute( aiScene* pScene)
pcNode->mMeshes[0] = i; pcNode->mMeshes[0] = i;
} }
// generate light nodes // generate light nodes
for (unsigned int i = 0; i < pScene->mNumLights;++i,++nodes) for (unsigned int i = 0; i < pScene->mNumLights; ++i, ++nodes) {
{ aiNode *pcNode = new aiNode();
aiNode* pcNode = new aiNode();
*nodes = pcNode; *nodes = pcNode;
pcNode->mParent = pScene->mRootNode; pcNode->mParent = pScene->mRootNode;
pcNode->mName.length = ai_snprintf(pcNode->mName.data, MAXLEN, "light_%u",i); pcNode->mName.length = ai_snprintf(pcNode->mName.data, MAXLEN, "light_%u", i);
pScene->mLights[i]->mName = pcNode->mName; pScene->mLights[i]->mName = pcNode->mName;
} }
// generate camera nodes // generate camera nodes
for (unsigned int i = 0; i < pScene->mNumCameras;++i,++nodes) for (unsigned int i = 0; i < pScene->mNumCameras; ++i, ++nodes) {
{ aiNode *pcNode = new aiNode();
aiNode* pcNode = new aiNode();
*nodes = pcNode; *nodes = pcNode;
pcNode->mParent = pScene->mRootNode; pcNode->mParent = pScene->mRootNode;
pcNode->mName.length = ::ai_snprintf(pcNode->mName.data,MAXLEN,"cam_%u",i); pcNode->mName.length = ::ai_snprintf(pcNode->mName.data, MAXLEN, "cam_%u", i);
pScene->mCameras[i]->mName = pcNode->mName; pScene->mCameras[i]->mName = pcNode->mName;
} }
} }
} } else {
else {
// ... and finally set the transformation matrix of all nodes to identity // ... and finally set the transformation matrix of all nodes to identity
MakeIdentityTransform(pScene->mRootNode); MakeIdentityTransform(pScene->mRootNode);
} }
if (configNormalize) { if (configNormalize) {
// compute the boundary of all meshes // compute the boundary of all meshes
aiVector3D min,max; aiVector3D min, max;
MinMaxChooser<aiVector3D> ()(min,max); MinMaxChooser<aiVector3D>()(min, max);
for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) { for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
aiMesh* m = pScene->mMeshes[a]; aiMesh *m = pScene->mMeshes[a];
for (unsigned int i = 0; i < m->mNumVertices;++i) { for (unsigned int i = 0; i < m->mNumVertices; ++i) {
min = std::min(m->mVertices[i],min); min = std::min(m->mVertices[i], min);
max = std::max(m->mVertices[i],max); max = std::max(m->mVertices[i], max);
} }
} }
// find the dominant axis // find the dominant axis
aiVector3D d = max-min; aiVector3D d = max - min;
const ai_real div = std::max(d.x,std::max(d.y,d.z))*ai_real( 0.5); const ai_real div = std::max(d.x, std::max(d.y, d.z)) * ai_real(0.5);
d = min + d * (ai_real)0.5; d = min + d * (ai_real)0.5;
for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) { for (unsigned int a = 0; a < pScene->mNumMeshes; ++a) {
aiMesh* m = pScene->mMeshes[a]; aiMesh *m = pScene->mMeshes[a];
for (unsigned int i = 0; i < m->mNumVertices;++i) { for (unsigned int i = 0; i < m->mNumVertices; ++i) {
m->mVertices[i] = (m->mVertices[i]-d)/div; m->mVertices[i] = (m->mVertices[i] - d) / div;
} }
} }
} }
@ -721,8 +681,8 @@ void PretransformVertices::Execute( aiScene* pScene)
ASSIMP_LOG_DEBUG("PretransformVerticesProcess finished"); ASSIMP_LOG_DEBUG("PretransformVerticesProcess finished");
ASSIMP_LOG_INFO_F("Removed ", iOldNodes, " nodes and ", iOldAnimationChannels, " animation channels (", ASSIMP_LOG_INFO_F("Removed ", iOldNodes, " nodes and ", iOldAnimationChannels, " animation channels (",
CountNodes(pScene->mRootNode) ," output nodes)" ); CountNodes(pScene->mRootNode), " output nodes)");
ASSIMP_LOG_INFO_F("Kept ", pScene->mNumLights, " lights and ", pScene->mNumCameras, " cameras." ); ASSIMP_LOG_INFO_F("Kept ", pScene->mNumLights, " lights and ", pScene->mNumCameras, " cameras.");
ASSIMP_LOG_INFO_F("Moved ", iOldMeshes, " meshes to WCS (number of output meshes: ", pScene->mNumMeshes, ")"); ASSIMP_LOG_INFO_F("Moved ", iOldMeshes, " meshes to WCS (number of output meshes: ", pScene->mNumMeshes, ")");
} }
} }

View File

@ -68,20 +68,20 @@ namespace Assimp {
*/ */
class ASSIMP_API PretransformVertices : public BaseProcess { class ASSIMP_API PretransformVertices : public BaseProcess {
public: public:
PretransformVertices (); PretransformVertices();
~PretransformVertices (); ~PretransformVertices();
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Check whether step is active // Check whether step is active
bool IsActive( unsigned int pFlags) const; bool IsActive(unsigned int pFlags) const override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Execute step on a given scene // Execute step on a given scene
void Execute( aiScene* pScene); void Execute(aiScene *pScene) override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Setup import settings // Setup import settings
void SetupProperties(const Importer* pImp); void SetupProperties(const Importer *pImp) override;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
/** @brief Toggle the 'keep hierarchy' option /** @brief Toggle the 'keep hierarchy' option
@ -102,56 +102,56 @@ public:
private: private:
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Count the number of nodes // Count the number of nodes
unsigned int CountNodes( aiNode* pcNode ); unsigned int CountNodes(const aiNode *pcNode) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Get a bitwise combination identifying the vertex format of a mesh // Get a bitwise combination identifying the vertex format of a mesh
unsigned int GetMeshVFormat(aiMesh* pcMesh); unsigned int GetMeshVFormat(aiMesh *pcMesh) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Count the number of vertices in the whole scene and a given // Count the number of vertices in the whole scene and a given
// material index // material index
void CountVerticesAndFaces( aiScene* pcScene, aiNode* pcNode, void CountVerticesAndFaces(const aiScene *pcScene, const aiNode *pcNode,
unsigned int iMat, unsigned int iMat,
unsigned int iVFormat, unsigned int iVFormat,
unsigned int* piFaces, unsigned int *piFaces,
unsigned int* piVertices); unsigned int *piVertices) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Collect vertex/face data // Collect vertex/face data
void CollectData( aiScene* pcScene, aiNode* pcNode, void CollectData(const aiScene *pcScene, const aiNode *pcNode,
unsigned int iMat, unsigned int iMat,
unsigned int iVFormat, unsigned int iVFormat,
aiMesh* pcMeshOut, aiMesh *pcMeshOut,
unsigned int aiCurrent[2], unsigned int aiCurrent[2],
unsigned int* num_refs); unsigned int *num_refs) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Get a list of all vertex formats that occur for a given material // Get a list of all vertex formats that occur for a given material
// The output list contains duplicate elements // The output list contains duplicate elements
void GetVFormatList( aiScene* pcScene, unsigned int iMat, void GetVFormatList(const aiScene *pcScene, unsigned int iMat,
std::list<unsigned int>& aiOut); std::list<unsigned int> &aiOut) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Compute the absolute transformation matrices of each node // Compute the absolute transformation matrices of each node
void ComputeAbsoluteTransform( aiNode* pcNode ); void ComputeAbsoluteTransform(aiNode *pcNode);
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Simple routine to build meshes in worldspace, no further optimization // Simple routine to build meshes in worldspace, no further optimization
void BuildWCSMeshes(std::vector<aiMesh*>& out, aiMesh** in, void BuildWCSMeshes(std::vector<aiMesh *> &out, aiMesh **in,
unsigned int numIn, aiNode* node); unsigned int numIn, aiNode *node) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Apply the node transformation to a mesh // Apply the node transformation to a mesh
void ApplyTransform(aiMesh* mesh, const aiMatrix4x4& mat); void ApplyTransform(aiMesh *mesh, const aiMatrix4x4 &mat) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Reset transformation matrices to identity // Reset transformation matrices to identity
void MakeIdentityTransform(aiNode* nd); void MakeIdentityTransform(aiNode *nd) const;
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Build reference counters for all meshes // Build reference counters for all meshes
void BuildMeshRefCountArray(aiNode* nd, unsigned int * refs); void BuildMeshRefCountArray(const aiNode *nd, unsigned int *refs) const;
//! Configuration option: keep scene hierarchy as long as possible //! Configuration option: keep scene hierarchy as long as possible
bool configKeepHierarchy; bool configKeepHierarchy;

View File

@ -202,6 +202,7 @@ inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset
} }
mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot)); mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot));
mat->AddProperty(&prop.texCoord, 1, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(texType, texSlot));
if (prop.textureTransformSupported) { if (prop.textureTransformSupported) {
aiUVTransform transform; aiUVTransform transform;

View File

@ -0,0 +1,172 @@
{
"asset" : {
"generator" : "Khronos glTF Blender I/O v1.0.5",
"version" : "2.0"
},
"scene" : 0,
"scenes" : [
{
"name" : "Scene",
"nodes" : [
0,
1,
2
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "Cube"
},
{
"name" : "Light",
"rotation" : [
0.16907575726509094,
0.7558803558349609,
-0.27217137813568115,
0.570947527885437
],
"translation" : [
4.076245307922363,
5.903861999511719,
-1.0054539442062378
]
},
{
"name" : "Camera",
"rotation" : [
0.483536034822464,
0.33687159419059753,
-0.20870360732078552,
0.7804827094078064
],
"translation" : [
7.358891487121582,
4.958309173583984,
6.925790786743164
]
}
],
"materials" : [
{
"doubleSided" : true,
"name" : "Material",
"pbrMetallicRoughness" : {
"baseColorTexture" : {
"index" : 0,
"texCoord" : 0
},
"metallicFactor" : 0,
"metallicRoughnessTexture" : {
"index" : 0,
"texCoord" : 1
}
}
}
],
"meshes" : [
{
"name" : "Cube",
"primitives" : [
{
"attributes" : {
"POSITION" : 0,
"NORMAL" : 1,
"TEXCOORD_0" : 2,
"TEXCOORD_1" : 3
},
"indices" : 4,
"material" : 0
}
]
}
],
"textures" : [
{
"source" : 0
}
],
"images" : [
{
"mimeType" : "image/png",
"name" : "Material Base Color",
"uri" : "texture.png"
}
],
"accessors" : [
{
"bufferView" : 0,
"componentType" : 5126,
"count" : 24,
"max" : [
1,
1,
1
],
"min" : [
-1,
-1,
-1
],
"type" : "VEC3"
},
{
"bufferView" : 1,
"componentType" : 5126,
"count" : 24,
"type" : "VEC3"
},
{
"bufferView" : 2,
"componentType" : 5126,
"count" : 24,
"type" : "VEC2"
},
{
"bufferView" : 3,
"componentType" : 5126,
"count" : 24,
"type" : "VEC2"
},
{
"bufferView" : 4,
"componentType" : 5123,
"count" : 36,
"type" : "SCALAR"
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 288,
"byteOffset" : 0
},
{
"buffer" : 0,
"byteLength" : 288,
"byteOffset" : 288
},
{
"buffer" : 0,
"byteLength" : 192,
"byteOffset" : 576
},
{
"buffer" : 0,
"byteLength" : 192,
"byteOffset" : 768
},
{
"buffer" : 0,
"byteLength" : 72,
"byteOffset" : 960
}
],
"buffers" : [
{
"byteLength" : 1032,
"uri" : "boxTexcoords.bin"
}
]
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -49,6 +49,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <array> #include <array>
#include <assimp/pbrmaterial.h>
using namespace Assimp; using namespace Assimp;
class utglTF2ImportExport : public AbstractImportExportBase { class utglTF2ImportExport : public AbstractImportExportBase {
@ -464,3 +466,30 @@ TEST_F(utglTF2ImportExport, sceneMetadata) {
ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0); ASSERT_EQ(strcmp(generator.C_Str(), "COLLADA2GLTF"), 0);
} }
} }
TEST_F(utglTF2ImportExport, texcoords) {
Assimp::Importer importer;
const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/glTF2/BoxTexcoords-glTF/boxTexcoords.gltf",
aiProcess_ValidateDataStructure);
ASSERT_NE(scene, nullptr);
ASSERT_TRUE(scene->HasMaterials());
const aiMaterial *material = scene->mMaterials[0];
aiString path;
aiTextureMapMode modes[2];
EXPECT_EQ(aiReturn_SUCCESS, material->GetTexture(aiTextureType_DIFFUSE, 0, &path, nullptr, nullptr,
nullptr, nullptr, modes));
EXPECT_STREQ(path.C_Str(), "texture.png");
int uvIndex = -1;
EXPECT_EQ(aiGetMaterialInteger(material, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(aiTextureType_DIFFUSE, 0), &uvIndex), aiReturn_SUCCESS);
EXPECT_EQ(uvIndex, 0);
// Using manual macro expansion of AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE here.
// The following works with some but not all compilers:
// #define APPLY(X, Y) X(Y)
// ..., APPLY(AI_MATKEY_GLTF_TEXTURE_TEXCOORD, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE), ...
EXPECT_EQ(aiGetMaterialInteger(material, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(aiTextureType_UNKNOWN, 0), &uvIndex), aiReturn_SUCCESS);
EXPECT_EQ(uvIndex, 1);
}