NFFLoader.cpp 48 KB

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