9 bool IO::write(const char *path)
11 binary::FileWriter w(path);
15 bool IO::write(const wchar_t *path)
17 binary::FileWriter w(path);
24 return text::trim(text::to_WideChar(CP_OEMCP,
25 std::string(name, name+20)));
31 return text::trim(text::to_WideChar(CP_OEMCP,
32 std::string(comment, comment+256)));
36 IO::getEnglishName()const
38 return text::trim(text::to_WideChar(CP_OEMCP,
39 std::string(english_name, english_name+20)));
43 IO::getEnglishComment()const
45 return text::trim(text::to_WideChar(CP_OEMCP,
46 std::string(english_comment, english_comment+256)));
49 const Vector2* IO::getUV(int index)const
51 return &vertices[index].uv;
54 void IO::setName(const char *src)
56 strncpy(name, src, 20);
59 void IO::setComment(const char *src)
61 strncpy(comment, src, 256);
64 void IO::setEnglishName(const char *src)
66 strncpy(english_name, src, 20);
69 void IO::setEnglishComment(const char *src)
71 strncpy(english_comment, src, 256);
77 Material::getTexture()const
79 return text::trim(text::to_WideChar(CP_OEMCP,
80 std::string(texture, texture+20)));
83 void Material::setTexture(const char *src)
85 strncpy(texture, src, 20);
92 return text::trim(text::to_WideChar(CP_OEMCP,
93 std::string(name, name+20)));
96 void Bone::setName(const char *src)
98 strncpy(name, src, 20);
101 void Bone::setEnglishName(const char *src)
103 strncpy(english_name, src, 20);
108 Morph::getName()const
110 return text::trim(text::to_WideChar(CP_OEMCP,
111 std::string(name, name+20)));
115 Morph::append(int index, float x, float y, float z)
117 indices.push_back(index);
118 pos_list.push_back(Vector3(x, y, z));
121 void Morph::setName(const char *src)
123 strncpy(name, src, 20);
126 void Morph::setEnglishName(const char *src)
128 strncpy(english_name, src, 20);
133 BoneGroup::getName()const
135 return text::trim(text::to_WideChar(CP_OEMCP,
136 std::string(name, name+50)));
139 void BoneGroup::setName(const char *src)
141 strncpy(name, src, 50);
144 void BoneGroup::setEnglishName(const char *src)
146 strncpy(english_name, src, 50);
151 RigidBody::getName()const
153 return text::trim(text::to_WideChar(CP_OEMCP,
154 std::string(name, name+20)));
157 void RigidBody::setName(const char *src)
159 strncpy(name, src, 20);
164 Constraint::getName()const
166 return text::trim(text::to_WideChar(CP_OEMCP,
167 std::string(name, name+20)));
170 void Constraint::setName(const char *src)
172 strncpy(name, src, 20);
177 ToonTexture::getName()const
179 return text::trim(text::to_WideChar(CP_OEMCP,
180 std::string(name, name+100)));
183 void ToonTexture::setName(const char *src)
185 strncpy(name, src, 100);
190 template<class READER>
192 read(READER &reader, Vertex &v)
194 unsigned int pos=reader.getPos();
196 reader.get(v.normal);
200 reader.get(v.weight0);
201 reader.get(v.edge_flag);
202 assert(reader.getPos()-pos==38);
207 template<class READER>
209 read(READER &reader, Material &m)
211 unsigned int pos=reader.getPos();
212 reader.get(m.diffuse);
213 reader.get(m.shinness);
214 reader.get(m.specular);
215 reader.get(m.ambient);
216 reader.get(m.toon_index);
218 reader.get(m.vertex_count);
219 text::copyStringAndFillZero(m.texture, reader.getString(20));
220 assert(reader.getPos()-pos==70);
224 template<class READER>
226 read(READER &reader, Bone &b)
228 unsigned int pos=reader.getPos();
229 text::copyStringAndFillZero(b.name, reader.getString(20));
230 reader.get(b.parent_index);
231 reader.get(b.tail_index);
232 b.type=static_cast<BONE_TYPE>(reader.getUchar());
233 reader.get(b.ik_index);
235 assert(reader.getPos()-pos==39);
238 // 11+2xIK_COUNT bytes
239 template<class READER>
241 read(READER &reader, IK &ik)
244 reader.get(ik.index);
245 reader.get(ik.target);
246 reader.get(ik.length);
247 reader.get(ik.iterations);
248 reader.get(ik.weight);
250 for(unsigned short j=0; j<ik.length; ++j){
251 ik.children.push_back(reader.getUshort());
255 // 25+12xMORPH_COUNT bytes
256 template<class READER>
258 read(READER &reader, Morph &m)
261 text::copyStringAndFillZero(m.name, reader.getString(20));
262 reader.get(m.vertex_count);
263 m.type=static_cast<MORPH_TYPE>(reader.getUchar());
264 // 12 x vertex_count bytes
265 for(unsigned short i=0; i<m.vertex_count; ++i){
266 m.indices.push_back(reader.getUint());
267 m.pos_list.push_back(Vector3());
268 reader.get(m.pos_list.back());
273 template<class READER>
275 read(READER &reader, RigidBody &r)
277 unsigned int pos=reader.getPos();
278 text::copyStringAndFillZero(r.name, reader.getString(20));
279 reader.get(r.boneIndex);
281 reader.get(r.target);
282 r.shapeType=static_cast<SHAPE_TYPE>(reader.getUchar());
286 reader.get(r.position);
287 reader.get(r.rotation);
288 reader.get(r.weight);
289 reader.get(r.linearDamping);
290 reader.get(r.angularDamping);
291 reader.get(r.restitution);
292 reader.get<float>(r.friction);
293 r.processType=static_cast<PROCESS_TYPE>(reader.getUchar());
294 assert(reader.getPos()-pos==83);
298 template<class READER>
300 read(READER &reader, Constraint &c)
302 unsigned int base_pos=reader.getPos();
303 text::copyStringAndFillZero(c.name, reader.getString(20));
304 reader.get(c.rigidA);
305 reader.get(c.rigidB);
308 reader.get(c.constraintPosMin);
309 reader.get(c.constraintPosMax);
310 reader.get(c.constraintRotMin);
311 reader.get(c.constraintRotMax);
312 reader.get(c.springPos);
313 reader.get(c.springRot);
314 assert(reader.getPos()-base_pos==124);
320 binary::IReader &reader_;
323 Impl(IO &io, binary::IReader &reader)
324 : io_(io), reader_(reader)
332 if(!parseVertices()){
338 if(!parseMaterials()){
350 if(!parseFaceList()){
353 if(!parseBoneGroupList()){
356 if(!parseBoneList()){
363 ////////////////////////////////////////////////////////////
365 ////////////////////////////////////////////////////////////
367 ////////////////////////////////////////////////////////////
368 if(reader_.getChar()){
369 if(!parseEnglishName()){
372 if(!parseEnglishBone()){
375 if(!parseEnglishMorph()){
378 if(!parseEnglishBoneList()){
387 ////////////////////////////////////////////////////////////
388 if(!parseToonTextures()){
396 ////////////////////////////////////////////////////////////
400 if(!parseConstraint()){
405 assert(reader_.isEnd());
411 bool parseConstraint()
413 unsigned int count=reader_.getUint();
414 for(unsigned int i=0; i<count; ++i){
415 io_.constraints.push_back(Constraint());
416 read(reader_, io_.constraints.back());
423 unsigned int count=reader_.getUint();
424 for(unsigned int i=0; i<count; ++i){
425 io_.rigidbodies.push_back(RigidBody());
426 read(reader_, io_.rigidbodies.back());
431 bool parseToonTextures()
433 for(size_t i=0; i<10; ++i){
434 text::copyStringAndFillZero(
435 io_.toon_textures[i].name,
436 reader_.getString(100));
441 bool parseEnglishBoneList()
443 for(size_t i=0; i<io_.bone_group_list.size(); ++i){
444 text::copyStringAndFillZero(
445 io_.bone_group_list[i].english_name,
446 reader_.getString(50));
451 bool parseEnglishMorph()
453 int count=io_.morph_list.size()-1;
454 for(int i=0; i<count; ++i){
455 text::copyStringAndFillZero(
456 io_.morph_list[i].english_name, reader_.getString(20));
461 bool parseEnglishBone()
463 for(size_t i=0; i<io_.bones.size(); ++i){
464 text::copyStringAndFillZero(
465 io_.bones[i].english_name, reader_.getString(20));
470 bool parseEnglishName()
472 text::copyStringAndFillZero(io_.english_name,
473 reader_.getString(20));
474 text::copyStringAndFillZero(io_.english_comment,
475 reader_.getString(256));
481 unsigned int count=reader_.getUint();
482 for(unsigned int i=0; i<count; ++i){
483 unsigned short bone=reader_.getUshort();
484 unsigned char disp=reader_.getUchar();
485 io_.bone_display_list.push_back(std::make_pair(bone, disp));
490 bool parseBoneGroupList()
492 unsigned int count=reader_.getUchar();
493 for(unsigned int i=0; i<count; ++i){
494 io_.bone_group_list.push_back(BoneGroup());
495 text::copyStringAndFillZero(
496 io_.bone_group_list.back().name,
497 reader_.getString(50));
504 unsigned int count=reader_.getUchar();
505 for(unsigned int i=0; i<count; ++i){
506 io_.face_list.push_back(reader_.getUshort());
513 unsigned int count=reader_.getUshort();
514 for(unsigned int i=0; i<count; ++i){
515 io_.morph_list.push_back(Morph());
516 read(reader_, io_.morph_list.back());
523 unsigned int count=reader_.getUshort();
524 for(unsigned int i=0; i<count; ++i){
525 io_.ik_list.push_back(IK());
526 read(reader_, io_.ik_list.back());
533 unsigned int count=reader_.getUshort();
534 for(unsigned int i=0; i<count; ++i){
535 io_.bones.push_back(Bone());
536 read(reader_, io_.bones.back());
541 bool parseMaterials()
543 unsigned int count=reader_.getUint();
544 for(unsigned int i=0; i<count; ++i){
545 io_.materials.push_back(new Material());
546 read(reader_, *io_.materials.back());
553 unsigned int count=reader_.getUint();
554 for(unsigned int i=0; i<count; ++i){
555 io_.indices.push_back(reader_.getUshort());
562 unsigned int count=reader_.getUint();
563 for(unsigned int i=0; i<count; ++i){
564 io_.vertices.push_back(Vertex());
565 read(reader_, io_.vertices.back());
572 if(reader_.getString(3)!="Pmd"){
573 //std::cout << "invalid pmd" << std::endl;
576 reader_.get(io_.version);
577 if(io_.version!=1.0){
578 std::cout << "invalid vesion: " << io_.version <<std::endl;
581 text::copyStringAndFillZero(io_.name, reader_.getString(20));
582 text::copyStringAndFillZero(io_.comment, reader_.getString(256));
589 ///////////////////////////////////////////////////////////////////////////////
591 ///////////////////////////////////////////////////////////////////////////////
598 for(size_t i=0; i<materials.size(); ++i){
604 bool IO::read(binary::IReader &input)
606 Impl impl(*this, input);
611 ////////////////////////////////////////////////////////////
613 ////////////////////////////////////////////////////////////
614 if(!morph_list.empty()){
616 assert(morph_list[0].type==MORPH_BASE);
618 Morph &base=morph_list[0];
619 for(size_t i=0; i<base.vertex_count; ++i){
620 assert(vertices[base.indices[i]].pos==base.pos_list[i]);
623 for(size_t i=1; i<morph_list.size(); ++i){
624 Morph &m=morph_list[i];
625 assert(m.type!=MORPH_BASE);
628 ////////////////////////////////////////////////////////////
630 ////////////////////////////////////////////////////////////
631 for(size_t i=0; i<bones.size(); ++i){
634 if(bone.parent_index!=0xFFFF){
635 bone.parent=&bones[bone.parent_index];
636 bone.parent->children.push_back(&bone);
638 if(bone.tail_index!=0){
639 bone.tail=bones[bone.tail_index].pos;
647 bool IO::read(const char *path)
649 std::vector<char> all;
650 binary::readAll(path, all);
654 binary::MemoryReader reader(&all[0], all.size());
659 bool IO::read(const wchar_t *path)
661 std::vector<char> all;
662 binary::readAll(path, all);
663 std::cerr << all.size() << "bytes" << std::endl;
667 binary::MemoryReader reader(&all[0], all.size());
671 bool IO::write(binary::IWriter &w)
674 w.writeValue<float>(version);
676 w.write(comment, 256);
679 //std::cout << "vertices" << std::endl;
680 w.writeValue<DWORD>(vertices.size());
681 for(size_t i=0; i<vertices.size(); ++i){
682 Vertex &v=vertices[i];
683 w.writeValue<float>(v.pos.x);
684 w.writeValue<float>(v.pos.y);
685 w.writeValue<float>(v.pos.z);
686 w.writeValue<float>(v.normal.x);
687 w.writeValue<float>(v.normal.y);
688 w.writeValue<float>(v.normal.z);
689 w.writeValue<float>(v.uv.x);
690 w.writeValue<float>(v.uv.y);
691 w.writeValue<WORD>(v.bone0);
692 w.writeValue<WORD>(v.bone1);
693 w.writeValue<BYTE>(v.weight0);
694 w.writeValue<BYTE>(v.edge_flag);
698 //std::cout << "faces" << std::endl;
699 w.writeValue<DWORD>(indices.size());
700 if(indices.size()>0){
701 w.writeArray<WORD>(&indices[0], indices.size());
705 //std::cout << "materials" << std::endl;
706 w.writeValue<DWORD>(materials.size());
707 for(size_t i=0; i<materials.size(); ++i){
708 Material &m=*materials[i];
709 w.writeValue<float>(m.diffuse.r);
710 w.writeValue<float>(m.diffuse.g);
711 w.writeValue<float>(m.diffuse.b);
712 w.writeValue<float>(m.diffuse.a);
713 w.writeValue<float>(m.shinness);
714 w.writeValue<float>(m.specular.r);
715 w.writeValue<float>(m.specular.g);
716 w.writeValue<float>(m.specular.b);
717 w.writeValue<float>(m.ambient.r);
718 w.writeValue<float>(m.ambient.g);
719 w.writeValue<float>(m.ambient.b);
720 w.writeValue<BYTE>(m.toon_index);
721 w.writeValue<BYTE>(m.flag);
722 w.writeValue<DWORD>(m.vertex_count);
723 w.writeArray<char>(m.texture, 20);
727 //std::cout << "bones" << std::endl;
728 w.writeValue<WORD>(bones.size());
729 for(size_t i=0; i<bones.size(); ++i){
731 w.writeArray<char>(b.name, 20);
732 w.writeValue<WORD>(b.parent_index);
733 w.writeValue<WORD>(b.tail_index);
734 w.writeValue<BYTE>(b.type);
735 w.writeValue<WORD>(b.ik_index);
736 w.writeValue<float>(b.pos.x);
737 w.writeValue<float>(b.pos.y);
738 w.writeValue<float>(b.pos.z);
742 //std::cout << "ik" << std::endl;
743 w.writeValue<WORD>(ik_list.size());
744 for(size_t i=0; i<ik_list.size(); ++i){
746 w.writeValue<WORD>(ik.index);
747 w.writeValue<WORD>(ik.target);
748 w.writeValue<BYTE>(ik.length);
749 w.writeValue<WORD>(ik.iterations);
750 w.writeValue<float>(ik.weight);
751 WORD parent_index=bones[ik.target].parent_index;
752 for(size_t j=0; j<ik.length;
753 ++j, parent_index=bones[parent_index].parent_index){
754 w.writeValue<WORD>(parent_index);
759 //std::cout << "morph" << std::endl;
760 w.writeValue<WORD>(morph_list.size());
761 for(size_t i=0; i<morph_list.size(); ++i){
762 Morph &m=morph_list[i];
763 w.writeArray<char>(m.name, 20);
764 w.writeValue<DWORD>(m.indices.size());
765 w.writeValue<BYTE>(m.type);
766 for(size_t j=0; j<m.indices.size(); ++j){
767 w.writeValue<DWORD>(m.indices[j]);
768 Vector3 &pos=m.pos_list[j];
769 w.writeValue<float>(pos.x);
770 w.writeValue<float>(pos.y);
771 w.writeValue<float>(pos.z);
776 //std::cout << "face list" << std::endl;
777 w.writeValue<BYTE>(face_list.size());
778 if(face_list.size()>0){
779 w.writeArray<WORD>(&face_list[0], face_list.size());
783 //std::cout << "bone name list" << std::endl;
784 w.writeValue<BYTE>(bone_group_list.size());
785 for(size_t i=0; i<bone_group_list.size(); ++i){
787 w.writeArray<char>(bone_group_list[i].name, 50);
791 //std::cout << "bone list" << std::endl;
792 w.writeValue<DWORD>(bone_display_list.size());
793 for(size_t i=0; i<bone_display_list.size(); ++i){
794 w.writeValue<WORD>(bone_display_list[i].first);
795 w.writeValue<BYTE>(bone_display_list[i].second);
798 ////////////////////////////////////////////////////////////
800 ////////////////////////////////////////////////////////////
801 w.writeValue<char>(0x01);
803 ////////////////////////////////////////////////////////////
805 ////////////////////////////////////////////////////////////
806 w.writeArray<char>(english_name, 20);
807 w.writeArray<char>(english_comment, 256);
809 for(size_t i=0; i<bones.size(); ++i){
810 w.writeArray<char>(bones[i].english_name, 20);
814 for(size_t i=1; i<morph_list.size(); ++i){
815 w.writeArray<char>(morph_list[i].english_name, 20);
818 for(size_t i=0; i<bone_group_list.size(); ++i){
819 w.writeArray<char>(bone_group_list[i].english_name, 50);
822 ////////////////////////////////////////////////////////////
824 ////////////////////////////////////////////////////////////
825 for(size_t i=0; i<10; ++i){
826 w.writeArray<char>(toon_textures[i].name, 100);
829 ////////////////////////////////////////////////////////////
831 ////////////////////////////////////////////////////////////
832 w.writeValue<DWORD>(rigidbodies.size());
833 for(size_t i=0; i<rigidbodies.size(); ++i){
834 RigidBody &rb=rigidbodies[i];
835 w.writeArray<char>(rb.name, 20);
836 w.writeValue<WORD>(rb.boneIndex);
837 w.writeValue<BYTE>(rb.group);
838 w.writeValue<WORD>(rb.target);
839 w.writeValue<BYTE>(rb.shapeType);
840 w.writeValue<float>(rb.w);
841 w.writeValue<float>(rb.h);
842 w.writeValue<float>(rb.d);
843 w.writeValue<float>(rb.position.x);
844 w.writeValue<float>(rb.position.y);
845 w.writeValue<float>(rb.position.z);
846 w.writeValue<float>(rb.rotation.x);
847 w.writeValue<float>(rb.rotation.y);
848 w.writeValue<float>(rb.rotation.z);
849 w.writeValue<float>(rb.weight);
850 w.writeValue<float>(rb.linearDamping);
851 w.writeValue<float>(rb.angularDamping);
852 w.writeValue<float>(rb.restitution);
853 w.writeValue<float>(rb.friction);
854 w.writeValue<BYTE>(rb.processType);
857 ////////////////////////////////////////////////////////////
859 ////////////////////////////////////////////////////////////
860 w.writeValue<DWORD>(constraints.size());
861 for(size_t i=0; i<constraints.size(); ++i){
862 Constraint &c=constraints[i];
863 w.writeArray<char>(c.name, 20);
864 w.writeValue<DWORD>(c.rigidA);
865 w.writeValue<DWORD>(c.rigidB);
866 w.writeValue<float>(c.pos.x);
867 w.writeValue<float>(c.pos.y);
868 w.writeValue<float>(c.pos.z);
869 w.writeValue<float>(c.rot.x);
870 w.writeValue<float>(c.rot.y);
871 w.writeValue<float>(c.rot.z);
872 w.writeValue<float>(c.constraintPosMin.x);
873 w.writeValue<float>(c.constraintPosMin.y);
874 w.writeValue<float>(c.constraintPosMin.z);
875 w.writeValue<float>(c.constraintPosMax.x);
876 w.writeValue<float>(c.constraintPosMax.y);
877 w.writeValue<float>(c.constraintPosMax.z);
878 w.writeValue<float>(c.constraintRotMin.x);
879 w.writeValue<float>(c.constraintRotMin.y);
880 w.writeValue<float>(c.constraintRotMin.z);
881 w.writeValue<float>(c.constraintRotMax.x);
882 w.writeValue<float>(c.constraintRotMax.y);
883 w.writeValue<float>(c.constraintRotMax.z);
884 w.writeValue<float>(c.springPos.x);
885 w.writeValue<float>(c.springPos.y);
886 w.writeValue<float>(c.springPos.z);
887 w.writeValue<float>(c.springRot.x);
888 w.writeValue<float>(c.springRot.y);
889 w.writeValue<float>(c.springRot.z);