From 77158e2b1a2646b8b9af62b6645939c4e9e3e766 Mon Sep 17 00:00:00 2001 From: Chuck Walbourn Date: Wed, 9 Jan 2019 17:08:53 -0800 Subject: [PATCH] WaveFrontReader: Ke, map_Ks, map_Kn/norm, map_Ke/map_emissive --- Meshconvert/Mesh.cpp | 48 ++++++++++++++++++++++++++++++++--------- Meshconvert/Mesh.h | 3 +++ Meshconvert/MeshOBJ.cpp | 43 +++++++++++++++++++++++------------- 3 files changed, 69 insertions(+), 25 deletions(-) diff --git a/Meshconvert/Mesh.cpp b/Meshconvert/Mesh.cpp index 3eb77d1b..211f93b2 100644 --- a/Meshconvert/Mesh.cpp +++ b/Meshconvert/Mesh.cpp @@ -2276,20 +2276,48 @@ HRESULT Mesh::ExportToSDKMESH(const wchar_t* szFileName, size_t nMaterials, cons memset(m, 0, sizeof(SDKMESH_MATERIAL)); - int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, - m0->name.c_str(), -1, - m->Name, MAX_MATERIAL_NAME, nullptr, FALSE); - if (!result) + if (!m0->name.empty()) { - *m->Name = 0; + int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, + m0->name.c_str(), -1, + m->Name, MAX_MATERIAL_NAME, nullptr, FALSE); + if (!result) + { + *m->Name = 0; + } } - result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, - m0->texture.c_str(), -1, - m->DiffuseTexture, MAX_TEXTURE_NAME, nullptr, FALSE); - if (!result) + if (!m0->texture.empty()) { - *m->DiffuseTexture = 0; + int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, + m0->texture.c_str(), -1, + m->DiffuseTexture, MAX_TEXTURE_NAME, nullptr, FALSE); + if (!result) + { + *m->DiffuseTexture = 0; + } + } + + if (!m0->normalTexture.empty()) + { + int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, + m0->normalTexture.c_str(), -1, + m->NormalTexture, MAX_TEXTURE_NAME, nullptr, FALSE); + if (!result) + { + *m->NormalTexture = 0; + } + } + + if (!m0->specularTexture.empty()) + { + int result = WideCharToMultiByte(CP_UTF8, WC_NO_BEST_FIT_CHARS, + m0->specularTexture.c_str(), -1, + m->SpecularTexture, MAX_TEXTURE_NAME, nullptr, FALSE); + if (!result) + { + *m->SpecularTexture = 0; + } } m->Diffuse.x = m0->diffuseColor.x; diff --git a/Meshconvert/Mesh.h b/Meshconvert/Mesh.h index f53837b2..8d89861d 100644 --- a/Meshconvert/Mesh.h +++ b/Meshconvert/Mesh.h @@ -95,6 +95,9 @@ class Mesh DirectX::XMFLOAT3 specularColor; DirectX::XMFLOAT3 emissiveColor; std::wstring texture; + std::wstring normalTexture; + std::wstring specularTexture; + std::wstring emissiveTexture; Material() noexcept : perVertexColor(false), diff --git a/Meshconvert/MeshOBJ.cpp b/Meshconvert/MeshOBJ.cpp index 55d6057a..0c4be2c6 100644 --- a/Meshconvert/MeshOBJ.cpp +++ b/Meshconvert/MeshOBJ.cpp @@ -26,6 +26,28 @@ using namespace DirectX; +namespace +{ + std::wstring ProcessTextureFileName(const wchar_t* inName, bool dds) + { + if (!inName || !*inName) + return std::wstring(); + + wchar_t txext[_MAX_EXT] = {}; + wchar_t txfname[_MAX_FNAME] = {}; + _wsplitpath_s(inName, nullptr, 0, nullptr, 0, txfname, _MAX_FNAME, txext, _MAX_EXT); + + if (dds) + { + wcscpy_s(txext, L".dds"); + } + + wchar_t texture[_MAX_PATH] = {}; + _wmakepath_s(texture, nullptr, nullptr, txfname, txext); + return std::wstring(texture); + } +} + HRESULT LoadFromOBJ( const wchar_t* szFilename, std::unique_ptr& inMesh, @@ -108,25 +130,16 @@ HRESULT LoadFromOBJ( mtl.ambientColor = it->vAmbient; mtl.diffuseColor = it->vDiffuse; mtl.specularColor = (it->bSpecular) ? it->vSpecular : XMFLOAT3(0.f, 0.f, 0.f); - mtl.emissiveColor = XMFLOAT3(0.f, 0.f, 0.f); + mtl.emissiveColor = (it->bEmissive) ? it->vEmissive : XMFLOAT3(0.f, 0.f, 0.f); - wchar_t texture[_MAX_PATH] = {}; - if (*it->strTexture) + mtl.texture = ProcessTextureFileName(it->strTexture, dds); + mtl.normalTexture = ProcessTextureFileName(it->strNormalTexture, dds); + mtl.specularTexture = ProcessTextureFileName(it->strSpecularTexture, dds); + if (it->bEmissive) { - wchar_t txext[_MAX_EXT]; - wchar_t txfname[_MAX_FNAME]; - _wsplitpath_s(it->strTexture, nullptr, 0, nullptr, 0, txfname, _MAX_FNAME, txext, _MAX_EXT); - - if (dds) - { - wcscpy_s(txext, L".dds"); - } - - _wmakepath_s(texture, nullptr, nullptr, txfname, txext); + mtl.emissiveTexture = ProcessTextureFileName(it->strEmissiveTexture, dds); } - mtl.texture = texture; - inMaterial.push_back(mtl); } }