CHANGE: Bugfixes for ObjFile material import
git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@53 67173fc5-114c-0410-ac8e-9d2fd5bffc1fpull/1/head
parent
31861d5828
commit
a3d2ff5f26
|
@ -50,7 +50,7 @@ namespace Assimp
|
||||||
|
|
||||||
namespace ObjFile
|
namespace ObjFile
|
||||||
{
|
{
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
struct Object;
|
struct Object;
|
||||||
struct Face;
|
struct Face;
|
||||||
struct Material;
|
struct Material;
|
||||||
|
@ -134,16 +134,39 @@ struct Object
|
||||||
//! \brief Data structure to store all material specific data
|
//! \brief Data structure to store all material specific data
|
||||||
struct Material
|
struct Material
|
||||||
{
|
{
|
||||||
|
//! NAme of material description
|
||||||
aiString MaterialName;
|
aiString MaterialName;
|
||||||
|
//! Name of used texture
|
||||||
aiString texture;
|
aiString texture;
|
||||||
|
//! Ambient color
|
||||||
aiColor3D ambient;
|
aiColor3D ambient;
|
||||||
|
//! Diffuse color
|
||||||
aiColor3D diffuse;
|
aiColor3D diffuse;
|
||||||
|
//! Speculao color
|
||||||
aiColor3D specular;
|
aiColor3D specular;
|
||||||
|
//! Alpha value
|
||||||
float alpha;
|
float alpha;
|
||||||
|
//! Shineness factor
|
||||||
float shineness;
|
float shineness;
|
||||||
|
//! Illumination model
|
||||||
int illumination_model;
|
int illumination_model;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
//! \struct Mesh
|
||||||
|
//! \brief Data structure to store a mesh
|
||||||
|
struct Mesh
|
||||||
|
{
|
||||||
|
std::vector<Face*> m_Faces;
|
||||||
|
Material *m_pMaterial;
|
||||||
|
|
||||||
|
Mesh() :
|
||||||
|
m_pMaterial(NULL)
|
||||||
|
{
|
||||||
|
// empty
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
//! \struct Model
|
//! \struct Model
|
||||||
//! \brief Data structure to store all obj-specific model datas
|
//! \brief Data structure to store all obj-specific model datas
|
||||||
|
@ -179,29 +202,44 @@ struct Model
|
||||||
std::string m_strActiveGroup;
|
std::string m_strActiveGroup;
|
||||||
//! Vector with generated texture coordinates
|
//! Vector with generated texture coordinates
|
||||||
std::vector<aiVector2D*> m_TextureCoord;
|
std::vector<aiVector2D*> m_TextureCoord;
|
||||||
|
//! Current mesh instance
|
||||||
|
Mesh *m_pCurrentMesh;
|
||||||
|
//! Vector with stored meshes
|
||||||
|
std::vector<Mesh*> m_Meshes;
|
||||||
//! Material map
|
//! Material map
|
||||||
std::map<std::string, Material*> m_MaterialMap;
|
std::map<std::string, Material*> m_MaterialMap;
|
||||||
|
|
||||||
|
|
||||||
//! \brief Default constructor
|
//! \brief Default constructor
|
||||||
Model() :
|
Model() :
|
||||||
m_ModelName(""),
|
m_ModelName(""),
|
||||||
m_pCurrent(NULL),
|
m_pCurrent(NULL),
|
||||||
m_pCurrentMaterial(NULL),
|
m_pCurrentMaterial(NULL),
|
||||||
m_pDefaultMaterial(NULL),
|
m_pDefaultMaterial(NULL),
|
||||||
m_strActiveGroup("")
|
m_strActiveGroup(""),
|
||||||
|
m_pCurrentMesh(NULL)
|
||||||
{
|
{
|
||||||
// empty
|
// empty
|
||||||
}
|
}
|
||||||
|
|
||||||
//! \brief DEstructor
|
//! \brief Destructor
|
||||||
~Model()
|
~Model()
|
||||||
{
|
{
|
||||||
|
// Clear all stored object instances
|
||||||
for (std::vector<Object*>::iterator it = m_Objects.begin();
|
for (std::vector<Object*>::iterator it = m_Objects.begin();
|
||||||
it != m_Objects.end(); ++it)
|
it != m_Objects.end(); ++it)
|
||||||
{
|
{
|
||||||
delete *it;
|
delete *it;
|
||||||
}
|
}
|
||||||
m_Objects.clear();
|
m_Objects.clear();
|
||||||
|
|
||||||
|
// Clear all stored mesh instances
|
||||||
|
for (std::vector<Mesh*>::iterator it = m_Meshes.begin();
|
||||||
|
it != m_Meshes.end(); ++it)
|
||||||
|
{
|
||||||
|
delete *it;
|
||||||
|
}
|
||||||
|
m_Meshes.clear();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -159,9 +159,13 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
|
||||||
if (pParent != NULL)
|
if (pParent != NULL)
|
||||||
this->appendChildToParentNode(pParent, pNode);
|
this->appendChildToParentNode(pParent, pNode);
|
||||||
|
|
||||||
|
for (int meshIndex = 0; meshIndex < pModel->m_Meshes.size(); meshIndex++)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
aiMesh *pMesh = new aiMesh();
|
aiMesh *pMesh = new aiMesh();
|
||||||
MeshArray.push_back(pMesh);
|
MeshArray.push_back( pMesh );
|
||||||
createTopology(pModel, pData, pMesh);
|
createTopology( pModel, pData, pMesh );
|
||||||
|
|
||||||
// Create all nodes from the subobjects stored in the current object
|
// Create all nodes from the subobjects stored in the current object
|
||||||
if (!pData->m_SubObjects.empty())
|
if (!pData->m_SubObjects.empty())
|
||||||
|
@ -175,13 +179,12 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
|
||||||
for (size_t index = 0; index < pData->m_SubObjects.size(); index++)
|
for (size_t index = 0; index < pData->m_SubObjects.size(); index++)
|
||||||
{
|
{
|
||||||
// Create all child nodes
|
// Create all child nodes
|
||||||
pNode->mChildren[index] = createNodes(pModel, pData, pNode, pScene, MeshArray);
|
pNode->mChildren[ index ] = createNodes( pModel, pData, pNode, pScene, MeshArray );
|
||||||
|
|
||||||
// Create meshes of this object
|
|
||||||
pMesh = new aiMesh();
|
pMesh = new aiMesh();
|
||||||
MeshArray.push_back(pMesh);
|
MeshArray.push_back( pMesh );
|
||||||
createTopology(pModel, pData->m_SubObjects[ index ], pMesh);
|
createTopology( pModel, pData, pMesh );
|
||||||
|
|
||||||
// Create material of this object
|
// Create material of this object
|
||||||
createMaterial(pModel, pData->m_SubObjects[ index ], pScene);
|
createMaterial(pModel, pData->m_SubObjects[ index ], pScene);
|
||||||
}
|
}
|
||||||
|
@ -208,7 +211,7 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
// Create topology data
|
// Create topology data
|
||||||
void ObjFileImporter::createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
void ObjFileImporter::createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
||||||
aiMesh* pMesh)
|
aiMesh* pMesh )
|
||||||
{
|
{
|
||||||
if (NULL == pData)
|
if (NULL == pData)
|
||||||
return;
|
return;
|
||||||
|
@ -218,14 +221,15 @@ void ObjFileImporter::createTopology(const ObjFile::Model* pModel, const ObjFile
|
||||||
|
|
||||||
// Create faces
|
// Create faces
|
||||||
pMesh->mNumFaces = (unsigned int)pData->m_Faces.size();
|
pMesh->mNumFaces = (unsigned int)pData->m_Faces.size();
|
||||||
pMesh->mFaces = new aiFace[pMesh->mNumFaces];
|
pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
|
||||||
|
//pMesh->mMaterialIndex = pMode;
|
||||||
for (size_t index = 0; index < pMesh->mNumFaces; index++)
|
for (size_t index = 0; index < pMesh->mNumFaces; index++)
|
||||||
{
|
{
|
||||||
aiFace *pFace = &pMesh->mFaces[ index ];
|
aiFace *pFace = &pMesh->mFaces[ index ];
|
||||||
pFace->mNumIndices = (unsigned int)pData->m_Faces[index]->m_pVertices->size();
|
pFace->mNumIndices = (unsigned int)pData->m_Faces[index]->m_pVertices->size();
|
||||||
if (pFace->mNumIndices > 0)
|
if (pFace->mNumIndices > 0)
|
||||||
{
|
{
|
||||||
pFace->mIndices = new unsigned int[pMesh->mFaces[index].mNumIndices];
|
pFace->mIndices = new unsigned int[ pMesh->mFaces[ index ].mNumIndices ];
|
||||||
ObjFile::Face::IndexArray *pArray = pData->m_Faces[index]->m_pVertices;
|
ObjFile::Face::IndexArray *pArray = pData->m_Faces[index]->m_pVertices;
|
||||||
|
|
||||||
// TODO: Should be implement much better
|
// TODO: Should be implement much better
|
||||||
|
@ -254,18 +258,19 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
if (pCurrentObject->m_Faces.empty())
|
if (pCurrentObject->m_Faces.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Copy all stored vertices, normals and so on
|
// Copy vertices of this mesh instance
|
||||||
pMesh->mNumVertices = (unsigned int)pModel->m_Vertices.size();
|
pMesh->mNumVertices = (unsigned int)pModel->m_Vertices.size();
|
||||||
pMesh->mVertices = new aiVector3D[pMesh->mNumVertices];
|
pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
|
||||||
for (size_t index=0; index < pModel->m_Vertices.size(); index++)
|
for ( size_t index=0; index < pModel->m_Vertices.size(); index++ )
|
||||||
{
|
{
|
||||||
pMesh->mVertices[ index ] = *pModel->m_Vertices[ index ];
|
pMesh->mVertices[ index ] = *pModel->m_Vertices[ index ];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!pModel->m_Normals.empty())
|
// Copy normals for this mesh
|
||||||
|
if ( !pModel->m_Normals.empty() )
|
||||||
{
|
{
|
||||||
pMesh->mNormals = new aiVector3D[pModel->m_Normals.size()];
|
pMesh->mNormals = new aiVector3D[pModel->m_Normals.size()];
|
||||||
for (size_t index = 0; index < pModel->m_Normals.size(); index++)
|
for ( size_t index = 0; index < pModel->m_Normals.size(); index++ )
|
||||||
{
|
{
|
||||||
pMesh->mNormals[ index ] = *pModel->m_Normals[ index ];
|
pMesh->mNormals[ index ] = *pModel->m_Normals[ index ];
|
||||||
}
|
}
|
||||||
|
@ -273,7 +278,19 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
||||||
|
|
||||||
if (!pModel->m_TextureCoord.empty())
|
if (!pModel->m_TextureCoord.empty())
|
||||||
{
|
{
|
||||||
// TODO: Implement texture coodinates
|
pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pModel->m_TextureCoord.size() ];
|
||||||
|
|
||||||
|
for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_TEXTURECOORDS; e++)
|
||||||
|
{
|
||||||
|
if( pMesh->HasTextureCoords( e ))
|
||||||
|
{
|
||||||
|
for (unsigned int index = 0; index < pModel->m_TextureCoord.size(); index++ )
|
||||||
|
{
|
||||||
|
aiVector2D *tex = pModel->m_TextureCoord[ index ];
|
||||||
|
pMesh->mTextureCoords[ e ][ index ] = aiVector3D( tex->x, tex->y, 0.0f);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,13 +321,37 @@ void ObjFileImporter::createMaterial(const ObjFile::Model* pModel, const ObjFile
|
||||||
if (NULL == pData)
|
if (NULL == pData)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Create only a dumy material to enshure a running viewer
|
const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
|
||||||
pScene->mNumMaterials = 1;
|
pScene->mNumMaterials = 0;
|
||||||
Assimp::MaterialHelper* mat = new Assimp::MaterialHelper;
|
if ( pModel->m_MaterialLib.empty() )
|
||||||
|
return;
|
||||||
|
|
||||||
// Create a new material
|
pScene->mMaterials = new aiMaterial*[ numMaterials ];
|
||||||
pScene->mMaterials = new aiMaterial*[1];
|
for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
|
||||||
pScene->mMaterials[0] = mat;
|
{
|
||||||
|
Assimp::MaterialHelper* mat = new Assimp::MaterialHelper();
|
||||||
|
|
||||||
|
// Store material name
|
||||||
|
std::map<std::string, ObjFile::Material*>::const_iterator it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
|
||||||
|
if ( pModel->m_MaterialMap.end() == it)
|
||||||
|
continue;
|
||||||
|
ObjFile::Material *pCurrentMaterial = (*it).second;
|
||||||
|
mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
|
||||||
|
|
||||||
|
// Adding material colors
|
||||||
|
mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT);
|
||||||
|
mat->AddProperty( &pCurrentMaterial->diffuse, 1, AI_MATKEY_COLOR_DIFFUSE);
|
||||||
|
mat->AddProperty( &pCurrentMaterial->specular, 1, AI_MATKEY_COLOR_SPECULAR);
|
||||||
|
mat->AddProperty( &pCurrentMaterial->shineness, 1, AI_MATKEY_SHININESS);
|
||||||
|
|
||||||
|
// Adding textures
|
||||||
|
if ( 0 != pCurrentMaterial->texture.length )
|
||||||
|
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
||||||
|
|
||||||
|
// Store material property info in material array in scene
|
||||||
|
pScene->mMaterials[ pScene->mNumMaterials ] = mat;
|
||||||
|
pScene->mNumMaterials++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
@ -346,7 +387,6 @@ void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
|
||||||
pParent->mChildren[ index ] = temp [ index ];
|
pParent->mChildren[ index ] = temp [ index ];
|
||||||
}
|
}
|
||||||
pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
|
pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------------------------------
|
// ------------------------------------------------------------------------------------------------
|
||||||
|
|
|
@ -58,7 +58,7 @@ struct Model;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \class ObjFileImporter
|
/// \class ObjFileImporter
|
||||||
/// \brief IMports a waveform obj file
|
/// \brief Imports a waveform obj file
|
||||||
class ObjFileImporter :
|
class ObjFileImporter :
|
||||||
BaseImporter
|
BaseImporter
|
||||||
{
|
{
|
||||||
|
@ -81,46 +81,51 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//! brief
|
//! \brief Appends the supported extention.
|
||||||
void GetExtensionList(std::string& append)
|
void GetExtensionList(std::string& append);
|
||||||
{
|
|
||||||
append.append("*.obj");
|
|
||||||
}
|
|
||||||
|
|
||||||
//! \brief
|
//! \brief File import implementation.
|
||||||
void InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
|
void InternReadFile(const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Create the data from imported content.
|
||||||
void CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene);
|
void CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Creates all nodes stored in imported content.
|
||||||
aiNode *createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
aiNode *createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
||||||
aiNode *pParent, aiScene* pScene, std::vector<aiMesh*> &MeshArray);
|
aiNode *pParent, aiScene* pScene, std::vector<aiMesh*> &MeshArray);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Creates topology data like faces and meshes for the geometry.
|
||||||
void createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
void createTopology(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
||||||
aiMesh* pMesh);
|
aiMesh* pMesh);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Creates vertices from model.
|
||||||
void createVertexArray(const ObjFile::Model* pModel,
|
void createVertexArray(const ObjFile::Model* pModel,
|
||||||
const ObjFile::Object* pCurrentObject, aiMesh* pMesh);
|
const ObjFile::Object* pCurrentObject, aiMesh* pMesh);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Object counter helper method.
|
||||||
void countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes);
|
void countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Material creation.
|
||||||
void createMaterial(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
void createMaterial(const ObjFile::Model* pModel, const ObjFile::Object* pData,
|
||||||
aiScene* pScene);
|
aiScene* pScene);
|
||||||
|
|
||||||
//! \brief
|
//! \brief Appends a child node to a parentnode and updates the datastructures.
|
||||||
void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
|
void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
//! Data buffer
|
||||||
std::vector<char> m_Buffer;
|
std::vector<char> m_Buffer;
|
||||||
|
//! Pointer to root object instance
|
||||||
ObjFile::Object *m_pRootObject;
|
ObjFile::Object *m_pRootObject;
|
||||||
|
//! Absolute pathname of model in filesystem
|
||||||
std::string m_strAbsPath;
|
std::string m_strAbsPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline void ObjFileImporter::GetExtensionList(std::string& append)
|
||||||
|
{
|
||||||
|
append.append("*.obj");
|
||||||
|
}
|
||||||
|
|
||||||
} // Namespace Assimp
|
} // Namespace Assimp
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -16,14 +16,13 @@ ObjFileMtlImporter::ObjFileMtlImporter( std::vector<char> &buffer,
|
||||||
m_DataIt( buffer.begin() ),
|
m_DataIt( buffer.begin() ),
|
||||||
m_DataItEnd( buffer.end() ),
|
m_DataItEnd( buffer.end() ),
|
||||||
m_uiLine( 0 ),
|
m_uiLine( 0 ),
|
||||||
m_pModel( NULL )
|
m_pModel( pModel )
|
||||||
{
|
{
|
||||||
ai_assert ( NULL != m_pModel );
|
ai_assert ( NULL != m_pModel );
|
||||||
if ( NULL == m_pModel->m_pDefaultMaterial )
|
if ( NULL == m_pModel->m_pDefaultMaterial )
|
||||||
{
|
{
|
||||||
m_pModel->m_pDefaultMaterial = new ObjFile::Material();
|
m_pModel->m_pDefaultMaterial = new ObjFile::Material();
|
||||||
|
m_pModel->m_pDefaultMaterial->MaterialName.Set( "default" );
|
||||||
//m_pModel->m_pDefaultMaterial->
|
|
||||||
}
|
}
|
||||||
load();
|
load();
|
||||||
}
|
}
|
||||||
|
@ -155,29 +154,26 @@ void ObjFileMtlImporter::getFloatValue( float &value )
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
void ObjFileMtlImporter::createMaterial()
|
void ObjFileMtlImporter::createMaterial()
|
||||||
{
|
{
|
||||||
m_pModel->m_pCurrentMaterial = new ObjFile::Material();
|
|
||||||
|
|
||||||
/*m_DataIt = getNextToken<DataArrayIt>( m_DataIt, m_DataItEnd );
|
|
||||||
if (m_DataIt == m_DataItEnd)
|
|
||||||
return;
|
|
||||||
|
|
||||||
char *pStart = &(*m_DataIt);
|
|
||||||
while ( !isSpace(*m_DataIt) && m_DataIt != m_DataItEnd )
|
|
||||||
++m_DataIt;
|
|
||||||
|
|
||||||
// Get name
|
|
||||||
std::string strName(pStart, &(*m_DataIt));
|
|
||||||
if ( strName.empty() )
|
|
||||||
return;*/
|
|
||||||
std::string strName;
|
std::string strName;
|
||||||
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strName );
|
m_DataIt = getName<DataArrayIt>( m_DataIt, m_DataItEnd, strName );
|
||||||
if ( m_DataItEnd == m_DataIt )
|
if ( m_DataItEnd == m_DataIt )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_pModel->m_pCurrentMaterial->MaterialName.Set( strName );
|
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
|
||||||
m_pModel->m_MaterialLib.push_back( strName );
|
if ( m_pModel->m_MaterialMap.end() == it)
|
||||||
m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
|
{
|
||||||
|
// New Material created
|
||||||
|
m_pModel->m_pCurrentMaterial = new ObjFile::Material();
|
||||||
|
m_pModel->m_pCurrentMaterial->MaterialName.Set( strName );
|
||||||
|
m_pModel->m_MaterialLib.push_back( strName );
|
||||||
|
m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Use older material
|
||||||
|
m_pModel->m_pCurrentMaterial = (*it).second;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
|
@ -82,7 +82,10 @@ private:
|
||||||
//! \brief Assignment operator, returns only a reference of this instance.
|
//! \brief Assignment operator, returns only a reference of this instance.
|
||||||
ObjFileMtlImporter &operator = (const ObjFileMtlImporter &rOther);
|
ObjFileMtlImporter &operator = (const ObjFileMtlImporter &rOther);
|
||||||
|
|
||||||
|
//! \brief Load the whole material description
|
||||||
void load();
|
void load();
|
||||||
|
|
||||||
|
//!
|
||||||
void getColorRGBA( aiColor3D *pColor);
|
void getColorRGBA( aiColor3D *pColor);
|
||||||
void getIlluminationModel( int &illum_model );
|
void getIlluminationModel( int &illum_model );
|
||||||
void getFloatValue( float &value );
|
void getFloatValue( float &value );
|
||||||
|
@ -90,11 +93,17 @@ private:
|
||||||
void getTexture();
|
void getTexture();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
//! Absolute pathname
|
||||||
std::string m_strAbsPath;
|
std::string m_strAbsPath;
|
||||||
|
//! Data iterator showing to the current position in data buffer
|
||||||
DataArrayIt m_DataIt;
|
DataArrayIt m_DataIt;
|
||||||
|
//! Data iterator to end of buffer
|
||||||
DataArrayIt m_DataItEnd;
|
DataArrayIt m_DataItEnd;
|
||||||
|
//! USed model instance
|
||||||
ObjFile::Model *m_pModel;
|
ObjFile::Model *m_pModel;
|
||||||
|
//! Current line in file
|
||||||
unsigned int m_uiLine;
|
unsigned int m_uiLine;
|
||||||
|
//! Helper buffer
|
||||||
char m_buffer[BUFFERSIZE];
|
char m_buffer[BUFFERSIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "ObjFileParser.h"
|
#include "ObjFileParser.h"
|
||||||
|
#include "ObjFileMtlImporter.h"
|
||||||
#include "ObjTools.h"
|
#include "ObjTools.h"
|
||||||
#include "ObjFileData.h"
|
#include "ObjFileData.h"
|
||||||
#include "DefaultIOSystem.h"
|
#include "DefaultIOSystem.h"
|
||||||
|
@ -15,7 +16,9 @@ namespace Assimp
|
||||||
{
|
{
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
ObjFileParser::ObjFileParser(std::vector<char> &Data, const std::string &strAbsPath, const std::string &strModelName) :
|
ObjFileParser::ObjFileParser(std::vector<char> &Data,
|
||||||
|
const std::string &strAbsPath,
|
||||||
|
const std::string &strModelName) :
|
||||||
m_strAbsPath(strAbsPath),
|
m_strAbsPath(strAbsPath),
|
||||||
m_DataIt(Data.begin()),
|
m_DataIt(Data.begin()),
|
||||||
m_DataItEnd(Data.end()),
|
m_DataItEnd(Data.end()),
|
||||||
|
@ -284,6 +287,9 @@ void ObjFileParser::getFace()
|
||||||
// Store the new instance
|
// Store the new instance
|
||||||
m_pModel->m_pCurrent->m_Faces.push_back(face);
|
m_pModel->m_pCurrent->m_Faces.push_back(face);
|
||||||
|
|
||||||
|
// Assign face to mesh
|
||||||
|
m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
|
||||||
|
|
||||||
// Skip the rest of the line
|
// Skip the rest of the line
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
@ -298,23 +304,33 @@ void ObjFileParser::getMaterialDesc()
|
||||||
return;
|
return;
|
||||||
|
|
||||||
char *pStart = &(*m_DataIt);
|
char *pStart = &(*m_DataIt);
|
||||||
while (!isSpace(*m_DataIt) && m_DataIt != m_DataItEnd)
|
while ( !isSpace(*m_DataIt) && m_DataIt != m_DataItEnd )
|
||||||
m_DataIt++;
|
++m_DataIt;
|
||||||
|
|
||||||
// Get name
|
// Get name
|
||||||
std::string strName(pStart, &(*m_DataIt));
|
std::string strName(pStart, &(*m_DataIt));
|
||||||
if (strName.empty())
|
if ( strName.empty() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Search for material
|
// Search for material
|
||||||
std::string strFile;
|
|
||||||
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
|
std::map<std::string, ObjFile::Material*>::iterator it = m_pModel->m_MaterialMap.find( strName );
|
||||||
if (it == m_pModel->m_MaterialMap.end())
|
if ( it == m_pModel->m_MaterialMap.end() )
|
||||||
{
|
{
|
||||||
m_pModel->m_pCurrentMaterial = new ObjFile::Material();
|
// Not found, use default material
|
||||||
m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
|
m_pModel->m_pCurrentMaterial = m_pModel->m_pDefaultMaterial;
|
||||||
|
m_pModel->m_MaterialMap[ strName ] = m_pModel->m_pCurrentMaterial;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Found, using detected material
|
||||||
|
m_pModel->m_pCurrentMaterial = (*it).second;
|
||||||
|
|
||||||
|
// Create a new mesh for a new material
|
||||||
|
m_pModel->m_pCurrentMesh = new ObjFile::Mesh();
|
||||||
|
m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip rest of line
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -337,7 +353,7 @@ void ObjFileParser::getComment()
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
//
|
// Get material library from file.
|
||||||
void ObjFileParser::getMaterialLib()
|
void ObjFileParser::getMaterialLib()
|
||||||
{
|
{
|
||||||
// Translate tuple
|
// Translate tuple
|
||||||
|
@ -353,16 +369,18 @@ void ObjFileParser::getMaterialLib()
|
||||||
DefaultIOSystem IOSystem;
|
DefaultIOSystem IOSystem;
|
||||||
std::string strMatName(pStart, &(*m_DataIt));
|
std::string strMatName(pStart, &(*m_DataIt));
|
||||||
std::string absName = m_strAbsPath + IOSystem.getOsSeparator() + strMatName;
|
std::string absName = m_strAbsPath + IOSystem.getOsSeparator() + strMatName;
|
||||||
if (!IOSystem.Exists(absName))
|
if ( !IOSystem.Exists(absName) )
|
||||||
{
|
{
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Extract the extention
|
||||||
std::string strExt("");
|
std::string strExt("");
|
||||||
extractExtension(strMatName, strExt);
|
extractExtension(strMatName, strExt);
|
||||||
std::string mat = "mtl";
|
std::string mat = "mtl";
|
||||||
|
|
||||||
|
// Load the material library
|
||||||
DefaultIOSystem FileSystem;
|
DefaultIOSystem FileSystem;
|
||||||
IOStream *pFile = FileSystem.Open(absName);
|
IOStream *pFile = FileSystem.Open(absName);
|
||||||
if (0L != pFile)
|
if (0L != pFile)
|
||||||
|
@ -371,21 +389,20 @@ void ObjFileParser::getMaterialLib()
|
||||||
std::vector<char> buffer;
|
std::vector<char> buffer;
|
||||||
buffer.resize( size );
|
buffer.resize( size );
|
||||||
|
|
||||||
size_t read_size = pFile->Read( &buffer[ 0 ], sizeof(char), size );
|
size_t read_size = pFile->Read( &buffer[ 0 ], sizeof( char ), size );
|
||||||
FileSystem.Close( pFile );
|
FileSystem.Close( pFile );
|
||||||
|
|
||||||
// TODO: Load mtl file
|
// Importing the material library
|
||||||
|
ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel );
|
||||||
|
m_pModel->m_MaterialLib.push_back( strMatName );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load material library (all materials will be created)
|
|
||||||
m_pModel->m_MaterialLib.push_back(strMatName);
|
|
||||||
|
|
||||||
|
// Skip rest of line
|
||||||
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
//
|
// Set a new material definition as the current material.
|
||||||
void ObjFileParser::getNewMaterial()
|
void ObjFileParser::getNewMaterial()
|
||||||
{
|
{
|
||||||
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
m_DataIt = getNextToken<DataArrayIt>(m_DataIt, m_DataItEnd);
|
||||||
|
@ -414,7 +431,7 @@ void ObjFileParser::getNewMaterial()
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
//
|
// Getter for a group name.
|
||||||
void ObjFileParser::getGroupName()
|
void ObjFileParser::getGroupName()
|
||||||
{
|
{
|
||||||
// Get next word from data buffer
|
// Get next word from data buffer
|
||||||
|
|
|
@ -44,6 +44,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
#include "../include/aiTypes.h"
|
#include "../include/aiTypes.h"
|
||||||
|
|
||||||
/*struct aiVector2D_t;
|
/*struct aiVector2D_t;
|
||||||
|
@ -62,6 +63,8 @@ struct Point2;
|
||||||
}
|
}
|
||||||
class ObjFileImporter;
|
class ObjFileImporter;
|
||||||
|
|
||||||
|
/// \class ObjFileParser
|
||||||
|
/// \brief Parser for a obj waveform file
|
||||||
class ObjFileParser
|
class ObjFileParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -94,13 +97,18 @@ private:
|
||||||
void extractExtension(const std::string strFile, std::string &strExt);
|
void extractExtension(const std::string strFile, std::string &strExt);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
//! Absolute filepath to model
|
||||||
std::string m_strAbsPath;
|
std::string m_strAbsPath;
|
||||||
|
//! Iterator to current position in buffer
|
||||||
DataArrayIt m_DataIt;
|
DataArrayIt m_DataIt;
|
||||||
|
//! Iterator to end position of buffer
|
||||||
DataArrayIt m_DataItEnd;
|
DataArrayIt m_DataItEnd;
|
||||||
|
//! Pointer to model instance
|
||||||
ObjFile::Model *m_pModel;
|
ObjFile::Model *m_pModel;
|
||||||
|
//! Current line (for debugging)
|
||||||
unsigned int m_uiLine;
|
unsigned int m_uiLine;
|
||||||
|
//! Helper buffer
|
||||||
char m_buffer[BUFFERSIZE];
|
char m_buffer[BUFFERSIZE];
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // Namespace Assimp
|
} // Namespace Assimp
|
||||||
|
|
Loading…
Reference in New Issue