MMDVmdParser.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. class VmdBoneFrame
  44. {
  45. public:
  46. std::string name;
  47. int frame;
  48. float position[3];
  49. float orientation[4];
  50. char interpolation[4][4][4];
  51. void Read(std::istream* stream)
  52. {
  53. char buffer[15];
  54. stream->read((char*) buffer, sizeof(char)*15);
  55. name = std::string(buffer);
  56. stream->read((char*) &frame, sizeof(int));
  57. stream->read((char*) position, sizeof(float)*3);
  58. stream->read((char*) orientation, sizeof(float)*4);
  59. stream->read((char*) interpolation, sizeof(char) * 4 * 4 * 4);
  60. }
  61. void Write(std::ostream* stream)
  62. {
  63. stream->write((char*)name.c_str(), sizeof(char) * 15);
  64. stream->write((char*)&frame, sizeof(int));
  65. stream->write((char*)position, sizeof(float) * 3);
  66. stream->write((char*)orientation, sizeof(float) * 4);
  67. stream->write((char*)interpolation, sizeof(char) * 4 * 4 * 4);
  68. }
  69. };
  70. class VmdFaceFrame
  71. {
  72. public:
  73. std::string face_name;
  74. float weight;
  75. uint32_t frame;
  76. void Read(std::istream* stream)
  77. {
  78. char buffer[15];
  79. stream->read((char*) &buffer, sizeof(char) * 15);
  80. face_name = std::string(buffer);
  81. stream->read((char*) &frame, sizeof(int));
  82. stream->read((char*) &weight, sizeof(float));
  83. }
  84. void Write(std::ostream* stream)
  85. {
  86. stream->write((char*)face_name.c_str(), sizeof(char) * 15);
  87. stream->write((char*)&frame, sizeof(int));
  88. stream->write((char*)&weight, sizeof(float));
  89. }
  90. };
  91. class VmdCameraFrame
  92. {
  93. public:
  94. int frame;
  95. float distance;
  96. float position[3];
  97. float orientation[3];
  98. char interpolation[6][4];
  99. float angle;
  100. char unknown[3];
  101. void Read(std::istream *stream)
  102. {
  103. stream->read((char*) &frame, sizeof(int));
  104. stream->read((char*) &distance, sizeof(float));
  105. stream->read((char*) position, sizeof(float) * 3);
  106. stream->read((char*) orientation, sizeof(float) * 3);
  107. stream->read((char*) interpolation, sizeof(char) * 24);
  108. stream->read((char*) &angle, sizeof(float));
  109. stream->read((char*) unknown, sizeof(char) * 3);
  110. }
  111. void Write(std::ostream *stream)
  112. {
  113. stream->write((char*)&frame, sizeof(int));
  114. stream->write((char*)&distance, sizeof(float));
  115. stream->write((char*)position, sizeof(float) * 3);
  116. stream->write((char*)orientation, sizeof(float) * 3);
  117. stream->write((char*)interpolation, sizeof(char) * 24);
  118. stream->write((char*)&angle, sizeof(float));
  119. stream->write((char*)unknown, sizeof(char) * 3);
  120. }
  121. };
  122. class VmdLightFrame
  123. {
  124. public:
  125. int frame;
  126. float color[3];
  127. float position[3];
  128. void Read(std::istream* stream)
  129. {
  130. stream->read((char*) &frame, sizeof(int));
  131. stream->read((char*) color, sizeof(float) * 3);
  132. stream->read((char*) position, sizeof(float) * 3);
  133. }
  134. void Write(std::ostream* stream)
  135. {
  136. stream->write((char*)&frame, sizeof(int));
  137. stream->write((char*)color, sizeof(float) * 3);
  138. stream->write((char*)position, sizeof(float) * 3);
  139. }
  140. };
  141. class VmdIkEnable
  142. {
  143. public:
  144. std::string ik_name;
  145. bool enable;
  146. };
  147. class VmdIkFrame
  148. {
  149. public:
  150. int frame;
  151. bool display;
  152. std::vector<VmdIkEnable> ik_enable;
  153. void Read(std::istream *stream)
  154. {
  155. char buffer[20];
  156. stream->read((char*) &frame, sizeof(int));
  157. stream->read((char*) &display, sizeof(uint8_t));
  158. int ik_count;
  159. stream->read((char*) &ik_count, sizeof(int));
  160. ik_enable.resize(ik_count);
  161. for (int i = 0; i < ik_count; i++)
  162. {
  163. stream->read(buffer, 20);
  164. ik_enable[i].ik_name = std::string(buffer);
  165. stream->read((char*) &ik_enable[i].enable, sizeof(uint8_t));
  166. }
  167. }
  168. void Write(std::ostream *stream)
  169. {
  170. stream->write((char*)&frame, sizeof(int));
  171. stream->write((char*)&display, sizeof(uint8_t));
  172. int ik_count = static_cast<int>(ik_enable.size());
  173. stream->write((char*)&ik_count, sizeof(int));
  174. for (int i = 0; i < ik_count; i++)
  175. {
  176. const VmdIkEnable& ik_enable_ref = this->ik_enable.at(i);
  177. stream->write(ik_enable_ref.ik_name.c_str(), 20);
  178. stream->write((char *)&ik_enable_ref.enable, sizeof(uint8_t));
  179. }
  180. }
  181. };
  182. class VmdMotion
  183. {
  184. public:
  185. std::string model_name;
  186. int version;
  187. std::vector<VmdBoneFrame> bone_frames;
  188. std::vector<VmdFaceFrame> face_frames;
  189. std::vector<VmdCameraFrame> camera_frames;
  190. std::vector<VmdLightFrame> light_frames;
  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 data
  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. }