This commit is contained in:
Chuck Walbourn 2018-07-20 13:25:35 -07:00
Родитель db622e2860
Коммит 8af57ff167
8 изменённых файлов: 170 добавлений и 120 удалений

Просмотреть файл

@ -7,12 +7,22 @@
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
// http://go.microsoft.com/fwlink/?LinkID=512686
//--------------------------------------------------------------------------------------
#pragma warning(push)
#pragma warning(disable : 4005)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#define NODRAWTEXT
#define NOGDI
#define NOBITMAP
#define NOMCX
#define NOSERVICE
#define NOHELP
#pragma warning(pop)
#include "mesh.h"
#include "Mesh.h"
#include <DirectXPackedVector.h>
#include <DirectXCollision.h>

Просмотреть файл

@ -7,6 +7,7 @@
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
// http://go.microsoft.com/fwlink/?LinkID=512686
//--------------------------------------------------------------------------------------
#include <windows.h>

136
UVAtlasTool/MeshOBJ.cpp Normal file
Просмотреть файл

@ -0,0 +1,136 @@
//--------------------------------------------------------------------------------------
// File: MeshOBJ.cpp
//
// Helper code for loading Mesh data from Wavefront OBJ
//
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
//
// http://go.microsoft.com/fwlink/?LinkID=324981
// http://go.microsoft.com/fwlink/?LinkID=512686
//--------------------------------------------------------------------------------------
#pragma warning(push)
#pragma warning(disable : 4005)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#define NODRAWTEXT
#define NOGDI
#define NOBITMAP
#define NOMCX
#define NOSERVICE
#define NOHELP
#pragma warning(pop)
#include "Mesh.h"
#include "WaveFrontReader.h"
using namespace DirectX;
HRESULT LoadFromOBJ(
const wchar_t* szFilename,
std::unique_ptr<Mesh>& inMesh,
std::vector<Mesh::Material>& inMaterial,
bool ccw,
bool dds)
{
WaveFrontReader<uint32_t> wfReader;
HRESULT hr = wfReader.Load(szFilename, ccw);
if (FAILED(hr))
return hr;
inMesh.reset(new (std::nothrow) Mesh);
if (!inMesh)
return E_OUTOFMEMORY;
if (wfReader.indices.empty() || wfReader.vertices.empty())
return E_FAIL;
hr = inMesh->SetIndexData(wfReader.indices.size() / 3, wfReader.indices.data(),
wfReader.attributes.empty() ? nullptr : wfReader.attributes.data());
if (FAILED(hr))
return hr;
static const D3D11_INPUT_ELEMENT_DESC s_vboLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
static const D3D11_INPUT_ELEMENT_DESC s_vboLayoutAlt[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
const D3D11_INPUT_ELEMENT_DESC* layout = s_vboLayout;
size_t nDecl = _countof(s_vboLayout);
if (!wfReader.hasNormals && !wfReader.hasTexcoords)
{
nDecl = 1;
}
else if (wfReader.hasNormals && !wfReader.hasTexcoords)
{
nDecl = 2;
}
else if (!wfReader.hasNormals && wfReader.hasTexcoords)
{
layout = s_vboLayoutAlt;
nDecl = _countof(s_vboLayoutAlt);
}
VBReader vbr;
hr = vbr.Initialize(layout, nDecl);
if (FAILED(hr))
return hr;
hr = vbr.AddStream(wfReader.vertices.data(), wfReader.vertices.size(), 0, sizeof(WaveFrontReader<uint32_t>::Vertex));
if (FAILED(hr))
return hr;
hr = inMesh->SetVertexData(vbr, wfReader.vertices.size());
if (FAILED(hr))
return hr;
if (!wfReader.materials.empty())
{
inMaterial.clear();
inMaterial.reserve(wfReader.materials.size());
for (auto it = wfReader.materials.cbegin(); it != wfReader.materials.cend(); ++it)
{
Mesh::Material mtl = {};
mtl.name = it->strName;
mtl.specularPower = (it->bSpecular) ? float(it->nShininess) : 1.f;
mtl.alpha = it->fAlpha;
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);
wchar_t texture[_MAX_PATH] = {};
if (*it->strTexture)
{
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.texture = texture;
inMaterial.push_back(mtl);
}
}
return S_OK;
}

Просмотреть файл

@ -36,7 +36,6 @@
#include "directxtex.h"
#include "Mesh.h"
#include "WaveFrontReader.h"
//Uncomment to add support for OpenEXR (.exr)
//#define USE_OPENEXR
@ -351,113 +350,9 @@ namespace
return S_OK;
}
//--------------------------------------------------------------------------------------
HRESULT LoadFromOBJ(const wchar_t* szFilename, std::unique_ptr<Mesh>& inMesh, std::vector<Mesh::Material>& inMaterial, DWORD64 options)
{
WaveFrontReader<uint32_t> wfReader;
HRESULT hr = wfReader.Load(szFilename, (options & (DWORD64(1) << OPT_CLOCKWISE)) ? false : true);
if (FAILED(hr))
return hr;
inMesh.reset(new (std::nothrow) Mesh);
if (!inMesh)
return E_OUTOFMEMORY;
if (wfReader.indices.empty() || wfReader.vertices.empty())
return E_FAIL;
hr = inMesh->SetIndexData(wfReader.indices.size() / 3, wfReader.indices.data(),
wfReader.attributes.empty() ? nullptr : wfReader.attributes.data());
if (FAILED(hr))
return hr;
static const D3D11_INPUT_ELEMENT_DESC s_vboLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
static const D3D11_INPUT_ELEMENT_DESC s_vboLayoutAlt[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
const D3D11_INPUT_ELEMENT_DESC* layout = s_vboLayout;
size_t nDecl = _countof(s_vboLayout);
if (!wfReader.hasNormals && !wfReader.hasTexcoords)
{
nDecl = 1;
}
else if (wfReader.hasNormals && !wfReader.hasTexcoords)
{
nDecl = 2;
}
else if (!wfReader.hasNormals && wfReader.hasTexcoords)
{
layout = s_vboLayoutAlt;
nDecl = _countof(s_vboLayoutAlt);
}
VBReader vbr;
hr = vbr.Initialize(layout, nDecl);
if (FAILED(hr))
return hr;
hr = vbr.AddStream(wfReader.vertices.data(), wfReader.vertices.size(), 0, sizeof(WaveFrontReader<uint32_t>::Vertex));
if (FAILED(hr))
return hr;
hr = inMesh->SetVertexData(vbr, wfReader.vertices.size());
if (FAILED(hr))
return hr;
if (!wfReader.materials.empty())
{
inMaterial.clear();
inMaterial.reserve(wfReader.materials.size());
for (auto it = wfReader.materials.cbegin(); it != wfReader.materials.cend(); ++it)
{
Mesh::Material mtl = {};
mtl.name = it->strName;
mtl.specularPower = (it->bSpecular) ? float(it->nShininess) : 1.f;
mtl.alpha = it->fAlpha;
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);
wchar_t texture[_MAX_PATH] = {};
if (*it->strTexture)
{
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 (!(options & (DWORD64(1) << OPT_NODDS)))
{
wcscpy_s(txext, L".dds");
}
_wmakepath_s(texture, nullptr, nullptr, txfname, txext);
}
mtl.texture = texture;
inMaterial.push_back(mtl);
}
}
return S_OK;
}
}
extern HRESULT LoadFromOBJ(const wchar_t* szFilename, std::unique_ptr<Mesh>& inMesh, std::vector<Mesh::Material>& inMaterial, bool ccw, bool dds);
//--------------------------------------------------------------------------------------
// Entry-point
@ -825,7 +720,9 @@ int __cdecl wmain(_In_ int argc, _In_z_count_(argc) wchar_t* argv[])
}
else
{
hr = LoadFromOBJ(pConv->szSrc, inMesh, inMaterial, dwOptions);
hr = LoadFromOBJ(pConv->szSrc, inMesh, inMaterial,
(dwOptions & (1 << OPT_CLOCKWISE)) ? false : true,
(dwOptions & (1 << OPT_NODDS)) ? false : true);
}
if (FAILED(hr))
{

Просмотреть файл

@ -308,6 +308,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MeshOBJ.cpp" />
<ClCompile Include="UVAtlas.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h" />

Просмотреть файл

@ -5,20 +5,22 @@
<UniqueIdentifier>{8e114980-c1a3-4ada-ad7c-83caadf5daeb}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{2c3d4c8c-5d1a-459a-a05a-a4e4b608a44e}</UniqueIdentifier>
<Extensions>fx;fxh;hlsl</Extensions>
<Filter Include="Wavefront OBJ">
<UniqueIdentifier>{22955106-e9b9-44fe-bb88-dc7647aebf20}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="UVAtlas.cpp" />
<ClCompile Include="Mesh.cpp" />
<CLInclude Include="Mesh.h" />
<ClCompile Include="MeshOBJ.cpp">
<Filter>Wavefront OBJ</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h">
<Filter>Wavefront OBJ</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="UVAtlas.rc">

Просмотреть файл

@ -327,6 +327,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<ClCompile Include="MeshOBJ.cpp" />
<ClCompile Include="UVAtlas.cpp" />
<ClCompile Include="Mesh.cpp" />
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h" />

Просмотреть файл

@ -5,20 +5,22 @@
<UniqueIdentifier>{8e114980-c1a3-4ada-ad7c-83caadf5daeb}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe</Extensions>
</Filter>
<Filter Include="Shaders">
<UniqueIdentifier>{2c3d4c8c-5d1a-459a-a05a-a4e4b608a44e}</UniqueIdentifier>
<Extensions>fx;fxh;hlsl</Extensions>
<Filter Include="Wavefront OBJ">
<UniqueIdentifier>{c8076e0b-beef-46c6-8c4b-128ccc82a987}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="UVAtlas.cpp" />
<ClCompile Include="Mesh.cpp" />
<CLInclude Include="Mesh.h" />
<ClCompile Include="MeshOBJ.cpp">
<Filter>Wavefront OBJ</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\DirectXMesh\Utilities\WaveFrontReader.h">
<Filter>Wavefront OBJ</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="UVAtlas.rc">