From 6bd66dc5d4f095d0aad1d2e78df73af2f3b12691 Mon Sep 17 00:00:00 2001 From: axelpicou <115532798+axelpicou@users.noreply.github.com> Date: Wed, 20 Mar 2024 13:03:56 +0100 Subject: [PATCH] debut triangle --- enginecustom/Color.cpp | 18 ++ enginecustom/Color.h | 21 +++ enginecustom/Color.idl | 10 ++ enginecustom/Color.ps.hlsl | 22 +++ enginecustom/Color.vs.hlsl | 50 ++++++ enginecustom/Colorshaderclass.cpp | 203 ++++++++++++++++++++++ enginecustom/Colorshaderclass.h | 56 ++++++ enginecustom/applicationclass.cpp | 2 +- enginecustom/enginecustom.vcxproj | 28 +++ enginecustom/enginecustom.vcxproj.filters | 20 +++ enginecustom/packages.config | 4 + 11 files changed, 433 insertions(+), 1 deletion(-) create mode 100644 enginecustom/Color.cpp create mode 100644 enginecustom/Color.h create mode 100644 enginecustom/Color.idl create mode 100644 enginecustom/Color.ps.hlsl create mode 100644 enginecustom/Color.vs.hlsl create mode 100644 enginecustom/Colorshaderclass.cpp create mode 100644 enginecustom/Colorshaderclass.h create mode 100644 enginecustom/packages.config 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() = default; + + int32_t MyProperty(); + void MyProperty(int32_t value); + }; +} + +namespace winrt::enginecustom::factory_implementation +{ + struct Color : ColorT + { + }; +} 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 +#include +#include +#include +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 @@ + Debug @@ -20,6 +21,7 @@ + @@ -27,10 +29,28 @@ + + + + + + + Pixel + Pixel + Pixel + Pixel + + + Vertex + Vertex + Vertex + Vertex + + 17.0 Win32Proj @@ -141,5 +161,13 @@ + + + + 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}. + + + + \ 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 @@ {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + {b016e481-576e-4d99-bdde-34cc10c55b1d} + @@ -30,6 +33,9 @@ Fichiers sources + + Fichiers sources + @@ -44,5 +50,19 @@ Fichiers d%27en-tête + + Fichiers d%27en-tête + + + + + + + + shader + + + shader + \ 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 @@ + + + + \ No newline at end of file