NFFLoader.cpp 37 KB

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