From f736a6bb54f06f84ffd4b66cadccbddbe0e7dff1 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Wed, 27 Mar 2024 12:01:50 +0100 Subject: [PATCH] merge avec Sprite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit en espérant que ça a rien kapout pt kc --- enginecustom/Light.ps | 16 ++++++++--- enginecustom/Light.vs | 2 +- enginecustom/Lightclass.cpp | 12 +++++++++ enginecustom/Lightclass.h | 19 +++++++------ enginecustom/Lightshaderclass.cpp | 44 ++++++++++++++++++------------- enginecustom/Lightshaderclass.h | 23 ++++++++-------- 6 files changed, 75 insertions(+), 41 deletions(-) diff --git a/enginecustom/Light.ps b/enginecustom/Light.ps index 66babd6..8af8dd4 100644 --- a/enginecustom/Light.ps +++ b/enginecustom/Light.ps @@ -10,6 +10,7 @@ Texture2D shaderTexture : register(t0); SamplerState SampleType : register(s0); cbuffer LightBuffer { + float4 ambientColor; float4 diffuseColor; float3 lightDirection; float padding; @@ -41,14 +42,23 @@ float4 LightPixelShader(PixelInputType input) : SV_TARGET // Sample the pixel color from the texture using the sampler at this texture coordinate location. textureColor = shaderTexture.Sample(SampleType, input.tex); - // Invert the light direction for calculations. + // Set the default output color to the ambient light value for all pixels. + color = ambientColor; + + // Invert the light direction for calculations. lightDir = -lightDirection; // Calculate the amount of light on this pixel. lightIntensity = saturate(dot(input.normal, lightDir)); - // Determine the final amount of diffuse color based on the diffuse color combined with the light intensity. - color = saturate(diffuseColor * lightIntensity); + if(lightIntensity > 0.0f) + { + // Determine the final diffuse color based on the diffuse color and the amount of light intensity. + color += (diffuseColor * lightIntensity); + } + + // Saturate the final light color. + color = saturate(color); // Multiply the texture pixel and the final diffuse color to get the final pixel color result. color = color * textureColor; diff --git a/enginecustom/Light.vs b/enginecustom/Light.vs index 87f1edd..940b5ec 100644 --- a/enginecustom/Light.vs +++ b/enginecustom/Light.vs @@ -49,7 +49,7 @@ PixelInputType LightVertexShader(VertexInputType input) // 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); diff --git a/enginecustom/Lightclass.cpp b/enginecustom/Lightclass.cpp index e6f0f9b..85f6160 100644 --- a/enginecustom/Lightclass.cpp +++ b/enginecustom/Lightclass.cpp @@ -18,6 +18,13 @@ LightClass::~LightClass() { } +void LightClass::SetAmbientColor(float red, float green, float blue, float alpha) +{ + m_ambientColor = XMFLOAT4(red, green, blue, alpha); + return; +} + + void LightClass::SetDiffuseColor(float red, float green, float blue, float alpha) { @@ -32,6 +39,11 @@ void LightClass::SetDirection(float x, float y, float z) return; } +XMFLOAT4 LightClass::GetAmbientColor() +{ + return m_ambientColor; +} + XMFLOAT4 LightClass::GetDiffuseColor() { diff --git a/enginecustom/Lightclass.h b/enginecustom/Lightclass.h index 0159c95..aafc8a2 100644 --- a/enginecustom/Lightclass.h +++ b/enginecustom/Lightclass.h @@ -23,15 +23,18 @@ public: LightClass(const LightClass&); ~LightClass(); - void SetDirection(float, float, float); - void SetDiffuseColor(float, float, float, float); - - XMFLOAT3 GetDirection(); - XMFLOAT4 GetDiffuseColor(); - + void SetAmbientColor(float, float, float, float); + void SetDiffuseColor(float, float, float, float); + void SetDirection(float, float, float); + + XMFLOAT4 GetAmbientColor(); + XMFLOAT4 GetDiffuseColor(); + XMFLOAT3 GetDirection(); + private: - XMFLOAT4 m_diffuseColor; - XMFLOAT3 m_direction; + XMFLOAT4 m_ambientColor; + XMFLOAT4 m_diffuseColor; + XMFLOAT3 m_direction; }; #endif \ No newline at end of file diff --git a/enginecustom/Lightshaderclass.cpp b/enginecustom/Lightshaderclass.cpp index c759fd3..9a8cc93 100644 --- a/enginecustom/Lightshaderclass.cpp +++ b/enginecustom/Lightshaderclass.cpp @@ -39,13 +39,19 @@ bool LightShaderClass::Initialize(ID3D11Device* device, HWND hwnd) return false; } - // Set the filename of the pixel shader. - error = wcscpy_s(psFilename, 128, L"./Light.ps"); - if (error != 0) - { - return false; - } + // Set the filename of the vertex shader. + error = wcscpy_s(vsFilename, 128, L"light.vs"); + if (error != 0) + { + return false; + } + // Set the filename of the pixel shader. + error = wcscpy_s(psFilename, 128, L"light.ps"); + if (error != 0) + { + return false; + } // Initialize the vertex and pixel shaders. result = InitializeShader(device, hwnd, vsFilename, psFilename); if (!result) @@ -66,17 +72,17 @@ void LightShaderClass::Shutdown() } bool LightShaderClass::Render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, - ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor) + ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor) { bool result; - // Set the shader parameters that it will use for rendering. - result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, diffuseColor); - if (!result) - { - return false; - } + // Set the shader parameters that it will use for rendering. + result = SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, lightDirection, ambientColor, diffuseColor); + if(!result) + { + return false; + } // Now render the prepared buffers with the shader. RenderShader(deviceContext, indexCount); @@ -339,8 +345,9 @@ void LightShaderClass::OutputShaderErrorMessage(ID3D10Blob* errorMessage, HWND h return; } + bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, - ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 diffuseColor) + ID3D11ShaderResourceView* texture, XMFLOAT3 lightDirection, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor) { HRESULT result; D3D11_MAPPED_SUBRESOURCE mappedResource; @@ -391,10 +398,11 @@ bool LightShaderClass::SetShaderParameters(ID3D11DeviceContext* deviceContext, X // Get a pointer to the data in the constant buffer. dataPtr2 = (LightBufferType*)mappedResource.pData; - // Copy the lighting variables into the constant buffer. - dataPtr2->diffuseColor = diffuseColor; - dataPtr2->lightDirection = lightDirection; - dataPtr2->padding = 0.0f; + // Copy the lighting variables into the constant buffer. + dataPtr2->ambientColor = ambientColor; + dataPtr2->diffuseColor = diffuseColor; + dataPtr2->lightDirection = lightDirection; + dataPtr2->padding = 0.0f; // Unlock the constant buffer. deviceContext->Unmap(m_lightBuffer, 0); diff --git a/enginecustom/Lightshaderclass.h b/enginecustom/Lightshaderclass.h index 247d3b8..6384900 100644 --- a/enginecustom/Lightshaderclass.h +++ b/enginecustom/Lightshaderclass.h @@ -29,29 +29,30 @@ private: XMMATRIX projection; }; - struct LightBufferType - { - XMFLOAT4 diffuseColor; - XMFLOAT3 lightDirection; - float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements. - }; + struct LightBufferType + { + XMFLOAT4 ambientColor; + XMFLOAT4 diffuseColor; + XMFLOAT3 lightDirection; + float padding; // Added extra padding so structure is a multiple of 16 for CreateBuffer function requirements. + }; public: LightShaderClass(); LightShaderClass(const LightShaderClass&); ~LightShaderClass(); - bool Initialize(ID3D11Device*, HWND); - void Shutdown(); - bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4); + bool Initialize(ID3D11Device*, HWND); + void Shutdown(); + bool Render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4); private: bool InitializeShader(ID3D11Device*, HWND, WCHAR*, WCHAR*); void ShutdownShader(); void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*); - bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4); - void RenderShader(ID3D11DeviceContext*, int); + bool SetShaderParameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4, XMFLOAT4); + void RenderShader(ID3D11DeviceContext*, int); private: ID3D11VertexShader* m_vertexShader;