ACLoader.cpp 25 KB

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