NFFLoader.cpp 48 KB

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