From 151ea9b1910dbc84615ece7075bab155b9d74a3b Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Tue, 28 Jan 2025 17:42:23 +0100 Subject: [PATCH] Minor update - celshading rework started --- .../.idea/discord.xml | 14 + .../.idea/encodings.xml | 8 + .../.idea/material_theme_project_new.xml | 4 +- .../.idea/projectSettingsUpdater.xml | 3 +- .../.idea/workspace.xml | 100 ++++++- enginecustom/assets/Texture/imgui.ini | 85 +++++- enginecustom/imgui.ini | 20 +- enginecustom/shader-error.txt | Bin 123 -> 148 bytes enginecustom/src/hlsl/celshading.ps | 78 +++-- enginecustom/src/hlsl/celshading.vs | 49 ++- .../src/inc/shader/CelShadingShader.h | 60 ++-- .../src/inc/shader/shadermanagerclass.h | 6 +- .../src/inc/system/applicationclass.h | 4 +- enginecustom/src/inc/system/imguiManager.h | 1 - .../src/src/shader/CelShadingShader.cpp | 282 +++++++++++------- .../src/src/shader/shadermanagerclass.cpp | 6 +- .../src/src/system/applicationclass.cpp | 39 ++- enginecustom/src/src/system/imguiManager.cpp | 16 - 18 files changed, 542 insertions(+), 233 deletions(-) create mode 100644 .idea/.idea.KhaoticEngineReborn/.idea/discord.xml create mode 100644 .idea/.idea.KhaoticEngineReborn/.idea/encodings.xml diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/discord.xml b/.idea/.idea.KhaoticEngineReborn/.idea/discord.xml new file mode 100644 index 0000000..912db82 --- /dev/null +++ b/.idea/.idea.KhaoticEngineReborn/.idea/discord.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/encodings.xml b/.idea/.idea.KhaoticEngineReborn/.idea/encodings.xml new file mode 100644 index 0000000..24f3ba2 --- /dev/null +++ b/.idea/.idea.KhaoticEngineReborn/.idea/encodings.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/material_theme_project_new.xml b/.idea/.idea.KhaoticEngineReborn/.idea/material_theme_project_new.xml index b3ff0c7..0f9005c 100644 --- a/.idea/.idea.KhaoticEngineReborn/.idea/material_theme_project_new.xml +++ b/.idea/.idea.KhaoticEngineReborn/.idea/material_theme_project_new.xml @@ -3,7 +3,9 @@ diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/projectSettingsUpdater.xml b/.idea/.idea.KhaoticEngineReborn/.idea/projectSettingsUpdater.xml index 64af657..4bb9f4d 100644 --- a/.idea/.idea.KhaoticEngineReborn/.idea/projectSettingsUpdater.xml +++ b/.idea/.idea.KhaoticEngineReborn/.idea/projectSettingsUpdater.xml @@ -1,7 +1,6 @@ - \ No newline at end of file diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml b/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml index 0eb482a..d1b362c 100644 --- a/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml +++ b/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml @@ -1,10 +1,77 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "associatedIndex": 6 +} + + + + + - + - + + + + + + 1738075515913 + + + + + + + + \ No newline at end of file diff --git a/enginecustom/assets/Texture/imgui.ini b/enginecustom/assets/Texture/imgui.ini index 244abcd..cfd977a 100644 --- a/enginecustom/assets/Texture/imgui.ini +++ b/enginecustom/assets/Texture/imgui.ini @@ -1,28 +1,89 @@ [Window][Debug##Default] Pos=60,60 Size=400,400 +Collapsed=0 [Window][Khaotic Engine] -Pos=1142,43 -Size=392,273 +Pos=1180,27 +Size=396,826 +Collapsed=0 +DockId=0x00000005,0 [Window][Objects] -Pos=598,29 -Size=457,294 +Pos=8,27 +Size=290,826 +Collapsed=0 +DockId=0x0000000A,0 [Window][Terrain] -Pos=58,62 -Size=342,82 +Pos=8,27 +Size=290,487 +Collapsed=0 +DockId=0x00000009,0 [Window][Light] -Pos=1548,17 -Size=358,535 +Pos=8,27 +Size=290,487 +Collapsed=0 +DockId=0x00000009,1 [Window][Shader Manager] -Pos=471,90 -Size=180,79 +Pos=8,27 +Size=330,487 +Collapsed=0 +DockId=0x00000001,2 [Window][Engine Settings] -Pos=106,212 -Size=407,81 +Pos=1516,27 +Size=396,974 +Collapsed=0 +DockId=0x00000005,1 + +[Window][DockSpace Demo] +Size=1584,861 +Collapsed=0 + +[Window][Render Window] +Pos=300,27 +Size=878,826 +Collapsed=0 +DockId=0x00000002,0 + +[Window][DockSpace] +Pos=0,0 +Size=1584,861 +Collapsed=0 + +[Window][Add Object] +Pos=1188,0 +Size=396,430 +Collapsed=0 + +[Window][Log] +Pos=8,518 +Size=1568,335 +Collapsed=0 +DockId=0x0000000C,0 + +[Window][Log Window] +Pos=8,775 +Size=1904,226 +Collapsed=0 +DockId=0x0000000E,0 + +[Docking][Data] +DockSpace ID=0xC0DFADC4 Pos=8,27 Size=1568,826 Split=X + DockNode ID=0x00000001 Parent=0xC0DFADC4 SizeRef=330,1094 Selected=0x393905AB + DockNode ID=0x00000003 Parent=0xC0DFADC4 SizeRef=1700,1094 CentralNode=1 +DockSpace ID=0xCCBD8CF7 Window=0x3DA2F1DE Pos=8,27 Size=1568,826 Split=Y + DockNode ID=0x0000000D Parent=0xCCBD8CF7 SizeRef=1568,598 Split=Y + DockNode ID=0x0000000B Parent=0x0000000D SizeRef=1568,637 Split=X + DockNode ID=0x00000007 Parent=0x0000000B SizeRef=290,826 Split=Y Selected=0x393905AB + DockNode ID=0x00000009 Parent=0x00000007 SizeRef=395,413 Selected=0x393905AB + DockNode ID=0x0000000A Parent=0x00000007 SizeRef=395,411 Selected=0x031DC75C + DockNode ID=0x00000008 Parent=0x0000000B SizeRef=1276,826 Split=X + DockNode ID=0x00000002 Parent=0x00000008 SizeRef=878,826 CentralNode=1 Selected=0x9204953B + DockNode ID=0x00000005 Parent=0x00000008 SizeRef=396,826 Selected=0x9F035453 + DockNode ID=0x0000000C Parent=0x0000000D SizeRef=1568,335 Selected=0x139FDA3F + DockNode ID=0x0000000E Parent=0xCCBD8CF7 SizeRef=1568,226 Selected=0xAB74BEE9 diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 3903abd..cfd977a 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -10,20 +10,20 @@ Collapsed=0 DockId=0x00000005,0 [Window][Objects] -Pos=8,442 -Size=290,411 +Pos=8,27 +Size=290,826 Collapsed=0 DockId=0x0000000A,0 [Window][Terrain] Pos=8,27 -Size=290,826 +Size=290,487 Collapsed=0 DockId=0x00000009,0 [Window][Light] Pos=8,27 -Size=290,826 +Size=290,487 Collapsed=0 DockId=0x00000009,1 @@ -34,8 +34,8 @@ Collapsed=0 DockId=0x00000001,2 [Window][Engine Settings] -Pos=1180,27 -Size=396,826 +Pos=1516,27 +Size=396,974 Collapsed=0 DockId=0x00000005,1 @@ -44,8 +44,8 @@ Size=1584,861 Collapsed=0 [Window][Render Window] -Pos=8,27 -Size=1170,826 +Pos=300,27 +Size=878,826 Collapsed=0 DockId=0x00000002,0 @@ -66,8 +66,8 @@ Collapsed=0 DockId=0x0000000C,0 [Window][Log Window] -Pos=8,627 -Size=1568,226 +Pos=8,775 +Size=1904,226 Collapsed=0 DockId=0x0000000E,0 diff --git a/enginecustom/shader-error.txt b/enginecustom/shader-error.txt index a0b1877983cb9fd1b130bd2df1de9e22ee7935f3..1cff77c8fedb05aeea8c5a5afed2305fd0d8580a 100644 GIT binary patch delta 117 zcmXBHu?oUK3;7l< zjGxJ&SFAHhIGi|!I>J3C4aSyaVxjg-qnf~fSUq$GH-ucg08;UsZYI%- NS5%`;i(0c@Eq-ujE9(FN delta 92 zcmbQjSnc9$6%$&VT2vg9l~Gz;oSK(f662DeT$-DjS5h34ospPdl9{ZVnwOrLml^}1 rl1qzA@^fQyGSf3k^a_eKjLav-stcrKrlpk@r#k27 0.95f) + { + lightIntensity = 1.0f; + } + else if(lightIntensity > 0.5f) + { + lightIntensity = 0.75f; + } + else if(lightIntensity > 0.25f) + { + lightIntensity = 0.5f; + } + else + { + lightIntensity = 0.25f; + } + + // Determine the diffuse color amount of the light. + colorArray = (diffuseColor * lightIntensity) * intensity; + + // Initialize the sum of colors. + colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f); + + // Add the light color. + colorSum.r += colorArray.r; + colorSum.g += colorArray.g; + colorSum.b += colorArray.b; + + // Multiply the texture pixel by the light color to get the final result. + color = saturate(colorSum) * textureColor; return color; } diff --git a/enginecustom/src/hlsl/celshading.vs b/enginecustom/src/hlsl/celshading.vs index 774baf8..e704290 100644 --- a/enginecustom/src/hlsl/celshading.vs +++ b/enginecustom/src/hlsl/celshading.vs @@ -1,3 +1,6 @@ +///////////// +// GLOBALS // +///////////// cbuffer MatrixBuffer { matrix worldMatrix; @@ -5,22 +8,42 @@ cbuffer MatrixBuffer matrix projectionMatrix; }; +cbuffer CameraBuffer +{ + float3 cameraPosition; + float padding; +}; + +cbuffer SunLightBuffer +{ + float4 ambientColor; // Color of the ambient light. + float4 diffuseColor; // Color of the diffuse light. + float3 lightDirection; // Direction of the light. + float intensity; // Intensity of the light. + +}; + +////////////// +// TYPEDEFS // +////////////// struct VertexInputType { float4 position : POSITION; - float3 normal : NORMAL; float2 tex : TEXCOORD0; + float3 normal : NORMAL; }; struct PixelInputType { float4 position : SV_POSITION; - float3 normal : NORMAL; float2 tex : TEXCOORD0; - float3 worldPos : TEXCOORD1; // Add world position + float3 normal : NORMAL; }; -PixelInputType CelShadingVertexShader(VertexInputType input) +//////////////////////////////////////////////////////////////////////////////// +// Vertex Shader +//////////////////////////////////////////////////////////////////////////////// +PixelInputType SunLightVertexShader(VertexInputType input) { PixelInputType output; @@ -28,18 +51,18 @@ PixelInputType CelShadingVertexShader(VertexInputType input) input.position.w = 1.0f; // Calculate the position of the vertex against the world, view, and projection matrices. - float4 worldPosition = mul(input.position, worldMatrix); - output.position = mul(worldPosition, viewMatrix); + output.position = mul(input.position, worldMatrix); + output.position = mul(output.position, viewMatrix); output.position = mul(output.position, projectionMatrix); - - // Pass the normal to the pixel shader - output.normal = mul((float3x3)worldMatrix, input.normal); - - // Pass the world position to the pixel shader - output.worldPos = worldPosition.xyz; - + // Store the texture coordinates for the pixel shader. output.tex = input.tex; + + // Calculate the normal vector against the world matrix only. + output.normal = mul(input.normal, (float3x3) worldMatrix); + + // Normalize the normal vector. + output.normal = normalize(output.normal); return output; } diff --git a/enginecustom/src/inc/shader/CelShadingShader.h b/enginecustom/src/inc/shader/CelShadingShader.h index 141c6a3..7522ea1 100644 --- a/enginecustom/src/inc/shader/CelShadingShader.h +++ b/enginecustom/src/inc/shader/CelShadingShader.h @@ -1,9 +1,5 @@ -#ifndef _CELSHADINGSHADER_H_ -#define _CELSHADINGSHADER_H_ +#pragma once -////////////// -// INCLUDES // -////////////// #include "Logger.h" #include #include @@ -12,57 +8,57 @@ using namespace DirectX; using namespace std; -//////////////////////////////////////////////////////////////////////////////// -// Class name: CelShadingShader -//////////////////////////////////////////////////////////////////////////////// -class CelShadingShader +class CelshadeClass { -private: - struct MatrixBufferType + + private : + struct MatrixBufferType { XMMATRIX world; XMMATRIX view; XMMATRIX projection; }; - struct LightBufferType + struct CameraBufferType { - XMFLOAT4 diffuseColor; - XMFLOAT3 lightDirection; - float padding; // Padding to ensure the structure is a multiple of 16 bytes. - XMFLOAT3 lightPosition; // Add light position - float padding2; // Padding to ensure the structure is a multiple of 16 bytes. - float constantAttenuation; - float linearAttenuation; - float quadraticAttenuation; - float padding3; // Padding to ensure the structure is a multiple of 16 bytes. + XMFLOAT3 cameraPosition; + float padding; }; + struct SunLightBufferType + { + XMFLOAT4 diffuseColor; + XMFLOAT4 ambientColor; + XMFLOAT3 sunDirection; + float intensity; + }; + -public: - CelShadingShader(); - CelShadingShader(const CelShadingShader&); - ~CelShadingShader(); +public : + CelshadeClass(); + CelshadeClass(const CelshadeClass&); + ~CelshadeClass(); bool Initialize(ID3D11Device*, HWND); void Shutdown(); - bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3); + bool Render(ID3D11DeviceContext* deviceContex, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection,float intensity); private: bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*); void ShutdownShader(); void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*); - bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3); + bool SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor, XMFLOAT3 lightDirection, float sunIntensity); void RenderShader(ID3D11DeviceContext*, int); private: ID3D11VertexShader* m_vertexShader; ID3D11PixelShader* m_pixelShader; ID3D11InputLayout* m_layout; - ID3D11Buffer* m_matrixBuffer; ID3D11SamplerState* m_sampleState; - ID3D11Buffer* m_lightBuffer; -}; - -#endif \ No newline at end of file + ID3D11Buffer* m_matrixBuffer; + ID3D11Buffer* m_cameraBuffer; + ID3D11Buffer* m_sunlightBuffer; + ID3D11Buffer* m_sunlightColorBuffer; + ID3D11Buffer* m_sunlightPositionBuffer; +}; \ No newline at end of file diff --git a/enginecustom/src/inc/shader/shadermanagerclass.h b/enginecustom/src/inc/shader/shadermanagerclass.h index d4a00bd..87be1c3 100644 --- a/enginecustom/src/inc/shader/shadermanagerclass.h +++ b/enginecustom/src/inc/shader/shadermanagerclass.h @@ -1,7 +1,7 @@ #ifndef _SHADERMANAGERCLASS_H_ #define _SHADERMANAGERCLASS_H_ -// Inclure les en-têtes nécessaires +// Inclure les en-têtes nécessaires #include #include #include @@ -42,7 +42,7 @@ public: bool RenderlightMapShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*); bool RenderRefractionShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4); bool RenderWaterShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float); - bool RenderCelShadingShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT3); + bool RenderCelShadingShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float); bool RenderSunlightShader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float); private: TextureShaderClass* m_TextureShader; @@ -56,7 +56,7 @@ private: LightMapShaderClass* m_LightMapShader; RefractionShaderClass* m_RefractionShader; WaterShaderClass* m_WaterShader; - CelShadingShader* m_CelShadingShader; + CelshadeClass* m_CelShadingShader; SunlightShaderClass* m_SunlightShader; }; diff --git a/enginecustom/src/inc/system/applicationclass.h b/enginecustom/src/inc/system/applicationclass.h index d61994b..76946fa 100644 --- a/enginecustom/src/inc/system/applicationclass.h +++ b/enginecustom/src/inc/system/applicationclass.h @@ -35,6 +35,9 @@ #include // Pour _com_error #include #include +#include +#include +#include ///////////// @@ -136,7 +139,6 @@ public: bool GetCanFixedUpdate() const { return CanFixedUpdate; }; void SetCanFixedUpdate(bool canFixedUpdate) { CanFixedUpdate = canFixedUpdate; }; - private: bool Render(float, float, float, float, float); bool RenderPhysics(bool keyLeft, bool keyRight, bool keyUp, bool keyDown, float deltaTime); diff --git a/enginecustom/src/inc/system/imguiManager.h b/enginecustom/src/inc/system/imguiManager.h index bf893b0..0333dc0 100644 --- a/enginecustom/src/inc/system/imguiManager.h +++ b/enginecustom/src/inc/system/imguiManager.h @@ -33,7 +33,6 @@ public: void WidgetObjectWindow(ApplicationClass* app); void WidgetTerrainWindow(ApplicationClass* app); void WidgetLightWindow(ApplicationClass* app); - void WidgetShaderWindow(ApplicationClass* app); void WidgetEngineSettingsWindow(ApplicationClass* app); void WidgetRenderWindow(ApplicationClass* app, ImVec2 availableSize); void WidgetLogWindow(ApplicationClass* app); diff --git a/enginecustom/src/src/shader/CelShadingShader.cpp b/enginecustom/src/src/shader/CelShadingShader.cpp index 25f238f..fa31940 100644 --- a/enginecustom/src/src/shader/CelShadingShader.cpp +++ b/enginecustom/src/src/shader/CelShadingShader.cpp @@ -1,38 +1,47 @@ +//////////////////////////////////////////////////////////////////////////////// +// Filename: lightshaderclass.cpp +//////////////////////////////////////////////////////////////////////////////// #include "CelShadingShader.h" -#include -CelShadingShader::CelShadingShader() + +CelshadeClass::CelshadeClass() { m_vertexShader = 0; m_pixelShader = 0; m_layout = 0; - m_matrixBuffer = 0; m_sampleState = 0; - m_lightBuffer = 0; + m_matrixBuffer = 0; + m_cameraBuffer = 0; + m_sunlightBuffer = 0; + m_sunlightColorBuffer = 0; + m_sunlightPositionBuffer = 0; } -CelShadingShader::CelShadingShader(const CelShadingShader& other) + +CelshadeClass::CelshadeClass(const CelshadeClass& other) { } -CelShadingShader::~CelShadingShader() + +CelshadeClass::~CelshadeClass() { } -bool CelShadingShader::Initialize(ID3D11Device* device, HWND hwnd) -{ - Logger::Get().Log("Initializing CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize); - bool result; +bool CelshadeClass::Initialize(ID3D11Device* device, HWND hwnd) +{ + Logger::Get().Log("Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize); + wchar_t vsFilename[128]; wchar_t psFilename[128]; int error; + bool result; // Set the filename of the vertex shader. error = wcscpy_s(vsFilename, 128, L"src/hlsl/celshading.vs"); if (error != 0) { - Logger::Get().Log("Failed to set the filename of the vertex shader", __FILE__, __LINE__); + Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } @@ -40,38 +49,42 @@ bool CelShadingShader::Initialize(ID3D11Device* device, HWND hwnd) error = wcscpy_s(psFilename, 128, L"src/hlsl/celshading.ps"); if (error != 0) { - Logger::Get().Log("Failed to set the filename of the pixel shader", __FILE__, __LINE__); + Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } - // Initialize the vertex and pixel shaders. result = InitializeShader(device, hwnd, vsFilename, psFilename); if (!result) { - Logger::Get().Log("Failed to initialize the vertex and pixel shaders", __FILE__, __LINE__); + Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } - Logger::Get().Log("Successfully initialized CelShadingShader", __FILE__, __LINE__, Logger::LogLevel::Initialize); + Logger::Get().Log("SunLightShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); return true; } -void CelShadingShader::Shutdown() + +void CelshadeClass::Shutdown() { // Shutdown the vertex and pixel shaders as well as the related objects. ShutdownShader(); + + return; } -bool CelShadingShader::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, - ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition) +bool CelshadeClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity) { bool result; + + // Set the shader parameters that it will use for rendering. - result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor, lightPosition); + result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity); if (!result) { - Logger::Get().Log("CelShading Error", __FILE__, __LINE__, Logger::LogLevel::Error); + Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } @@ -81,89 +94,92 @@ bool CelShadingShader::Render(ID3D11DeviceContext* deviceContext, int indexCount return true; } -bool CelShadingShader::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename) + +bool CelshadeClass::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename) { + Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize); + HRESULT result; - ID3D10Blob* errorMessage = nullptr; - ID3D10Blob* vertexShaderBuffer = nullptr; - ID3D10Blob* pixelShaderBuffer = nullptr; + ID3D10Blob* errorMessage; + ID3D10Blob* vertexShaderBuffer; + ID3D10Blob* pixelShaderBuffer; D3D11_INPUT_ELEMENT_DESC polygonLayout[3]; unsigned int numElements; - D3D11_BUFFER_DESC matrixBufferDesc; D3D11_SAMPLER_DESC samplerDesc; - D3D11_BUFFER_DESC lightBufferDesc; + D3D11_BUFFER_DESC matrixBufferDesc; + D3D11_BUFFER_DESC sunlightBufferDesc; + + // Initialize the pointers this function will use to null. + errorMessage = 0; + vertexShaderBuffer = 0; + pixelShaderBuffer = 0; // Compile the vertex shader code. - result = D3DCompileFromFile(vsFilename, nullptr, nullptr, "CelShadingVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage); + result = D3DCompileFromFile(vsFilename, NULL, NULL, "SunLightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage); if (FAILED(result)) { - // If the shader failed to compile it should have written 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); + Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error); } - return false; } // Compile the pixel shader code. - result = D3DCompileFromFile(psFilename, nullptr, nullptr, "CelShadingPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage); + result = D3DCompileFromFile(psFilename, NULL, NULL, "SunLightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage); if (FAILED(result)) { - // If the shader failed to compile it should have written 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); + Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error); } - return false; } // Create the vertex shader from the buffer. - result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), nullptr, &m_vertexShader); + result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &m_vertexShader); if (FAILED(result)) { + Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } // Create the pixel shader from the buffer. - result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), nullptr, &m_pixelShader); + result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &m_pixelShader); if (FAILED(result)) { + Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } // Create the vertex input layout description. - // This setup needs to match the VertexType structure in the ModelClass and in the shader. polygonLayout[0].SemanticName = "POSITION"; polygonLayout[0].SemanticIndex = 0; - polygonLayout[0].Format = DXGI_FORMAT_R32G32B32A32_FLOAT; + 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 = "NORMAL"; + polygonLayout[1].SemanticName = "TEXCOORD"; polygonLayout[1].SemanticIndex = 0; - polygonLayout[1].Format = DXGI_FORMAT_R32G32B32_FLOAT; + polygonLayout[1].Format = DXGI_FORMAT_R32G32_FLOAT; polygonLayout[1].InputSlot = 0; polygonLayout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; polygonLayout[1].InstanceDataStepRate = 0; - polygonLayout[2].SemanticName = "TEXCOORD"; + polygonLayout[2].SemanticName = "NORMAL"; polygonLayout[2].SemanticIndex = 0; - polygonLayout[2].Format = DXGI_FORMAT_R32G32_FLOAT; + polygonLayout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT; polygonLayout[2].InputSlot = 0; polygonLayout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT; polygonLayout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; @@ -176,30 +192,16 @@ bool CelShadingShader::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &m_layout); if (FAILED(result)) { + Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } // Release the vertex shader buffer and pixel shader buffer since they are no longer needed. vertexShaderBuffer->Release(); - vertexShaderBuffer = nullptr; + vertexShaderBuffer = 0; pixelShaderBuffer->Release(); - pixelShaderBuffer = nullptr; - - // 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, nullptr, &m_matrixBuffer); - if (FAILED(result)) - { - return false; - } + pixelShaderBuffer = 0; // Create a texture sampler state description. samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; @@ -220,78 +222,128 @@ bool CelShadingShader::InitializeShader(ID3D11Device* device, HWND hwnd, WCHAR* result = device->CreateSamplerState(&samplerDesc, &m_sampleState); if (FAILED(result)) { + Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } - // Setup the description of the light dynamic constant buffer that is in the pixel shader. - lightBufferDesc.Usage = D3D11_USAGE_DYNAMIC; - lightBufferDesc.ByteWidth = sizeof(LightBufferType); - lightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; - lightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; - lightBufferDesc.MiscFlags = 0; - lightBufferDesc.StructureByteStride = 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 pixel shader constant buffer from within this class. - result = device->CreateBuffer(&lightBufferDesc, nullptr, &m_lightBuffer); + // 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)) { + Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } + // Setup the description of the dynamic sunlight constant buffer that is in the pixel shader. + sunlightBufferDesc.Usage = D3D11_USAGE_DYNAMIC; + sunlightBufferDesc.ByteWidth = sizeof(SunLightBufferType); + sunlightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; + sunlightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + sunlightBufferDesc.MiscFlags = 0; + sunlightBufferDesc.StructureByteStride = 0; + + // Create the constant buffer pointer so we can access the pixel shader constant buffer from within this class. + result = device->CreateBuffer(&sunlightBufferDesc, NULL, &m_sunlightBuffer); + if (FAILED(result)) + { + Logger::Get().Log("Failed to create sunlight buffer", __FILE__, __LINE__, Logger::LogLevel::Error); + return false; + } + + Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); + return true; } -void CelShadingShader::ShutdownShader() + + +void CelshadeClass::ShutdownShader() { - // Release the light constant buffer. - if (m_lightBuffer) + Logger::Get().Log("Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown); + + // Release the light constant buffers. + if (m_sunlightColorBuffer) { - m_lightBuffer->Release(); - m_lightBuffer = nullptr; + m_sunlightColorBuffer->Release(); + m_sunlightColorBuffer = 0; } - // Release the sampler state. - if (m_sampleState) + if (m_sunlightPositionBuffer) { - m_sampleState->Release(); - m_sampleState = nullptr; + m_sunlightPositionBuffer->Release(); + m_sunlightPositionBuffer = 0; + } + + // Release the light constant buffer. + if (m_sunlightBuffer) + { + m_sunlightBuffer->Release(); + m_sunlightBuffer = 0; + } + + // Release the camera constant buffer. + if (m_cameraBuffer) + { + m_cameraBuffer->Release(); + m_cameraBuffer = 0; } // Release the matrix constant buffer. if (m_matrixBuffer) { m_matrixBuffer->Release(); - m_matrixBuffer = nullptr; + m_matrixBuffer = 0; + } + + // Release the sampler state. + if (m_sampleState) + { + m_sampleState->Release(); + m_sampleState = 0; } // Release the layout. if (m_layout) { m_layout->Release(); - m_layout = nullptr; + m_layout = 0; } // Release the pixel shader. if (m_pixelShader) { m_pixelShader->Release(); - m_pixelShader = nullptr; + m_pixelShader = 0; } // Release the vertex shader. if (m_vertexShader) { m_vertexShader->Release(); - m_vertexShader = nullptr; + m_vertexShader = 0; } + + Logger::Get().Log("SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown); + + return; } -void CelShadingShader::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename) + +void CelshadeClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename) { char* compileErrors; - unsigned long bufferSize, i; - std::ofstream fout; + unsigned __int64 bufferSize, i; + ofstream fout; + // Get a pointer to the error message text buffer. compileErrors = (char*)(errorMessage->GetBufferPointer()); @@ -313,20 +365,32 @@ void CelShadingShader::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h // Release the error message. errorMessage->Release(); - errorMessage = nullptr; + errorMessage = 0; // Pop a message up on the screen to notify the user to check the text file for compile errors. - MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK); + MessageBox(hwnd, L"Error compiling shader. Check shader-error.txt for message.", shaderFilename, MB_OK); + + return; } -bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, - ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition) +bool CelshadeClass::SetShaderParameters( + ID3D11DeviceContext* deviceContext, + XMMATRIX worldMatrix, + XMMATRIX viewMatrix, + XMMATRIX projectionMatrix, + ID3D11ShaderResourceView* texture, + XMFLOAT4 ambientColor, + XMFLOAT4 diffuseColor, + XMFLOAT3 lightDirection, + float sunIntensity + ) { HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; MatrixBufferType* dataPtr; - LightBufferType* dataPtr2; + CameraBufferType* dataPtr2; + SunLightBufferType* dataPtr3; unsigned int bufferNumber; // Transpose the matrices to prepare them for the shader. @@ -358,32 +422,30 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X // Finally set the constant buffer in the vertex shader with the updated values. deviceContext->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer); - // Lock the light constant buffer so it can be written to. - result = deviceContext->Map(m_lightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); + // Lock the sunlight constant buffer so it can be written to. + result = deviceContext->Map(m_sunlightBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); if (FAILED(result)) { return false; } // Get a pointer to the data in the constant buffer. - dataPtr2 = (LightBufferType*)mappedResource.pData; + dataPtr3 = (SunLightBufferType*)mappedResource.pData; // Copy the lighting variables into the constant buffer. - dataPtr2->diffuseColor = diffuseColor; - dataPtr2->lightDirection = lightDirection; - dataPtr2->lightPosition = lightPosition; - dataPtr2->constantAttenuation = 0.5f; // Set your attenuation values here - dataPtr2->linearAttenuation = 0.1f; - dataPtr2->quadraticAttenuation = 0.01f; + dataPtr3->ambientColor = ambientColor; + dataPtr3->diffuseColor = diffuseColor; + dataPtr3->sunDirection = lightDirection; + dataPtr3->intensity = sunIntensity; // Unlock the constant buffer. - deviceContext->Unmap(m_lightBuffer, 0); + deviceContext->Unmap(m_sunlightBuffer, 0); - // Set the position of the light constant buffer in the pixel shader. + // Set the position of the sunlight constant buffer in the pixel shader. bufferNumber = 0; - - // Finally set the light constant buffer in the pixel shader with the updated values. - deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_lightBuffer); + + // Finally set the sunlight constant buffer in the pixel shader with the updated values. + deviceContext->PSSetConstantBuffers(bufferNumber, 1, &m_sunlightBuffer); // Set shader texture resource in the pixel shader. deviceContext->PSSetShaderResources(0, 1, &texture); @@ -391,20 +453,20 @@ bool CelShadingShader::SetShaderParameters(ID3D11DeviceContext* deviceContext, X return true; } - - -void CelShadingShader::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount) +void CelshadeClass::RenderShader(ID3D11DeviceContext* deviceContext, int indexCount) { // Set the vertex input layout. deviceContext->IASetInputLayout(m_layout); // Set the vertex and pixel shaders that will be used to render this triangle. - deviceContext->VSSetShader(m_vertexShader, nullptr, 0); - deviceContext->PSSetShader(m_pixelShader, nullptr, 0); + deviceContext->VSSetShader(m_vertexShader, NULL, 0); + deviceContext->PSSetShader(m_pixelShader, NULL, 0); // Set the sampler state in the pixel shader. deviceContext->PSSetSamplers(0, 1, &m_sampleState); // Render the triangle. deviceContext->DrawIndexed(indexCount, 0, 0); + + return; } diff --git a/enginecustom/src/src/shader/shadermanagerclass.cpp b/enginecustom/src/src/shader/shadermanagerclass.cpp index 582dd24..eb38a3a 100644 --- a/enginecustom/src/src/shader/shadermanagerclass.cpp +++ b/enginecustom/src/src/shader/shadermanagerclass.cpp @@ -127,7 +127,7 @@ bool ShaderManagerClass::Initialize(ID3D11Device* device, HWND hwnd) return false; } - m_CelShadingShader = new CelShadingShader; + m_CelShadingShader = new CelshadeClass; result = m_CelShadingShader->Initialize(device, hwnd); if (!result) { @@ -422,11 +422,11 @@ bool ShaderManagerClass::RenderWaterShader(ID3D11DeviceContext* deviceContext, i } bool ShaderManagerClass::RenderCelShadingShader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, - ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor, XMFLOAT3 lightPosition) + ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity) { bool result; - result = m_CelShadingShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor, lightPosition); + result = m_CelShadingShader->Render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity); if (!result) { return false; diff --git a/enginecustom/src/src/system/applicationclass.cpp b/enginecustom/src/src/system/applicationclass.cpp index 8785b33..443fa52 100644 --- a/enginecustom/src/src/system/applicationclass.cpp +++ b/enginecustom/src/src/system/applicationclass.cpp @@ -1899,8 +1899,18 @@ bool ApplicationClass::RenderPass(const std::vectorGetActiveShader()) { case Object::CEL_SHADING: - result = m_ShaderManager->RenderCelShadingShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0), - m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor(), TrueLightPosition); + result = m_ShaderManager->RenderCelShadingShader( + m_Direct3D->GetDeviceContext(), + object->GetIndexCount(), + worldMatrix, + view, + projection, + object->GetTexture(0), + m_SunLight->GetDiffuseColor(), + m_SunLight->GetAmbientColor(), + m_SunLight->GetDirection(), + m_SunLight->GetIntensity() + ); if (!result) { Logger::Get().Log("Could not render the model using the cel shader", __FILE__, __LINE__, Logger::LogLevel::Error); @@ -1909,7 +1919,17 @@ bool ApplicationClass::RenderPass(const std::vectorRenderNormalMapShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0), object->GetTexture(1), m_Lights[0]->GetDirection(), m_Lights[0]->GetDiffuseColor()); + result = m_ShaderManager->RenderNormalMapShader( + m_Direct3D->GetDeviceContext(), + object->GetIndexCount(), + worldMatrix, + view, + projection, + object->GetTexture(0), + object->GetTexture(1), + m_Lights[0]->GetDirection(), + m_Lights[0]->GetDiffuseColor() + ); if (!result) { Logger::Get().Log("Could not render the model using the normal map shader", __FILE__, __LINE__, Logger::LogLevel::Error); @@ -1945,8 +1965,17 @@ bool ApplicationClass::RenderPass(const std::vectorRenderSunlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, - object->GetTexture(0), m_SunLight->GetDiffuseColor(), m_SunLight->GetAmbientColor(), m_SunLight->GetDirection(), m_SunLight->GetIntensity()); + result = m_ShaderManager->RenderSunlightShader( + m_Direct3D->GetDeviceContext(), + object->GetIndexCount(), + worldMatrix, + view, + projection, + object->GetTexture(0), + m_SunLight->GetDiffuseColor(), + m_SunLight->GetAmbientColor(), + m_SunLight->GetDirection(), + m_SunLight->GetIntensity()); if (!result) { Logger::Get().Log("Could not render the object model using the sunlight shader", __FILE__, __LINE__, Logger::LogLevel::Error); diff --git a/enginecustom/src/src/system/imguiManager.cpp b/enginecustom/src/src/system/imguiManager.cpp index 540ca07..833c238 100644 --- a/enginecustom/src/src/system/imguiManager.cpp +++ b/enginecustom/src/src/system/imguiManager.cpp @@ -134,17 +134,6 @@ void imguiManager::WidgetAddObject(ApplicationClass* app) } } -void imguiManager::WidgetShaderWindow(ApplicationClass* app) -{ - ImGui::Begin("Shader Manager"); - - // Checkbox for toggling cel shading globally in the application class by calling the SetCelShading function in the application class when the checkbox state changes - ImGui::Checkbox("Enable Cel Shading", &m_EnableCelShading); - app->SetCelShading(m_EnableCelShading); - - ImGui::End(); -} - void imguiManager::WidgetObjectWindow(ApplicationClass* app) { ImGui::Begin("Objects", &showObjectWindow); @@ -417,11 +406,6 @@ bool imguiManager::ImGuiWidgetRenderer(ApplicationClass* app) WidgetLightWindow(app); } - if (showShaderWindow) - { - WidgetShaderWindow(app); - } - if (showEngineSettingsWindow) { WidgetEngineSettingsWindow(app);