UVAtlas/UVAtlasTool/Mesh.h

179 строки
6.8 KiB
C
Исходник Постоянная ссылка Обычный вид История

2016-08-22 21:35:32 +03:00
//--------------------------------------------------------------------------------------
// File: Mesh.h
//
// Mesh processing helper class
//
2021-02-27 10:03:04 +03:00
// Copyright (c) Microsoft Corporation.
2018-02-24 10:08:45 +03:00
// Licensed under the MIT License.
2016-08-22 21:35:32 +03:00
//
// http://go.microsoft.com/fwlink/?LinkID=324981
2018-07-20 23:25:35 +03:00
// http://go.microsoft.com/fwlink/?LinkID=512686
2016-08-22 21:35:32 +03:00
//--------------------------------------------------------------------------------------
#include <Windows.h>
2016-08-22 21:35:32 +03:00
#include <cstddef>
#include <cstdint>
2016-08-22 21:35:32 +03:00
#include <memory>
#include <ostream>
2016-08-22 21:35:32 +03:00
#include <string>
#include <d3d11_1.h>
#include <DirectXMath.h>
2016-08-22 21:35:32 +03:00
#include "DirectXMesh.h"
2016-08-22 21:35:32 +03:00
class Mesh
{
public:
Mesh() noexcept : mnFaces(0), mnVerts(0) {}
Mesh(Mesh&& moveFrom) noexcept;
Mesh& operator= (Mesh&& moveFrom) noexcept;
2016-08-22 21:35:32 +03:00
Mesh(Mesh const&) = delete;
Mesh& operator= (Mesh const&) = delete;
// Methods
void Clear() noexcept;
2016-08-22 21:35:32 +03:00
void SetMTLFileName(const std::wstring& name) noexcept { mtlFileName = name; }
2021-03-05 06:00:29 +03:00
HRESULT SetIndexData(_In_ size_t nFaces, _In_reads_(nFaces * 3) const uint16_t* indices, _In_reads_opt_(nFaces) const uint32_t* attributes = nullptr) noexcept;
HRESULT SetIndexData(_In_ size_t nFaces, _In_reads_(nFaces * 3) const uint32_t* indices, _In_reads_opt_(nFaces) const uint32_t* attributes = nullptr) noexcept;
2016-08-22 21:35:32 +03:00
2022-03-13 04:27:59 +03:00
HRESULT SetVertexData(const DirectX::VBReader& reader, _In_ size_t nVerts) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT Validate(_In_ DirectX::VALIDATE_FLAGS flags, _In_opt_ std::wstring* msgs) const noexcept;
2016-08-22 21:35:32 +03:00
HRESULT Clean(std::vector<uint32_t>& dups, _In_ bool breakBowties = false) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT GenerateAdjacency(_In_ float epsilon) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT ComputeNormals(_In_ DirectX::CNORM_FLAGS flags) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT ComputeTangentFrame(_In_ bool bitangents) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT UpdateFaces(_In_ size_t nFaces, _In_reads_(nFaces * 3) const uint32_t* indices) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT UpdateAttributes(_In_ size_t nFaces, _In_reads_(nFaces * 3) const uint32_t* attributes) noexcept;
2016-08-22 21:35:32 +03:00
2021-06-09 23:13:01 +03:00
HRESULT UpdateUVs(_In_ size_t nVerts, _In_reads_(nVerts) const DirectX::XMFLOAT2* uvs, bool keepOriginal) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT VertexRemap(_In_reads_(nNewVerts) const uint32_t* remap, _In_ size_t nNewVerts) noexcept;
2016-08-22 21:35:32 +03:00
HRESULT ReverseWinding() noexcept;
2016-08-22 21:35:32 +03:00
HRESULT InvertUTexCoord() noexcept;
HRESULT InvertVTexCoord() noexcept;
2016-08-22 21:35:32 +03:00
HRESULT ReverseHandedness() noexcept;
2016-08-22 21:35:32 +03:00
HRESULT VisualizeUVs(bool useSecondUVs, bool vizNormals) noexcept;
2016-08-22 21:35:32 +03:00
// Accessors
const uint32_t* GetAttributeBuffer() const noexcept { return mAttributes.get(); }
const uint32_t* GetAdjacencyBuffer() const noexcept { return mAdjacency.get(); }
const DirectX::XMFLOAT3* GetPositionBuffer() const noexcept { return mPositions.get(); }
const DirectX::XMFLOAT3* GetNormalBuffer() const noexcept { return mNormals.get(); }
const DirectX::XMFLOAT2* GetTexCoordBuffer() const noexcept { return mTexCoords.get(); }
const DirectX::XMFLOAT4* GetTangentBuffer() const noexcept { return mTangents.get(); }
const DirectX::XMFLOAT4* GetColorBuffer() const noexcept { return mColors.get(); }
2016-08-22 21:35:32 +03:00
size_t GetFaceCount() const noexcept { return mnFaces; }
size_t GetVertexCount() const noexcept { return mnVerts; }
2016-08-22 21:35:32 +03:00
bool Is16BitIndexBuffer() const noexcept;
2016-08-22 21:35:32 +03:00
const uint32_t* GetIndexBuffer() const noexcept { return mIndices.get(); }
std::unique_ptr<uint16_t[]> GetIndexBuffer16() const noexcept;
2016-08-22 21:35:32 +03:00
2022-03-13 04:27:59 +03:00
HRESULT GetVertexBuffer(const DirectX::VBWriter& writer) const noexcept;
2016-08-22 21:35:32 +03:00
// Save mesh to file
struct Material
{
std::wstring name;
bool perVertexColor;
float specularPower;
float alpha;
DirectX::XMFLOAT3 ambientColor;
DirectX::XMFLOAT3 diffuseColor;
DirectX::XMFLOAT3 specularColor;
DirectX::XMFLOAT3 emissiveColor;
std::wstring texture;
std::wstring normalTexture;
std::wstring specularTexture;
std::wstring emissiveTexture;
std::wstring rmaTexture;
2018-08-17 05:47:25 +03:00
Material() noexcept :
perVertexColor(false),
specularPower(1.f),
alpha(1.f),
ambientColor{},
diffuseColor{},
specularColor{},
emissiveColor{}
{
}
Material(
const wchar_t* iname,
bool pvc,
float power,
float ialpha,
const DirectX::XMFLOAT3& ambient,
const DirectX::XMFLOAT3 diffuse,
const DirectX::XMFLOAT3& specular,
const DirectX::XMFLOAT3& emissive,
const wchar_t* txtname) :
name(iname),
perVertexColor(pvc),
specularPower(power),
alpha(ialpha),
ambientColor(ambient),
diffuseColor(diffuse),
specularColor(specular),
emissiveColor(emissive),
texture(txtname)
{
}
2016-08-22 21:35:32 +03:00
};
HRESULT ExportToOBJ(const wchar_t* szFileName, _In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials) const;
HRESULT ExportToVBO(_In_z_ const wchar_t* szFileName) const noexcept;
HRESULT ExportToCMO(_In_z_ const wchar_t* szFileName, _In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials) const noexcept;
HRESULT ExportToSDKMESH(_In_z_ const wchar_t* szFileName,
_In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials,
bool force32bit = false,
bool version2 = false,
DXGI_FORMAT normalFormat = DXGI_FORMAT_R32G32B32_FLOAT,
DXGI_FORMAT uvFormat = DXGI_FORMAT_R32G32_FLOAT,
DXGI_FORMAT colorFormat = DXGI_FORMAT_B8G8R8A8_UNORM) const noexcept;
2016-08-22 21:35:32 +03:00
// Create mesh from file
static HRESULT CreateFromVBO(_In_z_ const wchar_t* szFileName, _Inout_ std::unique_ptr<Mesh>& result) noexcept;
2016-08-22 21:35:32 +03:00
private:
size_t mnFaces;
size_t mnVerts;
std::unique_ptr<uint32_t[]> mIndices;
std::unique_ptr<uint32_t[]> mAttributes;
std::unique_ptr<uint32_t[]> mAdjacency;
std::unique_ptr<DirectX::XMFLOAT3[]> mPositions;
std::unique_ptr<DirectX::XMFLOAT3[]> mNormals;
std::unique_ptr<DirectX::XMFLOAT4[]> mTangents;
std::unique_ptr<DirectX::XMFLOAT3[]> mBiTangents;
std::unique_ptr<DirectX::XMFLOAT2[]> mTexCoords;
2021-06-09 23:13:01 +03:00
std::unique_ptr<DirectX::XMFLOAT2[]> mTexCoords2;
2016-08-22 21:35:32 +03:00
std::unique_ptr<DirectX::XMFLOAT4[]> mColors;
std::unique_ptr<DirectX::XMFLOAT4[]> mBlendIndices;
std::unique_ptr<DirectX::XMFLOAT4[]> mBlendWeights;
std::wstring mtlFileName;
void ExportToOBJ(std::wostream& os, _In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials) const;
};