9 Material::getTexture()const
11 return text::trim(text::to_WideChar(CP_OEMCP,
12 std::string(texture, texture+20)));
18 return text::trim(text::to_WideChar(CP_OEMCP,
19 std::string(name, name+20)));
25 return text::trim(text::to_WideChar(CP_OEMCP,
26 std::string(name, name+20)));
30 Morph::append(int index, float x, float y, float z)
32 indices.push_back(index);
33 pos_list.push_back(Vector3(x, y, z));
39 return text::trim(text::to_WideChar(CP_OEMCP,
40 std::string(name, name+20)));
44 IO::getEnglishName()const
46 return text::trim(text::to_WideChar(CP_OEMCP,
47 std::string(english_name, english_name+20)));
51 RigidBody::getName()const
53 return text::trim(text::to_WideChar(CP_OEMCP,
54 std::string(name, name+20)));
58 Constraint::getName()const
60 return text::trim(text::to_WideChar(CP_OEMCP,
61 std::string(name, name+20)));
65 template<class READER>
67 read(READER &reader, Vertex &v)
69 unsigned int pos=reader.getPos();
75 reader.get(v.weight0);
76 reader.get(v.edge_flag);
77 assert(reader.getPos()-pos==38);
82 template<class READER>
84 read(READER &reader, Material &m)
86 unsigned int pos=reader.getPos();
87 reader.get(m.diffuse);
88 reader.get(m.shinness);
89 reader.get(m.specular);
90 reader.get(m.ambient);
91 reader.get(m.toon_index);
93 reader.get(m.vertex_count);
94 text::copyStringAndFillZero(m.texture, reader.getString(20));
95 assert(reader.getPos()-pos==70);
99 template<class READER>
101 read(READER &reader, Bone &b)
103 unsigned int pos=reader.getPos();
104 text::copyStringAndFillZero(b.name, reader.getString(20));
105 reader.get(b.parent_index);
106 reader.get(b.tail_index);
107 b.type=static_cast<BONE_TYPE>(reader.getUchar());
108 reader.get(b.ik_index);
110 assert(reader.getPos()-pos==39);
113 // 11+2xIK_COUNT bytes
114 template<class READER>
116 read(READER &reader, IK &ik)
119 reader.get(ik.index);
120 reader.get(ik.target);
121 reader.get(ik.length);
122 reader.get(ik.iterations);
123 reader.get(ik.weight);
125 for(unsigned short j=0; j<ik.length; ++j){
126 ik.children.push_back(reader.getUshort());
130 // 25+12xMORPH_COUNT bytes
131 template<class READER>
133 read(READER &reader, Morph &m)
136 text::copyStringAndFillZero(m.name, reader.getString(20));
137 reader.get(m.vertex_count);
138 m.type=static_cast<MORPH_TYPE>(reader.getUchar());
139 // 12 x vertex_count bytes
140 for(unsigned short i=0; i<m.vertex_count; ++i){
141 m.indices.push_back(reader.getUint());
142 m.pos_list.push_back(Vector3());
143 reader.get(m.pos_list.back());
148 template<class READER>
150 read(READER &reader, RigidBody &r)
152 unsigned int pos=reader.getPos();
153 text::copyStringAndFillZero(r.name, reader.getString(20));
154 reader.get(r.boneIndex);
156 reader.get(r.target);
157 r.shapeType=static_cast<SHAPE_TYPE>(reader.getUchar());
161 reader.get(r.position);
162 reader.get(r.rotation);
163 reader.get(r.weight);
164 reader.get(r.linearDamping);
165 reader.get(r.angularDamping);
166 reader.get(r.restitution);
167 reader.get<float>(r.friction);
168 r.processType=static_cast<PROCESS_TYPE>(reader.getUchar());
169 assert(reader.getPos()-pos==83);
173 template<class READER>
175 read(READER &reader, Constraint &c)
177 unsigned int base_pos=reader.getPos();
178 text::copyStringAndFillZero(c.name, reader.getString(20));
179 reader.get(c.rigidA);
180 reader.get(c.rigidB);
183 reader.get(c.constraintPosMin);
184 reader.get(c.constraintPosMax);
185 reader.get(c.constraintRotMin);
186 reader.get(c.constraintRotMax);
187 reader.get(c.springPos);
188 reader.get(c.springRot);
189 assert(reader.getPos()-base_pos==124);
195 binary::IReader &reader_;
198 Impl(IO &io, binary::IReader &reader)
199 : io_(io), reader_(reader)
207 if(!parseVertices()){
213 if(!parseMaterials()){
225 if(!parseFaceList()){
228 if(!parseBoneNameList()){
231 if(!parseBoneList()){
238 ////////////////////////////////////////////////////////////
240 ////////////////////////////////////////////////////////////
242 ////////////////////////////////////////////////////////////
243 if(reader_.getChar()){
244 if(!parseEnglishName()){
247 if(!parseEnglishBone()){
250 if(!parseEnglishMorph()){
253 if(!parseEnglishBoneList()){
262 ////////////////////////////////////////////////////////////
263 if(!parseToonTextures()){
271 ////////////////////////////////////////////////////////////
275 if(!parseConstraint()){
280 assert(reader_.isEnd());
286 bool parseConstraint()
288 unsigned int count=reader_.getUint();
289 for(unsigned int i=0; i<count; ++i){
290 io_.constraints.push_back(Constraint());
291 read(reader_, io_.constraints.back());
298 unsigned int count=reader_.getUint();
299 for(unsigned int i=0; i<count; ++i){
300 io_.rigidbodies.push_back(RigidBody());
301 read(reader_, io_.rigidbodies.back());
306 bool parseToonTextures()
308 for(size_t i=0; i<10; ++i){
309 reader_.getString(100);
314 bool parseEnglishBoneList()
316 for(size_t i=0; i<io_.bone_display_name_list.size(); ++i){
317 text::copyStringAndFillZero(
318 io_.bone_display_name_list[i].english_name,
319 reader_.getString(50));
324 bool parseEnglishMorph()
326 int count=io_.morph_list.size()-1;
327 for(int i=0; i<count; ++i){
328 text::copyStringAndFillZero(
329 io_.morph_list[i].english_name, reader_.getString(20));
334 bool parseEnglishBone()
336 for(size_t i=0; i<io_.bones.size(); ++i){
337 text::copyStringAndFillZero(
338 io_.bones[i].english_name, reader_.getString(20));
343 bool parseEnglishName()
345 text::copyStringAndFillZero(io_.english_name,
346 reader_.getString(20));
347 text::copyStringAndFillZero(io_.english_comment,
348 reader_.getString(256));
354 unsigned int count=reader_.getUint();
355 for(unsigned int i=0; i<count; ++i){
356 unsigned short bone=reader_.getUshort();
357 unsigned char disp=reader_.getUchar();
358 io_.bone_display_list.push_back(std::make_pair(bone, disp));
363 bool parseBoneNameList()
365 unsigned int count=reader_.getUchar();
366 for(unsigned int i=0; i<count; ++i){
367 io_.bone_display_name_list.push_back(BoneDisplayName());
368 text::copyStringAndFillZero(
369 io_.bone_display_name_list.back().name,
370 reader_.getString(50));
377 unsigned int count=reader_.getUchar();
378 for(unsigned int i=0; i<count; ++i){
379 io_.face_list.push_back(reader_.getUshort());
386 unsigned int count=reader_.getUshort();
387 for(unsigned int i=0; i<count; ++i){
388 io_.morph_list.push_back(Morph());
389 read(reader_, io_.morph_list.back());
396 unsigned int count=reader_.getUshort();
397 for(unsigned int i=0; i<count; ++i){
398 io_.ik_list.push_back(IK());
399 read(reader_, io_.ik_list.back());
406 unsigned int count=reader_.getUshort();
407 for(unsigned int i=0; i<count; ++i){
408 io_.bones.push_back(Bone());
409 read(reader_, io_.bones.back());
414 bool parseMaterials()
416 unsigned int count=reader_.getUint();
417 for(unsigned int i=0; i<count; ++i){
418 io_.materials.push_back(new Material());
419 read(reader_, *io_.materials.back());
426 unsigned int count=reader_.getUint();
427 for(unsigned int i=0; i<count; ++i){
428 io_.indices.push_back(reader_.getUshort());
435 unsigned int count=reader_.getUint();
436 for(unsigned int i=0; i<count; ++i){
437 io_.vertices.push_back(Vertex());
438 read(reader_, io_.vertices.back());
445 if(reader_.getString(3)!="Pmd"){
446 //std::cout << "invalid pmd" << std::endl;
449 reader_.get(io_.version);
450 if(io_.version!=1.0){
451 std::cout << "invalid vesion: " << io_.version <<std::endl;
454 text::copyStringAndFillZero(io_.name, reader_.getString(20));
455 text::copyStringAndFillZero(io_.comment, reader_.getString(256));
462 ///////////////////////////////////////////////////////////////////////////////
464 ///////////////////////////////////////////////////////////////////////////////
471 for(size_t i=0; i<materials.size(); ++i){
477 bool IO::read(binary::IReader &input)
479 Impl impl(*this, input);
484 ////////////////////////////////////////////////////////////
486 ////////////////////////////////////////////////////////////
487 if(!morph_list.empty()){
489 assert(morph_list[0].type==MORPH_BASE);
491 Morph &base=morph_list[0];
492 for(size_t i=0; i<base.vertex_count; ++i){
493 assert(vertices[base.indices[i]].pos==base.pos_list[i]);
496 for(size_t i=1; i<morph_list.size(); ++i){
497 Morph &m=morph_list[i];
498 assert(m.type!=MORPH_BASE);
501 ////////////////////////////////////////////////////////////
503 ////////////////////////////////////////////////////////////
504 for(size_t i=0; i<bones.size(); ++i){
507 if(bone.parent_index!=0xFFFF){
508 bone.parent=&bones[bone.parent_index];
509 bone.parent->children.push_back(&bone);
511 if(bone.tail_index!=0){
512 bone.tail=bones[bone.tail_index].pos;
520 bool IO::read(const char *path)
522 std::vector<char> all;
523 binary::readAll(path, all);
527 binary::MemoryReader reader(&all[0], all.size());
532 bool IO::read(const wchar_t *path)
534 std::vector<char> all;
535 binary::readAll(path, all);
536 std::cerr << all.size() << "bytes" << std::endl;
540 binary::MemoryReader reader(&all[0], all.size());
544 bool IO::write(binary::IWriter &w)
547 w.writeValue<float>(version);
549 w.write(comment, 256);
552 //std::cout << "vertices" << std::endl;
553 w.writeValue<DWORD>(vertices.size());
554 for(size_t i=0; i<vertices.size(); ++i){
555 Vertex &v=vertices[i];
556 w.writeValue<float>(v.pos.x);
557 w.writeValue<float>(v.pos.y);
558 w.writeValue<float>(v.pos.z);
559 w.writeValue<float>(v.normal.x);
560 w.writeValue<float>(v.normal.y);
561 w.writeValue<float>(v.normal.z);
562 w.writeValue<float>(v.uv.x);
563 w.writeValue<float>(v.uv.y);
564 w.writeValue<WORD>(v.bone0);
565 w.writeValue<WORD>(v.bone1);
566 w.writeValue<BYTE>(v.weight0);
567 w.writeValue<BYTE>(v.edge_flag);
571 //std::cout << "faces" << std::endl;
572 w.writeValue<DWORD>(indices.size());
573 if(indices.size()>0){
574 w.writeArray<WORD>(&indices[0], indices.size());
578 //std::cout << "materials" << std::endl;
579 w.writeValue<DWORD>(materials.size());
580 for(size_t i=0; i<materials.size(); ++i){
581 Material &m=*materials[i];
582 w.writeValue<float>(m.diffuse.r);
583 w.writeValue<float>(m.diffuse.g);
584 w.writeValue<float>(m.diffuse.b);
585 w.writeValue<float>(m.diffuse.a);
586 w.writeValue<float>(m.shinness);
587 w.writeValue<float>(m.specular.r);
588 w.writeValue<float>(m.specular.g);
589 w.writeValue<float>(m.specular.b);
590 w.writeValue<float>(m.ambient.r);
591 w.writeValue<float>(m.ambient.g);
592 w.writeValue<float>(m.ambient.b);
593 w.writeValue<BYTE>(m.toon_index);
594 w.writeValue<BYTE>(m.flag);
595 w.writeValue<DWORD>(m.vertex_count);
596 w.writeArray<char>(m.texture, 20);
600 //std::cout << "bones" << std::endl;
601 w.writeValue<WORD>(bones.size());
602 for(size_t i=0; i<bones.size(); ++i){
604 w.writeArray<char>(b.name, 20);
605 w.writeValue<WORD>(b.parent_index);
606 w.writeValue<WORD>(b.tail_index);
607 w.writeValue<BYTE>(b.type);
608 w.writeValue<WORD>(b.ik_index);
609 w.writeValue<float>(b.pos.x);
610 w.writeValue<float>(b.pos.y);
611 w.writeValue<float>(b.pos.z);
615 //std::cout << "ik" << std::endl;
616 w.writeValue<WORD>(ik_list.size());
617 for(size_t i=0; i<ik_list.size(); ++i){
619 w.writeValue<WORD>(ik.index);
620 w.writeValue<WORD>(ik.target);
621 w.writeValue<BYTE>(ik.length);
622 w.writeValue<WORD>(ik.iterations);
623 w.writeValue<float>(ik.weight);
624 WORD parent_index=bones[ik.target].parent_index;
625 for(size_t j=0; j<ik.length;
626 ++j, parent_index=bones[parent_index].parent_index){
627 w.writeValue<WORD>(parent_index);
632 //std::cout << "morph" << std::endl;
633 w.writeValue<WORD>(morph_list.size());
634 for(size_t i=0; i<morph_list.size(); ++i){
635 Morph &m=morph_list[i];
636 w.writeArray<char>(m.name, 20);
637 w.writeValue<DWORD>(m.indices.size());
638 w.writeValue<BYTE>(m.type);
639 for(size_t j=0; j<m.indices.size(); ++j){
640 w.writeValue<DWORD>(m.indices[j]);
641 Vector3 &pos=m.pos_list[j];
642 w.writeValue<float>(pos.x);
643 w.writeValue<float>(pos.y);
644 w.writeValue<float>(pos.z);
649 //std::cout << "face list" << std::endl;
650 w.writeValue<BYTE>(face_list.size());
651 if(face_list.size()>0){
652 w.writeArray<WORD>(&face_list[0], face_list.size());
656 //std::cout << "bone name list" << std::endl;
657 w.writeValue<BYTE>(bone_display_name_list.size());
658 for(size_t i=0; i<bone_display_name_list.size(); ++i){
660 w.writeArray<char>(bone_display_name_list[i].name, 50);
664 //std::cout << "bone list" << std::endl;
665 w.writeValue<DWORD>(bone_display_list.size());
666 for(size_t i=0; i<bone_display_list.size(); ++i){
667 w.writeValue<WORD>(bone_display_list[i].first);
668 w.writeValue<BYTE>(bone_display_list[i].second);
671 ////////////////////////////////////////////////////////////
673 ////////////////////////////////////////////////////////////
674 w.writeValue<char>(0x01);
676 ////////////////////////////////////////////////////////////
678 ////////////////////////////////////////////////////////////
679 w.writeArray<char>(english_name, 20);
680 w.writeArray<char>(english_comment, 256);
682 for(size_t i=0; i<bones.size(); ++i){
683 w.writeArray<char>(bones[i].english_name, 20);
686 for(size_t i=1; i<morph_list.size(); ++i){
687 w.writeArray<char>(morph_list[i].english_name, 20);
690 for(size_t i=0; i<bone_display_name_list.size(); ++i){
691 w.writeArray<char>(bone_display_name_list[i].english_name, 50);
694 ////////////////////////////////////////////////////////////
696 ////////////////////////////////////////////////////////////
697 for(size_t i=0; i<10; ++i){
698 w.writeArray<char>(toon_textures[i].name, 100);
701 ////////////////////////////////////////////////////////////
703 ////////////////////////////////////////////////////////////
704 w.writeValue<DWORD>(rigidbodies.size());
705 for(size_t i=0; i<rigidbodies.size(); ++i){
706 RigidBody &rb=rigidbodies[i];
707 w.writeArray<char>(rb.name, 20);
708 w.writeValue<WORD>(rb.boneIndex);
709 w.writeValue<BYTE>(rb.group);
710 w.writeValue<WORD>(rb.target);
711 w.writeValue<BYTE>(rb.shapeType);
712 w.writeValue<float>(rb.w);
713 w.writeValue<float>(rb.h);
714 w.writeValue<float>(rb.d);
715 w.writeValue<float>(rb.position.x);
716 w.writeValue<float>(rb.position.y);
717 w.writeValue<float>(rb.position.z);
718 w.writeValue<float>(rb.rotation.x);
719 w.writeValue<float>(rb.rotation.y);
720 w.writeValue<float>(rb.rotation.z);
721 w.writeValue<float>(rb.weight);
722 w.writeValue<float>(rb.linearDamping);
723 w.writeValue<float>(rb.angularDamping);
724 w.writeValue<float>(rb.restitution);
725 w.writeValue<float>(rb.friction);
726 w.writeValue<BYTE>(rb.processType);
729 ////////////////////////////////////////////////////////////
731 ////////////////////////////////////////////////////////////
732 w.writeValue<DWORD>(constraints.size());
733 for(size_t i=0; i<constraints.size(); ++i){
734 Constraint &c=constraints[i];
735 w.writeArray<char>(c.name, 20);
736 w.writeValue<DWORD>(c.rigidA);
737 w.writeValue<DWORD>(c.rigidB);
738 w.writeValue<float>(c.pos.x);
739 w.writeValue<float>(c.pos.y);
740 w.writeValue<float>(c.pos.z);
741 w.writeValue<float>(c.rot.x);
742 w.writeValue<float>(c.rot.y);
743 w.writeValue<float>(c.rot.z);
744 w.writeValue<float>(c.constraintPosMin.x);
745 w.writeValue<float>(c.constraintPosMin.y);
746 w.writeValue<float>(c.constraintPosMin.z);
747 w.writeValue<float>(c.constraintPosMax.x);
748 w.writeValue<float>(c.constraintPosMax.y);
749 w.writeValue<float>(c.constraintPosMax.z);
750 w.writeValue<float>(c.constraintRotMin.x);
751 w.writeValue<float>(c.constraintRotMin.y);
752 w.writeValue<float>(c.constraintRotMin.z);
753 w.writeValue<float>(c.constraintRotMax.x);
754 w.writeValue<float>(c.constraintRotMax.y);
755 w.writeValue<float>(c.constraintRotMax.z);
756 w.writeValue<float>(c.springPos.x);
757 w.writeValue<float>(c.springPos.y);
758 w.writeValue<float>(c.springPos.z);
759 w.writeValue<float>(c.springRot.x);
760 w.writeValue<float>(c.springRot.y);
761 w.writeValue<float>(c.springRot.z);
767 bool IO::write(const char *path)
769 binary::FileWriter w(path);
773 bool IO::write(const wchar_t *path)
775 binary::FileWriter w(path);
779 const Vector2* IO::getUV(int index)const
781 return &vertices[index].uv;
784 void Bone::setName(const char *src)
786 strncpy(name, src, 20);
789 void BoneDisplayName::setName(const char *src)
791 strncpy(name, src, 20);
794 void BoneDisplayName::setEnglishName(const char *src)
796 strncpy(english_name, src, 20);
799 void Morph::setName(const char *src)
801 strncpy(name, src, 20);
804 void Morph::setEnglishName(const char *src)
806 strncpy(english_name, src, 20);
809 void RigidBody::setName(const char *src)
811 strncpy(name, src, 20);
814 void Constraint::setName(const char *src)
816 strncpy(name, src, 20);
819 void Material::setTexture(const char *src)
821 strncpy(texture, src, 20);