539 lines
15 KiB
C++
539 lines
15 KiB
C++
#include "pch.h"
|
|
#include "Material.h"
|
|
#include "GameInstance.h"
|
|
#include "Shader.h"
|
|
|
|
CMaterial::CMaterial(ID3D11Device* pDevice, ID3D11DeviceContext* pContext)
|
|
: m_pDevice{ pDevice }
|
|
, m_pContext{ pContext }
|
|
, m_pGameInstance{ CGameInstance::GetInstance() }
|
|
{
|
|
SAFE_ADDREF(m_pDevice);
|
|
SAFE_ADDREF(m_pContext);
|
|
SAFE_ADDREF(m_pGameInstance);
|
|
}
|
|
#ifdef EDITOR_PROJECT
|
|
HRESULT CMaterial::Initialize(const _char* pModelFilePath, const aiMaterial* pAIMaterial)
|
|
{
|
|
for (size_t i = 0; i < AI_TEXTURE_TYPE_MAX; i++)
|
|
{
|
|
_uint iNumSRVs = pAIMaterial->GetTextureCount(static_cast<aiTextureType>(i));
|
|
|
|
m_SRVs[i].reserve(iNumSRVs);
|
|
|
|
ID3D11ShaderResourceView* pSRV = { nullptr };
|
|
|
|
HRESULT hr = {};
|
|
|
|
for (size_t j = 0; j < iNumSRVs; j++)
|
|
{
|
|
aiString strTexturePath;
|
|
|
|
if (FAILED(pAIMaterial->GetTexture(static_cast<aiTextureType>(i), (_uint)j, &strTexturePath))) {
|
|
continue;
|
|
}
|
|
_char szTextureDir[MAX_PATH] = {};
|
|
_char szSaveDir[MAX_PATH] = {};
|
|
_char szDrive[MAX_PATH] = {};
|
|
_char szDir[MAX_PATH] = {};
|
|
_char szName[MAX_PATH] = {};
|
|
_char szExt[MAX_PATH] = {};
|
|
_splitpath_s(pModelFilePath, szDrive, MAX_PATH, szDir, MAX_PATH, nullptr, 0, nullptr, 0);
|
|
_splitpath_s(strTexturePath.C_Str(), nullptr, 0, nullptr, 0, szName, MAX_PATH, szExt, MAX_PATH);
|
|
strcpy_s(szTextureDir, sizeof(_char) * MAX_PATH, szDrive);
|
|
strcat_s(szTextureDir, sizeof(_char) * MAX_PATH, szDir);
|
|
strcat_s(szTextureDir, sizeof(_char) * MAX_PATH, szName);
|
|
strcat_s(szTextureDir, sizeof(_char) * MAX_PATH, szExt);
|
|
|
|
{
|
|
_char szTextureFileName[MAX_PATH] = {};
|
|
_char szTextureFilePath[MAX_PATH] = {};
|
|
_char szDrive[MAX_PATH] = {};
|
|
_char szDir[MAX_PATH] = {};
|
|
_char szFileName[MAX_PATH] = {};
|
|
_char szEXT[MAX_PATH] = {};
|
|
_splitpath_s(szTextureDir, szDrive, MAX_PATH, szDir, MAX_PATH, szFileName, MAX_PATH, szEXT, MAX_PATH);
|
|
strcpy_s(szTextureFileName, szDrive);
|
|
strcat_s(szTextureFileName, szDir);
|
|
strcat_s(szTextureFileName, szFileName);
|
|
|
|
strcat_s(szTextureFilePath, szTextureFileName);
|
|
strcat_s(szTextureFilePath, ".dds");
|
|
|
|
hr = CreateDDSTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV); // 일단 dds로 시도하고
|
|
if (FAILED(hr)) {
|
|
memset(szTextureFilePath, 0, sizeof(_char) * MAX_PATH); // 실패하면 원래 확장자로 다시 시도
|
|
strcat_s(szTextureFilePath, szTextureFileName);
|
|
strcat_s(szTextureFilePath, szEXT);
|
|
|
|
if (false == strcmp(".tga", szEXT)) {
|
|
assert(false); // trying to Load TGA
|
|
hr = S_OK;
|
|
}
|
|
else {
|
|
hr = CreateWICTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV);
|
|
}
|
|
}
|
|
m_SaveMaterial.Path[i].push_back(szTextureFilePath);
|
|
}
|
|
|
|
strcpy_s(szSaveDir, sizeof(_char) * MAX_PATH, szName);
|
|
strcat_s(szSaveDir, sizeof(_char) * MAX_PATH, szExt);
|
|
|
|
if (FAILED(hr)) {
|
|
return E_FAIL;
|
|
}
|
|
m_strPath[i].push_back(szSaveDir);
|
|
m_SRVs[i].push_back(pSRV);
|
|
}
|
|
}
|
|
|
|
return S_OK;
|
|
}
|
|
HRESULT CMaterial::Initialize(const _char* pMaterialFilePath, const _char* pTextureFilePath)
|
|
{
|
|
_char szTextureFilePath[MAX_PATH] = {};
|
|
_char szMaterialFilePath[MAX_PATH] = {};
|
|
|
|
strcpy_s(szTextureFilePath, pTextureFilePath);
|
|
|
|
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;
|
|
return S_OK;
|
|
}
|
|
|
|
_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(), "normal"))
|
|
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_SPECULAR;
|
|
else if (!strcmp(FileType.c_str(), "SROH"))
|
|
eTexture = aiTextureType::aiTextureType_SPECULAR;
|
|
else if (!strcmp(FileType.c_str(), "SROA"))
|
|
eTexture = aiTextureType::aiTextureType_SPECULAR;
|
|
else if (!strcmp(FileType.c_str(), "HDR"))
|
|
return S_OK;
|
|
else if (!strcmp(FileType.c_str(), "MSK"))
|
|
eTexture = aiTextureType::aiTextureType_MAYA_BASE;
|
|
else if (!strcmp(FileType.c_str(), "basecolor"))
|
|
eTexture = aiTextureType::aiTextureType_DIFFUSE;
|
|
else if (!strcmp(FileType.c_str(), "E"))
|
|
eTexture = aiTextureType::aiTextureType_EMISSIVE;
|
|
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)
|
|
{
|
|
// 실제 값을 저장하지 않고 파일이름.확장자명의 문자열을 저장해서 수정하기 편하게 만들기
|
|
// 대신 리소스를 공유해야함
|
|
// 텍스쳐를 저장하지 않고
|
|
// 텍스쳐 경로만 ( "이름.확장자" ) 만 저장함
|
|
for (int i = 0; i < AI_TEXTURE_TYPE_MAX; ++i) {
|
|
_uint iNumSrv = (_uint)m_strPath[i].size();
|
|
WriteFile(hFile, &i, sizeof(_uint), &dwByte, nullptr); // 종류 저장
|
|
WriteFile(hFile, &iNumSrv, sizeof(_uint), &dwByte, nullptr); // 종류의 갯수 저장
|
|
for (_uint j = 0; j < iNumSrv; ++j) {
|
|
m_strPath[i][j].shrink_to_fit();
|
|
_uint strLength = (_uint)(sizeof(_char) * m_strPath[i][j].length());
|
|
WriteFile(hFile, &strLength, sizeof(_uint), &dwByte, nullptr); // 사이즈 저장
|
|
WriteFile(hFile, m_strPath[i][j].data(), strLength, &dwByte, nullptr); // 실제 값 저장
|
|
}
|
|
}
|
|
return S_OK;
|
|
}
|
|
CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const aiMaterial* pAIMaterial)
|
|
{
|
|
CMaterial* pInstance = new CMaterial(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize(pModelFilePath, pAIMaterial)))
|
|
{
|
|
MSG_BOX("Failed to Created : CMaterial");
|
|
SAFE_RELEASE(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pMaterialFilePath, const _char* pTextureFilePath)
|
|
{
|
|
CMaterial* pInstance = new CMaterial(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize(pMaterialFilePath, pTextureFilePath)))
|
|
{
|
|
MSG_BOX("Failed to Created : CMaterial");
|
|
SAFE_RELEASE(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
#endif
|
|
|
|
HRESULT CMaterial::Bind_SRV(CShader* pShader, const _char* pConstantName, _uint iType, _uint iTextureIndex)
|
|
{
|
|
if (m_SRVs[iType].empty()) {
|
|
//assert(false);
|
|
return S_OK;
|
|
}
|
|
|
|
|
|
return pShader->Bind_SRV(pConstantName, m_SRVs[iType][iTextureIndex]);
|
|
}
|
|
|
|
|
|
HRESULT CMaterial::Initialize(const _char* pModelFilePath, HANDLE hFile, DWORD& dwByte)
|
|
{
|
|
for (int i = 0; i < AI_TEXTURE_TYPE_MAX; ++i) {
|
|
_uint iNumSrv = { 0 };
|
|
if (!ReadFile(hFile, &i, sizeof(_uint), &dwByte, nullptr)) {
|
|
return E_FAIL;
|
|
}
|
|
if (!ReadFile(hFile, &iNumSrv, sizeof(_uint), &dwByte, nullptr)) {
|
|
return E_FAIL;
|
|
}
|
|
if (iNumSrv == 0) {
|
|
continue;
|
|
}
|
|
m_SRVs[i].reserve(iNumSrv);
|
|
|
|
ID3D11ShaderResourceView* pSRV = { nullptr };
|
|
HRESULT hr = {};
|
|
|
|
for (_uint j = 0; j < iNumSrv; ++j) {
|
|
_uint strLength = { 0 };
|
|
if (!ReadFile(hFile, &strLength, sizeof(_uint), &dwByte, nullptr)) {
|
|
return E_FAIL;
|
|
}
|
|
_char szTextureName[MAX_PATH] = {};
|
|
if (!ReadFile(hFile, szTextureName, strLength, &dwByte, nullptr)) {
|
|
return E_FAIL;
|
|
}
|
|
|
|
_char szTextureDir[MAX_PATH] = {};
|
|
_char szDrive[MAX_PATH] = {};
|
|
_char szDir[MAX_PATH] = {};
|
|
_splitpath_s(pModelFilePath, szDrive, MAX_PATH, szDir, MAX_PATH, nullptr, 0, nullptr, 0);
|
|
strcpy_s(szTextureDir, sizeof(_char) * MAX_PATH, szDrive);
|
|
strcat_s(szTextureDir, sizeof(_char) * MAX_PATH, szDir);
|
|
strcat_s(szTextureDir, sizeof(_char) * MAX_PATH, szTextureName);
|
|
|
|
|
|
{
|
|
_char szTextureFileName[MAX_PATH] = {};
|
|
_char szTextureFilePath[MAX_PATH] = {};
|
|
_char szDrive[MAX_PATH] = {};
|
|
_char szDir[MAX_PATH] = {};
|
|
_char szFileName[MAX_PATH] = {};
|
|
_char szEXT[MAX_PATH] = {};
|
|
_splitpath_s(szTextureDir, szDrive, MAX_PATH, szDir, MAX_PATH, szFileName, MAX_PATH, szEXT, MAX_PATH);
|
|
strcpy_s(szTextureFileName, szDrive);
|
|
strcat_s(szTextureFileName, szDir);
|
|
strcat_s(szTextureFileName, szFileName);
|
|
|
|
strcat_s(szTextureFilePath, szTextureFileName);
|
|
strcat_s(szTextureFilePath, ".dds");
|
|
|
|
hr = CreateDDSTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV); // 일단 dds로 시도하고
|
|
if (FAILED(hr)) {
|
|
memset(szTextureFilePath, 0, sizeof(_char) * MAX_PATH); // 실패하면 원래 확장자로 다시 시도
|
|
strcat_s(szTextureFilePath, szTextureFileName);
|
|
strcat_s(szTextureFilePath, szEXT);
|
|
|
|
if (false == strcmp(".tga", szEXT)) {
|
|
assert(false); // trying to Load TGA
|
|
hr = S_OK;
|
|
}
|
|
else {
|
|
hr = CreateWICTextureFromFile(m_pDevice, CMyTools::ToWstring(szTextureFilePath).c_str(), nullptr, &pSRV);
|
|
}
|
|
}
|
|
|
|
}
|
|
if (FAILED(hr)) {
|
|
return E_FAIL;
|
|
}
|
|
|
|
m_SRVs[i].push_back(pSRV);
|
|
}
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
HRESULT CMaterial::Initialize(const _char* pModelFilePath, const SaveMaterial& _SaveMaterial)
|
|
{
|
|
for (size_t type = 0; type < 27; type++)
|
|
{
|
|
for (const auto& path : _SaveMaterial.Path[type])
|
|
{
|
|
char szDrive[MAX_PATH] = {};
|
|
char szDir[MAX_PATH] = {};
|
|
_splitpath_s(pModelFilePath, szDrive, MAX_PATH, szDir, MAX_PATH, nullptr, 0, nullptr, 0);
|
|
|
|
char szFullPath[MAX_PATH] = {};
|
|
strcpy_s(szFullPath, szDrive);
|
|
strcat_s(szFullPath, path.c_str());
|
|
|
|
_tchar szPerfectPath[MAX_PATH] = {};
|
|
MultiByteToWideChar(CP_ACP, 0, szFullPath, -1, szPerfectPath, MAX_PATH);
|
|
|
|
ID3D11ShaderResourceView* pSRV = nullptr;
|
|
HRESULT hr = S_OK;
|
|
|
|
const char* ext = strrchr(szFullPath, '.');
|
|
if (ext)
|
|
{
|
|
if (strcmp(ext, ".dds") == 0)
|
|
hr = CreateDDSTextureFromFile(m_pDevice, szPerfectPath, nullptr, &pSRV);
|
|
else if (strcmp(ext, ".tga") == 0)
|
|
hr = S_OK;
|
|
else
|
|
hr = CreateWICTextureFromFile(m_pDevice, szPerfectPath, nullptr, &pSRV);
|
|
}
|
|
|
|
if (FAILED(hr))
|
|
return E_FAIL;
|
|
|
|
m_SRVs[type].push_back(pSRV);
|
|
}
|
|
}
|
|
return S_OK;
|
|
}
|
|
|
|
|
|
CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, HANDLE hFile, DWORD& dwByte) {
|
|
CMaterial* pInstance = new CMaterial(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize(pModelFilePath, hFile, dwByte)))
|
|
{
|
|
MSG_BOX("Failed to Created : CMaterial");
|
|
SAFE_RELEASE(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
CMaterial* CMaterial::Create(ID3D11Device* pDevice, ID3D11DeviceContext* pContext, const _char* pModelFilePath, const SaveMaterial& _SaveMaterial)
|
|
{
|
|
CMaterial* pInstance = new CMaterial(pDevice, pContext);
|
|
|
|
if (FAILED(pInstance->Initialize(pModelFilePath, _SaveMaterial)))
|
|
{
|
|
MSG_BOX("Failed to Created : CMaterial");
|
|
Safe_Release(pInstance);
|
|
}
|
|
|
|
return pInstance;
|
|
}
|
|
|
|
|
|
void CMaterial::Free()
|
|
{
|
|
__super::Free();
|
|
|
|
SAFE_RELEASE(m_pGameInstance);
|
|
SAFE_RELEASE(m_pDevice);
|
|
SAFE_RELEASE(m_pContext);
|
|
|
|
for (auto& SRVs : m_SRVs)
|
|
{
|
|
for (auto& pSRV : SRVs) {
|
|
SAFE_RELEASE(pSRV);
|
|
}
|
|
SRVs.clear();
|
|
}
|
|
}
|