IRRMeshLoader.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. static const char* PropArray[4] =
  401. {
  402. AI_MATKEY_TEXBLEND_DIFFUSE(0),
  403. AI_MATKEY_TEXBLEND_DIFFUSE(1),
  404. AI_MATKEY_TEXBLEND_DIFFUSE(2),
  405. AI_MATKEY_TEXBLEND_DIFFUSE(3)
  406. };
  407. static const char* PropArray2[4] =
  408. {
  409. AI_MATKEY_TEXOP_DIFFUSE(0),
  410. AI_MATKEY_TEXOP_DIFFUSE(1),
  411. AI_MATKEY_TEXOP_DIFFUSE(2),
  412. AI_MATKEY_TEXOP_DIFFUSE(3)
  413. };
  414. float f = 1.f;
  415. // Additive lightmap?
  416. int op = (matFlags & AI_IRRMESH_MAT_lightmap_add
  417. ? aiTextureOp_Add : aiTextureOp_Multiply);
  418. // Handle Irrlicht's lightmapping scaling factor
  419. if (matFlags & AI_IRRMESH_MAT_lightmap_m2 ||
  420. matFlags & AI_IRRMESH_MAT_lightmap_light_m2)
  421. {
  422. f = 2.f;
  423. }
  424. else if (matFlags & AI_IRRMESH_MAT_lightmap_m4 ||
  425. matFlags & AI_IRRMESH_MAT_lightmap_light_m4)
  426. {
  427. f = 4.f;
  428. }
  429. mat->AddProperty( &f, 1, PropArray [cnt-1]);
  430. mat->AddProperty( &op,1, PropArray2 [cnt-1]);
  431. }
  432. return mat;
  433. }
  434. default:
  435. // GCC complains here ...
  436. break;
  437. }
  438. }
  439. DefaultLogger::get()->error("IRRMESH: Unexpected end of file. Material is not complete");
  440. return mat;
  441. }
  442. // ------------------------------------------------------------------------------------------------
  443. // Constructor to be privately used by Importer
  444. IRRMeshImporter::IRRMeshImporter()
  445. {
  446. // nothing to do here
  447. }
  448. // ------------------------------------------------------------------------------------------------
  449. // Destructor, private as well
  450. IRRMeshImporter::~IRRMeshImporter()
  451. {
  452. // nothing to do here
  453. }
  454. // ------------------------------------------------------------------------------------------------
  455. // Returns whether the class can handle the format of the given file.
  456. bool IRRMeshImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler) const
  457. {
  458. /* NOTE: A simple check for the file extension is not enough
  459. * here. Irrmesh and irr are easy, but xml is too generic
  460. * and could be collada, too. So we need to open the file and
  461. * search for typical tokens.
  462. */
  463. std::string::size_type pos = pFile.find_last_of('.');
  464. // no file extension - can't read
  465. if( pos == std::string::npos)
  466. return false;
  467. std::string extension = pFile.substr( pos);
  468. for (std::string::iterator i = extension.begin(); i != extension.end();++i)
  469. *i = ::tolower(*i);
  470. if (extension == ".irrmesh")return true;
  471. else if (extension == ".xml")
  472. {
  473. /* If CanRead() is called to check whether the loader
  474. * supports a specific file extension in general we
  475. * must return true here.
  476. */
  477. if (!pIOHandler)return true;
  478. const char* tokens[] = {"irrmesh"};
  479. return SearchFileHeaderForToken(pIOHandler,pFile,tokens,1);
  480. }
  481. return false;
  482. }
  483. // ------------------------------------------------------------------------------------------------
  484. // Imports the given file into the given scene structure.
  485. void IRRMeshImporter::InternReadFile( const std::string& pFile,
  486. aiScene* pScene, IOSystem* pIOHandler)
  487. {
  488. boost::scoped_ptr<IOStream> file( pIOHandler->Open( pFile));
  489. // Check whether we can read from the file
  490. if( file.get() == NULL)
  491. throw new ImportErrorException( "Failed to open IRRMESH file " + pFile + "");
  492. // Construct the irrXML parser
  493. CIrrXML_IOStreamReader st(file.get());
  494. reader = createIrrXMLReader((IFileReadCallBack*) &st);
  495. // final data
  496. std::vector<aiMaterial*> materials;
  497. std::vector<aiMesh*> meshes;
  498. materials.reserve (5);
  499. meshes.reserve (5);
  500. // temporary data - current mesh buffer
  501. aiMaterial* curMat = NULL;
  502. aiMesh* curMesh = NULL;
  503. unsigned int curMatFlags;
  504. std::vector<aiVector3D> curVertices,curNormals,curTangents,curBitangents;
  505. std::vector<aiColor4D> curColors;
  506. std::vector<aiVector3D> curUVs,curUV2s;
  507. // some temporary variables
  508. int textMeaning = 0;
  509. int vertexFormat = 0; // 0 = normal; 1 = 2 tcoords, 2 = tangents
  510. bool useColors = false;
  511. bool needLightMap = false;
  512. // Parse the XML file
  513. while (reader->read())
  514. {
  515. switch (reader->getNodeType())
  516. {
  517. case EXN_ELEMENT:
  518. if (!ASSIMP_stricmp(reader->getNodeName(),"buffer") && (curMat || curMesh))
  519. {
  520. // end of previous buffer. A material and a mesh should be there
  521. if ( !curMat || !curMesh)
  522. {
  523. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  524. delete curMat;
  525. delete curMesh;
  526. }
  527. else
  528. {
  529. materials.push_back(curMat);
  530. meshes.push_back(curMesh);
  531. }
  532. curMat = NULL;
  533. curMesh = NULL;
  534. curVertices.clear();
  535. curColors.clear();
  536. curNormals.clear();
  537. curUV2s.clear();
  538. curUVs.clear();
  539. curTangents.clear();
  540. curBitangents.clear();
  541. }
  542. if (!ASSIMP_stricmp(reader->getNodeName(),"material"))
  543. {
  544. if (curMat)
  545. {
  546. DefaultLogger::get()->warn("IRRMESH: Only one material description per buffer, please");
  547. delete curMat;
  548. }
  549. curMat = ParseMaterial(curMatFlags);
  550. }
  551. /* no else here! */ if (!ASSIMP_stricmp(reader->getNodeName(),"vertices"))
  552. {
  553. int num = reader->getAttributeValueAsInt("vertexCount");
  554. curVertices.reserve (num);
  555. curNormals.reserve (num);
  556. curColors.reserve (num);
  557. curUVs.reserve (num);
  558. // Determine the file format
  559. const char* t = reader->getAttributeValueSafe("type");
  560. if (!ASSIMP_stricmp("2tcoords", t))
  561. {
  562. curUV2s.reserve (num);
  563. vertexFormat = 1;
  564. if (curMatFlags & AI_IRRMESH_EXTRA_2ND_TEXTURE)
  565. {
  566. // *********************************************************
  567. // We have a second texture! So use this UV channel
  568. // for it. The 2nd texture can be either a normal
  569. // texture (solid_2layer or lightmap_xxx) or a normal
  570. // map (normal_..., parallax_...)
  571. // *********************************************************
  572. int idx = 1;
  573. MaterialHelper* mat = ( MaterialHelper* ) curMat;
  574. if (curMatFlags & (AI_IRRMESH_MAT_solid_2layer | AI_IRRMESH_MAT_lightmap))
  575. {
  576. mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_DIFFUSE(0));
  577. }
  578. else if (curMatFlags & AI_IRRMESH_MAT_normalmap_solid)
  579. {
  580. mat->AddProperty(&idx,1,AI_MATKEY_UVWSRC_NORMALS(0));
  581. }
  582. }
  583. }
  584. else if (!ASSIMP_stricmp("tangents", t))
  585. {
  586. curTangents.reserve (num);
  587. curBitangents.reserve (num);
  588. vertexFormat = 2;
  589. }
  590. else if (ASSIMP_stricmp("standard", t))
  591. {
  592. DefaultLogger::get()->warn("IRRMESH: Unknown vertex format");
  593. }
  594. else vertexFormat = 0;
  595. textMeaning = 1;
  596. }
  597. else if (!ASSIMP_stricmp(reader->getNodeName(),"indices"))
  598. {
  599. if (curVertices.empty())
  600. throw new ImportErrorException("IRRMESH: indices must come after vertices");
  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 % 3)
  607. {
  608. DefaultLogger::get()->warn("IRRMESH: Number if indices isn't divisible by 3");
  609. }
  610. curMesh->mNumFaces = curMesh->mNumVertices / 3;
  611. curMesh->mFaces = new aiFace[curMesh->mNumFaces];
  612. // setup some members
  613. curMesh->mMaterialIndex = (unsigned int)materials.size();
  614. curMesh->mPrimitiveTypes = aiPrimitiveType_TRIANGLE;
  615. // allocate storage for all vertices
  616. curMesh->mVertices = new aiVector3D[curMesh->mNumVertices];
  617. if (curNormals.size() == curVertices.size())
  618. {
  619. curMesh->mNormals = new aiVector3D[curMesh->mNumVertices];
  620. }
  621. if (curTangents.size() == curVertices.size())
  622. {
  623. curMesh->mTangents = new aiVector3D[curMesh->mNumVertices];
  624. }
  625. if (curBitangents.size() == curVertices.size())
  626. {
  627. curMesh->mBitangents = new aiVector3D[curMesh->mNumVertices];
  628. }
  629. if (curColors.size() == curVertices.size() && useColors)
  630. {
  631. curMesh->mColors[0] = new aiColor4D[curMesh->mNumVertices];
  632. }
  633. if (curUVs.size() == curVertices.size())
  634. {
  635. curMesh->mTextureCoords[0] = new aiVector3D[curMesh->mNumVertices];
  636. }
  637. if (curUV2s.size() == curVertices.size())
  638. {
  639. curMesh->mTextureCoords[1] = new aiVector3D[curMesh->mNumVertices];
  640. }
  641. }
  642. break;
  643. case EXN_TEXT:
  644. {
  645. const char* sz = reader->getNodeData();
  646. if (textMeaning == 1)
  647. {
  648. textMeaning = 0;
  649. // read vertices
  650. do
  651. {
  652. SkipSpacesAndLineEnd(&sz);
  653. aiVector3D temp;aiColor4D c;
  654. // Read the vertex position
  655. sz = fast_atof_move(sz,(float&)temp.x);
  656. SkipSpaces(&sz);
  657. sz = fast_atof_move(sz,(float&)temp.z);
  658. SkipSpaces(&sz);
  659. sz = fast_atof_move(sz,(float&)temp.y);
  660. SkipSpaces(&sz);
  661. temp.y *= -1.0f;
  662. curVertices.push_back(temp);
  663. // Read the vertex normals
  664. sz = fast_atof_move(sz,(float&)temp.x);
  665. SkipSpaces(&sz);
  666. sz = fast_atof_move(sz,(float&)temp.z);
  667. SkipSpaces(&sz);
  668. sz = fast_atof_move(sz,(float&)temp.y);
  669. SkipSpaces(&sz);
  670. temp.y *= -1.0f;
  671. curNormals.push_back(temp);
  672. // read the vertex colors
  673. uint32_t clr = strtol16(sz,&sz);
  674. ColorFromARGBPacked(clr,c);
  675. if (!curColors.empty() && c != *(curColors.end()-1))
  676. useColors = true;
  677. curColors.push_back(c);
  678. SkipSpaces(&sz);
  679. // read the first UV coordinate set
  680. sz = fast_atof_move(sz,(float&)temp.x);
  681. SkipSpaces(&sz);
  682. sz = fast_atof_move(sz,(float&)temp.y);
  683. SkipSpaces(&sz);
  684. temp.z = 0.f;
  685. temp.y = 1.f - temp.y; // DX to OGL
  686. curUVs.push_back(temp);
  687. // read the (optional) second UV coordinate set
  688. if (vertexFormat == 1)
  689. {
  690. sz = fast_atof_move(sz,(float&)temp.x);
  691. SkipSpaces(&sz);
  692. sz = fast_atof_move(sz,(float&)temp.y);
  693. temp.y = 1.f - temp.y; // DX to OGL
  694. curUV2s.push_back(temp);
  695. }
  696. // read optional tangent and bitangent vectors
  697. else if (vertexFormat == 2)
  698. {
  699. // tangents
  700. sz = fast_atof_move(sz,(float&)temp.x);
  701. SkipSpaces(&sz);
  702. sz = fast_atof_move(sz,(float&)temp.z);
  703. SkipSpaces(&sz);
  704. sz = fast_atof_move(sz,(float&)temp.y);
  705. SkipSpaces(&sz);
  706. temp.y *= -1.0f;
  707. curTangents.push_back(temp);
  708. // bitangents
  709. sz = fast_atof_move(sz,(float&)temp.x);
  710. SkipSpaces(&sz);
  711. sz = fast_atof_move(sz,(float&)temp.z);
  712. SkipSpaces(&sz);
  713. sz = fast_atof_move(sz,(float&)temp.y);
  714. SkipSpaces(&sz);
  715. temp.y *= -1.0f;
  716. curBitangents.push_back(temp);
  717. }
  718. }
  719. /* IMPORTANT: We assume that each vertex is specified in one
  720. line. So we can skip the rest of the line - unknown vertex
  721. elements are ignored.
  722. */
  723. while (SkipLine(&sz));
  724. }
  725. else if (textMeaning == 2)
  726. {
  727. textMeaning = 0;
  728. // read indices
  729. aiFace* curFace = curMesh->mFaces;
  730. aiFace* const faceEnd = curMesh->mFaces + curMesh->mNumFaces;
  731. aiVector3D* pcV = curMesh->mVertices;
  732. aiVector3D* pcN = curMesh->mNormals;
  733. aiVector3D* pcT = curMesh->mTangents;
  734. aiVector3D* pcB = curMesh->mBitangents;
  735. aiColor4D* pcC0 = curMesh->mColors[0];
  736. aiVector3D* pcT0 = curMesh->mTextureCoords[0];
  737. aiVector3D* pcT1 = curMesh->mTextureCoords[1];
  738. unsigned int curIdx = 0;
  739. unsigned int total = 0;
  740. while(SkipSpacesAndLineEnd(&sz))
  741. {
  742. if (curFace >= faceEnd)
  743. {
  744. DefaultLogger::get()->error("IRRMESH: Too many indices");
  745. break;
  746. }
  747. if (!curIdx)
  748. {
  749. curFace->mNumIndices = 3;
  750. curFace->mIndices = new unsigned int[3];
  751. }
  752. unsigned int idx = strtol10(sz,&sz);
  753. if (idx >= curVertices.size())
  754. {
  755. DefaultLogger::get()->error("IRRMESH: Index out of range");
  756. idx = 0;
  757. }
  758. curFace->mIndices[curIdx] = total++;
  759. *pcV++ = curVertices[idx];
  760. if (pcN)*pcN++ = curNormals[idx];
  761. if (pcT)*pcT++ = curTangents[idx];
  762. if (pcB)*pcB++ = curBitangents[idx];
  763. if (pcC0)*pcC0++ = curColors[idx];
  764. if (pcT0)*pcT0++ = curUVs[idx];
  765. if (pcT1)*pcT1++ = curUV2s[idx];
  766. if (++curIdx == 3)
  767. {
  768. ++curFace;
  769. curIdx = 0;
  770. }
  771. }
  772. if (curFace != faceEnd)
  773. DefaultLogger::get()->error("IRRMESH: Not enough indices");
  774. // Finish processing the mesh - do some small material workarounds
  775. if (curMatFlags & AI_IRRMESH_MAT_trans_vertex_alpha && !useColors)
  776. {
  777. // Take the opacity value of the current material
  778. // from the common vertex color alpha
  779. MaterialHelper* mat = (MaterialHelper*)curMat;
  780. mat->AddProperty(&curColors[0].a,1,AI_MATKEY_OPACITY);
  781. }
  782. }}
  783. break;
  784. default:
  785. // GCC complains here ...
  786. break;
  787. };
  788. }
  789. // end of the last buffer. A material and a mesh should be there
  790. if (curMat || curMesh)
  791. {
  792. if ( !curMat || !curMesh)
  793. {
  794. DefaultLogger::get()->error("IRRMESH: A buffer must contain a mesh and a material");
  795. delete curMat;
  796. delete curMesh;
  797. }
  798. else
  799. {
  800. materials.push_back(curMat);
  801. meshes.push_back(curMesh);
  802. }
  803. }
  804. if (materials.empty())
  805. throw new ImportErrorException("IRRMESH: Unable to read a mesh from this file");
  806. // now generate the output scene
  807. pScene->mNumMeshes = (unsigned int)meshes.size();
  808. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  809. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  810. {
  811. pScene->mMeshes[i] = meshes[i];
  812. // clean this value ...
  813. pScene->mMeshes[i]->mNumUVComponents[3] = 0;
  814. }
  815. pScene->mNumMaterials = (unsigned int)materials.size();
  816. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
  817. ::memcpy(pScene->mMaterials,&materials[0],sizeof(void*)*pScene->mNumMaterials);
  818. pScene->mRootNode = new aiNode();
  819. pScene->mRootNode->mName.Set("<IRRMesh>");
  820. pScene->mRootNode->mNumMeshes = pScene->mNumMeshes;
  821. pScene->mRootNode->mMeshes = new unsigned int[pScene->mNumMeshes];
  822. for (unsigned int i = 0; i < pScene->mNumMeshes;++i)
  823. pScene->mRootNode->mMeshes[i] = i;
  824. delete reader;
  825. AI_DEBUG_INVALIDATE_PTR(reader);
  826. }