diff --git a/enginecustom/Color.cpp b/enginecustom/Color.cpp new file mode 100644 index 0000000..0a1c2f3 --- /dev/null +++ b/enginecustom/Color.cpp @@ -0,0 +1,18 @@ +#include "pch.h" +#include "Color.h" +#if __has_include("Color.g.cpp") +#include "Color.g.cpp" +#endif + +namespace winrt::enginecustom::implementation +{ + int32_t Color::MyProperty() + { + throw hresult_not_implemented(); + } + + void Color::MyProperty(int32_t /*value*/) + { + throw hresult_not_implemented(); + } +} diff --git a/enginecustom/Color.h b/enginecustom/Color.h new file mode 100644 index 0000000..23ade1d --- /dev/null +++ b/enginecustom/Color.h @@ -0,0 +1,21 @@ +#pragma once + +#include "Color.g.h" + +namespace winrt::enginecustom::implementation +{ + struct Color : ColorT<Color> + { + Color() = default; + + int32_t MyProperty(); + void MyProperty(int32_t value); + }; +} + +namespace winrt::enginecustom::factory_implementation +{ + struct Color : ColorT<Color, implementation::Color> + { + }; +} diff --git a/enginecustom/Color.idl b/enginecustom/Color.idl new file mode 100644 index 0000000..cbbecc5 --- /dev/null +++ b/enginecustom/Color.idl @@ -0,0 +1,10 @@ +namespace enginecustom +{ + [bindable] + [default_interface] + runtimeclass Color + { + Color(); + Int32 MyProperty; + } +} diff --git a/enginecustom/Color.ps.hlsl b/enginecustom/Color.ps.hlsl new file mode 100644 index 0000000..f682d07 --- /dev/null +++ b/enginecustom/Color.ps.hlsl @@ -0,0 +1,22 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: color.ps +//////////////////////////////////////////////////////////////////////////////// + + +////////////// +// TYPEDEFS // +////////////// +struct PixelInputType +{ + float4 position : SV_POSITION; + float4 color : COLOR; +}; + + +//////////////////////////////////////////////////////////////////////////////// +// Pixel Shader +//////////////////////////////////////////////////////////////////////////////// +float4 ColorPixelShader(PixelInputType input) : SV_TARGET +{ + return input.color; +} diff --git a/enginecustom/Color.vs.hlsl b/enginecustom/Color.vs.hlsl new file mode 100644 index 0000000..a29c1a2 --- /dev/null +++ b/enginecustom/Color.vs.hlsl @@ -0,0 +1,50 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: color.vs +//////////////////////////////////////////////////////////////////////////////// + +///////////// +// GLOBALS // +///////////// +cbuffer MatrixBuffer +{ + matrix worldMatrix; + matrix viewMatrix; + matrix projectionMatrix; +}; + +////////////// +// TYPEDEFS // +////////////// +struct VertexInputType +{ + float4 position : POSITION; + float4 color : COLOR; +}; + +struct PixelInputType +{ + float4 position : SV_POSITION; + float4 color : COLOR; +}; + +//////////////////////////////////////////////////////////////////////////////// +// Vertex Shader +//////////////////////////////////////////////////////////////////////////////// +PixelInputType ColorVertexShader(VertexInputType input) +{ + PixelInputType output; + + + // Change the position vector to be 4 units for proper matrix calculations. + input.position.w = 1.0f; + + // Calculate the position of the vertex against the world, view, and projection matrices. + output.position = mul(input.position, worldMatrix); + output.position = mul(output.position, viewMatrix); + output.position = mul(output.position, projectionMatrix); + + // Store the input color for the pixel shader to use. + output.color = input.color; + + return output; +} \ No newline at end of file diff --git a/enginecustom/Colorshaderclass.cpp b/enginecustom/Colorshaderclass.cpp new file mode 100644 index 0000000..43bc0f3 --- /dev/null +++ b/enginecustom/Colorshaderclass.cpp @@ -0,0 +1,203 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: colorshaderclass.cpp +//////////////////////////////////////////////////////////////////////////////// +#include "colorshaderclass.h" + +ColorShaderClass::ColorShaderClass() +{ + m_vertexShader = 0; + m_pixelShader = 0; + m_layout = 0; + m_matrixBuffer = 0; +} + + +ColorShaderClass::ColorShaderClass(const ColorShaderClass& other) +{ +} + + +ColorShaderClass::~ColorShaderClass() +{ +} + +bool ColorShaderClass::Initialize(ID3D11Device* device, HWND hwnd) +{ + bool result; + wchar_t vsFilename[128]; + wchar_t psFilename[128]; + int error; + + + // Set the filename of the vertex shader. + error = wcscpy_s(vsFilename, 128, L"../Engine/color.vs"); + if (error != 0) + { + return false; + } + + // Set the filename of the pixel shader. + error = wcscpy_s(psFilename, 128, L"../Engine/color.ps"); + if (error != 0) + { + return false; + } + + // Initialize the vertex and pixel shaders. + result = InitializeShader(device, hwnd, vsFilename, psFilename); + if (!result) + { + return false; + } + + return true; +} + +void ColorShaderClass::Shutdown() +{ + // Shutdown the vertex and pixel shaders as well as the related objects. + ShutdownShader(); + + return; +} + +bool ColorShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, + XMMATRIX projectionMatrix) +{ + bool result; + + + // Set the shader parameters that it will use for rendering. + result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix); + if (!result) + { + return false; + } + + // Now render the prepared buffers with the shader. + RenderShader(deviceContext, indexCount); + + return true; +} + +bool ColorShaderClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename) +{ + HRESULT result; + ID3D10Blob* errorMessage; + ID3D10Blob* vertexShaderBuffer; + ID3D10Blob* pixelShaderBuffer; + D3D11_INPUT_ELEMENT_DESC polygonLayout[2]; + unsigned int numElements; + D3D11_BUFFER_DESC matrixBufferDesc; + + + // Initialize the pointers this function will use to null. + errorMessage = 0; + vertexShaderBuffer = 0; + pixelShaderBuffer = 0; + + // Compile the vertex shader code. + result = D3DCompileFromFile(vsFilename, NULL, NULL, "ColorVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, + &vertexShaderBuffer, &errorMessage); + if (FAILED(result)) + { + // If the shader failed to compile it should have writen something to the error message. + if (errorMessage) + { + OutputShaderErrorMessage(errorMessage, hwnd, vsFilename); + } + // If there was nothing in the error message then it simply could not find the shader file itself. + else + { + MessageBox(hwnd, vsFilename, L"Missing Shader File", MB_OK); + } + + return false; + } + + // Compile the pixel shader code. + result = D3DCompileFromFile(psFilename, NULL, NULL, "ColorPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, + &pixelShaderBuffer, &errorMessage); + if (FAILED(result)) + { + // If the shader failed to compile it should have writen something to the error message. + if (errorMessage) + { + OutputShaderErrorMessage(errorMessage, hwnd, psFilename); + } + // If there was nothing in the error message then it simply could not find the file itself. + else + { + MessageBox(hwnd, psFilename, L"Missing Shader File", MB_OK); + } + + return false; + } + + // Create the vertex shader from the buffer. + result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader); + if (FAILED(result)) + { + return false; + } + + // Create the pixel shader from the buffer. + result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader); + if (FAILED(result)) + { + return false; + } + + // Create the vertex input layout description. + // This setup needs to match the VertexType stucture in the ModelClass and in the shader. + polygonLayout[0].SemanticName = "POSITION"; + polygonLayout[0].SemanticIndex = 0; + polygonLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT; + polygonLayout[0].InputSlot = 0; + polygonLayout[0].AlignedByteOffset = 0; + polygonLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + polygonLayout[0].InstanceDataStepRate = 0; + + polygonLayout[1].SemanticName = "COLOR"; + polygonLayout[1].SemanticIndex = 0; + polygonLayout[1].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; + polygonLayout[1].InputSlot = 0; + polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; + polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; + polygonLayout[1].InstanceDataStepRate = 0; + + // Get a count of the elements in the layout. + numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]); + + // Create the vertex input layout. + result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), + vertexShaderBuffer->GetBufferSize(), &m_layout); + if (FAILED(result)) + { + return false; + } + + // Release the vertex shader buffer and pixel shader buffer since they are no longer needed. + vertexShaderBuffer->Release(); + vertexShaderBuffer = 0; + + pixelShaderBuffer->Release(); + pixelShaderBuffer = 0; + + // Setup the description of the dynamic matrix constant buffer that is in the vertex shader. + matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; + matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType); + matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + matrixBufferDesc.MiscFlags = 0; + matrixBufferDesc.StructureByteStride = 0; + + // Create the constant buffer pointer so we can access the vertex shader constant buffer from within this class. + result = device->CreateBuffer(&matrixBufferDesc, NULL, &m_matrixBuffer); + if (FAILED(result)) + { + return false; + } + + return true; +} \ No newline at end of file diff --git a/enginecustom/Colorshaderclass.h b/enginecustom/Colorshaderclass.h new file mode 100644 index 0000000..694dc8c --- /dev/null +++ b/enginecustom/Colorshaderclass.h @@ -0,0 +1,56 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: colorshaderclass.h +//////////////////////////////////////////////////////////////////////////////// +#ifndef _COLORSHADERCLASS_H_ +#define _COLORSHADERCLASS_H_ + + +////////////// +// INCLUDES // +////////////// +#include <d3d11.h> +#include <d3dcompiler.h> +#include <directxmath.h> +#include <fstream> +using namespace DirectX; +using namespace std; + + +//////////////////////////////////////////////////////////////////////////////// +// Class name: ColorShaderClass +//////////////////////////////////////////////////////////////////////////////// +class ColorShaderClass +{ +private: + struct MatrixBufferType + { + XMMATRIX world; + XMMATRIX view; + XMMATRIX projection; + }; + +public: + ColorShaderClass(); + ColorShaderClass(const ColorShaderClass&); + ~ColorShaderClass(); + + bool Initialize(ID3D11Device*, HWND); + void Shutdown(); + bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX); + +private: + bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*); + void ShutdownShader(); + void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*); + + bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX); + void RenderShader(ID3D11DeviceContext*, int); + +private: + ID3D11VertexShader* m_vertexShader; + ID3D11PixelShader* m_pixelShader; + ID3D11InputLayout* m_layout; + ID3D11Buffer* m_matrixBuffer; +}; + +#endif \ No newline at end of file diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 1b3d9ff..1751adc 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -67,7 +67,7 @@ bool ApplicationClass::Frame() bool ApplicationClass::Render() { // Clear the buffers to begin the scene. - m_Direct3D->BeginScene(0.5f, 0.5f, 0.5f, 1.0f); + m_Direct3D->BeginScene(255.0f, 255.0f, 0.0f, 1.0f); // Present the rendered scene to the screen. diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index 71dcc8c..22bd778 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -1,5 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props')" /> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> @@ -20,6 +21,7 @@ </ItemGroup> <ItemGroup> <ClCompile Include="applicationclass.cpp" /> + <ClCompile Include="Colorshaderclass.cpp" /> <ClCompile Include="d3dclass.cpp" /> <ClCompile Include="inputclass.cpp" /> <ClCompile Include="Main.cpp" /> @@ -27,10 +29,28 @@ </ItemGroup> <ItemGroup> <ClInclude Include="applicationclass.h" /> + <ClInclude Include="Colorshaderclass.h" /> <ClInclude Include="d3dclass.h" /> <ClInclude Include="inputclass.h" /> <ClInclude Include="systemclass.h" /> </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <FxCompile Include="Color.ps.hlsl"> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Pixel</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Pixel</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Pixel</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Pixel</ShaderType> + </FxCompile> + <FxCompile Include="Color.vs.hlsl"> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Vertex</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Vertex</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Vertex</ShaderType> + <ShaderType Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Vertex</ShaderType> + </FxCompile> + </ItemGroup> <PropertyGroup Label="Globals"> <VCProjectVersion>17.0</VCProjectVersion> <Keyword>Win32Proj</Keyword> @@ -141,5 +161,13 @@ </ItemDefinitionGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> + <Import Project="..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets" Condition="Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets')" /> </ImportGroup> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>Ce projet fait référence à des packages NuGet qui sont manquants sur cet ordinateur. Utilisez l'option de restauration des packages NuGet pour les télécharger. Pour plus d'informations, consultez http://go.microsoft.com/fwlink/?LinkID=322105. Le fichier manquant est : {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.props'))" /> + <Error Condition="!Exists('..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Windows.CppWinRT.2.0.220531.1\build\native\Microsoft.Windows.CppWinRT.targets'))" /> + </Target> </Project> \ No newline at end of file diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index 6bef417..64ca351 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -13,6 +13,9 @@ <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> </Filter> + <Filter Include="shader"> + <UniqueIdentifier>{b016e481-576e-4d99-bdde-34cc10c55b1d}</UniqueIdentifier> + </Filter> </ItemGroup> <ItemGroup> <ClCompile Include="Main.cpp"> @@ -30,6 +33,9 @@ <ClCompile Include="d3dclass.cpp"> <Filter>Fichiers sources</Filter> </ClCompile> + <ClCompile Include="Colorshaderclass.cpp"> + <Filter>Fichiers sources</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="systemclass.h"> @@ -44,5 +50,19 @@ <ClInclude Include="d3dclass.h"> <Filter>Fichiers d%27en-tête</Filter> </ClInclude> + <ClInclude Include="Colorshaderclass.h"> + <Filter>Fichiers d%27en-tête</Filter> + </ClInclude> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <FxCompile Include="Color.ps.hlsl"> + <Filter>shader</Filter> + </FxCompile> + <FxCompile Include="Color.vs.hlsl"> + <Filter>shader</Filter> + </FxCompile> </ItemGroup> </Project> \ No newline at end of file diff --git a/enginecustom/packages.config b/enginecustom/packages.config new file mode 100644 index 0000000..cbf6205 --- /dev/null +++ b/enginecustom/packages.config @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Microsoft.Windows.CppWinRT" version="2.0.220531.1" targetFramework="native" /> +</packages> \ No newline at end of file