[add] Material

This commit is contained in:
JinhoBa
2025-11-09 12:37:06 +09:00
parent 17803912d6
commit 09c1aafac9
6 changed files with 304 additions and 5 deletions
+220
View File
@@ -90,6 +90,222 @@ HRESULT CMaterial::Initialize(const _char* pModelFilePath, const aiMaterial* pAI
return S_OK;
}
HRESULT CMaterial::Initialize(const _char* pMaterialFilePath)
{
_char szTextureFilePath[MAX_PATH] = {};
_char szMaterialFilePath[MAX_PATH] = {};
strcpy_s(szTextureFilePath, "../Bin/Resources/Models/");
strcpy_s(szMaterialFilePath, pMaterialFilePath);
strcat_s(szMaterialFilePath, ".props.txt");
if (FAILED(Read_MaterialFile(szMaterialFilePath, szTextureFilePath)))
return E_FAIL;
return S_OK;
}
HRESULT CMaterial::Read_MaterialFile(const _char* pMaterialFilePath, const _char* pTextureFolderPath)
{
/* Read MaterialFile to Read TextureFilePath */
ifstream file(pMaterialFilePath);
if (!file.is_open())
{
MSG_BOX("Failed to Open Materail File");
return E_FAIL;
}
_char szTextureFilePath[MAX_PATH] = {};
string strText = {};
string Value = {};
string Type = {};
_uint iDataIndex = {};
_uint iNumParameter = {};
_uint iBeginIndex = {};
_uint iEndIndex = {};
_bool bParent = { false };
getline(file, strText);
if (!strcmp(strText.c_str(), "none"))
return S_OK;
while (file)
{
if (2 < iDataIndex)
break;
if (false == bParent)
{
getline(file, strText);
auto index = strText.find_first_of("[");
auto iBackindex = strText.find_first_of("]");
if (index == string::npos || iBackindex == string::npos)
{
return S_OK;
}
if (index + 1 != iBackindex - 1)
iNumParameter = (strText[iBackindex - 1] - '0') + 10;
else
iNumParameter = strText[index + 1] - '0';
getline(file, strText);
bParent = true;
}
else
{
if (0 == iNumParameter)
{
++iDataIndex;
auto index = strText.find_first_of("[");
iNumParameter = strText[index + 1] - '0';
getline(file, strText);
}
for (_uint i = 0; i < iNumParameter; ++i)
{
for (_uint j = 0; j < 3; ++j)
getline(file, strText);
getline(file, strText);
switch (iDataIndex)
{
/* Scala */
case 0:
// value
Value = strText.substr(iBeginIndex + 2);
getline(file, strText);
// name
iBeginIndex = (_uint)strText.find_first_of('=');
Type = strText.substr(iBeginIndex + 2);
break;
/* Texture */
case 1:
// value
iBeginIndex = (_uint)strText.find("Environment");
iEndIndex = (_uint)strText.find('.');
if (256 < iBeginIndex || 256 < iEndIndex)
Value = "none";
else
Value = strText.substr(iBeginIndex, iEndIndex - iBeginIndex);
strcpy_s(szTextureFilePath, pTextureFolderPath);
strcat_s(szTextureFilePath, Value.c_str());
// name
iBeginIndex = (_uint)strText.find_last_of('_');
iEndIndex = (_uint)strText.rfind('\'');
Type = strText.substr(iBeginIndex + 1, iEndIndex - iBeginIndex - 1);
getline(file, strText);
// Add Texture
if (FAILED(Add_Texture(szTextureFilePath, Type)))
return E_FAIL;
break;
case 2:
break;
default:
break;
}
getline(file, strText);
}
++iDataIndex;
bParent = false;
getline(file, strText);
}
}
file.close();
return S_OK;
}
HRESULT CMaterial::Add_Texture(const _char* pTextureFolderPath, string& FileType)
{
ID3D11ShaderResourceView* pSRV = {};
aiTextureType eTexture = {};
/* Find type */
if (!strcmp(FileType.c_str(), "D"))
eTexture = aiTextureType::aiTextureType_DIFFUSE;
else if (!strcmp(FileType.c_str(), "N"))
eTexture = aiTextureType::aiTextureType_NORMALS;
else if (!strcmp(FileType.c_str(), "MRO"))
eTexture = aiTextureType::aiTextureType_METALNESS;
else if (!strcmp(FileType.c_str(), "MROH"))
eTexture = aiTextureType::aiTextureType_METALNESS;
else if (!strcmp(FileType.c_str(), "MROA'"))
eTexture = aiTextureType::aiTextureType_DIFFUSE_ROUGHNESS;
else if (!strcmp(FileType.c_str(), "SRO"))
eTexture = aiTextureType::aiTextureType_AMBIENT_OCCLUSION;
else if (!strcmp(FileType.c_str(), "SROH"))
eTexture = aiTextureType::aiTextureType_AMBIENT_OCCLUSION;
else if (!strcmp(FileType.c_str(), "HDR'"))
eTexture = aiTextureType::aiTextureType_DISPLACEMENT;
else
{
MSG_BOX("Failed to Path Material Texture Type");
return S_OK;
}
_char TexturePath[MAX_PATH] = {};
strcpy_s(TexturePath, pTextureFolderPath);
strcat_s(TexturePath, ".dds");
_tchar szTextureFilePath[MAX_PATH] = {};
string strTexturePath;
MultiByteToWideChar(CP_ACP, 0, TexturePath, (_int)strlen(TexturePath), szTextureFilePath, MAX_PATH);
if (FAILED(CreateDDSTextureFromFile(m_pDevice, szTextureFilePath, nullptr, &pSRV)))
{
_char TexturePath_Png[MAX_PATH] = {};
strcpy_s(TexturePath_Png, pTextureFolderPath);
strcat_s(TexturePath_Png, ".png");
_tchar szTextureFilePath_Png[MAX_PATH] = {};
MultiByteToWideChar(CP_ACP, 0, TexturePath_Png, (_int)strlen(TexturePath_Png), szTextureFilePath_Png, MAX_PATH);
if (FAILED(CreateWICTextureFromFile(m_pDevice, szTextureFilePath_Png, nullptr, &pSRV)))
{
MSG_BOX("Failed to Load TextureFile");
return E_FAIL;
}
strTexturePath = CMyTools::ToString(szTextureFilePath_Png);
}
else
{
strTexturePath = CMyTools::ToString(szTextureFilePath);
}
m_SaveMaterial.Path[ENUM_CLASS(eTexture)].push_back(strTexturePath);
m_SRVs[ENUM_CLASS(eTexture)].push_back(pSRV);
return S_OK;
}
HRESULT CMaterial::SaveAsBinary(HANDLE hFile, DWORD& dwByte)
{
// 실제 값을 저장하지 않고 파일이름.확장자명의 문자열을 저장해서 수정하기 편하게 만들기
@@ -121,6 +337,10 @@ CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContex
return pInstance;
}
CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const _char* pMaterialName)
{
return nullptr;
}
#endif
HRESULT CMaterial::Bind_SRV(CShader* pShader, const _char* pConstantName, _uint iType, _uint iTextureIndex)