2008-05-26 21:28:19 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (ASSIMP)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
* Neither the name of the ASSIMP team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the ASSIMP Development Team.
|
|
|
|
|
|
|
|
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.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
/** @file Implementation of the post processing step to split up
|
|
|
|
* all faces with more than three indices into triangles.
|
|
|
|
*/
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "TriangulateProcess.h"
|
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Constructor to be privately used by Importer
|
|
|
|
TriangulateProcess::TriangulateProcess()
|
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor, private as well
|
|
|
|
TriangulateProcess::~TriangulateProcess()
|
|
|
|
{
|
|
|
|
// nothing to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns whether the processing step is present in the given flag field.
|
|
|
|
bool TriangulateProcess::IsActive( unsigned int pFlags) const
|
|
|
|
{
|
|
|
|
return (pFlags & aiProcess_Triangulate) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Executes the post processing step on the given imported data.
|
|
|
|
void TriangulateProcess::Execute( aiScene* pScene)
|
|
|
|
{
|
2008-05-13 23:26:52 +00:00
|
|
|
DefaultLogger::get()->debug("TriangulateProcess begin");
|
|
|
|
|
|
|
|
bool bHas = false;
|
2008-05-05 12:36:31 +00:00
|
|
|
for( unsigned int a = 0; a < pScene->mNumMeshes; a++)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
|
|
|
if( TriangulateMesh( pScene->mMeshes[a]))
|
|
|
|
bHas = true;
|
|
|
|
}
|
2009-01-09 21:29:54 +00:00
|
|
|
if (bHas)DefaultLogger::get()->info ("TriangulateProcess finished. All polygons have been triangulated");
|
|
|
|
else DefaultLogger::get()->debug("TriangulateProcess finished. There was nothing to do.");
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Triangulates the given mesh.
|
2008-05-13 23:26:52 +00:00
|
|
|
bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-05-13 23:26:52 +00:00
|
|
|
// check whether we will need to do something ...
|
2008-09-30 20:20:56 +00:00
|
|
|
// FIX: now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
|
|
|
|
if (!pMesh->mPrimitiveTypes)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
2008-09-30 20:20:56 +00:00
|
|
|
bool bNeed = false;
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
2008-09-30 20:20:56 +00:00
|
|
|
const aiFace& face = pMesh->mFaces[a];
|
|
|
|
if( face.mNumIndices != 3)
|
|
|
|
{
|
|
|
|
bNeed = true;
|
|
|
|
}
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
2008-09-30 20:20:56 +00:00
|
|
|
if (!bNeed)return false;
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
2008-09-30 20:20:56 +00:00
|
|
|
else if (!(pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// the output mesh will contain triangles, but no polys anymore
|
|
|
|
pMesh->mPrimitiveTypes |= aiPrimitiveType_TRIANGLE;
|
|
|
|
pMesh->mPrimitiveTypes &= ~aiPrimitiveType_POLYGON;
|
2008-05-13 23:26:52 +00:00
|
|
|
|
2009-01-09 21:29:54 +00:00
|
|
|
// Find out how many output faces we'll have
|
|
|
|
unsigned int numOut = 0;
|
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
|
|
|
{
|
|
|
|
aiFace& face = pMesh->mFaces[a];
|
|
|
|
if( face.mNumIndices <= 3)
|
|
|
|
numOut++;
|
|
|
|
|
|
|
|
else numOut += face.mNumIndices-2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just another check whether aiMesh::mPrimitiveTypes is correct
|
|
|
|
assert(numOut != pMesh->mNumFaces);
|
|
|
|
|
|
|
|
aiFace* out = new aiFace[numOut], *curOut = out;
|
2008-05-05 12:36:31 +00:00
|
|
|
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
|
|
|
{
|
2008-09-30 20:20:56 +00:00
|
|
|
aiFace& face = pMesh->mFaces[a];
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// if it's a simple primitive, just copy it
|
2008-09-30 20:20:56 +00:00
|
|
|
if( face.mNumIndices <= 3)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-01-09 21:29:54 +00:00
|
|
|
aiFace& nface = *curOut++;
|
2008-05-05 12:36:31 +00:00
|
|
|
nface.mNumIndices = face.mNumIndices;
|
2009-01-09 21:29:54 +00:00
|
|
|
nface.mIndices = face.mIndices;
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
|
|
|
else
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-01-09 21:29:54 +00:00
|
|
|
for( unsigned int b = 0, end = face.mNumIndices - 2; b < end; b++)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-01-09 21:29:54 +00:00
|
|
|
aiFace& nface = *curOut++;
|
2008-05-05 12:36:31 +00:00
|
|
|
nface.mNumIndices = 3;
|
2009-01-09 21:29:54 +00:00
|
|
|
|
|
|
|
// Reuse the buffer for the very last element to save another allocation
|
|
|
|
if (b == end-1)
|
|
|
|
nface.mIndices = face.mIndices;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nface.mIndices = new unsigned int[3];
|
|
|
|
nface.mIndices[0] = face.mIndices[0];
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
nface.mIndices[1] = face.mIndices[b+1];
|
|
|
|
nface.mIndices[2] = face.mIndices[b+2];
|
|
|
|
}
|
|
|
|
}
|
2009-01-09 21:29:54 +00:00
|
|
|
face.mIndices = NULL;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// kill the old faces
|
|
|
|
delete [] pMesh->mFaces;
|
2008-09-30 20:20:56 +00:00
|
|
|
|
2009-01-09 21:29:54 +00:00
|
|
|
// ... and store the new ones
|
|
|
|
pMesh->mFaces = out;
|
|
|
|
pMesh->mNumFaces = numOut;
|
2008-05-13 23:26:52 +00:00
|
|
|
return true;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|