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=Vector3(0, 0, 0);
642 bone.tail=bones[bone.tail_index].pos;
650 bool IO::read(const char *path)
652 std::vector<char> all;
653 binary::readAll(path, all);
657 binary::MemoryReader reader(&all[0], all.size());
662 bool IO::read(const wchar_t *path)
664 std::vector<char> all;
665 binary::readAll(path, all);
666 std::cerr << all.size() << "bytes" << std::endl;
670 binary::MemoryReader reader(&all[0], all.size());
674 bool IO::write(binary::IWriter &w)
677 w.writeValue<float>(version);
679 w.write(comment, 256);
682 //std::cout << "vertices" << std::endl;
683 w.writeValue<DWORD>(vertices.size());
684 for(size_t i=0; i<vertices.size(); ++i){
685 Vertex &v=vertices[i];
686 w.writeValue<float>(v.pos.x);
687 w.writeValue<float>(v.pos.y);
688 w.writeValue<float>(v.pos.z);
689 w.writeValue<float>(v.normal.x);
690 w.writeValue<float>(v.normal.y);
691 w.writeValue<float>(v.normal.z);
692 w.writeValue<float>(v.uv.x);
693 w.writeValue<float>(v.uv.y);
694 w.writeValue<WORD>(v.bone0);
695 w.writeValue<WORD>(v.bone1);
696 w.writeValue<BYTE>(v.weight0);
697 w.writeValue<BYTE>(v.edge_flag);
701 //std::cout << "faces" << std::endl;
702 w.writeValue<DWORD>(indices.size());
703 if(indices.size()>0){
704 w.writeArray<WORD>(&indices[0], indices.size());
708 //std::cout << "materials" << std::endl;
709 w.writeValue<DWORD>(materials.size());
710 for(size_t i=0; i<materials.size(); ++i){
711 Material &m=*materials[i];
712 w.writeValue<float>(m.diffuse.r);
713 w.writeValue<float>(m.diffuse.g);
714 w.writeValue<float>(m.diffuse.b);
715 w.writeValue<float>(m.diffuse.a);
716 w.writeValue<float>(m.shinness);
717 w.writeValue<float>(m.specular.r);
718 w.writeValue<float>(m.specular.g);
719 w.writeValue<float>(m.specular.b);
720 w.writeValue<float>(m.ambient.r);
721 w.writeValue<float>(m.ambient.g);
722 w.writeValue<float>(m.ambient.b);
723 w.writeValue<BYTE>(m.toon_index);
724 w.writeValue<BYTE>(m.flag);
725 w.writeValue<DWORD>(m.vertex_count);
726 w.writeArray<char>(m.texture, 20);
730 //std::cout << "bones" << std::endl;
731 w.writeValue<WORD>(bones.size());
732 for(size_t i=0; i<bones.size(); ++i){
734 w.writeArray<char>(b.name, 20);
735 w.writeValue<WORD>(b.parent_index);
736 w.writeValue<WORD>(b.tail_index);
737 w.writeValue<BYTE>(b.type);
738 w.writeValue<WORD>(b.ik_index);
739 w.writeValue<float>(b.pos.x);
740 w.writeValue<float>(b.pos.y);
741 w.writeValue<float>(b.pos.z);
745 //std::cout << "ik" << std::endl;
746 w.writeValue<WORD>(ik_list.size());
747 for(size_t i=0; i<ik_list.size(); ++i){
749 w.writeValue<WORD>(ik.index);
750 w.writeValue<WORD>(ik.target);
751 w.writeValue<BYTE>(ik.length);
752 w.writeValue<WORD>(ik.iterations);
753 w.writeValue<float>(ik.weight);
754 WORD parent_index=bones[ik.target].parent_index;
755 for(size_t j=0; j<ik.length;
756 ++j, parent_index=bones[parent_index].parent_index){
757 w.writeValue<WORD>(parent_index);
762 //std::cout << "morph" << std::endl;
763 w.writeValue<WORD>(morph_list.size());
764 for(size_t i=0; i<morph_list.size(); ++i){
765 Morph &m=morph_list[i];
766 w.writeArray<char>(m.name, 20);
767 w.writeValue<DWORD>(m.indices.size());
768 w.writeValue<BYTE>(m.type);
769 for(size_t j=0; j<m.indices.size(); ++j){
770 w.writeValue<DWORD>(m.indices[j]);
771 Vector3 &pos=m.pos_list[j];
772 w.writeValue<float>(pos.x);
773 w.writeValue<float>(pos.y);
774 w.writeValue<float>(pos.z);
779 //std::cout << "face list" << std::endl;
780 w.writeValue<BYTE>(face_list.size());
781 if(face_list.size()>0){
782 w.writeArray<WORD>(&face_list[0], face_list.size());
786 //std::cout << "bone name list" << std::endl;
787 w.writeValue<BYTE>(bone_group_list.size());
788 for(size_t i=0; i<bone_group_list.size(); ++i){
790 w.writeArray<char>(bone_group_list[i].name, 50);
794 //std::cout << "bone list" << std::endl;
795 w.writeValue<DWORD>(bone_display_list.size());
796 for(size_t i=0; i<bone_display_list.size(); ++i){
797 w.writeValue<WORD>(bone_display_list[i].first);
798 w.writeValue<BYTE>(bone_display_list[i].second);
801 ////////////////////////////////////////////////////////////
803 ////////////////////////////////////////////////////////////
804 w.writeValue<char>(0x01);
806 ////////////////////////////////////////////////////////////
808 ////////////////////////////////////////////////////////////
809 w.writeArray<char>(english_name, 20);
810 w.writeArray<char>(english_comment, 256);
812 for(size_t i=0; i<bones.size(); ++i){
813 w.writeArray<char>(bones[i].english_name, 20);
817 for(size_t i=1; i<morph_list.size(); ++i){
818 w.writeArray<char>(morph_list[i].english_name, 20);
821 for(size_t i=0; i<bone_group_list.size(); ++i){
822 w.writeArray<char>(bone_group_list[i].english_name, 50);
825 ////////////////////////////////////////////////////////////
827 ////////////////////////////////////////////////////////////
828 for(size_t i=0; i<10; ++i){
829 w.writeArray<char>(toon_textures[i].name, 100);
832 ////////////////////////////////////////////////////////////
834 ////////////////////////////////////////////////////////////
835 w.writeValue<DWORD>(rigidbodies.size());
836 for(size_t i=0; i<rigidbodies.size(); ++i){
837 RigidBody &rb=rigidbodies[i];
838 w.writeArray<char>(rb.name, 20);
839 w.writeValue<WORD>(rb.boneIndex);
840 w.writeValue<BYTE>(rb.group);
841 w.writeValue<WORD>(rb.target);
842 w.writeValue<BYTE>(rb.shapeType);
843 w.writeValue<float>(rb.w);
844 w.writeValue<float>(rb.h);
845 w.writeValue<float>(rb.d);
846 w.writeValue<float>(rb.position.x);
847 w.writeValue<float>(rb.position.y);
848 w.writeValue<float>(rb.position.z);
849 w.writeValue<float>(rb.rotation.x);
850 w.writeValue<float>(rb.rotation.y);
851 w.writeValue<float>(rb.rotation.z);
852 w.writeValue<float>(rb.weight);
853 w.writeValue<float>(rb.linearDamping);
854 w.writeValue<float>(rb.angularDamping);
855 w.writeValue<float>(rb.restitution);
856 w.writeValue<float>(rb.friction);
857 w.writeValue<BYTE>(rb.processType);
860 ////////////////////////////////////////////////////////////
862 ////////////////////////////////////////////////////////////
863 w.writeValue<DWORD>(constraints.size());
864 for(size_t i=0; i<constraints.size(); ++i){
865 Constraint &c=constraints[i];
866 w.writeArray<char>(c.name, 20);
867 w.writeValue<DWORD>(c.rigidA);
868 w.writeValue<DWORD>(c.rigidB);
869 w.writeValue<float>(c.pos.x);
870 w.writeValue<float>(c.pos.y);
871 w.writeValue<float>(c.pos.z);
872 w.writeValue<float>(c.rot.x);
873 w.writeValue<float>(c.rot.y);
874 w.writeValue<float>(c.rot.z);
875 w.writeValue<float>(c.constraintPosMin.x);
876 w.writeValue<float>(c.constraintPosMin.y);
877 w.writeValue<float>(c.constraintPosMin.z);
878 w.writeValue<float>(c.constraintPosMax.x);
879 w.writeValue<float>(c.constraintPosMax.y);
880 w.writeValue<float>(c.constraintPosMax.z);
881 w.writeValue<float>(c.constraintRotMin.x);
882 w.writeValue<float>(c.constraintRotMin.y);
883 w.writeValue<float>(c.constraintRotMin.z);
884 w.writeValue<float>(c.constraintRotMax.x);
885 w.writeValue<float>(c.constraintRotMax.y);
886 w.writeValue<float>(c.constraintRotMax.z);
887 w.writeValue<float>(c.springPos.x);
888 w.writeValue<float>(c.springPos.y);
889 w.writeValue<float>(c.springPos.z);
890 w.writeValue<float>(c.springRot.x);
891 w.writeValue<float>(c.springRot.y);
892 w.writeValue<float>(c.springRot.z);