COBLoader.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2021, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file COBLoader.cpp
  34. * @brief Implementation of the TrueSpace COB/SCN importer class.
  35. */
  36. #ifndef ASSIMP_BUILD_NO_COB_IMPORTER
  37. #include "AssetLib/COB/COBLoader.h"
  38. #include "AssetLib/COB/COBScene.h"
  39. #include "PostProcessing/ConvertToLHProcess.h"
  40. #include <assimp/LineSplitter.h>
  41. #include <assimp/ParsingUtils.h>
  42. #include <assimp/StreamReader.h>
  43. #include <assimp/TinyFormatter.h>
  44. #include <assimp/fast_atof.h>
  45. #include <assimp/importerdesc.h>
  46. #include <assimp/scene.h>
  47. #include <assimp/DefaultLogger.hpp>
  48. #include <assimp/IOSystem.hpp>
  49. #include <memory>
  50. using namespace Assimp;
  51. using namespace Assimp::COB;
  52. using namespace Assimp::Formatter;
  53. static const float units[] = {
  54. 1000.f,
  55. 100.f,
  56. 1.f,
  57. 0.001f,
  58. 1.f / 0.0254f,
  59. 1.f / 0.3048f,
  60. 1.f / 0.9144f,
  61. 1.f / 1609.344f
  62. };
  63. static const aiImporterDesc desc = {
  64. "TrueSpace Object Importer",
  65. "",
  66. "",
  67. "little-endian files only",
  68. aiImporterFlags_SupportTextFlavour | aiImporterFlags_SupportBinaryFlavour,
  69. 0,
  70. 0,
  71. 0,
  72. 0,
  73. "cob scn"
  74. };
  75. // ------------------------------------------------------------------------------------------------
  76. // Constructor to be privately used by Importer
  77. COBImporter::COBImporter() {
  78. // empty
  79. }
  80. // ------------------------------------------------------------------------------------------------
  81. // Destructor, private as well
  82. COBImporter::~COBImporter() {
  83. // empty
  84. }
  85. // ------------------------------------------------------------------------------------------------
  86. // Returns whether the class can handle the format of the given file.
  87. bool COBImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool checkSig) const {
  88. const std::string &extension = GetExtension(pFile);
  89. if (extension == "cob" || extension == "scn" || extension == "COB" || extension == "SCN") {
  90. return true;
  91. }
  92. else if ((!extension.length() || checkSig) && pIOHandler) {
  93. const char *tokens[] = { "Caligary" };
  94. return SearchFileHeaderForToken(pIOHandler, pFile, tokens, 1);
  95. }
  96. return false;
  97. }
  98. // ------------------------------------------------------------------------------------------------
  99. // Loader meta information
  100. const aiImporterDesc *COBImporter::GetInfo() const {
  101. return &desc;
  102. }
  103. // ------------------------------------------------------------------------------------------------
  104. // Setup configuration properties for the loader
  105. void COBImporter::SetupProperties(const Importer * /*pImp*/) {
  106. // nothing to be done for the moment
  107. }
  108. // ------------------------------------------------------------------------------------------------
  109. /*static*/ AI_WONT_RETURN void COBImporter::ThrowException(const std::string &msg) {
  110. throw DeadlyImportError("COB: ", msg);
  111. }
  112. // ------------------------------------------------------------------------------------------------
  113. // Imports the given file into the given scene structure.
  114. void COBImporter::InternReadFile(const std::string &pFile, aiScene *pScene, IOSystem *pIOHandler) {
  115. COB::Scene scene;
  116. auto file = pIOHandler->Open(pFile, "rb");
  117. if (!file) {
  118. ThrowException("Could not open " + pFile);
  119. }
  120. std::unique_ptr<StreamReaderLE> stream(new StreamReaderLE(file));
  121. // check header
  122. char head[32];
  123. stream->CopyAndAdvance(head, 32);
  124. if (strncmp(head, "Caligari ", 9) != 0) {
  125. ThrowException("Could not found magic id: `Caligari`");
  126. }
  127. ASSIMP_LOG_INFO("File format tag: ", std::string(head + 9, 6));
  128. if (head[16] != 'L') {
  129. ThrowException("File is big-endian, which is not supported");
  130. }
  131. // load data into intermediate structures
  132. if (head[15] == 'A') {
  133. ReadAsciiFile(scene, stream.get());
  134. } else {
  135. ReadBinaryFile(scene, stream.get());
  136. }
  137. if (scene.nodes.empty()) {
  138. ThrowException("No nodes loaded");
  139. }
  140. // sort faces by material indices
  141. for (std::shared_ptr<Node> &n : scene.nodes) {
  142. if (n->type == Node::TYPE_MESH) {
  143. Mesh &mesh = (Mesh &)(*n.get());
  144. for (Face &f : mesh.faces) {
  145. mesh.temp_map[f.material].push_back(&f);
  146. }
  147. }
  148. }
  149. // count meshes
  150. for (std::shared_ptr<Node> &n : scene.nodes) {
  151. if (n->type == Node::TYPE_MESH) {
  152. Mesh &mesh = (Mesh &)(*n.get());
  153. if (mesh.vertex_positions.size() && mesh.texture_coords.size()) {
  154. pScene->mNumMeshes += static_cast<unsigned int>(mesh.temp_map.size());
  155. }
  156. }
  157. }
  158. pScene->mMeshes = new aiMesh *[pScene->mNumMeshes]();
  159. pScene->mMaterials = new aiMaterial *[pScene->mNumMeshes]();
  160. pScene->mNumMeshes = 0;
  161. // count lights and cameras
  162. for (std::shared_ptr<Node> &n : scene.nodes) {
  163. if (n->type == Node::TYPE_LIGHT) {
  164. ++pScene->mNumLights;
  165. } else if (n->type == Node::TYPE_CAMERA) {
  166. ++pScene->mNumCameras;
  167. }
  168. }
  169. if (pScene->mNumLights) {
  170. pScene->mLights = new aiLight *[pScene->mNumLights]();
  171. }
  172. if (pScene->mNumCameras) {
  173. pScene->mCameras = new aiCamera *[pScene->mNumCameras]();
  174. }
  175. pScene->mNumLights = pScene->mNumCameras = 0;
  176. // resolve parents by their IDs and build the output graph
  177. std::unique_ptr<Node> root(new Group());
  178. for (size_t n = 0; n < scene.nodes.size(); ++n) {
  179. const Node &nn = *scene.nodes[n].get();
  180. if (nn.parent_id == 0) {
  181. root->temp_children.push_back(&nn);
  182. }
  183. for (size_t m = n; m < scene.nodes.size(); ++m) {
  184. const Node &mm = *scene.nodes[m].get();
  185. if (mm.parent_id == nn.id) {
  186. nn.temp_children.push_back(&mm);
  187. }
  188. }
  189. }
  190. pScene->mRootNode = BuildNodes(*root.get(), scene, pScene);
  191. //flip normals after import
  192. FlipWindingOrderProcess flip;
  193. flip.Execute(pScene);
  194. }
  195. // ------------------------------------------------------------------------------------------------
  196. void ConvertTexture(std::shared_ptr<Texture> tex, aiMaterial *out, aiTextureType type) {
  197. const aiString path(tex->path);
  198. out->AddProperty(&path, AI_MATKEY_TEXTURE(type, 0));
  199. out->AddProperty(&tex->transform, 1, AI_MATKEY_UVTRANSFORM(type, 0));
  200. }
  201. // ------------------------------------------------------------------------------------------------
  202. aiNode *COBImporter::BuildNodes(const Node &root, const Scene &scin, aiScene *fill) {
  203. aiNode *nd = new aiNode();
  204. nd->mName.Set(root.name);
  205. nd->mTransformation = root.transform;
  206. // Note to everybody believing Voodoo is appropriate here:
  207. // I know polymorphism, run as fast as you can ;-)
  208. if (Node::TYPE_MESH == root.type) {
  209. const Mesh &ndmesh = (const Mesh &)(root);
  210. if (ndmesh.vertex_positions.size() && ndmesh.texture_coords.size()) {
  211. typedef std::pair<const unsigned int, Mesh::FaceRefList> Entry;
  212. for (const Entry &reflist : ndmesh.temp_map) {
  213. { // create mesh
  214. size_t n = 0;
  215. for (Face *f : reflist.second) {
  216. n += f->indices.size();
  217. }
  218. if (!n) {
  219. continue;
  220. }
  221. aiMesh *outmesh = fill->mMeshes[fill->mNumMeshes++] = new aiMesh();
  222. ++nd->mNumMeshes;
  223. outmesh->mVertices = new aiVector3D[n];
  224. outmesh->mTextureCoords[0] = new aiVector3D[n];
  225. outmesh->mFaces = new aiFace[reflist.second.size()]();
  226. for (Face *f : reflist.second) {
  227. if (f->indices.empty()) {
  228. continue;
  229. }
  230. aiFace &fout = outmesh->mFaces[outmesh->mNumFaces++];
  231. fout.mIndices = new unsigned int[f->indices.size()];
  232. for (VertexIndex &v : f->indices) {
  233. if (v.pos_idx >= ndmesh.vertex_positions.size()) {
  234. ThrowException("Position index out of range");
  235. }
  236. if (v.uv_idx >= ndmesh.texture_coords.size()) {
  237. ThrowException("UV index out of range");
  238. }
  239. outmesh->mVertices[outmesh->mNumVertices] = ndmesh.vertex_positions[v.pos_idx];
  240. outmesh->mTextureCoords[0][outmesh->mNumVertices] = aiVector3D(
  241. ndmesh.texture_coords[v.uv_idx].x,
  242. ndmesh.texture_coords[v.uv_idx].y,
  243. 0.f);
  244. fout.mIndices[fout.mNumIndices++] = outmesh->mNumVertices++;
  245. }
  246. }
  247. outmesh->mMaterialIndex = fill->mNumMaterials;
  248. }
  249. { // create material
  250. const Material *min = nullptr;
  251. for (const Material &m : scin.materials) {
  252. if (m.parent_id == ndmesh.id && m.matnum == reflist.first) {
  253. min = &m;
  254. break;
  255. }
  256. }
  257. std::unique_ptr<const Material> defmat;
  258. if (!min) {
  259. ASSIMP_LOG_VERBOSE_DEBUG("Could not resolve material index ", reflist.first, " - creating default material for this slot");
  260. defmat.reset(min = new Material());
  261. }
  262. aiMaterial *mat = new aiMaterial();
  263. fill->mMaterials[fill->mNumMaterials++] = mat;
  264. const aiString s(format("#mat_") << fill->mNumMeshes << "_" << min->matnum);
  265. mat->AddProperty(&s, AI_MATKEY_NAME);
  266. if (int tmp = ndmesh.draw_flags & Mesh::WIRED ? 1 : 0) {
  267. mat->AddProperty(&tmp, 1, AI_MATKEY_ENABLE_WIREFRAME);
  268. }
  269. {
  270. int shader;
  271. switch (min->shader) {
  272. case Material::FLAT:
  273. shader = aiShadingMode_Gouraud;
  274. break;
  275. case Material::PHONG:
  276. shader = aiShadingMode_Phong;
  277. break;
  278. case Material::METAL:
  279. shader = aiShadingMode_CookTorrance;
  280. break;
  281. default:
  282. ASSIMP_LOG_ERROR("Unknown option.");
  283. ai_assert(false); // shouldn't be here
  284. break;
  285. }
  286. mat->AddProperty(&shader, 1, AI_MATKEY_SHADING_MODEL);
  287. if (shader != aiShadingMode_Gouraud) {
  288. mat->AddProperty(&min->exp, 1, AI_MATKEY_SHININESS);
  289. }
  290. }
  291. mat->AddProperty(&min->ior, 1, AI_MATKEY_REFRACTI);
  292. mat->AddProperty(&min->rgb, 1, AI_MATKEY_COLOR_DIFFUSE);
  293. aiColor3D c = aiColor3D(min->rgb) * min->ks;
  294. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_SPECULAR);
  295. c = aiColor3D(min->rgb) * min->ka;
  296. mat->AddProperty(&c, 1, AI_MATKEY_COLOR_AMBIENT);
  297. // convert textures if some exist.
  298. if (min->tex_color) {
  299. ConvertTexture(min->tex_color, mat, aiTextureType_DIFFUSE);
  300. }
  301. if (min->tex_env) {
  302. ConvertTexture(min->tex_env, mat, aiTextureType_UNKNOWN);
  303. }
  304. if (min->tex_bump) {
  305. ConvertTexture(min->tex_bump, mat, aiTextureType_HEIGHT);
  306. }
  307. }
  308. }
  309. }
  310. } else if (Node::TYPE_LIGHT == root.type) {
  311. const Light &ndlight = (const Light &)(root);
  312. aiLight *outlight = fill->mLights[fill->mNumLights++] = new aiLight();
  313. outlight->mName.Set(ndlight.name);
  314. outlight->mColorDiffuse = outlight->mColorAmbient = outlight->mColorSpecular = ndlight.color;
  315. outlight->mAngleOuterCone = AI_DEG_TO_RAD(ndlight.angle);
  316. outlight->mAngleInnerCone = AI_DEG_TO_RAD(ndlight.inner_angle);
  317. // XXX
  318. outlight->mType = ndlight.ltype == Light::SPOT ? aiLightSource_SPOT : aiLightSource_DIRECTIONAL;
  319. } else if (Node::TYPE_CAMERA == root.type) {
  320. const Camera &ndcam = (const Camera &)(root);
  321. aiCamera *outcam = fill->mCameras[fill->mNumCameras++] = new aiCamera();
  322. outcam->mName.Set(ndcam.name);
  323. }
  324. // add meshes
  325. if (nd->mNumMeshes) { // mMeshes must be nullptr if count is 0
  326. nd->mMeshes = new unsigned int[nd->mNumMeshes];
  327. for (unsigned int i = 0; i < nd->mNumMeshes; ++i) {
  328. nd->mMeshes[i] = fill->mNumMeshes - i - 1;
  329. }
  330. }
  331. // add children recursively
  332. nd->mChildren = new aiNode *[root.temp_children.size()]();
  333. for (const Node *n : root.temp_children) {
  334. (nd->mChildren[nd->mNumChildren++] = BuildNodes(*n, scin, fill))->mParent = nd;
  335. }
  336. return nd;
  337. }
  338. // ------------------------------------------------------------------------------------------------
  339. // Read an ASCII file into the given scene data structure
  340. void COBImporter::ReadAsciiFile(Scene &out, StreamReaderLE *stream) {
  341. ChunkInfo ci;
  342. for (LineSplitter splitter(*stream); splitter; ++splitter) {
  343. // add all chunks to be recognized here. /else ../ omitted intentionally.
  344. if (splitter.match_start("PolH ")) {
  345. ReadChunkInfo_Ascii(ci, splitter);
  346. ReadPolH_Ascii(out, splitter, ci);
  347. }
  348. if (splitter.match_start("BitM ")) {
  349. ReadChunkInfo_Ascii(ci, splitter);
  350. ReadBitM_Ascii(out, splitter, ci);
  351. }
  352. if (splitter.match_start("Mat1 ")) {
  353. ReadChunkInfo_Ascii(ci, splitter);
  354. ReadMat1_Ascii(out, splitter, ci);
  355. }
  356. if (splitter.match_start("Grou ")) {
  357. ReadChunkInfo_Ascii(ci, splitter);
  358. ReadGrou_Ascii(out, splitter, ci);
  359. }
  360. if (splitter.match_start("Lght ")) {
  361. ReadChunkInfo_Ascii(ci, splitter);
  362. ReadLght_Ascii(out, splitter, ci);
  363. }
  364. if (splitter.match_start("Came ")) {
  365. ReadChunkInfo_Ascii(ci, splitter);
  366. ReadCame_Ascii(out, splitter, ci);
  367. }
  368. if (splitter.match_start("Bone ")) {
  369. ReadChunkInfo_Ascii(ci, splitter);
  370. ReadBone_Ascii(out, splitter, ci);
  371. }
  372. if (splitter.match_start("Chan ")) {
  373. ReadChunkInfo_Ascii(ci, splitter);
  374. ReadChan_Ascii(out, splitter, ci);
  375. }
  376. if (splitter.match_start("Unit ")) {
  377. ReadChunkInfo_Ascii(ci, splitter);
  378. ReadUnit_Ascii(out, splitter, ci);
  379. }
  380. if (splitter.match_start("END ")) {
  381. // we don't need this, but I guess there is a reason this
  382. // chunk has been implemented into COB for.
  383. return;
  384. }
  385. }
  386. }
  387. // ------------------------------------------------------------------------------------------------
  388. void COBImporter::ReadChunkInfo_Ascii(ChunkInfo &out, const LineSplitter &splitter) {
  389. const char *all_tokens[8];
  390. splitter.get_tokens(all_tokens);
  391. out.version = (all_tokens[1][1] - '0') * 100 + (all_tokens[1][3] - '0') * 10 + (all_tokens[1][4] - '0');
  392. out.id = strtoul10(all_tokens[3]);
  393. out.parent_id = strtoul10(all_tokens[5]);
  394. out.size = strtol10(all_tokens[7]);
  395. }
  396. // ------------------------------------------------------------------------------------------------
  397. void COBImporter::UnsupportedChunk_Ascii(LineSplitter &splitter, const ChunkInfo &nfo, const char *name) {
  398. const std::string error = format("Encountered unsupported chunk: ") << name << " [version: " << nfo.version << ", size: " << nfo.size << "]";
  399. // we can recover if the chunk size was specified.
  400. if (nfo.size != static_cast<unsigned int>(-1)) {
  401. ASSIMP_LOG_ERROR(error);
  402. // (HACK) - our current position in the stream is the beginning of the
  403. // head line of the next chunk. That's fine, but the caller is going
  404. // to call ++ on `splitter`, which we need to swallow to avoid
  405. // missing the next line.
  406. splitter.get_stream().IncPtr(nfo.size);
  407. splitter.swallow_next_increment();
  408. } else {
  409. ThrowException(error);
  410. }
  411. }
  412. // ------------------------------------------------------------------------------------------------
  413. void COBImporter::ReadBasicNodeInfo_Ascii(Node &msh, LineSplitter &splitter, const ChunkInfo & /*nfo*/) {
  414. for (; splitter; ++splitter) {
  415. if (splitter.match_start("Name")) {
  416. msh.name = std::string(splitter[1]);
  417. // make nice names by merging the dupe count
  418. std::replace(msh.name.begin(), msh.name.end(),
  419. ',', '_');
  420. } else if (splitter.match_start("Transform")) {
  421. for (unsigned int y = 0; y < 4 && ++splitter; ++y) {
  422. const char *s = splitter->c_str();
  423. for (unsigned int x = 0; x < 4; ++x) {
  424. SkipSpaces(&s);
  425. msh.transform[y][x] = fast_atof(&s);
  426. }
  427. }
  428. // we need the transform chunk, so we won't return until we have it.
  429. return;
  430. }
  431. }
  432. }
  433. // ------------------------------------------------------------------------------------------------
  434. template <typename T>
  435. void COBImporter::ReadFloat3Tuple_Ascii(T &fill, const char **in) {
  436. const char *rgb = *in;
  437. for (unsigned int i = 0; i < 3; ++i) {
  438. SkipSpaces(&rgb);
  439. if (*rgb == ',') ++rgb;
  440. SkipSpaces(&rgb);
  441. fill[i] = fast_atof(&rgb);
  442. }
  443. *in = rgb;
  444. }
  445. // ------------------------------------------------------------------------------------------------
  446. void COBImporter::ReadMat1_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  447. if (nfo.version > 8) {
  448. return UnsupportedChunk_Ascii(splitter, nfo, "Mat1");
  449. }
  450. ++splitter;
  451. if (!splitter.match_start("mat# ")) {
  452. ASSIMP_LOG_WARN("Expected `mat#` line in `Mat1` chunk ", nfo.id);
  453. return;
  454. }
  455. out.materials.push_back(Material());
  456. Material &mat = out.materials.back();
  457. mat = nfo;
  458. mat.matnum = strtoul10(splitter[1]);
  459. ++splitter;
  460. if (!splitter.match_start("shader: ")) {
  461. ASSIMP_LOG_WARN("Expected `mat#` line in `Mat1` chunk ", nfo.id);
  462. return;
  463. }
  464. std::string shader = std::string(splitter[1]);
  465. shader = shader.substr(0, shader.find_first_of(" \t"));
  466. if (shader == "metal") {
  467. mat.shader = Material::METAL;
  468. } else if (shader == "phong") {
  469. mat.shader = Material::PHONG;
  470. } else if (shader != "flat") {
  471. ASSIMP_LOG_WARN("Unknown value for `shader` in `Mat1` chunk ", nfo.id);
  472. }
  473. ++splitter;
  474. if (!splitter.match_start("rgb ")) {
  475. ASSIMP_LOG_WARN("Expected `rgb` line in `Mat1` chunk ", nfo.id);
  476. }
  477. const char *rgb = splitter[1];
  478. ReadFloat3Tuple_Ascii(mat.rgb, &rgb);
  479. ++splitter;
  480. if (!splitter.match_start("alpha ")) {
  481. ASSIMP_LOG_WARN("Expected `alpha` line in `Mat1` chunk ", nfo.id);
  482. }
  483. const char *tokens[10];
  484. splitter.get_tokens(tokens);
  485. mat.alpha = fast_atof(tokens[1]);
  486. mat.ka = fast_atof(tokens[3]);
  487. mat.ks = fast_atof(tokens[5]);
  488. mat.exp = fast_atof(tokens[7]);
  489. mat.ior = fast_atof(tokens[9]);
  490. }
  491. // ------------------------------------------------------------------------------------------------
  492. void COBImporter::ReadUnit_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  493. if (nfo.version > 1) {
  494. return UnsupportedChunk_Ascii(splitter, nfo, "Unit");
  495. }
  496. ++splitter;
  497. if (!splitter.match_start("Units ")) {
  498. ASSIMP_LOG_WARN("Expected `Units` line in `Unit` chunk ", nfo.id);
  499. return;
  500. }
  501. // parent chunks preceede their childs, so we should have the
  502. // corresponding chunk already.
  503. for (std::shared_ptr<Node> &nd : out.nodes) {
  504. if (nd->id == nfo.parent_id) {
  505. const unsigned int t = strtoul10(splitter[1]);
  506. nd->unit_scale = t >= sizeof(units) / sizeof(units[0]) ? (
  507. ASSIMP_LOG_WARN(t, " is not a valid value for `Units` attribute in `Unit chunk` ", nfo.id), 1.f) :
  508. units[t];
  509. return;
  510. }
  511. }
  512. ASSIMP_LOG_WARN("`Unit` chunk ", nfo.id, " is a child of ", nfo.parent_id, " which does not exist");
  513. }
  514. // ------------------------------------------------------------------------------------------------
  515. void COBImporter::ReadChan_Ascii(Scene & /*out*/, LineSplitter &splitter, const ChunkInfo &nfo) {
  516. if (nfo.version > 8) {
  517. return UnsupportedChunk_Ascii(splitter, nfo, "Chan");
  518. }
  519. }
  520. // ------------------------------------------------------------------------------------------------
  521. void COBImporter::ReadLght_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  522. if (nfo.version > 8) {
  523. return UnsupportedChunk_Ascii(splitter, nfo, "Lght");
  524. }
  525. out.nodes.push_back(std::shared_ptr<Light>(new Light()));
  526. Light &msh = (Light &)(*out.nodes.back().get());
  527. msh = nfo;
  528. ReadBasicNodeInfo_Ascii(msh, ++splitter, nfo);
  529. if (splitter.match_start("Infinite ")) {
  530. msh.ltype = Light::INFINITE;
  531. } else if (splitter.match_start("Local ")) {
  532. msh.ltype = Light::LOCAL;
  533. } else if (splitter.match_start("Spot ")) {
  534. msh.ltype = Light::SPOT;
  535. } else {
  536. ASSIMP_LOG_WARN("Unknown kind of light source in `Lght` chunk ", nfo.id, " : ", *splitter);
  537. msh.ltype = Light::SPOT;
  538. }
  539. ++splitter;
  540. if (!splitter.match_start("color ")) {
  541. ASSIMP_LOG_WARN("Expected `color` line in `Lght` chunk ", nfo.id);
  542. }
  543. const char *rgb = splitter[1];
  544. ReadFloat3Tuple_Ascii(msh.color, &rgb);
  545. SkipSpaces(&rgb);
  546. if (strncmp(rgb, "cone angle", 10) != 0) {
  547. ASSIMP_LOG_WARN("Expected `cone angle` entity in `color` line in `Lght` chunk ", nfo.id);
  548. }
  549. SkipSpaces(rgb + 10, &rgb);
  550. msh.angle = fast_atof(&rgb);
  551. SkipSpaces(&rgb);
  552. if (strncmp(rgb, "inner angle", 11) != 0) {
  553. ASSIMP_LOG_WARN("Expected `inner angle` entity in `color` line in `Lght` chunk ", nfo.id);
  554. }
  555. SkipSpaces(rgb + 11, &rgb);
  556. msh.inner_angle = fast_atof(&rgb);
  557. // skip the rest for we can't handle this kind of physically-based lighting information.
  558. }
  559. // ------------------------------------------------------------------------------------------------
  560. void COBImporter::ReadCame_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  561. if (nfo.version > 2) {
  562. return UnsupportedChunk_Ascii(splitter, nfo, "Came");
  563. }
  564. out.nodes.push_back(std::shared_ptr<Camera>(new Camera()));
  565. Camera &msh = (Camera &)(*out.nodes.back().get());
  566. msh = nfo;
  567. ReadBasicNodeInfo_Ascii(msh, ++splitter, nfo);
  568. // skip the next line, we don't know this differenciation between a
  569. // standard camera and a panoramic camera.
  570. ++splitter;
  571. }
  572. // ------------------------------------------------------------------------------------------------
  573. void COBImporter::ReadBone_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  574. if (nfo.version > 5) {
  575. return UnsupportedChunk_Ascii(splitter, nfo, "Bone");
  576. }
  577. out.nodes.push_back(std::shared_ptr<Bone>(new Bone()));
  578. Bone &msh = (Bone &)(*out.nodes.back().get());
  579. msh = nfo;
  580. ReadBasicNodeInfo_Ascii(msh, ++splitter, nfo);
  581. // TODO
  582. }
  583. // ------------------------------------------------------------------------------------------------
  584. void COBImporter::ReadGrou_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  585. if (nfo.version > 1) {
  586. return UnsupportedChunk_Ascii(splitter, nfo, "Grou");
  587. }
  588. out.nodes.push_back(std::shared_ptr<Group>(new Group()));
  589. Group &msh = (Group &)(*out.nodes.back().get());
  590. msh = nfo;
  591. ReadBasicNodeInfo_Ascii(msh, ++splitter, nfo);
  592. }
  593. // ------------------------------------------------------------------------------------------------
  594. void COBImporter::ReadPolH_Ascii(Scene &out, LineSplitter &splitter, const ChunkInfo &nfo) {
  595. if (nfo.version > 8) {
  596. return UnsupportedChunk_Ascii(splitter, nfo, "PolH");
  597. }
  598. out.nodes.push_back(std::shared_ptr<Mesh>(new Mesh()));
  599. Mesh &msh = (Mesh &)(*out.nodes.back().get());
  600. msh = nfo;
  601. ReadBasicNodeInfo_Ascii(msh, ++splitter, nfo);
  602. // the chunk has a fixed order of components, but some are not interesting of us so
  603. // we're just looking for keywords in arbitrary order. The end of the chunk is
  604. // either the last `Face` or the `DrawFlags` attribute, depending on the format ver.
  605. for (; splitter; ++splitter) {
  606. if (splitter.match_start("World Vertices")) {
  607. const unsigned int cnt = strtoul10(splitter[2]);
  608. msh.vertex_positions.resize(cnt);
  609. for (unsigned int cur = 0; cur < cnt && ++splitter; ++cur) {
  610. const char *s = splitter->c_str();
  611. aiVector3D &v = msh.vertex_positions[cur];
  612. SkipSpaces(&s);
  613. v.x = fast_atof(&s);
  614. SkipSpaces(&s);
  615. v.y = fast_atof(&s);
  616. SkipSpaces(&s);
  617. v.z = fast_atof(&s);
  618. }
  619. } else if (splitter.match_start("Texture Vertices")) {
  620. const unsigned int cnt = strtoul10(splitter[2]);
  621. msh.texture_coords.resize(cnt);
  622. for (unsigned int cur = 0; cur < cnt && ++splitter; ++cur) {
  623. const char *s = splitter->c_str();
  624. aiVector2D &v = msh.texture_coords[cur];
  625. SkipSpaces(&s);
  626. v.x = fast_atof(&s);
  627. SkipSpaces(&s);
  628. v.y = fast_atof(&s);
  629. }
  630. } else if (splitter.match_start("Faces")) {
  631. const unsigned int cnt = strtoul10(splitter[1]);
  632. msh.faces.reserve(cnt);
  633. for (unsigned int cur = 0; cur < cnt && ++splitter; ++cur) {
  634. if (splitter.match_start("Hole")) {
  635. ASSIMP_LOG_WARN("Skipping unsupported `Hole` line");
  636. continue;
  637. }
  638. if (!splitter.match_start("Face")) {
  639. ThrowException("Expected Face line");
  640. }
  641. msh.faces.push_back(Face());
  642. Face &face = msh.faces.back();
  643. face.indices.resize(strtoul10(splitter[2]));
  644. face.flags = strtoul10(splitter[4]);
  645. face.material = strtoul10(splitter[6]);
  646. const char *s = (++splitter)->c_str();
  647. for (size_t i = 0; i < face.indices.size(); ++i) {
  648. if (!SkipSpaces(&s)) {
  649. ThrowException("Expected EOL token in Face entry");
  650. }
  651. if ('<' != *s++) {
  652. ThrowException("Expected < token in Face entry");
  653. }
  654. face.indices[i].pos_idx = strtoul10(s, &s);
  655. if (',' != *s++) {
  656. ThrowException("Expected , token in Face entry");
  657. }
  658. face.indices[i].uv_idx = strtoul10(s, &s);
  659. if ('>' != *s++) {
  660. ThrowException("Expected < token in Face entry");
  661. }
  662. }
  663. }
  664. if (nfo.version <= 4) {
  665. break;
  666. }
  667. } else if (splitter.match_start("DrawFlags")) {
  668. msh.draw_flags = strtoul10(splitter[1]);
  669. break;
  670. }
  671. }
  672. }
  673. // ------------------------------------------------------------------------------------------------
  674. void COBImporter::ReadBitM_Ascii(Scene & /*out*/, LineSplitter &splitter, const ChunkInfo &nfo) {
  675. if (nfo.version > 1) {
  676. return UnsupportedChunk_Ascii(splitter, nfo, "BitM");
  677. }
  678. const unsigned int head = strtoul10((++splitter)[1]);
  679. if (head != sizeof(Bitmap::BitmapHeader)) {
  680. ASSIMP_LOG_WARN("Unexpected ThumbNailHdrSize, skipping this chunk");
  681. return;
  682. }
  683. }
  684. // ------------------------------------------------------------------------------------------------
  685. void COBImporter::ReadString_Binary(std::string &out, StreamReaderLE &reader) {
  686. out.resize(reader.GetI2());
  687. for (char &c : out) {
  688. c = reader.GetI1();
  689. }
  690. }
  691. // ------------------------------------------------------------------------------------------------
  692. void COBImporter::ReadBasicNodeInfo_Binary(Node &msh, StreamReaderLE &reader, const ChunkInfo & /*nfo*/) {
  693. const unsigned int dupes = reader.GetI2();
  694. ReadString_Binary(msh.name, reader);
  695. msh.name = format(msh.name) << '_' << dupes;
  696. // skip local axes for the moment
  697. reader.IncPtr(48);
  698. msh.transform = aiMatrix4x4();
  699. for (unsigned int y = 0; y < 3; ++y) {
  700. for (unsigned int x = 0; x < 4; ++x) {
  701. msh.transform[y][x] = reader.GetF4();
  702. }
  703. }
  704. }
  705. // ------------------------------------------------------------------------------------------------
  706. void COBImporter::UnsupportedChunk_Binary(StreamReaderLE &reader, const ChunkInfo &nfo, const char *name) {
  707. const std::string error = format("Encountered unsupported chunk: ") << name << " [version: " << nfo.version << ", size: " << nfo.size << "]";
  708. // we can recover if the chunk size was specified.
  709. if (nfo.size != static_cast<unsigned int>(-1)) {
  710. ASSIMP_LOG_ERROR(error);
  711. reader.IncPtr(nfo.size);
  712. } else
  713. ThrowException(error);
  714. }
  715. // ------------------------------------------------------------------------------------------------
  716. // tiny utility guard to aid me at staying within chunk boundaries.
  717. class chunk_guard {
  718. public:
  719. chunk_guard(const COB::ChunkInfo &nfo, StreamReaderLE &reader) :
  720. nfo(nfo), reader(reader), cur(reader.GetCurrentPos()) {
  721. // empty
  722. }
  723. ~chunk_guard() {
  724. // don't do anything if the size is not given
  725. if (nfo.size != static_cast<unsigned int>(-1)) {
  726. try {
  727. reader.IncPtr(static_cast<int>(nfo.size) - reader.GetCurrentPos() + cur);
  728. } catch (const DeadlyImportError &) {
  729. // out of limit so correct the value
  730. reader.IncPtr(reader.GetReadLimit());
  731. }
  732. }
  733. }
  734. private:
  735. const COB::ChunkInfo &nfo;
  736. StreamReaderLE &reader;
  737. long cur;
  738. };
  739. // ------------------------------------------------------------------------------------------------
  740. void COBImporter::ReadBinaryFile(Scene &out, StreamReaderLE *reader) {
  741. if (nullptr == reader) {
  742. return;
  743. }
  744. while (1) {
  745. std::string type;
  746. type += reader->GetI1();
  747. type += reader->GetI1();
  748. type += reader->GetI1();
  749. type += reader->GetI1();
  750. ChunkInfo nfo;
  751. nfo.version = reader->GetI2() * 10;
  752. nfo.version += reader->GetI2();
  753. nfo.id = reader->GetI4();
  754. nfo.parent_id = reader->GetI4();
  755. nfo.size = reader->GetI4();
  756. if (type == "PolH") {
  757. ReadPolH_Binary(out, *reader, nfo);
  758. } else if (type == "BitM") {
  759. ReadBitM_Binary(out, *reader, nfo);
  760. } else if (type == "Grou") {
  761. ReadGrou_Binary(out, *reader, nfo);
  762. } else if (type == "Lght") {
  763. ReadLght_Binary(out, *reader, nfo);
  764. } else if (type == "Came") {
  765. ReadCame_Binary(out, *reader, nfo);
  766. } else if (type == "Mat1") {
  767. ReadMat1_Binary(out, *reader, nfo);
  768. } else if (type == "Unit") {
  769. ReadUnit_Binary(out, *reader, nfo);
  770. } else if (type == "OLay") {
  771. // ignore layer index silently.
  772. if (nfo.size != static_cast<unsigned int>(-1)) {
  773. reader->IncPtr(nfo.size);
  774. } else
  775. return UnsupportedChunk_Binary(*reader, nfo, type.c_str());
  776. } else if (type == "END ") {
  777. return;
  778. } else {
  779. UnsupportedChunk_Binary(*reader, nfo, type.c_str());
  780. }
  781. }
  782. }
  783. // ------------------------------------------------------------------------------------------------
  784. void COBImporter::ReadPolH_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  785. if (nfo.version > 8) {
  786. return UnsupportedChunk_Binary(reader, nfo, "PolH");
  787. }
  788. const chunk_guard cn(nfo, reader);
  789. out.nodes.push_back(std::shared_ptr<Mesh>(new Mesh()));
  790. Mesh &msh = (Mesh &)(*out.nodes.back().get());
  791. msh = nfo;
  792. ReadBasicNodeInfo_Binary(msh, reader, nfo);
  793. msh.vertex_positions.resize(reader.GetI4());
  794. for (aiVector3D &v : msh.vertex_positions) {
  795. v.x = reader.GetF4();
  796. v.y = reader.GetF4();
  797. v.z = reader.GetF4();
  798. }
  799. msh.texture_coords.resize(reader.GetI4());
  800. for (aiVector2D &v : msh.texture_coords) {
  801. v.x = reader.GetF4();
  802. v.y = reader.GetF4();
  803. }
  804. const size_t numf = reader.GetI4();
  805. msh.faces.reserve(numf);
  806. for (size_t i = 0; i < numf; ++i) {
  807. // XXX backface culling flag is 0x10 in flags
  808. // hole?
  809. bool hole = (reader.GetI1() & 0x08) != 0;
  810. if (hole) {
  811. // XXX Basically this should just work fine - then triangulator
  812. // should output properly triangulated data even for polygons
  813. // with holes. Test data specific to COB is needed to confirm it.
  814. if (msh.faces.empty()) {
  815. ThrowException(format("A hole is the first entity in the `PolH` chunk with id ") << nfo.id);
  816. }
  817. } else
  818. msh.faces.push_back(Face());
  819. Face &f = msh.faces.back();
  820. const size_t num = reader.GetI2();
  821. f.indices.reserve(f.indices.size() + num);
  822. if (!hole) {
  823. f.material = reader.GetI2();
  824. f.flags = 0;
  825. }
  826. for (size_t x = 0; x < num; ++x) {
  827. f.indices.push_back(VertexIndex());
  828. VertexIndex &v = f.indices.back();
  829. v.pos_idx = reader.GetI4();
  830. v.uv_idx = reader.GetI4();
  831. }
  832. if (hole) {
  833. std::reverse(f.indices.rbegin(), f.indices.rbegin() + num);
  834. }
  835. }
  836. if (nfo.version > 4) {
  837. msh.draw_flags = reader.GetI4();
  838. }
  839. nfo.version > 5 && nfo.version < 8 ? reader.GetI4() : 0;
  840. }
  841. // ------------------------------------------------------------------------------------------------
  842. void COBImporter::ReadBitM_Binary(COB::Scene & /*out*/, StreamReaderLE &reader, const ChunkInfo &nfo) {
  843. if (nfo.version > 1) {
  844. return UnsupportedChunk_Binary(reader, nfo, "BitM");
  845. }
  846. const chunk_guard cn(nfo, reader);
  847. const uint32_t len = reader.GetI4();
  848. reader.IncPtr(len);
  849. reader.GetI4();
  850. reader.IncPtr(reader.GetI4());
  851. }
  852. // ------------------------------------------------------------------------------------------------
  853. void COBImporter::ReadMat1_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  854. if (nfo.version > 8) {
  855. return UnsupportedChunk_Binary(reader, nfo, "Mat1");
  856. }
  857. const chunk_guard cn(nfo, reader);
  858. out.materials.push_back(Material());
  859. Material &mat = out.materials.back();
  860. mat = nfo;
  861. mat.matnum = reader.GetI2();
  862. switch (reader.GetI1()) {
  863. case 'f':
  864. mat.type = Material::FLAT;
  865. break;
  866. case 'p':
  867. mat.type = Material::PHONG;
  868. break;
  869. case 'm':
  870. mat.type = Material::METAL;
  871. break;
  872. default:
  873. ASSIMP_LOG_ERROR("Unrecognized shader type in `Mat1` chunk with id ", nfo.id);
  874. mat.type = Material::FLAT;
  875. }
  876. switch (reader.GetI1()) {
  877. case 'f':
  878. mat.autofacet = Material::FACETED;
  879. break;
  880. case 'a':
  881. mat.autofacet = Material::AUTOFACETED;
  882. break;
  883. case 's':
  884. mat.autofacet = Material::SMOOTH;
  885. break;
  886. default:
  887. ASSIMP_LOG_ERROR("Unrecognized faceting mode in `Mat1` chunk with id ", nfo.id);
  888. mat.autofacet = Material::FACETED;
  889. }
  890. mat.autofacet_angle = static_cast<float>(reader.GetI1());
  891. mat.rgb.r = reader.GetF4();
  892. mat.rgb.g = reader.GetF4();
  893. mat.rgb.b = reader.GetF4();
  894. mat.alpha = reader.GetF4();
  895. mat.ka = reader.GetF4();
  896. mat.ks = reader.GetF4();
  897. mat.exp = reader.GetF4();
  898. mat.ior = reader.GetF4();
  899. char id[2];
  900. id[0] = reader.GetI1(), id[1] = reader.GetI1();
  901. if (id[0] == 'e' && id[1] == ':') {
  902. mat.tex_env.reset(new Texture());
  903. reader.GetI1();
  904. ReadString_Binary(mat.tex_env->path, reader);
  905. // advance to next texture-id
  906. id[0] = reader.GetI1(), id[1] = reader.GetI1();
  907. }
  908. if (id[0] == 't' && id[1] == ':') {
  909. mat.tex_color.reset(new Texture());
  910. reader.GetI1();
  911. ReadString_Binary(mat.tex_color->path, reader);
  912. mat.tex_color->transform.mTranslation.x = reader.GetF4();
  913. mat.tex_color->transform.mTranslation.y = reader.GetF4();
  914. mat.tex_color->transform.mScaling.x = reader.GetF4();
  915. mat.tex_color->transform.mScaling.y = reader.GetF4();
  916. // advance to next texture-id
  917. id[0] = reader.GetI1(), id[1] = reader.GetI1();
  918. }
  919. if (id[0] == 'b' && id[1] == ':') {
  920. mat.tex_bump.reset(new Texture());
  921. reader.GetI1();
  922. ReadString_Binary(mat.tex_bump->path, reader);
  923. mat.tex_bump->transform.mTranslation.x = reader.GetF4();
  924. mat.tex_bump->transform.mTranslation.y = reader.GetF4();
  925. mat.tex_bump->transform.mScaling.x = reader.GetF4();
  926. mat.tex_bump->transform.mScaling.y = reader.GetF4();
  927. // skip amplitude for I don't know its purpose.
  928. reader.GetF4();
  929. }
  930. reader.IncPtr(-2);
  931. }
  932. // ------------------------------------------------------------------------------------------------
  933. void COBImporter::ReadCame_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  934. if (nfo.version > 2) {
  935. return UnsupportedChunk_Binary(reader, nfo, "Came");
  936. }
  937. const chunk_guard cn(nfo, reader);
  938. out.nodes.push_back(std::shared_ptr<Camera>(new Camera()));
  939. Camera &msh = (Camera &)(*out.nodes.back().get());
  940. msh = nfo;
  941. ReadBasicNodeInfo_Binary(msh, reader, nfo);
  942. // the rest is not interesting for us, so we skip over it.
  943. if (nfo.version > 1) {
  944. if (reader.GetI2() == 512) {
  945. reader.IncPtr(42);
  946. }
  947. }
  948. }
  949. // ------------------------------------------------------------------------------------------------
  950. void COBImporter::ReadLght_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  951. if (nfo.version > 2) {
  952. return UnsupportedChunk_Binary(reader, nfo, "Lght");
  953. }
  954. const chunk_guard cn(nfo, reader);
  955. out.nodes.push_back(std::shared_ptr<Light>(new Light()));
  956. Light &msh = (Light &)(*out.nodes.back().get());
  957. msh = nfo;
  958. ReadBasicNodeInfo_Binary(msh, reader, nfo);
  959. }
  960. // ------------------------------------------------------------------------------------------------
  961. void COBImporter::ReadGrou_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  962. if (nfo.version > 2) {
  963. return UnsupportedChunk_Binary(reader, nfo, "Grou");
  964. }
  965. const chunk_guard cn(nfo, reader);
  966. out.nodes.push_back(std::make_shared<Group>());
  967. Group &msh = (Group &)(*out.nodes.back().get());
  968. msh = nfo;
  969. ReadBasicNodeInfo_Binary(msh, reader, nfo);
  970. }
  971. // ------------------------------------------------------------------------------------------------
  972. void COBImporter::ReadUnit_Binary(COB::Scene &out, StreamReaderLE &reader, const ChunkInfo &nfo) {
  973. if (nfo.version > 1) {
  974. return UnsupportedChunk_Binary(reader, nfo, "Unit");
  975. }
  976. const chunk_guard cn(nfo, reader);
  977. // parent chunks preceede their childs, so we should have the
  978. // corresponding chunk already.
  979. for (std::shared_ptr<Node> &nd : out.nodes) {
  980. if (nd->id == nfo.parent_id) {
  981. const unsigned int t = reader.GetI2();
  982. nd->unit_scale = t >= sizeof(units) / sizeof(units[0]) ? (
  983. ASSIMP_LOG_WARN(t, " is not a valid value for `Units` attribute in `Unit chunk` ", nfo.id), 1.f) :
  984. units[t];
  985. return;
  986. }
  987. }
  988. ASSIMP_LOG_WARN("`Unit` chunk ", nfo.id, " is a child of ", nfo.parent_id, " which does not exist");
  989. }
  990. #endif // ASSIMP_BUILD_NO_COB_IMPORTER