NFFLoader.cpp 38 KB

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