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 template<class READER>
53 read(READER &reader, Vertex &v)
55 unsigned int pos=reader.getPos();
61 reader.get(v.weight0);
62 reader.get(v.edge_flag);
63 assert(reader.getPos()-pos==38);
68 template<class READER>
70 read(READER &reader, Material &m)
72 unsigned int pos=reader.getPos();
73 reader.get(m.diffuse);
74 reader.get(m.shinness);
75 reader.get(m.specular);
76 reader.get(m.ambient);
77 reader.get(m.toon_index);
79 reader.get(m.vertex_count);
80 text::copyStringAndFillZero(m.texture, reader.getString(20));
81 assert(reader.getPos()-pos==70);
85 template<class READER>
87 read(READER &reader, Bone &b)
89 unsigned int pos=reader.getPos();
90 text::copyStringAndFillZero(b.name, reader.getString(20));
91 reader.get(b.parent_index);
92 reader.get(b.tail_index);
93 b.type=static_cast<BONE_TYPE>(reader.getUchar());
94 reader.get(b.ik_index);
96 assert(reader.getPos()-pos==39);
99 // 11+2xIK_COUNT bytes
100 template<class READER>
102 read(READER &reader, IK &ik)
105 reader.get(ik.index);
106 reader.get(ik.target);
107 reader.get(ik.length);
108 reader.get(ik.iterations);
109 reader.get(ik.weight);
111 for(unsigned short j=0; j<ik.length; ++j){
112 ik.children.push_back(reader.getUshort());
116 // 25+12xMORPH_COUNT bytes
117 template<class READER>
119 read(READER &reader, Morph &m)
122 text::copyStringAndFillZero(m.name, reader.getString(20));
123 reader.get(m.vertex_count);
124 m.type=static_cast<MORPH_TYPE>(reader.getUchar());
125 // 12 x vertex_count bytes
126 for(unsigned short i=0; i<m.vertex_count; ++i){
127 m.indices.push_back(reader.getUint());
128 m.pos_list.push_back(Vector3());
129 reader.get(m.pos_list.back());
134 template<class READER>
136 read(READER &reader, RigidBody &r)
138 unsigned int pos=reader.getPos();
139 text::copyStringAndFillZero(r.name, reader.getString(20));
140 reader.get(r.boneIndex);
142 reader.get(r.target);
143 r.shapeType=static_cast<SHAPE_TYPE>(reader.getUchar());
147 reader.get(r.position);
148 reader.get(r.rotation);
149 reader.get(r.weight);
150 reader.get(r.linearDamping);
151 reader.get(r.angularDamping);
152 reader.get(r.restitution);
153 reader.get<float>(r.friction);
154 r.processType=static_cast<PROCESS_TYPE>(reader.getUchar());
155 assert(reader.getPos()-pos==83);
159 template<class READER>
161 read(READER &reader, Constraint &c)
163 unsigned int base_pos=reader.getPos();
164 text::copyStringAndFillZero(c.name, reader.getString(20));
165 reader.get(c.rigidA);
166 reader.get(c.rigidB);
169 reader.get(c.constraintPosMin);
170 reader.get(c.constraintPosMax);
171 reader.get(c.constraintRotMin);
172 reader.get(c.constraintRotMax);
173 reader.get(c.springPos);
174 reader.get(c.springRot);
175 assert(reader.getPos()-base_pos==124);
181 binary::IReader &reader_;
184 Impl(IO &io, binary::IReader &reader)
185 : io_(io), reader_(reader)
193 if(!parseVertices()){
199 if(!parseMaterials()){
211 if(!parseFaceList()){
214 if(!parseBoneNameList()){
217 if(!parseBoneList()){
224 ////////////////////////////////////////////////////////////
226 ////////////////////////////////////////////////////////////
228 ////////////////////////////////////////////////////////////
229 if(reader_.getChar()){
230 if(!parseEnglishName()){
233 if(!parseEnglishBone()){
236 if(!parseEnglishMorph()){
239 if(!parseEnglishBoneList()){
248 ////////////////////////////////////////////////////////////
249 if(!parseToonTextures()){
257 ////////////////////////////////////////////////////////////
261 if(!parseConstraint()){
266 assert(reader_.isEnd());
272 bool parseConstraint()
274 unsigned int count=reader_.getUint();
275 for(unsigned int i=0; i<count; ++i){
276 io_.constraints.push_back(Constraint());
277 read(reader_, io_.constraints.back());
284 unsigned int count=reader_.getUint();
285 for(unsigned int i=0; i<count; ++i){
286 io_.rigidbodies.push_back(RigidBody());
287 read(reader_, io_.rigidbodies.back());
292 bool parseToonTextures()
294 for(size_t i=0; i<10; ++i){
295 reader_.getString(100);
300 bool parseEnglishBoneList()
302 for(size_t i=0; i<io_.bone_display_name_list.size(); ++i){
303 text::copyStringAndFillZero(
304 io_.bone_display_name_list[i].english_name,
305 reader_.getString(50));
310 bool parseEnglishMorph()
312 int count=io_.morph_list.size()-1;
313 for(int i=0; i<count; ++i){
314 text::copyStringAndFillZero(
315 io_.morph_list[i].english_name, reader_.getString(20));
320 bool parseEnglishBone()
322 for(size_t i=0; i<io_.bones.size(); ++i){
323 text::copyStringAndFillZero(
324 io_.bones[i].english_name, reader_.getString(20));
329 bool parseEnglishName()
331 text::copyStringAndFillZero(io_.english_name,
332 reader_.getString(20));
333 text::copyStringAndFillZero(io_.english_comment,
334 reader_.getString(256));
340 unsigned int count=reader_.getUint();
341 for(unsigned int i=0; i<count; ++i){
342 unsigned short bone=reader_.getUshort();
343 unsigned char disp=reader_.getUchar();
344 io_.bone_display_list.push_back(std::make_pair(bone, disp));
349 bool parseBoneNameList()
351 unsigned int count=reader_.getUchar();
352 for(unsigned int i=0; i<count; ++i){
353 io_.bone_display_name_list.push_back(BoneDisplayName());
354 text::copyStringAndFillZero(
355 io_.bone_display_name_list.back().name,
356 reader_.getString(50));
363 unsigned int count=reader_.getUchar();
364 for(unsigned int i=0; i<count; ++i){
365 io_.face_list.push_back(reader_.getUshort());
372 unsigned int count=reader_.getUshort();
373 for(unsigned int i=0; i<count; ++i){
374 io_.morph_list.push_back(Morph());
375 read(reader_, io_.morph_list.back());
382 unsigned int count=reader_.getUshort();
383 for(unsigned int i=0; i<count; ++i){
384 io_.ik_list.push_back(IK());
385 read(reader_, io_.ik_list.back());
392 unsigned int count=reader_.getUshort();
393 for(unsigned int i=0; i<count; ++i){
394 io_.bones.push_back(Bone());
395 read(reader_, io_.bones.back());
400 bool parseMaterials()
402 unsigned int count=reader_.getUint();
403 for(unsigned int i=0; i<count; ++i){
404 io_.materials.push_back(new Material());
405 read(reader_, *io_.materials.back());
412 unsigned int count=reader_.getUint();
413 for(unsigned int i=0; i<count; ++i){
414 io_.indices.push_back(reader_.getUshort());
421 unsigned int count=reader_.getUint();
422 for(unsigned int i=0; i<count; ++i){
423 io_.vertices.push_back(Vertex());
424 read(reader_, io_.vertices.back());
431 if(reader_.getString(3)!="Pmd"){
432 //std::cout << "invalid pmd" << std::endl;
435 reader_.get(io_.version);
436 if(io_.version!=1.0){
437 std::cout << "invalid vesion: " << io_.version <<std::endl;
440 text::copyStringAndFillZero(io_.name, reader_.getString(20));
441 text::copyStringAndFillZero(io_.comment, reader_.getString(256));
448 ///////////////////////////////////////////////////////////////////////////////
450 ///////////////////////////////////////////////////////////////////////////////
457 for(size_t i=0; i<materials.size(); ++i){
463 bool IO::read(binary::IReader &input)
465 Impl impl(*this, input);
470 ////////////////////////////////////////////////////////////
472 ////////////////////////////////////////////////////////////
473 if(!morph_list.empty()){
475 assert(morph_list[0].type==MORPH_BASE);
477 Morph &base=morph_list[0];
478 for(size_t i=0; i<base.vertex_count; ++i){
479 assert(vertices[base.indices[i]].pos==base.pos_list[i]);
482 for(size_t i=1; i<morph_list.size(); ++i){
483 Morph &m=morph_list[i];
484 assert(m.type!=MORPH_BASE);
487 ////////////////////////////////////////////////////////////
489 ////////////////////////////////////////////////////////////
490 for(size_t i=0; i<bones.size(); ++i){
493 if(bone.parent_index!=0xFFFF){
494 bone.parent=&bones[bone.parent_index];
495 bone.parent->children.push_back(&bone);
497 if(bone.tail_index!=0){
498 bone.tail=bones[bone.tail_index].pos;
506 bool IO::read(const char *path)
508 std::vector<char> all;
509 binary::readAll(path, all);
513 binary::MemoryReader reader(&all[0], all.size());
518 bool IO::read(const wchar_t *path)
520 std::vector<char> all;
521 binary::readAll(path, all);
522 std::cerr << all.size() << "bytes" << std::endl;
526 binary::MemoryReader reader(&all[0], all.size());
530 bool IO::write(binary::IWriter &w)
533 w.writeValue<float>(version);
535 w.write(comment, 256);
538 //std::cout << "vertices" << std::endl;
539 w.writeValue<DWORD>(vertices.size());
540 for(size_t i=0; i<vertices.size(); ++i){
541 Vertex &v=vertices[i];
542 w.writeValue<float>(v.pos.x);
543 w.writeValue<float>(v.pos.y);
544 w.writeValue<float>(v.pos.z);
545 w.writeValue<float>(v.normal.x);
546 w.writeValue<float>(v.normal.y);
547 w.writeValue<float>(v.normal.z);
548 w.writeValue<float>(v.uv.x);
549 w.writeValue<float>(v.uv.y);
550 w.writeValue<WORD>(v.bone0);
551 w.writeValue<WORD>(v.bone1);
552 w.writeValue<BYTE>(v.weight0);
553 w.writeValue<BYTE>(v.edge_flag);
557 //std::cout << "faces" << std::endl;
558 w.writeValue<DWORD>(indices.size());
559 if(indices.size()>0){
560 w.writeArray<WORD>(&indices[0], indices.size());
564 //std::cout << "materials" << std::endl;
565 w.writeValue<DWORD>(materials.size());
566 for(size_t i=0; i<materials.size(); ++i){
567 Material &m=*materials[i];
568 w.writeValue<float>(m.diffuse.r);
569 w.writeValue<float>(m.diffuse.g);
570 w.writeValue<float>(m.diffuse.b);
571 w.writeValue<float>(m.diffuse.a);
572 w.writeValue<float>(m.shinness);
573 w.writeValue<float>(m.specular.r);
574 w.writeValue<float>(m.specular.g);
575 w.writeValue<float>(m.specular.b);
576 w.writeValue<float>(m.ambient.r);
577 w.writeValue<float>(m.ambient.g);
578 w.writeValue<float>(m.ambient.b);
579 w.writeValue<BYTE>(m.toon_index);
580 w.writeValue<BYTE>(m.flag);
581 w.writeValue<DWORD>(m.vertex_count);
582 w.writeArray<char>(m.texture, 20);
586 //std::cout << "bones" << std::endl;
587 w.writeValue<WORD>(bones.size());
588 for(size_t i=0; i<bones.size(); ++i){
590 w.writeArray<char>(b.name, 20);
591 w.writeValue<WORD>(b.parent_index);
592 w.writeValue<WORD>(b.tail_index);
593 w.writeValue<BYTE>(b.type);
594 w.writeValue<WORD>(b.ik_index);
595 w.writeValue<float>(b.pos.x);
596 w.writeValue<float>(b.pos.y);
597 w.writeValue<float>(b.pos.z);
601 //std::cout << "ik" << std::endl;
602 w.writeValue<WORD>(ik_list.size());
603 for(size_t i=0; i<ik_list.size(); ++i){
605 w.writeValue<WORD>(ik.index);
606 w.writeValue<WORD>(ik.target);
607 w.writeValue<BYTE>(ik.length);
608 w.writeValue<WORD>(ik.iterations);
609 w.writeValue<float>(ik.weight);
610 WORD parent_index=bones[ik.target].parent_index;
611 for(size_t j=0; j<ik.length;
612 ++j, parent_index=bones[parent_index].parent_index){
613 w.writeValue<WORD>(parent_index);
618 //std::cout << "morph" << std::endl;
619 w.writeValue<WORD>(morph_list.size());
620 for(size_t i=0; i<morph_list.size(); ++i){
621 Morph &m=morph_list[i];
622 w.writeArray<char>(m.name, 20);
623 w.writeValue<DWORD>(m.indices.size());
624 w.writeValue<BYTE>(m.type);
625 for(size_t j=0; j<m.indices.size(); ++j){
626 w.writeValue<DWORD>(m.indices[j]);
627 Vector3 &pos=m.pos_list[j];
628 w.writeValue<float>(pos.x);
629 w.writeValue<float>(pos.y);
630 w.writeValue<float>(pos.z);
635 //std::cout << "face list" << std::endl;
636 w.writeValue<BYTE>(face_list.size());
637 if(face_list.size()>0){
638 w.writeArray<WORD>(&face_list[0], face_list.size());
642 //std::cout << "bone name list" << std::endl;
643 w.writeValue<BYTE>(bone_display_name_list.size());
644 for(size_t i=0; i<bone_display_name_list.size(); ++i){
646 w.writeArray<char>(bone_display_name_list[i].name, 50);
650 //std::cout << "bone list" << std::endl;
651 w.writeValue<DWORD>(bone_display_list.size());
652 for(size_t i=0; i<bone_display_list.size(); ++i){
653 w.writeValue<WORD>(bone_display_list[i].first);
654 w.writeValue<BYTE>(bone_display_list[i].second);
657 ////////////////////////////////////////////////////////////
659 ////////////////////////////////////////////////////////////
660 w.writeValue<char>(0x01);
662 ////////////////////////////////////////////////////////////
664 ////////////////////////////////////////////////////////////
665 w.writeArray<char>(english_name, 20);
666 w.writeArray<char>(english_comment, 256);
668 for(size_t i=0; i<bones.size(); ++i){
669 w.writeArray<char>(bones[i].english_name, 20);
672 for(size_t i=1; i<morph_list.size(); ++i){
673 w.writeArray<char>(morph_list[i].english_name, 20);
676 for(size_t i=0; i<bone_display_name_list.size(); ++i){
677 w.writeArray<char>(bone_display_name_list[i].english_name, 50);
680 ////////////////////////////////////////////////////////////
682 ////////////////////////////////////////////////////////////
683 for(size_t i=0; i<10; ++i){
684 w.writeArray<char>(toon_textures[i].name, 100);
690 bool IO::write(const char *path)
692 binary::FileWriter w(path);
696 bool IO::write(const wchar_t *path)
698 binary::FileWriter w(path);
702 const Vector2* IO::getUV(int index)const
704 return &vertices[index].uv;
707 void Bone::setName(const char *src)
709 strncpy(name, src, 20);
712 void BoneDisplayName::setName(const char *src)
714 strncpy(name, src, 20);
717 void BoneDisplayName::setEnglishName(const char *src)
719 strncpy(english_name, src, 20);
722 void Morph::setName(const char *src)
724 strncpy(name, src, 20);
727 void Morph::setEnglishName(const char *src)
729 strncpy(english_name, src, 20);