From 04d70dc76d6fb27c58bb3d923a79910f7074f1eb Mon Sep 17 00:00:00 2001 From: aoowweenn Date: Sat, 31 Mar 2018 13:29:42 +0800 Subject: [PATCH 1/5] adjust encoding to fit the new utf8 library --- code/MMDPmxParser.cpp | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/code/MMDPmxParser.cpp b/code/MMDPmxParser.cpp index 2eb724a31..fa0d4b823 100644 --- a/code/MMDPmxParser.cpp +++ b/code/MMDPmxParser.cpp @@ -93,16 +93,13 @@ namespace pmx if (encoding == 0) { // UTF16 to UTF8 - std::string result; - - const char* sourceStart = buffer.data(); + const uint16_t* sourceStart = (uint16_t*)buffer.data(); const unsigned int targetSize = size * 3; // enough to encode - char* targetStart = new char[targetSize](); - const char* targetReserved = targetStart; - utf8::utf16to8( sourceStart, sourceStart + size, targetStart ); + char targetStart[targetSize] = { 0 }; + + utf8::utf16to8( sourceStart, sourceStart + size/2, targetStart ); - result.assign(targetReserved, targetStart - targetReserved); - delete[] targetReserved; + std::string result(targetStart); return result; } else @@ -474,7 +471,6 @@ namespace pmx void PmxSoftBody::Read(std::istream * /*stream*/, PmxSetting * /*setting*/) { - // 未実装 std::cerr << "Not Implemented Exception" << std::endl; throw; } @@ -510,7 +506,6 @@ namespace pmx void PmxModel::Read(std::istream *stream) { - // マジック char magic[4]; stream->read((char*) magic, sizeof(char) * 4); if (magic[0] != 0x50 || magic[1] != 0x4d || magic[2] != 0x58 || magic[3] != 0x20) @@ -518,23 +513,20 @@ namespace pmx std::cerr << "invalid magic number." << std::endl; throw; } - // バージョン stream->read((char*) &version, sizeof(float)); if (version != 2.0f && version != 2.1f) { std::cerr << "this is not ver2.0 or ver2.1 but " << version << "." << std::endl; throw; } - // ファイル設定 this->setting.Read(stream); - // モデル情報 this->model_name = ReadString(stream, setting.encoding); this->model_english_name = ReadString(stream, setting.encoding); this->model_comment = ReadString(stream, setting.encoding); this->model_english_comment = ReadString(stream, setting.encoding); - // 頂点 + // read vertices stream->read((char*) &vertex_count, sizeof(int)); this->vertices = mmd::make_unique(vertex_count); for (int i = 0; i < vertex_count; i++) @@ -542,7 +534,7 @@ namespace pmx vertices[i].Read(stream, &setting); } - // 面 + // read indices stream->read((char*) &index_count, sizeof(int)); this->indices = mmd::make_unique(index_count); for (int i = 0; i < index_count; i++) @@ -550,7 +542,7 @@ namespace pmx this->indices[i] = ReadIndex(stream, setting.vertex_index_size); } - // テクスチャ + // read texture names stream->read((char*) &texture_count, sizeof(int)); this->textures = mmd::make_unique(texture_count); for (int i = 0; i < texture_count; i++) @@ -558,7 +550,7 @@ namespace pmx this->textures[i] = ReadString(stream, setting.encoding); } - // マテリアル + // read materials stream->read((char*) &material_count, sizeof(int)); this->materials = mmd::make_unique(material_count); for (int i = 0; i < material_count; i++) @@ -566,7 +558,7 @@ namespace pmx this->materials[i].Read(stream, &setting); } - // ボーン + // read bones stream->read((char*) &this->bone_count, sizeof(int)); this->bones = mmd::make_unique(this->bone_count); for (int i = 0; i < this->bone_count; i++) @@ -574,7 +566,7 @@ namespace pmx this->bones[i].Read(stream, &setting); } - // モーフ + // read morphs stream->read((char*) &this->morph_count, sizeof(int)); this->morphs = mmd::make_unique(this->morph_count); for (int i = 0; i < this->morph_count; i++) @@ -582,7 +574,7 @@ namespace pmx this->morphs[i].Read(stream, &setting); } - // 表示枠 + // read display frames stream->read((char*) &this->frame_count, sizeof(int)); this->frames = mmd::make_unique(this->frame_count); for (int i = 0; i < this->frame_count; i++) @@ -590,7 +582,7 @@ namespace pmx this->frames[i].Read(stream, &setting); } - // 剛体 + // read rigid bodies stream->read((char*) &this->rigid_body_count, sizeof(int)); this->rigid_bodies = mmd::make_unique(this->rigid_body_count); for (int i = 0; i < this->rigid_body_count; i++) @@ -598,7 +590,7 @@ namespace pmx this->rigid_bodies[i].Read(stream, &setting); } - // ジョイント + // read joints stream->read((char*) &this->joint_count, sizeof(int)); this->joints = mmd::make_unique(this->joint_count); for (int i = 0; i < this->joint_count; i++) From 1ea723078590aca11a8fc55ba21799f0ef977f62 Mon Sep 17 00:00:00 2001 From: aoowweenn Date: Sat, 31 Mar 2018 14:06:26 +0800 Subject: [PATCH 2/5] Handle materials with texture id < 0 --- code/MMDImporter.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/code/MMDImporter.cpp b/code/MMDImporter.cpp index 76ad9115c..474586413 100644 --- a/code/MMDImporter.cpp +++ b/code/MMDImporter.cpp @@ -354,8 +354,11 @@ aiMaterial *MMDImporter::CreateMaterial(const pmx::PmxMaterial *pMat, float shininess = pMat->specularlity; mat->AddProperty(&shininess, 1, AI_MATKEY_SHININESS_STRENGTH); - aiString texture_path(pModel->textures[pMat->diffuse_texture_index]); - mat->AddProperty(&texture_path, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); + if(pMat->diffuse_texture_index >= 0) { + aiString texture_path(pModel->textures[pMat->diffuse_texture_index]); + mat->AddProperty(&texture_path, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); + } + int mapping_uvwsrc = 0; mat->AddProperty(&mapping_uvwsrc, 1, AI_MATKEY_UVWSRC(aiTextureType_DIFFUSE, 0)); From 21b518e350383f3954111b11910f5af726b9c6a5 Mon Sep 17 00:00:00 2001 From: aoowweenn Date: Sat, 31 Mar 2018 14:44:18 +0800 Subject: [PATCH 3/5] use memset to allow VS compiler --- code/MMDPmxParser.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/MMDPmxParser.cpp b/code/MMDPmxParser.cpp index fa0d4b823..2a6a20b48 100644 --- a/code/MMDPmxParser.cpp +++ b/code/MMDPmxParser.cpp @@ -95,7 +95,8 @@ namespace pmx // UTF16 to UTF8 const uint16_t* sourceStart = (uint16_t*)buffer.data(); const unsigned int targetSize = size * 3; // enough to encode - char targetStart[targetSize] = { 0 }; + char targetStart[targetSize]; + std::memset(targetStart, 0, targetSize * sizeof(char)); utf8::utf16to8( sourceStart, sourceStart + size/2, targetStart ); From d42f9a3226bfe62ef959653a59f892e29f4c40c7 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sat, 22 Sep 2018 12:46:38 +0200 Subject: [PATCH 4/5] Update MMDImporter.cpp Reformattings --- code/MMDImporter.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/code/MMDImporter.cpp b/code/MMDImporter.cpp index a7a76aa82..84b9e35a6 100644 --- a/code/MMDImporter.cpp +++ b/code/MMDImporter.cpp @@ -72,18 +72,16 @@ using namespace std; // ------------------------------------------------------------------------------------------------ // Default constructor MMDImporter::MMDImporter() - : m_Buffer(), - // m_pRootObject( NULL ), - m_strAbsPath("") { - DefaultIOSystem io; - m_strAbsPath = io.getOsSeparator(); +: m_Buffer() +, m_strAbsPath("") { + DefaultIOSystem io; + m_strAbsPath = io.getOsSeparator(); } // ------------------------------------------------------------------------------------------------ // Destructor. MMDImporter::~MMDImporter() { - // delete m_pRootObject; - // m_pRootObject = NULL; + // empty } // ------------------------------------------------------------------------------------------------ @@ -96,8 +94,7 @@ bool MMDImporter::CanRead(const std::string &pFile, IOSystem *pIOHandler, } else // Check file Header { static const char *pTokens[] = {"PMX "}; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, - 1); + return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 1); } } From 56392cc02f849e0f2e3bc17557dad2f116590d4a Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sat, 22 Sep 2018 12:50:23 +0200 Subject: [PATCH 5/5] Update MMDPmxParser.cpp Fix encoding for readstrings. --- code/MMDPmxParser.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/MMDPmxParser.cpp b/code/MMDPmxParser.cpp index 355ef2146..2c5bd9a8d 100644 --- a/code/MMDPmxParser.cpp +++ b/code/MMDPmxParser.cpp @@ -96,12 +96,13 @@ namespace pmx // UTF16 to UTF8 const uint16_t* sourceStart = (uint16_t*)buffer.data(); const unsigned int targetSize = size * 3; // enough to encode - char targetStart[targetSize]; + char *targetStart = new char[targetSize]; std::memset(targetStart, 0, targetSize * sizeof(char)); utf8::utf16to8( sourceStart, sourceStart + size/2, targetStart ); std::string result(targetStart); + delete [] targetStart; return result; } else