X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=src%2Fpmd.cpp;h=23059ab6c32311cf64451e565dae13fde397e000;hb=8093d81853548bc573586ee41f84f26971bc8acb;hp=6d0a95d952f43fd9de0784684827149373c76061;hpb=5b1d24fcf5aec37d73d79c1e7bd4dc0c7414d7f4;p=meshio%2Fmeshio.git diff --git a/src/pmd.cpp b/src/pmd.cpp old mode 100644 new mode 100755 index 6d0a95d..23059ab --- a/src/pmd.cpp +++ b/src/pmd.cpp @@ -1,9 +1,66 @@ #include "pmd.h" #include +#include "text.h" namespace meshio { namespace pmd { +std::wstring + Material::getTexture()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(texture, texture+20))); + } + +std::wstring + Bone::getName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(name, name+20))); + } + +std::wstring + Morph::getName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(name, name+20))); + } + +void + Morph::append(int index, float x, float y, float z) + { + indices.push_back(index); + pos_list.push_back(Vector3(x, y, z)); + } + +std::wstring + IO::getName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(name, name+20))); + } + +std::wstring + IO::getEnglishName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(english_name, english_name+20))); + } + +std::wstring + RigidBody::getName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(name, name+20))); + } + +std::wstring + Constraint::getName()const + { + return text::trim(text::to_WideChar(CP_OEMCP, + std::string(name, name+20))); + } + // 38bytes template void @@ -256,8 +313,10 @@ private: bool parseEnglishBoneList() { - for(size_t i=0; ichildren.push_back(&bone); } - if(bone.tail_index!=0xFFFF){ + if(bone.tail_index!=0){ bone.tail=bones[bone.tail_index].pos; } } @@ -453,9 +516,9 @@ bool IO::read(binary::IReader &input) return true; } +/* bool IO::read(const char *path) { - //std::locale::global(std::locale("japanese")); std::vector all; binary::readAll(path, all); if(all.empty()){ @@ -464,12 +527,296 @@ bool IO::read(const char *path) binary::MemoryReader reader(&all[0], all.size()); return read(reader); } +*/ -bool IO::write(std::ostream &os) +bool IO::read(const wchar_t *path) { - return false; + std::vector all; + binary::readAll(path, all); + std::cerr << all.size() << "bytes" << std::endl; + if(all.empty()){ + return false; + } + binary::MemoryReader reader(&all[0], all.size()); + return read(reader); } +bool IO::write(binary::IWriter &w) +{ + w.write("Pmd", 3); + w.writeValue(version); + w.write(name, 20); + w.write(comment, 256); + + // vertices + //std::cout << "vertices" << std::endl; + w.writeValue(vertices.size()); + for(size_t i=0; i(v.pos.x); + w.writeValue(v.pos.y); + w.writeValue(v.pos.z); + w.writeValue(v.normal.x); + w.writeValue(v.normal.y); + w.writeValue(v.normal.z); + w.writeValue(v.uv.x); + w.writeValue(v.uv.y); + w.writeValue(v.bone0); + w.writeValue(v.bone1); + w.writeValue(v.weight0); + w.writeValue(v.edge_flag); + } + + // faces + //std::cout << "faces" << std::endl; + w.writeValue(indices.size()); + if(indices.size()>0){ + w.writeArray(&indices[0], indices.size()); + } + + // materials + //std::cout << "materials" << std::endl; + w.writeValue(materials.size()); + for(size_t i=0; i(m.diffuse.r); + w.writeValue(m.diffuse.g); + w.writeValue(m.diffuse.b); + w.writeValue(m.diffuse.a); + w.writeValue(m.shinness); + w.writeValue(m.specular.r); + w.writeValue(m.specular.g); + w.writeValue(m.specular.b); + w.writeValue(m.ambient.r); + w.writeValue(m.ambient.g); + w.writeValue(m.ambient.b); + w.writeValue(m.toon_index); + w.writeValue(m.flag); + w.writeValue(m.vertex_count); + w.writeArray(m.texture, 20); + } + + // bones + //std::cout << "bones" << std::endl; + w.writeValue(bones.size()); + for(size_t i=0; i(b.name, 20); + w.writeValue(b.parent_index); + w.writeValue(b.tail_index); + w.writeValue(b.type); + w.writeValue(b.ik_index); + w.writeValue(b.pos.x); + w.writeValue(b.pos.y); + w.writeValue(b.pos.z); + } + + // ik + //std::cout << "ik" << std::endl; + w.writeValue(ik_list.size()); + for(size_t i=0; i(ik.index); + w.writeValue(ik.target); + w.writeValue(ik.length); + w.writeValue(ik.iterations); + w.writeValue(ik.weight); + WORD parent_index=bones[ik.target].parent_index; + for(size_t j=0; j(parent_index); + } + } + + // morph + //std::cout << "morph" << std::endl; + w.writeValue(morph_list.size()); + for(size_t i=0; i(m.name, 20); + w.writeValue(m.indices.size()); + w.writeValue(m.type); + for(size_t j=0; j(m.indices[j]); + Vector3 &pos=m.pos_list[j]; + w.writeValue(pos.x); + w.writeValue(pos.y); + w.writeValue(pos.z); + } + } + + // face list + //std::cout << "face list" << std::endl; + w.writeValue(face_list.size()); + if(face_list.size()>0){ + w.writeArray(&face_list[0], face_list.size()); + } + + // bone naem list + //std::cout << "bone name list" << std::endl; + w.writeValue(bone_display_name_list.size()); + for(size_t i=0; i(bone_display_name_list[i].name, 50); + } + + // bone list + //std::cout << "bone list" << std::endl; + w.writeValue(bone_display_list.size()); + for(size_t i=0; i(bone_display_list[i].first); + w.writeValue(bone_display_list[i].second); + } + + //////////////////////////////////////////////////////////// + // extend + //////////////////////////////////////////////////////////// + w.writeValue(0x01); + + //////////////////////////////////////////////////////////// + // english names + //////////////////////////////////////////////////////////// + w.writeArray(english_name, 20); + w.writeArray(english_comment, 256); + + for(size_t i=0; i(bones[i].english_name, 20); + } + + for(size_t i=1; i(morph_list[i].english_name, 20); + } + + for(size_t i=0; i(bone_display_name_list[i].english_name, 50); + } + + //////////////////////////////////////////////////////////// + // toon textures + //////////////////////////////////////////////////////////// + for(size_t i=0; i<10; ++i){ + w.writeArray(toon_textures[i].name, 100); + } + + //////////////////////////////////////////////////////////// + // rigid bodies + //////////////////////////////////////////////////////////// + w.writeValue(rigidbodies.size()); + for(size_t i=0; i(rb.name, 20); + w.writeValue(rb.boneIndex); + w.writeValue(rb.group); + w.writeValue(rb.target); + w.writeValue(rb.shapeType); + w.writeValue(rb.w); + w.writeValue(rb.h); + w.writeValue(rb.d); + w.writeValue(rb.position.x); + w.writeValue(rb.position.y); + w.writeValue(rb.position.z); + w.writeValue(rb.rotation.x); + w.writeValue(rb.rotation.y); + w.writeValue(rb.rotation.z); + w.writeValue(rb.weight); + w.writeValue(rb.linearDamping); + w.writeValue(rb.angularDamping); + w.writeValue(rb.restitution); + w.writeValue(rb.friction); + w.writeValue(rb.processType); + } + + //////////////////////////////////////////////////////////// + // constraints + //////////////////////////////////////////////////////////// + w.writeValue(constraints.size()); + for(size_t i=0; i(c.name, 20); + w.writeValue(c.rigidA); + w.writeValue(c.rigidB); + w.writeValue(c.pos.x); + w.writeValue(c.pos.y); + w.writeValue(c.pos.z); + w.writeValue(c.rot.x); + w.writeValue(c.rot.y); + w.writeValue(c.rot.z); + w.writeValue(c.constraintPosMin.x); + w.writeValue(c.constraintPosMin.y); + w.writeValue(c.constraintPosMin.z); + w.writeValue(c.constraintPosMax.x); + w.writeValue(c.constraintPosMax.y); + w.writeValue(c.constraintPosMax.z); + w.writeValue(c.constraintRotMin.x); + w.writeValue(c.constraintRotMin.y); + w.writeValue(c.constraintRotMin.z); + w.writeValue(c.constraintRotMax.x); + w.writeValue(c.constraintRotMax.y); + w.writeValue(c.constraintRotMax.z); + w.writeValue(c.springPos.x); + w.writeValue(c.springPos.y); + w.writeValue(c.springPos.z); + w.writeValue(c.springRot.x); + w.writeValue(c.springRot.y); + w.writeValue(c.springRot.z); + } + + return true; +} + +bool IO::write(const char *path) +{ + binary::FileWriter w(path); + return write(w); +} + +bool IO::write(const wchar_t *path) +{ + binary::FileWriter w(path); + return write(w); +} + +const Vector2* IO::getUV(int index)const +{ + return &vertices[index].uv; +} + +void Bone::setName(const char *src) +{ + strncpy(name, src, 20); +} + +void BoneDisplayName::setName(const char *src) +{ + strncpy(name, src, 20); +} + +void BoneDisplayName::setEnglishName(const char *src) +{ + strncpy(english_name, src, 20); +} + +void Morph::setName(const char *src) +{ + strncpy(name, src, 20); +} + +void Morph::setEnglishName(const char *src) +{ + strncpy(english_name, src, 20); +} + +void RigidBody::setName(const char *src) +{ + strncpy(name, src, 20); +} + +void Constraint::setName(const char *src) +{ + strncpy(name, src, 20); +} + + } // namespace } // namespace