2008-08-14 21:25:50 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2008-08-14 21:25:50 +00:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2008-08-14 21:25:50 +00:00
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2008-08-14 21:25:50 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2008-08-14 21:25:50 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
2008-10-13 16:45:48 +00:00
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2009-01-18 23:48:25 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_OBJ_IMPORTER
|
2008-10-13 16:45:48 +00:00
|
|
|
|
2009-03-08 16:27:36 +00:00
|
|
|
#include "DefaultIOSystem.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
#include "ObjFileImporter.h"
|
|
|
|
#include "ObjFileParser.h"
|
|
|
|
#include "ObjFileData.h"
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
"Wavefront Object Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"surfaces not supported",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"obj"
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
namespace Assimp {
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Default constructor
|
|
|
|
ObjFileImporter::ObjFileImporter() :
|
2010-07-08 21:25:48 +00:00
|
|
|
m_Buffer(),
|
|
|
|
m_pRootObject( NULL ),
|
|
|
|
m_strAbsPath( "" )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-08 16:27:36 +00:00
|
|
|
DefaultIOSystem io;
|
|
|
|
m_strAbsPath = io.getOsSeparator();
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-07-08 21:25:48 +00:00
|
|
|
// Destructor.
|
2008-05-05 12:36:31 +00:00
|
|
|
ObjFileImporter::~ObjFileImporter()
|
|
|
|
{
|
|
|
|
// Release root object instance
|
|
|
|
if (NULL != m_pRootObject)
|
|
|
|
{
|
|
|
|
delete m_pRootObject;
|
|
|
|
m_pRootObject = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-07-08 21:25:48 +00:00
|
|
|
// Returns true, if file is an obj file.
|
2009-09-30 17:55:21 +00:00
|
|
|
bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , bool checkSig ) const
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-09-30 17:55:21 +00:00
|
|
|
if(!checkSig) //Check File Extension
|
|
|
|
{
|
|
|
|
return SimpleExtensionCheck(pFile,"obj");
|
|
|
|
}
|
|
|
|
else //Check file Header
|
|
|
|
{
|
2011-03-18 12:21:26 +00:00
|
|
|
static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " };
|
|
|
|
return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 );
|
2009-09-30 17:55:21 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
const aiImporterDesc* ObjFileImporter::GetInfo () const
|
|
|
|
{
|
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Obj-file import implementation
|
|
|
|
void ObjFileImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOSystem* pIOHandler)
|
|
|
|
{
|
2009-03-08 16:27:36 +00:00
|
|
|
DefaultIOSystem io;
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// Read file into memory
|
2011-09-23 16:45:17 +00:00
|
|
|
const std::string mode = "rb";
|
2008-05-05 12:36:31 +00:00
|
|
|
boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, mode));
|
|
|
|
if (NULL == file.get())
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "Failed to open file " + pFile + ".");
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2011-09-23 16:45:17 +00:00
|
|
|
// Get the file-size and validate it, throwing an exception when fails
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t fileSize = file->FileSize();
|
|
|
|
if( fileSize < 16)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError( "OBJ-file is too small.");
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// Allocate buffer and read file into it
|
2009-08-21 22:49:58 +00:00
|
|
|
TextFileToBuffer(file.get(),m_Buffer);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2010-01-31 16:53:08 +00:00
|
|
|
// Get the model name
|
2009-08-24 08:46:04 +00:00
|
|
|
std::string strModelName;
|
|
|
|
std::string::size_type pos = pFile.find_last_of( "\\/" );
|
2010-01-31 16:53:08 +00:00
|
|
|
if ( pos != std::string::npos )
|
|
|
|
{
|
2008-05-05 12:36:31 +00:00
|
|
|
strModelName = pFile.substr(pos+1, pFile.size() - pos - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strModelName = pFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the file into a temporary representation
|
2009-08-24 08:46:04 +00:00
|
|
|
ObjFileParser parser(m_Buffer, strModelName, pIOHandler);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// And create the proper return structures out of it
|
|
|
|
CreateDataFromImport(parser.GetModel(), pScene);
|
2009-08-21 22:49:58 +00:00
|
|
|
|
|
|
|
// Clean up allocated storage for the next import
|
|
|
|
m_Buffer.clear();
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Create the data from parsed obj-file
|
|
|
|
void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene* pScene)
|
|
|
|
{
|
|
|
|
if (0L == pModel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create the root node of the scene
|
2010-05-30 12:45:38 +00:00
|
|
|
pScene->mRootNode = new aiNode;
|
|
|
|
if ( !pModel->m_ModelName.empty() )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
// Set the name of the scene
|
|
|
|
pScene->mRootNode->mName.Set(pModel->m_ModelName);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is an error, so break down the application
|
2010-05-13 15:50:42 +00:00
|
|
|
ai_assert(false);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create nodes for the whole scene
|
|
|
|
std::vector<aiMesh*> MeshArray;
|
|
|
|
for (size_t index = 0; index < pModel->m_Objects.size(); index++)
|
|
|
|
{
|
2009-11-27 14:33:29 +00:00
|
|
|
createNodes(pModel, pModel->m_Objects[ index ], index, pScene->mRootNode, pScene, MeshArray);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create mesh pointer buffer for this scene
|
|
|
|
if (pScene->mNumMeshes > 0)
|
|
|
|
{
|
2008-07-02 18:12:54 +00:00
|
|
|
pScene->mMeshes = new aiMesh*[ MeshArray.size() ];
|
|
|
|
for (size_t index =0; index < MeshArray.size(); index++)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
pScene->mMeshes [ index ] = MeshArray[ index ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all materials
|
2010-01-31 16:53:08 +00:00
|
|
|
createMaterials( pModel, pScene );
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Creates all nodes of the model
|
2010-05-30 12:45:38 +00:00
|
|
|
aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile::Object* pObject,
|
2011-06-09 14:29:32 +00:00
|
|
|
unsigned int /*uiMeshIndex*/,
|
2008-05-05 12:36:31 +00:00
|
|
|
aiNode *pParent, aiScene* pScene,
|
2010-05-30 12:45:38 +00:00
|
|
|
std::vector<aiMesh*> &MeshArray )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2010-01-31 16:53:08 +00:00
|
|
|
ai_assert( NULL != pModel );
|
2010-05-30 12:45:38 +00:00
|
|
|
if ( NULL == pObject )
|
2008-05-05 12:36:31 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-01-09 19:36:18 +00:00
|
|
|
// Store older mesh size to be able to computes mesh offsets for new mesh instances
|
2010-01-31 16:53:08 +00:00
|
|
|
const size_t oldMeshSize = MeshArray.size();
|
2010-05-30 12:45:38 +00:00
|
|
|
aiNode *pNode = new aiNode;
|
2011-01-09 19:36:18 +00:00
|
|
|
|
|
|
|
pNode->mName = pObject->m_strObjName;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2011-09-23 16:45:17 +00:00
|
|
|
// If we have a parent node, store it
|
2008-05-05 12:36:31 +00:00
|
|
|
if (pParent != NULL)
|
2010-05-30 12:45:38 +00:00
|
|
|
appendChildToParentNode(pParent, pNode);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2010-05-30 12:45:38 +00:00
|
|
|
for ( unsigned int i=0; i< pObject->m_Meshes.size(); i++ )
|
2010-01-31 16:53:08 +00:00
|
|
|
{
|
2010-05-30 12:45:38 +00:00
|
|
|
unsigned int meshId = pObject->m_Meshes[ i ];
|
|
|
|
aiMesh *pMesh = new aiMesh;
|
|
|
|
createTopology( pModel, pObject, meshId, pMesh );
|
|
|
|
if ( pMesh->mNumVertices > 0 )
|
|
|
|
{
|
|
|
|
MeshArray.push_back( pMesh );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pMesh;
|
|
|
|
}
|
2008-06-03 21:32:56 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2010-05-30 12:45:38 +00:00
|
|
|
// Create all nodes from the sub-objects stored in the current object
|
|
|
|
if ( !pObject->m_SubObjects.empty() )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2010-05-30 12:45:38 +00:00
|
|
|
size_t numChilds = pObject->m_SubObjects.size();
|
|
|
|
pNode->mNumChildren = static_cast<unsigned int>( numChilds );
|
|
|
|
pNode->mChildren = new aiNode*[ numChilds ];
|
2008-05-05 12:36:31 +00:00
|
|
|
pNode->mNumMeshes = 1;
|
2010-05-30 12:45:38 +00:00
|
|
|
pNode->mMeshes = new unsigned int[ 1 ];
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set mesh instances into scene- and node-instances
|
|
|
|
const size_t meshSizeDiff = MeshArray.size()- oldMeshSize;
|
2008-07-02 18:12:54 +00:00
|
|
|
if ( meshSizeDiff > 0 )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
pNode->mMeshes = new unsigned int[ meshSizeDiff ];
|
2010-05-30 12:45:38 +00:00
|
|
|
pNode->mNumMeshes = static_cast<unsigned int>( meshSizeDiff );
|
2008-05-05 12:36:31 +00:00
|
|
|
size_t index = 0;
|
|
|
|
for (size_t i = oldMeshSize; i < MeshArray.size(); i++)
|
|
|
|
{
|
|
|
|
pNode->mMeshes[ index ] = pScene->mNumMeshes;
|
|
|
|
pScene->mNumMeshes++;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Create topology data
|
2008-07-02 18:12:54 +00:00
|
|
|
void ObjFileImporter::createTopology(const ObjFile::Model* pModel,
|
|
|
|
const ObjFile::Object* pData,
|
|
|
|
unsigned int uiMeshIndex,
|
2008-06-03 21:32:56 +00:00
|
|
|
aiMesh* pMesh )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-07-02 18:12:54 +00:00
|
|
|
// Checking preconditions
|
|
|
|
ai_assert( NULL != pModel );
|
2008-05-05 12:36:31 +00:00
|
|
|
if (NULL == pData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Create faces
|
2008-07-02 18:12:54 +00:00
|
|
|
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
|
2008-08-14 21:25:50 +00:00
|
|
|
ai_assert( NULL != pObjMesh );
|
2011-08-23 16:23:13 +00:00
|
|
|
|
|
|
|
pMesh->mNumFaces = 0;
|
|
|
|
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
|
|
|
|
{
|
|
|
|
ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
|
|
|
|
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
|
|
|
|
pMesh->mNumFaces += inp->m_pVertices->size() - 1;
|
|
|
|
}
|
|
|
|
else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
|
|
|
|
pMesh->mNumFaces += inp->m_pVertices->size();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
++pMesh->mNumFaces;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int uiIdxCount = 0u;
|
2008-08-14 21:25:50 +00:00
|
|
|
if ( pMesh->mNumFaces > 0 )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-08-14 21:25:50 +00:00
|
|
|
pMesh->mFaces = new aiFace[ pMesh->mNumFaces ];
|
2010-05-30 12:45:38 +00:00
|
|
|
if ( pObjMesh->m_uiMaterialIndex != ObjFile::Mesh::NoMaterial )
|
|
|
|
{
|
|
|
|
pMesh->mMaterialIndex = pObjMesh->m_uiMaterialIndex;
|
|
|
|
}
|
2008-08-14 21:25:50 +00:00
|
|
|
|
2011-08-23 16:23:13 +00:00
|
|
|
unsigned int outIndex = 0;
|
|
|
|
|
2008-08-14 21:25:50 +00:00
|
|
|
// Copy all data from all stored meshes
|
|
|
|
for (size_t index = 0; index < pObjMesh->m_Faces.size(); index++)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2011-08-23 16:23:13 +00:00
|
|
|
ObjFile::Face* const inp = pObjMesh->m_Faces[ index ];
|
|
|
|
if (inp->m_PrimitiveType == aiPrimitiveType_LINE) {
|
|
|
|
for(size_t i = 0; i < inp->m_pVertices->size() - 1; ++i) {
|
|
|
|
aiFace& f = pMesh->mFaces[ outIndex++ ];
|
|
|
|
uiIdxCount += f.mNumIndices = 2;
|
|
|
|
f.mIndices = new unsigned int[2];
|
2008-08-14 21:25:50 +00:00
|
|
|
}
|
2011-08-23 16:23:13 +00:00
|
|
|
continue;
|
2008-08-14 21:25:50 +00:00
|
|
|
}
|
2011-08-23 16:23:13 +00:00
|
|
|
else if (inp->m_PrimitiveType == aiPrimitiveType_POINT) {
|
|
|
|
for(size_t i = 0; i < inp->m_pVertices->size(); ++i) {
|
|
|
|
aiFace& f = pMesh->mFaces[ outIndex++ ];
|
|
|
|
uiIdxCount += f.mNumIndices = 1;
|
|
|
|
f.mIndices = new unsigned int[1];
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
aiFace *pFace = &pMesh->mFaces[ outIndex++ ];
|
|
|
|
const unsigned int uiNumIndices = (unsigned int) pObjMesh->m_Faces[ index ]->m_pVertices->size();
|
|
|
|
uiIdxCount += pFace->mNumIndices = (unsigned int) uiNumIndices;
|
|
|
|
if (pFace->mNumIndices > 0) {
|
|
|
|
pFace->mIndices = new unsigned int[ uiNumIndices ];
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
|
|
|
|
// Create mesh vertices
|
2011-08-23 16:23:13 +00:00
|
|
|
createVertexArray(pModel, pData, uiMeshIndex, pMesh, uiIdxCount);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2010-05-30 12:45:38 +00:00
|
|
|
// Creates a vertex array
|
2008-05-05 12:36:31 +00:00
|
|
|
void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
|
|
|
|
const ObjFile::Object* pCurrentObject,
|
2008-07-02 18:12:54 +00:00
|
|
|
unsigned int uiMeshIndex,
|
2011-08-23 16:23:13 +00:00
|
|
|
aiMesh* pMesh,
|
|
|
|
unsigned int uiIdxCount)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-07-02 18:12:54 +00:00
|
|
|
// Checking preconditions
|
2008-08-14 21:25:50 +00:00
|
|
|
ai_assert( NULL != pCurrentObject );
|
2008-07-02 18:12:54 +00:00
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
// Break, if no faces are stored in object
|
2010-05-30 12:45:38 +00:00
|
|
|
if ( pCurrentObject->m_Meshes.empty() )
|
2008-05-05 12:36:31 +00:00
|
|
|
return;
|
2008-07-02 18:12:54 +00:00
|
|
|
|
|
|
|
// Get current mesh
|
|
|
|
ObjFile::Mesh *pObjMesh = pModel->m_Meshes[ uiMeshIndex ];
|
2009-09-29 11:01:20 +00:00
|
|
|
if ( NULL == pObjMesh || pObjMesh->m_uiNumIndices < 1)
|
2008-07-02 18:12:54 +00:00
|
|
|
return;
|
|
|
|
|
2008-06-03 21:32:56 +00:00
|
|
|
// Copy vertices of this mesh instance
|
2011-08-23 16:23:13 +00:00
|
|
|
pMesh->mNumVertices = uiIdxCount;
|
2008-06-03 21:32:56 +00:00
|
|
|
pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-07-02 18:12:54 +00:00
|
|
|
// Allocate buffer for normal vectors
|
2008-09-22 08:15:18 +00:00
|
|
|
if ( !pModel->m_Normals.empty() && pObjMesh->m_hasNormals )
|
2008-07-02 18:12:54 +00:00
|
|
|
pMesh->mNormals = new aiVector3D[ pMesh->mNumVertices ];
|
|
|
|
|
|
|
|
// Allocate buffer for texture coordinates
|
2009-01-19 00:40:48 +00:00
|
|
|
if ( !pModel->m_TextureCoord.empty() && pObjMesh->m_uiUVCoordinates[0] )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-08-02 17:11:38 +00:00
|
|
|
pMesh->mNumUVComponents[ 0 ] = 2;
|
2011-08-23 16:23:13 +00:00
|
|
|
pMesh->mTextureCoords[ 0 ] = new aiVector3D[ pMesh->mNumVertices ];
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
|
|
|
|
// Copy vertices, normals and textures into aiMesh instance
|
2011-08-23 16:23:13 +00:00
|
|
|
unsigned int newIndex = 0, outIndex = 0;
|
2008-07-02 18:12:54 +00:00
|
|
|
for ( size_t index=0; index < pObjMesh->m_Faces.size(); index++ )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-08-14 21:25:50 +00:00
|
|
|
// Get source face
|
2008-07-02 18:12:54 +00:00
|
|
|
ObjFile::Face *pSourceFace = pObjMesh->m_Faces[ index ];
|
2008-06-03 21:32:56 +00:00
|
|
|
|
2008-07-02 18:12:54 +00:00
|
|
|
// Copy all index arrays
|
2011-08-23 16:23:13 +00:00
|
|
|
for ( size_t vertexIndex = 0, outVertexIndex = 0; vertexIndex < pSourceFace->m_pVertices->size(); vertexIndex++ )
|
2008-06-03 21:32:56 +00:00
|
|
|
{
|
2010-08-07 15:10:27 +00:00
|
|
|
const unsigned int vertex = pSourceFace->m_pVertices->at( vertexIndex );
|
2011-09-23 16:45:17 +00:00
|
|
|
if ( vertex >= pModel->m_Vertices.size() )
|
|
|
|
throw DeadlyImportError( "OBJ: vertex index out of range" );
|
2011-05-07 11:14:06 +00:00
|
|
|
|
2009-04-19 10:36:36 +00:00
|
|
|
pMesh->mVertices[ newIndex ] = pModel->m_Vertices[ vertex ];
|
2008-07-02 18:12:54 +00:00
|
|
|
|
2008-08-14 21:25:50 +00:00
|
|
|
// Copy all normals
|
2011-05-29 20:38:46 +00:00
|
|
|
if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty())
|
2008-06-03 21:32:56 +00:00
|
|
|
{
|
2008-07-02 18:12:54 +00:00
|
|
|
const unsigned int normal = pSourceFace->m_pNormals->at( vertexIndex );
|
2011-09-23 16:45:17 +00:00
|
|
|
if ( normal >= pModel->m_Normals.size() )
|
2011-05-07 11:14:06 +00:00
|
|
|
throw DeadlyImportError("OBJ: vertex normal index out of range");
|
|
|
|
|
2009-04-19 10:36:36 +00:00
|
|
|
pMesh->mNormals[ newIndex ] = pModel->m_Normals[ normal ];
|
2008-07-02 18:12:54 +00:00
|
|
|
}
|
|
|
|
|
2008-08-14 21:25:50 +00:00
|
|
|
// Copy all texture coordinates
|
2008-07-02 18:12:54 +00:00
|
|
|
if ( !pModel->m_TextureCoord.empty() )
|
|
|
|
{
|
2008-09-09 22:32:04 +00:00
|
|
|
if ( !pSourceFace->m_pTexturCoords->empty() )
|
2008-06-03 21:32:56 +00:00
|
|
|
{
|
2008-09-09 22:32:04 +00:00
|
|
|
const unsigned int tex = pSourceFace->m_pTexturCoords->at( vertexIndex );
|
|
|
|
ai_assert( tex < pModel->m_TextureCoord.size() );
|
2011-08-23 16:23:13 +00:00
|
|
|
for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ )
|
2008-08-14 21:25:50 +00:00
|
|
|
{
|
2011-09-23 16:45:17 +00:00
|
|
|
if ( tex >= pModel->m_TextureCoord.size() )
|
|
|
|
throw DeadlyImportError("OBJ: texture coord index out of range");
|
|
|
|
|
2011-08-23 16:23:13 +00:00
|
|
|
aiVector2D coord2d = pModel->m_TextureCoord[ tex ];
|
|
|
|
pMesh->mTextureCoords[ i ][ newIndex ] = aiVector3D( coord2d.x, coord2d.y, 0.0 );
|
2008-08-14 21:25:50 +00:00
|
|
|
}
|
2008-06-03 21:32:56 +00:00
|
|
|
}
|
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
|
2008-08-14 21:25:50 +00:00
|
|
|
ai_assert( pMesh->mNumVertices > newIndex );
|
2011-08-23 16:23:13 +00:00
|
|
|
|
|
|
|
// Get destination face
|
|
|
|
aiFace *pDestFace = &pMesh->mFaces[ outIndex ];
|
|
|
|
|
2011-09-23 16:45:17 +00:00
|
|
|
const bool last = ( vertexIndex == pSourceFace->m_pVertices->size() - 1 );
|
|
|
|
if (pSourceFace->m_PrimitiveType != aiPrimitiveType_LINE || !last)
|
|
|
|
{
|
|
|
|
pDestFace->mIndices[ outVertexIndex ] = newIndex;
|
|
|
|
outVertexIndex++;
|
2011-08-23 16:23:13 +00:00
|
|
|
}
|
|
|
|
|
2011-09-23 16:45:17 +00:00
|
|
|
if (pSourceFace->m_PrimitiveType == aiPrimitiveType_POINT)
|
|
|
|
{
|
2011-08-23 16:23:13 +00:00
|
|
|
outIndex++;
|
|
|
|
outVertexIndex = 0;
|
|
|
|
}
|
2011-09-23 16:45:17 +00:00
|
|
|
else if (pSourceFace->m_PrimitiveType == aiPrimitiveType_LINE)
|
|
|
|
{
|
2011-08-23 16:23:13 +00:00
|
|
|
outVertexIndex = 0;
|
|
|
|
|
2011-09-23 16:45:17 +00:00
|
|
|
if(!last)
|
2011-08-23 16:23:13 +00:00
|
|
|
outIndex++;
|
|
|
|
|
|
|
|
if (vertexIndex) {
|
|
|
|
if(!last) {
|
|
|
|
pMesh->mVertices[ newIndex+1 ] = pMesh->mVertices[ newIndex ];
|
|
|
|
if ( !pSourceFace->m_pNormals->empty() && !pModel->m_Normals.empty()) {
|
|
|
|
pMesh->mNormals[ newIndex+1 ] = pMesh->mNormals[newIndex ];
|
|
|
|
}
|
|
|
|
if ( !pModel->m_TextureCoord.empty() ) {
|
|
|
|
for ( size_t i=0; i < pMesh->GetNumUVChannels(); i++ ) {
|
|
|
|
pMesh->mTextureCoords[ i ][ newIndex+1 ] = pMesh->mTextureCoords[ i ][ newIndex ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++newIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
pDestFace[-1].mIndices[1] = newIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (last) {
|
|
|
|
outIndex++;
|
|
|
|
}
|
2008-08-14 21:25:50 +00:00
|
|
|
++newIndex;
|
2008-06-03 21:32:56 +00:00
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-08-14 21:25:50 +00:00
|
|
|
// Counts all stored meshes
|
2008-05-05 12:36:31 +00:00
|
|
|
void ObjFileImporter::countObjects(const std::vector<ObjFile::Object*> &rObjects, int &iNumMeshes)
|
|
|
|
{
|
|
|
|
iNumMeshes = 0;
|
2010-05-30 12:45:38 +00:00
|
|
|
if ( rObjects.empty() )
|
2008-05-05 12:36:31 +00:00
|
|
|
return;
|
|
|
|
|
2010-05-30 12:45:38 +00:00
|
|
|
iNumMeshes += static_cast<unsigned int>( rObjects.size() );
|
2008-05-05 12:36:31 +00:00
|
|
|
for (std::vector<ObjFile::Object*>::const_iterator it = rObjects.begin();
|
|
|
|
it != rObjects.end();
|
|
|
|
++it)
|
|
|
|
{
|
|
|
|
if (!(*it)->m_SubObjects.empty())
|
|
|
|
{
|
|
|
|
countObjects((*it)->m_SubObjects, iNumMeshes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-08-19 20:45:51 +00:00
|
|
|
// Creates the material
|
2010-01-31 16:53:08 +00:00
|
|
|
void ObjFileImporter::createMaterials(const ObjFile::Model* pModel, aiScene* pScene )
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2010-01-31 16:53:08 +00:00
|
|
|
ai_assert( NULL != pScene );
|
|
|
|
if ( NULL == pScene )
|
2008-05-05 12:36:31 +00:00
|
|
|
return;
|
|
|
|
|
2008-06-03 21:32:56 +00:00
|
|
|
const unsigned int numMaterials = (unsigned int) pModel->m_MaterialLib.size();
|
|
|
|
pScene->mNumMaterials = 0;
|
2011-05-07 11:14:06 +00:00
|
|
|
if ( pModel->m_MaterialLib.empty() ) {
|
|
|
|
DefaultLogger::get()->debug("OBJ: no materials specified");
|
2008-06-03 21:32:56 +00:00
|
|
|
return;
|
2011-05-07 11:14:06 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-06-03 21:32:56 +00:00
|
|
|
pScene->mMaterials = new aiMaterial*[ numMaterials ];
|
|
|
|
for ( unsigned int matIndex = 0; matIndex < numMaterials; matIndex++ )
|
2012-02-26 13:36:29 +00:00
|
|
|
{
|
2008-06-03 21:32:56 +00:00
|
|
|
// Store material name
|
2010-05-30 12:45:38 +00:00
|
|
|
std::map<std::string, ObjFile::Material*>::const_iterator it;
|
|
|
|
it = pModel->m_MaterialMap.find( pModel->m_MaterialLib[ matIndex ] );
|
2008-07-02 18:12:54 +00:00
|
|
|
|
|
|
|
// No material found, use the default material
|
2010-05-30 12:45:38 +00:00
|
|
|
if ( pModel->m_MaterialMap.end() == it )
|
2008-06-03 21:32:56 +00:00
|
|
|
continue;
|
2008-07-02 18:12:54 +00:00
|
|
|
|
2012-02-26 13:36:29 +00:00
|
|
|
aiMaterial* mat = new aiMaterial;
|
2008-06-03 21:32:56 +00:00
|
|
|
ObjFile::Material *pCurrentMaterial = (*it).second;
|
|
|
|
mat->AddProperty( &pCurrentMaterial->MaterialName, AI_MATKEY_NAME );
|
2009-02-05 18:13:34 +00:00
|
|
|
|
|
|
|
// convert illumination model
|
2010-05-30 12:45:38 +00:00
|
|
|
int sm = 0;
|
2009-08-19 20:45:51 +00:00
|
|
|
switch (pCurrentMaterial->illumination_model)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
sm = aiShadingMode_NoShading;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sm = aiShadingMode_Gouraud;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
sm = aiShadingMode_Phong;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
sm = aiShadingMode_Gouraud;
|
2011-05-07 11:14:06 +00:00
|
|
|
DefaultLogger::get()->error("OBJ: unexpected illumination model (0-2 recognized)");
|
2009-02-05 18:13:34 +00:00
|
|
|
}
|
2012-02-26 13:36:29 +00:00
|
|
|
|
2009-02-05 18:13:34 +00:00
|
|
|
mat->AddProperty<int>( &sm, 1, AI_MATKEY_SHADING_MODEL);
|
2008-07-02 18:12:54 +00:00
|
|
|
|
2009-05-16 15:33:01 +00:00
|
|
|
// multiplying the specular exponent with 2 seems to yield better results
|
|
|
|
pCurrentMaterial->shineness *= 4.f;
|
|
|
|
|
2008-06-03 21:32:56 +00:00
|
|
|
// Adding material colors
|
2008-07-02 18:12:54 +00:00
|
|
|
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 );
|
2009-05-10 00:08:06 +00:00
|
|
|
mat->AddProperty( &pCurrentMaterial->alpha, 1, AI_MATKEY_OPACITY );
|
2008-06-03 21:32:56 +00:00
|
|
|
|
2009-02-08 21:22:03 +00:00
|
|
|
// Adding refraction index
|
|
|
|
mat->AddProperty( &pCurrentMaterial->ior, 1, AI_MATKEY_REFRACTI );
|
|
|
|
|
2008-06-03 21:32:56 +00:00
|
|
|
// Adding textures
|
|
|
|
if ( 0 != pCurrentMaterial->texture.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->texture, AI_MATKEY_TEXTURE_DIFFUSE(0));
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
if ( 0 != pCurrentMaterial->textureAmbient.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureAmbient, AI_MATKEY_TEXTURE_AMBIENT(0));
|
|
|
|
|
|
|
|
if ( 0 != pCurrentMaterial->textureSpecular.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureSpecular, AI_MATKEY_TEXTURE_SPECULAR(0));
|
|
|
|
|
|
|
|
if ( 0 != pCurrentMaterial->textureBump.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureBump, AI_MATKEY_TEXTURE_HEIGHT(0));
|
|
|
|
|
2013-01-07 21:05:53 +00:00
|
|
|
if ( 0 != pCurrentMaterial->textureDisp.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureDisp, AI_MATKEY_TEXTURE_DISPLACEMENT(0) );
|
|
|
|
|
2009-02-05 18:13:34 +00:00
|
|
|
if ( 0 != pCurrentMaterial->textureOpacity.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureOpacity, AI_MATKEY_TEXTURE_OPACITY(0));
|
|
|
|
|
2009-01-18 23:48:25 +00:00
|
|
|
if ( 0 != pCurrentMaterial->textureSpecularity.length )
|
|
|
|
mat->AddProperty( &pCurrentMaterial->textureSpecularity, AI_MATKEY_TEXTURE_SHININESS(0));
|
2008-06-03 21:32:56 +00:00
|
|
|
|
|
|
|
// Store material property info in material array in scene
|
|
|
|
pScene->mMaterials[ pScene->mNumMaterials ] = mat;
|
|
|
|
pScene->mNumMaterials++;
|
|
|
|
}
|
2008-07-02 18:12:54 +00:00
|
|
|
|
|
|
|
// Test number of created materials.
|
|
|
|
ai_assert( pScene->mNumMaterials == numMaterials );
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Appends this node to the parent node
|
|
|
|
void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
|
|
|
|
{
|
|
|
|
// Checking preconditions
|
2010-05-30 12:45:38 +00:00
|
|
|
ai_assert( NULL != pParent );
|
|
|
|
ai_assert( NULL != pChild );
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// Assign parent to child
|
|
|
|
pChild->mParent = pParent;
|
|
|
|
size_t sNumChildren = 0;
|
2011-06-09 14:29:32 +00:00
|
|
|
(void)sNumChildren; // remove warning on release build
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
// If already children was assigned to the parent node, store them in a
|
|
|
|
std::vector<aiNode*> temp;
|
|
|
|
if (pParent->mChildren != NULL)
|
|
|
|
{
|
|
|
|
sNumChildren = pParent->mNumChildren;
|
2010-05-30 12:45:38 +00:00
|
|
|
ai_assert( 0 != sNumChildren );
|
2008-05-05 12:36:31 +00:00
|
|
|
for (size_t index = 0; index < pParent->mNumChildren; index++)
|
|
|
|
{
|
|
|
|
temp.push_back(pParent->mChildren [ index ] );
|
|
|
|
}
|
|
|
|
delete [] pParent->mChildren;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy node instances into parent node
|
|
|
|
pParent->mNumChildren++;
|
|
|
|
pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
|
|
|
|
for (size_t index = 0; index < pParent->mNumChildren-1; index++)
|
|
|
|
{
|
|
|
|
pParent->mChildren[ index ] = temp [ index ];
|
|
|
|
}
|
|
|
|
pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
} // Namespace Assimp
|
2009-01-18 23:48:25 +00:00
|
|
|
|
|
|
|
#endif // !! ASSIMP_BUILD_NO_OBJ_IMPORTER
|