MMDPmdParser.h 15 KB

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