ACLoader.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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 Implementation of the AC3D importer class */
  35. #include "AssimpPCH.h"
  36. #ifndef ASSIMP_BUILD_NO_AC_IMPORTER
  37. // internal headers
  38. #include "ACLoader.h"
  39. #include "ParsingUtils.h"
  40. #include "fast_atof.h"
  41. #include "Subdivision.h"
  42. using namespace Assimp;
  43. static const aiImporterDesc desc = {
  44. "AC3D Importer",
  45. "",
  46. "",
  47. "",
  48. aiImporterFlags_SupportTextFlavour,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. "ac acc ac3d"
  54. };
  55. // ------------------------------------------------------------------------------------------------
  56. // skip to the next token
  57. #define AI_AC_SKIP_TO_NEXT_TOKEN() \
  58. if (!SkipSpaces(&buffer)) \
  59. { \
  60. DefaultLogger::get()->error("AC3D: Unexpected EOF/EOL"); \
  61. continue; \
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // read a string (may be enclosed in double quotation marks). buffer must point to "
  65. #define AI_AC_GET_STRING(out) \
  66. ++buffer; \
  67. const char* sz = buffer; \
  68. while ('\"' != *buffer) \
  69. { \
  70. if (IsLineEnd( *buffer )) \
  71. { \
  72. DefaultLogger::get()->error("AC3D: Unexpected EOF/EOL in string"); \
  73. out = "ERROR"; \
  74. break; \
  75. } \
  76. ++buffer; \
  77. } \
  78. if (IsLineEnd( *buffer ))continue; \
  79. out = std::string(sz,(unsigned int)(buffer-sz)); \
  80. ++buffer;
  81. // ------------------------------------------------------------------------------------------------
  82. // read 1 to n floats prefixed with an optional predefined identifier
  83. #define AI_AC_CHECKED_LOAD_FLOAT_ARRAY(name,name_length,num,out) \
  84. AI_AC_SKIP_TO_NEXT_TOKEN(); \
  85. if (name_length) \
  86. { \
  87. if (strncmp(buffer,name,name_length) || !IsSpace(buffer[name_length])) \
  88. { \
  89. DefaultLogger::get()->error("AC3D: Unexpexted token. " name " was expected."); \
  90. continue; \
  91. } \
  92. buffer += name_length+1; \
  93. } \
  94. for (unsigned int i = 0; i < num;++i) \
  95. { \
  96. AI_AC_SKIP_TO_NEXT_TOKEN(); \
  97. buffer = fast_atoreal_move<float>(buffer,((float*)out)[i]); \
  98. }
  99. // ------------------------------------------------------------------------------------------------
  100. // Constructor to be privately used by Importer
  101. AC3DImporter::AC3DImporter()
  102. {
  103. // nothing to be done here
  104. }
  105. // ------------------------------------------------------------------------------------------------
  106. // Destructor, private as well
  107. AC3DImporter::~AC3DImporter()
  108. {
  109. // nothing to be done here
  110. }
  111. // ------------------------------------------------------------------------------------------------
  112. // Returns whether the class can handle the format of the given file.
  113. bool AC3DImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler, bool checkSig) const
  114. {
  115. std::string extension = GetExtension(pFile);
  116. // fixme: are acc and ac3d *really* used? Some sources say they are
  117. if(extension == "ac" || extension == "ac3d" || extension == "acc") {
  118. return true;
  119. }
  120. if (!extension.length() || checkSig) {
  121. uint32_t token = AI_MAKE_MAGIC("AC3D");
  122. return CheckMagicToken(pIOHandler,pFile,&token,1,0);
  123. }
  124. return false;
  125. }
  126. // ------------------------------------------------------------------------------------------------
  127. // Loader meta information
  128. const aiImporterDesc* AC3DImporter::GetInfo () const
  129. {
  130. return &desc;
  131. }
  132. // ------------------------------------------------------------------------------------------------
  133. // Get a pointer to the next line from the file
  134. bool AC3DImporter::GetNextLine( )
  135. {
  136. SkipLine(&buffer);
  137. return SkipSpaces(&buffer);
  138. }
  139. // ------------------------------------------------------------------------------------------------
  140. // Parse an object section in an AC file
  141. void AC3DImporter::LoadObjectSection(std::vector<Object>& objects)
  142. {
  143. if (!TokenMatch(buffer,"OBJECT",6))
  144. return;
  145. SkipSpaces(&buffer);
  146. ++mNumMeshes;
  147. objects.push_back(Object());
  148. Object& obj = objects.back();
  149. aiLight* light = NULL;
  150. if (!ASSIMP_strincmp(buffer,"light",5))
  151. {
  152. // This is a light source. Add it to the list
  153. mLights->push_back(light = new aiLight());
  154. // Return a point light with no attenuation
  155. light->mType = aiLightSource_POINT;
  156. light->mColorDiffuse = light->mColorSpecular = aiColor3D(1.f,1.f,1.f);
  157. light->mAttenuationConstant = 1.f;
  158. // Generate a default name for both the light source and the node
  159. // FIXME - what's the right way to print a size_t? Is 'zu' universally available? stick with the safe version.
  160. light->mName.length = ::sprintf(light->mName.data,"ACLight_%i",static_cast<unsigned int>(mLights->size())-1);
  161. obj.name = std::string( light->mName.data );
  162. DefaultLogger::get()->debug("AC3D: Light source encountered");
  163. obj.type = Object::Light;
  164. }
  165. else if (!ASSIMP_strincmp(buffer,"group",5))
  166. {
  167. obj.type = Object::Group;
  168. }
  169. else if (!ASSIMP_strincmp(buffer,"world",5))
  170. {
  171. obj.type = Object::World;
  172. }
  173. else obj.type = Object::Poly;
  174. while (GetNextLine())
  175. {
  176. if (TokenMatch(buffer,"kids",4))
  177. {
  178. SkipSpaces(&buffer);
  179. unsigned int num = strtoul10(buffer,&buffer);
  180. GetNextLine();
  181. if (num)
  182. {
  183. // load the children of this object recursively
  184. obj.children.reserve(num);
  185. for (unsigned int i = 0; i < num; ++i)
  186. LoadObjectSection(obj.children);
  187. }
  188. return;
  189. }
  190. else if (TokenMatch(buffer,"name",4))
  191. {
  192. SkipSpaces(&buffer);
  193. AI_AC_GET_STRING(obj.name);
  194. // If this is a light source, we'll also need to store
  195. // the name of the node in it.
  196. if (light)
  197. {
  198. light->mName.Set(obj.name);
  199. }
  200. }
  201. else if (TokenMatch(buffer,"texture",7))
  202. {
  203. SkipSpaces(&buffer);
  204. AI_AC_GET_STRING(obj.texture);
  205. }
  206. else if (TokenMatch(buffer,"texrep",6))
  207. {
  208. SkipSpaces(&buffer);
  209. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&obj.texRepeat);
  210. if (!obj.texRepeat.x || !obj.texRepeat.y)
  211. obj.texRepeat = aiVector2D (1.f,1.f);
  212. }
  213. else if (TokenMatch(buffer,"texoff",6))
  214. {
  215. SkipSpaces(&buffer);
  216. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&obj.texOffset);
  217. }
  218. else if (TokenMatch(buffer,"rot",3))
  219. {
  220. SkipSpaces(&buffer);
  221. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,9,&obj.rotation);
  222. }
  223. else if (TokenMatch(buffer,"loc",3))
  224. {
  225. SkipSpaces(&buffer);
  226. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&obj.translation);
  227. }
  228. else if (TokenMatch(buffer,"subdiv",6))
  229. {
  230. SkipSpaces(&buffer);
  231. obj.subDiv = strtoul10(buffer,&buffer);
  232. }
  233. else if (TokenMatch(buffer,"crease",6))
  234. {
  235. SkipSpaces(&buffer);
  236. obj.crease = fast_atof(buffer);
  237. }
  238. else if (TokenMatch(buffer,"numvert",7))
  239. {
  240. SkipSpaces(&buffer);
  241. unsigned int t = strtoul10(buffer,&buffer);
  242. obj.vertices.reserve(t);
  243. for (unsigned int i = 0; i < t;++i)
  244. {
  245. if (!GetNextLine())
  246. {
  247. DefaultLogger::get()->error("AC3D: Unexpected EOF: not all vertices have been parsed yet");
  248. break;
  249. }
  250. else if (!IsNumeric(*buffer))
  251. {
  252. DefaultLogger::get()->error("AC3D: Unexpected token: not all vertices have been parsed yet");
  253. --buffer; // make sure the line is processed a second time
  254. break;
  255. }
  256. obj.vertices.push_back(aiVector3D());
  257. aiVector3D& v = obj.vertices.back();
  258. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,3,&v.x);
  259. }
  260. }
  261. else if (TokenMatch(buffer,"numsurf",7))
  262. {
  263. SkipSpaces(&buffer);
  264. bool Q3DWorkAround = false;
  265. const unsigned int t = strtoul10(buffer,&buffer);
  266. obj.surfaces.reserve(t);
  267. for (unsigned int i = 0; i < t;++i)
  268. {
  269. GetNextLine();
  270. if (!TokenMatch(buffer,"SURF",4))
  271. {
  272. // FIX: this can occur for some files - Quick 3D for
  273. // example writes no surf chunks
  274. if (!Q3DWorkAround)
  275. {
  276. DefaultLogger::get()->warn("AC3D: SURF token was expected");
  277. DefaultLogger::get()->debug("Continuing with Quick3D Workaround enabled");
  278. }
  279. --buffer; // make sure the line is processed a second time
  280. // break; --- see fix notes above
  281. Q3DWorkAround = true;
  282. }
  283. SkipSpaces(&buffer);
  284. obj.surfaces.push_back(Surface());
  285. Surface& surf = obj.surfaces.back();
  286. surf.flags = strtoul_cppstyle(buffer);
  287. while (1)
  288. {
  289. if(!GetNextLine())
  290. {
  291. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface is incomplete");
  292. break;
  293. }
  294. if (TokenMatch(buffer,"mat",3))
  295. {
  296. SkipSpaces(&buffer);
  297. surf.mat = strtoul10(buffer);
  298. }
  299. else if (TokenMatch(buffer,"refs",4))
  300. {
  301. // --- see fix notes above
  302. if (Q3DWorkAround)
  303. {
  304. if (!surf.entries.empty())
  305. {
  306. buffer -= 6;
  307. break;
  308. }
  309. }
  310. SkipSpaces(&buffer);
  311. const unsigned int m = strtoul10(buffer);
  312. surf.entries.reserve(m);
  313. obj.numRefs += m;
  314. for (unsigned int k = 0; k < m; ++k)
  315. {
  316. if(!GetNextLine())
  317. {
  318. DefaultLogger::get()->error("AC3D: Unexpected EOF: surface references are incomplete");
  319. break;
  320. }
  321. surf.entries.push_back(Surface::SurfaceEntry());
  322. Surface::SurfaceEntry& entry = surf.entries.back();
  323. entry.first = strtoul10(buffer,&buffer);
  324. SkipSpaces(&buffer);
  325. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("",0,2,&entry.second);
  326. }
  327. }
  328. else
  329. {
  330. --buffer; // make sure the line is processed a second time
  331. break;
  332. }
  333. }
  334. }
  335. }
  336. }
  337. DefaultLogger::get()->error("AC3D: Unexpected EOF: \'kids\' line was expected");
  338. }
  339. // ------------------------------------------------------------------------------------------------
  340. // Convert a material from AC3DImporter::Material to aiMaterial
  341. void AC3DImporter::ConvertMaterial(const Object& object,
  342. const Material& matSrc,
  343. aiMaterial& matDest)
  344. {
  345. aiString s;
  346. if (matSrc.name.length())
  347. {
  348. s.Set(matSrc.name);
  349. matDest.AddProperty(&s,AI_MATKEY_NAME);
  350. }
  351. if (object.texture.length())
  352. {
  353. s.Set(object.texture);
  354. matDest.AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  355. // UV transformation
  356. if (1.f != object.texRepeat.x || 1.f != object.texRepeat.y ||
  357. object.texOffset.x || object.texOffset.y)
  358. {
  359. aiUVTransform transform;
  360. transform.mScaling = object.texRepeat;
  361. transform.mTranslation = object.texOffset;
  362. matDest.AddProperty(&transform,1,AI_MATKEY_UVTRANSFORM_DIFFUSE(0));
  363. }
  364. }
  365. matDest.AddProperty<aiColor3D>(&matSrc.rgb,1, AI_MATKEY_COLOR_DIFFUSE);
  366. matDest.AddProperty<aiColor3D>(&matSrc.amb,1, AI_MATKEY_COLOR_AMBIENT);
  367. matDest.AddProperty<aiColor3D>(&matSrc.emis,1,AI_MATKEY_COLOR_EMISSIVE);
  368. matDest.AddProperty<aiColor3D>(&matSrc.spec,1,AI_MATKEY_COLOR_SPECULAR);
  369. int n;
  370. if (matSrc.shin)
  371. {
  372. n = aiShadingMode_Phong;
  373. matDest.AddProperty<float>(&matSrc.shin,1,AI_MATKEY_SHININESS);
  374. }
  375. else n = aiShadingMode_Gouraud;
  376. matDest.AddProperty<int>(&n,1,AI_MATKEY_SHADING_MODEL);
  377. float f = 1.f - matSrc.trans;
  378. matDest.AddProperty<float>(&f,1,AI_MATKEY_OPACITY);
  379. }
  380. // ------------------------------------------------------------------------------------------------
  381. // Converts the loaded data to the internal verbose representation
  382. aiNode* AC3DImporter::ConvertObjectSection(Object& object,
  383. std::vector<aiMesh*>& meshes,
  384. std::vector<aiMaterial*>& outMaterials,
  385. const std::vector<Material>& materials,
  386. aiNode* parent)
  387. {
  388. aiNode* node = new aiNode();
  389. node->mParent = parent;
  390. if (object.vertices.size())
  391. {
  392. if (!object.surfaces.size() || !object.numRefs)
  393. {
  394. /* " An object with 7 vertices (no surfaces, no materials defined).
  395. This is a good way of getting point data into AC3D.
  396. The Vertex->create convex-surface/object can be used on these
  397. vertices to 'wrap' a 3d shape around them "
  398. (http://www.opencity.info/html/ac3dfileformat.html)
  399. therefore: if no surfaces are defined return point data only
  400. */
  401. DefaultLogger::get()->info("AC3D: No surfaces defined in object definition, "
  402. "a point list is returned");
  403. meshes.push_back(new aiMesh());
  404. aiMesh* mesh = meshes.back();
  405. mesh->mNumFaces = mesh->mNumVertices = (unsigned int)object.vertices.size();
  406. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  407. aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  408. for (unsigned int i = 0; i < mesh->mNumVertices;++i,++faces,++verts)
  409. {
  410. *verts = object.vertices[i];
  411. faces->mNumIndices = 1;
  412. faces->mIndices = new unsigned int[1];
  413. faces->mIndices[0] = i;
  414. }
  415. // use the primary material in this case. this should be the
  416. // default material if all objects of the file contain points
  417. // and no faces.
  418. mesh->mMaterialIndex = 0;
  419. outMaterials.push_back(new aiMaterial());
  420. ConvertMaterial(object, materials[0], *outMaterials.back());
  421. }
  422. else
  423. {
  424. // need to generate one or more meshes for this object.
  425. // find out how many different materials we have
  426. typedef std::pair< unsigned int, unsigned int > IntPair;
  427. typedef std::vector< IntPair > MatTable;
  428. MatTable needMat(materials.size(),IntPair(0,0));
  429. std::vector<Surface>::iterator it,end = object.surfaces.end();
  430. std::vector<Surface::SurfaceEntry>::iterator it2,end2;
  431. for (it = object.surfaces.begin(); it != end; ++it)
  432. {
  433. register unsigned int idx = (*it).mat;
  434. if (idx >= needMat.size())
  435. {
  436. DefaultLogger::get()->error("AC3D: material index is out of range");
  437. idx = 0;
  438. }
  439. if ((*it).entries.empty())
  440. {
  441. DefaultLogger::get()->warn("AC3D: surface her zero vertex references");
  442. }
  443. // validate all vertex indices to make sure we won't crash here
  444. for (it2 = (*it).entries.begin(),
  445. end2 = (*it).entries.end(); it2 != end2; ++it2)
  446. {
  447. if ((*it2).first >= object.vertices.size())
  448. {
  449. DefaultLogger::get()->warn("AC3D: Invalid vertex reference");
  450. (*it2).first = 0;
  451. }
  452. }
  453. if (!needMat[idx].first)++node->mNumMeshes;
  454. switch ((*it).flags & 0xf)
  455. {
  456. // closed line
  457. case 0x1:
  458. needMat[idx].first += (unsigned int)(*it).entries.size();
  459. needMat[idx].second += (unsigned int)(*it).entries.size()<<1u;
  460. break;
  461. // unclosed line
  462. case 0x2:
  463. needMat[idx].first += (unsigned int)(*it).entries.size()-1;
  464. needMat[idx].second += ((unsigned int)(*it).entries.size()-1)<<1u;
  465. break;
  466. // 0 == polygon, else unknown
  467. default:
  468. if ((*it).flags & 0xf)
  469. {
  470. DefaultLogger::get()->warn("AC3D: The type flag of a surface is unknown");
  471. (*it).flags &= ~(0xf);
  472. }
  473. // the number of faces increments by one, the number
  474. // of vertices by surface.numref.
  475. needMat[idx].first++;
  476. needMat[idx].second += (unsigned int)(*it).entries.size();
  477. };
  478. }
  479. unsigned int* pip = node->mMeshes = new unsigned int[node->mNumMeshes];
  480. unsigned int mat = 0;
  481. const size_t oldm = meshes.size();
  482. for (MatTable::const_iterator cit = needMat.begin(), cend = needMat.end();
  483. cit != cend; ++cit, ++mat)
  484. {
  485. if (!(*cit).first)continue;
  486. // allocate a new aiMesh object
  487. *pip++ = (unsigned int)meshes.size();
  488. aiMesh* mesh = new aiMesh();
  489. meshes.push_back(mesh);
  490. mesh->mMaterialIndex = (unsigned int)outMaterials.size();
  491. outMaterials.push_back(new aiMaterial());
  492. ConvertMaterial(object, materials[mat], *outMaterials.back());
  493. // allocate storage for vertices and normals
  494. mesh->mNumFaces = (*cit).first;
  495. aiFace* faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  496. mesh->mNumVertices = (*cit).second;
  497. aiVector3D* vertices = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  498. unsigned int cur = 0;
  499. // allocate UV coordinates, but only if the texture name for the
  500. // surface is not empty
  501. aiVector3D* uv = NULL;
  502. if(object.texture.length())
  503. {
  504. uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  505. mesh->mNumUVComponents[0] = 2;
  506. }
  507. for (it = object.surfaces.begin(); it != end; ++it)
  508. {
  509. if (mat == (*it).mat)
  510. {
  511. const Surface& src = *it;
  512. // closed polygon
  513. unsigned int type = (*it).flags & 0xf;
  514. if (!type)
  515. {
  516. aiFace& face = *faces++;
  517. if((face.mNumIndices = (unsigned int)src.entries.size()))
  518. {
  519. face.mIndices = new unsigned int[face.mNumIndices];
  520. for (unsigned int i = 0; i < face.mNumIndices;++i,++vertices)
  521. {
  522. const Surface::SurfaceEntry& entry = src.entries[i];
  523. face.mIndices[i] = cur++;
  524. // copy vertex positions
  525. *vertices = object.vertices[entry.first] + object.translation;
  526. // copy texture coordinates
  527. if (uv)
  528. {
  529. uv->x = entry.second.x;
  530. uv->y = entry.second.y;
  531. ++uv;
  532. }
  533. }
  534. }
  535. }
  536. else
  537. {
  538. it2 = (*it).entries.begin();
  539. // either a closed or an unclosed line
  540. register unsigned int tmp = (unsigned int)(*it).entries.size();
  541. if (0x2 == type)--tmp;
  542. for (unsigned int m = 0; m < tmp;++m)
  543. {
  544. aiFace& face = *faces++;
  545. face.mNumIndices = 2;
  546. face.mIndices = new unsigned int[2];
  547. face.mIndices[0] = cur++;
  548. face.mIndices[1] = cur++;
  549. // copy vertex positions
  550. *vertices++ = object.vertices[(*it2).first];
  551. // copy texture coordinates
  552. if (uv)
  553. {
  554. uv->x = (*it2).second.x;
  555. uv->y = (*it2).second.y;
  556. ++uv;
  557. }
  558. if (0x1 == type && tmp-1 == m)
  559. {
  560. // if this is a closed line repeat its beginning now
  561. it2 = (*it).entries.begin();
  562. }
  563. else ++it2;
  564. // second point
  565. *vertices++ = object.vertices[(*it2).first];
  566. if (uv)
  567. {
  568. uv->x = (*it2).second.x;
  569. uv->y = (*it2).second.y;
  570. ++uv;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. // Now apply catmull clark subdivision if necessary. We split meshes into
  578. // materials which is not done by AC3D during smoothing, so we need to
  579. // collect all meshes using the same material group.
  580. if (object.subDiv) {
  581. if (configEvalSubdivision) {
  582. boost::scoped_ptr<Subdivider> div(Subdivider::Create(Subdivider::CATMULL_CLARKE));
  583. DefaultLogger::get()->info("AC3D: Evaluating subdivision surface: "+object.name);
  584. std::vector<aiMesh*> cpy(meshes.size()-oldm,NULL);
  585. div->Subdivide(&meshes[oldm],cpy.size(),&cpy.front(),object.subDiv,true);
  586. std::copy(cpy.begin(),cpy.end(),meshes.begin()+oldm);
  587. // previous meshes are deleted vy Subdivide().
  588. }
  589. else {
  590. DefaultLogger::get()->info("AC3D: Letting the subdivision surface untouched due to my configuration: "
  591. +object.name);
  592. }
  593. }
  594. }
  595. }
  596. if (object.name.length())
  597. node->mName.Set(object.name);
  598. else
  599. {
  600. // generate a name depending on the type of the node
  601. switch (object.type)
  602. {
  603. case Object::Group:
  604. node->mName.length = ::sprintf(node->mName.data,"ACGroup_%i",groups++);
  605. break;
  606. case Object::Poly:
  607. node->mName.length = ::sprintf(node->mName.data,"ACPoly_%i",polys++);
  608. break;
  609. case Object::Light:
  610. node->mName.length = ::sprintf(node->mName.data,"ACLight_%i",lights++);
  611. break;
  612. // there shouldn't be more than one world, but we don't care
  613. case Object::World:
  614. node->mName.length = ::sprintf(node->mName.data,"ACWorld_%i",worlds++);
  615. break;
  616. }
  617. }
  618. // setup the local transformation matrix of the object
  619. // compute the transformation offset to the parent node
  620. node->mTransformation = aiMatrix4x4 ( object.rotation );
  621. if (object.type == Object::Group || !object.numRefs)
  622. {
  623. node->mTransformation.a4 = object.translation.x;
  624. node->mTransformation.b4 = object.translation.y;
  625. node->mTransformation.c4 = object.translation.z;
  626. }
  627. // add children to the object
  628. if (object.children.size())
  629. {
  630. node->mNumChildren = (unsigned int)object.children.size();
  631. node->mChildren = new aiNode*[node->mNumChildren];
  632. for (unsigned int i = 0; i < node->mNumChildren;++i)
  633. {
  634. node->mChildren[i] = ConvertObjectSection(object.children[i],meshes,outMaterials,materials,node);
  635. }
  636. }
  637. return node;
  638. }
  639. // ------------------------------------------------------------------------------------------------
  640. void AC3DImporter::SetupProperties(const Importer* pImp)
  641. {
  642. configSplitBFCull = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_SEPARATE_BFCULL,1) ? true : false;
  643. configEvalSubdivision = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_EVAL_SUBDIVISION,1) ? true : false;
  644. }
  645. // ------------------------------------------------------------------------------------------------
  646. // Imports the given file into the given scene structure.
  647. void AC3DImporter::InternReadFile( const std::string& pFile,
  648. aiScene* pScene, IOSystem* pIOHandler)
  649. {
  650. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  651. // Check whether we can read from the file
  652. if( file.get() == NULL)
  653. throw DeadlyImportError( "Failed to open AC3D file " + pFile + ".");
  654. // allocate storage and copy the contents of the file to a memory buffer
  655. std::vector<char> mBuffer2;
  656. TextFileToBuffer(file.get(),mBuffer2);
  657. buffer = &mBuffer2[0];
  658. mNumMeshes = 0;
  659. lights = polys = worlds = groups = 0;
  660. if (::strncmp(buffer,"AC3D",4)) {
  661. throw DeadlyImportError("AC3D: No valid AC3D file, magic sequence not found");
  662. }
  663. // print the file format version to the console
  664. unsigned int version = HexDigitToDecimal( buffer[4] );
  665. char msg[3];
  666. ASSIMP_itoa10(msg,3,version);
  667. DefaultLogger::get()->info(std::string("AC3D file format version: ") + msg);
  668. std::vector<Material> materials;
  669. materials.reserve(5);
  670. std::vector<Object> rootObjects;
  671. rootObjects.reserve(5);
  672. std::vector<aiLight*> lights;
  673. mLights = & lights;
  674. while (GetNextLine())
  675. {
  676. if (TokenMatch(buffer,"MATERIAL",8))
  677. {
  678. materials.push_back(Material());
  679. Material& mat = materials.back();
  680. // manually parse the material ... sscanf would use the buldin atof ...
  681. // Format: (name) rgb %f %f %f amb %f %f %f emis %f %f %f spec %f %f %f shi %d trans %f
  682. AI_AC_SKIP_TO_NEXT_TOKEN();
  683. if ('\"' == *buffer)
  684. {
  685. AI_AC_GET_STRING(mat.name);
  686. AI_AC_SKIP_TO_NEXT_TOKEN();
  687. }
  688. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("rgb",3,3,&mat.rgb);
  689. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("amb",3,3,&mat.amb);
  690. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("emis",4,3,&mat.emis);
  691. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("spec",4,3,&mat.spec);
  692. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("shi",3,1,&mat.shin);
  693. AI_AC_CHECKED_LOAD_FLOAT_ARRAY("trans",5,1,&mat.trans);
  694. }
  695. LoadObjectSection(rootObjects);
  696. }
  697. if (rootObjects.empty() || !mNumMeshes)
  698. {
  699. throw DeadlyImportError("AC3D: No meshes have been loaded");
  700. }
  701. if (materials.empty())
  702. {
  703. DefaultLogger::get()->warn("AC3D: No material has been found");
  704. materials.push_back(Material());
  705. }
  706. mNumMeshes += (mNumMeshes>>2u) + 1;
  707. std::vector<aiMesh*> meshes;
  708. meshes.reserve(mNumMeshes);
  709. std::vector<aiMaterial*> omaterials;
  710. materials.reserve(mNumMeshes);
  711. // generate a dummy root if there are multiple objects on the top layer
  712. Object* root;
  713. if (1 == rootObjects.size())
  714. root = &rootObjects[0];
  715. else
  716. {
  717. root = new Object();
  718. }
  719. // now convert the imported stuff to our output data structure
  720. pScene->mRootNode = ConvertObjectSection(*root,meshes,omaterials,materials);
  721. if (1 != rootObjects.size())delete root;
  722. if (!::strncmp( pScene->mRootNode->mName.data, "Node", 4))
  723. pScene->mRootNode->mName.Set("<AC3DWorld>");
  724. // copy meshes
  725. if (meshes.empty())
  726. {
  727. throw DeadlyImportError("An unknown error occured during converting");
  728. }
  729. pScene->mNumMeshes = (unsigned int)meshes.size();
  730. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  731. ::memcpy(pScene->mMeshes,&meshes[0],pScene->mNumMeshes*sizeof(void*));
  732. // copy materials
  733. pScene->mNumMaterials = (unsigned int)omaterials.size();
  734. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  735. ::memcpy(pScene->mMaterials,&omaterials[0],pScene->mNumMaterials*sizeof(void*));
  736. // copy lights
  737. pScene->mNumLights = (unsigned int)lights.size();
  738. if (lights.size())
  739. {
  740. pScene->mLights = new aiLight*[lights.size()];
  741. ::memcpy(pScene->mLights,&lights[0],lights.size()*sizeof(void*));
  742. }
  743. }
  744. #endif //!defined ASSIMP_BUILD_NO_AC_IMPORTER