IRRMeshLoader.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. {
  173. return aiTextureMapMode_Wrap;
  174. }
  175. else if (mode == "texture_clamp_mirror")
  176. return aiTextureMapMode_Mirror;
  177. return aiTextureMapMode_Clamp;
  178. }
  179. // ------------------------------------------------------------------------------------------------
  180. // Parse a material from the XML file
  181. aiMaterial* IrrlichtBase::ParseMaterial(unsigned int& matFlags)
  182. {
  183. MaterialHelper* mat = new MaterialHelper();
  184. aiColor4D clr;
  185. aiString s;
  186. matFlags = 0; // zero output flags
  187. int cnt = 0; // number of used texture channels
  188. // Continue reading from the file
  189. while (reader->read())
  190. {
  191. switch (reader->getNodeType())
  192. {
  193. case EXN_ELEMENT:
  194. // Hex properties
  195. if (!ASSIMP_stricmp(reader->getNodeName(),"color"))
  196. {
  197. HexProperty prop;
  198. ReadHexProperty(prop);
  199. if (prop.name == "Diffuse")
  200. {
  201. ColorFromARGBPacked(prop.value,clr);
  202. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_DIFFUSE);
  203. }
  204. else if (prop.name == "Ambient")
  205. {
  206. ColorFromARGBPacked(prop.value,clr);
  207. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_AMBIENT);
  208. }
  209. else if (prop.name == "Specular")
  210. {
  211. ColorFromARGBPacked(prop.value,clr);
  212. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_SPECULAR);
  213. }
  214. else if (prop.name == "Emissive")
  215. {
  216. ColorFromARGBPacked(prop.value,clr);
  217. mat->AddProperty(&clr,1,AI_MATKEY_COLOR_EMISSIVE);
  218. }
  219. }
  220. // Float properties
  221. else if (!ASSIMP_stricmp(reader->getNodeName(),"float"))
  222. {
  223. FloatProperty prop;
  224. ReadFloatProperty(prop);
  225. if (prop.name == "Shininess")
  226. {
  227. mat->AddProperty(&prop.value,1,AI_MATKEY_SHININESS);
  228. }
  229. }
  230. // Bool properties
  231. else if (!ASSIMP_stricmp(reader->getNodeName(),"bool"))
  232. {
  233. BoolProperty prop;
  234. ReadBoolProperty(prop);
  235. if (prop.name == "Wireframe")
  236. {
  237. int val = (prop.value ? true : false);
  238. mat->AddProperty(&val,1,AI_MATKEY_ENABLE_WIREFRAME);
  239. }
  240. else if (prop.name == "GouraudShading")
  241. {
  242. int val = (prop.value ? aiShadingMode_Gouraud
  243. : aiShadingMode_NoShading);
  244. mat->AddProperty(&val,1,AI_MATKEY_SHADING_MODEL);
  245. }
  246. }
  247. // String properties - textures and texture related properties
  248. else if (!ASSIMP_stricmp(reader->getNodeName(),"texture") ||
  249. !ASSIMP_stricmp(reader->getNodeName(),"enum"))
  250. {
  251. StringProperty prop;
  252. ReadStringProperty(prop);
  253. if (prop.value.length())
  254. {
  255. // material type (shader)
  256. if (prop.name == "Type")
  257. {
  258. if (prop.value == "trans_vertex_alpha")
  259. {
  260. matFlags = AI_IRRMESH_MAT_trans_vertex_alpha;
  261. }
  262. else if (prop.value == "lightmap")
  263. {
  264. matFlags = AI_IRRMESH_MAT_lightmap;
  265. }
  266. else if (prop.value == "solid_2layer")
  267. {
  268. matFlags = AI_IRRMESH_MAT_solid_2layer;
  269. }
  270. else if (prop.value == "lightmap_m2")
  271. {
  272. matFlags = AI_IRRMESH_MAT_lightmap_m2;
  273. }
  274. else if (prop.value == "lightmap_m4")
  275. {
  276. matFlags = AI_IRRMESH_MAT_lightmap_m4;
  277. }
  278. else if (prop.value == "lightmap_light")
  279. {
  280. matFlags = AI_IRRMESH_MAT_lightmap_light;
  281. }
  282. else if (prop.value == "lightmap_light_m2")
  283. {
  284. matFlags = AI_IRRMESH_MAT_lightmap_light_m2;
  285. }
  286. else if (prop.value == "lightmap_light_m4")
  287. {
  288. matFlags = AI_IRRMESH_MAT_lightmap_light_m4;
  289. }
  290. else if (prop.value == "lightmap_add")
  291. {
  292. matFlags = AI_IRRMESH_MAT_lightmap_add;
  293. }
  294. // Normal and parallax maps are treated equally
  295. else if (prop.value == "normalmap_solid" ||
  296. prop.value == "parallaxmap_solid")
  297. {
  298. matFlags = AI_IRRMESH_MAT_normalmap_solid;
  299. }
  300. else if (prop.value == "normalmap_trans_vertex_alpha" ||
  301. prop.value == "parallaxmap_trans_vertex_alpha")
  302. {
  303. matFlags = AI_IRRMESH_MAT_normalmap_tva;
  304. }
  305. else if (prop.value == "normalmap_trans_add" ||
  306. prop.value == "parallaxmap_trans_add")
  307. {
  308. matFlags = AI_IRRMESH_MAT_normalmap_ta;
  309. }
  310. }
  311. // Up to 4 texture channels are supported
  312. else if (prop.name == "Texture1")
  313. {
  314. // Always accept the primary texture channel
  315. ++cnt;
  316. s.Set(prop.value);
  317. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  318. }
  319. else if (prop.name == "Texture2")
  320. {
  321. // 2-layer material lightmapped?
  322. if (matFlags & (AI_IRRMESH_MAT_solid_2layer | AI_IRRMESH_MAT_lightmap))
  323. {
  324. ++cnt;
  325. s.Set(prop.value);
  326. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(1));
  327. // set the corresponding material flag
  328. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  329. }
  330. // alternatively: normal or parallax mapping
  331. else if (matFlags & AI_IRRMESH_MAT_normalmap_solid)
  332. {
  333. ++cnt;
  334. s.Set(prop.value);
  335. mat->AddProperty(&s,AI_MATKEY_TEXTURE_NORMALS(1));
  336. // set the corresponding material flag
  337. matFlags |= AI_IRRMESH_EXTRA_2ND_TEXTURE;
  338. }
  339. }
  340. else if (prop.name == "Texture3")
  341. {
  342. // We don't process the third texture channel as Irrlicht
  343. // does not seem to use it.
  344. #if 0
  345. ++cnt;
  346. s.Set(prop.value);
  347. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(2));
  348. #endif
  349. }
  350. else if (prop.name == "Texture4" )
  351. {
  352. // We don't process the fourth texture channel as Irrlicht
  353. // does not seem to use it.
  354. #if 0
  355. ++cnt;
  356. s.Set(prop.value);
  357. mat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(3));
  358. #endif
  359. }
  360. // Texture mapping options
  361. if (prop.name == "TextureWrap1" && cnt >= 1)
  362. {
  363. int map = ConvertMappingMode(prop.value);
  364. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0));
  365. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0));
  366. }
  367. else if (prop.name == "TextureWrap2" && cnt >= 2)
  368. {
  369. int map = ConvertMappingMode(prop.value);
  370. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(1));
  371. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(1));
  372. }
  373. else if (prop.name == "TextureWrap3" && cnt >= 3)
  374. {
  375. int map = ConvertMappingMode(prop.value);
  376. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(2));
  377. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(2));
  378. }
  379. else if (prop.name == "TextureWrap4" && cnt >= 4)
  380. {
  381. int map = ConvertMappingMode(prop.value);
  382. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(3));
  383. mat->AddProperty(&map,1,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(3));
  384. }
  385. }
  386. }
  387. break;
  388. case EXN_ELEMENT_END:
  389. /* Assume there are no further nested nodes in <material> elements
  390. */
  391. if (/* IRRMESH */ !ASSIMP_stricmp(reader->getNodeName(),"material") ||
  392. /* IRR */ !ASSIMP_stricmp(reader->getNodeName(),"attributes"))
  393. {
  394. // Now process lightmapping flags
  395. // We should have at least one texture, however
  396. // if there are multiple textures we assign the
  397. // lightmap settings to the last texture.
  398. if (cnt && matFlags & AI_IRRMESH_MAT_lightmap)
  399. {
  400. float f = 1.f;
  401. // Additive lightmap?
  402. int op = (matFlags & AI_IRRMESH_MAT_lightmap_add
  403. ? aiTextureOp_Add : aiTextureOp_Multiply);
  404. // Handle Irrlicht's lightmapping scaling factor
  405. if (matFlags & AI_IRRMESH_MAT_lightmap_m2 ||
  406. matFlags & AI_IRRMESH_MAT_lightmap_light_m2)
  407. {
  408. f = 2.f;
  409. }
  410. else if (matFlags & AI_IRRMESH_MAT_lightmap_m4 ||
  411. matFlags & AI_IRRMESH_MAT_lightmap_light_m4)
  412. {
  413. f = 4.f;
  414. }
  415. mat->AddProperty( &f, 1, AI_MATKEY_TEXBLEND_DIFFUSE(cnt-1));
  416. mat->AddProperty( &op,1, AI_MATKEY_TEXOP_DIFFUSE(cnt-1));
  417. }
  418. return mat;
  419. }
  420. default:
  421. // GCC complains here ...
  422. break;
  423. }
  424. }
  425. DefaultLogger::get()->error("IRRMESH: Unexpected end of file. Material is not complete");
  426. return mat;
  427. }
  428. // ------------------------------------------------------------------------------------------------
  429. // Constructor to be privately used by Importer
  430. IRRMeshImporter::IRRMeshImporter()
  431. {
  432. // nothing to do here
  433. }
  434. // ------------------------------------------------------------------------------------------------
  435. // Destructor, private as well
  436. IRRMeshImporter::~IRRMeshImporter()
  437. {
  438. // nothing to do here
  439. }
  440. // ------------------------------------------------------------------------------------------------
  441. // Returns whether the class can handle the format of the given file.
  442. bool IRRMeshImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  443. {
  444. /* NOTE: A simple check for the file extension is not enough
  445. * here. Irrmesh and irr are easy, but xml is too generic
  446. * and could be collada, too. So we need to open the file and
  447. * search for typical tokens.
  448. */
  449. std::string::size_type pos = pFile.find_last_of('.');
  450. // no file extension - can't read
  451. if( pos == std::string::npos)
  452. return false;
  453. std::string extension = pFile.substr( pos);
  454. for (std::string::iterator i = extension.begin(); i != extension.end();++i)
  455. *i = ::tolower(*i);
  456. if (extension == ".irrmesh")return true;
  457. else if (extension == ".xml")
  458. {
  459. /* If CanRead() is called to check whether the loader
  460. * supports a specific file extension in general we
  461. * must return true here.
  462. */
  463. if (!pIOHandler)return true;
  464. const char* tokens[] = {"irrmesh"};
  465. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  466. }
  467. return false;
  468. }
  469. // ------------------------------------------------------------------------------------------------
  470. // Imports the given file into the given scene structure.
  471. void IRRMeshImporter::InternReadFile( const std::string& pFile,
  472. aiScene* pScene, IOSystem* pIOHandler)
  473. {
  474. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  475. // Check whether we can read from the file
  476. if( file.get() == NULL)
  477. throw new ImportErrorException( "Failed to open IRRMESH file " + pFile + "");
  478. // Construct the irrXML parser
  479. CIrrXML_IOStreamReader st(file.get());
  480. reader = createIrrXMLReader((IFileReadCallBack*) &st);
  481. // final data
  482. std::vector<aiMaterial*> materials;
  483. std::vector<aiMesh*> meshes;
  484. materials.reserve (5);
  485. meshes.reserve (5);
  486. // temporary data - current mesh buffer
  487. aiMaterial* curMat = NULL;
  488. aiMesh* curMesh = NULL;
  489. unsigned int curMatFlags;
  490. std::vector<aiVector3D> curVertices,curNormals,curTangents,curBitangents;
  491. std::vector<aiColor4D> curColors;
  492. std::vector<aiVector3D> curUVs,curUV2s;
  493. // some temporary variables
  494. int textMeaning = 0;
  495. int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
  496. bool useColors = false;
  497. bool needLightMap = false;
  498. // Parse the XML file
  499. while (reader->read())
  500. {
  501. switch (reader->getNodeType())
  502. {
  503. case EXN_ELEMENT:
  504. if (!ASSIMP_stricmp(reader->getNodeName(),"buffer") && (curMat || curMesh))
  505. {
  506. // end of previous buffer. A material and a mesh should be there
  507. if ( !curMat || !curMesh)
  508. {
  509. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  510. delete curMat;
  511. delete curMesh;
  512. }
  513. else
  514. {
  515. materials.push_back(curMat);
  516. meshes.push_back(curMesh);
  517. }
  518. curMat = NULL;
  519. curMesh = NULL;
  520. curVertices.clear();
  521. curColors.clear();
  522. curNormals.clear();
  523. curUV2s.clear();
  524. curUVs.clear();
  525. curTangents.clear();
  526. curBitangents.clear();
  527. }
  528. if (!ASSIMP_stricmp(reader->getNodeName(),"material"))
  529. {
  530. if (curMat)
  531. {
  532. DefaultLogger::get()->warn("IRRMESH: Only one material description per buffer, please");
  533. delete curMat;curMat = NULL;
  534. }
  535. curMat = ParseMaterial(curMatFlags);
  536. }
  537. /* no else here! */ if (!ASSIMP_stricmp(reader->getNodeName(),"vertices"))
  538. {
  539. int num = reader->getAttributeValueAsInt("vertexCount");
  540. if (!num)
  541. {
  542. // This is possible ... remove the mesh from the list
  543. // and skip further reading
  544. DefaultLogger::get()->warn("IRRMESH: Found mesh with zero vertices");
  545. delete curMat;curMat = NULL;
  546. curMesh = NULL;
  547. textMeaning = 0;
  548. continue;
  549. }
  550. curVertices.reserve (num);
  551. curNormals.reserve (num);
  552. curColors.reserve (num);
  553. curUVs.reserve (num);
  554. // Determine the file format
  555. const char* t = reader->getAttributeValueSafe("type");
  556. if (!ASSIMP_stricmp("2tcoords", t))
  557. {
  558. curUV2s.reserve (num);
  559. vertexFormat = 1;
  560. if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE)
  561. {
  562. // *********************************************************
  563. // We have a second texture! So use this UV channel
  564. // for it. The 2nd texture can be either a normal
  565. // texture (solid_2layer or lightmap_xxx) or a normal
  566. // map (normal_..., parallax_...)
  567. // *********************************************************
  568. int idx = 1;
  569. MaterialHelper* mat = ( MaterialHelper* ) curMat;
  570. if (curMatFlags & (AI_IRRMESH_MAT_solid_2layer | AI_IRRMESH_MAT_lightmap))
  571. {
  572. mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_DIFFUSE(0));
  573. }
  574. else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid)
  575. {
  576. mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_NORMALS(0));
  577. }
  578. }
  579. }
  580. else if (!ASSIMP_stricmp("tangents", t))
  581. {
  582. curTangents.reserve (num);
  583. curBitangents.reserve (num);
  584. vertexFormat = 2;
  585. }
  586. else if (ASSIMP_stricmp("standard", t))
  587. {
  588. delete curMat;
  589. DefaultLogger::get()->warn("IRRMESH: Unknown vertex format");
  590. }
  591. else vertexFormat = 0;
  592. textMeaning = 1;
  593. }
  594. else if (!ASSIMP_stricmp(reader->getNodeName(),"indices"))
  595. {
  596. if (curVertices.empty() && curMat)
  597. {
  598. delete curMat;
  599. throw new ImportErrorException("IRRMESH: indices must come after vertices");
  600. }
  601. textMeaning = 2;
  602. // start a new mesh
  603. curMesh = new aiMesh();
  604. // allocate storage for all faces
  605. curMesh->mNumVertices = reader->getAttributeValueAsInt("indexCount");
  606. if (!curMesh->mNumVertices)
  607. {
  608. // This is possible ... remove the mesh from the list
  609. // and skip further reading
  610. DefaultLogger::get()->warn("IRRMESH: Found mesh with zero indices");
  611. // mesh - away
  612. delete curMesh; curMesh = NULL;
  613. // material - away
  614. delete curMat;curMat = NULL;
  615. textMeaning = 0;
  616. continue;
  617. }
  618. if (curMesh->mNumVertices % 3)
  619. {
  620. DefaultLogger::get()->warn("IRRMESH: Number if indices isn't divisible by 3");
  621. }
  622. curMesh->mNumFaces = curMesh->mNumVertices / 3;
  623. curMesh->mFaces = new aiFace[curMesh->mNumFaces];
  624. // setup some members
  625. curMesh->mMaterialIndex = (unsigned int)materials.size();
  626. curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  627. // allocate storage for all vertices
  628. curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
  629. if (curNormals.size() == curVertices.size())
  630. {
  631. curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
  632. }
  633. if (curTangents.size() == curVertices.size())
  634. {
  635. curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
  636. }
  637. if (curBitangents.size() == curVertices.size())
  638. {
  639. curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
  640. }
  641. if (curColors.size() == curVertices.size() && useColors)
  642. {
  643. curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
  644. }
  645. if (curUVs.size() == curVertices.size())
  646. {
  647. curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
  648. }
  649. if (curUV2s.size() == curVertices.size())
  650. {
  651. curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
  652. }
  653. }
  654. break;
  655. case EXN_TEXT:
  656. {
  657. const char* sz = reader->getNodeData();
  658. if (textMeaning == 1)
  659. {
  660. textMeaning = 0;
  661. // read vertices
  662. do
  663. {
  664. SkipSpacesAndLineEnd(&sz);
  665. aiVector3D temp;aiColor4D c;
  666. // Read the vertex position
  667. sz = fast_atof_move(sz,(float&)temp.x);
  668. SkipSpaces(&sz);
  669. sz = fast_atof_move(sz,(float&)temp.z);
  670. SkipSpaces(&sz);
  671. sz = fast_atof_move(sz,(float&)temp.y);
  672. SkipSpaces(&sz);
  673. temp.y *= -1.0f;
  674. curVertices.push_back(temp);
  675. // Read the vertex normals
  676. sz = fast_atof_move(sz,(float&)temp.x);
  677. SkipSpaces(&sz);
  678. sz = fast_atof_move(sz,(float&)temp.z);
  679. SkipSpaces(&sz);
  680. sz = fast_atof_move(sz,(float&)temp.y);
  681. SkipSpaces(&sz);
  682. temp.y *= -1.0f;
  683. curNormals.push_back(temp);
  684. // read the vertex colors
  685. uint32_t clr = strtol16(sz,&sz);
  686. ColorFromARGBPacked(clr,c);
  687. if (!curColors.empty() && c != *(curColors.end()-1))
  688. useColors = true;
  689. curColors.push_back(c);
  690. SkipSpaces(&sz);
  691. // read the first UV coordinate set
  692. sz = fast_atof_move(sz,(float&)temp.x);
  693. SkipSpaces(&sz);
  694. sz = fast_atof_move(sz,(float&)temp.y);
  695. SkipSpaces(&sz);
  696. temp.z = 0.f;
  697. temp.y = 1.f - temp.y; // DX to OGL
  698. curUVs.push_back(temp);
  699. // read the (optional) second UV coordinate set
  700. if (vertexFormat == 1)
  701. {
  702. sz = fast_atof_move(sz,(float&)temp.x);
  703. SkipSpaces(&sz);
  704. sz = fast_atof_move(sz,(float&)temp.y);
  705. temp.y = 1.f - temp.y; // DX to OGL
  706. curUV2s.push_back(temp);
  707. }
  708. // read optional tangent and bitangent vectors
  709. else if (vertexFormat == 2)
  710. {
  711. // tangents
  712. sz = fast_atof_move(sz,(float&)temp.x);
  713. SkipSpaces(&sz);
  714. sz = fast_atof_move(sz,(float&)temp.z);
  715. SkipSpaces(&sz);
  716. sz = fast_atof_move(sz,(float&)temp.y);
  717. SkipSpaces(&sz);
  718. temp.y *= -1.0f;
  719. curTangents.push_back(temp);
  720. // bitangents
  721. sz = fast_atof_move(sz,(float&)temp.x);
  722. SkipSpaces(&sz);
  723. sz = fast_atof_move(sz,(float&)temp.z);
  724. SkipSpaces(&sz);
  725. sz = fast_atof_move(sz,(float&)temp.y);
  726. SkipSpaces(&sz);
  727. temp.y *= -1.0f;
  728. curBitangents.push_back(temp);
  729. }
  730. }
  731. /* IMPORTANT: We assume that each vertex is specified in one
  732. line. So we can skip the rest of the line - unknown vertex
  733. elements are ignored.
  734. */
  735. while (SkipLine(&sz));
  736. }
  737. else if (textMeaning == 2)
  738. {
  739. textMeaning = 0;
  740. // read indices
  741. aiFace* curFace = curMesh->mFaces;
  742. aiFace* const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
  743. aiVector3D* pcV = curMesh->mVertices;
  744. aiVector3D* pcN = curMesh->mNormals;
  745. aiVector3D* pcT = curMesh->mTangents;
  746. aiVector3D* pcB = curMesh->mBitangents;
  747. aiColor4D* pcC0 = curMesh->mColors[0];
  748. aiVector3D* pcT0 = curMesh->mTextureCoords[0];
  749. aiVector3D* pcT1 = curMesh->mTextureCoords[1];
  750. unsigned int curIdx = 0;
  751. unsigned int total = 0;
  752. while(SkipSpacesAndLineEnd(&sz))
  753. {
  754. if (curFace >= faceEnd)
  755. {
  756. DefaultLogger::get()->error("IRRMESH: Too many indices");
  757. break;
  758. }
  759. if (!curIdx)
  760. {
  761. curFace->mNumIndices = 3;
  762. curFace->mIndices = new unsigned int[3];
  763. }
  764. unsigned int idx = strtol10(sz,&sz);
  765. if (idx >= curVertices.size())
  766. {
  767. DefaultLogger::get()->error("IRRMESH: Index out of range");
  768. idx = 0;
  769. }
  770. curFace->mIndices[curIdx] = total++;
  771. *pcV++ = curVertices[idx];
  772. if (pcN)*pcN++ = curNormals[idx];
  773. if (pcT)*pcT++ = curTangents[idx];
  774. if (pcB)*pcB++ = curBitangents[idx];
  775. if (pcC0)*pcC0++ = curColors[idx];
  776. if (pcT0)*pcT0++ = curUVs[idx];
  777. if (pcT1)*pcT1++ = curUV2s[idx];
  778. if (++curIdx == 3)
  779. {
  780. ++curFace;
  781. curIdx = 0;
  782. }
  783. }
  784. if (curFace != faceEnd)
  785. DefaultLogger::get()->error("IRRMESH: Not enough indices");
  786. // Finish processing the mesh - do some small material workarounds
  787. if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors)
  788. {
  789. // Take the opacity value of the current material
  790. // from the common vertex color alpha
  791. MaterialHelper* mat = (MaterialHelper*)curMat;
  792. mat->AddProperty(&curColors[0].a,1,AI_MATKEY_OPACITY);
  793. }
  794. }}
  795. break;
  796. default:
  797. // GCC complains here ...
  798. break;
  799. };
  800. }
  801. // End of the last buffer. A material and a mesh should be there
  802. if (curMat || curMesh)
  803. {
  804. if ( !curMat || !curMesh)
  805. {
  806. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  807. delete curMat;
  808. delete curMesh;
  809. }
  810. else
  811. {
  812. materials.push_back(curMat);
  813. meshes.push_back(curMesh);
  814. }
  815. }
  816. if (materials.empty())
  817. throw new ImportErrorException("IRRMESH: Unable to read a mesh from this file");
  818. // now generate the output scene
  819. pScene->mNumMeshes = (unsigned int)meshes.size();
  820. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  821. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  822. {
  823. pScene->mMeshes[i] = meshes[i];
  824. // clean this value ...
  825. pScene->mMeshes[i]->mNumUVComponents[3] = 0;
  826. }
  827. pScene->mNumMaterials = (unsigned int)materials.size();
  828. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  829. ::memcpy(pScene->mMaterials,&materials[0],sizeof(void*)*pScene->mNumMaterials);
  830. pScene->mRootNode = new aiNode();
  831. pScene->mRootNode->mName.Set("<IRRMesh>");
  832. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  833. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  834. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  835. pScene->mRootNode->mMeshes[i] = i;
  836. delete reader;
  837. AI_DEBUG_INVALIDATE_PTR(reader);
  838. }