MMDVmdParser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2017, 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. #pragma once
  34. #include <vector>
  35. #include <string>
  36. #include <memory>
  37. #include <iostream>
  38. #include <fstream>
  39. #include <ostream>
  40. #include "MMDCpp14.h"
  41. namespace vmd
  42. {
  43. /// ボーンフレーム
  44. class VmdBoneFrame
  45. {
  46. public:
  47. /// ボーン名
  48. std::string name;
  49. /// フレーム番号
  50. int frame;
  51. /// 位置
  52. float position[3];
  53. /// 回転
  54. float orientation[4];
  55. /// 補間曲線
  56. char interpolation[4][4][4];
  57. void Read(std::istream* stream)
  58. {
  59. char buffer[15];
  60. stream->read((char*) buffer, sizeof(char)*15);
  61. name = std::string(buffer);
  62. stream->read((char*) &frame, sizeof(int));
  63. stream->read((char*) position, sizeof(float)*3);
  64. stream->read((char*) orientation, sizeof(float)*4);
  65. stream->read((char*) interpolation, sizeof(char) * 4 * 4 * 4);
  66. }
  67. void Write(std::ostream* stream)
  68. {
  69. stream->write((char*)name.c_str(), sizeof(char) * 15);
  70. stream->write((char*)&frame, sizeof(int));
  71. stream->write((char*)position, sizeof(float) * 3);
  72. stream->write((char*)orientation, sizeof(float) * 4);
  73. stream->write((char*)interpolation, sizeof(char) * 4 * 4 * 4);
  74. }
  75. };
  76. /// 表情フレーム
  77. class VmdFaceFrame
  78. {
  79. public:
  80. /// 表情名
  81. std::string face_name;
  82. /// 表情の重み
  83. float weight;
  84. /// フレーム番号
  85. uint32_t frame;
  86. void Read(std::istream* stream)
  87. {
  88. char buffer[15];
  89. stream->read((char*) &buffer, sizeof(char) * 15);
  90. face_name = std::string(buffer);
  91. stream->read((char*) &frame, sizeof(int));
  92. stream->read((char*) &weight, sizeof(float));
  93. }
  94. void Write(std::ostream* stream)
  95. {
  96. stream->write((char*)face_name.c_str(), sizeof(char) * 15);
  97. stream->write((char*)&frame, sizeof(int));
  98. stream->write((char*)&weight, sizeof(float));
  99. }
  100. };
  101. /// カメラフレーム
  102. class VmdCameraFrame
  103. {
  104. public:
  105. /// フレーム番号
  106. int frame;
  107. /// 距離
  108. float distance;
  109. /// 位置
  110. float position[3];
  111. /// 回転
  112. float orientation[3];
  113. /// 補間曲線
  114. char interpolation[6][4];
  115. /// 視野角
  116. float angle;
  117. /// 不明データ
  118. char unknown[3];
  119. void Read(std::istream *stream)
  120. {
  121. stream->read((char*) &frame, sizeof(int));
  122. stream->read((char*) &distance, sizeof(float));
  123. stream->read((char*) position, sizeof(float) * 3);
  124. stream->read((char*) orientation, sizeof(float) * 3);
  125. stream->read((char*) interpolation, sizeof(char) * 24);
  126. stream->read((char*) &angle, sizeof(float));
  127. stream->read((char*) unknown, sizeof(char) * 3);
  128. }
  129. void Write(std::ostream *stream)
  130. {
  131. stream->write((char*)&frame, sizeof(int));
  132. stream->write((char*)&distance, sizeof(float));
  133. stream->write((char*)position, sizeof(float) * 3);
  134. stream->write((char*)orientation, sizeof(float) * 3);
  135. stream->write((char*)interpolation, sizeof(char) * 24);
  136. stream->write((char*)&angle, sizeof(float));
  137. stream->write((char*)unknown, sizeof(char) * 3);
  138. }
  139. };
  140. /// ライトフレーム
  141. class VmdLightFrame
  142. {
  143. public:
  144. /// フレーム番号
  145. int frame;
  146. /// 色
  147. float color[3];
  148. /// 位置
  149. float position[3];
  150. void Read(std::istream* stream)
  151. {
  152. stream->read((char*) &frame, sizeof(int));
  153. stream->read((char*) color, sizeof(float) * 3);
  154. stream->read((char*) position, sizeof(float) * 3);
  155. }
  156. void Write(std::ostream* stream)
  157. {
  158. stream->write((char*)&frame, sizeof(int));
  159. stream->write((char*)color, sizeof(float) * 3);
  160. stream->write((char*)position, sizeof(float) * 3);
  161. }
  162. };
  163. /// IKの有効無効
  164. class VmdIkEnable
  165. {
  166. public:
  167. std::string ik_name;
  168. bool enable;
  169. };
  170. /// IKフレーム
  171. class VmdIkFrame
  172. {
  173. public:
  174. int frame;
  175. bool display;
  176. std::vector<VmdIkEnable> ik_enable;
  177. void Read(std::istream *stream)
  178. {
  179. char buffer[20];
  180. stream->read((char*) &frame, sizeof(int));
  181. stream->read((char*) &display, sizeof(uint8_t));
  182. int ik_count;
  183. stream->read((char*) &ik_count, sizeof(int));
  184. ik_enable.resize(ik_count);
  185. for (int i = 0; i < ik_count; i++)
  186. {
  187. stream->read(buffer, 20);
  188. ik_enable[i].ik_name = std::string(buffer);
  189. stream->read((char*) &ik_enable[i].enable, sizeof(uint8_t));
  190. }
  191. }
  192. void Write(std::ostream *stream)
  193. {
  194. stream->write((char*)&frame, sizeof(int));
  195. stream->write((char*)&display, sizeof(uint8_t));
  196. int ik_count = static_cast<int>(ik_enable.size());
  197. stream->write((char*)&ik_count, sizeof(int));
  198. for (int i = 0; i < ik_count; i++)
  199. {
  200. const VmdIkEnable& ik_enable = this->ik_enable.at(i);
  201. stream->write(ik_enable.ik_name.c_str(), 20);
  202. stream->write((char*)&ik_enable.enable, sizeof(uint8_t));
  203. }
  204. }
  205. };
  206. /// VMDモーション
  207. class VmdMotion
  208. {
  209. public:
  210. /// モデル名
  211. std::string model_name;
  212. /// バージョン
  213. int version;
  214. /// ボーンフレーム
  215. std::vector<VmdBoneFrame> bone_frames;
  216. /// 表情フレーム
  217. std::vector<VmdFaceFrame> face_frames;
  218. /// カメラフレーム
  219. std::vector<VmdCameraFrame> camera_frames;
  220. /// ライトフレーム
  221. std::vector<VmdLightFrame> light_frames;
  222. /// IKフレーム
  223. std::vector<VmdIkFrame> ik_frames;
  224. static std::unique_ptr<VmdMotion> LoadFromFile(char const *filename)
  225. {
  226. std::ifstream stream(filename, std::ios::binary);
  227. auto result = LoadFromStream(&stream);
  228. stream.close();
  229. return result;
  230. }
  231. static std::unique_ptr<VmdMotion> LoadFromStream(std::ifstream *stream)
  232. {
  233. char buffer[30];
  234. auto result = mmd::make_unique<VmdMotion>();
  235. // magic and version
  236. stream->read((char*) buffer, 30);
  237. if (strncmp(buffer, "Vocaloid Motion Data", 20))
  238. {
  239. std::cerr << "invalid vmd file." << std::endl;
  240. return nullptr;
  241. }
  242. result->version = std::atoi(buffer + 20);
  243. // name
  244. stream->read(buffer, 20);
  245. result->model_name = std::string(buffer);
  246. // bone frames
  247. int bone_frame_num;
  248. stream->read((char*) &bone_frame_num, sizeof(int));
  249. result->bone_frames.resize(bone_frame_num);
  250. for (int i = 0; i < bone_frame_num; i++)
  251. {
  252. result->bone_frames[i].Read(stream);
  253. }
  254. // face frames
  255. int face_frame_num;
  256. stream->read((char*) &face_frame_num, sizeof(int));
  257. result->face_frames.resize(face_frame_num);
  258. for (int i = 0; i < face_frame_num; i++)
  259. {
  260. result->face_frames[i].Read(stream);
  261. }
  262. // camera frames
  263. int camera_frame_num;
  264. stream->read((char*) &camera_frame_num, sizeof(int));
  265. result->camera_frames.resize(camera_frame_num);
  266. for (int i = 0; i < camera_frame_num; i++)
  267. {
  268. result->camera_frames[i].Read(stream);
  269. }
  270. // light frames
  271. int light_frame_num;
  272. stream->read((char*) &light_frame_num, sizeof(int));
  273. result->light_frames.resize(light_frame_num);
  274. for (int i = 0; i < light_frame_num; i++)
  275. {
  276. result->light_frames[i].Read(stream);
  277. }
  278. // unknown2
  279. stream->read(buffer, 4);
  280. // ik frames
  281. if (stream->peek() != std::ios::traits_type::eof())
  282. {
  283. int ik_num;
  284. stream->read((char*) &ik_num, sizeof(int));
  285. result->ik_frames.resize(ik_num);
  286. for (int i = 0; i < ik_num; i++)
  287. {
  288. result->ik_frames[i].Read(stream);
  289. }
  290. }
  291. if (stream->peek() != std::ios::traits_type::eof())
  292. {
  293. std::cerr << "vmd stream has unknown data." << std::endl;
  294. }
  295. return result;
  296. }
  297. bool SaveToFile(const std::u16string& filename)
  298. {
  299. // TODO: How to adapt u16string to string?
  300. /*
  301. std::ofstream stream(filename.c_str(), std::ios::binary);
  302. auto result = SaveToStream(&stream);
  303. stream.close();
  304. return result;
  305. */
  306. return false;
  307. }
  308. bool SaveToStream(std::ofstream *stream)
  309. {
  310. std::string magic = "Vocaloid Motion Data 0002\0";
  311. magic.resize(30);
  312. // magic and version
  313. stream->write(magic.c_str(), 30);
  314. // name
  315. stream->write(model_name.c_str(), 20);
  316. // bone frames
  317. const int bone_frame_num = static_cast<int>(bone_frames.size());
  318. stream->write(reinterpret_cast<const char*>(&bone_frame_num), sizeof(int));
  319. for (int i = 0; i < bone_frame_num; i++)
  320. {
  321. bone_frames[i].Write(stream);
  322. }
  323. // face frames
  324. const int face_frame_num = static_cast<int>(face_frames.size());
  325. stream->write(reinterpret_cast<const char*>(&face_frame_num), sizeof(int));
  326. for (int i = 0; i < face_frame_num; i++)
  327. {
  328. face_frames[i].Write(stream);
  329. }
  330. // camera frames
  331. const int camera_frame_num = static_cast<int>(camera_frames.size());
  332. stream->write(reinterpret_cast<const char*>(&camera_frame_num), sizeof(int));
  333. for (int i = 0; i < camera_frame_num; i++)
  334. {
  335. camera_frames[i].Write(stream);
  336. }
  337. // light frames
  338. const int light_frame_num = static_cast<int>(light_frames.size());
  339. stream->write(reinterpret_cast<const char*>(&light_frame_num), sizeof(int));
  340. for (int i = 0; i < light_frame_num; i++)
  341. {
  342. light_frames[i].Write(stream);
  343. }
  344. // self shadow datas
  345. const int self_shadow_num = 0;
  346. stream->write(reinterpret_cast<const char*>(&self_shadow_num), sizeof(int));
  347. // ik frames
  348. const int ik_num = static_cast<int>(ik_frames.size());
  349. stream->write(reinterpret_cast<const char*>(&ik_num), sizeof(int));
  350. for (int i = 0; i < ik_num; i++)
  351. {
  352. ik_frames[i].Write(stream);
  353. }
  354. return true;
  355. }
  356. };
  357. }