2009-04-24 20:53:24 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2009-04-24 20:53:24 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2015-06-28 19:48:25 +00:00
|
|
|
Copyright (c) 2006-2015, assimp team
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2009-04-24 20:53:24 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file OptimizeMeshes.cpp
|
|
|
|
* @brief Implementation of the aiProcess_OptimizeMeshes step
|
|
|
|
*/
|
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2009-04-24 20:53:24 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
|
2009-04-24 20:53:24 +00:00
|
|
|
#include "OptimizeMeshes.h"
|
|
|
|
#include "ProcessHelper.h"
|
2015-06-28 19:48:25 +00:00
|
|
|
#include "SceneCombiner.h"
|
2015-04-15 23:00:17 +00:00
|
|
|
#include "Exceptional.h"
|
2015-06-28 19:48:25 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
2009-04-24 20:53:24 +00:00
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
static const unsigned int NotSet = 0xffffffff;
|
|
|
|
static const unsigned int DeadBeef = 0xdeadbeef;
|
|
|
|
|
2009-04-24 20:53:24 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
OptimizeMeshesProcess::OptimizeMeshesProcess()
|
2015-05-16 19:13:22 +00:00
|
|
|
: mScene()
|
|
|
|
, pts(false)
|
|
|
|
, max_verts( NotSet )
|
|
|
|
, max_faces( NotSet ) {
|
2014-06-29 19:16:37 +00:00
|
|
|
// empty
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
2014-06-29 19:16:37 +00:00
|
|
|
OptimizeMeshesProcess::~OptimizeMeshesProcess() {
|
|
|
|
// empty
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
|
|
|
bool OptimizeMeshesProcess::IsActive( unsigned int pFlags) const
|
|
|
|
{
|
|
|
|
// Our behaviour needs to be different if the SortByPType or SplitLargeMeshes
|
|
|
|
// steps are active. Thus we need to query their flags here and store the
|
|
|
|
// information, although we're breaking const-correctness.
|
|
|
|
// That's a serious design flaw, consider redesign.
|
|
|
|
if( 0 != (pFlags & aiProcess_OptimizeMeshes) ) {
|
|
|
|
pts = (0 != (pFlags & aiProcess_SortByPType));
|
2014-06-29 19:16:37 +00:00
|
|
|
max_verts = ( 0 != ( pFlags & aiProcess_SplitLargeMeshes ) ) ? DeadBeef : max_verts;
|
2009-04-24 20:53:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-06-29 19:16:37 +00:00
|
|
|
// Setup properties for the post-processing step
|
2009-04-24 20:53:24 +00:00
|
|
|
void OptimizeMeshesProcess::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
2014-06-29 19:16:37 +00:00
|
|
|
if( max_verts == DeadBeef /* magic hack */ ) {
|
2009-04-24 20:53:24 +00:00
|
|
|
max_faces = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_TRIANGLE_LIMIT,AI_SLM_DEFAULT_MAX_TRIANGLES);
|
|
|
|
max_verts = pImp->GetPropertyInteger(AI_CONFIG_PP_SLM_VERTEX_LIMIT,AI_SLM_DEFAULT_MAX_VERTICES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Execute step
|
|
|
|
void OptimizeMeshesProcess::Execute( aiScene* pScene)
|
|
|
|
{
|
|
|
|
const unsigned int num_old = pScene->mNumMeshes;
|
|
|
|
if (num_old <= 1) {
|
|
|
|
DefaultLogger::get()->debug("Skipping OptimizeMeshesProcess");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultLogger::get()->debug("OptimizeMeshesProcess begin");
|
|
|
|
mScene = pScene;
|
|
|
|
|
2010-03-11 18:24:54 +00:00
|
|
|
// need to clear persistent members from previous runs
|
2014-06-29 19:16:37 +00:00
|
|
|
merge_list.resize( 0 );
|
|
|
|
output.resize( 0 );
|
2010-03-11 18:24:54 +00:00
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
// ensure we have the right sizes
|
2009-04-24 20:53:24 +00:00
|
|
|
merge_list.reserve(pScene->mNumMeshes);
|
|
|
|
output.reserve(pScene->mNumMeshes);
|
|
|
|
|
|
|
|
// Prepare lookup tables
|
|
|
|
meshes.resize(pScene->mNumMeshes);
|
|
|
|
FindInstancedMeshes(pScene->mRootNode);
|
2014-06-29 19:16:37 +00:00
|
|
|
if( max_verts == DeadBeef ) /* undo the magic hack */
|
|
|
|
max_verts = NotSet;
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
// ... instanced meshes are immediately processed and added to the output list
|
|
|
|
for (unsigned int i = 0, n = 0; i < pScene->mNumMeshes;++i) {
|
|
|
|
meshes[i].vertex_format = GetMeshVFormatUnique(pScene->mMeshes[i]);
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
if (meshes[i].instance_cnt > 1 && meshes[i].output_id == NotSet ) {
|
2009-04-24 20:53:24 +00:00
|
|
|
meshes[i].output_id = n++;
|
|
|
|
output.push_back(mScene->mMeshes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
// and process all nodes in the scenegraph recursively
|
2009-04-24 20:53:24 +00:00
|
|
|
ProcessNode(pScene->mRootNode);
|
|
|
|
if (!output.size()) {
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("OptimizeMeshes: No meshes remaining; there's definitely something wrong");
|
2009-04-24 20:53:24 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
meshes.resize( 0 );
|
2009-04-24 20:53:24 +00:00
|
|
|
ai_assert(output.size() <= num_old);
|
|
|
|
|
|
|
|
mScene->mNumMeshes = output.size();
|
|
|
|
std::copy(output.begin(),output.end(),mScene->mMeshes);
|
|
|
|
|
|
|
|
if (output.size() != num_old) {
|
|
|
|
char tmp[512];
|
2015-06-05 07:52:06 +00:00
|
|
|
::sprintf(tmp,"OptimizeMeshesProcess finished. Input meshes: %u, Output meshes: %u",num_old,pScene->mNumMeshes);
|
2009-04-24 20:53:24 +00:00
|
|
|
DefaultLogger::get()->info(tmp);
|
2014-06-29 19:16:37 +00:00
|
|
|
} else {
|
|
|
|
DefaultLogger::get()->debug( "OptimizeMeshesProcess finished" );
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Process meshes for a single node
|
|
|
|
void OptimizeMeshesProcess::ProcessNode( aiNode* pNode)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < pNode->mNumMeshes;++i) {
|
|
|
|
unsigned int& im = pNode->mMeshes[i];
|
|
|
|
|
|
|
|
if (meshes[im].instance_cnt > 1) {
|
|
|
|
im = meshes[im].output_id;
|
|
|
|
}
|
|
|
|
else {
|
2014-06-29 19:16:37 +00:00
|
|
|
merge_list.resize( 0 );
|
2009-04-24 20:53:24 +00:00
|
|
|
unsigned int verts = 0, faces = 0;
|
|
|
|
|
|
|
|
// Find meshes to merge with us
|
|
|
|
for (unsigned int a = i+1; a < pNode->mNumMeshes;++a) {
|
2015-03-02 22:44:02 +00:00
|
|
|
unsigned int am = pNode->mMeshes[a];
|
2009-04-24 20:53:24 +00:00
|
|
|
if (meshes[am].instance_cnt == 1 && CanJoin(im,am,verts,faces)) {
|
|
|
|
|
|
|
|
merge_list.push_back(mScene->mMeshes[am]);
|
|
|
|
verts += mScene->mMeshes[am]->mNumVertices;
|
|
|
|
faces += mScene->mMeshes[am]->mNumFaces;
|
|
|
|
|
|
|
|
--pNode->mNumMeshes;
|
2014-06-29 19:16:37 +00:00
|
|
|
for( unsigned int n = a; n < pNode->mNumMeshes; ++n ) {
|
|
|
|
pNode->mMeshes[ n ] = pNode->mMeshes[ n + 1 ];
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
|
|
|
|
--a;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// and merge all meshes which we found, replace the old ones
|
|
|
|
if (!merge_list.empty()) {
|
|
|
|
merge_list.push_back(mScene->mMeshes[im]);
|
|
|
|
|
|
|
|
aiMesh* out;
|
|
|
|
SceneCombiner::MergeMeshes(&out,0,merge_list.begin(),merge_list.end());
|
|
|
|
output.push_back(out);
|
2014-06-29 19:16:37 +00:00
|
|
|
} else {
|
2009-04-24 20:53:24 +00:00
|
|
|
output.push_back(mScene->mMeshes[im]);
|
|
|
|
}
|
|
|
|
im = output.size()-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
|
|
|
|
ProcessNode( pNode->mChildren[ i ] );
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Check whether two meshes can be joined
|
|
|
|
bool OptimizeMeshesProcess::CanJoin ( unsigned int a, unsigned int b, unsigned int verts, unsigned int faces )
|
|
|
|
{
|
|
|
|
if (meshes[a].vertex_format != meshes[b].vertex_format)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
aiMesh* ma = mScene->mMeshes[a], *mb = mScene->mMeshes[b];
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
if ((NotSet != max_verts && verts+mb->mNumVertices > max_verts) ||
|
|
|
|
(NotSet != max_faces && faces+mb->mNumFaces > max_faces)) {
|
2009-04-24 20:53:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never merge unskinned meshes with skinned meshes
|
|
|
|
if (ma->mMaterialIndex != mb->mMaterialIndex || ma->HasBones() != mb->HasBones())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Never merge meshes with different kinds of primitives if SortByPType did already
|
|
|
|
// do its work. We would destroy everything again ...
|
|
|
|
if (pts && ma->mPrimitiveTypes != mb->mPrimitiveTypes)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If both meshes are skinned, check whether we have many bones defined in both meshes.
|
2014-06-29 19:16:37 +00:00
|
|
|
// If yes, we can join them.
|
2009-04-24 20:53:24 +00:00
|
|
|
if (ma->HasBones()) {
|
|
|
|
// TODO
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2014-06-29 19:16:37 +00:00
|
|
|
// Build a LUT of all instanced meshes
|
2009-04-24 20:53:24 +00:00
|
|
|
void OptimizeMeshesProcess::FindInstancedMeshes (aiNode* pNode)
|
|
|
|
{
|
2014-06-29 19:16:37 +00:00
|
|
|
for( unsigned int i = 0; i < pNode->mNumMeshes; ++i ) {
|
|
|
|
++meshes[ pNode->mMeshes[ i ] ].instance_cnt;
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
for( unsigned int i = 0; i < pNode->mNumChildren; ++i ) {
|
|
|
|
FindInstancedMeshes( pNode->mChildren[ i ] );
|
|
|
|
}
|
2009-04-24 20:53:24 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 19:16:37 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
2009-10-11 13:57:55 +00:00
|
|
|
#endif // !! ASSIMP_BUILD_NO_OPTIMIZEMESHES_PROCESS
|