ACLoader.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 AC3D importer class */
  35. #ifndef ASSIMP_BUILD_NO_AC_IMPORTER
  36. // internal headers
  37. #include "ACLoader.h"
  38. #include "Common/Importer.h"
  39. #include <assimp/BaseImporter.h>
  40. #include <assimp/ParsingUtils.h>
  41. #include <assimp/Subdivision.h>
  42. #include <assimp/config.h>
  43. #include <assimp/fast_atof.h>
  44. #include <assimp/importerdesc.h>
  45. #include <assimp/light.h>
  46. #include <assimp/material.h>
  47. #include <assimp/scene.h>
  48. #include <assimp/DefaultLogger.hpp>
  49. #include <assimp/IOSystem.hpp>
  50. #include <assimp/Importer.hpp>
  51. #include <memory>
  52. namespace Assimp {
  53. static constexpr aiImporterDesc desc = {
  54. "AC3D Importer",
  55. "",
  56. "",
  57. "",
  58. aiImporterFlags_SupportTextFlavour,
  59. 0,
  60. 0,
  61. 0,
  62. 0,
  63. "ac acc ac3d"
  64. };
  65. static constexpr auto ACDoubleSidedFlag = 0x20;
  66. // ------------------------------------------------------------------------------------------------
  67. // skip to the next token
  68. inline const char *AcSkipToNextToken(const char *buffer, const char *end) {
  69. if (!SkipSpaces(&buffer, end)) {
  70. ASSIMP_LOG_ERROR("AC3D: Unexpected EOF/EOL");
  71. }
  72. return buffer;
  73. }
  74. // ------------------------------------------------------------------------------------------------
  75. // read a string (may be enclosed in double quotation marks). buffer must point to "
  76. inline const char *AcGetString(const char *buffer, const char *end, std::string &out) {
  77. if (*buffer == '\0') {
  78. throw DeadlyImportError("AC3D: Unexpected EOF in string");
  79. }
  80. ++buffer;
  81. const char *sz = buffer;
  82. while ('\"' != *buffer && buffer != end) {
  83. if (IsLineEnd(*buffer)) {
  84. ASSIMP_LOG_ERROR("AC3D: Unexpected EOF/EOL in string");
  85. out = "ERROR";
  86. break;
  87. }
  88. ++buffer;
  89. }
  90. if (IsLineEnd(*buffer)) {
  91. return buffer;
  92. }
  93. out = std::string(sz, (unsigned int)(buffer - sz));
  94. ++buffer;
  95. return buffer;
  96. }
  97. // ------------------------------------------------------------------------------------------------
  98. // read 1 to n floats prefixed with an optional predefined identifier
  99. template <class T>
  100. inline const char *TAcCheckedLoadFloatArray(const char *buffer, const char *end, const char *name, size_t name_length, size_t num, T *out) {
  101. buffer = AcSkipToNextToken(buffer, end);
  102. if (0 != name_length) {
  103. if (0 != strncmp(buffer, name, name_length) || !IsSpace(buffer[name_length])) {
  104. ASSIMP_LOG_ERROR("AC3D: Unexpected token. ", name, " was expected.");
  105. return buffer;
  106. }
  107. buffer += name_length + 1;
  108. }
  109. for (unsigned int _i = 0; _i < num; ++_i) {
  110. buffer = AcSkipToNextToken(buffer, end);
  111. buffer = fast_atoreal_move(buffer, ((float *)out)[_i]);
  112. }
  113. return buffer;
  114. }
  115. // ------------------------------------------------------------------------------------------------
  116. // Reverses vertex indices in a face.
  117. static void flipWindingOrder(aiFace &f) {
  118. std::reverse(f.mIndices, f.mIndices + f.mNumIndices);
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. // Duplicates a face and inverts it. Also duplicates all vertices (so the new face gets its own
  122. // set of normals and isn’t smoothed against the original).
  123. static void buildBacksideOfFace(const aiFace &origFace, aiFace *&outFaces, aiVector3D *&outVertices, const aiVector3D *allVertices,
  124. aiVector3D *&outUV, const aiVector3D *allUV, unsigned &curIdx) {
  125. auto &newFace = *outFaces++;
  126. newFace = origFace;
  127. flipWindingOrder(newFace);
  128. for (unsigned f = 0; f < newFace.mNumIndices; ++f) {
  129. *outVertices++ = allVertices[newFace.mIndices[f]];
  130. if (outUV) {
  131. *outUV = allUV[newFace.mIndices[f]];
  132. outUV++;
  133. }
  134. newFace.mIndices[f] = curIdx++;
  135. }
  136. }
  137. // ------------------------------------------------------------------------------------------------
  138. // Constructor to be privately used by Importer
  139. AC3DImporter::AC3DImporter() :
  140. mBuffer(),
  141. configSplitBFCull(),
  142. configEvalSubdivision(),
  143. mNumMeshes(),
  144. mLights(),
  145. mLightsCounter(0),
  146. mGroupsCounter(0),
  147. mPolysCounter(0),
  148. mWorldsCounter(0) {
  149. // nothing to be done here
  150. }
  151. // ------------------------------------------------------------------------------------------------
  152. // Returns whether the class can handle the format of the given file.
  153. bool AC3DImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /*checkSig*/) const {
  154. static constexpr uint32_t tokens[] = { AI_MAKE_MAGIC("AC3D") };
  155. return CheckMagicToken(pIOHandler, pFile, tokens, AI_COUNT_OF(tokens));
  156. }
  157. // ------------------------------------------------------------------------------------------------
  158. // Loader meta information
  159. const aiImporterDesc *AC3DImporter::GetInfo() const {
  160. return &desc;
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. // Get a pointer to the next line from the file
  164. bool AC3DImporter::GetNextLine() {
  165. SkipLine(&mBuffer.data, mBuffer.end);
  166. return SkipSpaces(&mBuffer.data, mBuffer.end);
  167. }
  168. // ------------------------------------------------------------------------------------------------
  169. // Parse an object section in an AC file
  170. bool AC3DImporter::LoadObjectSection(std::vector<Object> &objects) {
  171. if (!TokenMatch(mBuffer.data, "OBJECT", 6)) {
  172. return false;
  173. }
  174. SkipSpaces(&mBuffer.data, mBuffer.end);
  175. ++mNumMeshes;
  176. objects.emplace_back();
  177. Object &obj = objects.back();
  178. aiLight *light = nullptr;
  179. if (!ASSIMP_strincmp(mBuffer.data, "light", 5)) {
  180. // This is a light source. Add it to the list
  181. mLights->push_back(light = new aiLight());
  182. // Return a point light with no attenuation
  183. light->mType = aiLightSource_POINT;
  184. light->mColorDiffuse = light->mColorSpecular = aiColor3D(1.f, 1.f, 1.f);
  185. light->mAttenuationConstant = 1.f;
  186. // Generate a default name for both the light source and the node
  187. light->mName.length = ::ai_snprintf(light->mName.data, AI_MAXLEN, "ACLight_%i", static_cast<unsigned int>(mLights->size()) - 1);
  188. obj.name = std::string(light->mName.data);
  189. ASSIMP_LOG_VERBOSE_DEBUG("AC3D: Light source encountered");
  190. obj.type = Object::Light;
  191. } else if (!ASSIMP_strincmp(mBuffer.data, "group", 5)) {
  192. obj.type = Object::Group;
  193. } else if (!ASSIMP_strincmp(mBuffer.data, "world", 5)) {
  194. obj.type = Object::World;
  195. } else {
  196. obj.type = Object::Poly;
  197. }
  198. while (GetNextLine()) {
  199. if (TokenMatch(mBuffer.data, "kids", 4)) {
  200. SkipSpaces(&mBuffer.data, mBuffer.end);
  201. unsigned int num = strtoul10(mBuffer.data, &mBuffer.data);
  202. GetNextLine();
  203. if (num) {
  204. // load the children of this object recursively
  205. obj.children.reserve(num);
  206. for (unsigned int i = 0; i < num; ++i) {
  207. if (!LoadObjectSection(obj.children)) {
  208. ASSIMP_LOG_WARN("AC3D: wrong number of kids");
  209. break;
  210. }
  211. }
  212. }
  213. return true;
  214. } else if (TokenMatch(mBuffer.data, "name", 4)) {
  215. SkipSpaces(&mBuffer.data, mBuffer.data);
  216. mBuffer.data = AcGetString(mBuffer.data, mBuffer.end, obj.name);
  217. // If this is a light source, we'll also need to store
  218. // the name of the node in it.
  219. if (light) {
  220. light->mName.Set(obj.name);
  221. }
  222. } else if (TokenMatch(mBuffer.data, "texture", 7)) {
  223. SkipSpaces(&mBuffer.data, mBuffer.end);
  224. std::string texture;
  225. mBuffer.data = AcGetString(mBuffer.data, mBuffer.end, texture);
  226. obj.textures.push_back(texture);
  227. } else if (TokenMatch(mBuffer.data, "texrep", 6)) {
  228. SkipSpaces(&mBuffer.data, mBuffer.end);
  229. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 2, &obj.texRepeat);
  230. if (!obj.texRepeat.x || !obj.texRepeat.y)
  231. obj.texRepeat = aiVector2D(1.f, 1.f);
  232. } else if (TokenMatch(mBuffer.data, "texoff", 6)) {
  233. SkipSpaces(&mBuffer.data, mBuffer.end);
  234. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 2, &obj.texOffset);
  235. } else if (TokenMatch(mBuffer.data, "rot", 3)) {
  236. SkipSpaces(&mBuffer.data, mBuffer.end);
  237. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 9, &obj.rotation);
  238. } else if (TokenMatch(mBuffer.data, "loc", 3)) {
  239. SkipSpaces(&mBuffer.data, mBuffer.end);
  240. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 3, &obj.translation);
  241. } else if (TokenMatch(mBuffer.data, "subdiv", 6)) {
  242. SkipSpaces(&mBuffer.data, mBuffer.end);
  243. obj.subDiv = strtoul10(mBuffer.data, &mBuffer.data);
  244. } else if (TokenMatch(mBuffer.data, "crease", 6)) {
  245. SkipSpaces(&mBuffer.data, mBuffer.end);
  246. obj.crease = fast_atof(mBuffer.data);
  247. } else if (TokenMatch(mBuffer.data, "numvert", 7)) {
  248. SkipSpaces(&mBuffer.data, mBuffer.end);
  249. unsigned int t = strtoul10(mBuffer.data, &mBuffer.data);
  250. if (t >= AI_MAX_ALLOC(aiVector3D)) {
  251. throw DeadlyImportError("AC3D: Too many vertices, would run out of memory");
  252. }
  253. obj.vertices.reserve(t);
  254. for (unsigned int i = 0; i < t; ++i) {
  255. if (!GetNextLine()) {
  256. ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: not all vertices have been parsed yet");
  257. break;
  258. } else if (!IsNumeric(*mBuffer.data)) {
  259. ASSIMP_LOG_ERROR("AC3D: Unexpected token: not all vertices have been parsed yet");
  260. --mBuffer.data; // make sure the line is processed a second time
  261. break;
  262. }
  263. obj.vertices.emplace_back();
  264. aiVector3D &v = obj.vertices.back();
  265. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 3, &v.x);
  266. }
  267. } else if (TokenMatch(mBuffer.data, "numsurf", 7)) {
  268. SkipSpaces(&mBuffer.data, mBuffer.end);
  269. bool Q3DWorkAround = false;
  270. const unsigned int t = strtoul10(mBuffer.data, &mBuffer.data);
  271. obj.surfaces.reserve(t);
  272. for (unsigned int i = 0; i < t; ++i) {
  273. GetNextLine();
  274. if (!TokenMatch(mBuffer.data, "SURF", 4)) {
  275. // FIX: this can occur for some files - Quick 3D for
  276. // example writes no surf chunks
  277. if (!Q3DWorkAround) {
  278. ASSIMP_LOG_WARN("AC3D: SURF token was expected");
  279. ASSIMP_LOG_VERBOSE_DEBUG("Continuing with Quick3D Workaround enabled");
  280. }
  281. --mBuffer.data; // make sure the line is processed a second time
  282. // break; --- see fix notes above
  283. Q3DWorkAround = true;
  284. }
  285. SkipSpaces(&mBuffer.data, mBuffer.end);
  286. obj.surfaces.emplace_back();
  287. Surface &surf = obj.surfaces.back();
  288. surf.flags = strtoul_cppstyle(mBuffer.data);
  289. while (true) {
  290. if (!GetNextLine()) {
  291. throw DeadlyImportError("AC3D: Unexpected EOF: surface is incomplete");
  292. }
  293. if (TokenMatch(mBuffer.data, "mat", 3)) {
  294. SkipSpaces(&mBuffer.data, mBuffer.end);
  295. surf.mat = strtoul10(mBuffer.data);
  296. } else if (TokenMatch(mBuffer.data, "refs", 4)) {
  297. // --- see fix notes above
  298. if (Q3DWorkAround) {
  299. if (!surf.entries.empty()) {
  300. mBuffer.data -= 6;
  301. break;
  302. }
  303. }
  304. SkipSpaces(&mBuffer.data, mBuffer.end);
  305. const unsigned int m = strtoul10(mBuffer.data);
  306. surf.entries.reserve(m);
  307. obj.numRefs += m;
  308. for (unsigned int k = 0; k < m; ++k) {
  309. if (!GetNextLine()) {
  310. ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: surface references are incomplete");
  311. break;
  312. }
  313. surf.entries.emplace_back();
  314. Surface::SurfaceEntry &entry = surf.entries.back();
  315. entry.first = strtoul10(mBuffer.data, &mBuffer.data);
  316. SkipSpaces(&mBuffer.data, mBuffer.end);
  317. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "", 0, 2, &entry.second);
  318. }
  319. } else {
  320. --mBuffer.data; // make sure the line is processed a second time
  321. break;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. ASSIMP_LOG_ERROR("AC3D: Unexpected EOF: \'kids\' line was expected");
  328. return false;
  329. }
  330. // ------------------------------------------------------------------------------------------------
  331. // Convert a material from AC3DImporter::Material to aiMaterial
  332. void AC3DImporter::ConvertMaterial(const Object &object,
  333. const Material &matSrc,
  334. aiMaterial &matDest) {
  335. aiString s;
  336. if (matSrc.name.length()) {
  337. s.Set(matSrc.name);
  338. matDest.AddProperty(&s, AI_MATKEY_NAME);
  339. }
  340. if (!object.textures.empty()) {
  341. s.Set(object.textures[0]);
  342. matDest.AddProperty(&s, AI_MATKEY_TEXTURE_DIFFUSE(0));
  343. // UV transformation
  344. if (1.f != object.texRepeat.x || 1.f != object.texRepeat.y ||
  345. object.texOffset.x || object.texOffset.y) {
  346. aiUVTransform transform;
  347. transform.mScaling = object.texRepeat;
  348. transform.mTranslation = object.texOffset;
  349. matDest.AddProperty(&transform, 1, AI_MATKEY_UVTRANSFORM_DIFFUSE(0));
  350. }
  351. }
  352. matDest.AddProperty<aiColor3D>(&matSrc.rgb, 1, AI_MATKEY_COLOR_DIFFUSE);
  353. matDest.AddProperty<aiColor3D>(&matSrc.amb, 1, AI_MATKEY_COLOR_AMBIENT);
  354. matDest.AddProperty<aiColor3D>(&matSrc.emis, 1, AI_MATKEY_COLOR_EMISSIVE);
  355. matDest.AddProperty<aiColor3D>(&matSrc.spec, 1, AI_MATKEY_COLOR_SPECULAR);
  356. int n = -1;
  357. if (matSrc.shin) {
  358. n = aiShadingMode_Phong;
  359. matDest.AddProperty<float>(&matSrc.shin, 1, AI_MATKEY_SHININESS);
  360. } else {
  361. n = aiShadingMode_Gouraud;
  362. }
  363. matDest.AddProperty<int>(&n, 1, AI_MATKEY_SHADING_MODEL);
  364. float f = 1.f - matSrc.trans;
  365. matDest.AddProperty<float>(&f, 1, AI_MATKEY_OPACITY);
  366. }
  367. // ------------------------------------------------------------------------------------------------
  368. // Converts the loaded data to the internal verbose representation
  369. aiNode *AC3DImporter::ConvertObjectSection(Object &object,
  370. MeshArray &meshes,
  371. std::vector<aiMaterial *> &outMaterials,
  372. const std::vector<Material> &materials,
  373. aiNode *parent) {
  374. aiNode *node = new aiNode();
  375. node->mParent = parent;
  376. if (object.vertices.size()) {
  377. if (!object.surfaces.size() || !object.numRefs) {
  378. /* " An object with 7 vertices (no surfaces, no materials defined).
  379. This is a good way of getting point data into AC3D.
  380. The Vertex->create convex-surface/object can be used on these
  381. vertices to 'wrap' a 3d shape around them "
  382. (http://www.opencity.info/html/ac3dfileformat.html)
  383. therefore: if no surfaces are defined return point data only
  384. */
  385. ASSIMP_LOG_INFO("AC3D: No surfaces defined in object definition, "
  386. "a point list is returned");
  387. meshes.push_back(new aiMesh());
  388. aiMesh *mesh = meshes.back();
  389. mesh->mNumFaces = mesh->mNumVertices = (unsigned int)object.vertices.size();
  390. aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  391. aiVector3D *verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  392. for (unsigned int i = 0; i < mesh->mNumVertices; ++i, ++faces, ++verts) {
  393. *verts = object.vertices[i];
  394. faces->mNumIndices = 1;
  395. faces->mIndices = new unsigned int[1];
  396. faces->mIndices[0] = i;
  397. }
  398. // use the primary material in this case. this should be the
  399. // default material if all objects of the file contain points
  400. // and no faces.
  401. mesh->mMaterialIndex = 0;
  402. outMaterials.push_back(new aiMaterial());
  403. ConvertMaterial(object, materials[0], *outMaterials.back());
  404. } else {
  405. // need to generate one or more meshes for this object.
  406. // find out how many different materials we have
  407. typedef std::pair<unsigned int, unsigned int> IntPair;
  408. typedef std::vector<IntPair> MatTable;
  409. MatTable needMat(materials.size(), IntPair(0, 0));
  410. std::vector<Surface>::iterator it, end = object.surfaces.end();
  411. std::vector<Surface::SurfaceEntry>::iterator it2, end2;
  412. for (it = object.surfaces.begin(); it != end; ++it) {
  413. unsigned int idx = (*it).mat;
  414. if (idx >= needMat.size()) {
  415. ASSIMP_LOG_ERROR("AC3D: material index is out of range");
  416. idx = 0;
  417. }
  418. if ((*it).entries.empty()) {
  419. ASSIMP_LOG_WARN("AC3D: surface has zero vertex references");
  420. }
  421. const bool isDoubleSided = ACDoubleSidedFlag == (it->flags & ACDoubleSidedFlag);
  422. const int doubleSidedFactor = isDoubleSided ? 2 : 1;
  423. // validate all vertex indices to make sure we won't crash here
  424. for (it2 = (*it).entries.begin(),
  425. end2 = (*it).entries.end();
  426. it2 != end2; ++it2) {
  427. if ((*it2).first >= object.vertices.size()) {
  428. ASSIMP_LOG_WARN("AC3D: Invalid vertex reference");
  429. (*it2).first = 0;
  430. }
  431. }
  432. if (!needMat[idx].first) {
  433. ++node->mNumMeshes;
  434. }
  435. switch ((*it).GetType()) {
  436. case Surface::ClosedLine: // closed line
  437. needMat[idx].first += static_cast<unsigned int>((*it).entries.size());
  438. needMat[idx].second += static_cast<unsigned int>((*it).entries.size() << 1u);
  439. break;
  440. // unclosed line
  441. case Surface::OpenLine:
  442. needMat[idx].first += static_cast<unsigned int>((*it).entries.size() - 1);
  443. needMat[idx].second += static_cast<unsigned int>(((*it).entries.size() - 1) << 1u);
  444. break;
  445. // triangle strip
  446. case Surface::TriangleStrip:
  447. needMat[idx].first += static_cast<unsigned int>(it->entries.size() - 2) * doubleSidedFactor;
  448. needMat[idx].second += static_cast<unsigned int>(it->entries.size() - 2) * 3 * doubleSidedFactor;
  449. break;
  450. default:
  451. // Coerce unknowns to a polygon and warn
  452. ASSIMP_LOG_WARN("AC3D: The type flag of a surface is unknown: ", (*it).flags);
  453. (*it).flags &= ~(Surface::Mask);
  454. // fallthrough
  455. // polygon
  456. case Surface::Polygon:
  457. // the number of faces increments by one, the number
  458. // of vertices by surface.numref.
  459. needMat[idx].first += doubleSidedFactor;
  460. needMat[idx].second += static_cast<unsigned int>(it->entries.size()) * doubleSidedFactor;
  461. };
  462. }
  463. unsigned int *pip = node->mMeshes = new unsigned int[node->mNumMeshes];
  464. unsigned int mat = 0;
  465. const size_t oldm = meshes.size();
  466. for (MatTable::const_iterator cit = needMat.begin(), cend = needMat.end();
  467. cit != cend; ++cit, ++mat) {
  468. if (!(*cit).first) {
  469. continue;
  470. }
  471. // allocate a new aiMesh object
  472. *pip++ = (unsigned int)meshes.size();
  473. aiMesh *mesh = new aiMesh();
  474. meshes.push_back(mesh);
  475. mesh->mMaterialIndex = static_cast<unsigned int>(outMaterials.size());
  476. outMaterials.push_back(new aiMaterial());
  477. ConvertMaterial(object, materials[mat], *outMaterials.back());
  478. // allocate storage for vertices and normals
  479. mesh->mNumFaces = (*cit).first;
  480. if (mesh->mNumFaces == 0) {
  481. throw DeadlyImportError("AC3D: No faces");
  482. } else if (mesh->mNumFaces > AI_MAX_ALLOC(aiFace)) {
  483. throw DeadlyImportError("AC3D: Too many faces, would run out of memory");
  484. }
  485. aiFace *faces = mesh->mFaces = new aiFace[mesh->mNumFaces];
  486. mesh->mNumVertices = (*cit).second;
  487. if (mesh->mNumVertices == 0) {
  488. throw DeadlyImportError("AC3D: No vertices");
  489. } else if (mesh->mNumVertices > AI_MAX_ALLOC(aiVector3D)) {
  490. throw DeadlyImportError("AC3D: Too many vertices, would run out of memory");
  491. }
  492. aiVector3D *vertices = mesh->mVertices = new aiVector3D[mesh->mNumVertices];
  493. unsigned int cur = 0;
  494. // allocate UV coordinates, but only if the texture name for the
  495. // surface is not empty
  496. aiVector3D *uv = nullptr;
  497. if (!object.textures.empty()) {
  498. uv = mesh->mTextureCoords[0] = new aiVector3D[mesh->mNumVertices];
  499. mesh->mNumUVComponents[0] = 2;
  500. }
  501. for (it = object.surfaces.begin(); it != end; ++it) {
  502. if (mat == (*it).mat) {
  503. const Surface &src = *it;
  504. const bool isDoubleSided = ACDoubleSidedFlag == (src.flags & ACDoubleSidedFlag);
  505. // closed polygon
  506. uint8_t type = (*it).GetType();
  507. if (type == Surface::Polygon) {
  508. aiFace &face = *faces++;
  509. face.mNumIndices = (unsigned int)src.entries.size();
  510. if (0 != face.mNumIndices) {
  511. face.mIndices = new unsigned int[face.mNumIndices];
  512. for (unsigned int i = 0; i < face.mNumIndices; ++i, ++vertices) {
  513. const Surface::SurfaceEntry &entry = src.entries[i];
  514. face.mIndices[i] = cur++;
  515. // copy vertex positions
  516. if (static_cast<unsigned>(vertices - mesh->mVertices) >= mesh->mNumVertices) {
  517. throw DeadlyImportError("AC3D: Invalid number of vertices");
  518. }
  519. *vertices = object.vertices[entry.first] + object.translation;
  520. // copy texture coordinates
  521. if (uv) {
  522. uv->x = entry.second.x;
  523. uv->y = entry.second.y;
  524. ++uv;
  525. }
  526. }
  527. if(isDoubleSided) // Need a backface?
  528. buildBacksideOfFace(faces[-1], faces, vertices, mesh->mVertices, uv, mesh->mTextureCoords[0], cur);
  529. }
  530. } else if (type == Surface::TriangleStrip) {
  531. for (unsigned int i = 0; i < (unsigned int)src.entries.size() - 2; ++i) {
  532. const Surface::SurfaceEntry &entry1 = src.entries[i];
  533. const Surface::SurfaceEntry &entry2 = src.entries[i + 1];
  534. const Surface::SurfaceEntry &entry3 = src.entries[i + 2];
  535. aiFace &face = *faces++;
  536. face.mNumIndices = 3;
  537. face.mIndices = new unsigned int[face.mNumIndices];
  538. face.mIndices[0] = cur++;
  539. face.mIndices[1] = cur++;
  540. face.mIndices[2] = cur++;
  541. if (!(i & 1)) {
  542. *vertices++ = object.vertices[entry1.first] + object.translation;
  543. if (uv) {
  544. uv->x = entry1.second.x;
  545. uv->y = entry1.second.y;
  546. ++uv;
  547. }
  548. *vertices++ = object.vertices[entry2.first] + object.translation;
  549. if (uv) {
  550. uv->x = entry2.second.x;
  551. uv->y = entry2.second.y;
  552. ++uv;
  553. }
  554. } else {
  555. *vertices++ = object.vertices[entry2.first] + object.translation;
  556. if (uv) {
  557. uv->x = entry2.second.x;
  558. uv->y = entry2.second.y;
  559. ++uv;
  560. }
  561. *vertices++ = object.vertices[entry1.first] + object.translation;
  562. if (uv) {
  563. uv->x = entry1.second.x;
  564. uv->y = entry1.second.y;
  565. ++uv;
  566. }
  567. }
  568. if (static_cast<unsigned>(vertices - mesh->mVertices) >= mesh->mNumVertices) {
  569. throw DeadlyImportError("AC3D: Invalid number of vertices");
  570. }
  571. *vertices++ = object.vertices[entry3.first] + object.translation;
  572. if (uv) {
  573. uv->x = entry3.second.x;
  574. uv->y = entry3.second.y;
  575. ++uv;
  576. }
  577. if(isDoubleSided) // Need a backface?
  578. buildBacksideOfFace(faces[-1], faces, vertices, mesh->mVertices, uv, mesh->mTextureCoords[0], cur);
  579. }
  580. } else {
  581. it2 = (*it).entries.begin();
  582. // either a closed or an unclosed line
  583. unsigned int tmp = (unsigned int)(*it).entries.size();
  584. if (Surface::OpenLine == type) --tmp;
  585. for (unsigned int m = 0; m < tmp; ++m) {
  586. aiFace &face = *faces++;
  587. face.mNumIndices = 2;
  588. face.mIndices = new unsigned int[2];
  589. face.mIndices[0] = cur++;
  590. face.mIndices[1] = cur++;
  591. // copy vertex positions
  592. if (it2 == (*it).entries.end()) {
  593. throw DeadlyImportError("AC3D: Bad line");
  594. }
  595. ai_assert((*it2).first < object.vertices.size());
  596. *vertices++ = object.vertices[(*it2).first];
  597. // copy texture coordinates
  598. if (uv) {
  599. uv->x = (*it2).second.x;
  600. uv->y = (*it2).second.y;
  601. ++uv;
  602. }
  603. if (Surface::ClosedLine == type && tmp - 1 == m) {
  604. // if this is a closed line repeat its beginning now
  605. it2 = (*it).entries.begin();
  606. } else
  607. ++it2;
  608. // second point
  609. *vertices++ = object.vertices[(*it2).first];
  610. if (uv) {
  611. uv->x = (*it2).second.x;
  612. uv->y = (*it2).second.y;
  613. ++uv;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. // Now apply catmull clark subdivision if necessary. We split meshes into
  621. // materials which is not done by AC3D during smoothing, so we need to
  622. // collect all meshes using the same material group.
  623. if (object.subDiv) {
  624. if (configEvalSubdivision) {
  625. std::unique_ptr<Subdivider> div(Subdivider::Create(Subdivider::CATMULL_CLARKE));
  626. ASSIMP_LOG_INFO("AC3D: Evaluating subdivision surface: ", object.name);
  627. MeshArray cpy(meshes.size() - oldm, nullptr);
  628. div->Subdivide(&meshes[oldm], cpy.size(), &cpy.front(), object.subDiv, true);
  629. std::copy(cpy.begin(), cpy.end(), meshes.begin() + oldm);
  630. // previous meshes are deleted vy Subdivide().
  631. } else {
  632. ASSIMP_LOG_INFO("AC3D: Letting the subdivision surface untouched due to my configuration: ", object.name);
  633. }
  634. }
  635. }
  636. }
  637. if (object.name.length())
  638. node->mName.Set(object.name);
  639. else {
  640. // generate a name depending on the type of the node
  641. switch (object.type) {
  642. case Object::Group:
  643. node->mName.length = ::ai_snprintf(node->mName.data, AI_MAXLEN, "ACGroup_%i", mGroupsCounter++);
  644. break;
  645. case Object::Poly:
  646. node->mName.length = ::ai_snprintf(node->mName.data, AI_MAXLEN, "ACPoly_%i", mPolysCounter++);
  647. break;
  648. case Object::Light:
  649. node->mName.length = ::ai_snprintf(node->mName.data, AI_MAXLEN, "ACLight_%i", mLightsCounter++);
  650. break;
  651. // there shouldn't be more than one world, but we don't care
  652. case Object::World:
  653. node->mName.length = ::ai_snprintf(node->mName.data, AI_MAXLEN, "ACWorld_%i", mWorldsCounter++);
  654. break;
  655. }
  656. }
  657. // setup the local transformation matrix of the object
  658. // compute the transformation offset to the parent node
  659. node->mTransformation = aiMatrix4x4(object.rotation);
  660. if (object.type == Object::Group || !object.numRefs) {
  661. node->mTransformation.a4 = object.translation.x;
  662. node->mTransformation.b4 = object.translation.y;
  663. node->mTransformation.c4 = object.translation.z;
  664. }
  665. // add children to the object
  666. if (object.children.size()) {
  667. node->mNumChildren = (unsigned int)object.children.size();
  668. node->mChildren = new aiNode *[node->mNumChildren];
  669. for (unsigned int i = 0; i < node->mNumChildren; ++i) {
  670. node->mChildren[i] = ConvertObjectSection(object.children[i], meshes, outMaterials, materials, node);
  671. }
  672. }
  673. return node;
  674. }
  675. // ------------------------------------------------------------------------------------------------
  676. void AC3DImporter::SetupProperties(const Importer *pImp) {
  677. configSplitBFCull = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_SEPARATE_BFCULL, 1) ? true : false;
  678. configEvalSubdivision = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_AC_EVAL_SUBDIVISION, 1) ? true : false;
  679. }
  680. // ------------------------------------------------------------------------------------------------
  681. // Imports the given file into the given scene structure.
  682. void AC3DImporter::InternReadFile(const std::string &pFile,
  683. aiScene *pScene, IOSystem *pIOHandler) {
  684. std::unique_ptr<IOStream> file(pIOHandler->Open(pFile, "rb"));
  685. // Check whether we can read from the file
  686. if (file == nullptr) {
  687. throw DeadlyImportError("Failed to open AC3D file ", pFile, ".");
  688. }
  689. // allocate storage and copy the contents of the file to a memory buffer
  690. std::vector<char> mBuffer2;
  691. TextFileToBuffer(file.get(), mBuffer2);
  692. mBuffer.data = &mBuffer2[0];
  693. mBuffer.end = &mBuffer2[0] + mBuffer2.size();
  694. mNumMeshes = 0;
  695. mLightsCounter = mPolysCounter = mWorldsCounter = mGroupsCounter = 0;
  696. if (::strncmp(mBuffer.data, "AC3D", 4)) {
  697. throw DeadlyImportError("AC3D: No valid AC3D file, magic sequence not found");
  698. }
  699. // print the file format version to the console
  700. unsigned int version = HexDigitToDecimal(mBuffer.data[4]);
  701. char msg[3];
  702. ASSIMP_itoa10(msg, 3, version);
  703. ASSIMP_LOG_INFO("AC3D file format version: ", msg);
  704. std::vector<Material> materials;
  705. materials.reserve(5);
  706. std::vector<Object> rootObjects;
  707. rootObjects.reserve(5);
  708. std::vector<aiLight *> lights;
  709. mLights = &lights;
  710. while (GetNextLine()) {
  711. if (TokenMatch(mBuffer.data, "MATERIAL", 8)) {
  712. materials.emplace_back();
  713. Material &mat = materials.back();
  714. // manually parse the material ... sscanf would use the buldin atof ...
  715. // Format: (name) rgb %f %f %f amb %f %f %f emis %f %f %f spec %f %f %f shi %d trans %f
  716. mBuffer.data = AcSkipToNextToken(mBuffer.data, mBuffer.end);
  717. if ('\"' == *mBuffer.data) {
  718. mBuffer.data = AcGetString(mBuffer.data, mBuffer.end, mat.name);
  719. mBuffer.data = AcSkipToNextToken(mBuffer.data, mBuffer.end);
  720. }
  721. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "rgb", 3, 3, &mat.rgb);
  722. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "amb", 3, 3, &mat.amb);
  723. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "emis", 4, 3, &mat.emis);
  724. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "spec", 4, 3, &mat.spec);
  725. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "shi", 3, 1, &mat.shin);
  726. mBuffer.data = TAcCheckedLoadFloatArray(mBuffer.data, mBuffer.end, "trans", 5, 1, &mat.trans);
  727. } else {
  728. LoadObjectSection(rootObjects);
  729. }
  730. }
  731. if (rootObjects.empty() || mNumMeshes == 0u) {
  732. throw DeadlyImportError("AC3D: No meshes have been loaded");
  733. }
  734. if (materials.empty()) {
  735. ASSIMP_LOG_WARN("AC3D: No material has been found");
  736. materials.emplace_back();
  737. }
  738. mNumMeshes += (mNumMeshes >> 2u) + 1;
  739. MeshArray meshes;
  740. meshes.reserve(mNumMeshes);
  741. std::vector<aiMaterial *> omaterials;
  742. materials.reserve(mNumMeshes);
  743. // generate a dummy root if there are multiple objects on the top layer
  744. Object *root = nullptr;
  745. if (1 == rootObjects.size())
  746. root = &rootObjects[0];
  747. else {
  748. root = new Object();
  749. }
  750. // now convert the imported stuff to our output data structure
  751. pScene->mRootNode = ConvertObjectSection(*root, meshes, omaterials, materials);
  752. if (1 != rootObjects.size()) {
  753. delete root;
  754. }
  755. if (::strncmp(pScene->mRootNode->mName.data, "Node", 4) == 0) {
  756. pScene->mRootNode->mName.Set("<AC3DWorld>");
  757. }
  758. // copy meshes
  759. if (meshes.empty()) {
  760. throw DeadlyImportError("An unknown error occurred during converting");
  761. }
  762. pScene->mNumMeshes = (unsigned int)meshes.size();
  763. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes];
  764. ::memcpy(pScene->mMeshes, &meshes[0], pScene->mNumMeshes * sizeof(void *));
  765. // copy materials
  766. pScene->mNumMaterials = (unsigned int)omaterials.size();
  767. pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
  768. ::memcpy(pScene->mMaterials, &omaterials[0], pScene->mNumMaterials * sizeof(void *));
  769. // copy lights
  770. pScene->mNumLights = (unsigned int)lights.size();
  771. if (!lights.empty()) {
  772. pScene->mLights = new aiLight *[lights.size()];
  773. ::memcpy(pScene->mLights, &lights[0], lights.size() * sizeof(void *));
  774. }
  775. }
  776. } // namespace Assimp
  777. #endif //!defined ASSIMP_BUILD_NO_AC_IMPORTER