249 | 249 |
IDeviceContext* pContext,
|
250 | 250 |
const IGLTFModelCreateInfo& CI)
|
251 | 251 |
{
|
252 | |
LoadFromFile(pDevice, pContext, CI);
|
|
252 |
if (CI.FileName) {
|
|
253 |
LoadFromFile(pDevice, pContext, CI);
|
|
254 |
} else if (CI.Data) {
|
|
255 |
LoadFromMemory(pDevice, pContext, CI);
|
|
256 |
} else {
|
|
257 |
LOG_ERROR_AND_THROW("Either File Name or Data must be set");
|
|
258 |
}
|
253 | 259 |
}
|
254 | 260 |
|
255 | 261 |
Model::~Model()
|
|
1752 | 1758 |
LOG_WARNING_MESSAGE("Loaded gltf file ", filename, " with the following warning:", warning);
|
1753 | 1759 |
}
|
1754 | 1760 |
|
1755 | |
LoadTextureSamplers(pDevice, gltf_model);
|
1756 | |
LoadTextures(pDevice, gltf_model, LoaderData.BaseDir, pTextureCache, pResourceMgr);
|
1757 | |
LoadMaterials(gltf_model);
|
1758 | |
|
|
1761 |
LoadScene(pDevice, gltf_model, LoaderData.BaseDir, pTextureCache, pResourceMgr, CI);
|
|
1762 |
|
|
1763 |
if (pContext != nullptr)
|
|
1764 |
{
|
|
1765 |
PrepareGPUResources(pDevice, pContext);
|
|
1766 |
}
|
|
1767 |
}
|
|
1768 |
|
|
1769 |
void Model::LoadFromMemory(IRenderDevice* pDevice,
|
|
1770 |
IDeviceContext* pContext,
|
|
1771 |
const IGLTFModelCreateInfo& CI)
|
|
1772 |
{
|
|
1773 |
if (CI.Data == nullptr || CI.DataSize == 0)
|
|
1774 |
LOG_ERROR_AND_THROW("Data must not be empty");
|
|
1775 |
|
|
1776 |
auto* const pTextureCache = CI.pTextureCache;
|
|
1777 |
auto* const pResourceMgr = CI.pCacheInfo != nullptr ? CI.pCacheInfo->pResourceMgr : nullptr;
|
|
1778 |
if (CI.pTextureCache != nullptr && pResourceMgr != nullptr)
|
|
1779 |
LOG_WARNING_MESSAGE("Texture cache is ignored when resource manager is used");
|
|
1780 |
|
|
1781 |
Callbacks::ImageLoaderData LoaderData{pTextureCache, pResourceMgr};
|
|
1782 |
LoaderData.BaseDir = "";
|
|
1783 |
|
|
1784 |
tinygltf::TinyGLTF gltf_context;
|
|
1785 |
gltf_context.SetImageLoader(Callbacks::LoadImageData, &LoaderData);
|
|
1786 |
|
|
1787 |
bool binary;
|
|
1788 |
switch (CI.FileType) {
|
|
1789 |
case GLTF_FILE_TYPE_UNKNOWN:
|
|
1790 |
LOG_ERROR_AND_THROW("FileType must not be UNKNOWN when loading from memory");
|
|
1791 |
case GLTF_FILE_TYPE_ASCII:
|
|
1792 |
binary = false;
|
|
1793 |
break;
|
|
1794 |
case GLTF_FILE_TYPE_BINARY:
|
|
1795 |
binary = true;
|
|
1796 |
break;
|
|
1797 |
}
|
|
1798 |
|
|
1799 |
std::string error;
|
|
1800 |
std::string warning;
|
|
1801 |
tinygltf::Model gltf_model;
|
|
1802 |
|
|
1803 |
bool modelLoaded = false;
|
|
1804 |
if (binary)
|
|
1805 |
modelLoaded = gltf_context.LoadBinaryFromMemory(&gltf_model, &error, &warning, (const unsigned char*)CI.Data, CI.DataSize);
|
|
1806 |
else
|
|
1807 |
modelLoaded = gltf_context.LoadASCIIFromString(&gltf_model, &error, &warning, (const char*)CI.Data, CI.DataSize, "");
|
|
1808 |
if (!modelLoaded)
|
|
1809 |
{
|
|
1810 |
LOG_ERROR_AND_THROW("Failed to load gltf file from memory: ", error);
|
|
1811 |
}
|
|
1812 |
if (!warning.empty())
|
|
1813 |
{
|
|
1814 |
LOG_WARNING_MESSAGE("Loaded gltf file from memory with the following warning:", warning);
|
|
1815 |
}
|
|
1816 |
|
|
1817 |
LoadScene(pDevice, gltf_model, "", pTextureCache, pResourceMgr, CI);
|
|
1818 |
|
|
1819 |
if (pContext != nullptr)
|
|
1820 |
{
|
|
1821 |
PrepareGPUResources(pDevice, pContext);
|
|
1822 |
}
|
|
1823 |
}
|
|
1824 |
|
|
1825 |
void Model::LoadScene(IRenderDevice* pDevice,
|
|
1826 |
const tinygltf::Model& gltf_model,
|
|
1827 |
const std::string& BaseDir,
|
|
1828 |
GLTF_TextureCacheType* pTextureCache,
|
|
1829 |
ResourceManager* pResourceMgr,
|
|
1830 |
const IGLTFModelCreateInfo& CI)
|
|
1831 |
{
|
1759 | 1832 |
std::vector<Uint32> IndexData;
|
1760 | 1833 |
std::vector<VertexBasicAttribs> VertexBasicData;
|
1761 | 1834 |
std::vector<VertexSkinAttribs> VertexSkinData;
|
|
1835 |
|
|
1836 |
LoadTextureSamplers(pDevice, gltf_model);
|
|
1837 |
LoadTextures(pDevice, gltf_model, BaseDir, pTextureCache, pResourceMgr);
|
|
1838 |
LoadMaterials(gltf_model);
|
1762 | 1839 |
|
1763 | 1840 |
// TODO: scene handling with no default scene
|
1764 | 1841 |
const tinygltf::Scene& scene = gltf_model.scenes[gltf_model.defaultScene > -1 ? gltf_model.defaultScene : 0];
|
|
1853 | 1930 |
CreateBuffer(GLTF_BUFFER_ID_INDEX, IndexData.data(), IndexData.size() * sizeof(IndexData[0]),
|
1854 | 1931 |
BIND_INDEX_BUFFER, "GLTF index buffer");
|
1855 | 1932 |
}
|
1856 | |
|
1857 | |
if (pContext != nullptr)
|
1858 | |
{
|
1859 | |
PrepareGPUResources(pDevice, pContext);
|
1860 | |
}
|
1861 | 1933 |
}
|
1862 | 1934 |
|
1863 | 1935 |
void Model::CalculateBoundingBox(Node* node, const Node* parent)
|