MMDPmxParser.cpp 20 KB

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