diff --git a/DirectXTK_Desktop_2010.vcxproj b/DirectXTK_Desktop_2010.vcxproj index 8c60434f6..ffb79fd36 100644 --- a/DirectXTK_Desktop_2010.vcxproj +++ b/DirectXTK_Desktop_2010.vcxproj @@ -23,6 +23,7 @@ + @@ -48,8 +49,12 @@ + + + + Create Create diff --git a/DirectXTK_Desktop_2010.vcxproj.filters b/DirectXTK_Desktop_2010.vcxproj.filters index cc3ad0b2d..dc54c2489 100644 --- a/DirectXTK_Desktop_2010.vcxproj.filters +++ b/DirectXTK_Desktop_2010.vcxproj.filters @@ -75,6 +75,9 @@ Inc + + Inc + @@ -107,6 +110,9 @@ Src + + Src + Src @@ -128,6 +134,15 @@ Src + + Src + + + Src + + + Src + diff --git a/DirectXTK_Desktop_2012.vcxproj b/DirectXTK_Desktop_2012.vcxproj index a36cbc6d1..5b538753a 100644 --- a/DirectXTK_Desktop_2012.vcxproj +++ b/DirectXTK_Desktop_2012.vcxproj @@ -23,6 +23,7 @@ + @@ -48,8 +49,12 @@ + + + + Create Create diff --git a/DirectXTK_Desktop_2012.vcxproj.filters b/DirectXTK_Desktop_2012.vcxproj.filters index 4fb5ea43b..f924225f1 100644 --- a/DirectXTK_Desktop_2012.vcxproj.filters +++ b/DirectXTK_Desktop_2012.vcxproj.filters @@ -75,6 +75,9 @@ Src + + Inc + @@ -107,6 +110,9 @@ Src + + Src + Src @@ -128,6 +134,15 @@ Src + + Src + + + Src + + + Src + diff --git a/DirectXTK_Windows8.vcxproj b/DirectXTK_Windows8.vcxproj index 973e34b04..1a983120a 100644 --- a/DirectXTK_Windows8.vcxproj +++ b/DirectXTK_Windows8.vcxproj @@ -31,6 +31,7 @@ + @@ -55,9 +56,13 @@ + + + + Create Create diff --git a/DirectXTK_Windows8.vcxproj.filters b/DirectXTK_Windows8.vcxproj.filters index 8934a478a..f924225f1 100644 --- a/DirectXTK_Windows8.vcxproj.filters +++ b/DirectXTK_Windows8.vcxproj.filters @@ -39,9 +39,6 @@ Src - - Src - Src @@ -75,6 +72,12 @@ Inc + + Src + + + Inc + @@ -107,6 +110,9 @@ Src + + Src + Src @@ -128,6 +134,15 @@ Src + + Src + + + Src + + + Src + diff --git a/DirectXTK_WindowsPhone8.vcxproj b/DirectXTK_WindowsPhone8.vcxproj index 44d9b1582..e8710cb18 100644 --- a/DirectXTK_WindowsPhone8.vcxproj +++ b/DirectXTK_WindowsPhone8.vcxproj @@ -23,6 +23,7 @@ + @@ -46,9 +47,13 @@ + + + + Create diff --git a/DirectXTK_WindowsPhone8.vcxproj.filters b/DirectXTK_WindowsPhone8.vcxproj.filters index 0e3435ae5..0ffaacfc5 100644 --- a/DirectXTK_WindowsPhone8.vcxproj.filters +++ b/DirectXTK_WindowsPhone8.vcxproj.filters @@ -39,9 +39,6 @@ Src - - Src - Src @@ -72,6 +69,12 @@ Inc + + Src + + + Inc + @@ -104,6 +107,9 @@ Src + + Src + Src @@ -122,6 +128,15 @@ Src + + Src + + + Src + + + Src + diff --git a/Inc/Effects.h b/Inc/Effects.h index f92b8b15e..6c84b5040 100644 --- a/Inc/Effects.h +++ b/Inc/Effects.h @@ -380,6 +380,64 @@ namespace DirectX SkinnedEffect(SkinnedEffect const&); SkinnedEffect& operator= (SkinnedEffect const&); }; + + + + //---------------------------------------------------------------------------------- + // Abstract interface to factory for sharing effects and texture resources + class IEffectFactory + { + public: + virtual ~IEffectFactory() {} + + struct EffectInfo + { + const WCHAR* name; + bool perVertexColor; + float specularPower; + float alpha; + DirectX::XMFLOAT3 ambientColor; + DirectX::XMFLOAT3 diffuseColor; + DirectX::XMFLOAT3 specularColor; + DirectX::XMFLOAT3 emissiveColor; + const WCHAR* texture; + + EffectInfo() { memset( this, 0, sizeof(EffectInfo) ); }; + }; + + virtual std::shared_ptr CreateEffect( _In_ const EffectInfo& info, _In_opt_ ID3D11DeviceContext* deviceContext ) = 0; + + virtual void CreateTexture( _In_z_ const WCHAR* name, _In_opt_ ID3D11DeviceContext* deviceContext, _Outptr_ ID3D11ShaderResourceView** textureView ) = 0; + }; + + + // Factory for sharing effects and texture resources + class EffectFactory : public IEffectFactory + { + public: + explicit EffectFactory(_In_ ID3D11Device* device); + EffectFactory(EffectFactory&& moveFrom); + EffectFactory& operator= (EffectFactory&& moveFrom); + virtual ~EffectFactory(); + + virtual std::shared_ptr CreateEffect( _In_ const EffectInfo& info, _In_opt_ ID3D11DeviceContext* deviceContext ) override; + + virtual void CreateTexture( _In_z_ const WCHAR* name, _In_opt_ ID3D11DeviceContext* deviceContext, _Outptr_ ID3D11ShaderResourceView** textureView ) override; + + void ReleaseCache(); + + void SetSharing( bool enabled ); + + private: + // Private implementation. + class Impl; + + std::shared_ptr pImpl; + + // Prevent copying. + EffectFactory(EffectFactory const&); + EffectFactory& operator= (EffectFactory const&); + }; } #pragma warning(pop) diff --git a/Inc/Model.h b/Inc/Model.h new file mode 100644 index 000000000..047e41890 --- /dev/null +++ b/Inc/Model.h @@ -0,0 +1,130 @@ +//-------------------------------------------------------------------------------------- +// File: Model.h +// +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved. +// +// http://go.microsoft.com/fwlink/?LinkId=248929 +//-------------------------------------------------------------------------------------- + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#pragma warning(push) +#pragma warning(disable : 4005) +#include +#include +#pragma warning(pop) + + +namespace DirectX +{ + class IEffect; + class IEffectFactory; + class CommonStates; + class ModelMesh; + + // Each mesh part is a submesh with a single effect + class ModelMeshPart + { + public: + ModelMeshPart(); + + uint32_t indexCount; + uint32_t startIndex; + uint32_t vertexOffset; + uint32_t vertexStride; + D3D_PRIMITIVE_TOPOLOGY primitiveType; + DXGI_FORMAT indexFormat; + Microsoft::WRL::ComPtr inputLayout; + Microsoft::WRL::ComPtr indexBuffer; + Microsoft::WRL::ComPtr vertexBuffer; + std::shared_ptr effect; + std::shared_ptr> vbDecl; + bool isAlpha; + + typedef std::vector> Collection; + + // Draw mesh part with custom effect + void Draw( _In_ ID3D11DeviceContext* deviceContext, _In_ IEffect* effect, _In_ ID3D11InputLayout* inputLayout, + _In_opt_ std::function setCustomState = nullptr ) const; + + // Create input layout for drawing with a custom effect. + void CreateInputLayout( _In_ ID3D11Device* d3dDevice, _In_ IEffect* effect, _Outptr_ ID3D11InputLayout** inputLayout ); + + // Change effect used by part and regenerate input layout (be sure to call Model::Modified as well) + void ModifyEffect( _In_ ID3D11Device* d3dDevice, _In_ std::shared_ptr& effect, bool isalpha = false ); + }; + + + // A mesh consists of one or more model parts + class ModelMesh + { + public: + ModelMesh(); + + BoundingSphere boundingSphere; + BoundingBox boundingBox; + ModelMeshPart::Collection meshParts; + std::wstring name; + bool ccw; + bool pmalpha; + + typedef std::vector> Collection; + + // Setup states for drawing mesh + void PrepareForRendering( _In_ ID3D11DeviceContext* deviceContext, CommonStates& states, bool alpha = false, bool wireframe = false ) const; + + // Draw the mesh + void Draw( _In_ ID3D11DeviceContext* deviceContext, CXMMATRIX world, CXMMATRIX view, CXMMATRIX projection, + bool alpha = false, _In_opt_ std::function setCustomState = nullptr ) const; + }; + + + // A model consists of one or more meshes + class Model + { + public: + ModelMesh::Collection meshes; + std::wstring name; + + // Draw all the meshes in the model + void Draw( _In_ ID3D11DeviceContext* deviceContext, CommonStates& states, CXMMATRIX world, CXMMATRIX view, CXMMATRIX projection, + bool wireframe = false, _In_opt_ std::function setCustomState = nullptr ) const; + + // Notify model that effects, parts list, or mesh list has changed + void Modified() { mEffectCache.clear(); } + + // Update all effects used by the model + void UpdateEffects( _In_ std::function setEffect ); + + // Loads a model from a Visual Studio Starter Kit .CMO file + static std::unique_ptr CreateFromCMO( _In_ ID3D11Device* d3dDevice, _In_reads_bytes_(dataSize) const uint8_t* meshData, size_t dataSize, + _In_ IEffectFactory& fxFactory, bool ccw = true, bool pmalpha = false ); + static std::unique_ptr CreateFromCMO( _In_ ID3D11Device* d3dDevice, _In_z_ const wchar_t* szFileName, + _In_ IEffectFactory& fxFactory, bool ccw = true, bool pmalpha = false ); + + // Loads a model from a DirectX SDK .SDKMESH file + static std::unique_ptr CreateFromSDKMESH( _In_ ID3D11Device* d3dDevice, _In_reads_bytes_(dataSize) const uint8_t* meshData, _In_ size_t dataSize, + _In_ IEffectFactory& fxFactory, bool ccw = false, bool pmalpha = false ); + static std::unique_ptr CreateFromSDKMESH( _In_ ID3D11Device* d3dDevice, _In_z_ const wchar_t* szFileName, + _In_ IEffectFactory& fxFactory, bool ccw = false, bool pmalpha = false ); + + private: + std::set mEffectCache; + }; + } \ No newline at end of file