NFFLoader.cpp 38 KB

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