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));
37 BoneGroup::getName()const
39 return text::trim(text::to_WideChar(CP_OEMCP,
40 std::string(name, name+50)));
46 return text::trim(text::to_WideChar(CP_OEMCP,
47 std::string(name, name+20)));
51 IO::getEnglishName()const
53 return text::trim(text::to_WideChar(CP_OEMCP,
54 std::string(english_name, english_name+20)));
58 RigidBody::getName()const
60 return text::trim(text::to_WideChar(CP_OEMCP,
61 std::string(name, name+20)));
65 Constraint::getName()const
67 return text::trim(text::to_WideChar(CP_OEMCP,
68 std::string(name, name+20)));
72 template<class READER>
74 read(READER &reader, Vertex &v)
76 unsigned int pos=reader.getPos();
82 reader.get(v.weight0);
83 reader.get(v.edge_flag);
84 assert(reader.getPos()-pos==38);
89 template<class READER>
91 read(READER &reader, Material &m)
93 unsigned int pos=reader.getPos();
94 reader.get(m.diffuse);
95 reader.get(m.shinness);
96 reader.get(m.specular);
97 reader.get(m.ambient);
98 reader.get(m.toon_index);
100 reader.get(m.vertex_count);
101 text::copyStringAndFillZero(m.texture, reader.getString(20));
102 assert(reader.getPos()-pos==70);
106 template<class READER>
108 read(READER &reader, Bone &b)
110 unsigned int pos=reader.getPos();
111 text::copyStringAndFillZero(b.name, reader.getString(20));
112 reader.get(b.parent_index);
113 reader.get(b.tail_index);
114 b.type=static_cast<BONE_TYPE>(reader.getUchar());
115 reader.get(b.ik_index);
117 assert(reader.getPos()-pos==39);
120 // 11+2xIK_COUNT bytes
121 template<class READER>
123 read(READER &reader, IK &ik)
126 reader.get(ik.index);
127 reader.get(ik.target);
128 reader.get(ik.length);
129 reader.get(ik.iterations);
130 reader.get(ik.weight);
132 for(unsigned short j=0; j<ik.length; ++j){
133 ik.children.push_back(reader.getUshort());
137 // 25+12xMORPH_COUNT bytes
138 template<class READER>
140 read(READER &reader, Morph &m)
143 text::copyStringAndFillZero(m.name, reader.getString(20));
144 reader.get(m.vertex_count);
145 m.type=static_cast<MORPH_TYPE>(reader.getUchar());
146 // 12 x vertex_count bytes
147 for(unsigned short i=0; i<m.vertex_count; ++i){
148 m.indices.push_back(reader.getUint());
149 m.pos_list.push_back(Vector3());
150 reader.get(m.pos_list.back());
155 template<class READER>
157 read(READER &reader, RigidBody &r)
159 unsigned int pos=reader.getPos();
160 text::copyStringAndFillZero(r.name, reader.getString(20));
161 reader.get(r.boneIndex);
163 reader.get(r.target);
164 r.shapeType=static_cast<SHAPE_TYPE>(reader.getUchar());
168 reader.get(r.position);
169 reader.get(r.rotation);
170 reader.get(r.weight);
171 reader.get(r.linearDamping);
172 reader.get(r.angularDamping);
173 reader.get(r.restitution);
174 reader.get<float>(r.friction);
175 r.processType=static_cast<PROCESS_TYPE>(reader.getUchar());
176 assert(reader.getPos()-pos==83);
180 template<class READER>
182 read(READER &reader, Constraint &c)
184 unsigned int base_pos=reader.getPos();
185 text::copyStringAndFillZero(c.name, reader.getString(20));
186 reader.get(c.rigidA);
187 reader.get(c.rigidB);
190 reader.get(c.constraintPosMin);
191 reader.get(c.constraintPosMax);
192 reader.get(c.constraintRotMin);
193 reader.get(c.constraintRotMax);
194 reader.get(c.springPos);
195 reader.get(c.springRot);
196 assert(reader.getPos()-base_pos==124);
202 binary::IReader &reader_;
205 Impl(IO &io, binary::IReader &reader)
206 : io_(io), reader_(reader)
214 if(!parseVertices()){
220 if(!parseMaterials()){
232 if(!parseFaceList()){
235 if(!parseBoneGroupList()){
238 if(!parseBoneList()){
245 ////////////////////////////////////////////////////////////
247 ////////////////////////////////////////////////////////////
249 ////////////////////////////////////////////////////////////
250 if(reader_.getChar()){
251 if(!parseEnglishName()){
254 if(!parseEnglishBone()){
257 if(!parseEnglishMorph()){
260 if(!parseEnglishBoneList()){
269 ////////////////////////////////////////////////////////////
270 if(!parseToonTextures()){
278 ////////////////////////////////////////////////////////////
282 if(!parseConstraint()){
287 assert(reader_.isEnd());
293 bool parseConstraint()
295 unsigned int count=reader_.getUint();
296 for(unsigned int i=0; i<count; ++i){
297 io_.constraints.push_back(Constraint());
298 read(reader_, io_.constraints.back());
305 unsigned int count=reader_.getUint();
306 for(unsigned int i=0; i<count; ++i){
307 io_.rigidbodies.push_back(RigidBody());
308 read(reader_, io_.rigidbodies.back());
313 bool parseToonTextures()
315 for(size_t i=0; i<10; ++i){
316 reader_.getString(100);
321 bool parseEnglishBoneList()
323 for(size_t i=0; i<io_.bone_group_list.size(); ++i){
324 text::copyStringAndFillZero(
325 io_.bone_group_list[i].english_name,
326 reader_.getString(50));
331 bool parseEnglishMorph()
333 int count=io_.morph_list.size()-1;
334 for(int i=0; i<count; ++i){
335 text::copyStringAndFillZero(
336 io_.morph_list[i].english_name, reader_.getString(20));
341 bool parseEnglishBone()
343 for(size_t i=0; i<io_.bones.size(); ++i){
344 text::copyStringAndFillZero(
345 io_.bones[i].english_name, reader_.getString(20));
350 bool parseEnglishName()
352 text::copyStringAndFillZero(io_.english_name,
353 reader_.getString(20));
354 text::copyStringAndFillZero(io_.english_comment,
355 reader_.getString(256));
361 unsigned int count=reader_.getUint();
362 for(unsigned int i=0; i<count; ++i){
363 unsigned short bone=reader_.getUshort();
364 unsigned char disp=reader_.getUchar();
365 io_.bone_display_list.push_back(std::make_pair(bone, disp));
370 bool parseBoneGroupList()
372 unsigned int count=reader_.getUchar();
373 for(unsigned int i=0; i<count; ++i){
374 io_.bone_group_list.push_back(BoneGroup());
375 text::copyStringAndFillZero(
376 io_.bone_group_list.back().name,
377 reader_.getString(50));
384 unsigned int count=reader_.getUchar();
385 for(unsigned int i=0; i<count; ++i){
386 io_.face_list.push_back(reader_.getUshort());
393 unsigned int count=reader_.getUshort();
394 for(unsigned int i=0; i<count; ++i){
395 io_.morph_list.push_back(Morph());
396 read(reader_, io_.morph_list.back());
403 unsigned int count=reader_.getUshort();
404 for(unsigned int i=0; i<count; ++i){
405 io_.ik_list.push_back(IK());
406 read(reader_, io_.ik_list.back());
413 unsigned int count=reader_.getUshort();
414 for(unsigned int i=0; i<count; ++i){
415 io_.bones.push_back(Bone());
416 read(reader_, io_.bones.back());
421 bool parseMaterials()
423 unsigned int count=reader_.getUint();
424 for(unsigned int i=0; i<count; ++i){
425 io_.materials.push_back(new Material());
426 read(reader_, *io_.materials.back());
433 unsigned int count=reader_.getUint();
434 for(unsigned int i=0; i<count; ++i){
435 io_.indices.push_back(reader_.getUshort());
442 unsigned int count=reader_.getUint();
443 for(unsigned int i=0; i<count; ++i){
444 io_.vertices.push_back(Vertex());
445 read(reader_, io_.vertices.back());
452 if(reader_.getString(3)!="Pmd"){
453 //std::cout << "invalid pmd" << std::endl;
456 reader_.get(io_.version);
457 if(io_.version!=1.0){
458 std::cout << "invalid vesion: " << io_.version <<std::endl;
461 text::copyStringAndFillZero(io_.name, reader_.getString(20));
462 text::copyStringAndFillZero(io_.comment, reader_.getString(256));
469 ///////////////////////////////////////////////////////////////////////////////
471 ///////////////////////////////////////////////////////////////////////////////
478 for(size_t i=0; i<materials.size(); ++i){
484 bool IO::read(binary::IReader &input)
486 Impl impl(*this, input);
491 ////////////////////////////////////////////////////////////
493 ////////////////////////////////////////////////////////////
494 if(!morph_list.empty()){
496 assert(morph_list[0].type==MORPH_BASE);
498 Morph &base=morph_list[0];
499 for(size_t i=0; i<base.vertex_count; ++i){
500 assert(vertices[base.indices[i]].pos==base.pos_list[i]);
503 for(size_t i=1; i<morph_list.size(); ++i){
504 Morph &m=morph_list[i];
505 assert(m.type!=MORPH_BASE);
508 ////////////////////////////////////////////////////////////
510 ////////////////////////////////////////////////////////////
511 for(size_t i=0; i<bones.size(); ++i){
514 if(bone.parent_index!=0xFFFF){
515 bone.parent=&bones[bone.parent_index];
516 bone.parent->children.push_back(&bone);
518 if(bone.tail_index!=0){
519 bone.tail=bones[bone.tail_index].pos;
527 bool IO::read(const char *path)
529 std::vector<char> all;
530 binary::readAll(path, all);
534 binary::MemoryReader reader(&all[0], all.size());
539 bool IO::read(const wchar_t *path)
541 std::vector<char> all;
542 binary::readAll(path, all);
543 std::cerr << all.size() << "bytes" << std::endl;
547 binary::MemoryReader reader(&all[0], all.size());
551 bool IO::write(binary::IWriter &w)
554 w.writeValue<float>(version);
556 w.write(comment, 256);
559 //std::cout << "vertices" << std::endl;
560 w.writeValue<DWORD>(vertices.size());
561 for(size_t i=0; i<vertices.size(); ++i){
562 Vertex &v=vertices[i];
563 w.writeValue<float>(v.pos.x);
564 w.writeValue<float>(v.pos.y);
565 w.writeValue<float>(v.pos.z);
566 w.writeValue<float>(v.normal.x);
567 w.writeValue<float>(v.normal.y);
568 w.writeValue<float>(v.normal.z);
569 w.writeValue<float>(v.uv.x);
570 w.writeValue<float>(v.uv.y);
571 w.writeValue<WORD>(v.bone0);
572 w.writeValue<WORD>(v.bone1);
573 w.writeValue<BYTE>(v.weight0);
574 w.writeValue<BYTE>(v.edge_flag);
578 //std::cout << "faces" << std::endl;
579 w.writeValue<DWORD>(indices.size());
580 if(indices.size()>0){
581 w.writeArray<WORD>(&indices[0], indices.size());
585 //std::cout << "materials" << std::endl;
586 w.writeValue<DWORD>(materials.size());
587 for(size_t i=0; i<materials.size(); ++i){
588 Material &m=*materials[i];
589 w.writeValue<float>(m.diffuse.r);
590 w.writeValue<float>(m.diffuse.g);
591 w.writeValue<float>(m.diffuse.b);
592 w.writeValue<float>(m.diffuse.a);
593 w.writeValue<float>(m.shinness);
594 w.writeValue<float>(m.specular.r);
595 w.writeValue<float>(m.specular.g);
596 w.writeValue<float>(m.specular.b);
597 w.writeValue<float>(m.ambient.r);
598 w.writeValue<float>(m.ambient.g);
599 w.writeValue<float>(m.ambient.b);
600 w.writeValue<BYTE>(m.toon_index);
601 w.writeValue<BYTE>(m.flag);
602 w.writeValue<DWORD>(m.vertex_count);
603 w.writeArray<char>(m.texture, 20);
607 //std::cout << "bones" << std::endl;
608 w.writeValue<WORD>(bones.size());
609 for(size_t i=0; i<bones.size(); ++i){
611 w.writeArray<char>(b.name, 20);
612 w.writeValue<WORD>(b.parent_index);
613 w.writeValue<WORD>(b.tail_index);
614 w.writeValue<BYTE>(b.type);
615 w.writeValue<WORD>(b.ik_index);
616 w.writeValue<float>(b.pos.x);
617 w.writeValue<float>(b.pos.y);
618 w.writeValue<float>(b.pos.z);
622 //std::cout << "ik" << std::endl;
623 w.writeValue<WORD>(ik_list.size());
624 for(size_t i=0; i<ik_list.size(); ++i){
626 w.writeValue<WORD>(ik.index);
627 w.writeValue<WORD>(ik.target);
628 w.writeValue<BYTE>(ik.length);
629 w.writeValue<WORD>(ik.iterations);
630 w.writeValue<float>(ik.weight);
631 WORD parent_index=bones[ik.target].parent_index;
632 for(size_t j=0; j<ik.length;
633 ++j, parent_index=bones[parent_index].parent_index){
634 w.writeValue<WORD>(parent_index);
639 //std::cout << "morph" << std::endl;
640 w.writeValue<WORD>(morph_list.size());
641 for(size_t i=0; i<morph_list.size(); ++i){
642 Morph &m=morph_list[i];
643 w.writeArray<char>(m.name, 20);
644 w.writeValue<DWORD>(m.indices.size());
645 w.writeValue<BYTE>(m.type);
646 for(size_t j=0; j<m.indices.size(); ++j){
647 w.writeValue<DWORD>(m.indices[j]);
648 Vector3 &pos=m.pos_list[j];
649 w.writeValue<float>(pos.x);
650 w.writeValue<float>(pos.y);
651 w.writeValue<float>(pos.z);
656 //std::cout << "face list" << std::endl;
657 w.writeValue<BYTE>(face_list.size());
658 if(face_list.size()>0){
659 w.writeArray<WORD>(&face_list[0], face_list.size());
663 //std::cout << "bone name list" << std::endl;
664 w.writeValue<BYTE>(bone_group_list.size());
665 for(size_t i=0; i<bone_group_list.size(); ++i){
667 w.writeArray<char>(bone_group_list[i].name, 50);
671 //std::cout << "bone list" << std::endl;
672 w.writeValue<DWORD>(bone_display_list.size());
673 for(size_t i=0; i<bone_display_list.size(); ++i){
674 w.writeValue<WORD>(bone_display_list[i].first);
675 w.writeValue<BYTE>(bone_display_list[i].second);
678 ////////////////////////////////////////////////////////////
680 ////////////////////////////////////////////////////////////
681 w.writeValue<char>(0x01);
683 ////////////////////////////////////////////////////////////
685 ////////////////////////////////////////////////////////////
686 w.writeArray<char>(english_name, 20);
687 w.writeArray<char>(english_comment, 256);
689 for(size_t i=0; i<bones.size(); ++i){
690 w.writeArray<char>(bones[i].english_name, 20);
693 for(size_t i=1; i<morph_list.size(); ++i){
694 w.writeArray<char>(morph_list[i].english_name, 20);
697 for(size_t i=0; i<bone_group_list.size(); ++i){
698 w.writeArray<char>(bone_group_list[i].english_name, 50);
701 ////////////////////////////////////////////////////////////
703 ////////////////////////////////////////////////////////////
704 for(size_t i=0; i<10; ++i){
705 w.writeArray<char>(toon_textures[i].name, 100);
708 ////////////////////////////////////////////////////////////
710 ////////////////////////////////////////////////////////////
711 w.writeValue<DWORD>(rigidbodies.size());
712 for(size_t i=0; i<rigidbodies.size(); ++i){
713 RigidBody &rb=rigidbodies[i];
714 w.writeArray<char>(rb.name, 20);
715 w.writeValue<WORD>(rb.boneIndex);
716 w.writeValue<BYTE>(rb.group);
717 w.writeValue<WORD>(rb.target);
718 w.writeValue<BYTE>(rb.shapeType);
719 w.writeValue<float>(rb.w);
720 w.writeValue<float>(rb.h);
721 w.writeValue<float>(rb.d);
722 w.writeValue<float>(rb.position.x);
723 w.writeValue<float>(rb.position.y);
724 w.writeValue<float>(rb.position.z);
725 w.writeValue<float>(rb.rotation.x);
726 w.writeValue<float>(rb.rotation.y);
727 w.writeValue<float>(rb.rotation.z);
728 w.writeValue<float>(rb.weight);
729 w.writeValue<float>(rb.linearDamping);
730 w.writeValue<float>(rb.angularDamping);
731 w.writeValue<float>(rb.restitution);
732 w.writeValue<float>(rb.friction);
733 w.writeValue<BYTE>(rb.processType);
736 ////////////////////////////////////////////////////////////
738 ////////////////////////////////////////////////////////////
739 w.writeValue<DWORD>(constraints.size());
740 for(size_t i=0; i<constraints.size(); ++i){
741 Constraint &c=constraints[i];
742 w.writeArray<char>(c.name, 20);
743 w.writeValue<DWORD>(c.rigidA);
744 w.writeValue<DWORD>(c.rigidB);
745 w.writeValue<float>(c.pos.x);
746 w.writeValue<float>(c.pos.y);
747 w.writeValue<float>(c.pos.z);
748 w.writeValue<float>(c.rot.x);
749 w.writeValue<float>(c.rot.y);
750 w.writeValue<float>(c.rot.z);
751 w.writeValue<float>(c.constraintPosMin.x);
752 w.writeValue<float>(c.constraintPosMin.y);
753 w.writeValue<float>(c.constraintPosMin.z);
754 w.writeValue<float>(c.constraintPosMax.x);
755 w.writeValue<float>(c.constraintPosMax.y);
756 w.writeValue<float>(c.constraintPosMax.z);
757 w.writeValue<float>(c.constraintRotMin.x);
758 w.writeValue<float>(c.constraintRotMin.y);
759 w.writeValue<float>(c.constraintRotMin.z);
760 w.writeValue<float>(c.constraintRotMax.x);
761 w.writeValue<float>(c.constraintRotMax.y);
762 w.writeValue<float>(c.constraintRotMax.z);
763 w.writeValue<float>(c.springPos.x);
764 w.writeValue<float>(c.springPos.y);
765 w.writeValue<float>(c.springPos.z);
766 w.writeValue<float>(c.springRot.x);
767 w.writeValue<float>(c.springRot.y);
768 w.writeValue<float>(c.springRot.z);
774 bool IO::write(const char *path)
776 binary::FileWriter w(path);
780 bool IO::write(const wchar_t *path)
782 binary::FileWriter w(path);
786 const Vector2* IO::getUV(int index)const
788 return &vertices[index].uv;
791 void Bone::setName(const char *src)
793 strncpy(name, src, 20);
796 void BoneGroup::setName(const char *src)
798 strncpy(name, src, 20);
801 void BoneGroup::setEnglishName(const char *src)
803 strncpy(english_name, src, 20);
806 void Morph::setName(const char *src)
808 strncpy(name, src, 20);
811 void Morph::setEnglishName(const char *src)
813 strncpy(english_name, src, 20);
816 void RigidBody::setName(const char *src)
818 strncpy(name, src, 20);
821 void Constraint::setName(const char *src)
823 strncpy(name, src, 20);
826 void Material::setTexture(const char *src)
828 strncpy(texture, src, 20);