ACLoader.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 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. LoadObjectSection(obj.children);
  139. }
  140. return;
  141. }
  142. else if (TokenMatch(buffer,"name",4))
  143. {
  144. SkipSpaces(&buffer);
  145. AI_AC_GET_STRING(obj.name);
  146. }
  147. else if (TokenMatch(buffer,"texture",7))
  148. {
  149. SkipSpaces(&buffer);
  150. AI_AC_GET_STRING(obj.texture);
  151. }
  152. else if (TokenMatch(buffer,"texrep",6))
  153. {
  154. SkipSpaces(&buffer);
  155. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&obj.texRepeat);
  156. }
  157. else if (TokenMatch(buffer,"rot",3))
  158. {
  159. SkipSpaces(&buffer);
  160. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,9,&obj.rotation);
  161. }
  162. else if (TokenMatch(buffer,"loc",3))
  163. {
  164. SkipSpaces(&buffer);
  165. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&obj.translation);
  166. }
  167. else if (TokenMatch(buffer,"numvert",7))
  168. {
  169. SkipSpaces(&buffer);
  170. unsigned int t = strtol10(buffer,&buffer);
  171. obj.vertices.reserve(t);
  172. for (unsigned int i = 0; i < t;++i)
  173. {
  174. if (!GetNextLine())
  175. {
  176. DefaultLogger::get()->error("AC3D: Unexpected EOF: not all vertices have been parsed yet");
  177. break;
  178. }
  179. else if (!IsNumeric(*buffer))
  180. {
  181. DefaultLogger::get()->error("AC3D: Unexpected token: not all vertices have been parsed yet");
  182. --buffer; // make sure the line is processed a second time
  183. break;
  184. }
  185. obj.vertices.push_back(aiVector3D());
  186. aiVector3D& v = obj.vertices.back();
  187. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&v.x);
  188. }
  189. }
  190. else if (TokenMatch(buffer,"numsurf",7))
  191. {
  192. SkipSpaces(&buffer);
  193. const unsigned int t = strtol10(buffer,&buffer);
  194. obj.surfaces.reserve(t);
  195. for (unsigned int i = 0; i < t;++i)
  196. {
  197. GetNextLine();
  198. if (!TokenMatch(buffer,"SURF",4))
  199. {
  200. DefaultLogger::get()->error("AC3D: SURF token was expected");
  201. --buffer; // make sure the line is processed a second time
  202. break;
  203. }
  204. SkipSpaces(&buffer);
  205. obj.surfaces.push_back(Surface());
  206. Surface& surf = obj.surfaces.back();
  207. surf.flags = strtol_cppstyle(buffer);
  208. while (1)
  209. {
  210. if(!GetNextLine())
  211. {
  212. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface is incomplete");
  213. break;
  214. }
  215. if (TokenMatch(buffer,"mat",3))
  216. {
  217. SkipSpaces(&buffer);
  218. surf.mat = strtol10(buffer);
  219. }
  220. else if (TokenMatch(buffer,"refs",4))
  221. {
  222. SkipSpaces(&buffer);
  223. const unsigned int m = strtol10(buffer);
  224. surf.entries.reserve(m);
  225. obj.numRefs += m;
  226. for (unsigned int k = 0; k < m; ++k)
  227. {
  228. if(!GetNextLine())
  229. {
  230. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface references are incomplete");
  231. break;
  232. }
  233. surf.entries.push_back(Surface::SurfaceEntry());
  234. Surface::SurfaceEntry& entry = surf.entries.back();
  235. entry.first = strtol10(buffer,&buffer);
  236. SkipSpaces(&buffer);
  237. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&entry.second);
  238. }
  239. }
  240. else
  241. {
  242. --buffer; // make sure the line is processed a second time
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. }
  249. DefaultLogger::get()->error("AC3D: Unexpected EOF: \'kids\' line was expected");
  250. }
  251. // ------------------------------------------------------------------------------------------------
  252. // Convert a material from AC3DImporter::Material to aiMaterial
  253. void AC3DImporter::ConvertMaterial(const Object& object,
  254. const Material& matSrc,
  255. MaterialHelper& matDest)
  256. {
  257. aiString s;
  258. if (matSrc.name.length())
  259. {
  260. s.Set(matSrc.name);
  261. matDest.AddProperty(&s,AI_MATKEY_NAME);
  262. }
  263. if (object.texture.length())
  264. {
  265. s.Set(object.texture);
  266. matDest.AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  267. }
  268. matDest.AddProperty<aiColor3D>(&matSrc.rgb,1, AI_MATKEY_COLOR_DIFFUSE);
  269. matDest.AddProperty<aiColor3D>(&matSrc.amb,1, AI_MATKEY_COLOR_AMBIENT);
  270. matDest.AddProperty<aiColor3D>(&matSrc.emis,1,AI_MATKEY_COLOR_EMISSIVE);
  271. matDest.AddProperty<aiColor3D>(&matSrc.spec,1,AI_MATKEY_COLOR_SPECULAR);
  272. int n;
  273. if (matSrc.shin)
  274. {
  275. n = aiShadingMode_Phong;
  276. matDest.AddProperty<float>(&matSrc.shin,1,AI_MATKEY_SHININESS);
  277. }
  278. else n = aiShadingMode_Gouraud;
  279. matDest.AddProperty<int>(&n,1,AI_MATKEY_SHADING_MODEL);
  280. float f = 1.f - matSrc.trans;
  281. matDest.AddProperty<float>(&f,1,AI_MATKEY_OPACITY);
  282. }
  283. // ------------------------------------------------------------------------------------------------
  284. // Converts the loaded data to the internal verbose representation
  285. aiNode* AC3DImporter::ConvertObjectSection(Object& object,
  286. std::vector<aiMesh*>& meshes,
  287. std::vector<MaterialHelper*>& outMaterials,
  288. const std::vector<Material>& materials)
  289. {
  290. aiNode* node = new aiNode();
  291. if (object.vertices.size())
  292. {
  293. if (!object.surfaces.size() || !object.numRefs)
  294. {
  295. /* " An object with 7 vertices (no surfaces, no materials defined).
  296. This is a good way of getting point data into AC3D.
  297. The Vertex->create convex-surface/object can be used on these
  298. vertices to 'wrap' a 3d shape around them "
  299. (http://www.opencity.info/html/ac3dfileformat.html)
  300. therefore: if no surfaces are defined return point data only
  301. */
  302. DefaultLogger::get()->info("AC3D: No surfaces defined in object definition, "
  303. "a point list is returned");
  304. meshes.push_back(new aiMesh());
  305. aiMesh* mesh = meshes.back();
  306. mesh->mNumFaces = mesh->mNumVertices = (unsigned int)object.vertices.size();
  307. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  308. aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  309. for (unsigned int i = 0; i < mesh->mNumVertices;++i,++faces,++verts)
  310. {
  311. *verts = object.vertices[i];
  312. faces->mNumIndices = 1;
  313. faces->mIndices = new unsigned int[1];
  314. faces->mIndices[0] = i;
  315. }
  316. // use the primary material in this case. this should be the
  317. // default material if all objects of the file contain points
  318. // and no faces.
  319. mesh->mMaterialIndex = 0;
  320. outMaterials.push_back(new MaterialHelper());
  321. ConvertMaterial(object, materials[0], *outMaterials.back());
  322. }
  323. else
  324. {
  325. // need to generate one or more meshes for this object.
  326. // find out how many different materials we have
  327. typedef std::pair< unsigned int, unsigned int > IntPair;
  328. typedef std::vector< IntPair > MatTable;
  329. MatTable needMat(materials.size(),IntPair(0,0));
  330. std::vector<Surface>::iterator it,end = object.surfaces.end();
  331. std::vector<Surface::SurfaceEntry>::iterator it2,end2;
  332. for (it = object.surfaces.begin(); it != end; ++it)
  333. {
  334. register unsigned int idx = (*it).mat;
  335. if (idx >= needMat.size())
  336. {
  337. DefaultLogger::get()->error("AC3D: material index os out of range");
  338. idx = 0;
  339. }
  340. if ((*it).entries.empty())
  341. {
  342. DefaultLogger::get()->warn("AC3D: surface her zero vertex references");
  343. }
  344. // validate all vertex indices to make sure we won't crash here
  345. for (it2 = (*it).entries.begin(),
  346. end2 = (*it).entries.end(); it2 != end2; ++it2)
  347. {
  348. if ((*it2).first >= object.vertices.size())
  349. {
  350. DefaultLogger::get()->warn("AC3D: Invalid vertex reference");
  351. (*it2).first = 0;
  352. }
  353. }
  354. if (!needMat[idx].first)++node->mNumMeshes;
  355. switch ((*it).flags & 0xf)
  356. {
  357. // closed line
  358. case 0x1:
  359. needMat[idx].first += (unsigned int)(*it).entries.size();
  360. needMat[idx].second += (unsigned int)(*it).entries.size()<<1u;
  361. break;
  362. // unclosed line
  363. case 0x2:
  364. needMat[idx].first += (unsigned int)(*it).entries.size()-1;
  365. needMat[idx].second += ((unsigned int)(*it).entries.size()-1)<<1u;
  366. break;
  367. // 0 == polygon, else unknown
  368. default:
  369. if ((*it).flags & 0xf)
  370. {
  371. DefaultLogger::get()->warn("AC3D: The type flag of a surface is unknown");
  372. (*it).flags &= ~(0xf);
  373. }
  374. // the number of faces increments by one, the number
  375. // of vertices by surface.numref.
  376. needMat[idx].first++;
  377. needMat[idx].second += (unsigned int)(*it).entries.size();
  378. };
  379. }
  380. unsigned int* pip = node->mMeshes = new unsigned int[node->mNumMeshes];
  381. unsigned int mat = 0;
  382. for (MatTable::const_iterator cit = needMat.begin(), cend = needMat.end();
  383. cit != cend; ++cit, ++mat)
  384. {
  385. if (!(*cit).first)continue;
  386. // allocate a new aiMesh object
  387. *pip++ = (unsigned int)meshes.size();
  388. aiMesh* mesh = new aiMesh();
  389. meshes.push_back(mesh);
  390. mesh->mMaterialIndex = (unsigned int)outMaterials.size();
  391. outMaterials.push_back(new MaterialHelper());
  392. ConvertMaterial(object, materials[mat], *outMaterials.back());
  393. // allocate storage for vertices and normals
  394. mesh->mNumFaces = (*cit).first;
  395. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  396. mesh->mNumVertices = (*cit).second;
  397. aiVector3D* vertices = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  398. unsigned int cur = 0;
  399. // allocate UV coordinates, but only if the texture name for the
  400. // surface is not empty
  401. aiVector3D* uv = NULL;
  402. if(object.texture.length())
  403. {
  404. uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  405. mesh->mNumUVComponents[0] = 2;
  406. }
  407. for (it = object.surfaces.begin(); it != end; ++it)
  408. {
  409. if (mat == (*it).mat)
  410. {
  411. const Surface& src = *it;
  412. // closed polygon
  413. unsigned int type = (*it).flags & 0xf;
  414. if (!type)
  415. {
  416. aiFace& face = *faces++;
  417. if((face.mNumIndices = (unsigned int)src.entries.size()))
  418. {
  419. face.mIndices = new unsigned int[face.mNumIndices];
  420. for (unsigned int i = 0; i < face.mNumIndices;++i,++vertices)
  421. {
  422. const Surface::SurfaceEntry& entry = src.entries[i];
  423. face.mIndices[i] = cur++;
  424. // copy vertex positions
  425. *vertices = object.vertices[entry.first];
  426. // copy texture coordinates (apply the UV offset)
  427. if (uv)
  428. {
  429. uv->x = entry.second.x * object.texRepeat.x;
  430. uv->y = entry.second.y * object.texRepeat.y;
  431. ++uv;
  432. }
  433. }
  434. }
  435. }
  436. else
  437. {
  438. it2 = (*it).entries.begin();
  439. // either a closed or an unclosed line
  440. register unsigned int tmp = (unsigned int)(*it).entries.size();
  441. if (0x2 == type)--tmp;
  442. for (unsigned int m = 0; m < tmp;++m)
  443. {
  444. aiFace& face = *faces++;
  445. face.mNumIndices = 2;
  446. face.mIndices = new unsigned int[2];
  447. face.mIndices[0] = cur++;
  448. face.mIndices[1] = cur++;
  449. // copy vertex positions
  450. *vertices++ = object.vertices[(*it2).first];
  451. // copy texture coordinates (apply the UV offset)
  452. if (uv)
  453. {
  454. uv->x = (*it2).second.x * object.texRepeat.x;
  455. uv->y = (*it2).second.y * object.texRepeat.y;
  456. ++uv;
  457. }
  458. if (0x1 == type && tmp-1 == m)
  459. {
  460. // if this is a closed line repeat its beginning now
  461. it2 = (*it).entries.begin();
  462. }
  463. else ++it2;
  464. // second point
  465. *vertices++ = object.vertices[(*it2).first];
  466. if (uv)
  467. {
  468. uv->x = (*it2).second.x * object.texRepeat.x;
  469. uv->y = (*it2).second.y * object.texRepeat.y;
  470. ++uv;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. }
  479. // add children to the object
  480. if (object.children.size())
  481. {
  482. node->mNumChildren = (unsigned int)object.children.size();
  483. node->mChildren = new aiNode*[node->mNumChildren];
  484. for (unsigned int i = 0; i < node->mNumChildren;++i)
  485. {
  486. node->mChildren[i] = ConvertObjectSection(object.children[i],meshes,outMaterials,materials);
  487. node->mChildren[i]->mParent = node;
  488. }
  489. }
  490. node->mName.Set(object.name);
  491. // setup the local transformation matrix of the object
  492. node->mTransformation = aiMatrix4x4 ( object.rotation );
  493. node->mTransformation.a4 = object.translation.x;
  494. node->mTransformation.b4 = object.translation.y;
  495. node->mTransformation.c4 = object.translation.z;
  496. return node;
  497. }
  498. // ------------------------------------------------------------------------------------------------
  499. void AC3DImporter::SetupProperties(const Importer* pImp)
  500. {
  501. configSplitBFCull = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_SEPARATE_BFCULL,1) ? true : false;
  502. }
  503. // ------------------------------------------------------------------------------------------------
  504. // Imports the given file into the given scene structure.
  505. void AC3DImporter::InternReadFile( const std::string& pFile,
  506. aiScene* pScene, IOSystem* pIOHandler)
  507. {
  508. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  509. // Check whether we can read from the file
  510. if( file.get() == NULL)
  511. throw new ImportErrorException( "Failed to open AC3D file " + pFile + ".");
  512. const unsigned int fileSize = (unsigned int)file->FileSize();
  513. // allocate storage and copy the contents of the file to a memory buffer
  514. std::vector<char> mBuffer2(fileSize+1);
  515. file->Read(&mBuffer2[0], 1, fileSize);
  516. mBuffer2[fileSize] = '\0';
  517. buffer = &mBuffer2[0];
  518. mNumMeshes = 0;
  519. if (::strncmp(buffer,"AC3D",4))
  520. throw new ImportErrorException("AC3D: No valid AC3D file, magic sequence not found");
  521. // print the file format version to the console
  522. unsigned int version = HexDigitToDecimal( buffer[4] );
  523. char msg[3];
  524. #if defined(_MSC_VER)
  525. ::_itoa(version,msg,10);
  526. #else
  527. snprintf(msg, 3, "%d", version); //itoa is not available under linux
  528. #endif
  529. DefaultLogger::get()->info(std::string("AC3D file format version: ") + msg);
  530. std::vector<Material> materials;
  531. materials.reserve(5);
  532. std::vector<Object> rootObjects;
  533. rootObjects.reserve(5);
  534. while (GetNextLine())
  535. {
  536. if (TokenMatch(buffer,"MATERIAL",8))
  537. {
  538. materials.push_back(Material());
  539. Material& mat = materials.back();
  540. // manually parse the material ... sscanf would use the buldin atof ...
  541. // Format: (name) rgb %f %f %f amb %f %f %f emis %f %f %f spec %f %f %f shi %d trans %f
  542. AI_AC_SKIP_TO_NEXT_TOKEN();
  543. if ('\"' == *buffer)
  544. {
  545. AI_AC_GET_STRING(mat.name);
  546. AI_AC_SKIP_TO_NEXT_TOKEN();
  547. }
  548. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("rgb",3,3,&mat.rgb);
  549. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("amb",3,3,&mat.rgb);
  550. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("emis",4,3,&mat.rgb);
  551. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("spec",4,3,&mat.rgb);
  552. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("shi",3,1,&mat.shin);
  553. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("trans",5,1,&mat.trans);
  554. }
  555. LoadObjectSection(rootObjects);
  556. }
  557. if (rootObjects.empty() || !mNumMeshes)
  558. {
  559. throw new ImportErrorException("AC3D: No meshes have been loaded");
  560. }
  561. if (materials.empty())
  562. {
  563. DefaultLogger::get()->warn("AC3D: No material has been found");
  564. materials.push_back(Material());
  565. }
  566. mNumMeshes += (mNumMeshes>>2u) + 1;
  567. std::vector<aiMesh*> meshes;
  568. meshes.reserve(mNumMeshes);
  569. std::vector<MaterialHelper*> omaterials;
  570. materials.reserve(mNumMeshes);
  571. // generate a dummy root if there are multiple objects on the top layer
  572. Object* root;
  573. if (1 == rootObjects.size())
  574. root = &rootObjects[0];
  575. else
  576. {
  577. root = new Object();
  578. }
  579. // now convert the imported stuff to our output data structure
  580. pScene->mRootNode = ConvertObjectSection(*root,meshes,omaterials,materials);
  581. if (1 != rootObjects.size())delete root;
  582. // build output arrays
  583. ai_assert(!meshes.empty());
  584. pScene->mNumMeshes = (unsigned int)meshes.size();
  585. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  586. ::memcpy(pScene->mMeshes,&meshes[0],pScene->mNumMeshes*sizeof(void*));
  587. // build output arrays
  588. pScene->mNumMaterials = (unsigned int)omaterials.size();
  589. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  590. ::memcpy(pScene->mMaterials,&omaterials[0],pScene->mNumMaterials*sizeof(void*));
  591. }
  592. #endif //!defined AI_BUILD_NO_AC_IMPORTER