IRRMeshLoader.cpp 28 KB

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