json_exporter.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  1. /*
  2. Assimp2Json
  3. Copyright (c) 2011, Alexander C. Gessler
  4. Licensed under a 3-clause BSD license. See the LICENSE file for more information.
  5. */
  6. #ifndef ASSIMP_BUILD_NO_EXPORT
  7. #ifndef ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  8. #include <assimp/Importer.hpp>
  9. #include <assimp/Exporter.hpp>
  10. #include <assimp/IOStream.hpp>
  11. #include <assimp/IOSystem.hpp>
  12. #include <assimp/scene.h>
  13. #include <sstream>
  14. #include <limits>
  15. #include <cassert>
  16. #include <memory>
  17. #define CURRENT_FORMAT_VERSION 100
  18. // grab scoped_ptr from assimp to avoid a dependency on boost.
  19. //#include <assimp/../../code/BoostWorkaround/boost/scoped_ptr.hpp>
  20. #include "mesh_splitter.h"
  21. extern "C" {
  22. #include "cencode.h"
  23. }
  24. namespace {
  25. void Assimp2Json(const char*, Assimp::IOSystem*, const aiScene*, const Assimp::ExportProperties*);
  26. }
  27. Assimp::Exporter::ExportFormatEntry Assimp2Json_desc = Assimp::Exporter::ExportFormatEntry(
  28. "assimp.json",
  29. "Plain JSON representation of the Assimp scene data structure",
  30. "assimp.json",
  31. Assimp2Json,
  32. 0u);
  33. namespace {
  34. // small utility class to simplify serializing the aiScene to Json
  35. class JSONWriter
  36. {
  37. public:
  38. enum {
  39. Flag_DoNotIndent = 0x1,
  40. Flag_WriteSpecialFloats = 0x2,
  41. };
  42. public:
  43. JSONWriter(Assimp::IOStream& out, unsigned int flags = 0u)
  44. : out(out)
  45. , first()
  46. , flags(flags)
  47. {
  48. // make sure that all formatting happens using the standard, C locale and not the user's current locale
  49. buff.imbue( std::locale("C") );
  50. }
  51. ~JSONWriter()
  52. {
  53. Flush();
  54. }
  55. public:
  56. void Flush() {
  57. const std::string s = buff.str();
  58. out.Write(s.c_str(),s.length(),1);
  59. buff.clear();
  60. }
  61. void PushIndent() {
  62. indent += '\t';
  63. }
  64. void PopIndent() {
  65. indent.erase(indent.end()-1);
  66. }
  67. void Key(const std::string& name) {
  68. AddIndentation();
  69. Delimit();
  70. buff << '\"'+name+"\": ";
  71. }
  72. template<typename Literal>
  73. void Element(const Literal& name) {
  74. AddIndentation();
  75. Delimit();
  76. LiteralToString(buff, name) << '\n';
  77. }
  78. template<typename Literal>
  79. void SimpleValue(const Literal& s) {
  80. LiteralToString(buff, s) << '\n';
  81. }
  82. void SimpleValue(const void* buffer, size_t len) {
  83. base64_encodestate s;
  84. base64_init_encodestate(&s);
  85. char* const out = new char[std::max(len*2, static_cast<size_t>(16u))];
  86. const int n = base64_encode_block(reinterpret_cast<const char*>( buffer ), static_cast<int>( len ),out,&s);
  87. out[n+base64_encode_blockend(out + n,&s)] = '\0';
  88. // base64 encoding may add newlines, but JSON strings may not contain 'real' newlines
  89. // (only escaped ones). Remove any newlines in out.
  90. for(char* cur = out; *cur; ++cur) {
  91. if(*cur == '\n') {
  92. *cur = ' ';
  93. }
  94. }
  95. buff << '\"' << out << "\"\n";
  96. delete[] out;
  97. }
  98. void StartObj(bool is_element = false) {
  99. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  100. if(is_element) {
  101. AddIndentation();
  102. if(!first) {
  103. buff << ',';
  104. }
  105. }
  106. first = true;
  107. buff << "{\n";
  108. PushIndent();
  109. }
  110. void EndObj() {
  111. PopIndent();
  112. AddIndentation();
  113. first = false;
  114. buff << "}\n";
  115. }
  116. void StartArray(bool is_element = false) {
  117. // if this appears as a plain array element, we need to insert a delimiter and we should also indent it
  118. if(is_element) {
  119. AddIndentation();
  120. if(!first) {
  121. buff << ',';
  122. }
  123. }
  124. first = true;
  125. buff << "[\n";
  126. PushIndent();
  127. }
  128. void EndArray() {
  129. PopIndent();
  130. AddIndentation();
  131. buff << "]\n";
  132. first = false;
  133. }
  134. void AddIndentation() {
  135. if(!(flags & Flag_DoNotIndent)) {
  136. buff << indent;
  137. }
  138. }
  139. void Delimit() {
  140. if(!first) {
  141. buff << ',';
  142. }
  143. else {
  144. buff << ' ';
  145. first = false;
  146. }
  147. }
  148. private:
  149. template<typename Literal>
  150. std::stringstream& LiteralToString(std::stringstream& stream, const Literal& s) {
  151. stream << s;
  152. return stream;
  153. }
  154. std::stringstream& LiteralToString(std::stringstream& stream, const aiString& s) {
  155. std::string t;
  156. // escape backslashes and single quotes, both would render the JSON invalid if left as is
  157. t.reserve(s.length);
  158. for(size_t i = 0; i < s.length; ++i) {
  159. if (s.data[i] == '\\' || s.data[i] == '\'' || s.data[i] == '\"') {
  160. t.push_back('\\');
  161. }
  162. t.push_back(s.data[i]);
  163. }
  164. stream << "\"";
  165. stream << t;
  166. stream << "\"";
  167. return stream;
  168. }
  169. std::stringstream& LiteralToString(std::stringstream& stream, float f) {
  170. if (!std::numeric_limits<float>::is_iec559) {
  171. // on a non IEEE-754 platform, we make no assumptions about the representation or existence
  172. // of special floating-point numbers.
  173. stream << f;
  174. return stream;
  175. }
  176. // JSON does not support writing Inf/Nan
  177. // [RFC 4672: "Numeric values that cannot be represented as sequences of digits
  178. // (such as Infinity and NaN) are not permitted."]
  179. // Nevertheless, many parsers will accept the special keywords Infinity, -Infinity and NaN
  180. if (std::numeric_limits<float>::infinity() == fabs(f)) {
  181. if (flags & Flag_WriteSpecialFloats) {
  182. stream << (f < 0 ? "\"-" : "\"") + std::string( "Infinity\"" );
  183. return stream;
  184. }
  185. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  186. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  187. stream << "0.0";
  188. return stream;
  189. }
  190. // f!=f is the most reliable test for NaNs that I know of
  191. else if (f != f) {
  192. if (flags & Flag_WriteSpecialFloats) {
  193. stream << "\"NaN\"";
  194. return stream;
  195. }
  196. // we should print this warning, but we can't - this is called from within a generic assimp exporter, we cannot use cerr
  197. // std::cerr << "warning: cannot represent infinite number literal, substituting 0 instead (use -i flag to enforce Infinity/NaN)" << std::endl;
  198. stream << "0.0";
  199. return stream;
  200. }
  201. stream << f;
  202. return stream;
  203. }
  204. private:
  205. Assimp::IOStream& out;
  206. std::string indent, newline;
  207. std::stringstream buff;
  208. bool first;
  209. unsigned int flags;
  210. };
  211. void Write(JSONWriter& out, const aiVector3D& ai, bool is_elem = true)
  212. {
  213. out.StartArray(is_elem);
  214. out.Element(ai.x);
  215. out.Element(ai.y);
  216. out.Element(ai.z);
  217. out.EndArray();
  218. }
  219. void Write(JSONWriter& out, const aiQuaternion& ai, bool is_elem = true)
  220. {
  221. out.StartArray(is_elem);
  222. out.Element(ai.w);
  223. out.Element(ai.x);
  224. out.Element(ai.y);
  225. out.Element(ai.z);
  226. out.EndArray();
  227. }
  228. void Write(JSONWriter& out, const aiColor3D& ai, bool is_elem = true)
  229. {
  230. out.StartArray(is_elem);
  231. out.Element(ai.r);
  232. out.Element(ai.g);
  233. out.Element(ai.b);
  234. out.EndArray();
  235. }
  236. void Write(JSONWriter& out, const aiMatrix4x4& ai, bool is_elem = true)
  237. {
  238. out.StartArray(is_elem);
  239. for(unsigned int x = 0; x < 4; ++x) {
  240. for(unsigned int y = 0; y < 4; ++y) {
  241. out.Element(ai[x][y]);
  242. }
  243. }
  244. out.EndArray();
  245. }
  246. void Write(JSONWriter& out, const aiBone& ai, bool is_elem = true)
  247. {
  248. out.StartObj(is_elem);
  249. out.Key("name");
  250. out.SimpleValue(ai.mName);
  251. out.Key("offsetmatrix");
  252. Write(out,ai.mOffsetMatrix,false);
  253. out.Key("weights");
  254. out.StartArray();
  255. for(unsigned int i = 0; i < ai.mNumWeights; ++i) {
  256. out.StartArray(true);
  257. out.Element(ai.mWeights[i].mVertexId);
  258. out.Element(ai.mWeights[i].mWeight);
  259. out.EndArray();
  260. }
  261. out.EndArray();
  262. out.EndObj();
  263. }
  264. void Write(JSONWriter& out, const aiFace& ai, bool is_elem = true)
  265. {
  266. out.StartArray(is_elem);
  267. for(unsigned int i = 0; i < ai.mNumIndices; ++i) {
  268. out.Element(ai.mIndices[i]);
  269. }
  270. out.EndArray();
  271. }
  272. void Write(JSONWriter& out, const aiMesh& ai, bool is_elem = true)
  273. {
  274. out.StartObj(is_elem);
  275. out.Key("name");
  276. out.SimpleValue(ai.mName);
  277. out.Key("materialindex");
  278. out.SimpleValue(ai.mMaterialIndex);
  279. out.Key("primitivetypes");
  280. out.SimpleValue(ai.mPrimitiveTypes);
  281. out.Key("vertices");
  282. out.StartArray();
  283. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  284. out.Element(ai.mVertices[i].x);
  285. out.Element(ai.mVertices[i].y);
  286. out.Element(ai.mVertices[i].z);
  287. }
  288. out.EndArray();
  289. if(ai.HasNormals()) {
  290. out.Key("normals");
  291. out.StartArray();
  292. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  293. out.Element(ai.mNormals[i].x);
  294. out.Element(ai.mNormals[i].y);
  295. out.Element(ai.mNormals[i].z);
  296. }
  297. out.EndArray();
  298. }
  299. if(ai.HasTangentsAndBitangents()) {
  300. out.Key("tangents");
  301. out.StartArray();
  302. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  303. out.Element(ai.mTangents[i].x);
  304. out.Element(ai.mTangents[i].y);
  305. out.Element(ai.mTangents[i].z);
  306. }
  307. out.EndArray();
  308. out.Key("bitangents");
  309. out.StartArray();
  310. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  311. out.Element(ai.mBitangents[i].x);
  312. out.Element(ai.mBitangents[i].y);
  313. out.Element(ai.mBitangents[i].z);
  314. }
  315. out.EndArray();
  316. }
  317. if(ai.GetNumUVChannels()) {
  318. out.Key("numuvcomponents");
  319. out.StartArray();
  320. for(unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  321. out.Element(ai.mNumUVComponents[n]);
  322. }
  323. out.EndArray();
  324. out.Key("texturecoords");
  325. out.StartArray();
  326. for(unsigned int n = 0; n < ai.GetNumUVChannels(); ++n) {
  327. const unsigned int numc = ai.mNumUVComponents[n] ? ai.mNumUVComponents[n] : 2;
  328. out.StartArray(true);
  329. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  330. for(unsigned int c = 0; c < numc; ++c) {
  331. out.Element(ai.mTextureCoords[n][i][c]);
  332. }
  333. }
  334. out.EndArray();
  335. }
  336. out.EndArray();
  337. }
  338. if(ai.GetNumColorChannels()) {
  339. out.Key("colors");
  340. out.StartArray();
  341. for(unsigned int n = 0; n < ai.GetNumColorChannels(); ++n) {
  342. out.StartArray(true);
  343. for(unsigned int i = 0; i < ai.mNumVertices; ++i) {
  344. out.Element(ai.mColors[n][i].r);
  345. out.Element(ai.mColors[n][i].g);
  346. out.Element(ai.mColors[n][i].b);
  347. out.Element(ai.mColors[n][i].a);
  348. }
  349. out.EndArray();
  350. }
  351. out.EndArray();
  352. }
  353. if(ai.mNumBones) {
  354. out.Key("bones");
  355. out.StartArray();
  356. for(unsigned int n = 0; n < ai.mNumBones; ++n) {
  357. Write(out, *ai.mBones[n]);
  358. }
  359. out.EndArray();
  360. }
  361. out.Key("faces");
  362. out.StartArray();
  363. for(unsigned int n = 0; n < ai.mNumFaces; ++n) {
  364. Write(out, ai.mFaces[n]);
  365. }
  366. out.EndArray();
  367. out.EndObj();
  368. }
  369. void Write(JSONWriter& out, const aiNode& ai, bool is_elem = true)
  370. {
  371. out.StartObj(is_elem);
  372. out.Key("name");
  373. out.SimpleValue(ai.mName);
  374. out.Key("transformation");
  375. Write(out,ai.mTransformation,false);
  376. if(ai.mNumMeshes) {
  377. out.Key("meshes");
  378. out.StartArray();
  379. for(unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  380. out.Element(ai.mMeshes[n]);
  381. }
  382. out.EndArray();
  383. }
  384. if(ai.mNumChildren) {
  385. out.Key("children");
  386. out.StartArray();
  387. for(unsigned int n = 0; n < ai.mNumChildren; ++n) {
  388. Write(out,*ai.mChildren[n]);
  389. }
  390. out.EndArray();
  391. }
  392. out.EndObj();
  393. }
  394. void Write(JSONWriter& out, const aiMaterial& ai, bool is_elem = true)
  395. {
  396. out.StartObj(is_elem);
  397. out.Key("properties");
  398. out.StartArray();
  399. for(unsigned int i = 0; i < ai.mNumProperties; ++i) {
  400. const aiMaterialProperty* const prop = ai.mProperties[i];
  401. out.StartObj(true);
  402. out.Key("key");
  403. out.SimpleValue(prop->mKey);
  404. out.Key("semantic");
  405. out.SimpleValue(prop->mSemantic);
  406. out.Key("index");
  407. out.SimpleValue(prop->mIndex);
  408. out.Key("type");
  409. out.SimpleValue(prop->mType);
  410. out.Key("value");
  411. switch(prop->mType)
  412. {
  413. case aiPTI_Float:
  414. if(prop->mDataLength/sizeof(float) > 1) {
  415. out.StartArray();
  416. for(unsigned int i = 0; i < prop->mDataLength/sizeof(float); ++i) {
  417. out.Element(reinterpret_cast<float*>(prop->mData)[i]);
  418. }
  419. out.EndArray();
  420. }
  421. else {
  422. out.SimpleValue(*reinterpret_cast<float*>(prop->mData));
  423. }
  424. break;
  425. case aiPTI_Integer:
  426. if(prop->mDataLength/sizeof(int) > 1) {
  427. out.StartArray();
  428. for(unsigned int i = 0; i < prop->mDataLength/sizeof(int); ++i) {
  429. out.Element(reinterpret_cast<int*>(prop->mData)[i]);
  430. }
  431. out.EndArray();
  432. }
  433. else {
  434. out.SimpleValue(*reinterpret_cast<int*>(prop->mData));
  435. }
  436. break;
  437. case aiPTI_String:
  438. {
  439. aiString s;
  440. aiGetMaterialString(&ai,prop->mKey.data,prop->mSemantic,prop->mIndex,&s);
  441. out.SimpleValue(s);
  442. }
  443. break;
  444. case aiPTI_Buffer:
  445. {
  446. // binary data is written as series of hex-encoded octets
  447. out.SimpleValue(prop->mData,prop->mDataLength);
  448. }
  449. break;
  450. default:
  451. assert(false);
  452. }
  453. out.EndObj();
  454. }
  455. out.EndArray();
  456. out.EndObj();
  457. }
  458. void Write(JSONWriter& out, const aiTexture& ai, bool is_elem = true)
  459. {
  460. out.StartObj(is_elem);
  461. out.Key("width");
  462. out.SimpleValue(ai.mWidth);
  463. out.Key("height");
  464. out.SimpleValue(ai.mHeight);
  465. out.Key("formathint");
  466. out.SimpleValue(aiString(ai.achFormatHint));
  467. out.Key("data");
  468. if(!ai.mHeight) {
  469. out.SimpleValue(ai.pcData,ai.mWidth);
  470. }
  471. else {
  472. out.StartArray();
  473. for(unsigned int y = 0; y < ai.mHeight; ++y) {
  474. out.StartArray(true);
  475. for(unsigned int x = 0; x < ai.mWidth; ++x) {
  476. const aiTexel& tx = ai.pcData[y*ai.mWidth+x];
  477. out.StartArray(true);
  478. out.Element(static_cast<unsigned int>(tx.r));
  479. out.Element(static_cast<unsigned int>(tx.g));
  480. out.Element(static_cast<unsigned int>(tx.b));
  481. out.Element(static_cast<unsigned int>(tx.a));
  482. out.EndArray();
  483. }
  484. out.EndArray();
  485. }
  486. out.EndArray();
  487. }
  488. out.EndObj();
  489. }
  490. void Write(JSONWriter& out, const aiLight& ai, bool is_elem = true)
  491. {
  492. out.StartObj(is_elem);
  493. out.Key("name");
  494. out.SimpleValue(ai.mName);
  495. out.Key("type");
  496. out.SimpleValue(ai.mType);
  497. if(ai.mType == aiLightSource_SPOT || ai.mType == aiLightSource_UNDEFINED) {
  498. out.Key("angleinnercone");
  499. out.SimpleValue(ai.mAngleInnerCone);
  500. out.Key("angleoutercone");
  501. out.SimpleValue(ai.mAngleOuterCone);
  502. }
  503. out.Key("attenuationconstant");
  504. out.SimpleValue(ai.mAttenuationConstant);
  505. out.Key("attenuationlinear");
  506. out.SimpleValue(ai.mAttenuationLinear);
  507. out.Key("attenuationquadratic");
  508. out.SimpleValue(ai.mAttenuationQuadratic);
  509. out.Key("diffusecolor");
  510. Write(out,ai.mColorDiffuse,false);
  511. out.Key("specularcolor");
  512. Write(out,ai.mColorSpecular,false);
  513. out.Key("ambientcolor");
  514. Write(out,ai.mColorAmbient,false);
  515. if(ai.mType != aiLightSource_POINT) {
  516. out.Key("direction");
  517. Write(out,ai.mDirection,false);
  518. }
  519. if(ai.mType != aiLightSource_DIRECTIONAL) {
  520. out.Key("position");
  521. Write(out,ai.mPosition,false);
  522. }
  523. out.EndObj();
  524. }
  525. void Write(JSONWriter& out, const aiNodeAnim& ai, bool is_elem = true)
  526. {
  527. out.StartObj(is_elem);
  528. out.Key("name");
  529. out.SimpleValue(ai.mNodeName);
  530. out.Key("prestate");
  531. out.SimpleValue(ai.mPreState);
  532. out.Key("poststate");
  533. out.SimpleValue(ai.mPostState);
  534. if(ai.mNumPositionKeys) {
  535. out.Key("positionkeys");
  536. out.StartArray();
  537. for(unsigned int n = 0; n < ai.mNumPositionKeys; ++n) {
  538. const aiVectorKey& pos = ai.mPositionKeys[n];
  539. out.StartArray(true);
  540. out.Element(pos.mTime);
  541. Write(out,pos.mValue);
  542. out.EndArray();
  543. }
  544. out.EndArray();
  545. }
  546. if(ai.mNumRotationKeys) {
  547. out.Key("rotationkeys");
  548. out.StartArray();
  549. for(unsigned int n = 0; n < ai.mNumRotationKeys; ++n) {
  550. const aiQuatKey& rot = ai.mRotationKeys[n];
  551. out.StartArray(true);
  552. out.Element(rot.mTime);
  553. Write(out,rot.mValue);
  554. out.EndArray();
  555. }
  556. out.EndArray();
  557. }
  558. if(ai.mNumScalingKeys) {
  559. out.Key("scalingkeys");
  560. out.StartArray();
  561. for(unsigned int n = 0; n < ai.mNumScalingKeys; ++n) {
  562. const aiVectorKey& scl = ai.mScalingKeys[n];
  563. out.StartArray(true);
  564. out.Element(scl.mTime);
  565. Write(out,scl.mValue);
  566. out.EndArray();
  567. }
  568. out.EndArray();
  569. }
  570. out.EndObj();
  571. }
  572. void Write(JSONWriter& out, const aiAnimation& ai, bool is_elem = true)
  573. {
  574. out.StartObj(is_elem);
  575. out.Key("name");
  576. out.SimpleValue(ai.mName);
  577. out.Key("tickspersecond");
  578. out.SimpleValue(ai.mTicksPerSecond);
  579. out.Key("duration");
  580. out.SimpleValue(ai.mDuration);
  581. out.Key("channels");
  582. out.StartArray();
  583. for(unsigned int n = 0; n < ai.mNumChannels; ++n) {
  584. Write(out,*ai.mChannels[n]);
  585. }
  586. out.EndArray();
  587. out.EndObj();
  588. }
  589. void Write(JSONWriter& out, const aiCamera& ai, bool is_elem = true)
  590. {
  591. out.StartObj(is_elem);
  592. out.Key("name");
  593. out.SimpleValue(ai.mName);
  594. out.Key("aspect");
  595. out.SimpleValue(ai.mAspect);
  596. out.Key("clipplanefar");
  597. out.SimpleValue(ai.mClipPlaneFar);
  598. out.Key("clipplanenear");
  599. out.SimpleValue(ai.mClipPlaneNear);
  600. out.Key("horizontalfov");
  601. out.SimpleValue(ai.mHorizontalFOV);
  602. out.Key("up");
  603. Write(out,ai.mUp,false);
  604. out.Key("lookat");
  605. Write(out,ai.mLookAt,false);
  606. out.EndObj();
  607. }
  608. void WriteFormatInfo(JSONWriter& out)
  609. {
  610. out.StartObj();
  611. out.Key("format");
  612. out.SimpleValue("\"assimp2json\"");
  613. out.Key("version");
  614. out.SimpleValue(CURRENT_FORMAT_VERSION);
  615. out.EndObj();
  616. }
  617. void Write(JSONWriter& out, const aiScene& ai)
  618. {
  619. out.StartObj();
  620. out.Key("__metadata__");
  621. WriteFormatInfo(out);
  622. out.Key("rootnode");
  623. Write(out,*ai.mRootNode, false);
  624. out.Key("flags");
  625. out.SimpleValue(ai.mFlags);
  626. if(ai.HasMeshes()) {
  627. out.Key("meshes");
  628. out.StartArray();
  629. for(unsigned int n = 0; n < ai.mNumMeshes; ++n) {
  630. Write(out,*ai.mMeshes[n]);
  631. }
  632. out.EndArray();
  633. }
  634. if(ai.HasMaterials()) {
  635. out.Key("materials");
  636. out.StartArray();
  637. for(unsigned int n = 0; n < ai.mNumMaterials; ++n) {
  638. Write(out,*ai.mMaterials[n]);
  639. }
  640. out.EndArray();
  641. }
  642. if(ai.HasAnimations()) {
  643. out.Key("animations");
  644. out.StartArray();
  645. for(unsigned int n = 0; n < ai.mNumAnimations; ++n) {
  646. Write(out,*ai.mAnimations[n]);
  647. }
  648. out.EndArray();
  649. }
  650. if(ai.HasLights()) {
  651. out.Key("lights");
  652. out.StartArray();
  653. for(unsigned int n = 0; n < ai.mNumLights; ++n) {
  654. Write(out,*ai.mLights[n]);
  655. }
  656. out.EndArray();
  657. }
  658. if(ai.HasCameras()) {
  659. out.Key("cameras");
  660. out.StartArray();
  661. for(unsigned int n = 0; n < ai.mNumCameras; ++n) {
  662. Write(out,*ai.mCameras[n]);
  663. }
  664. out.EndArray();
  665. }
  666. if(ai.HasTextures()) {
  667. out.Key("textures");
  668. out.StartArray();
  669. for(unsigned int n = 0; n < ai.mNumTextures; ++n) {
  670. Write(out,*ai.mTextures[n]);
  671. }
  672. out.EndArray();
  673. }
  674. out.EndObj();
  675. }
  676. void ExportAssimp2Json(const char* file, Assimp::IOSystem* io, const aiScene* scene, const Assimp::ExportProperties*)
  677. {
  678. std::unique_ptr<Assimp::IOStream> str(io->Open(file,"wt"));
  679. if(!str) {
  680. //throw Assimp::DeadlyExportError("could not open output file");
  681. }
  682. // get a copy of the scene so we can modify it
  683. aiScene* scenecopy_tmp;
  684. aiCopyScene(scene, &scenecopy_tmp);
  685. try {
  686. // split meshes so they fit into a 16 bit index buffer
  687. MeshSplitter splitter;
  688. splitter.SetLimit(1 << 16);
  689. splitter.Execute(scenecopy_tmp);
  690. // XXX Flag_WriteSpecialFloats is turned on by default, right now we don't have a configuration interface for exporters
  691. JSONWriter s(*str,JSONWriter::Flag_WriteSpecialFloats);
  692. Write(s,*scenecopy_tmp);
  693. }
  694. catch(...) {
  695. aiFreeScene(scenecopy_tmp);
  696. throw;
  697. }
  698. aiFreeScene(scenecopy_tmp);
  699. }
  700. } //
  701. #endif // ASSIMP_BUILD_NO_ASSJSON_EXPORTER
  702. #endif // ASSIMP_BUILD_NO_EXPORT