NFFLoader.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2016, assimp 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 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 STL importer class */
  35. #ifndef ASSIMP_BUILD_NO_NFF_IMPORTER
  36. // internal headers
  37. #include "NFFLoader.h"
  38. #include "ParsingUtils.h"
  39. #include "StandardShapes.h"
  40. #include "qnan.h"
  41. #include "fast_atof.h"
  42. #include "RemoveComments.h"
  43. #include <assimp/IOSystem.hpp>
  44. #include <assimp/DefaultLogger.hpp>
  45. #include <assimp/scene.h>
  46. #include <assimp/importerdesc.h>
  47. #include <memory>
  48. using namespace Assimp;
  49. static const aiImporterDesc desc = {
  50. "Neutral File Format Importer",
  51. "",
  52. "",
  53. "",
  54. aiImporterFlags_SupportBinaryFlavour,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. "enff nff"
  60. };
  61. // ------------------------------------------------------------------------------------------------
  62. // Constructor to be privately used by Importer
  63. NFFImporter::NFFImporter()
  64. {}
  65. // ------------------------------------------------------------------------------------------------
  66. // Destructor, private as well
  67. NFFImporter::~NFFImporter()
  68. {}
  69. // ------------------------------------------------------------------------------------------------
  70. // Returns whether the class can handle the format of the given file.
  71. bool NFFImporter::CanRead( const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*checkSig*/) const
  72. {
  73. return SimpleExtensionCheck(pFile,"nff","enff");
  74. }
  75. // ------------------------------------------------------------------------------------------------
  76. // Get the list of all supported file extensions
  77. const aiImporterDesc* NFFImporter::GetInfo () const
  78. {
  79. return &desc;
  80. }
  81. // ------------------------------------------------------------------------------------------------
  82. #define AI_NFF_PARSE_FLOAT(f) \
  83. SkipSpaces(&sz); \
  84. if (!::IsLineEnd(*sz))sz = fast_atoreal_move<float>(sz, (float&)f);
  85. // ------------------------------------------------------------------------------------------------
  86. #define AI_NFF_PARSE_TRIPLE(v) \
  87. AI_NFF_PARSE_FLOAT(v[0]) \
  88. AI_NFF_PARSE_FLOAT(v[1]) \
  89. AI_NFF_PARSE_FLOAT(v[2])
  90. // ------------------------------------------------------------------------------------------------
  91. #define AI_NFF_PARSE_SHAPE_INFORMATION() \
  92. aiVector3D center, radius(1.0f,get_qnan(),get_qnan()); \
  93. AI_NFF_PARSE_TRIPLE(center); \
  94. AI_NFF_PARSE_TRIPLE(radius); \
  95. if (is_qnan(radius.z))radius.z = radius.x; \
  96. if (is_qnan(radius.y))radius.y = radius.x; \
  97. currentMesh.radius = radius; \
  98. currentMesh.center = center;
  99. // ------------------------------------------------------------------------------------------------
  100. #define AI_NFF2_GET_NEXT_TOKEN() \
  101. do \
  102. { \
  103. if (!GetNextLine(buffer,line)) \
  104. {DefaultLogger::get()->warn("NFF2: Unexpected EOF, can't read next token");break;} \
  105. SkipSpaces(line,&sz); \
  106. } \
  107. while(IsLineEnd(*sz))
  108. // ------------------------------------------------------------------------------------------------
  109. // Loads the materail table for the NFF2 file format from an external file
  110. void NFFImporter::LoadNFF2MaterialTable(std::vector<ShadingInfo>& output,
  111. const std::string& path, IOSystem* pIOHandler)
  112. {
  113. std::unique_ptr<IOStream> file( pIOHandler->Open( path, "rb"));
  114. // Check whether we can read from the file
  115. if( !file.get()) {
  116. DefaultLogger::get()->error("NFF2: Unable to open material library " + path + ".");
  117. return;
  118. }
  119. // get the size of the file
  120. const unsigned int m = (unsigned int)file->FileSize();
  121. // allocate storage and copy the contents of the file to a memory buffer
  122. // (terminate it with zero)
  123. std::vector<char> mBuffer2(m+1);
  124. TextFileToBuffer(file.get(),mBuffer2);
  125. const char* buffer = &mBuffer2[0];
  126. // First of all: remove all comments from the file
  127. CommentRemover::RemoveLineComments("//",&mBuffer2[0]);
  128. // The file should start with the magic sequence "mat"
  129. if (!TokenMatch(buffer,"mat",3)) {
  130. DefaultLogger::get()->error("NFF2: Not a valid material library " + path + ".");
  131. return;
  132. }
  133. ShadingInfo* curShader = NULL;
  134. // No read the file line per line
  135. char line[4096];
  136. const char* sz;
  137. while (GetNextLine(buffer,line))
  138. {
  139. SkipSpaces(line,&sz);
  140. // 'version' defines the version of the file format
  141. if (TokenMatch(sz,"version",7))
  142. {
  143. DefaultLogger::get()->info("NFF (Sense8) material library file format: " + std::string(sz));
  144. }
  145. // 'matdef' starts a new material in the file
  146. else if (TokenMatch(sz,"matdef",6))
  147. {
  148. // add a new material to the list
  149. output.push_back( ShadingInfo() );
  150. curShader = & output.back();
  151. // parse the name of the material
  152. }
  153. else if (!TokenMatch(sz,"valid",5))
  154. {
  155. // check whether we have an active material at the moment
  156. if (!IsLineEnd(*sz))
  157. {
  158. if (!curShader)
  159. {
  160. DefaultLogger::get()->error(std::string("NFF2 material library: Found element ") +
  161. sz + "but there is no active material");
  162. continue;
  163. }
  164. }
  165. else continue;
  166. // now read the material property and determine its type
  167. aiColor3D c;
  168. if (TokenMatch(sz,"ambient",7))
  169. {
  170. AI_NFF_PARSE_TRIPLE(c);
  171. curShader->ambient = c;
  172. }
  173. else if (TokenMatch(sz,"diffuse",7) || TokenMatch(sz,"ambientdiffuse",14) /* correct? */)
  174. {
  175. AI_NFF_PARSE_TRIPLE(c);
  176. curShader->diffuse = curShader->ambient = c;
  177. }
  178. else if (TokenMatch(sz,"specular",8))
  179. {
  180. AI_NFF_PARSE_TRIPLE(c);
  181. curShader->specular = c;
  182. }
  183. else if (TokenMatch(sz,"emission",8))
  184. {
  185. AI_NFF_PARSE_TRIPLE(c);
  186. curShader->emissive = c;
  187. }
  188. else if (TokenMatch(sz,"shininess",9))
  189. {
  190. AI_NFF_PARSE_FLOAT(curShader->shininess);
  191. }
  192. else if (TokenMatch(sz,"opacity",7))
  193. {
  194. AI_NFF_PARSE_FLOAT(curShader->opacity);
  195. }
  196. }
  197. }
  198. }
  199. // ------------------------------------------------------------------------------------------------
  200. // Imports the given file into the given scene structure.
  201. void NFFImporter::InternReadFile( const std::string& pFile,
  202. aiScene* pScene, IOSystem* pIOHandler)
  203. {
  204. std::unique_ptr<IOStream> file( pIOHandler->Open( pFile, "rb"));
  205. // Check whether we can read from the file
  206. if( !file.get())
  207. throw DeadlyImportError( "Failed to open NFF file " + pFile + ".");
  208. unsigned int m = (unsigned int)file->FileSize();
  209. // allocate storage and copy the contents of the file to a memory buffer
  210. // (terminate it with zero)
  211. std::vector<char> mBuffer2;
  212. TextFileToBuffer(file.get(),mBuffer2);
  213. const char* buffer = &mBuffer2[0];
  214. // mesh arrays - separate here to make the handling of the pointers below easier.
  215. std::vector<MeshInfo> meshes;
  216. std::vector<MeshInfo> meshesWithNormals;
  217. std::vector<MeshInfo> meshesWithUVCoords;
  218. std::vector<MeshInfo> meshesLocked;
  219. char line[4096];
  220. const char* sz;
  221. // camera parameters
  222. aiVector3D camPos, camUp(0.f,1.f,0.f), camLookAt(0.f,0.f,1.f);
  223. float angle = 45.f;
  224. aiVector2D resolution;
  225. bool hasCam = false;
  226. MeshInfo* currentMeshWithNormals = NULL;
  227. MeshInfo* currentMesh = NULL;
  228. MeshInfo* currentMeshWithUVCoords = NULL;
  229. ShadingInfo s; // current material info
  230. // degree of tesselation
  231. unsigned int iTesselation = 4;
  232. // some temporary variables we need to parse the file
  233. unsigned int sphere = 0,
  234. cylinder = 0,
  235. cone = 0,
  236. numNamed = 0,
  237. dodecahedron = 0,
  238. octahedron = 0,
  239. tetrahedron = 0,
  240. hexahedron = 0;
  241. // lights imported from the file
  242. std::vector<Light> lights;
  243. // check whether this is the NFF2 file format
  244. if (TokenMatch(buffer,"nff",3))
  245. {
  246. const float qnan = get_qnan();
  247. const aiColor4D cQNAN = aiColor4D (qnan,0.f,0.f,1.f);
  248. const aiVector3D vQNAN = aiVector3D(qnan,0.f,0.f);
  249. // another NFF file format ... just a raw parser has been implemented
  250. // no support for further details, I don't think it is worth the effort
  251. // http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/nff/nff2.html
  252. // http://www.netghost.narod.ru/gff/graphics/summary/sense8.htm
  253. // First of all: remove all comments from the file
  254. CommentRemover::RemoveLineComments("//",&mBuffer2[0]);
  255. while (GetNextLine(buffer,line))
  256. {
  257. SkipSpaces(line,&sz);
  258. if (TokenMatch(sz,"version",7))
  259. {
  260. DefaultLogger::get()->info("NFF (Sense8) file format: " + std::string(sz));
  261. }
  262. else if (TokenMatch(sz,"viewpos",7))
  263. {
  264. AI_NFF_PARSE_TRIPLE(camPos);
  265. hasCam = true;
  266. }
  267. else if (TokenMatch(sz,"viewdir",7))
  268. {
  269. AI_NFF_PARSE_TRIPLE(camLookAt);
  270. hasCam = true;
  271. }
  272. // This starts a new object section
  273. else if (!IsSpaceOrNewLine(*sz))
  274. {
  275. unsigned int subMeshIdx = 0;
  276. // read the name of the object, skip all spaces
  277. // at the end of it.
  278. const char* sz3 = sz;
  279. while (!IsSpaceOrNewLine(*sz))++sz;
  280. std::string objectName = std::string(sz3,(unsigned int)(sz-sz3));
  281. const unsigned int objStart = (unsigned int)meshes.size();
  282. // There could be a material table in a separate file
  283. std::vector<ShadingInfo> materialTable;
  284. while (true)
  285. {
  286. AI_NFF2_GET_NEXT_TOKEN();
  287. // material table - an external file
  288. if (TokenMatch(sz,"mtable",6))
  289. {
  290. SkipSpaces(&sz);
  291. sz3 = sz;
  292. while (!IsSpaceOrNewLine(*sz))++sz;
  293. const unsigned int diff = (unsigned int)(sz-sz3);
  294. if (!diff)DefaultLogger::get()->warn("NFF2: Found empty mtable token");
  295. else
  296. {
  297. // The material table has the file extension .mat.
  298. // If it is not there, we need to append it
  299. std::string path = std::string(sz3,diff);
  300. if(std::string::npos == path.find_last_of(".mat"))
  301. {
  302. path.append(".mat");
  303. }
  304. // Now extract the working directory from the path to
  305. // this file and append the material library filename
  306. // to it.
  307. std::string::size_type s;
  308. if ((std::string::npos == (s = path.find_last_of('\\')) || !s) &&
  309. (std::string::npos == (s = path.find_last_of('/')) || !s) )
  310. {
  311. s = pFile.find_last_of('\\');
  312. if (std::string::npos == s)s = pFile.find_last_of('/');
  313. if (std::string::npos != s)
  314. {
  315. path = pFile.substr(0,s+1) + path;
  316. }
  317. }
  318. LoadNFF2MaterialTable(materialTable,path,pIOHandler);
  319. }
  320. }
  321. else break;
  322. }
  323. // read the numbr of vertices
  324. unsigned int num = ::strtoul10(sz,&sz);
  325. // temporary storage
  326. std::vector<aiColor4D> tempColors;
  327. std::vector<aiVector3D> tempPositions,tempTextureCoords,tempNormals;
  328. bool hasNormals = false,hasUVs = false,hasColor = false;
  329. tempPositions.reserve (num);
  330. tempColors.reserve (num);
  331. tempNormals.reserve (num);
  332. tempTextureCoords.reserve (num);
  333. for (unsigned int i = 0; i < num; ++i)
  334. {
  335. AI_NFF2_GET_NEXT_TOKEN();
  336. aiVector3D v;
  337. AI_NFF_PARSE_TRIPLE(v);
  338. tempPositions.push_back(v);
  339. // parse all other attributes in the line
  340. while (true)
  341. {
  342. SkipSpaces(&sz);
  343. if (IsLineEnd(*sz))break;
  344. // color definition
  345. if (TokenMatch(sz,"0x",2))
  346. {
  347. hasColor = true;
  348. unsigned int numIdx = ::strtoul16(sz,&sz);
  349. aiColor4D clr;
  350. clr.a = 1.f;
  351. // 0xRRGGBB
  352. clr.r = ((numIdx >> 16u) & 0xff) / 255.f;
  353. clr.g = ((numIdx >> 8u) & 0xff) / 255.f;
  354. clr.b = ((numIdx) & 0xff) / 255.f;
  355. tempColors.push_back(clr);
  356. }
  357. // normal vector
  358. else if (TokenMatch(sz,"norm",4))
  359. {
  360. hasNormals = true;
  361. AI_NFF_PARSE_TRIPLE(v);
  362. tempNormals.push_back(v);
  363. }
  364. // UV coordinate
  365. else if (TokenMatch(sz,"uv",2))
  366. {
  367. hasUVs = true;
  368. AI_NFF_PARSE_FLOAT(v.x);
  369. AI_NFF_PARSE_FLOAT(v.y);
  370. v.z = 0.f;
  371. tempTextureCoords.push_back(v);
  372. }
  373. }
  374. // fill in dummies for all attributes that have not been set
  375. if (tempNormals.size() != tempPositions.size())
  376. tempNormals.push_back(vQNAN);
  377. if (tempTextureCoords.size() != tempPositions.size())
  378. tempTextureCoords.push_back(vQNAN);
  379. if (tempColors.size() != tempPositions.size())
  380. tempColors.push_back(cQNAN);
  381. }
  382. AI_NFF2_GET_NEXT_TOKEN();
  383. if (!num)throw DeadlyImportError("NFF2: There are zero vertices");
  384. num = ::strtoul10(sz,&sz);
  385. std::vector<unsigned int> tempIdx;
  386. tempIdx.reserve(10);
  387. for (unsigned int i = 0; i < num; ++i)
  388. {
  389. AI_NFF2_GET_NEXT_TOKEN();
  390. SkipSpaces(line,&sz);
  391. unsigned int numIdx = ::strtoul10(sz,&sz);
  392. // read all faces indices
  393. if (numIdx)
  394. {
  395. // mesh.faces.push_back(numIdx);
  396. // tempIdx.erase(tempIdx.begin(),tempIdx.end());
  397. tempIdx.resize(numIdx);
  398. for (unsigned int a = 0; a < numIdx;++a)
  399. {
  400. SkipSpaces(sz,&sz);
  401. m = ::strtoul10(sz,&sz);
  402. if (m >= (unsigned int)tempPositions.size())
  403. {
  404. DefaultLogger::get()->error("NFF2: Vertex index overflow");
  405. m= 0;
  406. }
  407. // mesh.vertices.push_back (tempPositions[idx]);
  408. tempIdx[a] = m;
  409. }
  410. }
  411. // build a temporary shader object for the face.
  412. ShadingInfo shader;
  413. unsigned int matIdx = 0;
  414. // white material color - we have vertex colors
  415. shader.color = aiColor3D(1.f,1.f,1.f);
  416. aiColor4D c = aiColor4D(1.f,1.f,1.f,1.f);
  417. while (true)
  418. {
  419. SkipSpaces(sz,&sz);
  420. if(IsLineEnd(*sz))break;
  421. // per-polygon colors
  422. if (TokenMatch(sz,"0x",2))
  423. {
  424. hasColor = true;
  425. const char* sz2 = sz;
  426. numIdx = ::strtoul16(sz,&sz);
  427. const unsigned int diff = (unsigned int)(sz-sz2);
  428. // 0xRRGGBB
  429. if (diff > 3)
  430. {
  431. c.r = ((numIdx >> 16u) & 0xff) / 255.f;
  432. c.g = ((numIdx >> 8u) & 0xff) / 255.f;
  433. c.b = ((numIdx) & 0xff) / 255.f;
  434. }
  435. // 0xRGB
  436. else
  437. {
  438. c.r = ((numIdx >> 8u) & 0xf) / 16.f;
  439. c.g = ((numIdx >> 4u) & 0xf) / 16.f;
  440. c.b = ((numIdx) & 0xf) / 16.f;
  441. }
  442. }
  443. // TODO - implement texture mapping here
  444. #if 0
  445. // mirror vertex texture coordinate?
  446. else if (TokenMatch(sz,"mirror",6))
  447. {
  448. }
  449. // texture coordinate scaling
  450. else if (TokenMatch(sz,"scale",5))
  451. {
  452. }
  453. // texture coordinate translation
  454. else if (TokenMatch(sz,"trans",5))
  455. {
  456. }
  457. // texture coordinate rotation angle
  458. else if (TokenMatch(sz,"rot",3))
  459. {
  460. }
  461. #endif
  462. // texture file name for this polygon + mapping information
  463. else if ('_' == sz[0])
  464. {
  465. // get mapping information
  466. switch (sz[1])
  467. {
  468. case 'v':
  469. case 'V':
  470. shader.shaded = false;
  471. break;
  472. case 't':
  473. case 'T':
  474. case 'u':
  475. case 'U':
  476. DefaultLogger::get()->warn("Unsupported NFF2 texture attribute: trans");
  477. };
  478. if (!sz[1] || '_' != sz[2])
  479. {
  480. DefaultLogger::get()->warn("NFF2: Expected underscore after texture attributes");
  481. continue;
  482. }
  483. const char* sz2 = sz+3;
  484. while (!IsSpaceOrNewLine( *sz ))++sz;
  485. const unsigned int diff = (unsigned int)(sz-sz2);
  486. if (diff)shader.texFile = std::string(sz2,diff);
  487. }
  488. // Two-sided material?
  489. else if (TokenMatch(sz,"both",4))
  490. {
  491. shader.twoSided = true;
  492. }
  493. // Material ID?
  494. else if (!materialTable.empty() && TokenMatch(sz,"matid",5))
  495. {
  496. SkipSpaces(&sz);
  497. matIdx = ::strtoul10(sz,&sz);
  498. if (matIdx >= materialTable.size())
  499. {
  500. DefaultLogger::get()->error("NFF2: Material index overflow.");
  501. matIdx = 0;
  502. }
  503. // now combine our current shader with the shader we
  504. // read from the material table.
  505. ShadingInfo& mat = materialTable[matIdx];
  506. shader.ambient = mat.ambient;
  507. shader.diffuse = mat.diffuse;
  508. shader.emissive = mat.emissive;
  509. shader.opacity = mat.opacity;
  510. shader.specular = mat.specular;
  511. shader.shininess = mat.shininess;
  512. }
  513. else SkipToken(sz);
  514. }
  515. // search the list of all shaders we have for this object whether
  516. // there is an identical one. In this case, we append our mesh
  517. // data to it.
  518. MeshInfo* mesh = NULL;
  519. for (std::vector<MeshInfo>::iterator it = meshes.begin() + objStart, end = meshes.end();
  520. it != end; ++it)
  521. {
  522. if ((*it).shader == shader && (*it).matIndex == matIdx)
  523. {
  524. // we have one, we can append our data to it
  525. mesh = &(*it);
  526. }
  527. }
  528. if (!mesh)
  529. {
  530. meshes.push_back(MeshInfo(PatchType_Simple,false));
  531. mesh = &meshes.back();
  532. mesh->matIndex = matIdx;
  533. // We need to add a new mesh to the list. We assign
  534. // an unique name to it to make sure the scene will
  535. // pass the validation step for the moment.
  536. // TODO: fix naming of objects in the scenegraph later
  537. if (objectName.length())
  538. {
  539. ::strcpy(mesh->name,objectName.c_str());
  540. ASSIMP_itoa10(&mesh->name[objectName.length()],30,subMeshIdx++);
  541. }
  542. // copy the shader to the mesh.
  543. mesh->shader = shader;
  544. }
  545. // fill the mesh with data
  546. if (!tempIdx.empty())
  547. {
  548. mesh->faces.push_back((unsigned int)tempIdx.size());
  549. for (std::vector<unsigned int>::const_iterator it = tempIdx.begin(), end = tempIdx.end();
  550. it != end;++it)
  551. {
  552. m = *it;
  553. // copy colors -vertex color specifications override polygon color specifications
  554. if (hasColor)
  555. {
  556. const aiColor4D& clr = tempColors[m];
  557. mesh->colors.push_back((is_qnan( clr.r ) ? c : clr));
  558. }
  559. // positions should always be there
  560. mesh->vertices.push_back (tempPositions[m]);
  561. // copy normal vectors
  562. if (hasNormals)
  563. mesh->normals.push_back (tempNormals[m]);
  564. // copy texture coordinates
  565. if (hasUVs)
  566. mesh->uvs.push_back (tempTextureCoords[m]);
  567. }
  568. }
  569. }
  570. if (!num)throw DeadlyImportError("NFF2: There are zero faces");
  571. }
  572. }
  573. camLookAt = camLookAt + camPos;
  574. }
  575. else // "Normal" Neutral file format that is quite more common
  576. {
  577. while (GetNextLine(buffer,line))
  578. {
  579. sz = line;
  580. if ('p' == line[0] || TokenMatch(sz,"tpp",3))
  581. {
  582. MeshInfo* out = NULL;
  583. // 'tpp' - texture polygon patch primitive
  584. if ('t' == line[0])
  585. {
  586. currentMeshWithUVCoords = NULL;
  587. for (auto &mesh : meshesWithUVCoords)
  588. {
  589. if (mesh.shader == s)
  590. {
  591. currentMeshWithUVCoords = &mesh;
  592. break;
  593. }
  594. }
  595. if (!currentMeshWithUVCoords)
  596. {
  597. meshesWithUVCoords.push_back(MeshInfo(PatchType_UVAndNormals));
  598. currentMeshWithUVCoords = &meshesWithUVCoords.back();
  599. currentMeshWithUVCoords->shader = s;
  600. }
  601. out = currentMeshWithUVCoords;
  602. }
  603. // 'pp' - polygon patch primitive
  604. else if ('p' == line[1])
  605. {
  606. currentMeshWithNormals = NULL;
  607. for (auto &mesh : meshesWithNormals)
  608. {
  609. if (mesh.shader == s)
  610. {
  611. currentMeshWithNormals = &mesh;
  612. break;
  613. }
  614. }
  615. if (!currentMeshWithNormals)
  616. {
  617. meshesWithNormals.push_back(MeshInfo(PatchType_Normals));
  618. currentMeshWithNormals = &meshesWithNormals.back();
  619. currentMeshWithNormals->shader = s;
  620. }
  621. sz = &line[2];out = currentMeshWithNormals;
  622. }
  623. // 'p' - polygon primitive
  624. else
  625. {
  626. currentMesh = NULL;
  627. for (auto &mesh : meshes)
  628. {
  629. if (mesh.shader == s)
  630. {
  631. currentMesh = &mesh;
  632. break;
  633. }
  634. }
  635. if (!currentMesh)
  636. {
  637. meshes.push_back(MeshInfo(PatchType_Simple));
  638. currentMesh = &meshes.back();
  639. currentMesh->shader = s;
  640. }
  641. sz = &line[1];out = currentMesh;
  642. }
  643. SkipSpaces(sz,&sz);
  644. m = strtoul10(sz);
  645. // ---- flip the face order
  646. out->vertices.resize(out->vertices.size()+m);
  647. if (out != currentMesh)
  648. {
  649. out->normals.resize(out->vertices.size());
  650. }
  651. if (out == currentMeshWithUVCoords)
  652. {
  653. out->uvs.resize(out->vertices.size());
  654. }
  655. for (unsigned int n = 0; n < m;++n)
  656. {
  657. if(!GetNextLine(buffer,line))
  658. {
  659. DefaultLogger::get()->error("NFF: Unexpected EOF was encountered. Patch definition incomplete");
  660. continue;
  661. }
  662. aiVector3D v; sz = &line[0];
  663. AI_NFF_PARSE_TRIPLE(v);
  664. out->vertices[out->vertices.size()-n-1] = v;
  665. if (out != currentMesh)
  666. {
  667. AI_NFF_PARSE_TRIPLE(v);
  668. out->normals[out->vertices.size()-n-1] = v;
  669. }
  670. if (out == currentMeshWithUVCoords)
  671. {
  672. // FIX: in one test file this wraps over multiple lines
  673. SkipSpaces(&sz);
  674. if (IsLineEnd(*sz))
  675. {
  676. GetNextLine(buffer,line);
  677. sz = line;
  678. }
  679. AI_NFF_PARSE_FLOAT(v.x);
  680. SkipSpaces(&sz);
  681. if (IsLineEnd(*sz))
  682. {
  683. GetNextLine(buffer,line);
  684. sz = line;
  685. }
  686. AI_NFF_PARSE_FLOAT(v.y);
  687. v.y = 1.f - v.y;
  688. out->uvs[out->vertices.size()-n-1] = v;
  689. }
  690. }
  691. out->faces.push_back(m);
  692. }
  693. // 'f' - shading information block
  694. else if (TokenMatch(sz,"f",1))
  695. {
  696. float d;
  697. // read the RGB colors
  698. AI_NFF_PARSE_TRIPLE(s.color);
  699. // read the other properties
  700. AI_NFF_PARSE_FLOAT(s.diffuse.r);
  701. AI_NFF_PARSE_FLOAT(s.specular.r);
  702. AI_NFF_PARSE_FLOAT(d); // skip shininess and transmittance
  703. AI_NFF_PARSE_FLOAT(d);
  704. AI_NFF_PARSE_FLOAT(s.refracti);
  705. // NFF2 uses full colors here so we need to use them too
  706. // although NFF uses simple scaling factors
  707. s.diffuse.g = s.diffuse.b = s.diffuse.r;
  708. s.specular.g = s.specular.b = s.specular.r;
  709. // if the next one is NOT a number we assume it is a texture file name
  710. // this feature is used by some NFF files on the internet and it has
  711. // been implemented as it can be really useful
  712. SkipSpaces(&sz);
  713. if (!IsNumeric(*sz))
  714. {
  715. // TODO: Support full file names with spaces and quotation marks ...
  716. const char* p = sz;
  717. while (!IsSpaceOrNewLine( *sz ))++sz;
  718. unsigned int diff = (unsigned int)(sz-p);
  719. if (diff)
  720. {
  721. s.texFile = std::string(p,diff);
  722. }
  723. }
  724. else
  725. {
  726. AI_NFF_PARSE_FLOAT(s.ambient); // optional
  727. }
  728. }
  729. // 'shader' - other way to specify a texture
  730. else if (TokenMatch(sz,"shader",6))
  731. {
  732. SkipSpaces(&sz);
  733. const char* old = sz;
  734. while (!IsSpaceOrNewLine(*sz))++sz;
  735. s.texFile = std::string(old, (uintptr_t)sz - (uintptr_t)old);
  736. }
  737. // 'l' - light source
  738. else if (TokenMatch(sz,"l",1))
  739. {
  740. lights.push_back(Light());
  741. Light& light = lights.back();
  742. AI_NFF_PARSE_TRIPLE(light.position);
  743. AI_NFF_PARSE_FLOAT (light.intensity);
  744. AI_NFF_PARSE_TRIPLE(light.color);
  745. }
  746. // 's' - sphere
  747. else if (TokenMatch(sz,"s",1))
  748. {
  749. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  750. MeshInfo& currentMesh = meshesLocked.back();
  751. currentMesh.shader = s;
  752. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  753. AI_NFF_PARSE_SHAPE_INFORMATION();
  754. // we don't need scaling or translation here - we do it in the node's transform
  755. StandardShapes::MakeSphere(iTesselation, currentMesh.vertices);
  756. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  757. // generate a name for the mesh
  758. ::ai_snprintf(currentMesh.name,128,"sphere_%i",sphere++);
  759. }
  760. // 'dod' - dodecahedron
  761. else if (TokenMatch(sz,"dod",3))
  762. {
  763. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  764. MeshInfo& currentMesh = meshesLocked.back();
  765. currentMesh.shader = s;
  766. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  767. AI_NFF_PARSE_SHAPE_INFORMATION();
  768. // we don't need scaling or translation here - we do it in the node's transform
  769. StandardShapes::MakeDodecahedron(currentMesh.vertices);
  770. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  771. // generate a name for the mesh
  772. ::ai_snprintf(currentMesh.name,128,"dodecahedron_%i",dodecahedron++);
  773. }
  774. // 'oct' - octahedron
  775. else if (TokenMatch(sz,"oct",3))
  776. {
  777. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  778. MeshInfo& currentMesh = meshesLocked.back();
  779. currentMesh.shader = s;
  780. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  781. AI_NFF_PARSE_SHAPE_INFORMATION();
  782. // we don't need scaling or translation here - we do it in the node's transform
  783. StandardShapes::MakeOctahedron(currentMesh.vertices);
  784. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  785. // generate a name for the mesh
  786. ::ai_snprintf(currentMesh.name,128,"octahedron_%i",octahedron++);
  787. }
  788. // 'tet' - tetrahedron
  789. else if (TokenMatch(sz,"tet",3))
  790. {
  791. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  792. MeshInfo& currentMesh = meshesLocked.back();
  793. currentMesh.shader = s;
  794. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  795. AI_NFF_PARSE_SHAPE_INFORMATION();
  796. // we don't need scaling or translation here - we do it in the node's transform
  797. StandardShapes::MakeTetrahedron(currentMesh.vertices);
  798. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  799. // generate a name for the mesh
  800. ::ai_snprintf(currentMesh.name,128,"tetrahedron_%i",tetrahedron++);
  801. }
  802. // 'hex' - hexahedron
  803. else if (TokenMatch(sz,"hex",3))
  804. {
  805. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  806. MeshInfo& currentMesh = meshesLocked.back();
  807. currentMesh.shader = s;
  808. currentMesh.shader.mapping = aiTextureMapping_BOX;
  809. AI_NFF_PARSE_SHAPE_INFORMATION();
  810. // we don't need scaling or translation here - we do it in the node's transform
  811. StandardShapes::MakeHexahedron(currentMesh.vertices);
  812. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  813. // generate a name for the mesh
  814. ::ai_snprintf(currentMesh.name,128,"hexahedron_%i",hexahedron++);
  815. }
  816. // 'c' - cone
  817. else if (TokenMatch(sz,"c",1))
  818. {
  819. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  820. MeshInfo& currentMesh = meshesLocked.back();
  821. currentMesh.shader = s;
  822. currentMesh.shader.mapping = aiTextureMapping_CYLINDER;
  823. if(!GetNextLine(buffer,line))
  824. {
  825. DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)");
  826. break;
  827. }
  828. sz = line;
  829. // read the two center points and the respective radii
  830. aiVector3D center1, center2; float radius1, radius2;
  831. AI_NFF_PARSE_TRIPLE(center1);
  832. AI_NFF_PARSE_FLOAT(radius1);
  833. if(!GetNextLine(buffer,line))
  834. {
  835. DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)");
  836. break;
  837. }
  838. sz = line;
  839. AI_NFF_PARSE_TRIPLE(center2);
  840. AI_NFF_PARSE_FLOAT(radius2);
  841. // compute the center point of the cone/cylinder -
  842. // it is its local transformation origin
  843. currentMesh.dir = center2-center1;
  844. currentMesh.center = center1+currentMesh.dir/(ai_real)2.0;
  845. float f;
  846. if (( f = currentMesh.dir.Length()) < 10e-3f )
  847. {
  848. DefaultLogger::get()->error("NFF: Cone height is close to zero");
  849. continue;
  850. }
  851. currentMesh.dir /= f; // normalize
  852. // generate the cone - it consists of simple triangles
  853. StandardShapes::MakeCone(f, radius1, radius2,
  854. integer_pow(4, iTesselation), currentMesh.vertices);
  855. // MakeCone() returns tris
  856. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  857. // generate a name for the mesh. 'cone' if it a cone,
  858. // 'cylinder' if it is a cylinder. Funny, isn't it?
  859. if (radius1 != radius2)
  860. ::ai_snprintf(currentMesh.name,128,"cone_%i",cone++);
  861. else ::ai_snprintf(currentMesh.name,128,"cylinder_%i",cylinder++);
  862. }
  863. // 'tess' - tesselation
  864. else if (TokenMatch(sz,"tess",4))
  865. {
  866. SkipSpaces(&sz);
  867. iTesselation = strtoul10(sz);
  868. }
  869. // 'from' - camera position
  870. else if (TokenMatch(sz,"from",4))
  871. {
  872. AI_NFF_PARSE_TRIPLE(camPos);
  873. hasCam = true;
  874. }
  875. // 'at' - camera look-at vector
  876. else if (TokenMatch(sz,"at",2))
  877. {
  878. AI_NFF_PARSE_TRIPLE(camLookAt);
  879. hasCam = true;
  880. }
  881. // 'up' - camera up vector
  882. else if (TokenMatch(sz,"up",2))
  883. {
  884. AI_NFF_PARSE_TRIPLE(camUp);
  885. hasCam = true;
  886. }
  887. // 'angle' - (half?) camera field of view
  888. else if (TokenMatch(sz,"angle",5))
  889. {
  890. AI_NFF_PARSE_FLOAT(angle);
  891. hasCam = true;
  892. }
  893. // 'resolution' - used to compute the screen aspect
  894. else if (TokenMatch(sz,"resolution",10))
  895. {
  896. AI_NFF_PARSE_FLOAT(resolution.x);
  897. AI_NFF_PARSE_FLOAT(resolution.y);
  898. hasCam = true;
  899. }
  900. // 'pb' - bezier patch. Not supported yet
  901. else if (TokenMatch(sz,"pb",2))
  902. {
  903. DefaultLogger::get()->error("NFF: Encountered unsupported ID: bezier patch");
  904. }
  905. // 'pn' - NURBS. Not supported yet
  906. else if (TokenMatch(sz,"pn",2) || TokenMatch(sz,"pnn",3))
  907. {
  908. DefaultLogger::get()->error("NFF: Encountered unsupported ID: NURBS");
  909. }
  910. // '' - comment
  911. else if ('#' == line[0])
  912. {
  913. const char* sz;SkipSpaces(&line[1],&sz);
  914. if (!IsLineEnd(*sz))DefaultLogger::get()->info(sz);
  915. }
  916. }
  917. }
  918. // copy all arrays into one large
  919. meshes.reserve (meshes.size()+meshesLocked.size()+meshesWithNormals.size()+meshesWithUVCoords.size());
  920. meshes.insert (meshes.end(),meshesLocked.begin(),meshesLocked.end());
  921. meshes.insert (meshes.end(),meshesWithNormals.begin(),meshesWithNormals.end());
  922. meshes.insert (meshes.end(),meshesWithUVCoords.begin(),meshesWithUVCoords.end());
  923. // now generate output meshes. first find out how many meshes we'll need
  924. std::vector<MeshInfo>::const_iterator it = meshes.begin(), end = meshes.end();
  925. for (;it != end;++it)
  926. {
  927. if (!(*it).faces.empty())
  928. {
  929. ++pScene->mNumMeshes;
  930. if ((*it).name[0])++numNamed;
  931. }
  932. }
  933. // generate a dummy root node - assign all unnamed elements such
  934. // as polygons and polygon patches to the root node and generate
  935. // sub nodes for named objects such as spheres and cones.
  936. aiNode* const root = new aiNode();
  937. root->mName.Set("<NFF_Root>");
  938. root->mNumChildren = numNamed + (hasCam ? 1 : 0) + (unsigned int) lights.size();
  939. root->mNumMeshes = pScene->mNumMeshes-numNamed;
  940. aiNode** ppcChildren = NULL;
  941. unsigned int* pMeshes = NULL;
  942. if (root->mNumMeshes)
  943. pMeshes = root->mMeshes = new unsigned int[root->mNumMeshes];
  944. if (root->mNumChildren)
  945. ppcChildren = root->mChildren = new aiNode*[root->mNumChildren];
  946. // generate the camera
  947. if (hasCam)
  948. {
  949. aiNode* nd = *ppcChildren = new aiNode();
  950. nd->mName.Set("<NFF_Camera>");
  951. nd->mParent = root;
  952. // allocate the camera in the scene
  953. pScene->mNumCameras = 1;
  954. pScene->mCameras = new aiCamera*[1];
  955. aiCamera* c = pScene->mCameras[0] = new aiCamera;
  956. c->mName = nd->mName; // make sure the names are identical
  957. c->mHorizontalFOV = AI_DEG_TO_RAD( angle );
  958. c->mLookAt = camLookAt - camPos;
  959. c->mPosition = camPos;
  960. c->mUp = camUp;
  961. // If the resolution is not specified in the file, we
  962. // need to set 1.0 as aspect.
  963. c->mAspect = (!resolution.y ? 0.f : resolution.x / resolution.y);
  964. ++ppcChildren;
  965. }
  966. // generate light sources
  967. if (!lights.empty())
  968. {
  969. pScene->mNumLights = (unsigned int)lights.size();
  970. pScene->mLights = new aiLight*[pScene->mNumLights];
  971. for (unsigned int i = 0; i < pScene->mNumLights;++i,++ppcChildren)
  972. {
  973. const Light& l = lights[i];
  974. aiNode* nd = *ppcChildren = new aiNode();
  975. nd->mParent = root;
  976. nd->mName.length = ::ai_snprintf(nd->mName.data,1024,"<NFF_Light%u>",i);
  977. // allocate the light in the scene data structure
  978. aiLight* out = pScene->mLights[i] = new aiLight();
  979. out->mName = nd->mName; // make sure the names are identical
  980. out->mType = aiLightSource_POINT;
  981. out->mColorDiffuse = out->mColorSpecular = l.color * l.intensity;
  982. out->mPosition = l.position;
  983. }
  984. }
  985. if (!pScene->mNumMeshes)throw DeadlyImportError("NFF: No meshes loaded");
  986. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  987. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = pScene->mNumMeshes];
  988. for (it = meshes.begin(), m = 0; it != end;++it)
  989. {
  990. if ((*it).faces.empty())continue;
  991. const MeshInfo& src = *it;
  992. aiMesh* const mesh = pScene->mMeshes[m] = new aiMesh();
  993. mesh->mNumVertices = (unsigned int)src.vertices.size();
  994. mesh->mNumFaces = (unsigned int)src.faces.size();
  995. // Generate sub nodes for named meshes
  996. if ( src.name[ 0 ] && NULL != ppcChildren ) {
  997. aiNode* const node = *ppcChildren = new aiNode();
  998. node->mParent = root;
  999. node->mNumMeshes = 1;
  1000. node->mMeshes = new unsigned int[1];
  1001. node->mMeshes[0] = m;
  1002. node->mName.Set(src.name);
  1003. // setup the transformation matrix of the node
  1004. aiMatrix4x4::FromToMatrix(aiVector3D(0.f,1.f,0.f),
  1005. src.dir,node->mTransformation);
  1006. aiMatrix4x4& mat = node->mTransformation;
  1007. mat.a1 *= src.radius.x; mat.b1 *= src.radius.x; mat.c1 *= src.radius.x;
  1008. mat.a2 *= src.radius.y; mat.b2 *= src.radius.y; mat.c2 *= src.radius.y;
  1009. mat.a3 *= src.radius.z; mat.b3 *= src.radius.z; mat.c3 *= src.radius.z;
  1010. mat.a4 = src.center.x;
  1011. mat.b4 = src.center.y;
  1012. mat.c4 = src.center.z;
  1013. ++ppcChildren;
  1014. } else {
  1015. *pMeshes++ = m;
  1016. }
  1017. // copy vertex positions
  1018. mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  1019. ::memcpy(mesh->mVertices,&src.vertices[0],
  1020. sizeof(aiVector3D)*mesh->mNumVertices);
  1021. // NFF2: there could be vertex colors
  1022. if (!src.colors.empty())
  1023. {
  1024. ai_assert(src.colors.size() == src.vertices.size());
  1025. // copy vertex colors
  1026. mesh->mColors[0] = new aiColor4D[mesh->mNumVertices];
  1027. ::memcpy(mesh->mColors[0],&src.colors[0],
  1028. sizeof(aiColor4D)*mesh->mNumVertices);
  1029. }
  1030. if (!src.normals.empty())
  1031. {
  1032. ai_assert(src.normals.size() == src.vertices.size());
  1033. // copy normal vectors
  1034. mesh->mNormals = new aiVector3D[mesh->mNumVertices];
  1035. ::memcpy(mesh->mNormals,&src.normals[0],
  1036. sizeof(aiVector3D)*mesh->mNumVertices);
  1037. }
  1038. if (!src.uvs.empty())
  1039. {
  1040. ai_assert(src.uvs.size() == src.vertices.size());
  1041. // copy texture coordinates
  1042. mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  1043. ::memcpy(mesh->mTextureCoords[0],&src.uvs[0],
  1044. sizeof(aiVector3D)*mesh->mNumVertices);
  1045. }
  1046. // generate faces
  1047. unsigned int p = 0;
  1048. aiFace* pFace = mesh->mFaces = new aiFace[mesh->mNumFaces];
  1049. for (std::vector<unsigned int>::const_iterator it2 = src.faces.begin(),
  1050. end2 = src.faces.end();
  1051. it2 != end2;++it2,++pFace)
  1052. {
  1053. pFace->mIndices = new unsigned int [ pFace->mNumIndices = *it2 ];
  1054. for (unsigned int o = 0; o < pFace->mNumIndices;++o)
  1055. pFace->mIndices[o] = p++;
  1056. }
  1057. // generate a material for the mesh
  1058. aiMaterial* pcMat = (aiMaterial*)(pScene->mMaterials[m] = new aiMaterial());
  1059. mesh->mMaterialIndex = m++;
  1060. aiString s;
  1061. s.Set(AI_DEFAULT_MATERIAL_NAME);
  1062. pcMat->AddProperty(&s, AI_MATKEY_NAME);
  1063. // FIX: Ignore diffuse == 0
  1064. aiColor3D c = src.shader.color * (src.shader.diffuse.r ? src.shader.diffuse : aiColor3D(1.f,1.f,1.f));
  1065. pcMat->AddProperty(&c,1,AI_MATKEY_COLOR_DIFFUSE);
  1066. c = src.shader.color * src.shader.specular;
  1067. pcMat->AddProperty(&c,1,AI_MATKEY_COLOR_SPECULAR);
  1068. // NFF2 - default values for NFF
  1069. pcMat->AddProperty(&src.shader.ambient, 1,AI_MATKEY_COLOR_AMBIENT);
  1070. pcMat->AddProperty(&src.shader.emissive,1,AI_MATKEY_COLOR_EMISSIVE);
  1071. pcMat->AddProperty(&src.shader.opacity, 1,AI_MATKEY_OPACITY);
  1072. // setup the first texture layer, if existing
  1073. if (src.shader.texFile.length())
  1074. {
  1075. s.Set(src.shader.texFile);
  1076. pcMat->AddProperty(&s,AI_MATKEY_TEXTURE_DIFFUSE(0));
  1077. if (aiTextureMapping_UV != src.shader.mapping) {
  1078. aiVector3D v(0.f,-1.f,0.f);
  1079. pcMat->AddProperty(&v, 1,AI_MATKEY_TEXMAP_AXIS_DIFFUSE(0));
  1080. pcMat->AddProperty((int*)&src.shader.mapping, 1,AI_MATKEY_MAPPING_DIFFUSE(0));
  1081. }
  1082. }
  1083. // setup the name of the material
  1084. if (src.shader.name.length())
  1085. {
  1086. s.Set(src.shader.texFile);
  1087. pcMat->AddProperty(&s,AI_MATKEY_NAME);
  1088. }
  1089. // setup some more material properties that are specific to NFF2
  1090. int i;
  1091. if (src.shader.twoSided)
  1092. {
  1093. i = 1;
  1094. pcMat->AddProperty(&i,1,AI_MATKEY_TWOSIDED);
  1095. }
  1096. i = (src.shader.shaded ? aiShadingMode_Gouraud : aiShadingMode_NoShading);
  1097. if (src.shader.shininess)
  1098. {
  1099. i = aiShadingMode_Phong;
  1100. pcMat->AddProperty(&src.shader.shininess,1,AI_MATKEY_SHININESS);
  1101. }
  1102. pcMat->AddProperty(&i,1,AI_MATKEY_SHADING_MODEL);
  1103. }
  1104. pScene->mRootNode = root;
  1105. }
  1106. #endif // !! ASSIMP_BUILD_NO_NFF_IMPORTER