2016-08-22 21:17:57 +03:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// File: ModelLoadSDKMESH.cpp
|
|
|
|
//
|
2021-02-27 10:01:15 +03:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-24 09:08:23 +03:00
|
|
|
// Licensed under the MIT License.
|
2016-08-22 21:17:57 +03:00
|
|
|
//
|
|
|
|
// http://go.microsoft.com/fwlink/?LinkID=615561
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "Model.h"
|
|
|
|
|
|
|
|
#include "Effects.h"
|
|
|
|
#include "VertexTypes.h"
|
|
|
|
|
|
|
|
#include "DirectXHelpers.h"
|
|
|
|
#include "PlatformHelpers.h"
|
|
|
|
#include "BinaryReader.h"
|
|
|
|
#include "DescriptorHeap.h"
|
|
|
|
#include "CommonStates.h"
|
|
|
|
|
|
|
|
#include "SDKMesh.h"
|
|
|
|
|
|
|
|
using namespace DirectX;
|
|
|
|
using Microsoft::WRL::ComPtr;
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2020-02-19 08:22:47 +03:00
|
|
|
enum : unsigned int
|
2016-10-06 00:35:54 +03:00
|
|
|
{
|
2018-10-30 00:49:04 +03:00
|
|
|
PER_VERTEX_COLOR = 0x1,
|
|
|
|
SKINNING = 0x2,
|
|
|
|
DUAL_TEXTURE = 0x4,
|
|
|
|
NORMAL_MAPS = 0x8,
|
|
|
|
BIASED_VERTEX_NORMALS = 0x10,
|
|
|
|
USES_OBSOLETE_DEC3N = 0x20,
|
2016-10-06 00:35:54 +03:00
|
|
|
};
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
int GetUniqueTextureIndex(const wchar_t* textureName, std::map<std::wstring, int>& textureDictionary)
|
|
|
|
{
|
|
|
|
if (textureName == nullptr || !textureName[0])
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
auto i = textureDictionary.find(textureName);
|
|
|
|
if (i == std::cend(textureDictionary))
|
|
|
|
{
|
|
|
|
int index = static_cast<int>(textureDictionary.size());
|
|
|
|
textureDictionary[textureName] = index;
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-17 09:19:38 +03:00
|
|
|
inline XMFLOAT3 GetMaterialColor(float r, float g, float b, bool srgb) noexcept
|
2020-02-19 08:22:47 +03:00
|
|
|
{
|
|
|
|
if (srgb)
|
|
|
|
{
|
|
|
|
XMVECTOR v = XMVectorSet(r, g, b, 1.f);
|
|
|
|
v = XMColorSRGBToRGB(v);
|
|
|
|
|
|
|
|
XMFLOAT3 result;
|
|
|
|
XMStoreFloat3(&result, v);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return XMFLOAT3(r, g, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:45:24 +03:00
|
|
|
template<size_t sizeOfBuffer>
|
|
|
|
inline void ASCIIToWChar(wchar_t (&buffer)[sizeOfBuffer], const char *ascii)
|
|
|
|
{
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, ascii, -1, buffer, sizeOfBuffer);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
void InitMaterial(
|
|
|
|
const DXUT::SDKMESH_MATERIAL& mh,
|
2016-10-06 00:35:54 +03:00
|
|
|
unsigned int flags,
|
2016-08-22 21:17:57 +03:00
|
|
|
_Out_ Model::ModelMaterialInfo& m,
|
2020-02-19 08:22:47 +03:00
|
|
|
_Inout_ std::map<std::wstring, int32_t>& textureDictionary,
|
|
|
|
bool srgb)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-10-25 03:42:54 +03:00
|
|
|
wchar_t matName[DXUT::MAX_MATERIAL_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(matName, mh.Name);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-10-25 03:42:54 +03:00
|
|
|
wchar_t diffuseName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(diffuseName, mh.DiffuseTexture);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-10-25 03:42:54 +03:00
|
|
|
wchar_t specularName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(specularName, mh.SpecularTexture);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-10-25 03:42:54 +03:00
|
|
|
wchar_t normalName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(normalName, mh.NormalTexture);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2016-10-06 00:35:54 +03:00
|
|
|
if ((flags & DUAL_TEXTURE) && !mh.SpecularTexture[0])
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
DebugTrace("WARNING: Material '%s' has multiple texture coords but not multiple textures\n", mh.Name);
|
2019-05-25 02:36:18 +03:00
|
|
|
flags &= ~static_cast<unsigned int>(DUAL_TEXTURE);
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 12:16:45 +03:00
|
|
|
if (mh.NormalTexture[0])
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2021-10-07 12:16:45 +03:00
|
|
|
flags |= NORMAL_MAPS;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m = {};
|
|
|
|
m.name = matName;
|
2016-10-06 00:35:54 +03:00
|
|
|
m.perVertexColor = (flags & PER_VERTEX_COLOR) != 0;
|
|
|
|
m.enableSkinning = (flags & SKINNING) != 0;
|
|
|
|
m.enableDualTexture = (flags & DUAL_TEXTURE) != 0;
|
|
|
|
m.enableNormalMaps = (flags & NORMAL_MAPS) != 0;
|
|
|
|
m.biasedVertexNormals = (flags & BIASED_VERTEX_NORMALS) != 0;
|
|
|
|
|
2017-06-08 02:30:35 +03:00
|
|
|
if (mh.Ambient.x == 0 && mh.Ambient.y == 0 && mh.Ambient.z == 0 && mh.Ambient.w == 0
|
|
|
|
&& mh.Diffuse.x == 0 && mh.Diffuse.y == 0 && mh.Diffuse.z == 0 && mh.Diffuse.w == 0)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2017-06-08 02:30:35 +03:00
|
|
|
// SDKMESH material color block is uninitalized; assume defaults
|
|
|
|
m.diffuseColor = XMFLOAT3(1.f, 1.f, 1.f);
|
|
|
|
m.alphaValue = 1.f;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-19 08:22:47 +03:00
|
|
|
m.ambientColor = GetMaterialColor(mh.Ambient.x, mh.Ambient.y, mh.Ambient.z, srgb);
|
|
|
|
m.diffuseColor = GetMaterialColor(mh.Diffuse.x, mh.Diffuse.y, mh.Diffuse.z, srgb);
|
|
|
|
m.emissiveColor = GetMaterialColor(mh.Emissive.x, mh.Emissive.y, mh.Emissive.z, srgb);
|
2017-06-08 02:30:35 +03:00
|
|
|
|
|
|
|
if (mh.Diffuse.w != 1.f && mh.Diffuse.w != 0.f)
|
|
|
|
{
|
|
|
|
m.alphaValue = mh.Diffuse.w;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m.alphaValue = 1.f;
|
|
|
|
|
2018-06-12 01:42:45 +03:00
|
|
|
if (mh.Power > 0)
|
2017-06-08 02:30:35 +03:00
|
|
|
{
|
|
|
|
m.specularPower = mh.Power;
|
|
|
|
m.specularColor = XMFLOAT3(mh.Specular.x, mh.Specular.y, mh.Specular.z);
|
|
|
|
}
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m.diffuseTextureIndex = GetUniqueTextureIndex(diffuseName, textureDictionary);
|
|
|
|
m.specularTextureIndex = GetUniqueTextureIndex(specularName, textureDictionary);
|
|
|
|
m.normalTextureIndex = GetUniqueTextureIndex(normalName, textureDictionary);
|
|
|
|
|
|
|
|
m.samplerIndex = (m.diffuseTextureIndex == -1) ? -1 : static_cast<int>(CommonStates::SamplerIndex::AnisotropicWrap);
|
2016-10-06 00:35:54 +03:00
|
|
|
m.samplerIndex2 = (flags & DUAL_TEXTURE) ? static_cast<int>(CommonStates::SamplerIndex::AnisotropicWrap) : -1;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2019-01-18 23:41:08 +03:00
|
|
|
void InitMaterial(
|
|
|
|
const DXUT::SDKMESH_MATERIAL_V2& mh,
|
|
|
|
unsigned int flags,
|
|
|
|
_Out_ Model::ModelMaterialInfo& m,
|
2019-05-25 02:36:18 +03:00
|
|
|
_Inout_ std::map<std::wstring, int>& textureDictionary)
|
2019-01-18 23:41:08 +03:00
|
|
|
{
|
|
|
|
wchar_t matName[DXUT::MAX_MATERIAL_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(matName, mh.Name);
|
2019-01-18 23:41:08 +03:00
|
|
|
|
2021-09-01 04:33:23 +03:00
|
|
|
wchar_t albedoTexture[DXUT::MAX_TEXTURE_NAME] = {};
|
|
|
|
ASCIIToWChar(albedoTexture, mh.AlbedoTexture);
|
2019-01-18 23:41:08 +03:00
|
|
|
|
|
|
|
wchar_t normalName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(normalName, mh.NormalTexture);
|
2019-01-18 23:41:08 +03:00
|
|
|
|
|
|
|
wchar_t rmaName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(rmaName, mh.RMATexture);
|
2019-01-18 23:41:08 +03:00
|
|
|
|
|
|
|
wchar_t emissiveName[DXUT::MAX_TEXTURE_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(emissiveName, mh.EmissiveTexture);
|
2019-01-18 23:41:08 +03:00
|
|
|
|
|
|
|
m = {};
|
|
|
|
m.name = matName;
|
|
|
|
m.perVertexColor = false;
|
2021-10-18 10:35:57 +03:00
|
|
|
m.enableSkinning = (flags & SKINNING) != 0;
|
2019-01-18 23:41:08 +03:00
|
|
|
m.enableDualTexture = false;
|
|
|
|
m.enableNormalMaps = true;
|
|
|
|
m.biasedVertexNormals = (flags & BIASED_VERTEX_NORMALS) != 0;
|
2019-05-25 02:36:18 +03:00
|
|
|
m.alphaValue = (mh.Alpha == 0.f) ? 1.f : mh.Alpha;
|
2019-01-18 23:41:08 +03:00
|
|
|
|
2021-09-01 04:33:23 +03:00
|
|
|
m.diffuseTextureIndex = GetUniqueTextureIndex(albedoTexture, textureDictionary);
|
2019-01-18 23:41:08 +03:00
|
|
|
m.specularTextureIndex = GetUniqueTextureIndex(rmaName, textureDictionary);
|
|
|
|
m.normalTextureIndex = GetUniqueTextureIndex(normalName, textureDictionary);
|
|
|
|
m.emissiveTextureIndex = GetUniqueTextureIndex(emissiveName, textureDictionary);
|
|
|
|
|
|
|
|
m.samplerIndex = m.samplerIndex2 = static_cast<int>(CommonStates::SamplerIndex::AnisotropicWrap);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Direct3D 9 Vertex Declaration to Direct3D 12 Input Layout mapping
|
|
|
|
|
2018-07-27 08:11:16 +03:00
|
|
|
static_assert(D3D12_IA_VERTEX_INPUT_STRUCTURE_ELEMENT_COUNT >= 32, "SDKMESH supports decls up to 32 entries");
|
|
|
|
|
2016-10-06 00:35:54 +03:00
|
|
|
unsigned int GetInputLayoutDesc(
|
|
|
|
_In_reads_(32) const DXUT::D3DVERTEXELEMENT9 decl[],
|
2021-09-26 23:14:55 +03:00
|
|
|
ModelMeshPart::InputLayoutCollection& inputDesc)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
static const D3D12_INPUT_ELEMENT_DESC s_elements[] =
|
2018-03-16 22:03:02 +03:00
|
|
|
{
|
|
|
|
{ "SV_Position", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "COLOR", 0, DXGI_FORMAT_B8G8R8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "TANGENT", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "BINORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "BLENDINDICES", 0, DXGI_FORMAT_R8G8B8A8_UINT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
|
|
|
{ "BLENDWEIGHT", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
|
2016-08-22 21:17:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
using namespace DXUT;
|
|
|
|
|
|
|
|
uint32_t offset = 0;
|
|
|
|
uint32_t texcoords = 0;
|
2016-10-06 00:35:54 +03:00
|
|
|
unsigned int flags = 0;
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
bool posfound = false;
|
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
for (size_t index = 0; index < DXUT::MAX_VERTEX_ELEMENTS; ++index)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
if (decl[index].Usage == 0xFF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (decl[index].Type == D3DDECLTYPE_UNUSED)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (decl[index].Offset != offset)
|
|
|
|
break;
|
|
|
|
|
2016-09-29 05:46:31 +03:00
|
|
|
if (decl[index].Usage == D3DDECLUSAGE_POSITION)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
if (decl[index].Type == D3DDECLTYPE_FLOAT3)
|
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
inputDesc.push_back(s_elements[0]);
|
2016-08-22 21:17:57 +03:00
|
|
|
offset += 12;
|
2016-09-29 05:46:31 +03:00
|
|
|
posfound = true;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_NORMAL
|
|
|
|
|| decl[index].Usage == D3DDECLUSAGE_TANGENT
|
|
|
|
|| decl[index].Usage == D3DDECLUSAGE_BINORMAL)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
size_t base = 1;
|
|
|
|
if (decl[index].Usage == D3DDECLUSAGE_TANGENT)
|
|
|
|
base = 3;
|
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_BINORMAL)
|
|
|
|
base = 4;
|
|
|
|
|
|
|
|
D3D12_INPUT_ELEMENT_DESC desc = s_elements[base];
|
|
|
|
|
|
|
|
bool unk = false;
|
|
|
|
switch (decl[index].Type)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
case D3DDECLTYPE_FLOAT3: assert(desc.Format == DXGI_FORMAT_R32G32B32_FLOAT); offset += 12; break;
|
|
|
|
case D3DDECLTYPE_UBYTE4N: desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; flags |= BIASED_VERTEX_NORMALS; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_SHORT4N: desc.Format = DXGI_FORMAT_R16G16B16A16_SNORM; offset += 8; break;
|
|
|
|
case D3DDECLTYPE_FLOAT16_4: desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; offset += 8; break;
|
|
|
|
case D3DDECLTYPE_DXGI_R10G10B10A2_UNORM: desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM; flags |= BIASED_VERTEX_NORMALS; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_DXGI_R11G11B10_FLOAT: desc.Format = DXGI_FORMAT_R11G11B10_FLOAT; flags |= BIASED_VERTEX_NORMALS; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_DXGI_R8G8B8A8_SNORM: desc.Format = DXGI_FORMAT_R8G8B8A8_SNORM; offset += 4; break;
|
|
|
|
|
2020-08-13 03:28:10 +03:00
|
|
|
#if (defined(_XBOX_ONE) && defined(_TITLE)) || defined(_GAMING_XBOX)
|
2018-10-30 08:14:30 +03:00
|
|
|
case D3DDECLTYPE_DEC3N: desc.Format = DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM; offset += 4; break;
|
2018-03-16 22:03:02 +03:00
|
|
|
case (32 + DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM): desc.Format = DXGI_FORMAT_R10G10B10_SNORM_A2_UNORM; offset += 4; break;
|
2018-10-30 00:49:04 +03:00
|
|
|
#else
|
2018-10-30 08:14:30 +03:00
|
|
|
case D3DDECLTYPE_DEC3N: desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM; flags |= USES_OBSOLETE_DEC3N; offset += 4; break;
|
2018-03-16 22:03:02 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
unk = true;
|
|
|
|
break;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
|
|
|
|
if (unk)
|
2016-08-22 21:17:57 +03:00
|
|
|
break;
|
2016-09-29 05:46:31 +03:00
|
|
|
|
|
|
|
inputDesc.push_back(desc);
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_COLOR)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
D3D12_INPUT_ELEMENT_DESC desc = s_elements[2];
|
|
|
|
|
|
|
|
bool unk = false;
|
|
|
|
switch (decl[index].Type)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
case D3DDECLTYPE_FLOAT4: desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; offset += 16; break;
|
|
|
|
case D3DDECLTYPE_D3DCOLOR: assert(desc.Format == DXGI_FORMAT_B8G8R8A8_UNORM); offset += 4; break;
|
|
|
|
case D3DDECLTYPE_UBYTE4N: desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_FLOAT16_4: desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; offset += 8; break;
|
|
|
|
case D3DDECLTYPE_DXGI_R10G10B10A2_UNORM: desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_DXGI_R11G11B10_FLOAT: desc.Format = DXGI_FORMAT_R11G11B10_FLOAT; offset += 4; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unk = true;
|
|
|
|
break;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
|
|
|
|
if (unk)
|
2016-08-22 21:17:57 +03:00
|
|
|
break;
|
2016-09-29 05:46:31 +03:00
|
|
|
|
2016-10-06 00:35:54 +03:00
|
|
|
flags |= PER_VERTEX_COLOR;
|
2016-09-29 05:46:31 +03:00
|
|
|
|
|
|
|
inputDesc.push_back(desc);
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_TEXCOORD)
|
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
D3D12_INPUT_ELEMENT_DESC desc = s_elements[5];
|
2016-08-22 21:17:57 +03:00
|
|
|
desc.SemanticIndex = decl[index].UsageIndex;
|
|
|
|
|
|
|
|
bool unk = false;
|
|
|
|
switch (decl[index].Type)
|
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
case D3DDECLTYPE_FLOAT1: desc.Format = DXGI_FORMAT_R32_FLOAT; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_FLOAT2: assert(desc.Format == DXGI_FORMAT_R32G32_FLOAT); offset += 8; break;
|
|
|
|
case D3DDECLTYPE_FLOAT3: desc.Format = DXGI_FORMAT_R32G32B32_FLOAT; offset += 12; break;
|
|
|
|
case D3DDECLTYPE_FLOAT4: desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT; offset += 16; break;
|
|
|
|
case D3DDECLTYPE_FLOAT16_2: desc.Format = DXGI_FORMAT_R16G16_FLOAT; offset += 4; break;
|
|
|
|
case D3DDECLTYPE_FLOAT16_4: desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT; offset += 8; break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
unk = true;
|
|
|
|
break;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unk)
|
|
|
|
break;
|
|
|
|
|
|
|
|
++texcoords;
|
|
|
|
|
|
|
|
inputDesc.push_back(desc);
|
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_BLENDINDICES)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
if (decl[index].Type == D3DDECLTYPE_UBYTE4)
|
|
|
|
{
|
2016-10-06 00:35:54 +03:00
|
|
|
flags |= SKINNING;
|
2016-09-29 05:46:31 +03:00
|
|
|
inputDesc.push_back(s_elements[6]);
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
2016-09-29 05:46:31 +03:00
|
|
|
else if (decl[index].Usage == D3DDECLUSAGE_BLENDWEIGHT)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2016-09-29 05:46:31 +03:00
|
|
|
if (decl[index].Type == D3DDECLTYPE_UBYTE4N)
|
|
|
|
{
|
2016-10-06 00:35:54 +03:00
|
|
|
flags |= SKINNING;
|
2016-09-29 05:46:31 +03:00
|
|
|
inputDesc.push_back(s_elements[7]);
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!posfound)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("SV_Position is required");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
if (texcoords == 2)
|
|
|
|
{
|
2016-10-06 00:35:54 +03:00
|
|
|
flags |= DUAL_TEXTURE;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
2016-10-06 00:35:54 +03:00
|
|
|
|
|
|
|
return flags;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//======================================================================================
|
|
|
|
// Model Loader
|
|
|
|
//======================================================================================
|
|
|
|
|
|
|
|
_Use_decl_annotations_
|
2020-02-19 08:22:47 +03:00
|
|
|
std::unique_ptr<Model> DirectX::Model::CreateFromSDKMESH(
|
|
|
|
ID3D12Device* device,
|
|
|
|
const uint8_t* meshData,
|
|
|
|
size_t idataSize,
|
|
|
|
ModelLoaderFlags flags)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!meshData)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::invalid_argument("meshData cannot be null");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2019-05-25 02:36:18 +03:00
|
|
|
uint64_t dataSize = idataSize;
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
// File Headers
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < sizeof(DXUT::SDKMESH_HEADER))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
auto header = reinterpret_cast<const DXUT::SDKMESH_HEADER*>(meshData);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
size_t headerSize = sizeof(DXUT::SDKMESH_HEADER)
|
|
|
|
+ header->NumVertexBuffers * sizeof(DXUT::SDKMESH_VERTEX_BUFFER_HEADER)
|
|
|
|
+ header->NumIndexBuffers * sizeof(DXUT::SDKMESH_INDEX_BUFFER_HEADER);
|
|
|
|
if (header->HeaderSize != headerSize)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Not a valid SDKMESH file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < header->HeaderSize)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2019-01-18 23:41:08 +03:00
|
|
|
if (header->Version != DXUT::SDKMESH_FILE_VERSION && header->Version != DXUT::SDKMESH_FILE_VERSION_V2)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Not a supported SDKMESH version");
|
2018-03-16 22:03:02 +03:00
|
|
|
|
|
|
|
if (header->IsBigEndian)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Loading BigEndian SDKMESH files not supported");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!header->NumMeshes)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("No meshes found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!header->NumVertexBuffers)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("No vertex buffers found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!header->NumIndexBuffers)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("No index buffers found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!header->NumTotalSubsets)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("No subsets found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!header->NumMaterials)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("No materials found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// Sub-headers
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < header->VertexStreamHeadersOffset
|
2019-05-25 02:36:18 +03:00
|
|
|
|| (dataSize < (header->VertexStreamHeadersOffset + uint64_t(header->NumVertexBuffers) * sizeof(DXUT::SDKMESH_VERTEX_BUFFER_HEADER))))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
auto vbArray = reinterpret_cast<const DXUT::SDKMESH_VERTEX_BUFFER_HEADER*>(meshData + header->VertexStreamHeadersOffset);
|
|
|
|
|
|
|
|
if (dataSize < header->IndexStreamHeadersOffset
|
2019-05-25 02:36:18 +03:00
|
|
|
|| (dataSize < (header->IndexStreamHeadersOffset + uint64_t(header->NumIndexBuffers) * sizeof(DXUT::SDKMESH_INDEX_BUFFER_HEADER))))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
auto ibArray = reinterpret_cast<const DXUT::SDKMESH_INDEX_BUFFER_HEADER*>(meshData + header->IndexStreamHeadersOffset);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < header->MeshDataOffset
|
2019-05-25 02:36:18 +03:00
|
|
|
|| (dataSize < (header->MeshDataOffset + uint64_t(header->NumMeshes) * sizeof(DXUT::SDKMESH_MESH))))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
auto meshArray = reinterpret_cast<const DXUT::SDKMESH_MESH*>(meshData + header->MeshDataOffset);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < header->SubsetDataOffset
|
2019-05-25 02:36:18 +03:00
|
|
|
|| (dataSize < (header->SubsetDataOffset + uint64_t(header->NumTotalSubsets) * sizeof(DXUT::SDKMESH_SUBSET))))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
auto subsetArray = reinterpret_cast<const DXUT::SDKMESH_SUBSET*>(meshData + header->SubsetDataOffset);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
const DXUT::SDKMESH_FRAME* frameArray = nullptr;
|
|
|
|
if (header->NumFrames > 0)
|
|
|
|
{
|
|
|
|
if (dataSize < header->FrameDataOffset
|
|
|
|
|| (dataSize < (header->FrameDataOffset + uint64_t(header->NumFrames) * sizeof(DXUT::SDKMESH_FRAME))))
|
|
|
|
throw std::runtime_error("End of file");
|
|
|
|
|
|
|
|
if (flags & ModelLoader_IncludeBones)
|
|
|
|
{
|
|
|
|
frameArray = reinterpret_cast<const DXUT::SDKMESH_FRAME*>(meshData + header->FrameDataOffset);
|
|
|
|
}
|
|
|
|
}
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < header->MaterialDataOffset
|
2019-05-25 02:36:18 +03:00
|
|
|
|| (dataSize < (header->MaterialDataOffset + uint64_t(header->NumMaterials) * sizeof(DXUT::SDKMESH_MATERIAL))))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2019-01-18 23:41:08 +03:00
|
|
|
|
|
|
|
const DXUT::SDKMESH_MATERIAL* materialArray = nullptr;
|
|
|
|
const DXUT::SDKMESH_MATERIAL_V2* materialArray_v2 = nullptr;
|
|
|
|
if (header->Version == DXUT::SDKMESH_FILE_VERSION_V2)
|
|
|
|
{
|
|
|
|
materialArray_v2 = reinterpret_cast<const DXUT::SDKMESH_MATERIAL_V2*>(meshData + header->MaterialDataOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
materialArray = reinterpret_cast<const DXUT::SDKMESH_MATERIAL*>(meshData + header->MaterialDataOffset);
|
|
|
|
}
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// Buffer data
|
|
|
|
uint64_t bufferDataOffset = header->HeaderSize + header->NonBufferDataSize;
|
2018-03-16 22:03:02 +03:00
|
|
|
if ((dataSize < bufferDataOffset)
|
|
|
|
|| (dataSize < bufferDataOffset + header->BufferDataSize))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2018-03-16 22:03:02 +03:00
|
|
|
const uint8_t* bufferData = meshData + bufferDataOffset;
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// Create vertex buffers
|
2021-09-26 23:14:55 +03:00
|
|
|
std::vector<std::shared_ptr<ModelMeshPart::InputLayoutCollection>> vbDecls;
|
2018-03-16 22:03:02 +03:00
|
|
|
vbDecls.resize(header->NumVertexBuffers);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2016-10-06 00:35:54 +03:00
|
|
|
std::vector<unsigned int> materialFlags;
|
|
|
|
materialFlags.resize(header->NumVertexBuffers);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-10-30 00:49:04 +03:00
|
|
|
bool dec3nwarning = false;
|
2021-09-29 06:40:20 +03:00
|
|
|
for (size_t j = 0; j < header->NumVertexBuffers; ++j)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
auto& vh = vbArray[j];
|
|
|
|
|
2020-02-19 08:22:47 +03:00
|
|
|
if (vh.SizeBytes > UINT32_MAX)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("VB too large");
|
2020-02-19 08:22:47 +03:00
|
|
|
|
|
|
|
if (!(flags & ModelLoader_AllowLargeModels))
|
|
|
|
{
|
|
|
|
if (vh.SizeBytes > (D3D12_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_A_TERM * 1024u * 1024u))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("VB too large for DirectX 12");
|
2020-02-19 08:22:47 +03:00
|
|
|
}
|
2018-07-27 08:11:16 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < vh.DataOffset
|
|
|
|
|| (dataSize < vh.DataOffset + vh.SizeBytes))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2021-09-26 23:14:55 +03:00
|
|
|
vbDecls[j] = std::make_shared<ModelMeshPart::InputLayoutCollection>();
|
2020-02-19 08:22:47 +03:00
|
|
|
unsigned int ilflags = GetInputLayoutDesc(vh.Decl, *vbDecls[j].get());
|
2016-10-06 00:35:54 +03:00
|
|
|
|
2021-10-07 23:04:12 +03:00
|
|
|
if (flags & ModelLoader_DisableSkinning)
|
|
|
|
{
|
|
|
|
ilflags &= ~static_cast<unsigned int>(SKINNING);
|
|
|
|
}
|
|
|
|
|
2020-02-19 08:22:47 +03:00
|
|
|
if (ilflags & SKINNING)
|
2016-10-06 00:35:54 +03:00
|
|
|
{
|
2021-10-07 12:16:45 +03:00
|
|
|
ilflags &= ~static_cast<unsigned int>(DUAL_TEXTURE);
|
2016-10-06 00:35:54 +03:00
|
|
|
}
|
2020-02-19 08:22:47 +03:00
|
|
|
if (ilflags & USES_OBSOLETE_DEC3N)
|
2018-10-30 00:49:04 +03:00
|
|
|
{
|
|
|
|
dec3nwarning = true;
|
|
|
|
}
|
|
|
|
|
2020-02-19 08:22:47 +03:00
|
|
|
materialFlags[j] = ilflags;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:49:04 +03:00
|
|
|
if (dec3nwarning)
|
|
|
|
{
|
|
|
|
DebugTrace("WARNING: Vertex declaration uses legacy Direct3D 9 D3DDECLTYPE_DEC3N which has no DXGI equivalent\n"
|
|
|
|
" (treating as DXGI_FORMAT_R10G10B10A2_UNORM which is not a signed format)\n");
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
// Validate index buffers
|
2021-09-29 06:40:20 +03:00
|
|
|
for (size_t j = 0; j < header->NumIndexBuffers; ++j)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
auto& ih = ibArray[j];
|
|
|
|
|
2020-02-19 08:22:47 +03:00
|
|
|
if (ih.SizeBytes > UINT32_MAX)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("IB too large");
|
2020-02-19 08:22:47 +03:00
|
|
|
|
|
|
|
if (!(flags & ModelLoader_AllowLargeModels))
|
|
|
|
{
|
|
|
|
if (ih.SizeBytes > (D3D12_REQ_RESOURCE_SIZE_IN_MEGABYTES_EXPRESSION_A_TERM * 1024u * 1024u))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("IB too large for DirectX 12");
|
2020-02-19 08:22:47 +03:00
|
|
|
}
|
2018-07-27 08:11:16 +03:00
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
if (dataSize < ih.DataOffset
|
|
|
|
|| (dataSize < ih.DataOffset + ih.SizeBytes))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
if (ih.IndexType != DXUT::IT_16BIT && ih.IndexType != DXUT::IT_32BIT)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Invalid index buffer type found");
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create meshes
|
|
|
|
std::vector<ModelMaterialInfo> materials;
|
2018-03-16 22:03:02 +03:00
|
|
|
materials.resize(header->NumMaterials);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
std::map<std::wstring, int> textureDictionary;
|
|
|
|
|
2020-01-22 11:55:12 +03:00
|
|
|
auto model = std::make_unique<Model>();
|
2018-03-16 22:03:02 +03:00
|
|
|
model->meshes.reserve(header->NumMeshes);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
uint32_t partCount = 0;
|
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
for (size_t meshIndex = 0; meshIndex < header->NumMeshes; ++meshIndex)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
auto& mh = meshArray[meshIndex];
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (!mh.NumSubsets
|
|
|
|
|| !mh.NumVertexBuffers
|
|
|
|
|| mh.IndexBuffer >= header->NumIndexBuffers
|
|
|
|
|| mh.VertexBuffers[0] >= header->NumVertexBuffers)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::out_of_range("Invalid mesh found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// mh.NumVertexBuffers is sometimes not what you'd expect, so we skip validating it
|
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < mh.SubsetOffset
|
2021-09-29 06:40:20 +03:00
|
|
|
|| (dataSize < mh.SubsetOffset + uint64_t(mh.NumSubsets) * sizeof(uint32_t)))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
auto subsets = reinterpret_cast<const uint32_t*>(meshData + mh.SubsetOffset);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
const uint32_t* influences = nullptr;
|
2018-03-16 22:03:02 +03:00
|
|
|
if (mh.NumFrameInfluences > 0)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
if (dataSize < mh.FrameInfluenceOffset
|
2021-09-29 06:40:20 +03:00
|
|
|
|| (dataSize < mh.FrameInfluenceOffset + uint64_t(mh.NumFrameInfluences) * sizeof(uint32_t)))
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("End of file");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
if (flags & ModelLoader_IncludeBones)
|
|
|
|
{
|
|
|
|
influences = reinterpret_cast<const uint32_t*>(meshData + mh.FrameInfluenceOffset);
|
|
|
|
}
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto mesh = std::make_shared<ModelMesh>();
|
2018-10-25 03:42:54 +03:00
|
|
|
wchar_t meshName[DXUT::MAX_MESH_NAME] = {};
|
2021-01-09 00:45:24 +03:00
|
|
|
ASCIIToWChar(meshName, mh.Name);
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
mesh->name = meshName;
|
|
|
|
|
|
|
|
// Extents
|
|
|
|
mesh->boundingBox.Center = mh.BoundingBoxCenter;
|
|
|
|
mesh->boundingBox.Extents = mh.BoundingBoxExtents;
|
2018-03-16 22:03:02 +03:00
|
|
|
BoundingSphere::CreateFromBoundingBox(mesh->boundingSphere, mesh->boundingBox);
|
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
if (influences)
|
|
|
|
{
|
|
|
|
mesh->boneInfluences.resize(mh.NumFrameInfluences);
|
|
|
|
memcpy(mesh->boneInfluences.data(), influences, sizeof(uint32_t) * mh.NumFrameInfluences);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
// Create subsets
|
2021-09-29 06:40:20 +03:00
|
|
|
for (size_t j = 0; j < mh.NumSubsets; ++j)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
auto sIndex = subsets[j];
|
|
|
|
if (sIndex >= header->NumTotalSubsets)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::out_of_range("Invalid mesh found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
auto& subset = subsetArray[sIndex];
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
D3D_PRIMITIVE_TOPOLOGY primType;
|
2018-03-16 22:03:02 +03:00
|
|
|
switch (subset.PrimitiveType)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2018-03-16 22:03:02 +03:00
|
|
|
case DXUT::PT_TRIANGLE_LIST: primType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST; break;
|
|
|
|
case DXUT::PT_TRIANGLE_STRIP: primType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP; break;
|
|
|
|
case DXUT::PT_LINE_LIST: primType = D3D_PRIMITIVE_TOPOLOGY_LINELIST; break;
|
|
|
|
case DXUT::PT_LINE_STRIP: primType = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP; break;
|
|
|
|
case DXUT::PT_POINT_LIST: primType = D3D_PRIMITIVE_TOPOLOGY_POINTLIST; break;
|
|
|
|
case DXUT::PT_TRIANGLE_LIST_ADJ: primType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST_ADJ; break;
|
|
|
|
case DXUT::PT_TRIANGLE_STRIP_ADJ: primType = D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP_ADJ; break;
|
|
|
|
case DXUT::PT_LINE_LIST_ADJ: primType = D3D_PRIMITIVE_TOPOLOGY_LINELIST_ADJ; break;
|
|
|
|
case DXUT::PT_LINE_STRIP_ADJ: primType = D3D_PRIMITIVE_TOPOLOGY_LINESTRIP_ADJ; break;
|
|
|
|
|
|
|
|
case DXUT::PT_QUAD_PATCH_LIST:
|
|
|
|
case DXUT::PT_TRIANGLE_PATCH_LIST:
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Direct3D9 era tessellation not supported");
|
2018-03-16 22:03:02 +03:00
|
|
|
|
|
|
|
default:
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("Unknown primitive type");
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
if (subset.MaterialID >= header->NumMaterials)
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::out_of_range("Invalid mesh found");
|
2016-08-22 21:17:57 +03:00
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
auto& mat = materials[subset.MaterialID];
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
const size_t vi = mh.VertexBuffers[0];
|
2019-01-18 23:41:08 +03:00
|
|
|
if (materialArray_v2)
|
|
|
|
{
|
|
|
|
InitMaterial(
|
|
|
|
materialArray_v2[subset.MaterialID],
|
|
|
|
materialFlags[vi],
|
|
|
|
mat,
|
|
|
|
textureDictionary);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InitMaterial(
|
|
|
|
materialArray[subset.MaterialID],
|
|
|
|
materialFlags[vi],
|
|
|
|
mat,
|
2020-02-19 08:22:47 +03:00
|
|
|
textureDictionary,
|
|
|
|
(flags & ModelLoader_MaterialColorsSRGB) != 0);
|
2019-01-18 23:41:08 +03:00
|
|
|
}
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
auto part = new ModelMeshPart(partCount++);
|
|
|
|
|
|
|
|
const auto& vh = vbArray[mh.VertexBuffers[0]];
|
|
|
|
const auto& ih = ibArray[mh.IndexBuffer];
|
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
part->indexCount = static_cast<uint32_t>(subset.IndexCount);
|
|
|
|
part->startIndex = static_cast<uint32_t>(subset.IndexStart);
|
2019-05-25 02:36:18 +03:00
|
|
|
part->vertexOffset = static_cast<int32_t>(subset.VertexStart);
|
2018-03-16 22:03:02 +03:00
|
|
|
part->vertexStride = static_cast<uint32_t>(vh.StrideBytes);
|
|
|
|
part->vertexCount = static_cast<uint32_t>(subset.VertexCount);
|
2016-09-27 02:53:46 +03:00
|
|
|
part->primitiveType = primType;
|
2018-03-16 22:03:02 +03:00
|
|
|
part->indexFormat = (ibArray[mh.IndexBuffer].IndexType == DXUT::IT_32BIT) ? DXGI_FORMAT_R32_UINT : DXGI_FORMAT_R16_UINT;
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// Vertex data
|
2018-04-03 10:53:52 +03:00
|
|
|
auto verts = bufferData + (vh.DataOffset - bufferDataOffset);
|
2018-04-21 09:32:50 +03:00
|
|
|
auto vbytes = static_cast<size_t>(vh.SizeBytes);
|
2018-06-23 01:17:08 +03:00
|
|
|
part->vertexBufferSize = static_cast<uint32_t>(vh.SizeBytes);
|
2018-04-21 09:32:50 +03:00
|
|
|
part->vertexBuffer = GraphicsMemory::Get(device).Allocate(vbytes);
|
|
|
|
memcpy(part->vertexBuffer.Memory(), verts, vbytes);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
// Index data
|
2018-04-03 10:53:52 +03:00
|
|
|
auto indices = bufferData + (ih.DataOffset - bufferDataOffset);
|
2018-04-21 09:32:50 +03:00
|
|
|
auto ibytes = static_cast<size_t>(ih.SizeBytes);
|
2018-06-23 01:17:08 +03:00
|
|
|
part->indexBufferSize = static_cast<uint32_t>(ih.SizeBytes);
|
2018-04-21 09:32:50 +03:00
|
|
|
part->indexBuffer = GraphicsMemory::Get(device).Allocate(ibytes);
|
|
|
|
memcpy(part->indexBuffer.Memory(), indices, ibytes);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
part->materialIndex = subset.MaterialID;
|
2018-03-16 22:03:02 +03:00
|
|
|
part->vbDecl = vbDecls[mh.VertexBuffers[0]];
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
if (mat.alphaValue < 1.0f)
|
2018-03-16 22:03:02 +03:00
|
|
|
mesh->alphaMeshParts.emplace_back(part);
|
2016-08-22 21:17:57 +03:00
|
|
|
else
|
2018-03-16 22:03:02 +03:00
|
|
|
mesh->opaqueMeshParts.emplace_back(part);
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2018-03-16 22:03:02 +03:00
|
|
|
model->meshes.emplace_back(mesh);
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the materials and texture names into contiguous arrays
|
|
|
|
model->materials = std::move(materials);
|
|
|
|
model->textureNames.resize(textureDictionary.size());
|
|
|
|
for (auto texture = std::cbegin(textureDictionary); texture != std::cend(textureDictionary); ++texture)
|
|
|
|
{
|
2019-05-25 02:36:18 +03:00
|
|
|
model->textureNames[static_cast<size_t>(texture->second)] = texture->first;
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2021-09-29 06:40:20 +03:00
|
|
|
// Load model bones (if present and requested)
|
|
|
|
if (frameArray)
|
|
|
|
{
|
|
|
|
static_assert(DXUT::INVALID_FRAME == ModelBone::c_Invalid, "ModelBone invalid type mismatch");
|
|
|
|
|
|
|
|
ModelBone::Collection bones;
|
|
|
|
bones.reserve(header->NumFrames);
|
|
|
|
auto transforms = ModelBone::MakeArray(header->NumFrames);
|
|
|
|
|
|
|
|
for (uint32_t j = 0; j < header->NumFrames; ++j)
|
|
|
|
{
|
|
|
|
ModelBone bone(
|
|
|
|
frameArray[j].ParentFrame,
|
|
|
|
frameArray[j].ChildFrame,
|
|
|
|
frameArray[j].SiblingFrame);
|
|
|
|
|
|
|
|
wchar_t boneName[DXUT::MAX_FRAME_NAME] = {};
|
|
|
|
ASCIIToWChar(boneName, frameArray[j].Name);
|
|
|
|
bone.name = boneName;
|
|
|
|
bones.emplace_back(bone);
|
|
|
|
|
|
|
|
transforms[j] = XMLoadFloat4x4(&frameArray[j].Matrix);
|
|
|
|
|
|
|
|
uint32_t index = frameArray[j].Mesh;
|
|
|
|
if (index != DXUT::INVALID_MESH)
|
|
|
|
{
|
|
|
|
if (index >= model->meshes.size())
|
|
|
|
{
|
|
|
|
throw std::out_of_range("Invalid mesh index found in frame data");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model->meshes[index]->boneIndex == ModelBone::c_Invalid)
|
|
|
|
{
|
|
|
|
// Bind the first bone that links to a given mesh
|
|
|
|
model->meshes[index]->boneIndex = j;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::swap(model->bones, bones);
|
|
|
|
|
|
|
|
// Compute inverse bind pose matrices for the model
|
|
|
|
auto bindPose = ModelBone::MakeArray(header->NumFrames);
|
|
|
|
model->CopyAbsoluteBoneTransforms(header->NumFrames, transforms.get(), bindPose.get());
|
|
|
|
|
|
|
|
auto invBoneTransforms = ModelBone::MakeArray(header->NumFrames);
|
|
|
|
for (size_t j = 0; j < header->NumFrames; ++j)
|
|
|
|
{
|
|
|
|
invBoneTransforms[j] = XMMatrixInverse(nullptr, bindPose[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::swap(model->boneMatrices, transforms);
|
|
|
|
std::swap(model->invBindPoseMatrices, invBoneTransforms);
|
|
|
|
}
|
|
|
|
|
2016-08-22 21:17:57 +03:00
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
_Use_decl_annotations_
|
2020-02-19 08:22:47 +03:00
|
|
|
std::unique_ptr<Model> DirectX::Model::CreateFromSDKMESH(
|
|
|
|
ID3D12Device* device,
|
|
|
|
const wchar_t* szFileName,
|
|
|
|
ModelLoaderFlags flags)
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
|
|
|
size_t dataSize = 0;
|
|
|
|
std::unique_ptr<uint8_t[]> data;
|
2018-03-16 22:03:02 +03:00
|
|
|
HRESULT hr = BinaryReader::ReadEntireFile(szFileName, data, &dataSize);
|
|
|
|
if (FAILED(hr))
|
2016-08-22 21:17:57 +03:00
|
|
|
{
|
2020-01-20 02:24:50 +03:00
|
|
|
DebugTrace("ERROR: CreateFromSDKMESH failed (%08X) loading '%ls'\n",
|
|
|
|
static_cast<unsigned int>(hr), szFileName);
|
2021-01-06 00:31:14 +03:00
|
|
|
throw std::runtime_error("CreateFromSDKMESH");
|
2016-08-22 21:17:57 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 08:22:47 +03:00
|
|
|
auto model = CreateFromSDKMESH(device, data.get(), dataSize, flags);
|
2016-08-22 21:17:57 +03:00
|
|
|
|
|
|
|
model->name = szFileName;
|
|
|
|
|
|
|
|
return model;
|
|
|
|
}
|