Triangulation:
- FEATURE: Concave polygons are now triangulated correctly. - internal face order is ccw now, flipwinding part of converttolh flag - added test files for the various formats supporting such polygons FindDegenerates: - improved behaviour when processing polygons, more tolerant. Obj: - material files are now properly read using the given IOSystem - redirecting some std::cerr calls to our logger - spaces and tabs are now allowed at the beginning of a line Viewer: - max smoothing angle for normals is set to 90 deg now vc9-workspace - added assimp_cmd, renamed some virtual folders to be sexier. git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@374 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
This commit is contained in:
@@ -131,6 +131,9 @@ void B3DImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS
|
||||
// convert to RH
|
||||
MakeLeftHandedProcess monster_maker;
|
||||
monster_maker.Execute(pScene);
|
||||
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
+16
-11
@@ -698,8 +698,8 @@ void ColladaLoader::StoreSceneMeshes( aiScene* pScene)
|
||||
{
|
||||
pScene->mMeshes = new aiMesh*[mMeshes.size()];
|
||||
std::copy( mMeshes.begin(), mMeshes.end(), pScene->mMeshes);
|
||||
mMeshes.clear();
|
||||
}
|
||||
mMeshes.clear();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -711,8 +711,8 @@ void ColladaLoader::StoreSceneCameras( aiScene* pScene)
|
||||
{
|
||||
pScene->mCameras = new aiCamera*[mCameras.size()];
|
||||
std::copy( mCameras.begin(), mCameras.end(), pScene->mCameras);
|
||||
mCameras.clear();
|
||||
}
|
||||
mCameras.clear();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -724,8 +724,8 @@ void ColladaLoader::StoreSceneLights( aiScene* pScene)
|
||||
{
|
||||
pScene->mLights = new aiLight*[mLights.size()];
|
||||
std::copy( mLights.begin(), mLights.end(), pScene->mLights);
|
||||
mLights.clear();
|
||||
}
|
||||
mLights.clear();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -737,8 +737,8 @@ void ColladaLoader::StoreSceneTextures( aiScene* pScene)
|
||||
{
|
||||
pScene->mTextures = new aiTexture*[mTextures.size()];
|
||||
std::copy( mTextures.begin(), mTextures.end(), pScene->mTextures);
|
||||
mTextures.clear();
|
||||
}
|
||||
mTextures.clear();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -746,12 +746,14 @@ void ColladaLoader::StoreSceneTextures( aiScene* pScene)
|
||||
void ColladaLoader::StoreSceneMaterials( aiScene* pScene)
|
||||
{
|
||||
pScene->mNumMaterials = newMats.size();
|
||||
|
||||
pScene->mMaterials = new aiMaterial*[newMats.size()];
|
||||
for (unsigned int i = 0; i < newMats.size();++i)
|
||||
pScene->mMaterials[i] = newMats[i].second;
|
||||
|
||||
newMats.clear();
|
||||
if (newMats.size() > 0) {
|
||||
pScene->mMaterials = new aiMaterial*[newMats.size()];
|
||||
for (unsigned int i = 0; i < newMats.size();++i)
|
||||
pScene->mMaterials[i] = newMats[i].second;
|
||||
|
||||
newMats.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -935,8 +937,10 @@ void ColladaLoader::BuildMaterials( const ColladaParser& pParser, aiScene* pScen
|
||||
mMaterialIndexByName[matIt->first] = newMats.size();
|
||||
newMats.push_back( std::pair<Collada::Effect*, aiMaterial*>(const_cast<Collada::Effect*>(&effect),mat) );
|
||||
}
|
||||
|
||||
// store a dummy material if none were given
|
||||
// ScenePreprocessor generates a default material automatically if none is there.
|
||||
// All further code here in this loader works well without a valid material so
|
||||
// we can safely let it to ScenePreprocessor.
|
||||
#if 0
|
||||
if( newMats.size() == 0)
|
||||
{
|
||||
Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
|
||||
@@ -952,6 +956,7 @@ void ColladaLoader::BuildMaterials( const ColladaParser& pParser, aiScene* pScen
|
||||
const float specExp = 5.0f;
|
||||
mat->AddProperty( &specExp, 1, AI_MATKEY_SHININESS);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -100,11 +100,6 @@ void MakeLeftHandedProcess::Execute( aiScene* pScene)
|
||||
ProcessAnimation( nodeAnim);
|
||||
}
|
||||
}
|
||||
|
||||
// flipping a single vector component means inverting face order ...
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
|
||||
DefaultLogger::get()->debug("MakeLeftHandedProcess finished");
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include "DXFLoader.h"
|
||||
#include "ParsingUtils.h"
|
||||
#include "ConvertToLHProcess.h"
|
||||
#include "fast_atof.h"
|
||||
|
||||
using namespace Assimp;
|
||||
@@ -307,6 +308,10 @@ void DXFImporter::InternReadFile( const std::string& pFile,
|
||||
pScene->mMaterials = new aiMaterial*[1];
|
||||
pScene->mMaterials[0] = pcMat;
|
||||
|
||||
// flip winding order to be ccw
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
|
||||
// --- everything destructs automatically ---
|
||||
}
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ void FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh)
|
||||
if (configRemoveDegenerates)
|
||||
remove_me.resize(mesh->mNumFaces,false);
|
||||
|
||||
unsigned int deg = 0;
|
||||
unsigned int deg = 0, limit;
|
||||
for (unsigned int a = 0; a < mesh->mNumFaces; ++a)
|
||||
{
|
||||
aiFace& face = mesh->mFaces[a];
|
||||
@@ -109,13 +109,20 @@ void FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh)
|
||||
// check whether the face contains degenerated entries
|
||||
for (register unsigned int i = 0; i < face.mNumIndices; ++i)
|
||||
{
|
||||
for (register unsigned int t = i+1; t < face.mNumIndices; ++t)
|
||||
// Polygons with more than 4 points are allowed to have double points, that is
|
||||
// simulating polygons with holes just with concave polygons. However,
|
||||
// double points may not come directly after another.
|
||||
limit = face.mNumIndices;
|
||||
if (face.mNumIndices > 4)
|
||||
limit = std::min(limit,i+2);
|
||||
|
||||
for (register unsigned int t = i+1; t < limit; ++t)
|
||||
{
|
||||
if (mesh->mVertices[face.mIndices[i]] == mesh->mVertices[face.mIndices[t]])
|
||||
{
|
||||
// we have found a matching vertex position
|
||||
// remove the corresponding index from the array
|
||||
--face.mNumIndices;
|
||||
--face.mNumIndices;--limit;
|
||||
for (unsigned int m = t; m < face.mNumIndices; ++m)
|
||||
{
|
||||
face.mIndices[m] = face.mIndices[m+1];
|
||||
|
||||
+5
-3
@@ -401,7 +401,6 @@ void LWOImporter::InternReadFile( const std::string& pFile,
|
||||
pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = (unsigned int)apcMeshes.size() ];
|
||||
::memcpy(pScene->mMeshes,&apcMeshes[0],pScene->mNumMeshes*sizeof(void*));
|
||||
|
||||
|
||||
// generate the final node graph
|
||||
GenerateNodeGraph(apcNodes);
|
||||
}
|
||||
@@ -434,7 +433,7 @@ void LWOImporter::ComputeNormals(aiMesh* mesh, const std::vector<unsigned int>&
|
||||
aiVector3D* pV2 = mesh->mVertices + face.mIndices[1];
|
||||
aiVector3D* pV3 = mesh->mVertices + face.mIndices[face.mNumIndices-1];
|
||||
|
||||
aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).Normalize();
|
||||
aiVector3D vNor = ((*pV2 - *pV1) ^(*pV3 - *pV1)).Normalize();
|
||||
for (unsigned int i = 0; i < face.mNumIndices;++i)
|
||||
out[face.mIndices[i]] = vNor;
|
||||
}
|
||||
@@ -596,9 +595,12 @@ void LWOImporter::GenerateNodeGraph(std::vector<aiNode*>& apcNodes)
|
||||
pScene->mRootNode = pc;
|
||||
}
|
||||
|
||||
// convert the whole stuff to RH
|
||||
// convert the whole stuff to RH with CCW winding
|
||||
MakeLeftHandedProcess maker;
|
||||
maker.Execute(pScene);
|
||||
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
+5
-1
@@ -370,7 +370,7 @@ void LWSImporter::BuildGraph(aiNode* nd, LWS::NodeDesc& src, std::vector<Attachm
|
||||
|
||||
// .. and construct animation channels
|
||||
aiNodeAnim* anim = NULL;
|
||||
#if 0 /* not yet */
|
||||
#if 1 /* not yet */
|
||||
if (first != last) {
|
||||
resolver.SetAnimationRange(first,last);
|
||||
resolver.ExtractAnimChannel(&anim,AI_LWO_ANIM_FLAG_SAMPLE_ANIMS|AI_LWO_ANIM_FLAG_START_AT_ZERO);
|
||||
@@ -863,6 +863,10 @@ void LWSImporter::InternReadFile( const std::string& pFile, aiScene* pScene,
|
||||
MakeLeftHandedProcess monster_cheat;
|
||||
monster_cheat.Execute(master);
|
||||
|
||||
// .. ccw
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
|
||||
// OK ... finally build the output graph
|
||||
SceneCombiner::MergeScenes(&pScene,master,attach,
|
||||
AI_INT_MERGE_SCENE_GEN_UNIQUE_NAMES | (!configSpeedFlag ? (
|
||||
|
||||
@@ -119,7 +119,7 @@ void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene,
|
||||
}
|
||||
|
||||
// parse the file into a temporary representation
|
||||
ObjFileParser parser(m_Buffer, strDirectory, strModelName);
|
||||
ObjFileParser parser(m_Buffer, strDirectory, strModelName, pIOHandler);
|
||||
|
||||
// And create the proper return structures out of it
|
||||
CreateDataFromImport(parser.GetModel(), pScene);
|
||||
|
||||
+17
-54
@@ -59,12 +59,13 @@ const std::string ObjFileParser::DEFAULT_MATERIAL = AI_DEFAULT_MATERIAL_NAME;
|
||||
// Constructor with loaded data and directories.
|
||||
ObjFileParser::ObjFileParser(std::vector<char> &Data,
|
||||
const std::string &strAbsPath,
|
||||
const std::string &strModelName) :
|
||||
const std::string &strModelName, IOSystem* _io) :
|
||||
m_strAbsPath(strAbsPath),
|
||||
m_DataIt(Data.begin()),
|
||||
m_DataItEnd(Data.end()),
|
||||
m_pModel(NULL),
|
||||
m_uiLine(0)
|
||||
m_uiLine(0),
|
||||
io(_io)
|
||||
{
|
||||
// Create the model instance to store all the data
|
||||
m_pModel = new ObjFile::Model();
|
||||
@@ -436,44 +437,27 @@ void ObjFileParser::getMaterialLib()
|
||||
char *pStart = &(*m_DataIt);
|
||||
while (!isNewLine(*m_DataIt))
|
||||
m_DataIt++;
|
||||
|
||||
// TODO: fix path construction
|
||||
// CLEANUP ... who is resposible for *two* identical DefaultIOSystems
|
||||
// where the IOSystem passed to ReadFile() should be used???
|
||||
|
||||
// Check for existence
|
||||
DefaultIOSystem IOSystem;
|
||||
std::string strMatName(pStart, &(*m_DataIt));
|
||||
std::string absName = m_strAbsPath + IOSystem.getOsSeparator() + strMatName;
|
||||
if ( !IOSystem.Exists( absName.c_str()) )
|
||||
std::string absName = m_strAbsPath + io->getOsSeparator() + strMatName;
|
||||
IOStream *pFile = io->Open(absName.c_str());
|
||||
|
||||
if (!pFile )
|
||||
{
|
||||
DefaultLogger::get()->error("OBJ: Unable to locate material file " + absName);
|
||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||
return;
|
||||
}
|
||||
|
||||
// Extract the extention
|
||||
std::string strExt("");
|
||||
extractExtension( strMatName, strExt );
|
||||
static const std::string mat = "mtl";
|
||||
// Import material library data from file
|
||||
size_t size = pFile->FileSize();
|
||||
std::vector<char> buffer(size);
|
||||
pFile->Read( &buffer[ 0 ], sizeof( char ), size );
|
||||
io->Close( pFile );
|
||||
|
||||
// Load the material library
|
||||
DefaultIOSystem FileSystem;
|
||||
IOStream *pFile = FileSystem.Open(absName.c_str());
|
||||
if (0L != pFile)
|
||||
{
|
||||
// Import material library data from file
|
||||
size_t size = pFile->FileSize();
|
||||
std::vector<char> buffer;
|
||||
buffer.resize( size );
|
||||
pFile->Read( &buffer[ 0 ], sizeof( char ), size );
|
||||
FileSystem.Close( pFile );
|
||||
|
||||
// Importing the material library
|
||||
ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel );
|
||||
}
|
||||
|
||||
// Skip rest of line
|
||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||
// Importing the material library
|
||||
ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel );
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
@@ -493,9 +477,7 @@ void ObjFileParser::getNewMaterial()
|
||||
if (it == m_pModel->m_MaterialMap.end())
|
||||
{
|
||||
// Show a warning, if material was not found
|
||||
std::string strWarn ("Unsupported material requested: ");
|
||||
strWarn += strMat;
|
||||
std::cerr << "Warning : " << strWarn << std::endl;
|
||||
DefaultLogger::get()->warn("OBJ: Unsupported material requested: " + strMat);
|
||||
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
|
||||
}
|
||||
else
|
||||
@@ -630,27 +612,8 @@ void ObjFileParser::createObject(const std::string &strObjectName)
|
||||
// Shows an error in parsing process.
|
||||
void ObjFileParser::reportErrorTokenInFace()
|
||||
{
|
||||
std::string strErr("");
|
||||
|
||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||
std::cerr << "Not supported token in face desc. detected : " << strErr << std::endl;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// Extracts the extention from a filename
|
||||
void ObjFileParser::extractExtension(const std::string &strFile,
|
||||
std::string &strExt)
|
||||
{
|
||||
strExt = "";
|
||||
if (strFile.empty())
|
||||
return;
|
||||
|
||||
// Search for extention delimiter
|
||||
std::string::size_type pos = strFile.find_last_of(".");
|
||||
if ( pos == std::string::npos )
|
||||
return;
|
||||
|
||||
strExt = strFile.substr(pos, strFile.size() - pos);
|
||||
DefaultLogger::get()->error("OBJ: Not supported token in face description detected");
|
||||
}
|
||||
// -------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ public:
|
||||
|
||||
public:
|
||||
/// \brief Constructor with data array.
|
||||
ObjFileParser(std::vector<char> &Data, const std::string &strAbsPath, const std::string &strModelName);
|
||||
ObjFileParser(std::vector<char> &Data, const std::string &strAbsPath, const std::string &strModelName, IOSystem* io);
|
||||
/// \brief Destructor
|
||||
~ObjFileParser();
|
||||
/// \brief Model getter.
|
||||
@@ -113,8 +113,6 @@ private:
|
||||
void createObject(const std::string &strObjectName);
|
||||
/// Error report in token
|
||||
void reportErrorTokenInFace();
|
||||
/// Extractor for extention
|
||||
void extractExtension(const std::string &strFile, std::string &strExt);
|
||||
|
||||
private:
|
||||
/// Default material name
|
||||
@@ -131,6 +129,8 @@ private:
|
||||
unsigned int m_uiLine;
|
||||
//! Helper buffer
|
||||
char m_buffer[BUFFERSIZE];
|
||||
|
||||
IOSystem* io;
|
||||
};
|
||||
|
||||
} // Namespace Assimp
|
||||
|
||||
+4
-1
@@ -121,6 +121,9 @@ inline char_t skipLine( char_t it, char_t end, unsigned int &uiLine )
|
||||
++it;
|
||||
++uiLine;
|
||||
}
|
||||
/* fix .. from time to time there are spaces at the beginning of a material line */
|
||||
while ( it != end && (*it == '\t' || *it == ' ') )
|
||||
++it;
|
||||
return it;
|
||||
}
|
||||
|
||||
@@ -187,7 +190,7 @@ inline char_t getFloat( char_t it, char_t end, float &value )
|
||||
{
|
||||
static const size_t BUFFERSIZE = 1024;
|
||||
char buffer[ BUFFERSIZE ];
|
||||
memset( buffer, '\0', BUFFERSIZE );
|
||||
//memset( buffer, '\0', BUFFERSIZE );
|
||||
it = CopyNextWord<char_t>( it, end, buffer, BUFFERSIZE );
|
||||
value = (float) fast_atof( buffer );
|
||||
|
||||
|
||||
+86
-3
@@ -174,7 +174,12 @@ template <> struct MinMaxChooser<aiVertexWeight> {
|
||||
}};
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
// Find the min/max values of an array of Ts
|
||||
/** @brief Find the min/max values of an array of Ts
|
||||
* @param in Input array
|
||||
* @param size Numebr of elements to process
|
||||
* @param[out] min minimum value
|
||||
* @param[out] max maximum value
|
||||
*/
|
||||
template <typename T>
|
||||
inline void ArrayBounds(const T* in, unsigned int size, T& min, T& max)
|
||||
{
|
||||
@@ -185,6 +190,72 @@ inline void ArrayBounds(const T* in, unsigned int size, T& min, T& max)
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
/** @brief Compute the newell normal of a polygon regardless of its shape
|
||||
*
|
||||
* @param out Receives the output normal
|
||||
* @param num Number of input vertices
|
||||
* @param x X data source. x[ofs_x*n] is the n'th element.
|
||||
* @param y Y data source. y[ofs_y*n] is the y'th element
|
||||
* @param z Z data source. z[ofs_z*n] is the z'th element
|
||||
*
|
||||
* @note The data arrays must have storage for at least num+2 elements. Using
|
||||
* this method is much faster than the 'other' NewellNormal()
|
||||
*/
|
||||
template <int ofs_x, int ofs_y, int ofs_z>
|
||||
inline void NewellNormal (aiVector3D& out, int num, float* x, float* y, float* z)
|
||||
{
|
||||
// Duplicate the first two vertices at the end
|
||||
x[(num+0)*ofs_x] = x[0];
|
||||
x[(num+1)*ofs_x] = x[ofs_x];
|
||||
|
||||
y[(num+0)*ofs_y] = y[0];
|
||||
y[(num+1)*ofs_y] = y[ofs_y];
|
||||
|
||||
z[(num+0)*ofs_z] = z[0];
|
||||
z[(num+1)*ofs_z] = z[ofs_z];
|
||||
|
||||
float sum_xy = 0.0, sum_yz = 0.0, sum_zx = 0.0;
|
||||
|
||||
float *xptr = x +ofs_x, *xlow = x, *xhigh = x + ofs_x*2;
|
||||
float *yptr = y +ofs_y, *ylow = y, *yhigh = y + ofs_y*2;
|
||||
float *zptr = z +ofs_z, *zlow = z, *zhigh = z + ofs_z*2;
|
||||
|
||||
for (int tmp=0; tmp < num; tmp++) {
|
||||
sum_xy += (*xptr) * ( (*yhigh) - (*ylow) );
|
||||
sum_yz += (*yptr) * ( (*zhigh) - (*zlow) );
|
||||
sum_zx += (*zptr) * ( (*xhigh) - (*xlow) );
|
||||
|
||||
xptr += ofs_x;
|
||||
xlow += ofs_x;
|
||||
xhigh += ofs_x;
|
||||
|
||||
yptr += ofs_y;
|
||||
ylow += ofs_y;
|
||||
yhigh += ofs_y;
|
||||
|
||||
zptr += ofs_z;
|
||||
zlow += ofs_z;
|
||||
zhigh += ofs_z;
|
||||
}
|
||||
out = aiVector3D(sum_yz,sum_zx,sum_xy);
|
||||
}
|
||||
|
||||
#if 0
|
||||
// -------------------------------------------------------------------------------
|
||||
/** @brief Compute newell normal of a polgon regardless of its shape
|
||||
*
|
||||
* @param out Receives the output normal
|
||||
* @param data Input vertices
|
||||
* @param idx Index buffer
|
||||
* @param num Number of indices
|
||||
*/
|
||||
inline void NewellNormal (aiVector3D& out, const aiVector3D* data, unsigned int* idx, unsigned int num )
|
||||
{
|
||||
// TODO: intended to be used in GenNormals.
|
||||
}
|
||||
#endif
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
/** Little helper function to calculate the quadratic difference
|
||||
* of two colours.
|
||||
@@ -201,7 +272,12 @@ inline float GetColorDifference( const aiColor4D& pColor1, const aiColor4D& pCol
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
// Compute the AABB of a mesh after applying a given transform
|
||||
/** @brief Compute the AABB of a mesh after applying a given transform
|
||||
* @param mesh Input mesh
|
||||
* @param[out] min Receives minimum transformed vertex
|
||||
* @param[out] max Receives maximum transformed vertex
|
||||
* @param m Transformation matrix to be applied
|
||||
*/
|
||||
inline void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D& max,
|
||||
const aiMatrix4x4& m)
|
||||
{
|
||||
@@ -216,7 +292,14 @@ inline void FindAABBTransformed (const aiMesh* mesh, aiVector3D& min, aiVector3D
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
// Helper function to determine the 'real' center of a mesh
|
||||
/** @brief Helper function to determine the 'real' center of a mesh
|
||||
*
|
||||
* That is the center of its axis-aligned bounding box.
|
||||
* @param mesh Input mesh
|
||||
* @param[out] min Minimum vertex of the mesh
|
||||
* @param[out] max maximum vertex of the mesh
|
||||
* @param[out] out Center point
|
||||
*/
|
||||
inline void FindMeshCenter (aiMesh* mesh, aiVector3D& out, aiVector3D& min, aiVector3D& max)
|
||||
{
|
||||
ArrayBounds(mesh->mVertices,mesh->mNumVertices, min,max);
|
||||
|
||||
+251
-39
@@ -3,7 +3,7 @@
|
||||
Open Asset Import Library (ASSIMP)
|
||||
---------------------------------------------------------------------------
|
||||
|
||||
Copyright (c) 2006-2008, ASSIMP Development Team
|
||||
Copyright (c) 2006-2009, ASSIMP Development Team
|
||||
|
||||
All rights reserved.
|
||||
|
||||
@@ -39,13 +39,27 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
---------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
/** @file Implementation of the post processing step to split up
|
||||
* all faces with more than three indices into triangles.
|
||||
/** @file TriangulateProcess.cpp
|
||||
* @brief Implementation of the post processing step to split up
|
||||
* all faces with more than three indices into triangles.
|
||||
*
|
||||
*
|
||||
* The triangulation algorithm will handle concave or convex polygons.
|
||||
* Self-intersecting or non-planar polygons are not rejected, but
|
||||
* they're probably not triangulated correctly.
|
||||
*
|
||||
* AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
|
||||
* - generates vertex colors to represent the face winding order.
|
||||
* the first vertex of a polygon becomes red, the last blue.
|
||||
*/
|
||||
|
||||
#include "AssimpPCH.h"
|
||||
#include "TriangulateProcess.h"
|
||||
|
||||
#ifndef ASSIMP_BUILD_NO_TRIANGULATE_PROCESS
|
||||
#include "TriangulateProcess.h"
|
||||
#include "ProcessHelper.h"
|
||||
|
||||
//#define AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
|
||||
using namespace Assimp;
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@@ -81,55 +95,116 @@ void TriangulateProcess::Execute( aiScene* pScene)
|
||||
if( TriangulateMesh( pScene->mMeshes[a]))
|
||||
bHas = true;
|
||||
}
|
||||
if (bHas)DefaultLogger::get()->info ("TriangulateProcess finished. All polygons have been triangulated");
|
||||
else DefaultLogger::get()->debug("TriangulateProcess finished. There was nothing to do.");
|
||||
if (bHas)DefaultLogger::get()->info ("TriangulateProcess finished. All polygons have been triangulated.");
|
||||
else DefaultLogger::get()->debug("TriangulateProcess finished. There was nothing to be done.");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Test whether a point p2 is on the left side of the line formed by p0-p1
|
||||
inline bool OnLeftSideOfLine(const aiVector2D& p0, const aiVector2D& p1,const aiVector2D& p2)
|
||||
{
|
||||
return ( (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y) ) > 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Test whether a point is inside a given triangle in R2
|
||||
inline bool PointInTriangle2D(const aiVector2D& p0, const aiVector2D& p1,const aiVector2D& p2, const aiVector2D& pp)
|
||||
{
|
||||
// Point in triangle test using baryzentric coordinates
|
||||
const aiVector2D v0 = p1 - p0;
|
||||
const aiVector2D v1 = p2 - p0;
|
||||
const aiVector2D v2 = pp - p0;
|
||||
|
||||
float dot00 = v0 * v0;
|
||||
float dot01 = v0 * v1;
|
||||
float dot02 = v0 * v2;
|
||||
float dot11 = v1 * v1;
|
||||
float dot12 = v1 * v2;
|
||||
|
||||
const float invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
|
||||
dot11 = (dot11 * dot02 - dot01 * dot12) * invDenom;
|
||||
dot00 = (dot00 * dot12 - dot01 * dot02) * invDenom;
|
||||
|
||||
return (dot11 > 0) && (dot00 > 0) && (dot11 + dot00 < 1);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
// Triangulates the given mesh.
|
||||
bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
|
||||
{
|
||||
// check whether we will need to do something ...
|
||||
// FIX: now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
|
||||
if (!pMesh->mPrimitiveTypes)
|
||||
{
|
||||
// Now we have aiMesh::mPrimitiveTypes, so this is only here for test cases
|
||||
if (!pMesh->mPrimitiveTypes) {
|
||||
bool bNeed = false;
|
||||
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
||||
{
|
||||
|
||||
for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
|
||||
const aiFace& face = pMesh->mFaces[a];
|
||||
if( face.mNumIndices != 3)
|
||||
{
|
||||
|
||||
if( face.mNumIndices != 3) {
|
||||
bNeed = true;
|
||||
}
|
||||
}
|
||||
if (!bNeed)return false;
|
||||
if (!bNeed)
|
||||
return false;
|
||||
}
|
||||
else if (!(pMesh->mPrimitiveTypes & aiPrimitiveType_POLYGON))
|
||||
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;
|
||||
|
||||
// Find out how many output faces we'll have
|
||||
unsigned int numOut = 0;
|
||||
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
||||
{
|
||||
// Find out how many output faces we'll get
|
||||
unsigned int numOut = 0, max_out = 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;
|
||||
else {
|
||||
numOut += face.mNumIndices-2;
|
||||
max_out = std::max(max_out,face.mNumIndices);
|
||||
}
|
||||
}
|
||||
|
||||
// Just another check whether aiMesh::mPrimitiveTypes is correct
|
||||
assert(numOut != pMesh->mNumFaces);
|
||||
|
||||
|
||||
aiVector3D* nor_out = NULL;
|
||||
if (!pMesh->mNormals && pMesh->mPrimitiveTypes == aiPrimitiveType_POLYGON) {
|
||||
nor_out = pMesh->mNormals = new aiVector3D[pMesh->mNumVertices];
|
||||
}
|
||||
|
||||
aiFace* out = new aiFace[numOut], *curOut = out;
|
||||
for( unsigned int a = 0; a < pMesh->mNumFaces; a++)
|
||||
{
|
||||
std::vector<aiVector3D> temp_verts(max_out+2); /* temporary storage for vertices */
|
||||
|
||||
// Apply vertex colors to represent the face winding?
|
||||
#ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
|
||||
if (!pMesh->mColors[0])
|
||||
pMesh->mColors[0] = new aiColor4D[pMesh->mNumVertices];
|
||||
else
|
||||
new(pMesh->mColors[0]) aiColor4D[pMesh->mNumVertices];
|
||||
|
||||
aiColor4D* clr = pMesh->mColors[0];
|
||||
#endif
|
||||
|
||||
// use boost::scoped_array to avoid slow std::vector<bool> specialiations
|
||||
boost::scoped_array<bool> done(new bool[max_out]);
|
||||
for( unsigned int a = 0; a < pMesh->mNumFaces; a++) {
|
||||
aiFace& face = pMesh->mFaces[a];
|
||||
|
||||
unsigned int* idx = face.mIndices;
|
||||
int num = (int)face.mNumIndices, ear = 0, tmp, prev = num-1, next = 0, max = num;
|
||||
|
||||
// Apply vertex colors to represent the face winding?
|
||||
#ifdef AI_BUILD_TRIANGULATE_COLOR_FACE_WINDING
|
||||
for (unsigned int i = 0; i < face.mNumIndices; ++i) {
|
||||
aiColor4D& c = clr[idx[i]];
|
||||
c.r = (i+1) / (float)max;
|
||||
c.b = 1.f - c.r;
|
||||
}
|
||||
#endif
|
||||
|
||||
// if it's a simple primitive, just copy it
|
||||
if( face.mNumIndices <= 3)
|
||||
{
|
||||
@@ -139,25 +214,160 @@ bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
|
||||
}
|
||||
else
|
||||
{
|
||||
for( unsigned int b = 0, end = face.mNumIndices - 2; b < end; b++)
|
||||
{
|
||||
aiFace& nface = *curOut++;
|
||||
nface.mNumIndices = 3;
|
||||
// A polygon with more than 3 vertices can be either concave or convex.
|
||||
// Usually everything we're getting is convex and we could easily
|
||||
// triangulate by trifanning. However, LightWave is probably the only
|
||||
// modeller making extensive use of highly concave monster polygons ...
|
||||
// so we need to apply the full 'ear cutting' algorithm.
|
||||
|
||||
// RERQUIREMENT: polygon is expected to be simple and *nearly* planar.
|
||||
// We project it onto a plane to get 2d data. Working in R3 would
|
||||
// also be possible but it's more difficult to implement.
|
||||
|
||||
// Collect all vertices of of the polygon.
|
||||
aiVector3D* verts = pMesh->mVertices;
|
||||
for (tmp = 0; tmp < max; ++tmp)
|
||||
temp_verts[tmp] = verts[idx[tmp]];
|
||||
|
||||
// Get newell normal of the polygon. Store it for future use if it's a polygon-only mesh
|
||||
aiVector3D n;
|
||||
NewellNormal<3,3,3>(n,max,&temp_verts.front().x,&temp_verts.front().y,&temp_verts.front().z);
|
||||
if (nor_out) {
|
||||
for (tmp = 0; tmp < max; ++tmp)
|
||||
nor_out[idx[tmp]] = n;
|
||||
}
|
||||
|
||||
// Select largest normal coordinate to ignore for projection
|
||||
const float ax = (n.x>0 ? n.x : -n.x);
|
||||
const float ay = (n.y>0 ? n.y : -n.y);
|
||||
const float az = (n.z>0 ? n.z : -n.z);
|
||||
|
||||
unsigned int ac = 0, bc = 1; /* no z coord. projection to xy */
|
||||
float inv = n.z;
|
||||
if (ax > ay) {
|
||||
if (ax > az) { /* no x coord. projection to yz */
|
||||
ac = 1; bc = 2;
|
||||
inv = n.x;
|
||||
}
|
||||
}
|
||||
else if (ay > az) { /* no y coord. projection to zy */
|
||||
ac = 2; bc = 0;
|
||||
inv = n.y;
|
||||
}
|
||||
|
||||
// Swap projection axes to take the negated projection vector into account
|
||||
if (inv < 0.f) {
|
||||
std::swap(ac,bc);
|
||||
}
|
||||
|
||||
for (tmp =0; tmp < max; ++tmp) {
|
||||
temp_verts[tmp].x = verts[idx[tmp]][ac];
|
||||
temp_verts[tmp].y = verts[idx[tmp]][bc];
|
||||
done[tmp] = false;
|
||||
}
|
||||
|
||||
//
|
||||
// FIXME: currently this is the slow O(kn) variant with a worst case
|
||||
// complexity of O(n^2) (I think). Can be done in O(n).
|
||||
while (num > 3) {
|
||||
|
||||
// Find the next ear of the polygon
|
||||
int num_found = 0;
|
||||
for (ear = next;;prev = ear,ear = next) {
|
||||
|
||||
// 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];
|
||||
// break after we looped two times without a positive match
|
||||
for (next=ear+1;done[(next>max-1?next=0:next)];++next);
|
||||
if (next < ear) {
|
||||
if (++num_found == 2)
|
||||
break;
|
||||
}
|
||||
const aiVector2D* pnt1 = (const aiVector2D*)&temp_verts[ear],
|
||||
*pnt0 = (const aiVector2D*)&temp_verts[prev],
|
||||
*pnt2 = (const aiVector2D*)&temp_verts[next];
|
||||
|
||||
// Must be a convex point. Assuming ccw winding, it must be on the right of the line between p-1 and p+1.
|
||||
if (OnLeftSideOfLine (*pnt0,*pnt2,*pnt1))
|
||||
continue;
|
||||
|
||||
// and no other point may be contained in this triangle
|
||||
for ( tmp = 0; tmp < max; ++tmp) {
|
||||
|
||||
// We need to compare the actual values because it's possible that multiple indexes in
|
||||
// the polygon are refering to the same position. concave_polygon.obj is a sample
|
||||
//
|
||||
// FIXME: Use 'epsiloned' comparisons instead? Due to numeric inaccuracies in
|
||||
// PointInTriangle() I'm guessing that it's actually possible to construct
|
||||
// input data that would cause us to end up with no ears. The problem is,
|
||||
// which epsilon? If we chose a too large value, we'd get wrong results
|
||||
const aiVector2D& vtmp = * ((aiVector2D*) &temp_verts[tmp] );
|
||||
if ( vtmp != *pnt1 && vtmp != *pnt2 && vtmp != *pnt0 && PointInTriangle2D(*pnt0,*pnt1,*pnt2,vtmp))
|
||||
break;
|
||||
|
||||
}
|
||||
if (tmp != max)
|
||||
continue;
|
||||
|
||||
// this vertex is an ear
|
||||
break;
|
||||
}
|
||||
if (num_found == 2) {
|
||||
|
||||
// Due to the 'two ear theorem', every simple polygon with more than three points must
|
||||
// have 2 'ears'. Here's definitely someting wrong ... but we don't give up yet.
|
||||
//
|
||||
|
||||
// Instead we're continuting with the standard trifanning algorithm which we'd
|
||||
// use if we had only convex polygons. That's life.
|
||||
DefaultLogger::get()->error("Failed to triangulate polygon (no ear found). Probably not a simple polygon?");
|
||||
|
||||
curOut -= (max-num); /* undo all previous work */
|
||||
for (tmp = 0; tmp < max-2; ++tmp) {
|
||||
aiFace& nface = *curOut++;
|
||||
|
||||
nface.mNumIndices = 3;
|
||||
if (!nface.mIndices)
|
||||
nface.mIndices = new unsigned int[3];
|
||||
|
||||
nface.mIndices[0] = idx[0];
|
||||
nface.mIndices[1] = idx[tmp+1];
|
||||
nface.mIndices[2] = idx[tmp+2];
|
||||
}
|
||||
num = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
nface.mIndices[1] = face.mIndices[b+1];
|
||||
nface.mIndices[2] = face.mIndices[b+2];
|
||||
aiFace& nface = *curOut++;
|
||||
nface.mNumIndices = 3;
|
||||
|
||||
if (!nface.mIndices)
|
||||
nface.mIndices = new unsigned int[3];
|
||||
|
||||
// setup indices for the new triangle ...
|
||||
nface.mIndices[0] = idx[prev];
|
||||
nface.mIndices[1] = idx[ear];
|
||||
nface.mIndices[2] = idx[next];
|
||||
|
||||
// exclude the ear from most further processing
|
||||
done[ear] = true;
|
||||
--num;
|
||||
}
|
||||
if (num > 0) {
|
||||
// We have three indices forming the last 'ear' remaining. Collect them.
|
||||
aiFace& nface = *curOut++;
|
||||
nface.mNumIndices = 3;
|
||||
nface.mIndices = face.mIndices;
|
||||
|
||||
for (tmp = 0; done[tmp]; ++tmp);
|
||||
idx[0] = idx[tmp];
|
||||
|
||||
for (++tmp; done[tmp]; ++tmp);
|
||||
idx[1] = idx[tmp];
|
||||
|
||||
for (++tmp; done[tmp]; ++tmp);
|
||||
idx[2] = idx[tmp];
|
||||
}
|
||||
}
|
||||
face.mIndices = NULL;
|
||||
face.mIndices = NULL; /* prevent unintended deletion of our awesome results. would be a pity */
|
||||
}
|
||||
|
||||
// kill the old faces
|
||||
@@ -165,6 +375,8 @@ bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh)
|
||||
|
||||
// ... and store the new ones
|
||||
pMesh->mFaces = out;
|
||||
pMesh->mNumFaces = numOut;
|
||||
pMesh->mNumFaces = (unsigned int)(curOut-out); /* not necessarily equal to numOut */
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // !! ASSIMP_BUILD_NO_TRIANGULATE_PROCESS
|
||||
|
||||
@@ -419,6 +419,9 @@ void UnrealImporter::InternReadFile( const std::string& pFile,
|
||||
// convert to RH
|
||||
MakeLeftHandedProcess hero;
|
||||
hero.Execute(pScene);
|
||||
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
}
|
||||
|
||||
#endif // !! AI_BUILD_NO_3D_IMPORTER
|
||||
@@ -144,6 +144,9 @@ void XFileImporter::CreateDataRepresentationFromImport( aiScene* pScene, const X
|
||||
MakeLeftHandedProcess convertProcess;
|
||||
convertProcess.Execute( pScene);
|
||||
|
||||
FlipWindingOrderProcess flipper;
|
||||
flipper.Execute(pScene);
|
||||
|
||||
// finally: create a dummy material if not material was imported
|
||||
if( pScene->mNumMaterials == 0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user