NFFLoader.cpp 38 KB

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