NFFLoader.cpp 38 KB

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