CHANGE: Prototype for correct material loading, now the meshes have correct references to the used material.

BUGFIX: Parsing of normals and texture coordinates fixed.

git-svn-id: https://assimp.svn.sourceforge.net/svnroot/assimp/trunk@61 67173fc5-114c-0410-ac8e-9d2fd5bffc1f
pull/1/head
kimmi 2008-07-02 18:12:54 +00:00
parent b422d4e303
commit 0e125c2255
7 changed files with 185 additions and 91 deletions

View File

@ -150,6 +150,14 @@ struct Material
float shineness; float shineness;
//! Illumination model //! Illumination model
int illumination_model; int illumination_model;
Material()
{
}
~Material()
{
}
}; };
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -159,9 +167,18 @@ struct Mesh
{ {
std::vector<Face*> m_Faces; std::vector<Face*> m_Faces;
Material *m_pMaterial; Material *m_pMaterial;
unsigned int m_uiNumIndices;
unsigned int m_uiMaterialIndex;
Mesh() : Mesh() :
m_pMaterial(NULL) m_pMaterial(NULL),
m_uiNumIndices(0),
m_uiMaterialIndex(0)
{
// empty
}
~Mesh()
{ {
// empty // empty
} }

View File

@ -129,8 +129,8 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
// Create mesh pointer buffer for this scene // Create mesh pointer buffer for this scene
if (pScene->mNumMeshes > 0) if (pScene->mNumMeshes > 0)
{ {
pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ]; pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
for (size_t index =0; index < pScene->mNumMeshes; index++) for (size_t index =0; index < MeshArray.size(); index++)
{ {
pScene->mMeshes [ index ] = MeshArray[ index ]; pScene->mMeshes [ index ] = MeshArray[ index ];
} }
@ -159,13 +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 = NULL;
for (unsigned int meshIndex = 0; meshIndex < pModel->m_Meshes.size(); meshIndex++)
{ {
pMesh = new aiMesh();
}
aiMesh *pMesh = new aiMesh();
MeshArray.push_back( pMesh ); MeshArray.push_back( pMesh );
createTopology( pModel, pData, pMesh ); createTopology( pModel, pData, meshIndex, 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,19 +175,21 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
pNode->mNumMeshes = 1; pNode->mNumMeshes = 1;
pNode->mMeshes = new unsigned int[1]; pNode->mMeshes = new unsigned int[1];
// Loop over all child objects // Loop over all child objects, TODO
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 );
for (unsigned int meshIndex = 0; meshIndex < pData->m_SubObjects[ index ]->m_Meshes.size(); meshIndex++)
{
pMesh = new aiMesh(); pMesh = new aiMesh();
MeshArray.push_back( pMesh ); MeshArray.push_back( pMesh );
createTopology( pModel, pData, pMesh ); createTopology( pModel, pData, meshIndex, 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);
} }*/
} }
// Set mesh instances into scene- and node-instances // Set mesh instances into scene- and node-instances
@ -195,7 +197,7 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
if ( meshSizeDiff > 0 ) if ( meshSizeDiff > 0 )
{ {
pNode->mMeshes = new unsigned int[ meshSizeDiff ]; pNode->mMeshes = new unsigned int[ meshSizeDiff ];
pNode->mNumMeshes++; pNode->mNumMeshes = meshSizeDiff;
size_t index = 0; size_t index = 0;
for (size_t i = oldMeshSize; i < MeshArray.size(); i++) for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
{ {
@ -210,86 +212,119 @@ 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,
unsigned int uiMeshIndex,
aiMesh* pMesh ) aiMesh* pMesh )
{ {
// Checking preconditions
ai_assert( NULL != pModel );
if (NULL == pData) if (NULL == pData)
return; return;
// Create mesh vertices
createVertexArray(pModel, pData, pMesh);
// Create faces // Create faces
pMesh->mNumFaces = (unsigned int)pData->m_Faces.size(); ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
pMesh->mNumFaces = (unsigned int) pObjMesh->m_Faces.size();
pMesh->mFaces = new aiFace[ pMesh->mNumFaces ]; pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
//pMesh->mMaterialIndex = pMode; pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
for (size_t index = 0; index < pMesh->mNumFaces; index++)
// Copy all data from all stored meshes
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
{ {
aiFace *pFace = &pMesh->mFaces[ index ]; aiFace *pFace = &pMesh->mFaces[ index ];
pFace->mNumIndices = (unsigned int)pData->m_Faces[index]->m_pVertices->size(); const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
pFace->mNumIndices = (unsigned int) uiNumIndices;
if (pFace->mNumIndices > 0) if (pFace->mNumIndices > 0)
{ {
pFace->mIndices = new unsigned int[ pMesh->mFaces[ index ].mNumIndices ]; pFace->mIndices = new unsigned int[ uiNumIndices ];
ObjFile::Face::IndexArray *pArray = pData->m_Faces[index]->m_pVertices; ObjFile::Face::IndexArray *pIndexArray = pObjMesh->m_Faces[ index ]->m_pVertices;
ai_assert ( NULL != pIndexArray );
// TODO: Should be implement much better
//memcpy(pFace->mIndices, &pData->m_Faces[index]->m_pVertices[0], pFace->mNumIndices * sizeof(unsigned int));
if (pArray != NULL)
{
for ( size_t a=0; a<pFace->mNumIndices; a++ ) for ( size_t a=0; a<pFace->mNumIndices; a++ )
{ {
pFace->mIndices[a] = pArray->at( a ); pFace->mIndices[ a ] = pIndexArray->at( a );
} }
} }
else else
{ {
ai_assert (false); pFace->mIndices = NULL;
}
} }
} }
// Create mesh vertices
createVertexArray(pModel, pData, uiMeshIndex, pMesh);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel, void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
const ObjFile::Object* pCurrentObject, const ObjFile::Object* pCurrentObject,
unsigned int uiMeshIndex,
aiMesh* pMesh) aiMesh* pMesh)
{ {
// Checking preconditions
ai_assert ( NULL != pCurrentObject );
// Break, if no faces are stored in object // Break, if no faces are stored in object
if (pCurrentObject->m_Faces.empty()) if (pCurrentObject->m_Faces.empty())
return; return;
// Get current mesh
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
if ( NULL == pObjMesh )
return;
// Copy vertices of this mesh instance // Copy vertices of this mesh instance
pMesh->mNumVertices = (unsigned int)pModel->m_Vertices.size(); pMesh->mNumVertices = (unsigned int) pObjMesh->m_uiNumIndices;
pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ]; pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
for ( size_t index=0; index < pModel->m_Vertices.size(); index++ )
// Allocate buffer for normal vectors
if ( !pModel->m_Normals.empty() )
pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
// Allocate buffer for texture coordinates
if ( !pModel->m_TextureCoord.empty() )
{ {
pMesh->mVertices[ index ] = *pModel->m_Vertices[ index ]; for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++)
pMesh->mTextureCoords[ i ] = new aiVector3D[ pModel->m_TextureCoord.size() ];
} }
// Copy normals for this mesh // Copy vertices, normals and textures into aiMesh instance
unsigned int newIndex = 0;
for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
{
// get destination face
aiFace *pDestFace = &pMesh->mFaces[ index ];
// get source face
ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
// Copy all index arrays
for ( size_t vertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
{
unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
assert ( vertex < pModel->m_Vertices.size() );
pMesh->mVertices[ newIndex ] = *pModel->m_Vertices[ vertex ];
if ( !pModel->m_Normals.empty() ) if ( !pModel->m_Normals.empty() )
{ {
pMesh->mNormals = new aiVector3D[pModel->m_Normals.size()]; const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
for ( size_t index = 0; index < pModel->m_Normals.size(); index++ ) assert( normal < pModel->m_Normals.size() );
{ pMesh->mNormals[ newIndex ] = *pModel->m_Normals[ normal ];
pMesh->mNormals[ index ] = *pModel->m_Normals[ index ];
}
} }
if ( !pModel->m_TextureCoord.empty() ) if ( !pModel->m_TextureCoord.empty() )
{ {
pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pModel->m_TextureCoord.size() ]; const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
ai_assert ( tex < pModel->m_TextureCoord.size() );
for ( size_t i=0; i < AI_MAX_NUMBER_OF_TEXTURECOORDS; i++)
{
aiVector2D coord2d = *pModel->m_TextureCoord[ tex ];
pMesh->mTextureCoords[ i ][ newIndex ] = aiVector3D( coord2d.x, coord2d.y, 0.0 );
}
}
for( unsigned int e = 0; e < AI_MAX_NUMBER_OF_TEXTURECOORDS; e++) assert( pMesh->mNumVertices > newIndex );
{ pDestFace->mIndices[ vertexIndex ] = newIndex;
if( pMesh->HasTextureCoords( e )) newIndex++;
{
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);
}
}
} }
} }
} }
@ -333,10 +368,14 @@ void ObjFileImporter::createMaterial(const ObjFile::Model* pModel, const ObjFile
// Store material name // Store material name
std::map<std::string, ObjFile::Material*>::const_iterator it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] ); std::map<std::string, ObjFile::Material*>::const_iterator it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
// No material found, use the default material
if ( pModel->m_MaterialMap.end() == it) if ( pModel->m_MaterialMap.end() == it)
continue; continue;
ObjFile::Material *pCurrentMaterial = (*it).second; ObjFile::Material *pCurrentMaterial = (*it).second;
mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME ); mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
mat->AddProperty<int>( &pCurrentMaterial->illumination_model, 1, AI_MATKEY_SHADING_MODEL);
// Adding material colors // Adding material colors
mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT ); mat->AddProperty( &pCurrentMaterial->ambient, 1, AI_MATKEY_COLOR_AMBIENT );
@ -352,6 +391,9 @@ void ObjFileImporter::createMaterial(const ObjFile::Model* pModel, const ObjFile
pScene->mMaterials[ pScene->mNumMaterials ] = mat; pScene->mMaterials[ pScene->mNumMaterials ] = mat;
pScene->mNumMaterials++; pScene->mNumMaterials++;
} }
// Test number of created materials.
ai_assert( pScene->mNumMaterials == numMaterials );
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------

View File

@ -96,11 +96,11 @@ private:
//! \brief Creates topology data like faces and meshes for the geometry. //! \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); unsigned int uiMeshIndex, aiMesh* pMesh);
//! \brief Creates vertices from model. //! \brief Creates vertices from model.
void createVertexArray(const ObjFile::Model* pModel, void createVertexArray(const ObjFile::Model* pModel, const ObjFile::Object* pCurrentObject,
const ObjFile::Object* pCurrentObject, aiMesh* pMesh); unsigned int uiMeshIndex, aiMesh* pMesh);
//! \brief Object counter helper method. //! \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);
@ -112,6 +112,9 @@ private:
//! \brief Appends a child node to a parentnode and updates the datastructures. //! \brief Appends a child node to a parentnode and updates the datastructures.
void appendChildToParentNode(aiNode *pParent, aiNode *pChild); void appendChildToParentNode(aiNode *pParent, aiNode *pChild);
//! \brief
void createAnimations();
private: private:
//! Data buffer //! Data buffer
std::vector<char> m_Buffer; std::vector<char> m_Buffer;

View File

@ -71,11 +71,13 @@ void ObjFileParser::parseFile()
else if (*m_DataIt == 't') else if (*m_DataIt == 't')
{ {
// Read in texture coordinate (2D) // Read in texture coordinate (2D)
++m_DataIt;
getVector2(m_pModel->m_TextureCoord); getVector2(m_pModel->m_TextureCoord);
} }
else if (*m_DataIt == 'n') else if (*m_DataIt == 'n')
{ {
// Read in normal vector definition // Read in normal vector definition
++m_DataIt;
getVector3(m_pModel->m_Normals); getVector3(m_pModel->m_Normals);
} }
} }
@ -299,6 +301,8 @@ void ObjFileParser::getFace()
m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh ); m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
} }
m_pModel->m_pCurrentMesh->m_Faces.push_back( face ); m_pModel->m_pCurrentMesh->m_Faces.push_back( face );
if ( !face->m_pVertices->empty() )
m_pModel->m_pCurrentMesh->m_uiNumIndices += face->m_pVertices->size();
// 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 );
@ -338,6 +342,7 @@ void ObjFileParser::getMaterialDesc()
// Create a new mesh for a new material // Create a new mesh for a new material
m_pModel->m_pCurrentMesh = new ObjFile::Mesh(); m_pModel->m_pCurrentMesh = new ObjFile::Mesh();
m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh ); m_pModel->m_Meshes.push_back( m_pModel->m_pCurrentMesh );
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strName );
} }
// Skip rest of line // Skip rest of line
@ -388,23 +393,22 @@ void ObjFileParser::getMaterialLib()
// Extract the extention // Extract the extention
std::string strExt(""); std::string strExt("");
extractExtension( strMatName, strExt ); extractExtension( strMatName, strExt );
std::string mat = "mtl"; static const std::string mat = "mtl";
// Load the material library // Load the material library
DefaultIOSystem FileSystem; DefaultIOSystem FileSystem;
IOStream *pFile = FileSystem.Open(absName); IOStream *pFile = FileSystem.Open(absName);
if (0L != pFile) if (0L != pFile)
{ {
// Import material library data from file
size_t size = pFile->FileSize(); size_t size = pFile->FileSize();
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 );
// Importing the material library // Importing the material library
ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel ); ObjFileMtlImporter mtlImporter( buffer, absName, m_pModel );
m_pModel->m_MaterialLib.push_back( strMatName );
} }
// Skip rest of line // Skip rest of line
@ -435,11 +439,29 @@ void ObjFileParser::getNewMaterial()
{ {
// Set new material // Set new material
m_pModel->m_pCurrentMaterial = (*it).second; m_pModel->m_pCurrentMaterial = (*it).second;
m_pModel->m_pCurrentMesh->m_uiMaterialIndex = getMaterialIndex( strMat );
} }
m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine ); m_DataIt = skipLine<DataArrayIt>( m_DataIt, m_DataItEnd, m_uiLine );
} }
// -------------------------------------------------------------------
int ObjFileParser::getMaterialIndex( const std::string &strMaterialName )
{
int mat_index = -1;
if ( strMaterialName.empty() )
return mat_index;
for (size_t index = 0; index < m_pModel->m_MaterialLib.size(); ++index)
{
if ( strMaterialName == m_pModel->m_MaterialLib[ index ])
{
mat_index = index;
break;
}
}
return mat_index;
}
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// Getter for a group name. // Getter for a group name.
void ObjFileParser::getGroupName() void ObjFileParser::getGroupName()

View File

@ -91,6 +91,7 @@ private:
void getNewMaterial(); void getNewMaterial();
void getGroupName(); void getGroupName();
void getGroupNumber(); void getGroupNumber();
int getMaterialIndex( const std::string &strMaterialName );
void getObjectName(); void getObjectName();
void createObject(const std::string &strObjectName); void createObject(const std::string &strObjectName);
void reportErrorTokenInFace(); void reportErrorTokenInFace();

View File

@ -428,34 +428,43 @@ int CDisplay::AddTextureToDisplayList(unsigned int iType,
{ {
case AI_TEXTYPE_DIFFUSE: case AI_TEXTYPE_DIFFUSE:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piDiffuseTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piDiffuseTexture;
szType = "Diffuse";break; szType = "Diffuse";
break;
case AI_TEXTYPE_SPECULAR: case AI_TEXTYPE_SPECULAR:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piSpecularTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piSpecularTexture;
szType = "Specular";break; szType = "Specular";
break;
case AI_TEXTYPE_AMBIENT: case AI_TEXTYPE_AMBIENT:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piAmbientTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piAmbientTexture;
szType = "Ambient";break; szType = "Ambient";
break;
case AI_TEXTYPE_EMISSIVE: case AI_TEXTYPE_EMISSIVE:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piEmissiveTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piEmissiveTexture;
szType = "Emissive";break; szType = "Emissive";
break;
case AI_TEXTYPE_HEIGHT: case AI_TEXTYPE_HEIGHT:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture;
szType = "HeightMap";break; szType = "HeightMap";
break;
case AI_TEXTYPE_NORMALS: case AI_TEXTYPE_NORMALS:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piNormalTexture;
szType = "NormalMap";break; szType = "NormalMap";
break;
case AI_TEXTYPE_SHININESS: case AI_TEXTYPE_SHININESS:
piTexture = &g_pcAsset->apcMeshes[iMesh]->piShininessTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piShininessTexture;
szType = "Shininess";break; szType = "Shininess";
break;
default: // opacity + opacity | mask default: // opacity + opacity | mask
piTexture = &g_pcAsset->apcMeshes[iMesh]->piOpacityTexture; piTexture = &g_pcAsset->apcMeshes[iMesh]->piOpacityTexture;
szType = "Opacity";break; szType = "Opacity";
break;
}; };
if (bIsExtraOpacity) if (bIsExtraOpacity)
{ {
sprintf(chTemp,"%s %i (<copy of diffuse #1>)",szType,iIndex+1); sprintf(chTemp,"%s %i (<copy of diffuse #1>)",szType,iIndex+1);
} }
else sprintf(chTemp,"%s %i (%s)",szType,iIndex+1,sz); else
sprintf(chTemp,"%s %i (%s)",szType,iIndex+1,sz);
TVITEMEX tvi; TVITEMEX tvi;
TVINSERTSTRUCT sNew; TVINSERTSTRUCT sNew;