From 7a25f5ac252d0efb8b9928c18c32d473ba123b07 Mon Sep 17 00:00:00 2001 From: aoowweenn Date: Thu, 2 Mar 2017 23:16:29 +0800 Subject: [PATCH] finish UV texture --- code/MMDImporter.cpp | 73 ++++++++++++++++++++++++++++++++++++++------ code/MMDImporter.h | 13 ++------ 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/code/MMDImporter.cpp b/code/MMDImporter.cpp index 69e885498..681608516 100644 --- a/code/MMDImporter.cpp +++ b/code/MMDImporter.cpp @@ -159,6 +159,12 @@ void MMDImporter::CreateDataFromImport(const pmx::PmxModel* pModel, aiScene* pSc std::cout << pScene->mRootNode->mName.C_Str() << std::endl; std::cout << pModel->index_count << std::endl; + pNode = new aiNode; + pScene->mRootNode->addChildren(1, &pNode); + pScene->mRootNode->mNumChildren = 1; + pNode->mParent = pScene->mRootNode; + pNode->mName.Set(string(pModel->model_name) + string("_mesh")); + // split mesh by materials pNode->mNumMeshes = pModel->material_count; pNode->mMeshes = new unsigned int[pNode->mNumMeshes]; @@ -180,15 +186,34 @@ void MMDImporter::CreateDataFromImport(const pmx::PmxModel* pModel, aiScene* pSc indexStart += indexCount; } + // create textures, may be dummy? + /* + pScene->mNumTextures = pModel->texture_count; + pScene->mTextures = new aiTexture*[pScene->mNumTextures]; + for( unsigned int i = 0; i < pScene->mNumTextures; ++i) { + aiTexture *tex = new aiTexture; + pScene->mTextures[i] = tex; + strcpy(tex->achFormatHint, "png"); + tex->mHeight = 0; + ifstream file(pModel->textures[i], ios::binary | ios::ate); + streamsize size = file.tellg(); + file.seekg(0, ios::beg); + char *buffer = new char[size]; + file.read(buffer, size); + if(file.bad()) { + string err("PMX: Can't open texture file"); + err.append(pModel->textures[i]); + throw DeadlyExportError(err); + } + tex->pcData = (aiTexel*)buffer; + } + */ + + // create materials pScene->mNumMaterials = pModel->material_count; pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials]; for( unsigned int i = 0; i < pScene->mNumMaterials; i++ ) { - aiMaterial* mat = new aiMaterial; - pScene->mMaterials[i] = mat; - aiString name(pModel->materials[i].material_name); - mat->AddProperty(&name, AI_MATKEY_NAME); - aiColor3D color(0.01*i, 0.01*i, 0.01*i); - mat->AddProperty(&color, 1, AI_MATKEY_COLOR_DIFFUSE); + pScene->mMaterials[i] = CreateMaterial(&pModel->materials[i], pModel); } // Convert everything to OpenGL space @@ -238,17 +263,47 @@ aiMesh* MMDImporter::CreateMesh(const pmx::PmxModel* pModel, const int indexStar const float* normal = v->normal; pMesh->mNormals[index].Set(normal[0], normal[1], normal[2]); pMesh->mTextureCoords[0][index].x = v->uv[0]; - pMesh->mTextureCoords[0][index].y = v->uv[1]; + pMesh->mTextureCoords[0][index].y = -v->uv[1]; for( int i = 1; i <= pModel->setting.uv; i++ ) { // TODO: wrong here? use quaternion transform? - pMesh->mTextureCoords[i][index].x = v->uva[i][2] - v->uva[i][0]; - pMesh->mTextureCoords[i][index].y = v->uva[i][3] - v->uva[i][1]; + pMesh->mTextureCoords[i][index].x = v->uva[i][0]; + pMesh->mTextureCoords[i][index].y = v->uva[i][1]; } } return pMesh; } +// ------------------------------------------------------------------------------------------------ +aiMaterial* MMDImporter::CreateMaterial(const pmx::PmxMaterial* pMat, const pmx::PmxModel* pModel) +{ + aiMaterial *mat = new aiMaterial(); + aiString name(pMat->material_english_name); + mat->AddProperty(&name, AI_MATKEY_NAME); + + aiColor3D diffuse(pMat->diffuse[0], pMat->diffuse[1], pMat->diffuse[2]); + mat->AddProperty(&diffuse, 1, AI_MATKEY_COLOR_DIFFUSE); + aiColor3D specular(pMat->specular[0], pMat->specular[1], pMat->specular[2]); + mat->AddProperty(&specular, 1, AI_MATKEY_COLOR_SPECULAR); + aiColor3D ambient(pMat->ambient[0], pMat->ambient[1], pMat->ambient[2]); + mat->AddProperty(&ambient, 1, AI_MATKEY_COLOR_AMBIENT); + + float opacity = pMat->diffuse[3]; + mat->AddProperty(&opacity, 1, AI_MATKEY_OPACITY); + 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)); + int mapping_uvwsrc = 0; + mat->AddProperty(&mapping_uvwsrc, 1, AI_MATKEY_UVWSRC(aiTextureType_DIFFUSE, 0)); + int mapping_mode = aiTextureMapMode_Mirror; + mat->AddProperty(&mapping_mode, 1, AI_MATKEY_MAPPINGMODE_U(aiTextureType_DIFFUSE, 0)); + mat->AddProperty(&mapping_mode, 1, AI_MATKEY_MAPPINGMODE_V(aiTextureType_DIFFUSE, 0)); + + return mat; +} + // ------------------------------------------------------------------------------------------------ } // Namespace Assimp diff --git a/code/MMDImporter.h b/code/MMDImporter.h index 66e9c1fd4..e660abaaa 100644 --- a/code/MMDImporter.h +++ b/code/MMDImporter.h @@ -46,17 +46,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include struct aiMesh; -struct aiNode; namespace Assimp { -/* -namespace MMDFile { - struct Object; - struct Model; -} -*/ - // ------------------------------------------------------------------------------------------------ /// \class MMDImporter /// \brief Imports MMD a pmx/pmd/vmd file @@ -87,11 +79,12 @@ private: //! \brief Create the mesh aiMesh* CreateMesh(const pmx::PmxModel* pModel, const int indexStart, const int indexCount); + //! \brief Create the material + aiMaterial* CreateMaterial(const pmx::PmxMaterial* pMat, const pmx::PmxModel* pModel); + private: //! Data buffer std::vector m_Buffer; - //! Pointer to root object instance - //MMDFile::Object *m_pRootObject; //! Absolute pathname of model in file system std::string m_strAbsPath; };