2010-04-30 07:15:08 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2010-04-30 07:15:08 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2010-04-30 07:15:08 +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.
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2010-04-30 07:15:08 +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.
|
2010-04-30 07:15:08 +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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file OgreImporter.cpp
|
|
|
|
* @brief Implementation of the Ogre XML (.mesh.xml) loader.
|
|
|
|
*/
|
|
|
|
#include "AssimpPCH.h"
|
2009-09-05 12:04:32 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
using namespace std;
|
|
|
|
|
2012-02-20 10:27:27 +00:00
|
|
|
#include "OgreImporter.hpp"
|
2012-03-07 22:42:40 +00:00
|
|
|
#include "TinyFormatter.h"
|
2009-09-05 12:04:32 +00:00
|
|
|
#include "irrXMLWrapper.h"
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
static const aiImporterDesc desc = {
|
|
|
|
"Ogre XML Mesh Importer",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
aiImporterFlags_SupportTextFlavour,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"mesh.xml"
|
|
|
|
};
|
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
namespace Ogre
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
|
|
|
|
{
|
|
|
|
if(!checkSig)//Check File Extension
|
|
|
|
{
|
|
|
|
std::string extension("mesh.xml");
|
|
|
|
int l=extension.length();
|
|
|
|
return pFile.substr(pFile.length()-l, l)==extension;
|
|
|
|
}
|
|
|
|
else//Check file Header
|
|
|
|
{
|
|
|
|
const char* tokens[] = {"<mesh>"};
|
|
|
|
return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
|
|
|
|
{
|
|
|
|
m_CurrentFilename=pFile;
|
|
|
|
m_CurrentIOHandler=pIOHandler;
|
|
|
|
m_CurrentScene=pScene;
|
|
|
|
|
|
|
|
//Open the File:
|
|
|
|
boost::scoped_ptr<IOStream> file(pIOHandler->Open(pFile));
|
|
|
|
if( file.get() == NULL)
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("Failed to open file "+pFile+".");
|
2009-09-05 12:04:32 +00:00
|
|
|
|
|
|
|
//Read the Mesh File:
|
|
|
|
boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
|
|
|
|
XmlReader* MeshFile = irr::io::createIrrXMLReader(mIOWrapper.get());
|
|
|
|
if(!MeshFile)//parse the xml file
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("Failed to create XML Reader for "+pFile);
|
2009-09-05 12:04:32 +00:00
|
|
|
|
|
|
|
|
2009-09-05 15:49:23 +00:00
|
|
|
DefaultLogger::get()->debug("Mesh File opened");
|
2009-09-05 12:04:32 +00:00
|
|
|
|
|
|
|
//Read root Node:
|
|
|
|
if(!(XmlRead(MeshFile) && string(MeshFile->getNodeName())=="mesh"))
|
|
|
|
{
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("Root Node is not <mesh>! "+pFile+" "+MeshFile->getNodeName());
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2012-03-07 22:42:40 +00:00
|
|
|
//eventually load shared geometry
|
|
|
|
XmlRead(MeshFile);//shared geometry is optional, so we need a reed for the next two if's
|
|
|
|
if(MeshFile->getNodeName()==string("sharedgeometry"))
|
|
|
|
{
|
|
|
|
unsigned int NumVertices=GetAttribute<int>(MeshFile, "vertexcount");;
|
|
|
|
|
|
|
|
XmlRead(MeshFile);
|
|
|
|
while(MeshFile->getNodeName()==string("vertexbuffer"))
|
|
|
|
{
|
|
|
|
ReadVertexBuffer(m_SharedGeometry, MeshFile, NumVertices);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
//Go to the submeshs:
|
2012-03-07 22:42:40 +00:00
|
|
|
if(MeshFile->getNodeName()!=string("submeshes"))
|
2009-09-05 12:04:32 +00:00
|
|
|
{
|
2010-03-18 17:00:12 +00:00
|
|
|
throw DeadlyImportError("No <submeshes> node in <mesh> node! "+pFile);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-04-19 17:50:52 +00:00
|
|
|
//-------------------Read the submeshs and materials:-----------------------
|
2010-05-10 21:06:44 +00:00
|
|
|
std::list<boost::shared_ptr<SubMesh> > SubMeshes;
|
2010-04-19 17:50:52 +00:00
|
|
|
vector<aiMaterial*> Materials;
|
2009-09-05 12:04:32 +00:00
|
|
|
XmlRead(MeshFile);
|
2010-04-19 17:50:52 +00:00
|
|
|
while(MeshFile->getNodeName()==string("submesh"))
|
2009-09-05 12:04:32 +00:00
|
|
|
{
|
2010-04-19 17:50:52 +00:00
|
|
|
SubMesh* theSubMesh=new SubMesh();
|
|
|
|
theSubMesh->MaterialName=GetAttribute<string>(MeshFile, "material");
|
|
|
|
DefaultLogger::get()->debug("Loading Submehs with Material: "+theSubMesh->MaterialName);
|
|
|
|
ReadSubMesh(*theSubMesh, MeshFile);
|
|
|
|
|
|
|
|
//just a index in a array, we add a mesh in each loop cycle, so we get indicies like 0, 1, 2 ... n;
|
|
|
|
//so it is important to do this before pushing the mesh in the vector!
|
|
|
|
theSubMesh->MaterialIndex=SubMeshes.size();
|
|
|
|
|
2010-05-10 21:06:44 +00:00
|
|
|
SubMeshes.push_back(boost::shared_ptr<SubMesh>(theSubMesh));
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2009-09-28 16:53:33 +00:00
|
|
|
//Load the Material:
|
2010-04-19 17:50:52 +00:00
|
|
|
aiMaterial* MeshMat=LoadMaterial(theSubMesh->MaterialName);
|
2009-09-28 16:53:33 +00:00
|
|
|
|
|
|
|
//Set the Material:
|
2010-04-19 17:50:52 +00:00
|
|
|
Materials.push_back(MeshMat);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2010-04-19 17:50:52 +00:00
|
|
|
if(SubMeshes.empty())
|
|
|
|
throw DeadlyImportError("no submesh loaded!");
|
|
|
|
if(SubMeshes.size()!=Materials.size())
|
|
|
|
throw DeadlyImportError("materialcount doesn't match mesh count!");
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2009-09-13 18:26:54 +00:00
|
|
|
//____________________________________________________________
|
|
|
|
|
|
|
|
|
|
|
|
//----------------Load the skeleton: -------------------------------
|
2009-09-28 16:53:33 +00:00
|
|
|
vector<Bone> Bones;
|
|
|
|
vector<Animation> Animations;
|
2009-09-13 18:26:54 +00:00
|
|
|
if(MeshFile->getNodeName()==string("skeletonlink"))
|
|
|
|
{
|
|
|
|
string SkeletonFile=GetAttribute<string>(MeshFile, "name");
|
2009-09-28 16:53:33 +00:00
|
|
|
LoadSkeleton(SkeletonFile, Bones, Animations);
|
2012-03-13 16:29:29 +00:00
|
|
|
XmlRead(MeshFile);
|
2009-09-13 18:26:54 +00:00
|
|
|
}
|
2009-09-17 19:05:46 +00:00
|
|
|
else
|
2009-09-28 16:53:33 +00:00
|
|
|
{
|
|
|
|
DefaultLogger::get()->warn("No skeleton file will be loaded");
|
2009-09-17 19:05:46 +00:00
|
|
|
DefaultLogger::get()->warn(MeshFile->getNodeName());
|
2009-09-28 16:53:33 +00:00
|
|
|
}
|
2009-09-13 18:26:54 +00:00
|
|
|
//__________________________________________________________________
|
|
|
|
|
2012-03-13 16:29:29 +00:00
|
|
|
|
|
|
|
//now there might be boneassignments for the shared geometry:
|
|
|
|
if(MeshFile->getNodeName()==string("boneassignments"))
|
|
|
|
{
|
|
|
|
ReadBoneWeights(m_SharedGeometry, MeshFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------- Process Meshs -----------------------
|
|
|
|
BOOST_FOREACH(boost::shared_ptr<SubMesh> theSubMesh, SubMeshes)
|
|
|
|
{
|
|
|
|
ProcessSubMesh(*theSubMesh, m_SharedGeometry);
|
|
|
|
}
|
|
|
|
//_______________________________________________________
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-04-19 17:50:52 +00:00
|
|
|
|
|
|
|
//----------------- Now fill the Assimp scene ---------------------------
|
|
|
|
|
|
|
|
//put the aiMaterials in the scene:
|
|
|
|
m_CurrentScene->mMaterials=new aiMaterial*[Materials.size()];
|
|
|
|
m_CurrentScene->mNumMaterials=Materials.size();
|
|
|
|
for(unsigned int i=0; i<Materials.size(); ++i)
|
|
|
|
m_CurrentScene->mMaterials[i]=Materials[i];
|
|
|
|
|
|
|
|
//create the aiMehs...
|
|
|
|
vector<aiMesh*> aiMeshes;
|
2010-05-10 21:06:44 +00:00
|
|
|
BOOST_FOREACH(boost::shared_ptr<SubMesh> theSubMesh, SubMeshes)
|
2010-04-19 17:50:52 +00:00
|
|
|
{
|
|
|
|
aiMeshes.push_back(CreateAssimpSubMesh(*theSubMesh, Bones));
|
|
|
|
}
|
|
|
|
//... and put them in the scene:
|
|
|
|
m_CurrentScene->mNumMeshes=aiMeshes.size();
|
|
|
|
m_CurrentScene->mMeshes=new aiMesh*[aiMeshes.size()];
|
|
|
|
memcpy(m_CurrentScene->mMeshes, &(aiMeshes[0]), sizeof(aiMeshes[0])*aiMeshes.size());
|
|
|
|
|
|
|
|
//Create the root node
|
|
|
|
m_CurrentScene->mRootNode=new aiNode("root");
|
|
|
|
|
|
|
|
//link the meshs with the root node:
|
|
|
|
m_CurrentScene->mRootNode->mMeshes=new unsigned int[SubMeshes.size()];
|
|
|
|
m_CurrentScene->mRootNode->mNumMeshes=SubMeshes.size();
|
|
|
|
for(unsigned int i=0; i<SubMeshes.size(); ++i)
|
|
|
|
m_CurrentScene->mRootNode->mMeshes[i]=i;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-28 16:53:33 +00:00
|
|
|
CreateAssimpSkeleton(Bones, Animations);
|
2010-11-09 18:13:08 +00:00
|
|
|
PutAnimationsInScene(Bones, Animations);
|
2010-04-19 17:50:52 +00:00
|
|
|
//___________________________________________________________
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-22 22:26:26 +00:00
|
|
|
const aiImporterDesc* OgreImporter::GetInfo () const
|
2009-09-05 12:04:32 +00:00
|
|
|
{
|
2012-04-22 22:26:26 +00:00
|
|
|
return &desc;
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OgreImporter::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
|
|
|
m_MaterialLibFilename=pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
|
2012-06-03 18:01:32 +00:00
|
|
|
m_TextureTypeFromFilename=pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2012-02-20 10:27:27 +00:00
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
}//namespace Ogre
|
|
|
|
}//namespace Assimp
|
|
|
|
|
2010-02-06 23:52:41 +00:00
|
|
|
#endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER
|