MMDPmdParser.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 "MMDCpp14.h"
  40. namespace pmd
  41. {
  42. /// ヘッダ
  43. class PmdHeader
  44. {
  45. public:
  46. /// モデル名
  47. std::string name;
  48. /// モデル名(英語)
  49. std::string name_english;
  50. /// コメント
  51. std::string comment;
  52. /// コメント(英語)
  53. std::string comment_english;
  54. bool Read(std::ifstream* stream)
  55. {
  56. char buffer[256];
  57. stream->read(buffer, 20);
  58. name = std::string(buffer);
  59. stream->read(buffer, 256);
  60. comment = std::string(buffer);
  61. return true;
  62. }
  63. bool ReadExtension(std::ifstream* stream)
  64. {
  65. char buffer[256];
  66. stream->read(buffer, 20);
  67. name_english = std::string(buffer);
  68. stream->read(buffer, 256);
  69. comment_english = std::string(buffer);
  70. return true;
  71. }
  72. };
  73. /// 頂点
  74. class PmdVertex
  75. {
  76. public:
  77. /// 位置
  78. float position[3];
  79. /// 法線
  80. float normal[3];
  81. /// UV座標
  82. float uv[2];
  83. /// 関連ボーンインデックス
  84. uint16_t bone_index[2];
  85. /// ボーンウェイト
  86. uint8_t bone_weight;
  87. /// エッジ不可視
  88. bool edge_invisible;
  89. bool Read(std::ifstream* stream)
  90. {
  91. stream->read((char*) position, sizeof(float) * 3);
  92. stream->read((char*) normal, sizeof(float) * 3);
  93. stream->read((char*) uv, sizeof(float) * 2);
  94. stream->read((char*) bone_index, sizeof(uint16_t) * 2);
  95. stream->read((char*) &bone_weight, sizeof(uint8_t));
  96. stream->read((char*) &edge_invisible, sizeof(uint8_t));
  97. return true;
  98. }
  99. };
  100. /// 材質
  101. class PmdMaterial
  102. {
  103. public:
  104. /// 減衰色
  105. float diffuse[4];
  106. /// 光沢度
  107. float power;
  108. /// 光沢色
  109. float specular[3];
  110. /// 環境色
  111. float ambient[3];
  112. /// トーンインデックス
  113. uint8_t toon_index;
  114. /// エッジ
  115. uint8_t edge_flag;
  116. /// インデックス数
  117. uint32_t index_count;
  118. /// テクスチャファイル名
  119. std::string texture_filename;
  120. /// スフィアファイル名
  121. std::string sphere_filename;
  122. bool Read(std::ifstream* stream)
  123. {
  124. char buffer[20];
  125. stream->read((char*) &diffuse, sizeof(float) * 4);
  126. stream->read((char*) &power, sizeof(float));
  127. stream->read((char*) &specular, sizeof(float) * 3);
  128. stream->read((char*) &ambient, sizeof(float) * 3);
  129. stream->read((char*) &toon_index, sizeof(uint8_t));
  130. stream->read((char*) &edge_flag, sizeof(uint8_t));
  131. stream->read((char*) &index_count, sizeof(uint32_t));
  132. stream->read((char*) &buffer, sizeof(char) * 20);
  133. char* pstar = strchr(buffer, '*');
  134. if (NULL == pstar)
  135. {
  136. texture_filename = std::string(buffer);
  137. sphere_filename.clear();
  138. }
  139. else {
  140. *pstar = 0;
  141. texture_filename = std::string(buffer);
  142. sphere_filename = std::string(pstar+1);
  143. }
  144. return true;
  145. }
  146. };
  147. enum class BoneType : uint8_t
  148. {
  149. Rotation,
  150. RotationAndMove,
  151. IkEffector,
  152. Unknown,
  153. IkEffectable,
  154. RotationEffectable,
  155. IkTarget,
  156. Invisible,
  157. Twist,
  158. RotationMovement
  159. };
  160. /// ボーン
  161. class PmdBone
  162. {
  163. public:
  164. /// ボーン名
  165. std::string name;
  166. /// ボーン名(英語)
  167. std::string name_english;
  168. /// 親ボーン番号
  169. uint16_t parent_bone_index;
  170. /// 末端ボーン番号
  171. uint16_t tail_pos_bone_index;
  172. /// ボーン種類
  173. BoneType bone_type;
  174. /// IKボーン番号
  175. uint16_t ik_parent_bone_index;
  176. /// ボーンのヘッドの位置
  177. float bone_head_pos[3];
  178. void Read(std::istream *stream)
  179. {
  180. char buffer[20];
  181. stream->read(buffer, 20);
  182. name = std::string(buffer);
  183. stream->read((char*) &parent_bone_index, sizeof(uint16_t));
  184. stream->read((char*) &tail_pos_bone_index, sizeof(uint16_t));
  185. stream->read((char*) &bone_type, sizeof(uint8_t));
  186. stream->read((char*) &ik_parent_bone_index, sizeof(uint16_t));
  187. stream->read((char*) &bone_head_pos, sizeof(float) * 3);
  188. }
  189. void ReadExpantion(std::istream *stream)
  190. {
  191. char buffer[20];
  192. stream->read(buffer, 20);
  193. name_english = std::string(buffer);
  194. }
  195. };
  196. /// IK
  197. class PmdIk
  198. {
  199. public:
  200. /// IKボーン番号
  201. uint16_t ik_bone_index;
  202. /// IKターゲットボーン番号
  203. uint16_t target_bone_index;
  204. /// 再帰回数
  205. uint16_t interations;
  206. /// 角度制限
  207. float angle_limit;
  208. /// 影響下ボーン番号
  209. std::vector<uint16_t> ik_child_bone_index;
  210. void Read(std::istream *stream)
  211. {
  212. stream->read((char *) &ik_bone_index, sizeof(uint16_t));
  213. stream->read((char *) &target_bone_index, sizeof(uint16_t));
  214. uint8_t ik_chain_length;
  215. stream->read((char*) &ik_chain_length, sizeof(uint8_t));
  216. stream->read((char *) &interations, sizeof(uint16_t));
  217. stream->read((char *) &angle_limit, sizeof(float));
  218. ik_child_bone_index.resize(ik_chain_length);
  219. for (int i = 0; i < ik_chain_length; i++)
  220. {
  221. stream->read((char *) &ik_child_bone_index[i], sizeof(uint16_t));
  222. }
  223. }
  224. };
  225. class PmdFaceVertex
  226. {
  227. public:
  228. int vertex_index;
  229. float position[3];
  230. void Read(std::istream *stream)
  231. {
  232. stream->read((char *) &vertex_index, sizeof(int));
  233. stream->read((char *) position, sizeof(float) * 3);
  234. }
  235. };
  236. enum class FaceCategory : uint8_t
  237. {
  238. Base,
  239. Eyebrow,
  240. Eye,
  241. Mouth,
  242. Other
  243. };
  244. class PmdFace
  245. {
  246. public:
  247. std::string name;
  248. FaceCategory type;
  249. std::vector<PmdFaceVertex> vertices;
  250. std::string name_english;
  251. void Read(std::istream *stream)
  252. {
  253. char buffer[20];
  254. stream->read(buffer, 20);
  255. name = std::string(buffer);
  256. int vertex_count;
  257. stream->read((char*) &vertex_count, sizeof(int));
  258. stream->read((char*) &type, sizeof(uint8_t));
  259. vertices.resize(vertex_count);
  260. for (int i = 0; i < vertex_count; i++)
  261. {
  262. vertices[i].Read(stream);
  263. }
  264. }
  265. void ReadExpantion(std::istream *stream)
  266. {
  267. char buffer[20];
  268. stream->read(buffer, 20);
  269. name_english = std::string(buffer);
  270. }
  271. };
  272. /// ボーン枠用の枠名
  273. class PmdBoneDispName
  274. {
  275. public:
  276. std::string bone_disp_name;
  277. std::string bone_disp_name_english;
  278. void Read(std::istream *stream)
  279. {
  280. char buffer[50];
  281. stream->read(buffer, 50);
  282. bone_disp_name = std::string(buffer);
  283. bone_disp_name_english.clear();
  284. }
  285. void ReadExpantion(std::istream *stream)
  286. {
  287. char buffer[50];
  288. stream->read(buffer, 50);
  289. bone_disp_name_english = std::string(buffer);
  290. }
  291. };
  292. class PmdBoneDisp
  293. {
  294. public:
  295. uint16_t bone_index;
  296. uint8_t bone_disp_index;
  297. void Read(std::istream *stream)
  298. {
  299. stream->read((char*) &bone_index, sizeof(uint16_t));
  300. stream->read((char*) &bone_disp_index, sizeof(uint8_t));
  301. }
  302. };
  303. /// 衝突形状
  304. enum class RigidBodyShape : uint8_t
  305. {
  306. /// 球
  307. Sphere = 0,
  308. /// 直方体
  309. Box = 1,
  310. /// カプセル
  311. Cpusel = 2
  312. };
  313. /// 剛体タイプ
  314. enum class RigidBodyType : uint8_t
  315. {
  316. /// ボーン追従
  317. BoneConnected = 0,
  318. /// 物理演算
  319. Physics = 1,
  320. /// 物理演算(Bone位置合せ)
  321. ConnectedPhysics = 2
  322. };
  323. /// 剛体
  324. class PmdRigidBody
  325. {
  326. public:
  327. /// 名前
  328. std::string name;
  329. /// 関連ボーン番号
  330. uint16_t related_bone_index;
  331. /// グループ番号
  332. uint8_t group_index;
  333. /// マスク
  334. uint16_t mask;
  335. /// 形状
  336. RigidBodyShape shape;
  337. /// 大きさ
  338. float size[3];
  339. /// 位置
  340. float position[3];
  341. /// 回転
  342. float orientation[3];
  343. /// 質量
  344. float weight;
  345. /// 移動ダンピング
  346. float linear_damping;
  347. /// 回転ダンピング
  348. float anglar_damping;
  349. /// 反発係数
  350. float restitution;
  351. /// 摩擦係数
  352. float friction;
  353. /// 演算方法
  354. RigidBodyType rigid_type;
  355. void Read(std::istream *stream)
  356. {
  357. char buffer[20];
  358. stream->read(buffer, sizeof(char) * 20);
  359. name = (std::string(buffer));
  360. stream->read((char*) &related_bone_index, sizeof(uint16_t));
  361. stream->read((char*) &group_index, sizeof(uint8_t));
  362. stream->read((char*) &mask, sizeof(uint16_t));
  363. stream->read((char*) &shape, sizeof(uint8_t));
  364. stream->read((char*) size, sizeof(float) * 3);
  365. stream->read((char*) position, sizeof(float) * 3);
  366. stream->read((char*) orientation, sizeof(float) * 3);
  367. stream->read((char*) &weight, sizeof(float));
  368. stream->read((char*) &linear_damping, sizeof(float));
  369. stream->read((char*) &anglar_damping, sizeof(float));
  370. stream->read((char*) &restitution, sizeof(float));
  371. stream->read((char*) &friction, sizeof(float));
  372. stream->read((char*) &rigid_type, sizeof(char));
  373. }
  374. };
  375. /// 剛体の拘束
  376. class PmdConstraint
  377. {
  378. public:
  379. /// 名前
  380. std::string name;
  381. /// 剛体Aのインデックス
  382. uint32_t rigid_body_index_a;
  383. /// 剛体Bのインデックス
  384. uint32_t rigid_body_index_b;
  385. /// 位置
  386. float position[3];
  387. /// 回転
  388. float orientation[3];
  389. /// 最小移動制限
  390. float linear_lower_limit[3];
  391. /// 最大移動制限
  392. float linear_upper_limit[3];
  393. /// 最小回転制限
  394. float angular_lower_limit[3];
  395. /// 最大回転制限
  396. float angular_upper_limit[3];
  397. /// 移動に対する復元力
  398. float linear_stiffness[3];
  399. /// 回転に対する復元力
  400. float angular_stiffness[3];
  401. void Read(std::istream *stream)
  402. {
  403. char buffer[20];
  404. stream->read(buffer, 20);
  405. name = std::string(buffer);
  406. stream->read((char *) &rigid_body_index_a, sizeof(uint32_t));
  407. stream->read((char *) &rigid_body_index_b, sizeof(uint32_t));
  408. stream->read((char *) position, sizeof(float) * 3);
  409. stream->read((char *) orientation, sizeof(float) * 3);
  410. stream->read((char *) linear_lower_limit, sizeof(float) * 3);
  411. stream->read((char *) linear_upper_limit, sizeof(float) * 3);
  412. stream->read((char *) angular_lower_limit, sizeof(float) * 3);
  413. stream->read((char *) angular_upper_limit, sizeof(float) * 3);
  414. stream->read((char *) linear_stiffness, sizeof(float) * 3);
  415. stream->read((char *) angular_stiffness, sizeof(float) * 3);
  416. }
  417. };
  418. /// PMDモデル
  419. class PmdModel
  420. {
  421. public:
  422. float version;
  423. PmdHeader header;
  424. std::vector<PmdVertex> vertices;
  425. std::vector<uint16_t> indices;
  426. std::vector<PmdMaterial> materials;
  427. std::vector<PmdBone> bones;
  428. std::vector<PmdIk> iks;
  429. std::vector<PmdFace> faces;
  430. std::vector<uint16_t> faces_indices;
  431. std::vector<PmdBoneDispName> bone_disp_name;
  432. std::vector<PmdBoneDisp> bone_disp;
  433. std::vector<std::string> toon_filenames;
  434. std::vector<PmdRigidBody> rigid_bodies;
  435. std::vector<PmdConstraint> constraints;
  436. static std::unique_ptr<PmdModel> LoadFromFile(const char *filename)
  437. {
  438. std::ifstream stream(filename, std::ios::binary);
  439. if (stream.fail())
  440. {
  441. std::cerr << "could not open \"" << filename << "\"" << std::endl;
  442. return nullptr;
  443. }
  444. auto result = LoadFromStream(&stream);
  445. stream.close();
  446. return result;
  447. }
  448. /// ファイルからPmdModelを生成する
  449. static std::unique_ptr<PmdModel> LoadFromStream(std::ifstream *stream)
  450. {
  451. auto result = mmd::make_unique<PmdModel>();
  452. char buffer[100];
  453. // magic
  454. char magic[3];
  455. stream->read(magic, 3);
  456. if (magic[0] != 'P' || magic[1] != 'm' || magic[2] != 'd')
  457. {
  458. std::cerr << "invalid file" << std::endl;
  459. return nullptr;
  460. }
  461. // version
  462. stream->read((char*) &(result->version), sizeof(float));
  463. if (result ->version != 1.0f)
  464. {
  465. std::cerr << "invalid version" << std::endl;
  466. return nullptr;
  467. }
  468. // header
  469. result->header.Read(stream);
  470. // vertices
  471. uint32_t vertex_num;
  472. stream->read((char*) &vertex_num, sizeof(uint32_t));
  473. result->vertices.resize(vertex_num);
  474. for (uint32_t i = 0; i < vertex_num; i++)
  475. {
  476. result->vertices[i].Read(stream);
  477. }
  478. // indices
  479. uint32_t index_num;
  480. stream->read((char*) &index_num, sizeof(uint32_t));
  481. result->indices.resize(index_num);
  482. for (uint32_t i = 0; i < index_num; i++)
  483. {
  484. stream->read((char*) &result->indices[i], sizeof(uint16_t));
  485. }
  486. // materials
  487. uint32_t material_num;
  488. stream->read((char*) &material_num, sizeof(uint32_t));
  489. result->materials.resize(material_num);
  490. for (uint32_t i = 0; i < material_num; i++)
  491. {
  492. result->materials[i].Read(stream);
  493. }
  494. // bones
  495. uint16_t bone_num;
  496. stream->read((char*) &bone_num, sizeof(uint16_t));
  497. result->bones.resize(bone_num);
  498. for (uint32_t i = 0; i < bone_num; i++)
  499. {
  500. result->bones[i].Read(stream);
  501. }
  502. // iks
  503. uint16_t ik_num;
  504. stream->read((char*) &ik_num, sizeof(uint16_t));
  505. result->iks.resize(ik_num);
  506. for (uint32_t i = 0; i < ik_num; i++)
  507. {
  508. result->iks[i].Read(stream);
  509. }
  510. // faces
  511. uint16_t face_num;
  512. stream->read((char*) &face_num, sizeof(uint16_t));
  513. result->faces.resize(face_num);
  514. for (uint32_t i = 0; i < face_num; i++)
  515. {
  516. result->faces[i].Read(stream);
  517. }
  518. // face frames
  519. uint8_t face_frame_num;
  520. stream->read((char*) &face_frame_num, sizeof(uint8_t));
  521. result->faces_indices.resize(face_frame_num);
  522. for (uint32_t i = 0; i < face_frame_num; i++)
  523. {
  524. stream->read((char*) &result->faces_indices[i], sizeof(uint16_t));
  525. }
  526. // bone names
  527. uint8_t bone_disp_num;
  528. stream->read((char*) &bone_disp_num, sizeof(uint8_t));
  529. result->bone_disp_name.resize(bone_disp_num);
  530. for (uint32_t i = 0; i < bone_disp_num; i++)
  531. {
  532. result->bone_disp_name[i].Read(stream);
  533. }
  534. // bone frame
  535. uint32_t bone_frame_num;
  536. stream->read((char*) &bone_frame_num, sizeof(uint32_t));
  537. result->bone_disp.resize(bone_frame_num);
  538. for (uint32_t i = 0; i < bone_frame_num; i++)
  539. {
  540. result->bone_disp[i].Read(stream);
  541. }
  542. // english name
  543. bool english;
  544. stream->read((char*) &english, sizeof(char));
  545. if (english)
  546. {
  547. result->header.ReadExtension(stream);
  548. for (uint32_t i = 0; i < bone_num; i++)
  549. {
  550. result->bones[i].ReadExpantion(stream);
  551. }
  552. for (uint32_t i = 0; i < face_num; i++)
  553. {
  554. if (result->faces[i].type == pmd::FaceCategory::Base)
  555. {
  556. continue;
  557. }
  558. result->faces[i].ReadExpantion(stream);
  559. }
  560. for (uint32_t i = 0; i < result->bone_disp_name.size(); i++)
  561. {
  562. result->bone_disp_name[i].ReadExpantion(stream);
  563. }
  564. }
  565. // toon textures
  566. if (stream->peek() == std::ios::traits_type::eof())
  567. {
  568. result->toon_filenames.clear();
  569. }
  570. else {
  571. result->toon_filenames.resize(10);
  572. for (uint32_t i = 0; i < 10; i++)
  573. {
  574. stream->read(buffer, 100);
  575. result->toon_filenames[i] = std::string(buffer);
  576. }
  577. }
  578. // physics
  579. if (stream->peek() == std::ios::traits_type::eof())
  580. {
  581. result->rigid_bodies.clear();
  582. result->constraints.clear();
  583. }
  584. else {
  585. uint32_t rigid_body_num;
  586. stream->read((char*) &rigid_body_num, sizeof(uint32_t));
  587. result->rigid_bodies.resize(rigid_body_num);
  588. for (uint32_t i = 0; i < rigid_body_num; i++)
  589. {
  590. result->rigid_bodies[i].Read(stream);
  591. }
  592. uint32_t constraint_num;
  593. stream->read((char*) &constraint_num, sizeof(uint32_t));
  594. result->constraints.resize(constraint_num);
  595. for (uint32_t i = 0; i < constraint_num; i++)
  596. {
  597. result->constraints[i].Read(stream);
  598. }
  599. }
  600. if (stream->peek() != std::ios::traits_type::eof())
  601. {
  602. std::cerr << "there is unknown data" << std::endl;
  603. }
  604. return result;
  605. }
  606. };
  607. }