ACLoader.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (ASSIMP)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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 Implementation of the AC3D importer class */
  35. #include "AssimpPCH.h"
  36. #ifndef AI_BUILD_NO_AC_IMPORTER
  37. // internal headers
  38. #include "ACLoader.h"
  39. #include "ParsingUtils.h"
  40. #include "fast_atof.h"
  41. using namespace Assimp;
  42. // ------------------------------------------------------------------------------------------------
  43. // skip to the next token
  44. #define AI_AC_SKIP_TO_NEXT_TOKEN() \
  45. if (!SkipSpaces(&buffer)) \
  46. { \
  47. DefaultLogger::get()->error("AC3D: Unexpected EOF/EOL"); \
  48. continue; \
  49. }
  50. // ------------------------------------------------------------------------------------------------
  51. // read a string (may be enclosed in double quotation marks). buffer must point to "
  52. #define AI_AC_GET_STRING(out) \
  53. ++buffer; \
  54. const char* sz = buffer; \
  55. while ('\"' != *buffer) \
  56. { \
  57. if (IsLineEnd( *buffer )) \
  58. { \
  59. DefaultLogger::get()->error("AC3D: Unexpected EOF/EOL in string"); \
  60. out = "ERROR"; \
  61. break; \
  62. } \
  63. ++buffer; \
  64. } \
  65. if (IsLineEnd( *buffer ))continue; \
  66. out = std::string(sz,(unsigned int)(buffer-sz)); \
  67. ++buffer;
  68. // ------------------------------------------------------------------------------------------------
  69. // read 1 to n floats prefixed with an optional predefined identifier
  70. #define AI_AC_CHECKED_LOAD_FLOAT_ARRAY(name,name_length,num,out) \
  71. AI_AC_SKIP_TO_NEXT_TOKEN(); \
  72. if (name_length) \
  73. { \
  74. if (strncmp(buffer,name,name_length) || !IsSpace(buffer[name_length])) \
  75. { \
  76. DefaultLogger::get()->error("AC3D: Unexpexted token. " name " was expected."); \
  77. continue; \
  78. } \
  79. buffer += name_length+1; \
  80. } \
  81. for (unsigned int i = 0; i < num;++i) \
  82. { \
  83. AI_AC_SKIP_TO_NEXT_TOKEN(); \
  84. buffer = fast_atof_move(buffer,((float*)out)[i]); \
  85. }
  86. // ------------------------------------------------------------------------------------------------
  87. // Constructor to be privately used by Importer
  88. AC3DImporter::AC3DImporter()
  89. {
  90. }
  91. // ------------------------------------------------------------------------------------------------
  92. // Destructor, private as well
  93. AC3DImporter::~AC3DImporter()
  94. {
  95. }
  96. // ------------------------------------------------------------------------------------------------
  97. // Returns whether the class can handle the format of the given file.
  98. bool AC3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  99. {
  100. // simple check of file extension is enough for the moment
  101. std::string::size_type pos = pFile.find_last_of('.');
  102. // no file extension - can't read
  103. if( pos == std::string::npos)return false;
  104. std::string extension = pFile.substr( pos);
  105. for( std::string::iterator it = extension.begin(); it != extension.end(); ++it)
  106. *it = tolower( *it);
  107. if( extension == ".ac" || extension == "ac")
  108. return true;
  109. return false;
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // Get a pointer to the next line from the file
  113. bool AC3DImporter::GetNextLine( )
  114. {
  115. SkipLine(&buffer);
  116. return SkipSpaces(&buffer);
  117. }
  118. // ------------------------------------------------------------------------------------------------
  119. // Parse an object section in an AC file
  120. void AC3DImporter::LoadObjectSection(std::vector<Object>& objects)
  121. {
  122. if (!TokenMatch(buffer,"OBJECT",6))
  123. return;
  124. ++mNumMeshes;
  125. objects.push_back(Object());
  126. Object& obj = objects.back();
  127. while (GetNextLine())
  128. {
  129. if (TokenMatch(buffer,"kids",4))
  130. {
  131. SkipSpaces(&buffer);
  132. unsigned int num = strtol10(buffer,&buffer);
  133. GetNextLine();
  134. if (num)
  135. {
  136. // load the children of this object recursively
  137. obj.children.reserve(num);
  138. for (unsigned int i = 0; i < num; ++i)
  139. LoadObjectSection(obj.children);
  140. }
  141. return;
  142. }
  143. else if (TokenMatch(buffer,"name",4))
  144. {
  145. SkipSpaces(&buffer);
  146. AI_AC_GET_STRING(obj.name);
  147. }
  148. else if (TokenMatch(buffer,"texture",7))
  149. {
  150. SkipSpaces(&buffer);
  151. AI_AC_GET_STRING(obj.texture);
  152. }
  153. else if (TokenMatch(buffer,"texrep",6))
  154. {
  155. SkipSpaces(&buffer);
  156. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&obj.texRepeat);
  157. }
  158. else if (TokenMatch(buffer,"rot",3))
  159. {
  160. SkipSpaces(&buffer);
  161. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,9,&obj.rotation);
  162. }
  163. else if (TokenMatch(buffer,"loc",3))
  164. {
  165. SkipSpaces(&buffer);
  166. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&obj.translation);
  167. }
  168. else if (TokenMatch(buffer,"numvert",7))
  169. {
  170. SkipSpaces(&buffer);
  171. unsigned int t = strtol10(buffer,&buffer);
  172. obj.vertices.reserve(t);
  173. for (unsigned int i = 0; i < t;++i)
  174. {
  175. if (!GetNextLine())
  176. {
  177. DefaultLogger::get()->error("AC3D: Unexpected EOF: not all vertices have been parsed yet");
  178. break;
  179. }
  180. else if (!IsNumeric(*buffer))
  181. {
  182. DefaultLogger::get()->error("AC3D: Unexpected token: not all vertices have been parsed yet");
  183. --buffer; // make sure the line is processed a second time
  184. break;
  185. }
  186. obj.vertices.push_back(aiVector3D());
  187. aiVector3D& v = obj.vertices.back();
  188. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&v.x);
  189. //std::swap(v.z,v.y);
  190. v.z *= -1.f;
  191. }
  192. }
  193. else if (TokenMatch(buffer,"numsurf",7))
  194. {
  195. SkipSpaces(&buffer);
  196. bool Q3DWorkAround = false;
  197. const unsigned int t = strtol10(buffer,&buffer);
  198. obj.surfaces.reserve(t);
  199. for (unsigned int i = 0; i < t;++i)
  200. {
  201. GetNextLine();
  202. if (!TokenMatch(buffer,"SURF",4))
  203. {
  204. // FIX: this can occur for some files - Quick 3D for
  205. // example writes no surf chunks
  206. if (!Q3DWorkAround)
  207. {
  208. DefaultLogger::get()->warn("AC3D: SURF token was expected");
  209. DefaultLogger::get()->debug("Continuing with Quick3D Workaround enabled");
  210. }
  211. --buffer; // make sure the line is processed a second time
  212. // break; --- see fix notes above
  213. Q3DWorkAround = true;
  214. }
  215. SkipSpaces(&buffer);
  216. obj.surfaces.push_back(Surface());
  217. Surface& surf = obj.surfaces.back();
  218. surf.flags = strtol_cppstyle(buffer);
  219. while (1)
  220. {
  221. if(!GetNextLine())
  222. {
  223. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface is incomplete");
  224. break;
  225. }
  226. if (TokenMatch(buffer,"mat",3))
  227. {
  228. SkipSpaces(&buffer);
  229. surf.mat = strtol10(buffer);
  230. }
  231. else if (TokenMatch(buffer,"refs",4))
  232. {
  233. // --- see fix notes above
  234. if (Q3DWorkAround)
  235. {
  236. if (!surf.entries.empty())
  237. {
  238. buffer -= 6;
  239. break;
  240. }
  241. }
  242. SkipSpaces(&buffer);
  243. const unsigned int m = strtol10(buffer);
  244. surf.entries.reserve(m);
  245. obj.numRefs += m;
  246. for (unsigned int k = 0; k < m; ++k)
  247. {
  248. if(!GetNextLine())
  249. {
  250. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface references are incomplete");
  251. break;
  252. }
  253. surf.entries.push_back(Surface::SurfaceEntry());
  254. Surface::SurfaceEntry& entry = surf.entries.back();
  255. entry.first = strtol10(buffer,&buffer);
  256. SkipSpaces(&buffer);
  257. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&entry.second);
  258. }
  259. }
  260. else
  261. {
  262. --buffer; // make sure the line is processed a second time
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. }
  269. DefaultLogger::get()->error("AC3D: Unexpected EOF: \'kids\' line was expected");
  270. }
  271. // ------------------------------------------------------------------------------------------------
  272. // Convert a material from AC3DImporter::Material to aiMaterial
  273. void AC3DImporter::ConvertMaterial(const Object& object,
  274. const Material& matSrc,
  275. MaterialHelper& matDest)
  276. {
  277. aiString s;
  278. if (matSrc.name.length())
  279. {
  280. s.Set(matSrc.name);
  281. matDest.AddProperty(&s,AI_MATKEY_NAME);
  282. }
  283. if (object.texture.length())
  284. {
  285. s.Set(object.texture);
  286. matDest.AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  287. }
  288. matDest.AddProperty<aiColor3D>(&matSrc.rgb,1, AI_MATKEY_COLOR_DIFFUSE);
  289. matDest.AddProperty<aiColor3D>(&matSrc.amb,1, AI_MATKEY_COLOR_AMBIENT);
  290. matDest.AddProperty<aiColor3D>(&matSrc.emis,1,AI_MATKEY_COLOR_EMISSIVE);
  291. matDest.AddProperty<aiColor3D>(&matSrc.spec,1,AI_MATKEY_COLOR_SPECULAR);
  292. int n;
  293. if (matSrc.shin)
  294. {
  295. n = aiShadingMode_Phong;
  296. matDest.AddProperty<float>(&matSrc.shin,1,AI_MATKEY_SHININESS);
  297. }
  298. else n = aiShadingMode_Gouraud;
  299. matDest.AddProperty<int>(&n,1,AI_MATKEY_SHADING_MODEL);
  300. float f = 1.f - matSrc.trans;
  301. matDest.AddProperty<float>(&f,1,AI_MATKEY_OPACITY);
  302. }
  303. // ------------------------------------------------------------------------------------------------
  304. // Converts the loaded data to the internal verbose representation
  305. aiNode* AC3DImporter::ConvertObjectSection(Object& object,
  306. std::vector<aiMesh*>& meshes,
  307. std::vector<MaterialHelper*>& outMaterials,
  308. const std::vector<Material>& materials)
  309. {
  310. aiNode* node = new aiNode();
  311. if (object.vertices.size())
  312. {
  313. if (!object.surfaces.size() || !object.numRefs)
  314. {
  315. /* " An object with 7 vertices (no surfaces, no materials defined).
  316. This is a good way of getting point data into AC3D.
  317. The Vertex->create convex-surface/object can be used on these
  318. vertices to 'wrap' a 3d shape around them "
  319. (http://www.opencity.info/html/ac3dfileformat.html)
  320. therefore: if no surfaces are defined return point data only
  321. */
  322. DefaultLogger::get()->info("AC3D: No surfaces defined in object definition, "
  323. "a point list is returned");
  324. meshes.push_back(new aiMesh());
  325. aiMesh* mesh = meshes.back();
  326. mesh->mNumFaces = mesh->mNumVertices = (unsigned int)object.vertices.size();
  327. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  328. aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  329. for (unsigned int i = 0; i < mesh->mNumVertices;++i,++faces,++verts)
  330. {
  331. *verts = object.vertices[i];
  332. faces->mNumIndices = 1;
  333. faces->mIndices = new unsigned int[1];
  334. faces->mIndices[0] = i;
  335. }
  336. // use the primary material in this case. this should be the
  337. // default material if all objects of the file contain points
  338. // and no faces.
  339. mesh->mMaterialIndex = 0;
  340. outMaterials.push_back(new MaterialHelper());
  341. ConvertMaterial(object, materials[0], *outMaterials.back());
  342. }
  343. else
  344. {
  345. // need to generate one or more meshes for this object.
  346. // find out how many different materials we have
  347. typedef std::pair< unsigned int, unsigned int > IntPair;
  348. typedef std::vector< IntPair > MatTable;
  349. MatTable needMat(materials.size(),IntPair(0,0));
  350. std::vector<Surface>::iterator it,end = object.surfaces.end();
  351. std::vector<Surface::SurfaceEntry>::iterator it2,end2;
  352. for (it = object.surfaces.begin(); it != end; ++it)
  353. {
  354. register unsigned int idx = (*it).mat;
  355. if (idx >= needMat.size())
  356. {
  357. DefaultLogger::get()->error("AC3D: material index os out of range");
  358. idx = 0;
  359. }
  360. if ((*it).entries.empty())
  361. {
  362. DefaultLogger::get()->warn("AC3D: surface her zero vertex references");
  363. }
  364. // validate all vertex indices to make sure we won't crash here
  365. for (it2 = (*it).entries.begin(),
  366. end2 = (*it).entries.end(); it2 != end2; ++it2)
  367. {
  368. if ((*it2).first >= object.vertices.size())
  369. {
  370. DefaultLogger::get()->warn("AC3D: Invalid vertex reference");
  371. (*it2).first = 0;
  372. }
  373. }
  374. if (!needMat[idx].first)++node->mNumMeshes;
  375. switch ((*it).flags & 0xf)
  376. {
  377. // closed line
  378. case 0x1:
  379. needMat[idx].first += (unsigned int)(*it).entries.size();
  380. needMat[idx].second += (unsigned int)(*it).entries.size()<<1u;
  381. break;
  382. // unclosed line
  383. case 0x2:
  384. needMat[idx].first += (unsigned int)(*it).entries.size()-1;
  385. needMat[idx].second += ((unsigned int)(*it).entries.size()-1)<<1u;
  386. break;
  387. // 0 == polygon, else unknown
  388. default:
  389. if ((*it).flags & 0xf)
  390. {
  391. DefaultLogger::get()->warn("AC3D: The type flag of a surface is unknown");
  392. (*it).flags &= ~(0xf);
  393. }
  394. // the number of faces increments by one, the number
  395. // of vertices by surface.numref.
  396. needMat[idx].first++;
  397. needMat[idx].second += (unsigned int)(*it).entries.size();
  398. };
  399. }
  400. unsigned int* pip = node->mMeshes = new unsigned int[node->mNumMeshes];
  401. unsigned int mat = 0;
  402. for (MatTable::const_iterator cit = needMat.begin(), cend = needMat.end();
  403. cit != cend; ++cit, ++mat)
  404. {
  405. if (!(*cit).first)continue;
  406. // allocate a new aiMesh object
  407. *pip++ = (unsigned int)meshes.size();
  408. aiMesh* mesh = new aiMesh();
  409. meshes.push_back(mesh);
  410. mesh->mMaterialIndex = (unsigned int)outMaterials.size();
  411. outMaterials.push_back(new MaterialHelper());
  412. ConvertMaterial(object, materials[mat], *outMaterials.back());
  413. // allocate storage for vertices and normals
  414. mesh->mNumFaces = (*cit).first;
  415. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  416. mesh->mNumVertices = (*cit).second;
  417. aiVector3D* vertices = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  418. unsigned int cur = 0;
  419. // allocate UV coordinates, but only if the texture name for the
  420. // surface is not empty
  421. aiVector3D* uv = NULL;
  422. if(object.texture.length())
  423. {
  424. uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  425. mesh->mNumUVComponents[0] = 2;
  426. }
  427. for (it = object.surfaces.begin(); it != end; ++it)
  428. {
  429. if (mat == (*it).mat)
  430. {
  431. const Surface& src = *it;
  432. // closed polygon
  433. unsigned int type = (*it).flags & 0xf;
  434. if (!type)
  435. {
  436. aiFace& face = *faces++;
  437. if((face.mNumIndices = (unsigned int)src.entries.size()))
  438. {
  439. face.mIndices = new unsigned int[face.mNumIndices];
  440. for (unsigned int i = 0; i < face.mNumIndices;++i,++vertices)
  441. {
  442. const Surface::SurfaceEntry& entry = src.entries[i];
  443. face.mIndices[i] = cur++;
  444. // copy vertex positions
  445. *vertices = object.vertices[entry.first];
  446. // copy texture coordinates (apply the UV offset)
  447. if (uv)
  448. {
  449. uv->x = entry.second.x * object.texRepeat.x;
  450. uv->y = entry.second.y * object.texRepeat.y;
  451. ++uv;
  452. }
  453. }
  454. }
  455. }
  456. else
  457. {
  458. it2 = (*it).entries.begin();
  459. // either a closed or an unclosed line
  460. register unsigned int tmp = (unsigned int)(*it).entries.size();
  461. if (0x2 == type)--tmp;
  462. for (unsigned int m = 0; m < tmp;++m)
  463. {
  464. aiFace& face = *faces++;
  465. face.mNumIndices = 2;
  466. face.mIndices = new unsigned int[2];
  467. face.mIndices[0] = cur++;
  468. face.mIndices[1] = cur++;
  469. // copy vertex positions
  470. *vertices++ = object.vertices[(*it2).first];
  471. // copy texture coordinates (apply the UV offset)
  472. if (uv)
  473. {
  474. uv->x = (*it2).second.x * object.texRepeat.x;
  475. uv->y = (*it2).second.y * object.texRepeat.y;
  476. ++uv;
  477. }
  478. if (0x1 == type && tmp-1 == m)
  479. {
  480. // if this is a closed line repeat its beginning now
  481. it2 = (*it).entries.begin();
  482. }
  483. else ++it2;
  484. // second point
  485. *vertices++ = object.vertices[(*it2).first];
  486. if (uv)
  487. {
  488. uv->x = (*it2).second.x * object.texRepeat.x;
  489. uv->y = (*it2).second.y * object.texRepeat.y;
  490. ++uv;
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. // add children to the object
  500. if (object.children.size())
  501. {
  502. node->mNumChildren = (unsigned int)object.children.size();
  503. node->mChildren = new aiNode*[node->mNumChildren];
  504. for (unsigned int i = 0; i < node->mNumChildren;++i)
  505. {
  506. node->mChildren[i] = ConvertObjectSection(object.children[i],meshes,outMaterials,materials);
  507. node->mChildren[i]->mParent = node;
  508. }
  509. }
  510. node->mName.Set(object.name);
  511. // setup the local transformation matrix of the object
  512. node->mTransformation = aiMatrix4x4 ( object.rotation );
  513. node->mTransformation.a4 = object.translation.x;
  514. node->mTransformation.b4 = object.translation.y;
  515. node->mTransformation.c4 = object.translation.z;
  516. return node;
  517. }
  518. // ------------------------------------------------------------------------------------------------
  519. void AC3DImporter::SetupProperties(const Importer* pImp)
  520. {
  521. configSplitBFCull = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_SEPARATE_BFCULL,1) ? true : false;
  522. }
  523. // ------------------------------------------------------------------------------------------------
  524. // Imports the given file into the given scene structure.
  525. void AC3DImporter::InternReadFile( const std::string& pFile,
  526. aiScene* pScene, IOSystem* pIOHandler)
  527. {
  528. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  529. // Check whether we can read from the file
  530. if( file.get() == NULL)
  531. throw new ImportErrorException( "Failed to open AC3D file " + pFile + ".");
  532. const unsigned int fileSize = (unsigned int)file->FileSize();
  533. // allocate storage and copy the contents of the file to a memory buffer
  534. std::vector<char> mBuffer2(fileSize+1);
  535. file->Read(&mBuffer2[0], 1, fileSize);
  536. mBuffer2[fileSize] = '\0';
  537. buffer = &mBuffer2[0];
  538. mNumMeshes = 0;
  539. if (::strncmp(buffer,"AC3D",4))
  540. throw new ImportErrorException("AC3D: No valid AC3D file, magic sequence not found");
  541. // print the file format version to the console
  542. unsigned int version = HexDigitToDecimal( buffer[4] );
  543. char msg[3];
  544. itoa10(msg,3,version);
  545. DefaultLogger::get()->info(std::string("AC3D file format version: ") + msg);
  546. std::vector<Material> materials;
  547. materials.reserve(5);
  548. std::vector<Object> rootObjects;
  549. rootObjects.reserve(5);
  550. while (GetNextLine())
  551. {
  552. if (TokenMatch(buffer,"MATERIAL",8))
  553. {
  554. materials.push_back(Material());
  555. Material& mat = materials.back();
  556. // manually parse the material ... sscanf would use the buldin atof ...
  557. // Format: (name) rgb %f %f %f amb %f %f %f emis %f %f %f spec %f %f %f shi %d trans %f
  558. AI_AC_SKIP_TO_NEXT_TOKEN();
  559. if ('\"' == *buffer)
  560. {
  561. AI_AC_GET_STRING(mat.name);
  562. AI_AC_SKIP_TO_NEXT_TOKEN();
  563. }
  564. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("rgb",3,3,&mat.rgb);
  565. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("amb",3,3,&mat.rgb);
  566. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("emis",4,3,&mat.rgb);
  567. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("spec",4,3,&mat.rgb);
  568. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("shi",3,1,&mat.shin);
  569. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("trans",5,1,&mat.trans);
  570. }
  571. LoadObjectSection(rootObjects);
  572. }
  573. if (rootObjects.empty() || !mNumMeshes)
  574. {
  575. throw new ImportErrorException("AC3D: No meshes have been loaded");
  576. }
  577. if (materials.empty())
  578. {
  579. DefaultLogger::get()->warn("AC3D: No material has been found");
  580. materials.push_back(Material());
  581. }
  582. mNumMeshes += (mNumMeshes>>2u) + 1;
  583. std::vector<aiMesh*> meshes;
  584. meshes.reserve(mNumMeshes);
  585. std::vector<MaterialHelper*> omaterials;
  586. materials.reserve(mNumMeshes);
  587. // generate a dummy root if there are multiple objects on the top layer
  588. Object* root;
  589. if (1 == rootObjects.size())
  590. root = &rootObjects[0];
  591. else
  592. {
  593. root = new Object();
  594. }
  595. // now convert the imported stuff to our output data structure
  596. pScene->mRootNode = ConvertObjectSection(*root,meshes,omaterials,materials);
  597. if (1 != rootObjects.size())delete root;
  598. // build output arrays
  599. if (meshes.empty())
  600. {
  601. throw new ImportErrorException("An unknown error occured during converting");
  602. }
  603. pScene->mNumMeshes = (unsigned int)meshes.size();
  604. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  605. ::memcpy(pScene->mMeshes,&meshes[0],pScene->mNumMeshes*sizeof(void*));
  606. // build output arrays
  607. pScene->mNumMaterials = (unsigned int)omaterials.size();
  608. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  609. ::memcpy(pScene->mMaterials,&omaterials[0],pScene->mNumMaterials*sizeof(void*));
  610. }
  611. #endif //!defined AI_BUILD_NO_AC_IMPORTER