vrshoot

view libs/assimp/OgreImporter.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file OgreImporter.cpp
42 * @brief Implementation of the Ogre XML (.mesh.xml) loader.
43 */
44 #include "AssimpPCH.h"
45 #ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
47 #include <vector>
48 #include <sstream>
49 using namespace std;
51 #include "OgreImporter.hpp"
52 #include "TinyFormatter.h"
53 #include "irrXMLWrapper.h"
55 static const aiImporterDesc desc = {
56 "Ogre XML Mesh Importer",
57 "",
58 "",
59 "",
60 aiImporterFlags_SupportTextFlavour,
61 0,
62 0,
63 0,
64 0,
65 "mesh.xml"
66 };
68 namespace Assimp
69 {
70 namespace Ogre
71 {
74 bool OgreImporter::CanRead(const std::string &pFile, Assimp::IOSystem *pIOHandler, bool checkSig) const
75 {
76 if(!checkSig)//Check File Extension
77 {
78 std::string extension("mesh.xml");
79 int l=extension.length();
80 return pFile.substr(pFile.length()-l, l)==extension;
81 }
82 else//Check file Header
83 {
84 const char* tokens[] = {"<mesh>"};
85 return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
86 }
87 }
90 void OgreImporter::InternReadFile(const std::string &pFile, aiScene *pScene, Assimp::IOSystem *pIOHandler)
91 {
92 m_CurrentFilename=pFile;
93 m_CurrentIOHandler=pIOHandler;
94 m_CurrentScene=pScene;
96 //Open the File:
97 boost::scoped_ptr<IOStream> file(pIOHandler->Open(pFile));
98 if( file.get() == NULL)
99 throw DeadlyImportError("Failed to open file "+pFile+".");
101 //Read the Mesh File:
102 boost::scoped_ptr<CIrrXML_IOStreamReader> mIOWrapper( new CIrrXML_IOStreamReader( file.get()));
103 boost::scoped_ptr<XmlReader> MeshFile(irr::io::createIrrXMLReader(mIOWrapper.get()));
104 if(!MeshFile)//parse the xml file
105 throw DeadlyImportError("Failed to create XML Reader for "+pFile);
108 DefaultLogger::get()->debug("Mesh File opened");
110 //Read root Node:
111 if(!(XmlRead(MeshFile.get()) && string(MeshFile->getNodeName())=="mesh"))
112 {
113 throw DeadlyImportError("Root Node is not <mesh>! "+pFile+" "+MeshFile->getNodeName());
114 }
116 //eventually load shared geometry
117 XmlRead(MeshFile.get());//shared geometry is optional, so we need a reed for the next two if's
118 if(MeshFile->getNodeName()==string("sharedgeometry"))
119 {
120 unsigned int NumVertices=GetAttribute<int>(MeshFile.get(), "vertexcount");;
122 XmlRead(MeshFile.get());
123 while(MeshFile->getNodeName()==string("vertexbuffer"))
124 {
125 ReadVertexBuffer(m_SharedGeometry, MeshFile.get(), NumVertices);
126 }
127 }
129 //Go to the submeshs:
130 if(MeshFile->getNodeName()!=string("submeshes"))
131 {
132 throw DeadlyImportError("No <submeshes> node in <mesh> node! "+pFile);
133 }
136 //-------------------Read the submeshs and materials:-----------------------
137 std::list<boost::shared_ptr<SubMesh> > SubMeshes;
138 vector<aiMaterial*> Materials;
139 XmlRead(MeshFile.get());
140 while(MeshFile->getNodeName()==string("submesh"))
141 {
142 SubMesh* theSubMesh=new SubMesh();
143 theSubMesh->MaterialName=GetAttribute<string>(MeshFile.get(), "material");
144 DefaultLogger::get()->debug("Loading Submehs with Material: "+theSubMesh->MaterialName);
145 ReadSubMesh(*theSubMesh, MeshFile.get());
147 //just a index in a array, we add a mesh in each loop cycle, so we get indicies like 0, 1, 2 ... n;
148 //so it is important to do this before pushing the mesh in the vector!
149 theSubMesh->MaterialIndex=SubMeshes.size();
151 SubMeshes.push_back(boost::shared_ptr<SubMesh>(theSubMesh));
153 //Load the Material:
154 aiMaterial* MeshMat=LoadMaterial(theSubMesh->MaterialName);
156 //Set the Material:
157 Materials.push_back(MeshMat);
158 }
160 if(SubMeshes.empty())
161 throw DeadlyImportError("no submesh loaded!");
162 if(SubMeshes.size()!=Materials.size())
163 throw DeadlyImportError("materialcount doesn't match mesh count!");
165 //____________________________________________________________
168 //skip submeshnames (stupid irrxml)
169 if(MeshFile->getNodeName()==string("submeshnames"))
170 {
171 XmlRead(MeshFile.get());
172 while(MeshFile->getNodeName()==string("submesh"))
173 XmlRead(MeshFile.get());
174 }
177 //----------------Load the skeleton: -------------------------------
178 vector<Bone> Bones;
179 vector<Animation> Animations;
180 if(MeshFile->getNodeName()==string("skeletonlink"))
181 {
182 string SkeletonFile=GetAttribute<string>(MeshFile.get(), "name");
183 LoadSkeleton(SkeletonFile, Bones, Animations);
184 XmlRead(MeshFile.get());
185 }
186 else
187 {
188 DefaultLogger::get()->debug("No skeleton file will be loaded");
189 DefaultLogger::get()->debug(MeshFile->getNodeName());
190 }
191 //__________________________________________________________________
194 //now there might be boneassignments for the shared geometry:
195 if(MeshFile->getNodeName()==string("boneassignments"))
196 {
197 ReadBoneWeights(m_SharedGeometry, MeshFile.get());
198 }
201 //----------------- Process Meshs -----------------------
202 BOOST_FOREACH(boost::shared_ptr<SubMesh> theSubMesh, SubMeshes)
203 {
204 ProcessSubMesh(*theSubMesh, m_SharedGeometry);
205 }
206 //_______________________________________________________
211 //----------------- Now fill the Assimp scene ---------------------------
213 //put the aiMaterials in the scene:
214 m_CurrentScene->mMaterials=new aiMaterial*[Materials.size()];
215 m_CurrentScene->mNumMaterials=Materials.size();
216 for(unsigned int i=0; i<Materials.size(); ++i)
217 m_CurrentScene->mMaterials[i]=Materials[i];
219 //create the aiMehs...
220 vector<aiMesh*> aiMeshes;
221 BOOST_FOREACH(boost::shared_ptr<SubMesh> theSubMesh, SubMeshes)
222 {
223 aiMeshes.push_back(CreateAssimpSubMesh(*theSubMesh, Bones));
224 }
225 //... and put them in the scene:
226 m_CurrentScene->mNumMeshes=aiMeshes.size();
227 m_CurrentScene->mMeshes=new aiMesh*[aiMeshes.size()];
228 memcpy(m_CurrentScene->mMeshes, &(aiMeshes[0]), sizeof(aiMeshes[0])*aiMeshes.size());
230 //Create the root node
231 m_CurrentScene->mRootNode=new aiNode("root");
233 //link the meshs with the root node:
234 m_CurrentScene->mRootNode->mMeshes=new unsigned int[SubMeshes.size()];
235 m_CurrentScene->mRootNode->mNumMeshes=SubMeshes.size();
236 for(unsigned int i=0; i<SubMeshes.size(); ++i)
237 m_CurrentScene->mRootNode->mMeshes[i]=i;
241 CreateAssimpSkeleton(Bones, Animations);
242 PutAnimationsInScene(Bones, Animations);
243 //___________________________________________________________
244 }
247 const aiImporterDesc* OgreImporter::GetInfo () const
248 {
249 return &desc;
250 }
253 void OgreImporter::SetupProperties(const Importer* pImp)
254 {
255 m_MaterialLibFilename=pImp->GetPropertyString(AI_CONFIG_IMPORT_OGRE_MATERIAL_FILE, "Scene.material");
256 m_TextureTypeFromFilename=pImp->GetPropertyBool(AI_CONFIG_IMPORT_OGRE_TEXTURETYPE_FROM_FILENAME, false);
257 }
260 }//namespace Ogre
261 }//namespace Assimp
263 #endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER