FBXExportNode.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2018, 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. #ifndef ASSIMP_BUILD_NO_EXPORT
  34. #ifndef ASSIMP_BUILD_NO_FBX_EXPORTER
  35. #include "FBXExportNode.h"
  36. #include "FBXCommon.h"
  37. #include <assimp/StreamWriter.h> // StreamWriterLE
  38. #include <assimp/ai_assert.h>
  39. #include <string>
  40. #include <memory> // shared_ptr
  41. // AddP70<type> helpers... there's no usable pattern here,
  42. // so all are defined as separate functions.
  43. // Even "animatable" properties are often completely different
  44. // from the standard (nonanimated) property definition,
  45. // so they are specified with an 'A' suffix.
  46. void FBX::Node::AddP70int(
  47. const std::string& name, int32_t value
  48. ) {
  49. FBX::Node n("P");
  50. n.AddProperties(name, "int", "Integer", "", value);
  51. AddChild(n);
  52. }
  53. void FBX::Node::AddP70bool(
  54. const std::string& name, bool value
  55. ) {
  56. FBX::Node n("P");
  57. n.AddProperties(name, "bool", "", "", int32_t(value));
  58. AddChild(n);
  59. }
  60. void FBX::Node::AddP70double(
  61. const std::string& name, double value
  62. ) {
  63. FBX::Node n("P");
  64. n.AddProperties(name, "double", "Number", "", value);
  65. AddChild(n);
  66. }
  67. void FBX::Node::AddP70numberA(
  68. const std::string& name, double value
  69. ) {
  70. FBX::Node n("P");
  71. n.AddProperties(name, "Number", "", "A", value);
  72. AddChild(n);
  73. }
  74. void FBX::Node::AddP70color(
  75. const std::string& name, double r, double g, double b
  76. ) {
  77. FBX::Node n("P");
  78. n.AddProperties(name, "ColorRGB", "Color", "", r, g, b);
  79. AddChild(n);
  80. }
  81. void FBX::Node::AddP70colorA(
  82. const std::string& name, double r, double g, double b
  83. ) {
  84. FBX::Node n("P");
  85. n.AddProperties(name, "Color", "", "A", r, g, b);
  86. AddChild(n);
  87. }
  88. void FBX::Node::AddP70vector(
  89. const std::string& name, double x, double y, double z
  90. ) {
  91. FBX::Node n("P");
  92. n.AddProperties(name, "Vector3D", "Vector", "", x, y, z);
  93. AddChild(n);
  94. }
  95. void FBX::Node::AddP70vectorA(
  96. const std::string& name, double x, double y, double z
  97. ) {
  98. FBX::Node n("P");
  99. n.AddProperties(name, "Vector", "", "A", x, y, z);
  100. AddChild(n);
  101. }
  102. void FBX::Node::AddP70string(
  103. const std::string& name, const std::string& value
  104. ) {
  105. FBX::Node n("P");
  106. n.AddProperties(name, "KString", "", "", value);
  107. AddChild(n);
  108. }
  109. void FBX::Node::AddP70enum(
  110. const std::string& name, int32_t value
  111. ) {
  112. FBX::Node n("P");
  113. n.AddProperties(name, "enum", "", "", value);
  114. AddChild(n);
  115. }
  116. void FBX::Node::AddP70time(
  117. const std::string& name, int64_t value
  118. ) {
  119. FBX::Node n("P");
  120. n.AddProperties(name, "KTime", "Time", "", value);
  121. AddChild(n);
  122. }
  123. // public member functions for writing to binary fbx
  124. void FBX::Node::Dump(std::shared_ptr<Assimp::IOStream> outfile)
  125. {
  126. Assimp::StreamWriterLE outstream(outfile);
  127. Dump(outstream);
  128. }
  129. void FBX::Node::Dump(Assimp::StreamWriterLE &s)
  130. {
  131. // write header section (with placeholders for some things)
  132. Begin(s);
  133. // write properties
  134. DumpProperties(s);
  135. // go back and fill in property related placeholders
  136. EndProperties(s, properties.size());
  137. // write children
  138. DumpChildren(s);
  139. // finish, filling in end offset placeholder
  140. End(s, !children.empty());
  141. }
  142. void FBX::Node::Begin(Assimp::StreamWriterLE &s)
  143. {
  144. // remember start pos so we can come back and write the end pos
  145. this->start_pos = s.Tell();
  146. // placeholders for end pos and property section info
  147. s.PutU4(0); // end pos
  148. s.PutU4(0); // number of properties
  149. s.PutU4(0); // total property section length
  150. // node name
  151. s.PutU1(name.size()); // length of node name
  152. s.PutString(name); // node name as raw bytes
  153. // property data comes after here
  154. this->property_start = s.Tell();
  155. }
  156. void FBX::Node::DumpProperties(Assimp::StreamWriterLE& s)
  157. {
  158. for (auto &p : properties) {
  159. p.Dump(s);
  160. }
  161. }
  162. void FBX::Node::DumpChildren(Assimp::StreamWriterLE& s)
  163. {
  164. for (FBX::Node& child : children) {
  165. child.Dump(s);
  166. }
  167. }
  168. void FBX::Node::EndProperties(Assimp::StreamWriterLE &s)
  169. {
  170. EndProperties(s, properties.size());
  171. }
  172. void FBX::Node::EndProperties(
  173. Assimp::StreamWriterLE &s,
  174. size_t num_properties
  175. ) {
  176. if (num_properties == 0) { return; }
  177. size_t pos = s.Tell();
  178. ai_assert(pos > property_start);
  179. size_t property_section_size = pos - property_start;
  180. s.Seek(start_pos + 4);
  181. s.PutU4(num_properties);
  182. s.PutU4(property_section_size);
  183. s.Seek(pos);
  184. }
  185. void FBX::Node::End(
  186. Assimp::StreamWriterLE &s,
  187. bool has_children
  188. ) {
  189. // if there were children, add a null record
  190. if (has_children) { s.PutString(FBX::NULL_RECORD); }
  191. // now go back and write initial pos
  192. this->end_pos = s.Tell();
  193. s.Seek(start_pos);
  194. s.PutU4(end_pos);
  195. s.Seek(end_pos);
  196. }
  197. // static member functions
  198. // convenience function to create and write a property node,
  199. // holding a single property which is an array of values.
  200. // does not copy the data, so is efficient for large arrays.
  201. // TODO: optional zip compression!
  202. void FBX::Node::WritePropertyNode(
  203. const std::string& name,
  204. const std::vector<double>& v,
  205. Assimp::StreamWriterLE& s
  206. ){
  207. Node node(name);
  208. node.Begin(s);
  209. s.PutU1('d');
  210. s.PutU4(v.size()); // number of elements
  211. s.PutU4(0); // no encoding (1 would be zip-compressed)
  212. s.PutU4(v.size() * 8); // data size
  213. for (auto it = v.begin(); it != v.end(); ++it) { s.PutF8(*it); }
  214. node.EndProperties(s, 1);
  215. node.End(s, false);
  216. }
  217. // convenience function to create and write a property node,
  218. // holding a single property which is an array of values.
  219. // does not copy the data, so is efficient for large arrays.
  220. // TODO: optional zip compression!
  221. void FBX::Node::WritePropertyNode(
  222. const std::string& name,
  223. const std::vector<int32_t>& v,
  224. Assimp::StreamWriterLE& s
  225. ){
  226. Node node(name);
  227. node.Begin(s);
  228. s.PutU1('i');
  229. s.PutU4(v.size()); // number of elements
  230. s.PutU4(0); // no encoding (1 would be zip-compressed)
  231. s.PutU4(v.size() * 4); // data size
  232. for (auto it = v.begin(); it != v.end(); ++it) { s.PutI4(*it); }
  233. node.EndProperties(s, 1);
  234. node.End(s, false);
  235. }
  236. #endif // ASSIMP_BUILD_NO_FBX_EXPORTER
  237. #endif // ASSIMP_BUILD_NO_EXPORT