IRRMeshLoader.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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 IrrMesh importer class */
  35. #include "AssimpPCH.h"
  36. #include "IRRMeshLoader.h"
  37. #include "ParsingUtils.h"
  38. #include "fast_atof.h"
  39. using namespace Assimp;
  40. /**** AT FIRST: IrrlightBase, base class for IrrMesh and Irr *******/
  41. // ------------------------------------------------------------------------------------------------
  42. // read a property in hexadecimal format (i.e. ffffffff)
  43. void IrrlichtBase::ReadHexProperty (HexProperty& out)
  44. {
  45. for (int i = 0; i < reader->getAttributeCount();++i)
  46. {
  47. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  48. {
  49. out.name = std::string( reader->getAttributeValue(i) );
  50. }
  51. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  52. {
  53. // parse the hexadecimal value
  54. out.value = strtol16(reader->getAttributeValue(i));
  55. }
  56. }
  57. }
  58. // ------------------------------------------------------------------------------------------------
  59. // read a decimal property
  60. void IrrlichtBase::ReadIntProperty (IntProperty& out)
  61. {
  62. for (int i = 0; i < reader->getAttributeCount();++i)
  63. {
  64. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  65. {
  66. out.name = std::string( reader->getAttributeValue(i) );
  67. }
  68. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  69. {
  70. // parse the ecimal value
  71. out.value = strtol10s(reader->getAttributeValue(i));
  72. }
  73. }
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // read a string property
  77. void IrrlichtBase::ReadStringProperty (StringProperty& out)
  78. {
  79. for (int i = 0; i < reader->getAttributeCount();++i)
  80. {
  81. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  82. {
  83. out.name = std::string( reader->getAttributeValue(i) );
  84. }
  85. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  86. {
  87. // simple copy the string
  88. out.value = std::string (reader->getAttributeValue(i));
  89. }
  90. }
  91. }
  92. // ------------------------------------------------------------------------------------------------
  93. // read a boolean property
  94. void IrrlichtBase::ReadBoolProperty (BoolProperty& out)
  95. {
  96. for (int i = 0; i < reader->getAttributeCount();++i)
  97. {
  98. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  99. {
  100. out.name = std::string( reader->getAttributeValue(i) );
  101. }
  102. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  103. {
  104. // true or false, case insensitive
  105. out.value = (ASSIMP_stricmp( reader->getAttributeValue(i),
  106. "true") ? false : true);
  107. }
  108. }
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. // read a float property
  112. void IrrlichtBase::ReadFloatProperty (FloatProperty& out)
  113. {
  114. for (int i = 0; i < reader->getAttributeCount();++i)
  115. {
  116. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  117. {
  118. out.name = std::string( reader->getAttributeValue(i) );
  119. }
  120. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  121. {
  122. // just parse the float
  123. out.value = fast_atof( reader->getAttributeValue(i) );
  124. }
  125. }
  126. }
  127. // ------------------------------------------------------------------------------------------------
  128. // read a vector property
  129. void IrrlichtBase::ReadVectorProperty (VectorProperty& out)
  130. {
  131. for (int i = 0; i < reader->getAttributeCount();++i)
  132. {
  133. if (!ASSIMP_stricmp(reader->getAttributeName(i),"name"))
  134. {
  135. out.name = std::string( reader->getAttributeValue(i) );
  136. }
  137. else if (!ASSIMP_stricmp(reader->getAttributeName(i),"value"))
  138. {
  139. // three floats, separated with commas
  140. const char* ptr = reader->getAttributeValue(i);
  141. SkipSpaces(&ptr);
  142. ptr = fast_atof_move( ptr,(float&)out.value.x );
  143. SkipSpaces(&ptr);
  144. if (',' != *ptr)
  145. {
  146. DefaultLogger::get()->error("IRR(MESH): Expected comma in vector definition");
  147. }
  148. else SkipSpaces(ptr+1,&ptr);
  149. ptr = fast_atof_move( ptr,(float&)out.value.y );
  150. SkipSpaces(&ptr);
  151. if (',' != *ptr)
  152. {
  153. DefaultLogger::get()->error("IRR(MESH): Expected comma in vector definition");
  154. }
  155. else SkipSpaces(ptr+1,&ptr);
  156. ptr = fast_atof_move( ptr,(float&)out.value.z );
  157. }
  158. }
  159. }
  160. // ------------------------------------------------------------------------------------------------
  161. void ColorFromARGBPacked(uint32_t in, aiColor4D& clr)
  162. {
  163. clr.a = ((in >> 24) & 0xff) / 255.f;
  164. clr.r = ((in >> 16) & 0xff) / 255.f;
  165. clr.g = ((in >> 8) & 0xff) / 255.f;
  166. clr.b = ((in ) & 0xff) / 255.f;
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. int ConvertMappingMode(const std::string& mode)
  170. {
  171. if (mode == "texture_clamp_repeat")
  172. return aiTextureMapMode_Wrap;
  173. else if (mode == "texture_clamp_mirror")
  174. return aiTextureMapMode_Mirror;
  175. return aiTextureMapMode_Clamp;
  176. }
  177. // ------------------------------------------------------------------------------------------------
  178. // Parse a material from the XML file
  179. aiMaterial* IrrlichtBase::ParseMaterial(unsigned int& matFlags)
  180. {
  181. MaterialHelper* mat = new MaterialHelper();
  182. aiColor4D clr;
  183. aiString s;
  184. matFlags = 0;
  185. // Continue reading from the file
  186. while (reader->read())
  187. {
  188. switch (reader->getNodeType())
  189. {
  190. case EXN_ELEMENT:
  191. // Hex properties
  192. if (!ASSIMP_stricmp(reader->getNodeName(),"color"))
  193. {
  194. HexProperty prop;
  195. ReadHexProperty(prop);
  196. if (prop.name == "Diffuse")
  197. {
  198. ColorFromARGBPacked(prop.value,clr);
  199. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  200. }
  201. else if (prop.name == "Ambient")
  202. {
  203. ColorFromARGBPacked(prop.value,clr);
  204. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
  205. }
  206. else if (prop.name == "Specular")
  207. {
  208. ColorFromARGBPacked(prop.value,clr);
  209. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_SPECULAR);
  210. }
  211. else if (prop.name == "Emissive")
  212. {
  213. ColorFromARGBPacked(prop.value,clr);
  214. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  215. }
  216. }
  217. // Float properties
  218. else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
  219. {
  220. FloatProperty prop;
  221. ReadFloatProperty(prop);
  222. if (prop.name == "Shininess")
  223. {
  224. mat->AddProperty(&prop.value,1,AI_MATKEY_SHININESS);
  225. }
  226. }
  227. // Bool properties
  228. else if (!ASSIMP_stricmp(reader->getNodeName(),"bool"))
  229. {
  230. BoolProperty prop;
  231. ReadBoolProperty(prop);
  232. if (prop.name == "Wireframe")
  233. {
  234. int val = (prop.value ? true : false);
  235. mat->AddProperty(&val,1,AI_MATKEY_ENABLE_WIREFRAME);
  236. }
  237. else if (prop.name == "GouraudShading")
  238. {
  239. int val = (prop.value ? aiShadingMode_Gouraud
  240. : aiShadingMode_NoShading);
  241. mat->AddProperty(&val,1,AI_MATKEY_SHADING_MODEL);
  242. }
  243. }
  244. // String properties - textures and texture related properties
  245. else if (!ASSIMP_stricmp(reader->getNodeName(),"texture") ||
  246. !ASSIMP_stricmp(reader->getNodeName(),"enum"))
  247. {
  248. StringProperty prop;
  249. ReadStringProperty(prop);
  250. int cnt = 0;
  251. if (prop.value.length())
  252. {
  253. // material type (shader)
  254. if (prop.name == "Type")
  255. {
  256. if (prop.value == "trans_vertex_alpha")
  257. {
  258. matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
  259. }
  260. else if (prop.value == "lightmap")
  261. {
  262. matFlags = AI_IRRMESH_MAT_lightmap;
  263. }
  264. else if (prop.value == "lightmap_m2")
  265. {
  266. matFlags = AI_IRRMESH_MAT_lightmap_m2;
  267. }
  268. else if (prop.value == "lightmap_m4")
  269. {
  270. matFlags = AI_IRRMESH_MAT_lightmap_m4;
  271. }
  272. }
  273. // Up to 4 texture channels are supported
  274. else if (prop.name == "Texture1")
  275. {
  276. ++cnt;
  277. s.Set(prop.value);
  278. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  279. }
  280. else if (prop.name == "Texture2")
  281. {
  282. ++cnt;
  283. s.Set(prop.value);
  284. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(1));
  285. }
  286. else if (prop.name == "Texture3")
  287. {
  288. ++cnt;
  289. s.Set(prop.value);
  290. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(2));
  291. }
  292. else if (prop.name == "Texture4" )
  293. {
  294. ++cnt;
  295. s.Set(prop.value);
  296. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(3));
  297. }
  298. // Texture mapping options
  299. if (prop.name == "TextureWrap1" && cnt >= 1)
  300. {
  301. int map = ConvertMappingMode(prop.value);
  302. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  303. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  304. }
  305. else if (prop.name == "TextureWrap2" && cnt >= 2)
  306. {
  307. int map = ConvertMappingMode(prop.value);
  308. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
  309. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));
  310. }
  311. else if (prop.name == "TextureWrap3" && cnt >= 3)
  312. {
  313. int map = ConvertMappingMode(prop.value);
  314. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(2));
  315. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(2));
  316. }
  317. else if (prop.name == "TextureWrap4" && cnt >= 4)
  318. {
  319. int map = ConvertMappingMode(prop.value);
  320. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(3));
  321. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(3));
  322. }
  323. }
  324. }
  325. break;
  326. case EXN_ELEMENT_END:
  327. /* Assume there are no further nested nodes in <material> elements
  328. */
  329. if (/* IRRMESH */ !ASSIMP_stricmp(reader->getNodeName(),"material") ||
  330. /* IRR */ !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  331. {
  332. return mat;
  333. }
  334. default:
  335. // GCC complains here ...
  336. break;
  337. }
  338. }
  339. DefaultLogger::get()->error("IRRMESH: Unexpected end of file. Material is not complete");
  340. return mat;
  341. }
  342. // ------------------------------------------------------------------------------------------------
  343. // Constructor to be privately used by Importer
  344. IRRMeshImporter::IRRMeshImporter()
  345. {
  346. // nothing to do here
  347. }
  348. // ------------------------------------------------------------------------------------------------
  349. // Destructor, private as well
  350. IRRMeshImporter::~IRRMeshImporter()
  351. {
  352. // nothing to do here
  353. }
  354. // ------------------------------------------------------------------------------------------------
  355. // Returns whether the class can handle the format of the given file.
  356. bool IRRMeshImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  357. {
  358. /* NOTE: A simple check for the file extension is not enough
  359. * here. Irrmesh and irr are easy, but xml is too generic
  360. * and could be collada, too. So we need to open the file and
  361. * search for typical tokens.
  362. */
  363. std::string::size_type pos = pFile.find_last_of('.');
  364. // no file extension - can't read
  365. if( pos == std::string::npos)
  366. return false;
  367. std::string extension = pFile.substr( pos);
  368. for (std::string::iterator i = extension.begin(); i != extension.end();++i)
  369. *i = ::tolower(*i);
  370. if (extension == ".irrmesh")return true;
  371. else if (extension == ".xml")
  372. {
  373. /* If CanRead() is called to check whether the loader
  374. * supports a specific file extension in general we
  375. * must return true here.
  376. */
  377. if (!pIOHandler)return true;
  378. const char* tokens[] = {"irrmesh"};
  379. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  380. }
  381. return false;
  382. }
  383. // ------------------------------------------------------------------------------------------------
  384. // Imports the given file into the given scene structure.
  385. void IRRMeshImporter::InternReadFile( const std::string& pFile,
  386. aiScene* pScene, IOSystem* pIOHandler)
  387. {
  388. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  389. // Check whether we can read from the file
  390. if( file.get() == NULL)
  391. throw new ImportErrorException( "Failed to open IRRMESH file " + pFile + "");
  392. // Construct the irrXML parser
  393. CIrrXML_IOStreamReader st(file.get());
  394. reader = createIrrXMLReader((IFileReadCallBack*) &st);
  395. // final data
  396. std::vector<aiMaterial*> materials;
  397. std::vector<aiMesh*> meshes;
  398. materials.reserve (5);
  399. meshes.reserve (5);
  400. // temporary data - current mesh buffer
  401. aiMaterial* curMat = NULL;
  402. aiMesh* curMesh = NULL;
  403. unsigned int curMatFlags;
  404. std::vector<aiVector3D> curVertices,curNormals,curTangents,curBitangents;
  405. std::vector<aiColor4D> curColors;
  406. std::vector<aiVector3D> curUVs,curUV2s;
  407. // some temporary variables
  408. int textMeaning = 0;
  409. int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
  410. bool useColors = false;
  411. // Parse the XML file
  412. while (reader->read())
  413. {
  414. switch (reader->getNodeType())
  415. {
  416. case EXN_ELEMENT:
  417. if (!ASSIMP_stricmp(reader->getNodeName(),"buffer") && (curMat || curMesh))
  418. {
  419. // end of previous buffer. A material and a mesh should be there
  420. if ( !curMat || !curMesh)
  421. {
  422. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  423. delete curMat;
  424. delete curMesh;
  425. }
  426. else
  427. {
  428. materials.push_back(curMat);
  429. meshes.push_back(curMesh);
  430. }
  431. curMat = NULL;
  432. curMesh = NULL;
  433. curVertices.clear();
  434. curColors.clear();
  435. curNormals.clear();
  436. curUV2s.clear();
  437. curUVs.clear();
  438. curTangents.clear();
  439. curBitangents.clear();
  440. }
  441. if (!ASSIMP_stricmp(reader->getNodeName(),"material"))
  442. {
  443. if (curMat)
  444. {
  445. DefaultLogger::get()->warn("IRRMESH: Only one material description per buffer, please");
  446. delete curMat;
  447. }
  448. curMat = ParseMaterial(curMatFlags);
  449. }
  450. /* no else here! */ if (!ASSIMP_stricmp(reader->getNodeName(),"vertices"))
  451. {
  452. int num = reader->getAttributeValueAsInt("vertexCount");
  453. curVertices.reserve (num);
  454. curNormals.reserve (num);
  455. curColors.reserve (num);
  456. curUVs.reserve (num);
  457. // Determine the file format
  458. const char* t = reader->getAttributeValueSafe("type");
  459. if (!ASSIMP_stricmp("2tcoords", t))
  460. {
  461. curUV2s.reserve (num);
  462. vertexFormat = 1;
  463. }
  464. else if (!ASSIMP_stricmp("tangents", t))
  465. {
  466. curTangents.reserve (num);
  467. curBitangents.reserve (num);
  468. vertexFormat = 2;
  469. }
  470. else if (ASSIMP_stricmp("standard", t))
  471. {
  472. DefaultLogger::get()->warn("IRRMESH: Unknown vertex format");
  473. }
  474. else vertexFormat = 0;
  475. textMeaning = 1;
  476. }
  477. else if (!ASSIMP_stricmp(reader->getNodeName(),"indices"))
  478. {
  479. if (curVertices.empty())
  480. throw new ImportErrorException("IRRMESH: indices must come after vertices");
  481. textMeaning = 2;
  482. // start a new mesh
  483. curMesh = new aiMesh();
  484. // allocate storage for all faces
  485. curMesh->mNumVertices = reader->getAttributeValueAsInt("indexCount");
  486. if (curMesh->mNumVertices % 3)
  487. {
  488. DefaultLogger::get()->warn("IRRMESH: Number if indices isn't divisible by 3");
  489. }
  490. curMesh->mNumFaces = curMesh->mNumVertices / 3;
  491. curMesh->mFaces = new aiFace[curMesh->mNumFaces];
  492. // setup some members
  493. curMesh->mMaterialIndex = (unsigned int)materials.size();
  494. curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  495. // allocate storage for all vertices
  496. curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
  497. if (curNormals.size() == curVertices.size())
  498. {
  499. curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
  500. }
  501. if (curTangents.size() == curVertices.size())
  502. {
  503. curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
  504. }
  505. if (curBitangents.size() == curVertices.size())
  506. {
  507. curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
  508. }
  509. if (curColors.size() == curVertices.size() && useColors)
  510. {
  511. curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
  512. }
  513. if (curUVs.size() == curVertices.size())
  514. {
  515. curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
  516. }
  517. if (curUV2s.size() == curVertices.size())
  518. {
  519. curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
  520. }
  521. }
  522. break;
  523. case EXN_TEXT:
  524. {
  525. const char* sz = reader->getNodeData();
  526. if (textMeaning == 1)
  527. {
  528. textMeaning = 0;
  529. // read vertices
  530. do
  531. {
  532. SkipSpacesAndLineEnd(&sz);
  533. aiVector3D temp;aiColor4D c;
  534. // Read the vertex position
  535. sz = fast_atof_move(sz,(float&)temp.x);
  536. SkipSpaces(&sz);
  537. sz = fast_atof_move(sz,(float&)temp.z);
  538. SkipSpaces(&sz);
  539. sz = fast_atof_move(sz,(float&)temp.y);
  540. SkipSpaces(&sz);
  541. temp.y *= -1.0f;
  542. curVertices.push_back(temp);
  543. // Read the vertex normals
  544. sz = fast_atof_move(sz,(float&)temp.x);
  545. SkipSpaces(&sz);
  546. sz = fast_atof_move(sz,(float&)temp.z);
  547. SkipSpaces(&sz);
  548. sz = fast_atof_move(sz,(float&)temp.y);
  549. SkipSpaces(&sz);
  550. temp.y *= -1.0f;
  551. curNormals.push_back(temp);
  552. // read the vertex colors
  553. uint32_t clr = strtol16(sz,&sz);
  554. ColorFromARGBPacked(clr,c);
  555. if (!curColors.empty() && c != *(curColors.end()-1))
  556. useColors = true;
  557. curColors.push_back(c);
  558. SkipSpaces(&sz);
  559. // read the first UV coordinate set
  560. sz = fast_atof_move(sz,(float&)temp.x);
  561. SkipSpaces(&sz);
  562. sz = fast_atof_move(sz,(float&)temp.y);
  563. SkipSpaces(&sz);
  564. temp.z = 0.f;
  565. temp.y = 1.f - temp.y; // DX to OGL
  566. curUVs.push_back(temp);
  567. // read the (optional) second UV coordinate set
  568. if (vertexFormat == 1)
  569. {
  570. sz = fast_atof_move(sz,(float&)temp.x);
  571. SkipSpaces(&sz);
  572. sz = fast_atof_move(sz,(float&)temp.y);
  573. temp.y = 1.f - temp.y; // DX to OGL
  574. curUV2s.push_back(temp);
  575. }
  576. // read optional tangent and bitangent vectors
  577. else if (vertexFormat == 2)
  578. {
  579. // tangents
  580. sz = fast_atof_move(sz,(float&)temp.x);
  581. SkipSpaces(&sz);
  582. sz = fast_atof_move(sz,(float&)temp.z);
  583. SkipSpaces(&sz);
  584. sz = fast_atof_move(sz,(float&)temp.y);
  585. SkipSpaces(&sz);
  586. temp.y *= -1.0f;
  587. curTangents.push_back(temp);
  588. // bitangents
  589. sz = fast_atof_move(sz,(float&)temp.x);
  590. SkipSpaces(&sz);
  591. sz = fast_atof_move(sz,(float&)temp.z);
  592. SkipSpaces(&sz);
  593. sz = fast_atof_move(sz,(float&)temp.y);
  594. SkipSpaces(&sz);
  595. temp.y *= -1.0f;
  596. curBitangents.push_back(temp);
  597. }
  598. }
  599. /* IMPORTANT: We assume that each vertex is specified in one
  600. line. So we can skip the rest of the line - unknown vertex
  601. elements are ignored.
  602. */
  603. while (SkipLine(&sz));
  604. }
  605. else if (textMeaning == 2)
  606. {
  607. textMeaning = 0;
  608. // read indices
  609. aiFace* curFace = curMesh->mFaces;
  610. aiFace* const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
  611. aiVector3D* pcV = curMesh->mVertices;
  612. aiVector3D* pcN = curMesh->mNormals;
  613. aiVector3D* pcT = curMesh->mTangents;
  614. aiVector3D* pcB = curMesh->mBitangents;
  615. aiColor4D* pcC0 = curMesh->mColors[0];
  616. aiVector3D* pcT0 = curMesh->mTextureCoords[0];
  617. aiVector3D* pcT1 = curMesh->mTextureCoords[1];
  618. unsigned int curIdx = 0;
  619. unsigned int total = 0;
  620. while(SkipSpacesAndLineEnd(&sz))
  621. {
  622. if (curFace >= faceEnd)
  623. {
  624. DefaultLogger::get()->error("IRRMESH: Too many indices");
  625. break;
  626. }
  627. if (!curIdx)
  628. {
  629. curFace->mNumIndices = 3;
  630. curFace->mIndices = new unsigned int[3];
  631. }
  632. unsigned int idx = strtol10(sz,&sz);
  633. if (idx >= curVertices.size())
  634. {
  635. DefaultLogger::get()->error("IRRMESH: Index out of range");
  636. idx = 0;
  637. }
  638. curFace->mIndices[curIdx] = total++;
  639. *pcV++ = curVertices[idx];
  640. if (pcN)*pcN++ = curNormals[idx];
  641. if (pcT)*pcT++ = curTangents[idx];
  642. if (pcB)*pcB++ = curBitangents[idx];
  643. if (pcC0)*pcC0++ = curColors[idx];
  644. if (pcT0)*pcT0++ = curUVs[idx];
  645. if (pcT1)*pcT1++ = curUV2s[idx];
  646. if (++curIdx == 3)
  647. {
  648. ++curFace;
  649. curIdx = 0;
  650. }
  651. }
  652. if (curFace != faceEnd)
  653. DefaultLogger::get()->error("IRRMESH: Not enough indices");
  654. // Finish processing the mesh - do some small material workarounds
  655. if (curMatFlags == AI_IRRMESH_MAT_trans_vertex_alpha && !useColors)
  656. {
  657. // Take the opacity value of the current material
  658. // from the common vertex color alpha
  659. MaterialHelper* mat = (MaterialHelper*)curMat;
  660. mat->AddProperty(&curColors[0].a,1,AI_MATKEY_OPACITY);
  661. }
  662. /* TODO: Add lightmapping support here */
  663. }}
  664. break;
  665. default:
  666. // GCC complains here ...
  667. break;
  668. };
  669. }
  670. // end of the last buffer. A material and a mesh should be there
  671. if (curMat || curMesh)
  672. {
  673. if ( !curMat || !curMesh)
  674. {
  675. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  676. delete curMat;
  677. delete curMesh;
  678. }
  679. else
  680. {
  681. materials.push_back(curMat);
  682. meshes.push_back(curMesh);
  683. }
  684. }
  685. if (materials.empty())
  686. throw new ImportErrorException("IRRMESH: Unable to read a mesh from this file");
  687. // now generate the output scene
  688. pScene->mNumMeshes = (unsigned int)meshes.size();
  689. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  690. ::memcpy(pScene->mMeshes,&meshes[0],sizeof(void*)*pScene->mNumMeshes);
  691. pScene->mNumMaterials = (unsigned int)materials.size();
  692. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  693. ::memcpy(pScene->mMaterials,&materials[0],sizeof(void*)*pScene->mNumMaterials);
  694. pScene->mRootNode = new aiNode();
  695. pScene->mRootNode->mName.Set("<IRRMesh>");
  696. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  697. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  698. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  699. pScene->mRootNode->mMeshes[i] = i;
  700. delete reader;
  701. AI_DEBUG_INVALIDATE_PTR(reader);
  702. }