MMDVmdParser.h 8.9 KB

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