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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "AssimpPCH.h"
|
2014-05-01 15:51:26 +00:00
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-05-01 14:19:03 +00:00
|
|
|
#include "OgreImporter.h"
|
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"
|
|
|
|
};
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
namespace Ogre
|
|
|
|
{
|
|
|
|
|
2014-05-01 21:19:57 +00:00
|
|
|
const aiImporterDesc* OgreImporter::GetInfo() const
|
|
|
|
{
|
|
|
|
return &desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OgreImporter::SetupProperties(const Importer* pImp)
|
|
|
|
{
|
|
|
|
m_userDefinedMaterialLibFile = pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
|
|
|
|
m_detectTextureTypeFromFilename = pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
|
|
|
|
}
|
|
|
|
|
2009-09-05 12:04:32 +00:00
|
|
|
bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
|
|
|
|
{
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!checkSig) {
|
2014-05-01 15:51:26 +00:00
|
|
|
return EndsWith(pFile, ".mesh.xml", false);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 15:51:26 +00:00
|
|
|
|
|
|
|
const char* tokens[] = { "<mesh>" };
|
|
|
|
return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
|
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
// -------------------- Initial file and XML operations --------------------
|
|
|
|
|
|
|
|
// Open
|
2009-09-05 12:04:32 +00:00
|
|
|
boost::scoped_ptr<IOStream> file(pIOHandler->Open(pFile));
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!file.get()) {
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Failed to open file " + pFile);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// Read
|
|
|
|
boost::scoped_ptr<CIrrXML_IOStreamReader> xmlStream(new CIrrXML_IOStreamReader(file.get()));
|
|
|
|
boost::scoped_ptr<XmlReader> reader(irr::io::createIrrXMLReader(xmlStream.get()));
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!reader) {
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Failed to create XML Reader for " + pFile);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
DefaultLogger::get()->debug("Opened a XML reader for " + pFile);
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// Read root node
|
|
|
|
NextNode(reader.get());
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!CurrentNodeNameEquals(reader, "mesh")) {
|
2014-05-01 15:51:26 +00:00
|
|
|
throw DeadlyImportError("Root node is not <mesh> but <" + string(reader->getNodeName()) + "> in " + pFile);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// Node names
|
2014-04-30 02:52:59 +00:00
|
|
|
const string nnSharedGeometry = "sharedgeometry";
|
|
|
|
const string nnVertexBuffer = "vertexbuffer";
|
|
|
|
const string nnSubMeshes = "submeshes";
|
|
|
|
const string nnSubMesh = "submesh";
|
|
|
|
const string nnSubMeshNames = "submeshnames";
|
|
|
|
const string nnSkeletonLink = "skeletonlink";
|
2014-04-30 02:43:13 +00:00
|
|
|
|
|
|
|
// -------------------- Shared Geometry --------------------
|
|
|
|
// This can be used to share geometry between submeshes
|
|
|
|
|
|
|
|
NextNode(reader.get());
|
|
|
|
if (CurrentNodeNameEquals(reader, nnSharedGeometry))
|
2012-03-07 22:42:40 +00:00
|
|
|
{
|
2014-04-30 02:52:59 +00:00
|
|
|
DefaultLogger::get()->debug("Reading shared geometry");
|
2014-04-30 02:43:13 +00:00
|
|
|
unsigned int NumVertices = GetAttribute<unsigned int>(reader.get(), "vertexcount");
|
2012-03-07 22:42:40 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
NextNode(reader.get());
|
2014-05-02 21:20:17 +00:00
|
|
|
while(CurrentNodeNameEquals(reader, nnVertexBuffer)) {
|
2014-04-30 02:43:13 +00:00
|
|
|
ReadVertexBuffer(m_SharedGeometry, reader.get(), NumVertices);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-03-07 22:42:40 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// -------------------- Sub Meshes --------------------
|
|
|
|
|
2014-05-02 21:20:17 +00:00
|
|
|
if (!CurrentNodeNameEquals(reader, nnSubMeshes)) {
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Could not find <submeshes> node inside root <mesh> node");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-05-01 21:19:57 +00:00
|
|
|
vector<boost::shared_ptr<SubMesh> > subMeshes;
|
2014-04-30 02:43:13 +00:00
|
|
|
vector<aiMaterial*> materials;
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
NextNode(reader.get());
|
|
|
|
while(CurrentNodeNameEquals(reader, nnSubMesh))
|
2009-09-05 12:04:32 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
SubMesh* submesh = new SubMesh();
|
|
|
|
ReadSubMesh(subMeshes.size(), *submesh, reader.get());
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// 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!
|
2014-04-30 02:52:59 +00:00
|
|
|
/// @todo Not sure if this really is needed, refactor out if possible.
|
2014-04-30 02:43:13 +00:00
|
|
|
submesh->MaterialIndex = subMeshes.size();
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
subMeshes.push_back(boost::shared_ptr<SubMesh>(submesh));
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-04-30 02:52:59 +00:00
|
|
|
/** @todo What is the correct way of handling empty ref here.
|
|
|
|
Does Assimp require there to be a valid material index for each mesh,
|
|
|
|
even if its a dummy material. */
|
2014-05-01 13:33:15 +00:00
|
|
|
aiMaterial* material = ReadMaterial(pFile, pIOHandler, submesh->MaterialName);
|
2014-04-30 02:52:59 +00:00
|
|
|
materials.push_back(material);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 21:20:17 +00:00
|
|
|
if (subMeshes.empty()) {
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Could not find <submeshes> node inside root <mesh> node");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-05 12:04:32 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// This is really a internal error if we failed to create dummy materials.
|
2014-05-02 21:20:17 +00:00
|
|
|
if (subMeshes.size() != materials.size()) {
|
2014-04-30 02:43:13 +00:00
|
|
|
throw DeadlyImportError("Internal Error: Material count does not match the submesh count");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2009-09-13 18:26:54 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// Skip submesh names.
|
|
|
|
/// @todo Should these be read to scene etc. metadata?
|
|
|
|
if (CurrentNodeNameEquals(reader, nnSubMeshNames))
|
2013-03-16 19:59:47 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
NextNode(reader.get());
|
2014-05-02 21:20:17 +00:00
|
|
|
while(CurrentNodeNameEquals(reader, nnSubMesh)) {
|
2014-04-30 02:43:13 +00:00
|
|
|
NextNode(reader.get());
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2013-03-16 19:59:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// -------------------- Skeleton --------------------
|
2013-03-16 19:59:47 +00:00
|
|
|
|
2009-09-28 16:53:33 +00:00
|
|
|
vector<Bone> Bones;
|
|
|
|
vector<Animation> Animations;
|
2014-05-01 13:33:15 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
if (CurrentNodeNameEquals(reader, nnSkeletonLink))
|
2009-09-13 18:26:54 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
string skeletonFile = GetAttribute<string>(reader.get(), "name");
|
|
|
|
if (!skeletonFile.empty())
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
ReadSkeleton(pFile, pIOHandler, pScene, skeletonFile, Bones, Animations);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-04-30 02:43:13 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
DefaultLogger::get()->debug("Found a unusual <" + nnSkeletonLink + "> with a empty file reference");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-04-30 02:43:13 +00:00
|
|
|
NextNode(reader.get());
|
2009-09-13 18:26:54 +00:00
|
|
|
}
|
2009-09-17 19:05:46 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-04-30 02:43:13 +00:00
|
|
|
DefaultLogger::get()->debug("Mesh has no assigned skeleton with <" + nnSkeletonLink + ">");
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-03-13 16:29:29 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// Now there might be <boneassignments> for the shared geometry
|
2014-05-02 21:20:17 +00:00
|
|
|
if (CurrentNodeNameEquals(reader, "boneassignments")) {
|
2014-04-30 02:43:13 +00:00
|
|
|
ReadBoneWeights(m_SharedGeometry, reader.get());
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-03-13 16:29:29 +00:00
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// -------------------- Process Results --------------------
|
2014-05-01 21:56:35 +00:00
|
|
|
BOOST_FOREACH(boost::shared_ptr<SubMesh> submesh, subMeshes)
|
2012-03-13 16:29:29 +00:00
|
|
|
{
|
2014-05-01 21:56:35 +00:00
|
|
|
ProcessSubMesh(*submesh.get(), m_SharedGeometry);
|
2012-03-13 16:29:29 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 02:43:13 +00:00
|
|
|
// -------------------- Apply to aiScene --------------------
|
2012-03-13 16:29:29 +00:00
|
|
|
|
2014-05-01 21:19:57 +00:00
|
|
|
// Materials
|
|
|
|
pScene->mMaterials = new aiMaterial*[materials.size()];
|
|
|
|
pScene->mNumMaterials = materials.size();
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-05-02 21:20:17 +00:00
|
|
|
for(size_t i=0, len=materials.size(); i<len; ++i) {
|
2014-05-01 21:19:57 +00:00
|
|
|
pScene->mMaterials[i] = materials[i];
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 21:19:57 +00:00
|
|
|
|
|
|
|
// Meshes
|
|
|
|
pScene->mMeshes = new aiMesh*[subMeshes.size()];
|
|
|
|
pScene->mNumMeshes = subMeshes.size();
|
|
|
|
|
|
|
|
for(size_t i=0, len=subMeshes.size(); i<len; ++i)
|
2010-04-19 17:50:52 +00:00
|
|
|
{
|
2014-05-01 21:19:57 +00:00
|
|
|
boost::shared_ptr<SubMesh> submesh = subMeshes[i];
|
|
|
|
pScene->mMeshes[i] = CreateAssimpSubMesh(pScene, *(submesh.get()), Bones);
|
2010-04-19 17:50:52 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 21:19:57 +00:00
|
|
|
// Create the root node
|
|
|
|
pScene->mRootNode = new aiNode();
|
|
|
|
pScene->mRootNode->mMeshes = new unsigned int[subMeshes.size()];
|
|
|
|
pScene->mRootNode->mNumMeshes = subMeshes.size();
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-05-02 21:20:17 +00:00
|
|
|
for(size_t i=0, len=subMeshes.size(); i<len; ++i) {
|
2014-05-01 21:19:57 +00:00
|
|
|
pScene->mRootNode->mMeshes[i] = static_cast<unsigned int>(i);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2010-04-19 17:50:52 +00:00
|
|
|
|
2014-05-01 21:19:57 +00:00
|
|
|
// Skeleton and animations
|
2014-05-01 13:33:15 +00:00
|
|
|
CreateAssimpSkeleton(pScene, Bones, Animations);
|
2009-09-05 12:04:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 15:51:26 +00:00
|
|
|
} // Ogre
|
|
|
|
} // Assimp
|
2012-02-20 10:27:27 +00:00
|
|
|
|
2014-05-01 15:51:26 +00:00
|
|
|
#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
|