Mesh.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //--------------------------------------------------------------------------------------
  2. // File: Mesh.h
  3. //
  4. // Mesh processing helper class
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  9. // PARTICULAR PURPOSE.
  10. //
  11. // Copyright (c) Microsoft Corporation. All rights reserved.
  12. //
  13. // http://go.microsoft.com/fwlink/?LinkID=324981
  14. //--------------------------------------------------------------------------------------
  15. #include <windows.h>
  16. #include <memory>
  17. #include <string>
  18. #include <vector>
  19. #pragma warning(push)
  20. #pragma warning(disable : 4005)
  21. #include <stdint.h>
  22. #pragma warning(pop)
  23. #if defined(_XBOX_ONE) && defined(_TITLE)
  24. #include <d3d11_x.h>
  25. #define DCOMMON_H_INCLUDED
  26. #else
  27. #include <d3d11_1.h>
  28. #endif
  29. #include <directxmath.h>
  30. #include "directxmesh.h"
  31. class Mesh
  32. {
  33. public:
  34. Mesh() : mnFaces(0), mnVerts(0) {}
  35. Mesh(Mesh&& moveFrom);
  36. Mesh& operator= (Mesh&& moveFrom);
  37. // Methods
  38. void Clear();
  39. HRESULT SetIndexData( _In_ size_t nFaces, _In_reads_(nFaces*3) const uint16_t* indices, _In_reads_opt_(nFaces) uint32_t* attributes = nullptr );
  40. HRESULT SetIndexData( _In_ size_t nFaces, _In_reads_(nFaces*3) const uint32_t* indices, _In_reads_opt_(nFaces) uint32_t* attributes = nullptr );
  41. HRESULT SetVertexData( _Inout_ DirectX::VBReader& reader, _In_ size_t nVerts );
  42. HRESULT Validate( _In_ DWORD flags, _In_opt_ std::wstring* msgs ) const;
  43. HRESULT Clean( _In_ bool breakBowties=false );
  44. HRESULT GenerateAdjacency( _In_ float epsilon );
  45. HRESULT ComputeNormals( _In_ DWORD flags );
  46. HRESULT ComputeTangentFrame( _In_ bool bitangents );
  47. HRESULT UpdateFaces( _In_ size_t nFaces, _In_reads_(nFaces*3) const uint32_t* indices );
  48. HRESULT UpdateAttributes( _In_ size_t nFaces, _In_reads_(nFaces*3) const uint32_t* attributes );
  49. HRESULT UpdateUVs( _In_ size_t nVerts, _In_reads_(nVerts) const DirectX::XMFLOAT2* uvs );
  50. HRESULT VertexRemap( _In_reads_(nNewVerts) const uint32_t* remap, _In_ size_t nNewVerts );
  51. HRESULT ReverseWinding( bool texcoords );
  52. HRESULT VisualizeUVs();
  53. // Accessors
  54. const uint32_t* GetAttributeBuffer() const { return mAttributes.get(); }
  55. const uint32_t* GetAdjacencyBuffer() const { return mAdjacency.get(); }
  56. const DirectX::XMFLOAT3* GetPositionBuffer() const { return mPositions.get(); }
  57. const DirectX::XMFLOAT3* GetNormalBuffer() const { return mNormals.get(); }
  58. const DirectX::XMFLOAT2* GetTexCoordBuffer() const { return mTexCoords.get(); }
  59. const DirectX::XMFLOAT4* GetTangentBuffer() const { return mTangents.get(); }
  60. const DirectX::XMFLOAT4* GetColorBuffer() const { return mColors.get(); }
  61. size_t GetFaceCount() const { return mnFaces; }
  62. size_t GetVertexCount() const { return mnVerts; }
  63. bool Is16BitIndexBuffer() const;
  64. const uint32_t* GetIndexBuffer() const { return mIndices.get(); }
  65. std::unique_ptr<uint16_t []> GetIndexBuffer16() const;
  66. HRESULT GetVertexBuffer(_Inout_ DirectX::VBWriter& writer ) const;
  67. // Save mesh to file
  68. struct Material
  69. {
  70. std::wstring name;
  71. bool perVertexColor;
  72. float specularPower;
  73. float alpha;
  74. DirectX::XMFLOAT3 ambientColor;
  75. DirectX::XMFLOAT3 diffuseColor;
  76. DirectX::XMFLOAT3 specularColor;
  77. DirectX::XMFLOAT3 emissiveColor;
  78. std::wstring texture;
  79. };
  80. HRESULT ExportToVBO( _In_z_ const wchar_t* szFileName ) const;
  81. HRESULT ExportToCMO( _In_z_ const wchar_t* szFileName, _In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials ) const;
  82. HRESULT ExportToSDKMESH( _In_z_ const wchar_t* szFileName, _In_ size_t nMaterials, _In_reads_opt_(nMaterials) const Material* materials ) const;
  83. // Create mesh from file
  84. static HRESULT CreateFromVBO( _In_z_ const wchar_t* szFileName, _Inout_ std::unique_ptr<Mesh>& result );
  85. private:
  86. size_t mnFaces;
  87. size_t mnVerts;
  88. std::unique_ptr<uint32_t[]> mIndices;
  89. std::unique_ptr<uint32_t[]> mAttributes;
  90. std::unique_ptr<uint32_t[]> mAdjacency;
  91. std::unique_ptr<DirectX::XMFLOAT3[]> mPositions;
  92. std::unique_ptr<DirectX::XMFLOAT3[]> mNormals;
  93. std::unique_ptr<DirectX::XMFLOAT4[]> mTangents;
  94. std::unique_ptr<DirectX::XMFLOAT3[]> mBiTangents;
  95. std::unique_ptr<DirectX::XMFLOAT2[]> mTexCoords;
  96. std::unique_ptr<DirectX::XMFLOAT4[]> mColors;
  97. std::unique_ptr<DirectX::XMFLOAT4[]> mBlendIndices;
  98. std::unique_ptr<DirectX::XMFLOAT4[]> mBlendWeights;
  99. // Prevent copying
  100. Mesh(Mesh const&) DIRECTX_CTOR_DELETE
  101. Mesh& operator= (Mesh const&) DIRECTX_CTOR_DELETE
  102. };