Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

303 строки
9.1 KiB

  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2012, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file NDOLoader.cpp
  35. * Implementation of the NDO importer class.
  36. */
  37. #include "AssimpPCH.h"
  38. #ifndef ASSIMP_BUILD_NO_NDO_IMPORTER
  39. #include "NDOLoader.h"
  40. using namespace Assimp;
  41. #define for_each BOOST_FOREACH
  42. static const aiImporterDesc desc = {
  43. "Nendo Mesh Importer",
  44. "",
  45. "",
  46. "http://www.izware.com/nendo/index.htm",
  47. aiImporterFlags_SupportBinaryFlavour,
  48. 0,
  49. 0,
  50. 0,
  51. 0,
  52. "ndo"
  53. };
  54. // ------------------------------------------------------------------------------------------------
  55. // Constructor to be privately used by Importer
  56. NDOImporter::NDOImporter()
  57. {}
  58. // ------------------------------------------------------------------------------------------------
  59. // Destructor, private as well
  60. NDOImporter::~NDOImporter()
  61. {}
  62. // ------------------------------------------------------------------------------------------------
  63. // Returns whether the class can handle the format of the given file.
  64. bool NDOImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  65. {
  66. // check file extension
  67. const std::string extension = GetExtension(pFile);
  68. if( extension == "ndo")
  69. return true;
  70. if ((checkSig || !extension.length()) && pIOHandler) {
  71. const char* tokens[] = {"nendo"};
  72. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1,5);
  73. }
  74. return false;
  75. }
  76. // ------------------------------------------------------------------------------------------------
  77. // Build a string of all file extensions supported
  78. const aiImporterDesc* NDOImporter::GetInfo () const
  79. {
  80. return &desc;
  81. }
  82. // ------------------------------------------------------------------------------------------------
  83. // Setup configuration properties for the loader
  84. void NDOImporter::SetupProperties(const Importer* /*pImp*/)
  85. {
  86. // nothing to be done for the moment
  87. }
  88. // ------------------------------------------------------------------------------------------------
  89. // Imports the given file into the given scene structure.
  90. void NDOImporter::InternReadFile( const std::string& pFile,
  91. aiScene* pScene, IOSystem* pIOHandler)
  92. {
  93. StreamReaderBE reader(pIOHandler->Open( pFile, "rb"));
  94. // first 9 bytes are nendo file format ("nendo 1.n")
  95. const char* head = (const char*)reader.GetPtr();
  96. reader.IncPtr(9);
  97. if (strncmp("nendo ",head,6)) {
  98. throw DeadlyImportError("Not a Nendo file; magic signature missing");
  99. }
  100. // check if this is a supported version. if not, continue, too -- users,
  101. // please don't complain if it doesn't work then ...
  102. unsigned int file_format = 12;
  103. if (!strncmp("1.0",head+6,3)) {
  104. file_format = 10;
  105. DefaultLogger::get()->info("NDO file format is 1.0");
  106. }
  107. else if (!strncmp("1.1",head+6,3)) {
  108. file_format = 11;
  109. DefaultLogger::get()->info("NDO file format is 1.1");
  110. }
  111. else if (!strncmp("1.2",head+6,3)) {
  112. file_format = 12;
  113. DefaultLogger::get()->info("NDO file format is 1.2");
  114. }
  115. else {
  116. DefaultLogger::get()->warn(std::string("Unrecognized nendo file format version, continuing happily ... :") + (head+6));
  117. }
  118. reader.IncPtr(2); /* skip flags */
  119. if (file_format >= 12) {
  120. reader.IncPtr(2);
  121. }
  122. unsigned int temp = reader.GetU1();
  123. std::vector<Object> objects(temp); /* buffer to store all the loaded objects in */
  124. // read all objects
  125. for (unsigned int o = 0; o < objects.size(); ++o) {
  126. // if (file_format < 12) {
  127. if (!reader.GetI1()) {
  128. continue; /* skip over empty object */
  129. }
  130. // reader.GetI2();
  131. // }
  132. Object& obj = objects[o];
  133. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  134. head = (const char*)reader.GetPtr();
  135. reader.IncPtr(temp + 76); /* skip unknown stuff */
  136. obj.name = std::string(head, temp);
  137. // read edge table
  138. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  139. obj.edges.reserve(temp);
  140. for (unsigned int e = 0; e < temp; ++e) {
  141. obj.edges.push_back(Edge());
  142. Edge& edge = obj.edges.back();
  143. for (unsigned int i = 0; i< 8; ++i) {
  144. edge.edge[i] = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  145. }
  146. edge.hard = file_format >= 11 ? reader.GetU1() : 0;
  147. for (unsigned int i = 0; i< 8; ++i) {
  148. edge.color[i] = reader.GetU1();
  149. }
  150. }
  151. // read face table
  152. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  153. obj.faces.reserve(temp);
  154. for (unsigned int e = 0; e < temp; ++e) {
  155. obj.faces.push_back(Face());
  156. Face& face = obj.faces.back();
  157. face.elem = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  158. }
  159. // read vertex table
  160. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  161. obj.vertices.reserve(temp);
  162. for (unsigned int e = 0; e < temp; ++e) {
  163. obj.vertices.push_back(Vertex());
  164. Vertex& v = obj.vertices.back();
  165. v.num = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  166. v.val.x = reader.GetF4();
  167. v.val.y = reader.GetF4();
  168. v.val.z = reader.GetF4();
  169. }
  170. // read UVs
  171. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  172. for (unsigned int e = 0; e < temp; ++e) {
  173. file_format >= 12 ? reader.GetU4() : reader.GetU2();
  174. }
  175. temp = file_format >= 12 ? reader.GetU4() : reader.GetU2();
  176. for (unsigned int e = 0; e < temp; ++e) {
  177. file_format >= 12 ? reader.GetU4() : reader.GetU2();
  178. }
  179. if (reader.GetU1()) {
  180. const unsigned int x = reader.GetU2(), y = reader.GetU2();
  181. temp = 0;
  182. while (temp < x*y) {
  183. unsigned int repeat = reader.GetU1();
  184. reader.GetU1();
  185. reader.GetU1();
  186. reader.GetU1();
  187. temp += repeat;
  188. }
  189. }
  190. }
  191. // construct a dummy node graph and add all named objects as child nodes
  192. aiNode* root = pScene->mRootNode = new aiNode("$NDODummyRoot");
  193. aiNode** cc = root->mChildren = new aiNode* [ root->mNumChildren = static_cast<unsigned int>( objects.size()) ] ();
  194. pScene->mMeshes = new aiMesh* [ root->mNumChildren] ();
  195. std::vector<aiVector3D> vertices;
  196. std::vector<unsigned int> indices;
  197. for_each(const Object& obj,objects) {
  198. aiNode* nd = *cc++ = new aiNode(obj.name);
  199. nd->mParent = root;
  200. // translated from a python dict() - a vector might be sufficient as well
  201. typedef std::map<unsigned int, unsigned int> FaceTable;
  202. FaceTable face_table;
  203. unsigned int n = 0;
  204. for_each(const Edge& edge, obj.edges) {
  205. face_table[edge.edge[2]] = n;
  206. face_table[edge.edge[3]] = n;
  207. ++n;
  208. }
  209. aiMesh* mesh = new aiMesh();
  210. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces=face_table.size()];
  211. vertices.clear();
  212. vertices.reserve(4 * face_table.size()); // arbitrarily choosen
  213. for_each(FaceTable::value_type& v, face_table) {
  214. indices.clear();
  215. aiFace& f = *faces++;
  216. const unsigned int key = v.first;
  217. unsigned int cur_edge = v.second;
  218. while (1) {
  219. unsigned int next_edge, next_vert;
  220. if (key == obj.edges[cur_edge].edge[3]) {
  221. next_edge = obj.edges[cur_edge].edge[5];
  222. next_vert = obj.edges[cur_edge].edge[1];
  223. }
  224. else {
  225. next_edge = obj.edges[cur_edge].edge[4];
  226. next_vert = obj.edges[cur_edge].edge[0];
  227. }
  228. indices.push_back( vertices.size() );
  229. vertices.push_back(obj.vertices[ next_vert ].val);
  230. cur_edge = next_edge;
  231. if (cur_edge == v.second) {
  232. break;
  233. }
  234. }
  235. f.mIndices = new unsigned int[f.mNumIndices = indices.size()];
  236. std::copy(indices.begin(),indices.end(),f.mIndices);
  237. }
  238. mesh->mVertices = new aiVector3D[mesh->mNumVertices = vertices.size()];
  239. std::copy(vertices.begin(),vertices.end(),mesh->mVertices);
  240. if (mesh->mNumVertices) {
  241. pScene->mMeshes[pScene->mNumMeshes] = mesh;
  242. (nd->mMeshes = new unsigned int[nd->mNumMeshes=1])[0]=pScene->mNumMeshes++;
  243. }
  244. }
  245. }
  246. #endif