NFFLoader.cpp 38 KB

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