MMDPmdParser.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, 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 "MMDCpp14.h"
  40. namespace pmd
  41. {
  42. class PmdHeader
  43. {
  44. public:
  45. std::string name;
  46. std::string name_english;
  47. std::string comment;
  48. std::string comment_english;
  49. bool Read(std::ifstream* stream)
  50. {
  51. char buffer[256];
  52. stream->read(buffer, 20);
  53. name = std::string(buffer);
  54. stream->read(buffer, 256);
  55. comment = std::string(buffer);
  56. return true;
  57. }
  58. bool ReadExtension(std::ifstream* stream)
  59. {
  60. char buffer[256];
  61. stream->read(buffer, 20);
  62. name_english = std::string(buffer);
  63. stream->read(buffer, 256);
  64. comment_english = std::string(buffer);
  65. return true;
  66. }
  67. };
  68. class PmdVertex
  69. {
  70. public:
  71. float position[3];
  72. float normal[3];
  73. float uv[2];
  74. uint16_t bone_index[2];
  75. uint8_t bone_weight;
  76. bool edge_invisible;
  77. bool Read(std::ifstream* stream)
  78. {
  79. stream->read((char*) position, sizeof(float) * 3);
  80. stream->read((char*) normal, sizeof(float) * 3);
  81. stream->read((char*) uv, sizeof(float) * 2);
  82. stream->read((char*) bone_index, sizeof(uint16_t) * 2);
  83. stream->read((char*) &bone_weight, sizeof(uint8_t));
  84. stream->read((char*) &edge_invisible, sizeof(uint8_t));
  85. return true;
  86. }
  87. };
  88. class PmdMaterial
  89. {
  90. public:
  91. float diffuse[4];
  92. float power;
  93. float specular[3];
  94. float ambient[3];
  95. uint8_t toon_index;
  96. uint8_t edge_flag;
  97. uint32_t index_count;
  98. std::string texture_filename;
  99. std::string sphere_filename;
  100. bool Read(std::ifstream* stream)
  101. {
  102. char buffer[20];
  103. stream->read((char*) &diffuse, sizeof(float) * 4);
  104. stream->read((char*) &power, sizeof(float));
  105. stream->read((char*) &specular, sizeof(float) * 3);
  106. stream->read((char*) &ambient, sizeof(float) * 3);
  107. stream->read((char*) &toon_index, sizeof(uint8_t));
  108. stream->read((char*) &edge_flag, sizeof(uint8_t));
  109. stream->read((char*) &index_count, sizeof(uint32_t));
  110. stream->read((char*) &buffer, sizeof(char) * 20);
  111. char* pstar = strchr(buffer, '*');
  112. if (nullptr == pstar)
  113. {
  114. texture_filename = std::string(buffer);
  115. sphere_filename.clear();
  116. }
  117. else {
  118. *pstar = 0;
  119. texture_filename = std::string(buffer);
  120. sphere_filename = std::string(pstar+1);
  121. }
  122. return true;
  123. }
  124. };
  125. enum class BoneType : uint8_t
  126. {
  127. Rotation,
  128. RotationAndMove,
  129. IkEffector,
  130. Unknown,
  131. IkEffectable,
  132. RotationEffectable,
  133. IkTarget,
  134. Invisible,
  135. Twist,
  136. RotationMovement
  137. };
  138. class PmdBone
  139. {
  140. public:
  141. std::string name;
  142. std::string name_english;
  143. uint16_t parent_bone_index;
  144. uint16_t tail_pos_bone_index;
  145. BoneType bone_type;
  146. uint16_t ik_parent_bone_index;
  147. float bone_head_pos[3];
  148. void Read(std::istream *stream)
  149. {
  150. char buffer[20];
  151. stream->read(buffer, 20);
  152. name = std::string(buffer);
  153. stream->read((char*) &parent_bone_index, sizeof(uint16_t));
  154. stream->read((char*) &tail_pos_bone_index, sizeof(uint16_t));
  155. stream->read((char*) &bone_type, sizeof(uint8_t));
  156. stream->read((char*) &ik_parent_bone_index, sizeof(uint16_t));
  157. stream->read((char*) &bone_head_pos, sizeof(float) * 3);
  158. }
  159. void ReadExpantion(std::istream *stream)
  160. {
  161. char buffer[20];
  162. stream->read(buffer, 20);
  163. name_english = std::string(buffer);
  164. }
  165. };
  166. class PmdIk
  167. {
  168. public:
  169. uint16_t ik_bone_index;
  170. uint16_t target_bone_index;
  171. uint16_t interations;
  172. float angle_limit;
  173. std::vector<uint16_t> ik_child_bone_index;
  174. void Read(std::istream *stream)
  175. {
  176. stream->read((char *) &ik_bone_index, sizeof(uint16_t));
  177. stream->read((char *) &target_bone_index, sizeof(uint16_t));
  178. uint8_t ik_chain_length;
  179. stream->read((char*) &ik_chain_length, sizeof(uint8_t));
  180. stream->read((char *) &interations, sizeof(uint16_t));
  181. stream->read((char *) &angle_limit, sizeof(float));
  182. ik_child_bone_index.resize(ik_chain_length);
  183. for (int i = 0; i < ik_chain_length; i++)
  184. {
  185. stream->read((char *) &ik_child_bone_index[i], sizeof(uint16_t));
  186. }
  187. }
  188. };
  189. class PmdFaceVertex
  190. {
  191. public:
  192. int vertex_index;
  193. float position[3];
  194. void Read(std::istream *stream)
  195. {
  196. stream->read((char *) &vertex_index, sizeof(int));
  197. stream->read((char *) position, sizeof(float) * 3);
  198. }
  199. };
  200. enum class FaceCategory : uint8_t
  201. {
  202. Base,
  203. Eyebrow,
  204. Eye,
  205. Mouth,
  206. Other
  207. };
  208. class PmdFace
  209. {
  210. public:
  211. std::string name;
  212. FaceCategory type;
  213. std::vector<PmdFaceVertex> vertices;
  214. std::string name_english;
  215. void Read(std::istream *stream)
  216. {
  217. char buffer[20];
  218. stream->read(buffer, 20);
  219. name = std::string(buffer);
  220. int vertex_count;
  221. stream->read((char*) &vertex_count, sizeof(int));
  222. stream->read((char*) &type, sizeof(uint8_t));
  223. vertices.resize(vertex_count);
  224. for (int i = 0; i < vertex_count; i++)
  225. {
  226. vertices[i].Read(stream);
  227. }
  228. }
  229. void ReadExpantion(std::istream *stream)
  230. {
  231. char buffer[20];
  232. stream->read(buffer, 20);
  233. name_english = std::string(buffer);
  234. }
  235. };
  236. class PmdBoneDispName
  237. {
  238. public:
  239. std::string bone_disp_name;
  240. std::string bone_disp_name_english;
  241. void Read(std::istream *stream)
  242. {
  243. char buffer[50];
  244. stream->read(buffer, 50);
  245. bone_disp_name = std::string(buffer);
  246. bone_disp_name_english.clear();
  247. }
  248. void ReadExpantion(std::istream *stream)
  249. {
  250. char buffer[50];
  251. stream->read(buffer, 50);
  252. bone_disp_name_english = std::string(buffer);
  253. }
  254. };
  255. class PmdBoneDisp
  256. {
  257. public:
  258. uint16_t bone_index;
  259. uint8_t bone_disp_index;
  260. void Read(std::istream *stream)
  261. {
  262. stream->read((char*) &bone_index, sizeof(uint16_t));
  263. stream->read((char*) &bone_disp_index, sizeof(uint8_t));
  264. }
  265. };
  266. enum class RigidBodyShape : uint8_t
  267. {
  268. Sphere = 0,
  269. Box = 1,
  270. Cpusel = 2
  271. };
  272. enum class RigidBodyType : uint8_t
  273. {
  274. BoneConnected = 0,
  275. Physics = 1,
  276. ConnectedPhysics = 2
  277. };
  278. class PmdRigidBody
  279. {
  280. public:
  281. std::string name;
  282. uint16_t related_bone_index;
  283. uint8_t group_index;
  284. uint16_t mask;
  285. RigidBodyShape shape;
  286. float size[3];
  287. float position[3];
  288. float orientation[3];
  289. float weight;
  290. float linear_damping;
  291. float anglar_damping;
  292. float restitution;
  293. float friction;
  294. RigidBodyType rigid_type;
  295. void Read(std::istream *stream)
  296. {
  297. char buffer[20];
  298. stream->read(buffer, sizeof(char) * 20);
  299. name = (std::string(buffer));
  300. stream->read((char*) &related_bone_index, sizeof(uint16_t));
  301. stream->read((char*) &group_index, sizeof(uint8_t));
  302. stream->read((char*) &mask, sizeof(uint16_t));
  303. stream->read((char*) &shape, sizeof(uint8_t));
  304. stream->read((char*) size, sizeof(float) * 3);
  305. stream->read((char*) position, sizeof(float) * 3);
  306. stream->read((char*) orientation, sizeof(float) * 3);
  307. stream->read((char*) &weight, sizeof(float));
  308. stream->read((char*) &linear_damping, sizeof(float));
  309. stream->read((char*) &anglar_damping, sizeof(float));
  310. stream->read((char*) &restitution, sizeof(float));
  311. stream->read((char*) &friction, sizeof(float));
  312. stream->read((char*) &rigid_type, sizeof(char));
  313. }
  314. };
  315. class PmdConstraint
  316. {
  317. public:
  318. std::string name;
  319. uint32_t rigid_body_index_a;
  320. uint32_t rigid_body_index_b;
  321. float position[3];
  322. float orientation[3];
  323. float linear_lower_limit[3];
  324. float linear_upper_limit[3];
  325. float angular_lower_limit[3];
  326. float angular_upper_limit[3];
  327. float linear_stiffness[3];
  328. float angular_stiffness[3];
  329. void Read(std::istream *stream)
  330. {
  331. char buffer[20];
  332. stream->read(buffer, 20);
  333. name = std::string(buffer);
  334. stream->read((char *) &rigid_body_index_a, sizeof(uint32_t));
  335. stream->read((char *) &rigid_body_index_b, sizeof(uint32_t));
  336. stream->read((char *) position, sizeof(float) * 3);
  337. stream->read((char *) orientation, sizeof(float) * 3);
  338. stream->read((char *) linear_lower_limit, sizeof(float) * 3);
  339. stream->read((char *) linear_upper_limit, sizeof(float) * 3);
  340. stream->read((char *) angular_lower_limit, sizeof(float) * 3);
  341. stream->read((char *) angular_upper_limit, sizeof(float) * 3);
  342. stream->read((char *) linear_stiffness, sizeof(float) * 3);
  343. stream->read((char *) angular_stiffness, sizeof(float) * 3);
  344. }
  345. };
  346. class PmdModel
  347. {
  348. public:
  349. float version;
  350. PmdHeader header;
  351. std::vector<PmdVertex> vertices;
  352. std::vector<uint16_t> indices;
  353. std::vector<PmdMaterial> materials;
  354. std::vector<PmdBone> bones;
  355. std::vector<PmdIk> iks;
  356. std::vector<PmdFace> faces;
  357. std::vector<uint16_t> faces_indices;
  358. std::vector<PmdBoneDispName> bone_disp_name;
  359. std::vector<PmdBoneDisp> bone_disp;
  360. std::vector<std::string> toon_filenames;
  361. std::vector<PmdRigidBody> rigid_bodies;
  362. std::vector<PmdConstraint> constraints;
  363. static std::unique_ptr<PmdModel> LoadFromFile(const char *filename)
  364. {
  365. std::ifstream stream(filename, std::ios::binary);
  366. if (stream.fail())
  367. {
  368. std::cerr << "could not open \"" << filename << "\"" << std::endl;
  369. return nullptr;
  370. }
  371. auto result = LoadFromStream(&stream);
  372. stream.close();
  373. return result;
  374. }
  375. static std::unique_ptr<PmdModel> LoadFromStream(std::ifstream *stream)
  376. {
  377. auto result = mmd::make_unique<PmdModel>();
  378. char buffer[100];
  379. // magic
  380. char magic[3];
  381. stream->read(magic, 3);
  382. if (magic[0] != 'P' || magic[1] != 'm' || magic[2] != 'd')
  383. {
  384. std::cerr << "invalid file" << std::endl;
  385. return nullptr;
  386. }
  387. // version
  388. stream->read((char*) &(result->version), sizeof(float));
  389. if (result ->version != 1.0f)
  390. {
  391. std::cerr << "invalid version" << std::endl;
  392. return nullptr;
  393. }
  394. // header
  395. result->header.Read(stream);
  396. // vertices
  397. uint32_t vertex_num;
  398. stream->read((char*) &vertex_num, sizeof(uint32_t));
  399. result->vertices.resize(vertex_num);
  400. for (uint32_t i = 0; i < vertex_num; i++)
  401. {
  402. result->vertices[i].Read(stream);
  403. }
  404. // indices
  405. uint32_t index_num;
  406. stream->read((char*) &index_num, sizeof(uint32_t));
  407. result->indices.resize(index_num);
  408. for (uint32_t i = 0; i < index_num; i++)
  409. {
  410. stream->read((char*) &result->indices[i], sizeof(uint16_t));
  411. }
  412. // materials
  413. uint32_t material_num;
  414. stream->read((char*) &material_num, sizeof(uint32_t));
  415. result->materials.resize(material_num);
  416. for (uint32_t i = 0; i < material_num; i++)
  417. {
  418. result->materials[i].Read(stream);
  419. }
  420. // bones
  421. uint16_t bone_num;
  422. stream->read((char*) &bone_num, sizeof(uint16_t));
  423. result->bones.resize(bone_num);
  424. for (uint32_t i = 0; i < bone_num; i++)
  425. {
  426. result->bones[i].Read(stream);
  427. }
  428. // iks
  429. uint16_t ik_num;
  430. stream->read((char*) &ik_num, sizeof(uint16_t));
  431. result->iks.resize(ik_num);
  432. for (uint32_t i = 0; i < ik_num; i++)
  433. {
  434. result->iks[i].Read(stream);
  435. }
  436. // faces
  437. uint16_t face_num;
  438. stream->read((char*) &face_num, sizeof(uint16_t));
  439. result->faces.resize(face_num);
  440. for (uint32_t i = 0; i < face_num; i++)
  441. {
  442. result->faces[i].Read(stream);
  443. }
  444. // face frames
  445. uint8_t face_frame_num;
  446. stream->read((char*) &face_frame_num, sizeof(uint8_t));
  447. result->faces_indices.resize(face_frame_num);
  448. for (uint32_t i = 0; i < face_frame_num; i++)
  449. {
  450. stream->read((char*) &result->faces_indices[i], sizeof(uint16_t));
  451. }
  452. // bone names
  453. uint8_t bone_disp_num;
  454. stream->read((char*) &bone_disp_num, sizeof(uint8_t));
  455. result->bone_disp_name.resize(bone_disp_num);
  456. for (uint32_t i = 0; i < bone_disp_num; i++)
  457. {
  458. result->bone_disp_name[i].Read(stream);
  459. }
  460. // bone frame
  461. uint32_t bone_frame_num;
  462. stream->read((char*) &bone_frame_num, sizeof(uint32_t));
  463. result->bone_disp.resize(bone_frame_num);
  464. for (uint32_t i = 0; i < bone_frame_num; i++)
  465. {
  466. result->bone_disp[i].Read(stream);
  467. }
  468. // english name
  469. bool english;
  470. stream->read((char*) &english, sizeof(char));
  471. if (english)
  472. {
  473. result->header.ReadExtension(stream);
  474. for (uint32_t i = 0; i < bone_num; i++)
  475. {
  476. result->bones[i].ReadExpantion(stream);
  477. }
  478. for (uint32_t i = 0; i < face_num; i++)
  479. {
  480. if (result->faces[i].type == pmd::FaceCategory::Base)
  481. {
  482. continue;
  483. }
  484. result->faces[i].ReadExpantion(stream);
  485. }
  486. for (uint32_t i = 0; i < result->bone_disp_name.size(); i++)
  487. {
  488. result->bone_disp_name[i].ReadExpantion(stream);
  489. }
  490. }
  491. // toon textures
  492. if (stream->peek() == std::ios::traits_type::eof())
  493. {
  494. result->toon_filenames.clear();
  495. }
  496. else {
  497. result->toon_filenames.resize(10);
  498. for (uint32_t i = 0; i < 10; i++)
  499. {
  500. stream->read(buffer, 100);
  501. result->toon_filenames[i] = std::string(buffer);
  502. }
  503. }
  504. // physics
  505. if (stream->peek() == std::ios::traits_type::eof())
  506. {
  507. result->rigid_bodies.clear();
  508. result->constraints.clear();
  509. }
  510. else {
  511. uint32_t rigid_body_num;
  512. stream->read((char*) &rigid_body_num, sizeof(uint32_t));
  513. result->rigid_bodies.resize(rigid_body_num);
  514. for (uint32_t i = 0; i < rigid_body_num; i++)
  515. {
  516. result->rigid_bodies[i].Read(stream);
  517. }
  518. uint32_t constraint_num;
  519. stream->read((char*) &constraint_num, sizeof(uint32_t));
  520. result->constraints.resize(constraint_num);
  521. for (uint32_t i = 0; i < constraint_num; i++)
  522. {
  523. result->constraints[i].Read(stream);
  524. }
  525. }
  526. if (stream->peek() != std::ios::traits_type::eof())
  527. {
  528. std::cerr << "there is unknown data" << std::endl;
  529. }
  530. return result;
  531. }
  532. };
  533. }