NFFLoader.cpp 49 KB

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