MMDPmxParser.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include <utility>
  2. #include "MMDPmxParser.h"
  3. #include "Exceptional.h"
  4. #include "../contrib/ConvertUTF/ConvertUTF.h"
  5. namespace pmx
  6. {
  7. /// インデックス値を読み込む
  8. int ReadIndex(std::istream *stream, int size)
  9. {
  10. switch (size)
  11. {
  12. case 1:
  13. uint8_t tmp8;
  14. stream->read((char*) &tmp8, sizeof(uint8_t));
  15. if (255 == tmp8)
  16. {
  17. return -1;
  18. }
  19. else {
  20. return (int) tmp8;
  21. }
  22. case 2:
  23. uint16_t tmp16;
  24. stream->read((char*) &tmp16, sizeof(uint16_t));
  25. if (65535 == tmp16)
  26. {
  27. return -1;
  28. }
  29. else {
  30. return (int) tmp16;
  31. }
  32. case 4:
  33. int tmp32;
  34. stream->read((char*) &tmp32, sizeof(int));
  35. return tmp32;
  36. default:
  37. return -1;
  38. }
  39. }
  40. /// 文字列を読み込む
  41. std::string ReadString(std::istream *stream, uint8_t encoding)
  42. {
  43. int size;
  44. stream->read((char*) &size, sizeof(int));
  45. std::vector<char> buffer;
  46. if (size == 0)
  47. {
  48. return std::string("");
  49. }
  50. buffer.reserve(size);
  51. stream->read((char*) buffer.data(), size);
  52. if (encoding == 0)
  53. {
  54. // UTF16 to UTF8
  55. std::string result;
  56. const char* sourceStart = buffer.data();
  57. const unsigned int targetSize = size * 3; // enough to encode
  58. char* targetStart = new char[targetSize]();
  59. const char* targetReserved = targetStart;
  60. ConversionFlags flags = ConversionFlags::lenientConversion;
  61. ConversionResult conversionResult;
  62. if( ( conversionResult = ConvertUTF16toUTF8(
  63. (const UTF16**)&sourceStart, (const UTF16*)(sourceStart + size),
  64. (UTF8**)&targetStart, (UTF8*)(targetStart + targetSize),
  65. flags) ) != ConversionResult::conversionOK) {
  66. throw DeadlyImportError( "Convert " + std::string(sourceStart) + " to UTF8 is not valid." );
  67. }
  68. result.assign(targetReserved, targetStart - targetReserved);
  69. delete[] targetReserved;
  70. return result;
  71. }
  72. else
  73. {
  74. // the name is already UTF8
  75. return std::string((const char*)buffer.data(), size);
  76. }
  77. }
  78. void PmxSetting::Read(std::istream *stream)
  79. {
  80. uint8_t count;
  81. stream->read((char*) &count, sizeof(uint8_t));
  82. if (count < 8)
  83. {
  84. throw;
  85. }
  86. stream->read((char*) &encoding, sizeof(uint8_t));
  87. stream->read((char*) &uv, sizeof(uint8_t));
  88. stream->read((char*) &vertex_index_size, sizeof(uint8_t));
  89. stream->read((char*) &texture_index_size, sizeof(uint8_t));
  90. stream->read((char*) &material_index_size, sizeof(uint8_t));
  91. stream->read((char*) &bone_index_size, sizeof(uint8_t));
  92. stream->read((char*) &morph_index_size, sizeof(uint8_t));
  93. stream->read((char*) &rigidbody_index_size, sizeof(uint8_t));
  94. uint8_t temp;
  95. for (int i = 8; i < count; i++)
  96. {
  97. stream->read((char*)&temp, sizeof(uint8_t));
  98. }
  99. }
  100. void PmxVertexSkinningBDEF1::Read(std::istream *stream, PmxSetting *setting)
  101. {
  102. this->bone_index = ReadIndex(stream, setting->bone_index_size);
  103. }
  104. void PmxVertexSkinningBDEF2::Read(std::istream *stream, PmxSetting *setting)
  105. {
  106. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  107. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  108. stream->read((char*) &this->bone_weight, sizeof(float));
  109. }
  110. void PmxVertexSkinningBDEF4::Read(std::istream *stream, PmxSetting *setting)
  111. {
  112. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  113. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  114. this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
  115. this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
  116. stream->read((char*) &this->bone_weight1, sizeof(float));
  117. stream->read((char*) &this->bone_weight2, sizeof(float));
  118. stream->read((char*) &this->bone_weight3, sizeof(float));
  119. stream->read((char*) &this->bone_weight4, sizeof(float));
  120. }
  121. void PmxVertexSkinningSDEF::Read(std::istream *stream, PmxSetting *setting)
  122. {
  123. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  124. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  125. stream->read((char*) &this->bone_weight, sizeof(float));
  126. stream->read((char*) this->sdef_c, sizeof(float) * 3);
  127. stream->read((char*) this->sdef_r0, sizeof(float) * 3);
  128. stream->read((char*) this->sdef_r1, sizeof(float) * 3);
  129. }
  130. void PmxVertexSkinningQDEF::Read(std::istream *stream, PmxSetting *setting)
  131. {
  132. this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
  133. this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
  134. this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
  135. this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
  136. stream->read((char*) &this->bone_weight1, sizeof(float));
  137. stream->read((char*) &this->bone_weight2, sizeof(float));
  138. stream->read((char*) &this->bone_weight3, sizeof(float));
  139. stream->read((char*) &this->bone_weight4, sizeof(float));
  140. }
  141. void PmxVertex::Read(std::istream *stream, PmxSetting *setting)
  142. {
  143. stream->read((char*) this->position, sizeof(float) * 3);
  144. stream->read((char*) this->normal, sizeof(float) * 3);
  145. stream->read((char*) this->uv, sizeof(float) * 2);
  146. for (int i = 0; i < setting->uv; ++i)
  147. {
  148. stream->read((char*) this->uva[i], sizeof(float) * 4);
  149. }
  150. stream->read((char*) &this->skinning_type, sizeof(PmxVertexSkinningType));
  151. switch (this->skinning_type)
  152. {
  153. case PmxVertexSkinningType::BDEF1:
  154. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF1>();
  155. break;
  156. case PmxVertexSkinningType::BDEF2:
  157. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF2>();
  158. break;
  159. case PmxVertexSkinningType::BDEF4:
  160. this->skinning = mmd::make_unique<PmxVertexSkinningBDEF4>();
  161. break;
  162. case PmxVertexSkinningType::SDEF:
  163. this->skinning = mmd::make_unique<PmxVertexSkinningSDEF>();
  164. break;
  165. case PmxVertexSkinningType::QDEF:
  166. this->skinning = mmd::make_unique<PmxVertexSkinningQDEF>();
  167. break;
  168. default:
  169. throw "invalid skinning type";
  170. }
  171. this->skinning->Read(stream, setting);
  172. stream->read((char*) &this->edge, sizeof(float));
  173. }
  174. void PmxMaterial::Read(std::istream *stream, PmxSetting *setting)
  175. {
  176. this->material_name = std::move(ReadString(stream, setting->encoding));
  177. this->material_english_name = std::move(ReadString(stream, setting->encoding));
  178. stream->read((char*) this->diffuse, sizeof(float) * 4);
  179. stream->read((char*) this->specular, sizeof(float) * 3);
  180. stream->read((char*) &this->specularlity, sizeof(float));
  181. stream->read((char*) this->ambient, sizeof(float) * 3);
  182. stream->read((char*) &this->flag, sizeof(uint8_t));
  183. stream->read((char*) this->edge_color, sizeof(float) * 4);
  184. stream->read((char*) &this->edge_size, sizeof(float));
  185. this->diffuse_texture_index = ReadIndex(stream, setting->texture_index_size);
  186. this->sphere_texture_index = ReadIndex(stream, setting->texture_index_size);
  187. stream->read((char*) &this->sphere_op_mode, sizeof(uint8_t));
  188. stream->read((char*) &this->common_toon_flag, sizeof(uint8_t));
  189. if (this->common_toon_flag)
  190. {
  191. stream->read((char*) &this->toon_texture_index, sizeof(uint8_t));
  192. }
  193. else {
  194. this->toon_texture_index = ReadIndex(stream, setting->texture_index_size);
  195. }
  196. this->memo = std::move(ReadString(stream, setting->encoding));
  197. stream->read((char*) &this->index_count, sizeof(int));
  198. }
  199. void PmxIkLink::Read(std::istream *stream, PmxSetting *setting)
  200. {
  201. this->link_target = ReadIndex(stream, setting->bone_index_size);
  202. stream->read((char*) &this->angle_lock, sizeof(uint8_t));
  203. if (angle_lock == 1)
  204. {
  205. stream->read((char*) this->max_radian, sizeof(float) * 3);
  206. stream->read((char*) this->min_radian, sizeof(float) * 3);
  207. }
  208. }
  209. void PmxBone::Read(std::istream *stream, PmxSetting *setting)
  210. {
  211. this->bone_name = std::move(ReadString(stream, setting->encoding));
  212. this->bone_english_name = std::move(ReadString(stream, setting->encoding));
  213. stream->read((char*) this->position, sizeof(float) * 3);
  214. this->parent_index = ReadIndex(stream, setting->bone_index_size);
  215. stream->read((char*) &this->level, sizeof(int));
  216. stream->read((char*) &this->bone_flag, sizeof(uint16_t));
  217. if (this->bone_flag & 0x0001) {
  218. this->target_index = ReadIndex(stream, setting->bone_index_size);
  219. }
  220. else {
  221. stream->read((char*)this->offset, sizeof(float) * 3);
  222. }
  223. if (this->bone_flag & (0x0100 | 0x0200)) {
  224. this->grant_parent_index = ReadIndex(stream, setting->bone_index_size);
  225. stream->read((char*) &this->grant_weight, sizeof(float));
  226. }
  227. if (this->bone_flag & 0x0400) {
  228. stream->read((char*)this->lock_axis_orientation, sizeof(float) * 3);
  229. }
  230. if (this->bone_flag & 0x0800) {
  231. stream->read((char*)this->local_axis_x_orientation, sizeof(float) * 3);
  232. stream->read((char*)this->local_axis_y_orientation, sizeof(float) * 3);
  233. }
  234. if (this->bone_flag & 0x2000) {
  235. stream->read((char*) &this->key, sizeof(int));
  236. }
  237. if (this->bone_flag & 0x0020) {
  238. this->ik_target_bone_index = ReadIndex(stream, setting->bone_index_size);
  239. stream->read((char*) &ik_loop, sizeof(int));
  240. stream->read((char*) &ik_loop_angle_limit, sizeof(float));
  241. stream->read((char*) &ik_link_count, sizeof(int));
  242. this->ik_links = mmd::make_unique<PmxIkLink []>(ik_link_count);
  243. for (int i = 0; i < ik_link_count; i++) {
  244. ik_links[i].Read(stream, setting);
  245. }
  246. }
  247. }
  248. void PmxMorphVertexOffset::Read(std::istream *stream, PmxSetting *setting)
  249. {
  250. this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
  251. stream->read((char*)this->position_offset, sizeof(float) * 3);
  252. }
  253. void PmxMorphUVOffset::Read(std::istream *stream, PmxSetting *setting)
  254. {
  255. this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
  256. stream->read((char*)this->uv_offset, sizeof(float) * 4);
  257. }
  258. void PmxMorphBoneOffset::Read(std::istream *stream, PmxSetting *setting)
  259. {
  260. this->bone_index = ReadIndex(stream, setting->bone_index_size);
  261. stream->read((char*)this->translation, sizeof(float) * 3);
  262. stream->read((char*)this->rotation, sizeof(float) * 4);
  263. }
  264. void PmxMorphMaterialOffset::Read(std::istream *stream, PmxSetting *setting)
  265. {
  266. this->material_index = ReadIndex(stream, setting->material_index_size);
  267. stream->read((char*) &this->offset_operation, sizeof(uint8_t));
  268. stream->read((char*)this->diffuse, sizeof(float) * 4);
  269. stream->read((char*)this->specular, sizeof(float) * 3);
  270. stream->read((char*) &this->specularity, sizeof(float));
  271. stream->read((char*)this->ambient, sizeof(float) * 3);
  272. stream->read((char*)this->edge_color, sizeof(float) * 4);
  273. stream->read((char*) &this->edge_size, sizeof(float));
  274. stream->read((char*)this->texture_argb, sizeof(float) * 4);
  275. stream->read((char*)this->sphere_texture_argb, sizeof(float) * 4);
  276. stream->read((char*)this->toon_texture_argb, sizeof(float) * 4);
  277. }
  278. void PmxMorphGroupOffset::Read(std::istream *stream, PmxSetting *setting)
  279. {
  280. this->morph_index = ReadIndex(stream, setting->morph_index_size);
  281. stream->read((char*) &this->morph_weight, sizeof(float));
  282. }
  283. void PmxMorphFlipOffset::Read(std::istream *stream, PmxSetting *setting)
  284. {
  285. this->morph_index = ReadIndex(stream, setting->morph_index_size);
  286. stream->read((char*) &this->morph_value, sizeof(float));
  287. }
  288. void PmxMorphImplusOffset::Read(std::istream *stream, PmxSetting *setting)
  289. {
  290. this->rigid_body_index = ReadIndex(stream, setting->rigidbody_index_size);
  291. stream->read((char*) &this->is_local, sizeof(uint8_t));
  292. stream->read((char*)this->velocity, sizeof(float) * 3);
  293. stream->read((char*)this->angular_torque, sizeof(float) * 3);
  294. }
  295. void PmxMorph::Read(std::istream *stream, PmxSetting *setting)
  296. {
  297. this->morph_name = ReadString(stream, setting->encoding);
  298. this->morph_english_name = ReadString(stream, setting->encoding);
  299. stream->read((char*) &category, sizeof(MorphCategory));
  300. stream->read((char*) &morph_type, sizeof(MorphType));
  301. stream->read((char*) &this->offset_count, sizeof(int));
  302. switch (this->morph_type)
  303. {
  304. case MorphType::Group:
  305. group_offsets = mmd::make_unique<PmxMorphGroupOffset []>(this->offset_count);
  306. for (int i = 0; i < offset_count; i++)
  307. {
  308. group_offsets[i].Read(stream, setting);
  309. }
  310. break;
  311. case MorphType::Vertex:
  312. vertex_offsets = mmd::make_unique<PmxMorphVertexOffset []>(this->offset_count);
  313. for (int i = 0; i < offset_count; i++)
  314. {
  315. vertex_offsets[i].Read(stream, setting);
  316. }
  317. break;
  318. case MorphType::Bone:
  319. bone_offsets = mmd::make_unique<PmxMorphBoneOffset []>(this->offset_count);
  320. for (int i = 0; i < offset_count; i++)
  321. {
  322. bone_offsets[i].Read(stream, setting);
  323. }
  324. break;
  325. case MorphType::Matrial:
  326. material_offsets = mmd::make_unique<PmxMorphMaterialOffset []>(this->offset_count);
  327. for (int i = 0; i < offset_count; i++)
  328. {
  329. material_offsets[i].Read(stream, setting);
  330. }
  331. break;
  332. case MorphType::UV:
  333. case MorphType::AdditionalUV1:
  334. case MorphType::AdditionalUV2:
  335. case MorphType::AdditionalUV3:
  336. case MorphType::AdditionalUV4:
  337. uv_offsets = mmd::make_unique<PmxMorphUVOffset []>(this->offset_count);
  338. for (int i = 0; i < offset_count; i++)
  339. {
  340. uv_offsets[i].Read(stream, setting);
  341. }
  342. break;
  343. default:
  344. throw;
  345. }
  346. }
  347. void PmxFrameElement::Read(std::istream *stream, PmxSetting *setting)
  348. {
  349. stream->read((char*) &this->element_target, sizeof(uint8_t));
  350. if (this->element_target == 0x00)
  351. {
  352. this->index = ReadIndex(stream, setting->bone_index_size);
  353. }
  354. else {
  355. this->index = ReadIndex(stream, setting->morph_index_size);
  356. }
  357. }
  358. void PmxFrame::Read(std::istream *stream, PmxSetting *setting)
  359. {
  360. this->frame_name = ReadString(stream, setting->encoding);
  361. this->frame_english_name = ReadString(stream, setting->encoding);
  362. stream->read((char*) &this->frame_flag, sizeof(uint8_t));
  363. stream->read((char*) &this->element_count, sizeof(int));
  364. this->elements = mmd::make_unique<PmxFrameElement []>(this->element_count);
  365. for (int i = 0; i < this->element_count; i++)
  366. {
  367. this->elements[i].Read(stream, setting);
  368. }
  369. }
  370. void PmxRigidBody::Read(std::istream *stream, PmxSetting *setting)
  371. {
  372. this->girid_body_name = ReadString(stream, setting->encoding);
  373. this->girid_body_english_name = ReadString(stream, setting->encoding);
  374. this->target_bone = ReadIndex(stream, setting->bone_index_size);
  375. stream->read((char*) &this->group, sizeof(uint8_t));
  376. stream->read((char*) &this->mask, sizeof(uint16_t));
  377. stream->read((char*) &this->shape, sizeof(uint8_t));
  378. stream->read((char*) this->size, sizeof(float) * 3);
  379. stream->read((char*) this->position, sizeof(float) * 3);
  380. stream->read((char*) this->orientation, sizeof(float) * 3);
  381. stream->read((char*) &this->mass, sizeof(float));
  382. stream->read((char*) &this->move_attenuation, sizeof(float));
  383. stream->read((char*) &this->rotation_attenuation, sizeof(float));
  384. stream->read((char*) &this->repulsion, sizeof(float));
  385. stream->read((char*) &this->friction, sizeof(float));
  386. stream->read((char*) &this->physics_calc_type, sizeof(uint8_t));
  387. }
  388. void PmxJointParam::Read(std::istream *stream, PmxSetting *setting)
  389. {
  390. this->rigid_body1 = ReadIndex(stream, setting->rigidbody_index_size);
  391. this->rigid_body2 = ReadIndex(stream, setting->rigidbody_index_size);
  392. stream->read((char*) this->position, sizeof(float) * 3);
  393. stream->read((char*) this->orientaiton, sizeof(float) * 3);
  394. stream->read((char*) this->move_limitation_min, sizeof(float) * 3);
  395. stream->read((char*) this->move_limitation_max, sizeof(float) * 3);
  396. stream->read((char*) this->rotation_limitation_min, sizeof(float) * 3);
  397. stream->read((char*) this->rotation_limitation_max, sizeof(float) * 3);
  398. stream->read((char*) this->spring_move_coefficient, sizeof(float) * 3);
  399. stream->read((char*) this->spring_rotation_coefficient, sizeof(float) * 3);
  400. }
  401. void PmxJoint::Read(std::istream *stream, PmxSetting *setting)
  402. {
  403. this->joint_name = ReadString(stream, setting->encoding);
  404. this->joint_english_name = ReadString(stream, setting->encoding);
  405. stream->read((char*) &this->joint_type, sizeof(uint8_t));
  406. this->param.Read(stream, setting);
  407. }
  408. void PmxAncherRigidBody::Read(std::istream *stream, PmxSetting *setting)
  409. {
  410. this->related_rigid_body = ReadIndex(stream, setting->rigidbody_index_size);
  411. this->related_vertex = ReadIndex(stream, setting->vertex_index_size);
  412. stream->read((char*) &this->is_near, sizeof(uint8_t));
  413. }
  414. void PmxSoftBody::Read(std::istream *stream, PmxSetting *setting)
  415. {
  416. // 未実装
  417. std::cerr << "Not Implemented Exception" << std::endl;
  418. throw;
  419. }
  420. void PmxModel::Init()
  421. {
  422. this->version = 0.0f;
  423. this->model_name.clear();
  424. this->model_english_name.clear();
  425. this->model_comment.clear();
  426. this->model_english_comment.clear();
  427. this->vertex_count = 0;
  428. this->vertices = nullptr;
  429. this->index_count = 0;
  430. this->indices = nullptr;
  431. this->texture_count = 0;
  432. this->textures = nullptr;
  433. this->material_count = 0;
  434. this->materials = nullptr;
  435. this->bone_count = 0;
  436. this->bones = nullptr;
  437. this->morph_count = 0;
  438. this->morphs = nullptr;
  439. this->frame_count = 0;
  440. this->frames = nullptr;
  441. this->rigid_body_count = 0;
  442. this->rigid_bodies = nullptr;
  443. this->joint_count = 0;
  444. this->joints = nullptr;
  445. this->soft_body_count = 0;
  446. this->soft_bodies = nullptr;
  447. }
  448. void PmxModel::Read(std::istream *stream)
  449. {
  450. // マジック
  451. char magic[4];
  452. stream->read((char*) magic, sizeof(char) * 4);
  453. if (magic[0] != 0x50 || magic[1] != 0x4d || magic[2] != 0x58 || magic[3] != 0x20)
  454. {
  455. std::cerr << "invalid magic number." << std::endl;
  456. throw;
  457. }
  458. // バージョン
  459. stream->read((char*) &version, sizeof(float));
  460. if (version != 2.0f && version != 2.1f)
  461. {
  462. std::cerr << "this is not ver2.0 or ver2.1 but " << version << "." << std::endl;
  463. throw;
  464. }
  465. // ファイル設定
  466. this->setting.Read(stream);
  467. // モデル情報
  468. this->model_name = std::move(ReadString(stream, setting.encoding));
  469. this->model_english_name = std::move(ReadString(stream, setting.encoding));
  470. this->model_comment = std::move(ReadString(stream, setting.encoding));
  471. this->model_english_comment = std::move(ReadString(stream, setting.encoding));
  472. // 頂点
  473. stream->read((char*) &vertex_count, sizeof(int));
  474. this->vertices = mmd::make_unique<PmxVertex []>(vertex_count);
  475. for (int i = 0; i < vertex_count; i++)
  476. {
  477. vertices[i].Read(stream, &setting);
  478. }
  479. // 面
  480. stream->read((char*) &index_count, sizeof(int));
  481. this->indices = mmd::make_unique<int []>(index_count);
  482. for (int i = 0; i < index_count; i++)
  483. {
  484. this->indices[i] = ReadIndex(stream, setting.vertex_index_size);
  485. }
  486. // テクスチャ
  487. stream->read((char*) &texture_count, sizeof(int));
  488. this->textures = mmd::make_unique<std::string []>(texture_count);
  489. for (int i = 0; i < texture_count; i++)
  490. {
  491. this->textures[i] = ReadString(stream, setting.encoding);
  492. }
  493. // マテリアル
  494. stream->read((char*) &material_count, sizeof(int));
  495. this->materials = mmd::make_unique<PmxMaterial []>(material_count);
  496. for (int i = 0; i < material_count; i++)
  497. {
  498. this->materials[i].Read(stream, &setting);
  499. }
  500. // ボーン
  501. stream->read((char*) &this->bone_count, sizeof(int));
  502. this->bones = mmd::make_unique<PmxBone []>(this->bone_count);
  503. for (int i = 0; i < this->bone_count; i++)
  504. {
  505. this->bones[i].Read(stream, &setting);
  506. }
  507. // モーフ
  508. stream->read((char*) &this->morph_count, sizeof(int));
  509. this->morphs = mmd::make_unique<PmxMorph []>(this->morph_count);
  510. for (int i = 0; i < this->morph_count; i++)
  511. {
  512. this->morphs[i].Read(stream, &setting);
  513. }
  514. // 表示枠
  515. stream->read((char*) &this->frame_count, sizeof(int));
  516. this->frames = mmd::make_unique<PmxFrame []>(this->frame_count);
  517. for (int i = 0; i < this->frame_count; i++)
  518. {
  519. this->frames[i].Read(stream, &setting);
  520. }
  521. // 剛体
  522. stream->read((char*) &this->rigid_body_count, sizeof(int));
  523. this->rigid_bodies = mmd::make_unique<PmxRigidBody []>(this->rigid_body_count);
  524. for (int i = 0; i < this->rigid_body_count; i++)
  525. {
  526. this->rigid_bodies[i].Read(stream, &setting);
  527. }
  528. // ジョイント
  529. stream->read((char*) &this->joint_count, sizeof(int));
  530. this->joints = mmd::make_unique<PmxJoint []>(this->joint_count);
  531. for (int i = 0; i < this->joint_count; i++)
  532. {
  533. this->joints[i].Read(stream, &setting);
  534. }
  535. //// ソフトボディ
  536. //if (this->version == 2.1f)
  537. //{
  538. // stream->read((char*) &this->soft_body_count, sizeof(int));
  539. // this->soft_bodies = mmd::make_unique<PmxSoftBody []>(this->soft_body_count);
  540. // for (int i = 0; i < this->soft_body_count; i++)
  541. // {
  542. // this->soft_bodies[i].Read(stream, &setting);
  543. // }
  544. //}
  545. }
  546. //std::unique_ptr<PmxModel> ReadFromFile(const char *filename)
  547. //{
  548. // auto stream = std::ifstream(filename, std::ios_base::binary);
  549. // auto pmx = PmxModel::ReadFromStream(&stream);
  550. // if (!stream.eof())
  551. // {
  552. // std::cerr << "don't reach the end of file." << std::endl;
  553. // }
  554. // stream.close();
  555. // return pmx;
  556. //}
  557. //std::unique_ptr<PmxModel> ReadFromStream(std::istream *stream)
  558. //{
  559. // auto pmx = mmd::make_unique<PmxModel>();
  560. // pmx->Read(stream);
  561. // return pmx;
  562. //}
  563. }