ACLoader.cpp 25 KB

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