NFFLoader.cpp 49 KB

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