NFFLoader.cpp 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2017, 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. // allocate storage and copy the contents of the file to a memory buffer
  209. // (terminate it with zero)
  210. std::vector<char> mBuffer2;
  211. TextFileToBuffer(file.get(),mBuffer2);
  212. const char* buffer = &mBuffer2[0];
  213. // mesh arrays - separate here to make the handling of the pointers below easier.
  214. std::vector<MeshInfo> meshes;
  215. std::vector<MeshInfo> meshesWithNormals;
  216. std::vector<MeshInfo> meshesWithUVCoords;
  217. std::vector<MeshInfo> meshesLocked;
  218. char line[4096];
  219. const char* sz;
  220. // camera parameters
  221. aiVector3D camPos, camUp(0.f,1.f,0.f), camLookAt(0.f,0.f,1.f);
  222. float angle = 45.f;
  223. aiVector2D resolution;
  224. bool hasCam = false;
  225. MeshInfo* currentMeshWithNormals = NULL;
  226. MeshInfo* currentMesh = NULL;
  227. MeshInfo* currentMeshWithUVCoords = NULL;
  228. ShadingInfo s; // current material info
  229. // degree of tesselation
  230. unsigned int iTesselation = 4;
  231. // some temporary variables we need to parse the file
  232. unsigned int sphere = 0,
  233. cylinder = 0,
  234. cone = 0,
  235. numNamed = 0,
  236. dodecahedron = 0,
  237. octahedron = 0,
  238. tetrahedron = 0,
  239. hexahedron = 0;
  240. // lights imported from the file
  241. std::vector<Light> lights;
  242. // check whether this is the NFF2 file format
  243. if (TokenMatch(buffer,"nff",3))
  244. {
  245. const float qnan = get_qnan();
  246. const aiColor4D cQNAN = aiColor4D (qnan,0.f,0.f,1.f);
  247. const aiVector3D vQNAN = aiVector3D(qnan,0.f,0.f);
  248. // another NFF file format ... just a raw parser has been implemented
  249. // no support for further details, I don't think it is worth the effort
  250. // http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/nff/nff2.html
  251. // http://www.netghost.narod.ru/gff/graphics/summary/sense8.htm
  252. // First of all: remove all comments from the file
  253. CommentRemover::RemoveLineComments("//",&mBuffer2[0]);
  254. while (GetNextLine(buffer,line))
  255. {
  256. SkipSpaces(line,&sz);
  257. if (TokenMatch(sz,"version",7))
  258. {
  259. DefaultLogger::get()->info("NFF (Sense8) file format: " + std::string(sz));
  260. }
  261. else if (TokenMatch(sz,"viewpos",7))
  262. {
  263. AI_NFF_PARSE_TRIPLE(camPos);
  264. hasCam = true;
  265. }
  266. else if (TokenMatch(sz,"viewdir",7))
  267. {
  268. AI_NFF_PARSE_TRIPLE(camLookAt);
  269. hasCam = true;
  270. }
  271. // This starts a new object section
  272. else if (!IsSpaceOrNewLine(*sz))
  273. {
  274. unsigned int subMeshIdx = 0;
  275. // read the name of the object, skip all spaces
  276. // at the end of it.
  277. const char* sz3 = sz;
  278. while (!IsSpaceOrNewLine(*sz))++sz;
  279. std::string objectName = std::string(sz3,(unsigned int)(sz-sz3));
  280. const unsigned int objStart = (unsigned int)meshes.size();
  281. // There could be a material table in a separate file
  282. std::vector<ShadingInfo> materialTable;
  283. while (true)
  284. {
  285. AI_NFF2_GET_NEXT_TOKEN();
  286. // material table - an external file
  287. if (TokenMatch(sz,"mtable",6))
  288. {
  289. SkipSpaces(&sz);
  290. sz3 = sz;
  291. while (!IsSpaceOrNewLine(*sz))++sz;
  292. const unsigned int diff = (unsigned int)(sz-sz3);
  293. if (!diff)DefaultLogger::get()->warn("NFF2: Found empty mtable token");
  294. else
  295. {
  296. // The material table has the file extension .mat.
  297. // If it is not there, we need to append it
  298. std::string path = std::string(sz3,diff);
  299. if(std::string::npos == path.find_last_of(".mat"))
  300. {
  301. path.append(".mat");
  302. }
  303. // Now extract the working directory from the path to
  304. // this file and append the material library filename
  305. // to it.
  306. std::string::size_type s;
  307. if ((std::string::npos == (s = path.find_last_of('\\')) || !s) &&
  308. (std::string::npos == (s = path.find_last_of('/')) || !s) )
  309. {
  310. s = pFile.find_last_of('\\');
  311. if (std::string::npos == s)s = pFile.find_last_of('/');
  312. if (std::string::npos != s)
  313. {
  314. path = pFile.substr(0,s+1) + path;
  315. }
  316. }
  317. LoadNFF2MaterialTable(materialTable,path,pIOHandler);
  318. }
  319. }
  320. else break;
  321. }
  322. // read the numbr of vertices
  323. unsigned int num = ::strtoul10(sz,&sz);
  324. // temporary storage
  325. std::vector<aiColor4D> tempColors;
  326. std::vector<aiVector3D> tempPositions,tempTextureCoords,tempNormals;
  327. bool hasNormals = false,hasUVs = false,hasColor = false;
  328. tempPositions.reserve (num);
  329. tempColors.reserve (num);
  330. tempNormals.reserve (num);
  331. tempTextureCoords.reserve (num);
  332. for (unsigned int i = 0; i < num; ++i)
  333. {
  334. AI_NFF2_GET_NEXT_TOKEN();
  335. aiVector3D v;
  336. AI_NFF_PARSE_TRIPLE(v);
  337. tempPositions.push_back(v);
  338. // parse all other attributes in the line
  339. while (true)
  340. {
  341. SkipSpaces(&sz);
  342. if (IsLineEnd(*sz))break;
  343. // color definition
  344. if (TokenMatch(sz,"0x",2))
  345. {
  346. hasColor = true;
  347. unsigned int numIdx = ::strtoul16(sz,&sz);
  348. aiColor4D clr;
  349. clr.a = 1.f;
  350. // 0xRRGGBB
  351. clr.r = ((numIdx >> 16u) & 0xff) / 255.f;
  352. clr.g = ((numIdx >> 8u) & 0xff) / 255.f;
  353. clr.b = ((numIdx) & 0xff) / 255.f;
  354. tempColors.push_back(clr);
  355. }
  356. // normal vector
  357. else if (TokenMatch(sz,"norm",4))
  358. {
  359. hasNormals = true;
  360. AI_NFF_PARSE_TRIPLE(v);
  361. tempNormals.push_back(v);
  362. }
  363. // UV coordinate
  364. else if (TokenMatch(sz,"uv",2))
  365. {
  366. hasUVs = true;
  367. AI_NFF_PARSE_FLOAT(v.x);
  368. AI_NFF_PARSE_FLOAT(v.y);
  369. v.z = 0.f;
  370. tempTextureCoords.push_back(v);
  371. }
  372. }
  373. // fill in dummies for all attributes that have not been set
  374. if (tempNormals.size() != tempPositions.size())
  375. tempNormals.push_back(vQNAN);
  376. if (tempTextureCoords.size() != tempPositions.size())
  377. tempTextureCoords.push_back(vQNAN);
  378. if (tempColors.size() != tempPositions.size())
  379. tempColors.push_back(cQNAN);
  380. }
  381. AI_NFF2_GET_NEXT_TOKEN();
  382. if (!num)throw DeadlyImportError("NFF2: There are zero vertices");
  383. num = ::strtoul10(sz,&sz);
  384. std::vector<unsigned int> tempIdx;
  385. tempIdx.reserve(10);
  386. for (unsigned int i = 0; i < num; ++i)
  387. {
  388. AI_NFF2_GET_NEXT_TOKEN();
  389. SkipSpaces(line,&sz);
  390. unsigned int numIdx = ::strtoul10(sz,&sz);
  391. // read all faces indices
  392. if (numIdx)
  393. {
  394. // mesh.faces.push_back(numIdx);
  395. // tempIdx.erase(tempIdx.begin(),tempIdx.end());
  396. tempIdx.resize(numIdx);
  397. for (unsigned int a = 0; a < numIdx;++a)
  398. {
  399. SkipSpaces(sz,&sz);
  400. unsigned int m = ::strtoul10(sz,&sz);
  401. if (m >= (unsigned int)tempPositions.size())
  402. {
  403. DefaultLogger::get()->error("NFF2: Vertex index overflow");
  404. m= 0;
  405. }
  406. // mesh.vertices.push_back (tempPositions[idx]);
  407. tempIdx[a] = m;
  408. }
  409. }
  410. // build a temporary shader object for the face.
  411. ShadingInfo shader;
  412. unsigned int matIdx = 0;
  413. // white material color - we have vertex colors
  414. shader.color = aiColor3D(1.f,1.f,1.f);
  415. aiColor4D c = aiColor4D(1.f,1.f,1.f,1.f);
  416. while (true)
  417. {
  418. SkipSpaces(sz,&sz);
  419. if(IsLineEnd(*sz))break;
  420. // per-polygon colors
  421. if (TokenMatch(sz,"0x",2))
  422. {
  423. hasColor = true;
  424. const char* sz2 = sz;
  425. numIdx = ::strtoul16(sz,&sz);
  426. const unsigned int diff = (unsigned int)(sz-sz2);
  427. // 0xRRGGBB
  428. if (diff > 3)
  429. {
  430. c.r = ((numIdx >> 16u) & 0xff) / 255.f;
  431. c.g = ((numIdx >> 8u) & 0xff) / 255.f;
  432. c.b = ((numIdx) & 0xff) / 255.f;
  433. }
  434. // 0xRGB
  435. else
  436. {
  437. c.r = ((numIdx >> 8u) & 0xf) / 16.f;
  438. c.g = ((numIdx >> 4u) & 0xf) / 16.f;
  439. c.b = ((numIdx) & 0xf) / 16.f;
  440. }
  441. }
  442. // TODO - implement texture mapping here
  443. #if 0
  444. // mirror vertex texture coordinate?
  445. else if (TokenMatch(sz,"mirror",6))
  446. {
  447. }
  448. // texture coordinate scaling
  449. else if (TokenMatch(sz,"scale",5))
  450. {
  451. }
  452. // texture coordinate translation
  453. else if (TokenMatch(sz,"trans",5))
  454. {
  455. }
  456. // texture coordinate rotation angle
  457. else if (TokenMatch(sz,"rot",3))
  458. {
  459. }
  460. #endif
  461. // texture file name for this polygon + mapping information
  462. else if ('_' == sz[0])
  463. {
  464. // get mapping information
  465. switch (sz[1])
  466. {
  467. case 'v':
  468. case 'V':
  469. shader.shaded = false;
  470. break;
  471. case 't':
  472. case 'T':
  473. case 'u':
  474. case 'U':
  475. DefaultLogger::get()->warn("Unsupported NFF2 texture attribute: trans");
  476. };
  477. if (!sz[1] || '_' != sz[2])
  478. {
  479. DefaultLogger::get()->warn("NFF2: Expected underscore after texture attributes");
  480. continue;
  481. }
  482. const char* sz2 = sz+3;
  483. while (!IsSpaceOrNewLine( *sz ))++sz;
  484. const unsigned int diff = (unsigned int)(sz-sz2);
  485. if (diff)shader.texFile = std::string(sz2,diff);
  486. }
  487. // Two-sided material?
  488. else if (TokenMatch(sz,"both",4))
  489. {
  490. shader.twoSided = true;
  491. }
  492. // Material ID?
  493. else if (!materialTable.empty() && TokenMatch(sz,"matid",5))
  494. {
  495. SkipSpaces(&sz);
  496. matIdx = ::strtoul10(sz,&sz);
  497. if (matIdx >= materialTable.size())
  498. {
  499. DefaultLogger::get()->error("NFF2: Material index overflow.");
  500. matIdx = 0;
  501. }
  502. // now combine our current shader with the shader we
  503. // read from the material table.
  504. ShadingInfo& mat = materialTable[matIdx];
  505. shader.ambient = mat.ambient;
  506. shader.diffuse = mat.diffuse;
  507. shader.emissive = mat.emissive;
  508. shader.opacity = mat.opacity;
  509. shader.specular = mat.specular;
  510. shader.shininess = mat.shininess;
  511. }
  512. else SkipToken(sz);
  513. }
  514. // search the list of all shaders we have for this object whether
  515. // there is an identical one. In this case, we append our mesh
  516. // data to it.
  517. MeshInfo* mesh = NULL;
  518. for (std::vector<MeshInfo>::iterator it = meshes.begin() + objStart, end = meshes.end();
  519. it != end; ++it)
  520. {
  521. if ((*it).shader == shader && (*it).matIndex == matIdx)
  522. {
  523. // we have one, we can append our data to it
  524. mesh = &(*it);
  525. }
  526. }
  527. if (!mesh)
  528. {
  529. meshes.push_back(MeshInfo(PatchType_Simple,false));
  530. mesh = &meshes.back();
  531. mesh->matIndex = matIdx;
  532. // We need to add a new mesh to the list. We assign
  533. // an unique name to it to make sure the scene will
  534. // pass the validation step for the moment.
  535. // TODO: fix naming of objects in the scenegraph later
  536. if (objectName.length())
  537. {
  538. ::strcpy(mesh->name,objectName.c_str());
  539. ASSIMP_itoa10(&mesh->name[objectName.length()],30,subMeshIdx++);
  540. }
  541. // copy the shader to the mesh.
  542. mesh->shader = shader;
  543. }
  544. // fill the mesh with data
  545. if (!tempIdx.empty())
  546. {
  547. mesh->faces.push_back((unsigned int)tempIdx.size());
  548. for (std::vector<unsigned int>::const_iterator it = tempIdx.begin(), end = tempIdx.end();
  549. it != end;++it)
  550. {
  551. unsigned int m = *it;
  552. // copy colors -vertex color specifications override polygon color specifications
  553. if (hasColor)
  554. {
  555. const aiColor4D& clr = tempColors[m];
  556. mesh->colors.push_back((is_qnan( clr.r ) ? c : clr));
  557. }
  558. // positions should always be there
  559. mesh->vertices.push_back (tempPositions[m]);
  560. // copy normal vectors
  561. if (hasNormals)
  562. mesh->normals.push_back (tempNormals[m]);
  563. // copy texture coordinates
  564. if (hasUVs)
  565. mesh->uvs.push_back (tempTextureCoords[m]);
  566. }
  567. }
  568. }
  569. if (!num)throw DeadlyImportError("NFF2: There are zero faces");
  570. }
  571. }
  572. camLookAt = camLookAt + camPos;
  573. }
  574. else // "Normal" Neutral file format that is quite more common
  575. {
  576. while (GetNextLine(buffer,line))
  577. {
  578. sz = line;
  579. if ('p' == line[0] || TokenMatch(sz,"tpp",3))
  580. {
  581. MeshInfo* out = NULL;
  582. // 'tpp' - texture polygon patch primitive
  583. if ('t' == line[0])
  584. {
  585. currentMeshWithUVCoords = NULL;
  586. for (auto &mesh : meshesWithUVCoords)
  587. {
  588. if (mesh.shader == s)
  589. {
  590. currentMeshWithUVCoords = &mesh;
  591. break;
  592. }
  593. }
  594. if (!currentMeshWithUVCoords)
  595. {
  596. meshesWithUVCoords.push_back(MeshInfo(PatchType_UVAndNormals));
  597. currentMeshWithUVCoords = &meshesWithUVCoords.back();
  598. currentMeshWithUVCoords->shader = s;
  599. }
  600. out = currentMeshWithUVCoords;
  601. }
  602. // 'pp' - polygon patch primitive
  603. else if ('p' == line[1])
  604. {
  605. currentMeshWithNormals = NULL;
  606. for (auto &mesh : meshesWithNormals)
  607. {
  608. if (mesh.shader == s)
  609. {
  610. currentMeshWithNormals = &mesh;
  611. break;
  612. }
  613. }
  614. if (!currentMeshWithNormals)
  615. {
  616. meshesWithNormals.push_back(MeshInfo(PatchType_Normals));
  617. currentMeshWithNormals = &meshesWithNormals.back();
  618. currentMeshWithNormals->shader = s;
  619. }
  620. sz = &line[2];out = currentMeshWithNormals;
  621. }
  622. // 'p' - polygon primitive
  623. else
  624. {
  625. currentMesh = NULL;
  626. for (auto &mesh : meshes)
  627. {
  628. if (mesh.shader == s)
  629. {
  630. currentMesh = &mesh;
  631. break;
  632. }
  633. }
  634. if (!currentMesh)
  635. {
  636. meshes.push_back(MeshInfo(PatchType_Simple));
  637. currentMesh = &meshes.back();
  638. currentMesh->shader = s;
  639. }
  640. sz = &line[1];out = currentMesh;
  641. }
  642. SkipSpaces(sz,&sz);
  643. unsigned int m = strtoul10(sz);
  644. // ---- flip the face order
  645. out->vertices.resize(out->vertices.size()+m);
  646. if (out != currentMesh)
  647. {
  648. out->normals.resize(out->vertices.size());
  649. }
  650. if (out == currentMeshWithUVCoords)
  651. {
  652. out->uvs.resize(out->vertices.size());
  653. }
  654. for (unsigned int n = 0; n < m;++n)
  655. {
  656. if(!GetNextLine(buffer,line))
  657. {
  658. DefaultLogger::get()->error("NFF: Unexpected EOF was encountered. Patch definition incomplete");
  659. continue;
  660. }
  661. aiVector3D v; sz = &line[0];
  662. AI_NFF_PARSE_TRIPLE(v);
  663. out->vertices[out->vertices.size()-n-1] = v;
  664. if (out != currentMesh)
  665. {
  666. AI_NFF_PARSE_TRIPLE(v);
  667. out->normals[out->vertices.size()-n-1] = v;
  668. }
  669. if (out == currentMeshWithUVCoords)
  670. {
  671. // FIX: in one test file this wraps over multiple lines
  672. SkipSpaces(&sz);
  673. if (IsLineEnd(*sz))
  674. {
  675. GetNextLine(buffer,line);
  676. sz = line;
  677. }
  678. AI_NFF_PARSE_FLOAT(v.x);
  679. SkipSpaces(&sz);
  680. if (IsLineEnd(*sz))
  681. {
  682. GetNextLine(buffer,line);
  683. sz = line;
  684. }
  685. AI_NFF_PARSE_FLOAT(v.y);
  686. v.y = 1.f - v.y;
  687. out->uvs[out->vertices.size()-n-1] = v;
  688. }
  689. }
  690. out->faces.push_back(m);
  691. }
  692. // 'f' - shading information block
  693. else if (TokenMatch(sz,"f",1))
  694. {
  695. float d;
  696. // read the RGB colors
  697. AI_NFF_PARSE_TRIPLE(s.color);
  698. // read the other properties
  699. AI_NFF_PARSE_FLOAT(s.diffuse.r);
  700. AI_NFF_PARSE_FLOAT(s.specular.r);
  701. AI_NFF_PARSE_FLOAT(d); // skip shininess and transmittance
  702. AI_NFF_PARSE_FLOAT(d);
  703. AI_NFF_PARSE_FLOAT(s.refracti);
  704. // NFF2 uses full colors here so we need to use them too
  705. // although NFF uses simple scaling factors
  706. s.diffuse.g = s.diffuse.b = s.diffuse.r;
  707. s.specular.g = s.specular.b = s.specular.r;
  708. // if the next one is NOT a number we assume it is a texture file name
  709. // this feature is used by some NFF files on the internet and it has
  710. // been implemented as it can be really useful
  711. SkipSpaces(&sz);
  712. if (!IsNumeric(*sz))
  713. {
  714. // TODO: Support full file names with spaces and quotation marks ...
  715. const char* p = sz;
  716. while (!IsSpaceOrNewLine( *sz ))++sz;
  717. unsigned int diff = (unsigned int)(sz-p);
  718. if (diff)
  719. {
  720. s.texFile = std::string(p,diff);
  721. }
  722. }
  723. else
  724. {
  725. AI_NFF_PARSE_FLOAT(s.ambient); // optional
  726. }
  727. }
  728. // 'shader' - other way to specify a texture
  729. else if (TokenMatch(sz,"shader",6))
  730. {
  731. SkipSpaces(&sz);
  732. const char* old = sz;
  733. while (!IsSpaceOrNewLine(*sz))++sz;
  734. s.texFile = std::string(old, (uintptr_t)sz - (uintptr_t)old);
  735. }
  736. // 'l' - light source
  737. else if (TokenMatch(sz,"l",1))
  738. {
  739. lights.push_back(Light());
  740. Light& light = lights.back();
  741. AI_NFF_PARSE_TRIPLE(light.position);
  742. AI_NFF_PARSE_FLOAT (light.intensity);
  743. AI_NFF_PARSE_TRIPLE(light.color);
  744. }
  745. // 's' - sphere
  746. else if (TokenMatch(sz,"s",1))
  747. {
  748. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  749. MeshInfo& currentMesh = meshesLocked.back();
  750. currentMesh.shader = s;
  751. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  752. AI_NFF_PARSE_SHAPE_INFORMATION();
  753. // we don't need scaling or translation here - we do it in the node's transform
  754. StandardShapes::MakeSphere(iTesselation, currentMesh.vertices);
  755. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  756. // generate a name for the mesh
  757. ::ai_snprintf(currentMesh.name,128,"sphere_%i",sphere++);
  758. }
  759. // 'dod' - dodecahedron
  760. else if (TokenMatch(sz,"dod",3))
  761. {
  762. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  763. MeshInfo& currentMesh = meshesLocked.back();
  764. currentMesh.shader = s;
  765. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  766. AI_NFF_PARSE_SHAPE_INFORMATION();
  767. // we don't need scaling or translation here - we do it in the node's transform
  768. StandardShapes::MakeDodecahedron(currentMesh.vertices);
  769. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  770. // generate a name for the mesh
  771. ::ai_snprintf(currentMesh.name,128,"dodecahedron_%i",dodecahedron++);
  772. }
  773. // 'oct' - octahedron
  774. else if (TokenMatch(sz,"oct",3))
  775. {
  776. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  777. MeshInfo& currentMesh = meshesLocked.back();
  778. currentMesh.shader = s;
  779. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  780. AI_NFF_PARSE_SHAPE_INFORMATION();
  781. // we don't need scaling or translation here - we do it in the node's transform
  782. StandardShapes::MakeOctahedron(currentMesh.vertices);
  783. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  784. // generate a name for the mesh
  785. ::ai_snprintf(currentMesh.name,128,"octahedron_%i",octahedron++);
  786. }
  787. // 'tet' - tetrahedron
  788. else if (TokenMatch(sz,"tet",3))
  789. {
  790. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  791. MeshInfo& currentMesh = meshesLocked.back();
  792. currentMesh.shader = s;
  793. currentMesh.shader.mapping = aiTextureMapping_SPHERE;
  794. AI_NFF_PARSE_SHAPE_INFORMATION();
  795. // we don't need scaling or translation here - we do it in the node's transform
  796. StandardShapes::MakeTetrahedron(currentMesh.vertices);
  797. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  798. // generate a name for the mesh
  799. ::ai_snprintf(currentMesh.name,128,"tetrahedron_%i",tetrahedron++);
  800. }
  801. // 'hex' - hexahedron
  802. else if (TokenMatch(sz,"hex",3))
  803. {
  804. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  805. MeshInfo& currentMesh = meshesLocked.back();
  806. currentMesh.shader = s;
  807. currentMesh.shader.mapping = aiTextureMapping_BOX;
  808. AI_NFF_PARSE_SHAPE_INFORMATION();
  809. // we don't need scaling or translation here - we do it in the node's transform
  810. StandardShapes::MakeHexahedron(currentMesh.vertices);
  811. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  812. // generate a name for the mesh
  813. ::ai_snprintf(currentMesh.name,128,"hexahedron_%i",hexahedron++);
  814. }
  815. // 'c' - cone
  816. else if (TokenMatch(sz,"c",1))
  817. {
  818. meshesLocked.push_back(MeshInfo(PatchType_Simple,true));
  819. MeshInfo& currentMesh = meshesLocked.back();
  820. currentMesh.shader = s;
  821. currentMesh.shader.mapping = aiTextureMapping_CYLINDER;
  822. if(!GetNextLine(buffer,line))
  823. {
  824. DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)");
  825. break;
  826. }
  827. sz = line;
  828. // read the two center points and the respective radii
  829. aiVector3D center1, center2; float radius1, radius2;
  830. AI_NFF_PARSE_TRIPLE(center1);
  831. AI_NFF_PARSE_FLOAT(radius1);
  832. if(!GetNextLine(buffer,line))
  833. {
  834. DefaultLogger::get()->error("NFF: Unexpected end of file (cone definition not complete)");
  835. break;
  836. }
  837. sz = line;
  838. AI_NFF_PARSE_TRIPLE(center2);
  839. AI_NFF_PARSE_FLOAT(radius2);
  840. // compute the center point of the cone/cylinder -
  841. // it is its local transformation origin
  842. currentMesh.dir = center2-center1;
  843. currentMesh.center = center1+currentMesh.dir/(ai_real)2.0;
  844. float f;
  845. if (( f = currentMesh.dir.Length()) < 10e-3f )
  846. {
  847. DefaultLogger::get()->error("NFF: Cone height is close to zero");
  848. continue;
  849. }
  850. currentMesh.dir /= f; // normalize
  851. // generate the cone - it consists of simple triangles
  852. StandardShapes::MakeCone(f, radius1, radius2,
  853. integer_pow(4, iTesselation), currentMesh.vertices);
  854. // MakeCone() returns tris
  855. currentMesh.faces.resize(currentMesh.vertices.size()/3,3);
  856. // generate a name for the mesh. 'cone' if it a cone,
  857. // 'cylinder' if it is a cylinder. Funny, isn't it?
  858. if (radius1 != radius2)
  859. ::ai_snprintf(currentMesh.name,128,"cone_%i",cone++);
  860. else ::ai_snprintf(currentMesh.name,128,"cylinder_%i",cylinder++);
  861. }
  862. // 'tess' - tesselation
  863. else if (TokenMatch(sz,"tess",4))
  864. {
  865. SkipSpaces(&sz);
  866. iTesselation = strtoul10(sz);
  867. }
  868. // 'from' - camera position
  869. else if (TokenMatch(sz,"from",4))
  870. {
  871. AI_NFF_PARSE_TRIPLE(camPos);
  872. hasCam = true;
  873. }
  874. // 'at' - camera look-at vector
  875. else if (TokenMatch(sz,"at",2))
  876. {
  877. AI_NFF_PARSE_TRIPLE(camLookAt);
  878. hasCam = true;
  879. }
  880. // 'up' - camera up vector
  881. else if (TokenMatch(sz,"up",2))
  882. {
  883. AI_NFF_PARSE_TRIPLE(camUp);
  884. hasCam = true;
  885. }
  886. // 'angle' - (half?) camera field of view
  887. else if (TokenMatch(sz,"angle",5))
  888. {
  889. AI_NFF_PARSE_FLOAT(angle);
  890. hasCam = true;
  891. }
  892. // 'resolution' - used to compute the screen aspect
  893. else if (TokenMatch(sz,"resolution",10))
  894. {
  895. AI_NFF_PARSE_FLOAT(resolution.x);
  896. AI_NFF_PARSE_FLOAT(resolution.y);
  897. hasCam = true;
  898. }
  899. // 'pb' - bezier patch. Not supported yet
  900. else if (TokenMatch(sz,"pb",2))
  901. {
  902. DefaultLogger::get()->error("NFF: Encountered unsupported ID: bezier patch");
  903. }
  904. // 'pn' - NURBS. Not supported yet
  905. else if (TokenMatch(sz,"pn",2) || TokenMatch(sz,"pnn",3))
  906. {
  907. DefaultLogger::get()->error("NFF: Encountered unsupported ID: NURBS");
  908. }
  909. // '' - comment
  910. else if ('#' == line[0])
  911. {
  912. const char* sz;SkipSpaces(&line[1],&sz);
  913. if (!IsLineEnd(*sz))DefaultLogger::get()->info(sz);
  914. }
  915. }
  916. }
  917. // copy all arrays into one large
  918. meshes.reserve (meshes.size()+meshesLocked.size()+meshesWithNormals.size()+meshesWithUVCoords.size());
  919. meshes.insert (meshes.end(),meshesLocked.begin(),meshesLocked.end());
  920. meshes.insert (meshes.end(),meshesWithNormals.begin(),meshesWithNormals.end());
  921. meshes.insert (meshes.end(),meshesWithUVCoords.begin(),meshesWithUVCoords.end());
  922. // now generate output meshes. first find out how many meshes we'll need
  923. std::vector<MeshInfo>::const_iterator it = meshes.begin(), end = meshes.end();
  924. for (;it != end;++it)
  925. {
  926. if (!(*it).faces.empty())
  927. {
  928. ++pScene->mNumMeshes;
  929. if ((*it).name[0])++numNamed;
  930. }
  931. }
  932. // generate a dummy root node - assign all unnamed elements such
  933. // as polygons and polygon patches to the root node and generate
  934. // sub nodes for named objects such as spheres and cones.
  935. aiNode* const root = new aiNode();
  936. root->mName.Set("<NFF_Root>");
  937. root->mNumChildren = numNamed + (hasCam ? 1 : 0) + (unsigned int) lights.size();
  938. root->mNumMeshes = pScene->mNumMeshes-numNamed;
  939. aiNode** ppcChildren = NULL;
  940. unsigned int* pMeshes = NULL;
  941. if (root->mNumMeshes)
  942. pMeshes = root->mMeshes = new unsigned int[root->mNumMeshes];
  943. if (root->mNumChildren)
  944. ppcChildren = root->mChildren = new aiNode*[root->mNumChildren];
  945. // generate the camera
  946. if (hasCam)
  947. {
  948. aiNode* nd = *ppcChildren = new aiNode();
  949. nd->mName.Set("<NFF_Camera>");
  950. nd->mParent = root;
  951. // allocate the camera in the scene
  952. pScene->mNumCameras = 1;
  953. pScene->mCameras = new aiCamera*[1];
  954. aiCamera* c = pScene->mCameras[0] = new aiCamera;
  955. c->mName = nd->mName; // make sure the names are identical
  956. c->mHorizontalFOV = AI_DEG_TO_RAD( angle );
  957. c->mLookAt = camLookAt - camPos;
  958. c->mPosition = camPos;
  959. c->mUp = camUp;
  960. // If the resolution is not specified in the file, we
  961. // need to set 1.0 as aspect.
  962. c->mAspect = (!resolution.y ? 0.f : resolution.x / resolution.y);
  963. ++ppcChildren;
  964. }
  965. // generate light sources
  966. if (!lights.empty())
  967. {
  968. pScene->mNumLights = (unsigned int)lights.size();
  969. pScene->mLights = new aiLight*[pScene->mNumLights];
  970. for (unsigned int i = 0; i < pScene->mNumLights;++i,++ppcChildren)
  971. {
  972. const Light& l = lights[i];
  973. aiNode* nd = *ppcChildren = new aiNode();
  974. nd->mParent = root;
  975. nd->mName.length = ::ai_snprintf(nd->mName.data,1024,"<NFF_Light%u>",i);
  976. // allocate the light in the scene data structure
  977. aiLight* out = pScene->mLights[i] = new aiLight();
  978. out->mName = nd->mName; // make sure the names are identical
  979. out->mType = aiLightSource_POINT;
  980. out->mColorDiffuse = out->mColorSpecular = l.color * l.intensity;
  981. out->mPosition = l.position;
  982. }
  983. }
  984. if (!pScene->mNumMeshes)throw DeadlyImportError("NFF: No meshes loaded");
  985. pScene->mMeshes = new aiMesh*[pScene->mNumMeshes];
  986. pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = pScene->mNumMeshes];
  987. unsigned int m = 0;
  988. for (it = meshes.begin(); 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