13 bool IO::write(const char *path)
15 binary::FileWriter w(path);
20 template<class READER>
22 read(READER &reader, Vertex &v)
24 unsigned int pos=reader.getPos();
30 reader.get(v.weight0);
31 reader.get(v.edge_flag);
32 assert(reader.getPos()-pos==38);
36 template<class READER>
38 read(READER &reader, Material &m)
40 unsigned int pos=reader.getPos();
41 reader.get(m.diffuse);
42 reader.get(m.shinness);
43 reader.get(m.specular);
44 reader.get(m.ambient);
45 reader.get(m.toon_index);
47 reader.get(m.vertex_count);
48 reader.get(m.texture);
49 assert(reader.getPos()-pos==70);
53 template<class READER>
55 read(READER &reader, Bone &b)
57 unsigned int pos=reader.getPos();
59 reader.get(b.parent_index);
60 reader.get(b.tail_index);
61 b.type=static_cast<BONE_TYPE>(reader.getUchar());
62 reader.get(b.ik_index);
64 assert(reader.getPos()-pos==39);
67 // 11+2xIK_COUNT bytes
68 template<class READER>
70 read(READER &reader, IK &ik)
74 reader.get(ik.target);
75 reader.get(ik.length);
76 reader.get(ik.iterations);
77 reader.get(ik.weight);
79 for(unsigned short j=0; j<ik.length; ++j){
80 ik.children.push_back(reader.getUshort());
84 // 25+12xMORPH_COUNT bytes
85 template<class READER>
87 read(READER &reader, Morph &m)
91 reader.get(m.vertex_count);
92 m.type=static_cast<MORPH_TYPE>(reader.getUchar());
93 // 12 x vertex_count bytes
94 for(unsigned short i=0; i<m.vertex_count; ++i){
95 m.indices.push_back(reader.getUint());
96 m.pos_list.push_back(Vector3());
97 reader.get(m.pos_list.back());
102 template<class READER>
104 read(READER &reader, RigidBody &r)
106 unsigned int pos=reader.getPos();
108 reader.get(r.boneIndex);
110 reader.get(r.target);
111 r.shapeType=static_cast<SHAPE_TYPE>(reader.getUchar());
115 reader.get(r.position);
116 reader.get(r.rotation);
117 reader.get(r.weight);
118 reader.get(r.linearDamping);
119 reader.get(r.angularDamping);
120 reader.get(r.restitution);
121 reader.template get<float>(r.friction);
122 r.processType=static_cast<PROCESS_TYPE>(reader.getUchar());
123 assert(reader.getPos()-pos==83);
127 template<class READER>
129 read(READER &reader, Constraint &c)
131 unsigned int base_pos=reader.getPos();
133 reader.get(c.rigidA);
134 reader.get(c.rigidB);
137 reader.get(c.constraintPosMin);
138 reader.get(c.constraintPosMax);
139 reader.get(c.constraintRotMin);
140 reader.get(c.constraintRotMax);
141 reader.get(c.springPos);
142 reader.get(c.springRot);
143 assert(reader.getPos()-base_pos==124);
149 binary::IReader &reader_;
152 Impl(IO &io, binary::IReader &reader)
153 : io_(io), reader_(reader)
161 if(!parseVertices()){
167 if(!parseMaterials()){
179 if(!parseFaceList()){
182 if(!parseBoneGroupList()){
185 if(!parseBoneList()){
192 ////////////////////////////////////////////////////////////
194 ////////////////////////////////////////////////////////////
196 ////////////////////////////////////////////////////////////
197 if(reader_.getChar()){
198 if(!parseEnglishName()){
201 if(!parseEnglishBone()){
204 if(!parseEnglishMorph()){
207 if(!parseEnglishBoneList()){
216 ////////////////////////////////////////////////////////////
217 if(!parseToonTextures()){
225 ////////////////////////////////////////////////////////////
229 if(!parseConstraint()){
234 assert(reader_.isEnd());
240 bool parseConstraint()
242 unsigned int count=reader_.getUint();
243 for(unsigned int i=0; i<count; ++i){
244 io_.constraints.push_back(Constraint());
245 read(reader_, io_.constraints.back());
252 unsigned int count=reader_.getUint();
253 for(unsigned int i=0; i<count; ++i){
254 io_.rigidbodies.push_back(RigidBody());
255 read(reader_, io_.rigidbodies.back());
260 bool parseToonTextures()
262 for(size_t i=0; i<10; ++i){
263 reader_.get(io_.toon_textures[i]);
268 bool parseEnglishBoneList()
270 for(size_t i=0; i<io_.bone_group_list.size(); ++i){
271 reader_.get(io_.bone_group_list[i].english_name);
276 bool parseEnglishMorph()
278 int count=io_.morph_list.size()-1;
279 for(int i=0; i<count; ++i){
280 reader_.get(io_.morph_list[i].english_name);
285 bool parseEnglishBone()
287 for(size_t i=0; i<io_.bones.size(); ++i){
288 reader_.get(io_.bones[i].english_name);
293 bool parseEnglishName()
295 reader_.get(io_.english_name);
296 reader_.get(io_.english_comment);
302 unsigned int count=reader_.getUint();
303 for(unsigned int i=0; i<count; ++i){
304 unsigned short bone=reader_.getUshort();
305 unsigned char disp=reader_.getUchar();
306 io_.bone_display_list.push_back(std::make_pair(bone, disp));
311 bool parseBoneGroupList()
313 unsigned int count=reader_.getUchar();
314 for(unsigned int i=0; i<count; ++i){
315 io_.bone_group_list.push_back(BoneGroup());
316 reader_.get(io_.bone_group_list.back().name);
323 unsigned int count=reader_.getUchar();
324 for(unsigned int i=0; i<count; ++i){
325 io_.face_list.push_back(reader_.getUshort());
332 unsigned int count=reader_.getUshort();
333 for(unsigned int i=0; i<count; ++i){
334 io_.morph_list.push_back(Morph());
335 read(reader_, io_.morph_list.back());
342 unsigned int count=reader_.getUshort();
343 for(unsigned int i=0; i<count; ++i){
344 io_.ik_list.push_back(IK());
345 read(reader_, io_.ik_list.back());
352 unsigned int count=reader_.getUshort();
353 for(unsigned int i=0; i<count; ++i){
354 io_.bones.push_back(Bone());
355 read(reader_, io_.bones.back());
360 bool parseMaterials()
362 unsigned int count=reader_.getUint();
363 for(unsigned int i=0; i<count; ++i){
364 io_.materials.push_back(Material());
365 read(reader_, io_.materials.back());
372 unsigned int count=reader_.getUint();
373 for(unsigned int i=0; i<count; ++i){
374 io_.indices.push_back(reader_.getUshort());
381 unsigned int count=reader_.getUint();
382 for(unsigned int i=0; i<count; ++i){
383 io_.vertices.push_back(Vertex());
384 read(reader_, io_.vertices.back());
391 if(reader_.getString(3)!="Pmd"){
392 //std::cout << "invalid pmd" << std::endl;
395 reader_.get(io_.version);
396 if(io_.version!=1.0){
397 std::cout << "invalid vesion: " << io_.version <<std::endl;
400 reader_.get(io_.name);
401 reader_.get(io_.comment);
412 for(auto it=toon_textures.begin(); it!=toon_textures.end(); ++it){
414 sprintf(toon, "toon%02d.bmp", i);
419 bool IO::read(binary::IReader &input)
421 Impl impl(*this, input);
426 ////////////////////////////////////////////////////////////
428 ////////////////////////////////////////////////////////////
429 if(!morph_list.empty()){
431 assert(morph_list[0].type==MORPH_BASE);
433 Morph &base=morph_list[0];
434 for(size_t i=0; i<base.vertex_count; ++i){
435 assert(vertices[base.indices[i]].pos==base.pos_list[i]);
438 for(size_t i=1; i<morph_list.size(); ++i){
439 Morph &m=morph_list[i];
440 assert(m.type!=MORPH_BASE);
443 ////////////////////////////////////////////////////////////
445 ////////////////////////////////////////////////////////////
446 for(size_t i=0; i<bones.size(); ++i){
449 if(bone.parent_index!=0xFFFF){
450 bone.parent=&bones[bone.parent_index];
451 bone.parent->children.push_back(&bone);
453 if(bone.tail_index==0){
454 bone.tail=Vector3(0, 0, 0);
457 bone.tail=bones[bone.tail_index].pos;
464 bool IO::read(const char *path)
466 std::vector<char> all;
467 binary::readAll(path, all);
471 binary::MemoryReader reader(&all[0], all.size());
475 bool IO::write(binary::IWriter &w)
478 w.writeValue<float>(version);
483 //std::cout << "vertices" << std::endl;
484 w.writeValue<DWORD>(vertices.size());
485 for(size_t i=0; i<vertices.size(); ++i){
486 Vertex &v=vertices[i];
487 w.writeValue<float>(v.pos.x);
488 w.writeValue<float>(v.pos.y);
489 w.writeValue<float>(v.pos.z);
490 w.writeValue<float>(v.normal.x);
491 w.writeValue<float>(v.normal.y);
492 w.writeValue<float>(v.normal.z);
493 w.writeValue<float>(v.uv.x);
494 w.writeValue<float>(v.uv.y);
495 w.writeValue<WORD>(v.bone0);
496 w.writeValue<WORD>(v.bone1);
497 w.writeValue<BYTE>(v.weight0);
498 w.writeValue<BYTE>(v.edge_flag);
502 //std::cout << "faces" << std::endl;
503 w.writeValue<DWORD>(indices.size());
504 if(indices.size()>0){
505 w.writeArray<WORD>(&indices[0], indices.size());
509 //std::cout << "materials" << std::endl;
510 w.writeValue<DWORD>(materials.size());
511 for(size_t i=0; i<materials.size(); ++i){
512 Material &m=materials[i];
513 w.writeValue<float>(m.diffuse.r);
514 w.writeValue<float>(m.diffuse.g);
515 w.writeValue<float>(m.diffuse.b);
516 w.writeValue<float>(m.diffuse.a);
517 w.writeValue<float>(m.shinness);
518 w.writeValue<float>(m.specular.r);
519 w.writeValue<float>(m.specular.g);
520 w.writeValue<float>(m.specular.b);
521 w.writeValue<float>(m.ambient.r);
522 w.writeValue<float>(m.ambient.g);
523 w.writeValue<float>(m.ambient.b);
524 w.writeValue<BYTE>(m.toon_index);
525 w.writeValue<BYTE>(m.flag);
526 w.writeValue<DWORD>(m.vertex_count);
531 //std::cout << "bones" << std::endl;
532 w.writeValue<WORD>(bones.size());
533 for(size_t i=0; i<bones.size(); ++i){
536 w.writeValue<WORD>(b.parent_index);
537 w.writeValue<WORD>(b.tail_index);
538 w.writeValue<BYTE>(b.type);
539 w.writeValue<WORD>(b.ik_index);
540 w.writeValue<float>(b.pos.x);
541 w.writeValue<float>(b.pos.y);
542 w.writeValue<float>(b.pos.z);
546 //std::cout << "ik" << std::endl;
547 w.writeValue<WORD>(ik_list.size());
548 for(size_t i=0; i<ik_list.size(); ++i){
550 w.writeValue<WORD>(ik.index);
551 w.writeValue<WORD>(ik.target);
552 w.writeValue<BYTE>(ik.length);
553 w.writeValue<WORD>(ik.iterations);
554 w.writeValue<float>(ik.weight);
555 WORD parent_index=bones[ik.target].parent_index;
556 for(size_t j=0; j<ik.length;
557 ++j, parent_index=bones[parent_index].parent_index){
558 w.writeValue<WORD>(parent_index);
563 //std::cout << "morph" << std::endl;
564 w.writeValue<WORD>(morph_list.size());
565 for(size_t i=0; i<morph_list.size(); ++i){
566 Morph &m=morph_list[i];
568 w.writeValue<DWORD>(m.indices.size());
569 w.writeValue<BYTE>(m.type);
570 for(size_t j=0; j<m.indices.size(); ++j){
571 w.writeValue<DWORD>(m.indices[j]);
572 Vector3 &pos=m.pos_list[j];
573 w.writeValue<float>(pos.x);
574 w.writeValue<float>(pos.y);
575 w.writeValue<float>(pos.z);
580 //std::cout << "face list" << std::endl;
581 w.writeValue<BYTE>(face_list.size());
582 if(face_list.size()>0){
583 w.writeArray<WORD>(&face_list[0], face_list.size());
587 //std::cout << "bone name list" << std::endl;
588 w.writeValue<BYTE>(bone_group_list.size());
589 for(size_t i=0; i<bone_group_list.size(); ++i){
591 w.write(bone_group_list[i].name);
595 //std::cout << "bone list" << std::endl;
596 w.writeValue<DWORD>(bone_display_list.size());
597 for(size_t i=0; i<bone_display_list.size(); ++i){
598 w.writeValue<WORD>(bone_display_list[i].first);
599 w.writeValue<BYTE>(bone_display_list[i].second);
602 ////////////////////////////////////////////////////////////
604 ////////////////////////////////////////////////////////////
605 w.writeValue<char>(0x01);
607 ////////////////////////////////////////////////////////////
609 ////////////////////////////////////////////////////////////
610 w.write(english_name);
611 w.write(english_comment);
613 for(size_t i=0; i<bones.size(); ++i){
614 w.write(bones[i].english_name);
618 for(size_t i=1; i<morph_list.size(); ++i){
619 w.write(morph_list[i].english_name);
622 for(size_t i=0; i<bone_group_list.size(); ++i){
623 w.write(bone_group_list[i].english_name);
626 ////////////////////////////////////////////////////////////
628 ////////////////////////////////////////////////////////////
629 for(size_t i=0; i<10; ++i){
630 w.write(toon_textures[i]);
633 ////////////////////////////////////////////////////////////
635 ////////////////////////////////////////////////////////////
636 w.writeValue<DWORD>(rigidbodies.size());
637 for(size_t i=0; i<rigidbodies.size(); ++i){
638 RigidBody &rb=rigidbodies[i];
640 w.writeValue<WORD>(rb.boneIndex);
641 w.writeValue<BYTE>(rb.group);
642 w.writeValue<WORD>(rb.target);
643 w.writeValue<BYTE>(rb.shapeType);
644 w.writeValue<float>(rb.w);
645 w.writeValue<float>(rb.h);
646 w.writeValue<float>(rb.d);
647 w.writeValue<float>(rb.position.x);
648 w.writeValue<float>(rb.position.y);
649 w.writeValue<float>(rb.position.z);
650 w.writeValue<float>(rb.rotation.x);
651 w.writeValue<float>(rb.rotation.y);
652 w.writeValue<float>(rb.rotation.z);
653 w.writeValue<float>(rb.weight);
654 w.writeValue<float>(rb.linearDamping);
655 w.writeValue<float>(rb.angularDamping);
656 w.writeValue<float>(rb.restitution);
657 w.writeValue<float>(rb.friction);
658 w.writeValue<BYTE>(rb.processType);
661 ////////////////////////////////////////////////////////////
663 ////////////////////////////////////////////////////////////
664 w.writeValue<DWORD>(constraints.size());
665 for(size_t i=0; i<constraints.size(); ++i){
666 Constraint &c=constraints[i];
668 w.writeValue<DWORD>(c.rigidA);
669 w.writeValue<DWORD>(c.rigidB);
670 w.writeValue<float>(c.pos.x);
671 w.writeValue<float>(c.pos.y);
672 w.writeValue<float>(c.pos.z);
673 w.writeValue<float>(c.rot.x);
674 w.writeValue<float>(c.rot.y);
675 w.writeValue<float>(c.rot.z);
676 w.writeValue<float>(c.constraintPosMin.x);
677 w.writeValue<float>(c.constraintPosMin.y);
678 w.writeValue<float>(c.constraintPosMin.z);
679 w.writeValue<float>(c.constraintPosMax.x);
680 w.writeValue<float>(c.constraintPosMax.y);
681 w.writeValue<float>(c.constraintPosMax.z);
682 w.writeValue<float>(c.constraintRotMin.x);
683 w.writeValue<float>(c.constraintRotMin.y);
684 w.writeValue<float>(c.constraintRotMin.z);
685 w.writeValue<float>(c.constraintRotMax.x);
686 w.writeValue<float>(c.constraintRotMax.y);
687 w.writeValue<float>(c.constraintRotMax.z);
688 w.writeValue<float>(c.springPos.x);
689 w.writeValue<float>(c.springPos.y);
690 w.writeValue<float>(c.springPos.z);
691 w.writeValue<float>(c.springRot.x);
692 w.writeValue<float>(c.springRot.y);
693 w.writeValue<float>(c.springRot.z);
701 } // namespace meshio