diff --git a/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml b/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml
index 690b46b..d43725b 100644
--- a/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml
+++ b/.idea/.idea.KhaoticEngineReborn/.idea/workspace.xml
@@ -5,97 +5,24 @@
-
-
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
@@ -181,64 +108,10 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
{}
@@ -253,6 +126,9 @@
"associatedIndex": 6
}
+
+
+
@@ -406,6 +282,7 @@
+
diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj
index 3fa969b..338bdf3 100644
--- a/enginecustom/enginecustom.vcxproj
+++ b/enginecustom/enginecustom.vcxproj
@@ -33,6 +33,82 @@
+
+ MultiThreadedDebugDll
+ EnableFastChecks
+ Disabled
+ true
+ NoListing
+ x64\Debug\
+ false
+ false
+ x64\Debug\
+ Default
+ true
+ Column
+ false
+ Prompt
+ false
+ Sync
+ false
+ false
+ false
+ NotSet
+ NotSet
+ Precise
+ true
+ false
+ false
+ false
+ Default
+ false
+ false
+ stdcpp17
+ Default
+ false
+ x64\Debug\
+ x64\Debug\
+ false
+ Neither
+ false
+ x64\Debug\
+ Cdecl
+ x64\Debug\vc143.pdb
+ NotUsing
+ stdafx.h
+ x64\Debug\KhaoticCore.pch
+ false
+ false
+ false
+ true
+ false
+ false
+ x64\Debug\
+ true
+ true
+ false
+ false
+ Default
+ x64\Debug\KhaoticCore.tlog\
+ false
+ true
+ false
+ true
+ true
+ Level3
+ x64\Debug\
+ EditAndContinue
+ false
+ false
+ false
+ InheritWarningLevel
+ true
+ false
+ _DEBUG;_WINDOWS;_UNICODE;UNICODE;
+ true
+ F:\Github_Repo\khaotic-engine-Reborn\enginecustom\include\backends;F:\Github_Repo\khaotic-engine-Reborn\enginecustom\include\Inc;F:\Github_Repo\khaotic-engine-Reborn\enginecustom\include;F:\Github_Repo\khaotic-engine-Reborn\enginecustom\src\inc\shader;F:\Github_Repo\khaotic-engine-Reborn\enginecustom\src\inc\system;F:\Github_Repo\khaotic-engine-Reborn\enginecustom\include\Vulkan\Include;
+ true
+
@@ -67,6 +143,7 @@
+
@@ -92,6 +169,7 @@
+
@@ -128,6 +206,7 @@
+
@@ -167,6 +246,16 @@
$(OutDir)\src\hlsl
$(OutDir)\src\hlsl
+
+ F:\Github_Repo\khaotic-engine-Reborn\x64\Debug\\src\hlsl
+ %(Filename)%(Extension)
+ Document
+
+
+ F:\Github_Repo\khaotic-engine-Reborn\x64\Debug\\src\hlsl
+ %(Filename)%(Extension)
+ Document
+
Document
$(OutDir)\src\hlsl
diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini
index 1dbfb45..3640275 100644
--- a/enginecustom/imgui.ini
+++ b/enginecustom/imgui.ini
@@ -16,8 +16,8 @@ Collapsed=0
DockId=0x00000011,0
[Window][Terrain]
-Pos=283,19
-Size=280,615
+Pos=0,19
+Size=280,842
Collapsed=0
DockId=0x00000007,0
diff --git a/enginecustom/shader-error.txt b/enginecustom/shader-error.txt
index 7490cae..b511831 100644
Binary files a/enginecustom/shader-error.txt and b/enginecustom/shader-error.txt differ
diff --git a/enginecustom/src/hlsl/depth.ps b/enginecustom/src/hlsl/depth.ps
new file mode 100644
index 0000000..d13f893
--- /dev/null
+++ b/enginecustom/src/hlsl/depth.ps
@@ -0,0 +1,10 @@
+struct PS_INPUT
+{
+ float4 position : SV_POSITION;
+};
+
+float4 DepthPixelShader(PS_INPUT input) : SV_TARGET
+{
+ // Pas de couleur, juste la profondeur
+ return float4(0.0f, 0.0f, 0.0f, 1.0f);
+}
\ No newline at end of file
diff --git a/enginecustom/src/hlsl/depth.vs b/enginecustom/src/hlsl/depth.vs
new file mode 100644
index 0000000..f58476b
--- /dev/null
+++ b/enginecustom/src/hlsl/depth.vs
@@ -0,0 +1,25 @@
+cbuffer MatrixBuffer : register(b0)
+{
+ matrix world;
+ matrix view;
+ matrix projection;
+};
+
+struct VS_INPUT
+{
+ float3 position : POSITION;
+};
+
+struct VS_OUTPUT
+{
+ float4 position : SV_POSITION;
+};
+
+VS_OUTPUT DepthVertexShader(VS_INPUT input)
+{
+ VS_OUTPUT output;
+ float4 worldPosition = mul(float4(input.position, 1.0f), world);
+ float4 viewPosition = mul(worldPosition, view);
+ output.position = mul(viewPosition, projection);
+ return output;
+}
\ No newline at end of file
diff --git a/enginecustom/src/hlsl/sunlight.ps b/enginecustom/src/hlsl/sunlight.ps
index 0e1b84b..729c220 100644
--- a/enginecustom/src/hlsl/sunlight.ps
+++ b/enginecustom/src/hlsl/sunlight.ps
@@ -3,6 +3,9 @@
/////////////
Texture2D shaderTexture : register(t0);
SamplerState SampleType : register(s0);
+Texture2D shadowMap : register(t1);
+SamplerState shadowSampleType : register(s1);
+
cbuffer SunLightBuffer
{
float4 ambientColor;
@@ -24,6 +27,7 @@ struct PixelInputType
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
+ float4 lightPosition : TEXCOORD1;
};
////////////////////////////////////////////////////////////////////////////////
@@ -31,23 +35,47 @@ struct PixelInputType
////////////////////////////////////////////////////////////////////////////////
float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
{
+
+ // temp return to check if the shader is working
+ return float4(1.0f, 0.0f, 0.0f, 1.0f);
+
float4 textureColor;
float4 color;
float lightIntensity;
float4 colorArray;
float4 colorSum;
+ float bias = 0.001f;
+ float shadowFactor = 1.0f;
- // Sample the pixel color from the texture using the sampler at this texture coordinate location.
+ // Sample the pixel color from the texture using the sampler at this coordinate
textureColor = shaderTexture.Sample(SampleType, input.tex);
+ // Calculate shadow factor
+ // Projection division to normalize coordinates
+ float2 projectTexCoord;
+ projectTexCoord.x = input.lightPosition.x / input.lightPosition.w / 2.0f + 0.5f;
+ projectTexCoord.y = -input.lightPosition.y / input.lightPosition.w / 2.0f + 0.5f;
+
+ if((saturate(projectTexCoord.x) == projectTexCoord.x) && (saturate(projectTexCoord.y) == projectTexCoord.y))
+ {
+ float depthValue = input.lightPosition.z / input.lightPosition.w;
+ float shadowDepth = shadowMap.Sample(shadowSampleType, projectTexCoord).r;
+
+ if(depthValue - bias > shadowDepth)
+ {
+ // Pixel est dans l'ombre
+ shadowFactor = 0.3f; // Ombre non totale pour un effet plus réaliste
+ }
+ }
+
// Calculate the different amounts of light on this pixel based on the direction of the light.
lightIntensity = saturate(dot(input.normal, -lightDirection));
// Determine the diffuse color amount of the light.
- colorArray = (diffuseColor * lightIntensity) * intensity;
-
+ colorArray = (diffuseColor * lightIntensity) * intensity * shadowFactor;
+
// Initialize the sum of colors.
- colorSum = float4(0.0f, 0.0f, 0.0f, 1.0f);
+ colorSum = ambientColor; // Conserver l'éclairage ambiant même dans l'ombre
// Add the light color.
colorSum.r += colorArray.r;
@@ -58,4 +86,4 @@ float4 SunLightPixelShader(PixelInputType input) : SV_TARGET
color = saturate(colorSum) * textureColor;
return color;
-}
+}
\ No newline at end of file
diff --git a/enginecustom/src/hlsl/sunlight.vs b/enginecustom/src/hlsl/sunlight.vs
index 46fab3b..34a047a 100644
--- a/enginecustom/src/hlsl/sunlight.vs
+++ b/enginecustom/src/hlsl/sunlight.vs
@@ -22,6 +22,12 @@ cbuffer SunLightBuffer
float intensity;
};
+cbuffer LightViewMatrixBuffer : register(b2)
+{
+ matrix lightViewMatrix;
+ matrix lightProjectionMatrix;
+};
+
//////////////
// TYPEDEFS //
//////////////
@@ -37,6 +43,7 @@ struct PixelInputType
float4 position : SV_POSITION;
float2 tex : TEXCOORD0;
float3 normal : NORMAL;
+ float4 lightPosition : TEXCOORD1;
};
////////////////////////////////////////////////////////////////////////////////
@@ -45,23 +52,30 @@ struct PixelInputType
PixelInputType SunLightVertexShader(VertexInputType input)
{
PixelInputType output;
+ float4 worldPosition; // Déclaration de la variable manquante
// 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 matrix
+ worldPosition = mul(input.position, worldMatrix);
+
+ // Calculate position in light view space.
+ output.lightPosition = mul(worldPosition, lightViewMatrix);
+ output.lightPosition = mul(output.lightPosition, lightProjectionMatrix);
- // 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);
+ // Standard position calculation
+ output.position = mul(worldPosition, viewMatrix);
output.position = mul(output.position, projectionMatrix);
// 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);
+ output.normal = mul(input.normal, (float3x3)worldMatrix);
// Normalize the normal vector.
output.normal = normalize(output.normal);
return output;
-}
+}
\ No newline at end of file
diff --git a/enginecustom/src/inc/shader/depth_shader_class.h b/enginecustom/src/inc/shader/depth_shader_class.h
new file mode 100644
index 0000000..f442c8c
--- /dev/null
+++ b/enginecustom/src/inc/shader/depth_shader_class.h
@@ -0,0 +1,37 @@
+#pragma once
+
+#include
+#include
+using namespace DirectX;
+
+class depth_shader_class
+{
+public:
+ depth_shader_class();
+ ~depth_shader_class();
+
+ bool initialize(ID3D11Device* device, HWND hwnd);
+ void shutdown();
+ bool render(ID3D11DeviceContext* deviceContext, int indexCount,
+ XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix);
+
+private:
+ struct MatrixBufferType
+ {
+ XMMATRIX world;
+ XMMATRIX view;
+ XMMATRIX projection;
+ };
+
+ bool initialize_shader(ID3D11Device* device, HWND hwnd, const WCHAR* vsFilename, const WCHAR* psFilename);
+ void shutdown_shader();
+ void output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, const WCHAR* shaderFilename);
+ bool set_shader_parameters(ID3D11DeviceContext* deviceContext,
+ XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix);
+ void render_shader(ID3D11DeviceContext* deviceContext, int indexCount);
+
+ ID3D11VertexShader* vertex_shader_;
+ ID3D11PixelShader* pixel_shader_;
+ ID3D11InputLayout* layout_;
+ ID3D11Buffer* matrix_buffer_;
+};
\ No newline at end of file
diff --git a/enginecustom/src/inc/shader/shader_manager_class.h b/enginecustom/src/inc/shader/shader_manager_class.h
index 11ea684..cf805ff 100644
--- a/enginecustom/src/inc/shader/shader_manager_class.h
+++ b/enginecustom/src/inc/shader/shader_manager_class.h
@@ -19,6 +19,7 @@
#include "celshade_class.h"
#include "skybox_shader_class.h"
#include "sunlight_shader_class.h"
+#include "depth_shader_class.h"
using namespace DirectX;
@@ -44,8 +45,10 @@ public:
bool render_refraction_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT3, XMFLOAT4[], XMFLOAT4[], XMFLOAT4[], XMFLOAT4);
bool render_water_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, ID3D11ShaderResourceView*, float, float);
bool render_cel_shading_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
- bool render_sunlight_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
+ bool render_sunlight_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float, ID3D11ShaderResourceView
+ * shadowMap, XMMATRIX lightViewMatrix, XMMATRIX lightProjectionMatrix);
bool render_skybox_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
+ bool render_depth_shader(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX);
private:
texture_shader_class* texture_shader_;
@@ -62,6 +65,7 @@ private:
celshade_class* cel_shading_shader_;
sunlight_shader_class* sunlight_shader_;
skybox_shader_class* skybox_shader_;
+ depth_shader_class* depth_shader_;
};
#endif
\ No newline at end of file
diff --git a/enginecustom/src/inc/shader/sunlight_shader_class.h b/enginecustom/src/inc/shader/sunlight_shader_class.h
index 2d15d4d..90b73b2 100644
--- a/enginecustom/src/inc/shader/sunlight_shader_class.h
+++ b/enginecustom/src/inc/shader/sunlight_shader_class.h
@@ -38,6 +38,12 @@ private :
XMFLOAT4 sun_color;
};
+ struct light_view_matrix_buffer_type
+ {
+ XMMATRIX light_view;
+ XMMATRIX light_projection;
+ };
+
public :
sunlight_shader_class();
sunlight_shader_class(const sunlight_shader_class&);
@@ -45,14 +51,29 @@ public :
bool initialize(ID3D11Device*, HWND);
void shutdown();
- bool render(ID3D11DeviceContext*, int, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3,float);
+ bool render(
+ ID3D11DeviceContext* device_context,
+ int index_count,
+ XMMATRIX world_matrix,
+ XMMATRIX view_matrix,
+ XMMATRIX projection_matrix,
+ ID3D11ShaderResourceView* texture,
+ XMFLOAT4 diffuse_color,
+ XMFLOAT4 ambient_color,
+ XMFLOAT3 sun_direction,
+ float sun_intensity,
+ ID3D11ShaderResourceView* shadow_map,
+ XMMATRIX light_view_matrix,
+ XMMATRIX light_projection_matrix
+ );
private:
bool initialize_shader(ID3D11Device*, HWND, WCHAR*, WCHAR*);
void shutdown_shader();
void output_shader_error_message(ID3D10Blob*, HWND, WCHAR*);
- bool set_shader_parameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float);
+ bool set_shader_parameters(ID3D11DeviceContext*, XMMATRIX, XMMATRIX, XMMATRIX, ID3D11ShaderResourceView*, XMFLOAT4, XMFLOAT4, XMFLOAT3, float, ID3D11ShaderResourceView
+ * shadowMap, XMMATRIX lightViewMatrix, XMMATRIX lightProjectionMatrix);
void render_shader(ID3D11DeviceContext*, int);
private:
@@ -65,4 +86,7 @@ private:
ID3D11Buffer* sunlight_buffer_;
ID3D11Buffer* sunlight_color_buffer_;
ID3D11Buffer* sunlight_position_buffer_;
+ ID3D11Buffer* light_view_matrix_buffer_;
+ ID3D11ShaderResourceView* shadow_map_texture_;
+ ID3D11SamplerState* shadow_sampler_state_;
};
\ No newline at end of file
diff --git a/enginecustom/src/inc/system/application_class.h b/enginecustom/src/inc/system/application_class.h
index 08af6f5..82c650b 100644
--- a/enginecustom/src/inc/system/application_class.h
+++ b/enginecustom/src/inc/system/application_class.h
@@ -43,6 +43,8 @@
#include
#include
+#include "shadow_map.h"
+
/////////////
// GLOBALS //
@@ -185,8 +187,17 @@ private:
bool render_scene_to_texture(float);
bool render_refraction_to_texture();
bool render_reflection_to_texture();
- bool render_pass(const std::vector>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection);
-
+ bool render_pass(
+ const std::vector>>& RenderQueues,
+ XMFLOAT4* diffuse,
+ XMFLOAT4* position,
+ XMFLOAT4* ambient,
+ XMMATRIX view,
+ XMMATRIX projection
+ );
+ bool create_shadow_map(
+ const std::vector>>& RenderQueues
+ );
void update_skybox_position();
public :
@@ -320,6 +331,18 @@ private :
// ------------------------------------------------- //
input inputs_;
+
+ // ------------------------------------------------- //
+ // -------------------- Shadows -------------------- //
+ // ------------------------------------------------- //
+
+ shadow_map* shadow_map_;
+ camera_class* light_camera_;
+ XMFLOAT4 light_position_buffer_;
+ XMFLOAT3 light_rotation_buffer_;
+ XMMATRIX light_view_matrix_;
+ XMMATRIX light_projection_matrix_;
+ ID3D11ShaderResourceView* shadow_srv_;
};
#endif
\ No newline at end of file
diff --git a/enginecustom/src/inc/system/shadow_map.h b/enginecustom/src/inc/system/shadow_map.h
new file mode 100644
index 0000000..2f4e19f
--- /dev/null
+++ b/enginecustom/src/inc/system/shadow_map.h
@@ -0,0 +1,33 @@
+#pragma once
+#include
+
+class shadow_map
+{
+public:
+
+ shadow_map();
+ shadow_map(const shadow_map&) = delete;
+ shadow_map& operator=(const shadow_map&) = delete;
+ ~shadow_map();
+
+ bool initialize(
+ ID3D11Device* device,
+ int shadow_map_width,
+ int shadow_map_height
+ );
+
+ void set_render_target(ID3D11DeviceContext* context);
+ void clear_render_target(ID3D11DeviceContext* context, float depth);
+
+ ID3D11ShaderResourceView* get_shader_resource_view() const;
+
+ void shutdown();
+
+private:
+ ID3D11Texture2D* depth_texture_;
+ ID3D11DepthStencilView* depth_stencil_view_;
+ ID3D11ShaderResourceView* shader_resource_view_;
+ D3D11_VIEWPORT viewport_;
+
+
+};
diff --git a/enginecustom/src/src/shader/depth_shader_class.cpp b/enginecustom/src/src/shader/depth_shader_class.cpp
new file mode 100644
index 0000000..183261a
--- /dev/null
+++ b/enginecustom/src/src/shader/depth_shader_class.cpp
@@ -0,0 +1,224 @@
+#include "depth_shader_class.h"
+
+#include
+
+#include "logger.h"
+
+depth_shader_class::depth_shader_class()
+{
+ vertex_shader_ = nullptr;
+ pixel_shader_ = nullptr;
+ layout_ = nullptr;
+ matrix_buffer_ = nullptr;
+}
+
+depth_shader_class::~depth_shader_class()
+{
+ shutdown();
+}
+
+bool depth_shader_class::initialize(ID3D11Device* device, HWND hwnd)
+{
+
+ Logger::Get().Log("Initializing depth shader class", __FILE__, __LINE__, Logger::LogLevel::Initialize);
+
+ // Charger et compiler les shaders (remplacez les chemins par les vôtres)
+ bool result = initialize_shader(device, hwnd, L"src/hlsl/depth.vs", L"src/hlsl/depth.ps");
+ if (!result)
+ {
+ Logger::Get().Log("Failed to initialize depth shader", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ Logger::Get().Log("Initialized depth shader class", __FILE__, __LINE__, Logger::LogLevel::Info);
+
+ return true;
+}
+
+void depth_shader_class::shutdown()
+{
+ shutdown_shader();
+}
+
+bool depth_shader_class::render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix)
+{
+ if (!set_shader_parameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix))
+ return false;
+
+ render_shader(deviceContext, indexCount);
+ return true;
+}
+
+bool depth_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, const WCHAR* vsFilename, const WCHAR* psFilename)
+{
+ HRESULT result;
+ ID3DBlob* vertexShaderBuffer = nullptr;
+ ID3DBlob* pixelShaderBuffer = nullptr;
+ ID3DBlob* errorMessage = nullptr;
+
+ // Compiler le vertex shader
+ result = D3DCompileFromFile(vsFilename, nullptr, nullptr, "DepthVertexShader", "vs_5_0", 0, 0, &vertexShaderBuffer, &errorMessage);
+ if (FAILED(result))
+ {
+ if (errorMessage)
+ {
+ output_shader_error_message(errorMessage, hwnd, vsFilename);
+ errorMessage->Release();
+ }
+ else
+ {
+ Logger::Get().Log("Fichier de vertex shader introuvable: " + std::to_string(reinterpret_cast(vsFilename)), __FILE__, __LINE__, Logger::LogLevel::Error);
+ }
+ return false;
+ }
+
+ // Compiler le pixel shader
+ errorMessage = nullptr;
+ result = D3DCompileFromFile(psFilename, nullptr, nullptr, "DepthPixelShader", "ps_5_0", 0, 0, &pixelShaderBuffer, &errorMessage);
+ if (FAILED(result))
+ {
+ if (errorMessage)
+ {
+ output_shader_error_message(errorMessage, hwnd, psFilename);
+ errorMessage->Release();
+ }
+ else
+ {
+ Logger::Get().Log("Fichier de pixel shader introuvable: " + std::to_string(reinterpret_cast(psFilename)), __FILE__, __LINE__, Logger::LogLevel::Error);
+ }
+
+ if (vertexShaderBuffer)
+ {
+ vertexShaderBuffer->Release();
+ }
+ return false;
+ }
+
+ // Créer les shaders
+ device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), nullptr, &vertex_shader_);
+ device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), nullptr, &pixel_shader_);
+
+ // Définir la description du layout d'entrée
+ D3D11_INPUT_ELEMENT_DESC polygonLayout[1];
+ 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;
+
+ // Créer le layout
+ device->CreateInputLayout(polygonLayout, 1, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &layout_);
+
+ // Libérer les buffers
+ vertexShaderBuffer->Release();
+ pixelShaderBuffer->Release();
+
+ // Créer le buffer de matrices
+ D3D11_BUFFER_DESC matrixBufferDesc = {};
+ matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
+ matrixBufferDesc.ByteWidth = sizeof(MatrixBufferType);
+ matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ device->CreateBuffer(&matrixBufferDesc, nullptr, &matrix_buffer_);
+
+ return true;
+}
+
+bool depth_shader_class::set_shader_parameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix)
+{
+ D3D11_MAPPED_SUBRESOURCE mappedResource;
+ MatrixBufferType* dataPtr;
+
+ // Transposer les matrices pour le shader
+ worldMatrix = XMMatrixTranspose(worldMatrix);
+ viewMatrix = XMMatrixTranspose(viewMatrix);
+ projectionMatrix = XMMatrixTranspose(projectionMatrix);
+
+ // Mapper le buffer
+ HRESULT result = deviceContext->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+ if (FAILED(result))
+ return false;
+
+ dataPtr = (MatrixBufferType*)mappedResource.pData;
+ dataPtr->world = worldMatrix;
+ dataPtr->view = viewMatrix;
+ dataPtr->projection = projectionMatrix;
+
+ deviceContext->Unmap(matrix_buffer_, 0);
+
+ // Définir le buffer de constantes pour le vertex shader
+ deviceContext->VSSetConstantBuffers(0, 1, &matrix_buffer_);
+
+ return true;
+}
+
+void depth_shader_class::render_shader(ID3D11DeviceContext* deviceContext, int indexCount)
+{
+ // Définir le layout d'entrée
+ deviceContext->IASetInputLayout(layout_);
+
+ // Définir les shaders
+ deviceContext->VSSetShader(vertex_shader_, nullptr, 0);
+ deviceContext->PSSetShader(pixel_shader_, nullptr, 0);
+
+ // Dessiner
+ deviceContext->DrawIndexed(indexCount, 0, 0);
+}
+
+void depth_shader_class::shutdown_shader()
+{
+ // Libérer le buffer de matrice
+ if (matrix_buffer_)
+ {
+ matrix_buffer_->Release();
+ matrix_buffer_ = nullptr;
+ }
+
+ // Libérer le layout
+ if (layout_)
+ {
+ layout_->Release();
+ layout_ = nullptr;
+ }
+
+ // Libérer le pixel shader
+ if (pixel_shader_)
+ {
+ pixel_shader_->Release();
+ pixel_shader_ = nullptr;
+ }
+
+ // Libérer le vertex shader
+ if (vertex_shader_)
+ {
+ vertex_shader_->Release();
+ vertex_shader_ = nullptr;
+ }
+}
+
+void depth_shader_class::output_shader_error_message(ID3DBlob* errorMessage, HWND hwnd, const WCHAR* shaderFilename)
+{
+ // Obtenir un pointeur vers le message d'erreur
+ char* compileErrors = (char*)(errorMessage->GetBufferPointer());
+
+ // Obtenir la taille du message
+ SIZE_T bufferSize = errorMessage->GetBufferSize();
+
+ // Allouer de la mémoire pour le message d'erreur formaté
+ char* message = new char[bufferSize + 100];
+
+ // Définir le message d'erreur formaté
+ sprintf_s(message, bufferSize + 100, "Erreur de compilation du shader %ls :\n%s",
+ shaderFilename, compileErrors);
+
+ // Afficher le message d'erreur dans la console
+ Logger::Get().Log(message, __FILE__, __LINE__, Logger::LogLevel::Error);
+
+ // Afficher une boîte de dialogue avec l'erreur
+ MessageBoxA(hwnd, message, "Erreur de Shader", MB_OK);
+
+ // Libérer la mémoire
+ delete[] message;
+}
\ No newline at end of file
diff --git a/enginecustom/src/src/shader/shader_manager_class.cpp b/enginecustom/src/src/shader/shader_manager_class.cpp
index dcdb19c..756f44b 100644
--- a/enginecustom/src/src/shader/shader_manager_class.cpp
+++ b/enginecustom/src/src/shader/shader_manager_class.cpp
@@ -149,6 +149,14 @@ bool shader_manager_class::initialize(ID3D11Device* device, HWND hwnd)
return false;
}
+ depth_shader_ = new depth_shader_class;
+ result = depth_shader_->initialize(device, hwnd);
+ if (!result)
+ {
+ Logger::Get().Log("Error initializing depth_shader_class", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
Logger::Get().Log("shader_manager_class initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
return true;
@@ -268,6 +276,13 @@ void shader_manager_class::shutdown()
skybox_shader_ = 0;
}
+ if (depth_shader_)
+ {
+ skybox_shader_->Shutdown();
+ delete depth_shader_;
+ depth_shader_ = 0;
+ }
+
Logger::Get().Log("shader_manager_class shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
}
@@ -450,12 +465,39 @@ bool shader_manager_class::render_cel_shading_shader(ID3D11DeviceContext* device
return true;
}
-bool shader_manager_class::render_sunlight_shader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
- ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity)
+bool shader_manager_class::render_sunlight_shader(
+ ID3D11DeviceContext* deviceContext,
+ int indexCount,
+ XMMATRIX worldMatrix,
+ XMMATRIX viewMatrix,
+ XMMATRIX projectionMatrix,
+ ID3D11ShaderResourceView* texture,
+ XMFLOAT4 diffuseColor,
+ XMFLOAT4 ambientColor,
+ XMFLOAT3 sunDirection,
+ float sunIntensity,
+ ID3D11ShaderResourceView* shadowMap,
+ XMMATRIX lightViewMatrix,
+ XMMATRIX lightProjectionMatrix
+ )
{
bool result;
- result = sunlight_shader_->render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity);
+ result = sunlight_shader_->render(
+ deviceContext,
+ indexCount,
+ worldMatrix,
+ viewMatrix,
+ projectionMatrix,
+ texture,
+ diffuseColor,
+ ambientColor,
+ sunDirection,
+ sunIntensity,
+ shadowMap,
+ lightViewMatrix,
+ lightProjectionMatrix
+ );
if (!result)
{
return false;
@@ -477,3 +519,16 @@ bool shader_manager_class::render_skybox_shader(ID3D11DeviceContext* deviceConte
return true;
}
+
+bool shader_manager_class::render_depth_shader(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix)
+{
+ bool result;
+
+ result = depth_shader_->render(deviceContext, indexCount, worldMatrix, viewMatrix, projectionMatrix);
+ if (!result)
+ {
+ return false;
+ }
+
+ return true;
+}
\ No newline at end of file
diff --git a/enginecustom/src/src/shader/sunlight_shader_class.cpp b/enginecustom/src/src/shader/sunlight_shader_class.cpp
index da53368..e81c4da 100644
--- a/enginecustom/src/src/shader/sunlight_shader_class.cpp
+++ b/enginecustom/src/src/shader/sunlight_shader_class.cpp
@@ -1,462 +1,552 @@
-////////////////////////////////////////////////////////////////////////////////
-// Filename: lightshaderclass.cpp
-////////////////////////////////////////////////////////////////////////////////
#include "sunlight_shader_class.h"
-
sunlight_shader_class::sunlight_shader_class()
{
- vertex_shader_ = 0;
- pixel_shader_ = 0;
- layout_ = 0;
- sample_state_ = 0;
- matrix_buffer_ = 0;
- camera_buffer_ = 0;
- sunlight_buffer_ = 0;
- sunlight_color_buffer_ = 0;
- sunlight_position_buffer_ = 0;
+ vertex_shader_ = nullptr;
+ pixel_shader_ = nullptr;
+ layout_ = nullptr;
+ sample_state_ = nullptr;
+ matrix_buffer_ = nullptr;
+ camera_buffer_ = nullptr;
+ sunlight_buffer_ = nullptr;
+ sunlight_color_buffer_ = nullptr;
+ sunlight_position_buffer_ = nullptr;
+ light_view_matrix_buffer_ = nullptr;
+ shadow_map_texture_ = nullptr;
+ shadow_sampler_state_ = nullptr;
}
-
-sunlight_shader_class::sunlight_shader_class(const sunlight_shader_class& other)
+sunlight_shader_class::sunlight_shader_class(const sunlight_shader_class&)
{
}
-
sunlight_shader_class::~sunlight_shader_class()
{
}
-
bool sunlight_shader_class::initialize(ID3D11Device* device, HWND hwnd)
{
- Logger::Get().Log("Initializing LightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
+ bool result;
+ WCHAR vs_filename[128];
+ WCHAR ps_filename[128];
- wchar_t vsFilename[128];
- wchar_t psFilename[128];
- int error;
- bool result;
+ // Définir les fichiers des shaders vertex et pixel
+ wcscpy_s(vs_filename, 128, L"./src/hlsl/sunlight.vs");
+ wcscpy_s(ps_filename, 128, L"./src/hlsl/sunlight.ps");
- // Set the filename of the vertex shader.
- error = wcscpy_s(vsFilename, 128, L"src/hlsl/sunlight.vs");
- if (error != 0)
- {
- Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Initialiser les shaders
+ result = initialize_shader(device, hwnd, vs_filename, ps_filename);
+ if (!result)
+ {
+ Logger::Get().Log("Échec d'initialisation des shaders sunlight", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Set the filename of the pixel shader.
- error = wcscpy_s(psFilename, 128, L"src/hlsl/sunlight.ps");
- if (error != 0)
- {
- Logger::Get().Log("Failed to copy string", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
- // initialize the vertex and pixel shaders.
- result = initialize_shader(device, hwnd, vsFilename, psFilename);
- if (!result)
- {
- Logger::Get().Log("Failed to initialize shader", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
-
- Logger::Get().Log("SunLightShaderClass initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
-
- return true;
+ return true;
}
-
void sunlight_shader_class::shutdown()
{
- // shutdown the vertex and pixel shaders as well as the related objects.
- shutdown_shader();
-
- return;
+ shutdown_shader();
+ return;
}
-bool sunlight_shader_class::render(ID3D11DeviceContext* deviceContext, int indexCount, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix,
- ID3D11ShaderResourceView* texture, XMFLOAT4 diffuseColor, XMFLOAT4 ambientColor, XMFLOAT3 sunDirection, float sunIntensity)
+bool sunlight_shader_class::render(ID3D11DeviceContext* device_context, int index_count, XMMATRIX world_matrix, XMMATRIX view_matrix, XMMATRIX projection_matrix, ID3D11ShaderResourceView* texture, XMFLOAT4 diffuse_color, XMFLOAT4 ambient_color, XMFLOAT3 sun_direction, float sun_intensity, ID3D11ShaderResourceView* shadow_map, XMMATRIX light_view_matrix, XMMATRIX light_projection_matrix)
{
- bool result;
+ bool result;
+ // Définir les paramètres du shader
+ result = set_shader_parameters(device_context, world_matrix, view_matrix, projection_matrix, texture, diffuse_color, ambient_color, sun_direction, sun_intensity, shadow_map, light_view_matrix, light_projection_matrix);
+ if (!result)
+ {
+ Logger::Get().Log("Échec de définition des paramètres du shader sunlight", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Set the shader parameters that it will use for rendering.
- result = set_shader_parameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, texture, diffuseColor, ambientColor, sunDirection, sunIntensity);
- if (!result)
- {
- Logger::Get().Log("Failed to set shader parameters", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Rendre le shader
+ render_shader(device_context, index_count);
- // Now render the prepared buffers with the shader.
- render_shader(deviceContext, indexCount);
-
- return true;
+ return true;
}
-
-bool sunlight_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vsFilename, WCHAR* psFilename)
+bool sunlight_shader_class::initialize_shader(ID3D11Device* device, HWND hwnd, WCHAR* vs_filename, WCHAR* ps_filename)
{
- Logger::Get().Log("Initializing shader", __FILE__, __LINE__, Logger::LogLevel::Initialize);
+ HRESULT result;
+ ID3D10Blob* error_message = nullptr;
+ ID3D10Blob* vertex_shader_buffer = nullptr;
+ ID3D10Blob* pixel_shader_buffer = nullptr;
+ D3D11_INPUT_ELEMENT_DESC polygon_layout[3];
+ unsigned int num_elements;
+ D3D11_BUFFER_DESC matrix_buffer_desc;
+ D3D11_BUFFER_DESC camera_buffer_desc;
+ D3D11_BUFFER_DESC light_buffer_desc;
+ D3D11_BUFFER_DESC light_color_buffer_desc;
+ D3D11_BUFFER_DESC light_view_matrix_buffer_desc;
+ D3D11_SAMPLER_DESC sampler_desc;
+ D3D11_SAMPLER_DESC shadow_sampler_desc;
- HRESULT result;
- ID3D10Blob* errorMessage;
- ID3D10Blob* vertexShaderBuffer;
- ID3D10Blob* pixelShaderBuffer;
- D3D11_INPUT_ELEMENT_DESC polygonLayout[3];
- unsigned int numElements;
- D3D11_SAMPLER_DESC samplerDesc;
- D3D11_BUFFER_DESC matrixBufferDesc;
- D3D11_BUFFER_DESC sunlightBufferDesc;
+ // Compiler le vertex shader
+ result = D3DCompileFromFile(vs_filename, NULL, NULL, "SunLightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertex_shader_buffer, &error_message);
+ if (FAILED(result))
+ {
+ if (error_message)
+ {
+ output_shader_error_message(error_message, hwnd, vs_filename);
+ }
+ else
+ {
+ MessageBox(hwnd, vs_filename, L"Fichier shader manquant", MB_OK);
+ }
- // initialize the pointers this function will use to null.
- errorMessage = 0;
- vertexShaderBuffer = 0;
- pixelShaderBuffer = 0;
+ return false;
+ }
- // Compile the vertex shader code.
- result = D3DCompileFromFile(vsFilename, NULL, NULL, "SunLightVertexShader", "vs_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &vertexShaderBuffer, &errorMessage);
- if (FAILED(result))
- {
- if (errorMessage)
- {
- output_shader_error_message(errorMessage, hwnd, vsFilename);
- }
- else
- {
- Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
- }
- return false;
- }
+ // Compiler le pixel shader
+ result = D3DCompileFromFile(ps_filename, NULL, NULL, "SunLightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixel_shader_buffer, &error_message);
+ if (FAILED(result))
+ {
+ if (error_message)
+ {
+ output_shader_error_message(error_message, hwnd, ps_filename);
+ }
+ else
+ {
+ MessageBox(hwnd, ps_filename, L"Fichier shader manquant", MB_OK);
+ }
- // Compile the pixel shader code.
- result = D3DCompileFromFile(psFilename, NULL, NULL, "SunLightPixelShader", "ps_5_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, &pixelShaderBuffer, &errorMessage);
- if (FAILED(result))
- {
- if (errorMessage)
- {
- output_shader_error_message(errorMessage, hwnd, psFilename);
- }
- else
- {
- Logger::Get().Log("Failed to compile shader", __FILE__, __LINE__, Logger::LogLevel::Error);
- }
- return false;
- }
+ return false;
+ }
- // Create the vertex shader from the buffer.
- result = device->CreateVertexShader(vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), NULL, &vertex_shader_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le vertex shader à partir du buffer
+ result = device->CreateVertexShader(vertex_shader_buffer->GetBufferPointer(), vertex_shader_buffer->GetBufferSize(), NULL, &vertex_shader_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du vertex shader", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Create the pixel shader from the buffer.
- result = device->CreatePixelShader(pixelShaderBuffer->GetBufferPointer(), pixelShaderBuffer->GetBufferSize(), NULL, &pixel_shader_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le pixel shader à partir du buffer
+ result = device->CreatePixelShader(pixel_shader_buffer->GetBufferPointer(), pixel_shader_buffer->GetBufferSize(), NULL, &pixel_shader_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du pixel shader", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Create the vertex input layout description.
- 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;
+ // Créer le layout d'entrée du vertex shader
+ polygon_layout[0].SemanticName = "POSITION";
+ polygon_layout[0].SemanticIndex = 0;
+ polygon_layout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
+ polygon_layout[0].InputSlot = 0;
+ polygon_layout[0].AlignedByteOffset = 0;
+ polygon_layout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
+ polygon_layout[0].InstanceDataStepRate = 0;
- polygonLayout[1].SemanticName = "TEXCOORD";
- polygonLayout[1].SemanticIndex = 0;
- 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;
+ polygon_layout[1].SemanticName = "TEXCOORD";
+ polygon_layout[1].SemanticIndex = 0;
+ polygon_layout[1].Format = DXGI_FORMAT_R32G32_FLOAT;
+ polygon_layout[1].InputSlot = 0;
+ polygon_layout[1].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
+ polygon_layout[1].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
+ polygon_layout[1].InstanceDataStepRate = 0;
- polygonLayout[2].SemanticName = "NORMAL";
- polygonLayout[2].SemanticIndex = 0;
- 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;
- polygonLayout[2].InstanceDataStepRate = 0;
+ polygon_layout[2].SemanticName = "NORMAL";
+ polygon_layout[2].SemanticIndex = 0;
+ polygon_layout[2].Format = DXGI_FORMAT_R32G32B32_FLOAT;
+ polygon_layout[2].InputSlot = 0;
+ polygon_layout[2].AlignedByteOffset = D3D11_APPEND_ALIGNED_ELEMENT;
+ polygon_layout[2].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
+ polygon_layout[2].InstanceDataStepRate = 0;
- // Get a count of the elements in the layout.
- numElements = sizeof(polygonLayout) / sizeof(polygonLayout[0]);
+ // Obtenir le nombre d'éléments dans le layout
+ num_elements = sizeof(polygon_layout) / sizeof(polygon_layout[0]);
- // Create the vertex input layout.
- result = device->CreateInputLayout(polygonLayout, numElements, vertexShaderBuffer->GetBufferPointer(), vertexShaderBuffer->GetBufferSize(), &layout_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create input layout", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le layout d'entrée du vertex
+ result = device->CreateInputLayout(polygon_layout, num_elements, vertex_shader_buffer->GetBufferPointer(), vertex_shader_buffer->GetBufferSize(), &layout_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du layout d'entrée", __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 = 0;
+ // Libérer les buffers des shaders
+ vertex_shader_buffer->Release();
+ vertex_shader_buffer = nullptr;
- pixelShaderBuffer->Release();
- pixelShaderBuffer = 0;
+ pixel_shader_buffer->Release();
+ pixel_shader_buffer = nullptr;
- // Create a texture sampler state description.
- samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
- samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
- samplerDesc.MipLODBias = 0.0f;
- samplerDesc.MaxAnisotropy = 1;
- samplerDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
- samplerDesc.BorderColor[0] = 0;
- samplerDesc.BorderColor[1] = 0;
- samplerDesc.BorderColor[2] = 0;
- samplerDesc.BorderColor[3] = 0;
- samplerDesc.MinLOD = 0;
- samplerDesc.MaxLOD = D3D11_FLOAT32_MAX;
+ // Configurer les descripteurs des buffers constants
+ // Descripteur du buffer de matrices
+ matrix_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ matrix_buffer_desc.ByteWidth = sizeof(matrix_buffer_type);
+ matrix_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ matrix_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ matrix_buffer_desc.MiscFlags = 0;
+ matrix_buffer_desc.StructureByteStride = 0;
- // Create the texture sampler state.
- result = device->CreateSamplerState(&samplerDesc, &sample_state_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le buffer constant de matrices
+ result = device->CreateBuffer(&matrix_buffer_desc, NULL, &matrix_buffer_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du buffer constant de matrices", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Setup the description of the dynamic matrix constant buffer that is in the vertex shader.
- matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC;
- matrixBufferDesc.ByteWidth = sizeof(matrix_buffer_type);
- matrixBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- matrixBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- matrixBufferDesc.MiscFlags = 0;
- matrixBufferDesc.StructureByteStride = 0;
+ // Descripteur du buffer de caméra
+ camera_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ camera_buffer_desc.ByteWidth = sizeof(camera_buffer_type);
+ camera_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ camera_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ camera_buffer_desc.MiscFlags = 0;
+ camera_buffer_desc.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, &matrix_buffer_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create matrix buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le buffer constant de caméra
+ result = device->CreateBuffer(&camera_buffer_desc, NULL, &camera_buffer_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du buffer constant de caméra", __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(sun_light_buffer_type);
- sunlightBufferDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
- sunlightBufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
- sunlightBufferDesc.MiscFlags = 0;
- sunlightBufferDesc.StructureByteStride = 0;
+ // Descripteur du buffer de lumière du soleil
+ light_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ light_buffer_desc.ByteWidth = sizeof(sun_light_buffer_type);
+ light_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ light_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ light_buffer_desc.MiscFlags = 0;
+ light_buffer_desc.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, &sunlight_buffer_);
- if (FAILED(result))
- {
- Logger::Get().Log("Failed to create sunlight buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
- return false;
- }
+ // Créer le buffer constant de lumière du soleil
+ result = device->CreateBuffer(&light_buffer_desc, NULL, &sunlight_buffer_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du buffer constant de lumière du soleil", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- Logger::Get().Log("Shader initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize);
+ // Descripteur du buffer de couleur de lumière du soleil
+ light_color_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ light_color_buffer_desc.ByteWidth = sizeof(sun_light_color_buffer_type);
+ light_color_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ light_color_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ light_color_buffer_desc.MiscFlags = 0;
+ light_color_buffer_desc.StructureByteStride = 0;
- return true;
+ // Créer le buffer constant de couleur de lumière du soleil
+ result = device->CreateBuffer(&light_color_buffer_desc, NULL, &sunlight_color_buffer_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du buffer constant de couleur de lumière du soleil", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ // Descripteur du buffer de matrices de vue de lumière
+ light_view_matrix_buffer_desc.Usage = D3D11_USAGE_DYNAMIC;
+ light_view_matrix_buffer_desc.ByteWidth = sizeof(light_view_matrix_buffer_type);
+ light_view_matrix_buffer_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
+ light_view_matrix_buffer_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
+ light_view_matrix_buffer_desc.MiscFlags = 0;
+ light_view_matrix_buffer_desc.StructureByteStride = 0;
+
+ // Créer le buffer constant de matrices de vue de lumière
+ result = device->CreateBuffer(&light_view_matrix_buffer_desc, NULL, &light_view_matrix_buffer_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du buffer constant de matrices de vue de lumière", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ // Créer un sampler state pour la texture
+ sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
+ sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
+ sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
+ sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
+ sampler_desc.MipLODBias = 0.0f;
+ sampler_desc.MaxAnisotropy = 1;
+ sampler_desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+ sampler_desc.BorderColor[0] = 0;
+ sampler_desc.BorderColor[1] = 0;
+ sampler_desc.BorderColor[2] = 0;
+ sampler_desc.BorderColor[3] = 0;
+ sampler_desc.MinLOD = 0;
+ sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
+
+ // Créer le sampler state
+ result = device->CreateSamplerState(&sampler_desc, &sample_state_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du sampler state", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ // Créer un sampler state pour la shadow map
+ shadow_sampler_desc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT;
+ shadow_sampler_desc.AddressU = D3D11_TEXTURE_ADDRESS_BORDER;
+ shadow_sampler_desc.AddressV = D3D11_TEXTURE_ADDRESS_BORDER;
+ shadow_sampler_desc.AddressW = D3D11_TEXTURE_ADDRESS_BORDER;
+ shadow_sampler_desc.MipLODBias = 0.0f;
+ shadow_sampler_desc.MaxAnisotropy = 1;
+ shadow_sampler_desc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
+ shadow_sampler_desc.BorderColor[0] = 1.0f;
+ shadow_sampler_desc.BorderColor[1] = 1.0f;
+ shadow_sampler_desc.BorderColor[2] = 1.0f;
+ shadow_sampler_desc.BorderColor[3] = 1.0f;
+ shadow_sampler_desc.MinLOD = 0;
+ shadow_sampler_desc.MaxLOD = D3D11_FLOAT32_MAX;
+
+ // Créer le sampler state pour la shadow map
+ result = device->CreateSamplerState(&shadow_sampler_desc, &shadow_sampler_state_);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec de création du sampler state pour la shadow map", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ return true;
}
-
-
-
void sunlight_shader_class::shutdown_shader()
{
- Logger::Get().Log("Shutting down SunLightShaderClass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
+ // Libérer tous les objets
+ if (shadow_sampler_state_)
+ {
+ shadow_sampler_state_->Release();
+ shadow_sampler_state_ = nullptr;
+ }
- // Release the light constant buffers.
- if (sunlight_color_buffer_)
- {
- sunlight_color_buffer_->Release();
- sunlight_color_buffer_ = 0;
- }
+ if (light_view_matrix_buffer_)
+ {
+ light_view_matrix_buffer_->Release();
+ light_view_matrix_buffer_ = nullptr;
+ }
- if (sunlight_position_buffer_)
- {
- sunlight_position_buffer_->Release();
- sunlight_position_buffer_ = 0;
- }
+ if (sunlight_color_buffer_)
+ {
+ sunlight_color_buffer_->Release();
+ sunlight_color_buffer_ = nullptr;
+ }
- // Release the light constant buffer.
- if (sunlight_buffer_)
- {
- sunlight_buffer_->Release();
- sunlight_buffer_ = 0;
- }
+ if (sunlight_buffer_)
+ {
+ sunlight_buffer_->Release();
+ sunlight_buffer_ = nullptr;
+ }
- // Release the camera constant buffer.
- if (camera_buffer_)
- {
- camera_buffer_->Release();
- camera_buffer_ = 0;
- }
+ if (camera_buffer_)
+ {
+ camera_buffer_->Release();
+ camera_buffer_ = nullptr;
+ }
- // Release the matrix constant buffer.
- if (matrix_buffer_)
- {
- matrix_buffer_->Release();
- matrix_buffer_ = 0;
- }
+ if (matrix_buffer_)
+ {
+ matrix_buffer_->Release();
+ matrix_buffer_ = nullptr;
+ }
- // Release the sampler state.
- if (sample_state_)
- {
- sample_state_->Release();
- sample_state_ = 0;
- }
+ if (sample_state_)
+ {
+ sample_state_->Release();
+ sample_state_ = nullptr;
+ }
- // Release the layout.
- if (layout_)
- {
- layout_->Release();
- layout_ = 0;
- }
+ if (layout_)
+ {
+ layout_->Release();
+ layout_ = nullptr;
+ }
- // Release the pixel shader.
- if (pixel_shader_)
- {
- pixel_shader_->Release();
- pixel_shader_ = 0;
- }
+ if (pixel_shader_)
+ {
+ pixel_shader_->Release();
+ pixel_shader_ = nullptr;
+ }
- // Release the vertex shader.
- if (vertex_shader_)
- {
- vertex_shader_->Release();
- vertex_shader_ = 0;
- }
+ if (vertex_shader_)
+ {
+ vertex_shader_->Release();
+ vertex_shader_ = nullptr;
+ }
- Logger::Get().Log("SunLightShaderClass shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
-
- return;
+ return;
}
-
-void sunlight_shader_class::output_shader_error_message(ID3D10Blob* errorMessage, HWND hwnd, WCHAR* shaderFilename)
+void sunlight_shader_class::output_shader_error_message(ID3D10Blob* error_message, HWND hwnd, WCHAR* shader_filename)
{
- char* compileErrors;
- unsigned __int64 bufferSize, i;
- ofstream fout;
+ char* compile_errors;
+ unsigned long long buffer_size;
+ ofstream fout;
+ // Obtenir un pointeur vers le texte d'erreur dans le buffer d'erreur
+ compile_errors = (char*)(error_message->GetBufferPointer());
- // Get a pointer to the error message text buffer.
- compileErrors = (char*)(errorMessage->GetBufferPointer());
+ // Obtenir la taille du message
+ buffer_size = error_message->GetBufferSize();
- // Get the length of the message.
- bufferSize = errorMessage->GetBufferSize();
+ // Ouvrir un fichier pour écrire les messages d'erreur
+ fout.open("shader-error.txt");
- // Open a file to write the error message to.
- fout.open("shader-error.txt");
+ // Écrire le message d'erreur
+ for (unsigned long long i = 0; i < buffer_size; i++)
+ {
+ fout << compile_errors[i];
+ }
- // Write out the error message.
- for (i = 0; i < bufferSize; i++)
- {
- fout << compileErrors[i];
- }
+ // Fermer le fichier
+ fout.close();
- // Close the file.
- fout.close();
+ // Libérer le buffer d'erreur
+ error_message->Release();
+ error_message = nullptr;
- // Release the error message.
- errorMessage->Release();
- errorMessage = 0;
+ // Afficher un message d'erreur
+ MessageBox(hwnd, L"Erreur de compilation du shader. Voir shader-error.txt pour plus de détails.", shader_filename, MB_OK);
- // 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);
-
- return;
+ return;
}
-
-bool sunlight_shader_class::set_shader_parameters(ID3D11DeviceContext* deviceContext, XMMATRIX worldMatrix, XMMATRIX viewMatrix, XMMATRIX projectionMatrix, ID3D11ShaderResourceView* texture, XMFLOAT4 ambientColor, XMFLOAT4 diffuseColor, XMFLOAT3 lightDirection, float sunIntensity)
+bool sunlight_shader_class::set_shader_parameters(ID3D11DeviceContext* device_context, XMMATRIX world_matrix, XMMATRIX view_matrix, XMMATRIX projection_matrix, ID3D11ShaderResourceView* texture, XMFLOAT4 diffuse_color, XMFLOAT4 ambient_color, XMFLOAT3 sun_direction, float sun_intensity, ID3D11ShaderResourceView* shadow_map, XMMATRIX light_view_matrix, XMMATRIX light_projection_matrix)
{
- HRESULT result;
- D3D11_MAPPED_SUBRESOURCE mappedResource;
- matrix_buffer_type* dataPtr;
- camera_buffer_type* dataPtr2;
- sun_light_buffer_type* dataPtr3;
- unsigned int bufferNumber;
+ HRESULT result;
+ D3D11_MAPPED_SUBRESOURCE mapped_resource;
+ matrix_buffer_type* data_ptr;
+ camera_buffer_type* camera_data_ptr;
+ sun_light_buffer_type* light_data_ptr;
+ sun_light_color_buffer_type* light_color_data_ptr;
+ light_view_matrix_buffer_type* light_view_data_ptr;
- // Transpose the matrices to prepare them for the shader.
- worldMatrix = XMMatrixTranspose(worldMatrix);
- viewMatrix = XMMatrixTranspose(viewMatrix);
- projectionMatrix = XMMatrixTranspose(projectionMatrix);
+ // Transposer les matrices pour qu'elles soient prêtes pour le shader
+ world_matrix = XMMatrixTranspose(world_matrix);
+ view_matrix = XMMatrixTranspose(view_matrix);
+ projection_matrix = XMMatrixTranspose(projection_matrix);
+ light_view_matrix = XMMatrixTranspose(light_view_matrix);
+ light_projection_matrix = XMMatrixTranspose(light_projection_matrix);
- // Lock the constant buffer so it can be written to.
- result = deviceContext->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
- if (FAILED(result))
- {
- return false;
- }
+ // Verrouiller le buffer constant pour l'écriture
+ result = device_context->Map(matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec du verrouillage du buffer constant de matrices", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Get a pointer to the data in the constant buffer.
- dataPtr = (matrix_buffer_type*)mappedResource.pData;
+ // Obtenir un pointeur vers les données
+ data_ptr = (matrix_buffer_type*)mapped_resource.pData;
- // Copy the matrices into the constant buffer.
- dataPtr->world = worldMatrix;
- dataPtr->view = viewMatrix;
- dataPtr->projection = projectionMatrix;
+ // Copier les matrices dans le buffer constant
+ data_ptr->world = world_matrix;
+ data_ptr->view = view_matrix;
+ data_ptr->projection = projection_matrix;
- // Unlock the constant buffer.
- deviceContext->Unmap(matrix_buffer_, 0);
+ // Déverrouiller le buffer constant
+ device_context->Unmap(matrix_buffer_, 0);
- // Set the position of the constant buffer in the vertex shader.
- bufferNumber = 0;
+ // Définir la position du buffer constant dans le vertex shader
+ unsigned int buffer_number = 0;
- // Finally set the constant buffer in the vertex shader with the updated values.
- deviceContext->VSSetConstantBuffers(bufferNumber, 1, &matrix_buffer_);
+ // Définir le buffer constant du vertex shader
+ device_context->VSSetConstantBuffers(buffer_number, 1, &matrix_buffer_);
- // Lock the sunlight constant buffer so it can be written to.
- result = deviceContext->Map(sunlight_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
- if (FAILED(result))
- {
- return false;
- }
+ // Verrouiller le buffer constant de caméra
+ result = device_context->Map(camera_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec du verrouillage du buffer constant de caméra", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- // Get a pointer to the data in the constant buffer.
- dataPtr3 = (sun_light_buffer_type*)mappedResource.pData;
+ // Obtenir un pointeur vers les données
+ camera_data_ptr = (camera_buffer_type*)mapped_resource.pData;
- // Copy the lighting variables into the constant buffer.
- dataPtr3->ambient_color = ambientColor;
- dataPtr3->diffuse_color = diffuseColor;
- dataPtr3->sun_direction = lightDirection;
- dataPtr3->intensity = sunIntensity;
+ // TODO: Ajouter la position de la caméra ici si nécessaire
+ camera_data_ptr->camera_position = XMFLOAT3(0.0f, 0.0f, 0.0f);
+ camera_data_ptr->padding = 0.0f;
- // Unlock the constant buffer.
- deviceContext->Unmap(sunlight_buffer_, 0);
+ // Déverrouiller le buffer constant
+ device_context->Unmap(camera_buffer_, 0);
- // Set the position of the sunlight constant buffer in the pixel shader.
- bufferNumber = 0;
+ // Définir la position du buffer constant dans le vertex shader
+ buffer_number = 1;
- // Finally set the sunlight constant buffer in the pixel shader with the updated values.
- deviceContext->PSSetConstantBuffers(bufferNumber, 1, &sunlight_buffer_);
+ // Définir le buffer constant du vertex shader
+ device_context->VSSetConstantBuffers(buffer_number, 1, &camera_buffer_);
- // Set shader texture resource in the pixel shader.
- deviceContext->PSSetShaderResources(0, 1, &texture);
+ // Verrouiller le buffer constant de lumière
+ result = device_context->Map(sunlight_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec du verrouillage du buffer constant de lumière", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
- return true;
+ // Obtenir un pointeur vers les données
+ light_data_ptr = (sun_light_buffer_type*)mapped_resource.pData;
+
+ // Copier les paramètres de lumière dans le buffer constant
+ light_data_ptr->ambient_color = ambient_color;
+ light_data_ptr->diffuse_color = diffuse_color;
+ light_data_ptr->sun_direction = sun_direction;
+ light_data_ptr->intensity = sun_intensity;
+
+ // Déverrouiller le buffer constant
+ device_context->Unmap(sunlight_buffer_, 0);
+
+ // Définir la position du buffer constant dans le pixel shader
+ buffer_number = 0;
+
+ // Définir le buffer constant du pixel shader
+ device_context->PSSetConstantBuffers(buffer_number, 1, &sunlight_buffer_);
+ device_context->VSSetConstantBuffers(buffer_number, 1, &sunlight_buffer_);
+
+ // Verrouiller le buffer constant de matrices de vue de lumière
+ result = device_context->Map(light_view_matrix_buffer_, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped_resource);
+ if (FAILED(result))
+ {
+ Logger::Get().Log("Échec du verrouillage du buffer constant de matrices de vue de lumière", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ // Obtenir un pointeur vers les données
+ light_view_data_ptr = (light_view_matrix_buffer_type*)mapped_resource.pData;
+
+ // Copier les matrices de vue de lumière dans le buffer constant
+ light_view_data_ptr->light_view = light_view_matrix;
+ light_view_data_ptr->light_projection = light_projection_matrix;
+
+ // Déverrouiller le buffer constant
+ device_context->Unmap(light_view_matrix_buffer_, 0);
+
+ // Définir la position du buffer constant dans le vertex shader
+ buffer_number = 2;
+
+ // Définir le buffer constant du vertex shader
+ device_context->VSSetConstantBuffers(buffer_number, 1, &light_view_matrix_buffer_);
+
+ // Définir la ressource de texture du shader
+ device_context->PSSetShaderResources(0, 1, &texture);
+
+ // Définir la ressource de shadow map du shader
+ device_context->PSSetShaderResources(1, 1, &shadow_map);
+
+ // Définir les sampler states dans le pixel shader
+ device_context->PSSetSamplers(0, 1, &sample_state_);
+ device_context->PSSetSamplers(1, 1, &shadow_sampler_state_);
+
+ return true;
}
-void sunlight_shader_class::render_shader(ID3D11DeviceContext* deviceContext, int indexCount)
+void sunlight_shader_class::render_shader(ID3D11DeviceContext* device_context, int index_count)
{
- // Set the vertex input layout.
- deviceContext->IASetInputLayout(layout_);
+ // Définir le layout du vertex input
+ device_context->IASetInputLayout(layout_);
- // Set the vertex and pixel shaders that will be used to render this triangle.
- deviceContext->VSSetShader(vertex_shader_, NULL, 0);
- deviceContext->PSSetShader(pixel_shader_, NULL, 0);
+ // Définir les shaders vertex et pixel
+ device_context->VSSetShader(vertex_shader_, NULL, 0);
+ device_context->PSSetShader(pixel_shader_, NULL, 0);
- // Set the sampler state in the pixel shader.
- deviceContext->PSSetSamplers(0, 1, &sample_state_);
+ // Rendre la géométrie
+ device_context->DrawIndexed(index_count, 0, 0);
- // render the triangle.
- deviceContext->DrawIndexed(indexCount, 0, 0);
-
- return;
-}
+ return;
+}
\ No newline at end of file
diff --git a/enginecustom/src/src/system/application_class.cpp b/enginecustom/src/src/system/application_class.cpp
index b291414..1d09277 100644
--- a/enginecustom/src/src/system/application_class.cpp
+++ b/enginecustom/src/src/system/application_class.cpp
@@ -126,6 +126,14 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
camera_->set_rotation(0.0f, 0.0f, 0.0f);
camera_->render();
camera_->get_view_matrix(base_view_matrix_);
+
+ // create the ligth camera
+ light_camera_ = new camera_class;
+ if (!light_camera_)
+ {
+ Logger::Get().Log("Could not create the light camera object", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
// Create and initialize the font shader object.
font_shader_ = new font_shader_class;
@@ -465,6 +473,13 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
return false;
}
+ shadow_map_ = new shadow_map();
+ if (!shadow_map_->initialize(direct_3d_->get_device(), screenWidth, screenHeight))
+ {
+ Logger::Get().Log("Could not create the shadow map object", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
physics_ = new physics;
physics_thread_ = std::thread(&application_class::physics_thread_function, this);
@@ -493,6 +508,12 @@ bool application_class::initialize(int screenWidth, int screenHeight, HWND hwnd,
void application_class::shutdown()
{
Logger::Get().Log("Shutting down application class", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
+
+ if (shadow_map_) {
+ shadow_map_->shutdown();
+ delete shadow_map_;
+ shadow_map_ = nullptr;
+ }
// Release the shader manager object.
if (shader_manager_)
@@ -1018,11 +1039,11 @@ bool application_class::render(float rotation, float x, float y, float z, float
// Redimensionner la texture de rendu si nécessaire
if (DEBUG_MODE)
{
- if ((float)scene_texture_->GetTextureWidth() != window_size_.x || (float)scene_texture_->GetTextureHeight() != window_size_.y)
- {
- scene_texture_->Shutdown();
- scene_texture_->Initialize(direct_3d_->get_device(), (int)window_size_.x, (int)window_size_.y, screen_depth, screen_near, 1);
- }
+ // if ((float)scene_texture_->GetTextureWidth() != window_size_.x || (float)scene_texture_->GetTextureHeight() != window_size_.y)
+ // {
+ // scene_texture_->Shutdown();
+ // scene_texture_->Initialize(direct_3d_->get_device(), (int)window_size_.x, (int)window_size_.y, screen_depth, screen_near, 1);
+ // }
direct_3d_->set_back_buffer_render_target();
direct_3d_->reset_viewport();
@@ -1053,9 +1074,51 @@ bool application_class::render(float rotation, float x, float y, float z, float
direct_3d_->turn_z_buffer_on(); // Enable the Z buffer after rendering the skybox.
+
+ // --------------------------------------------------------- //
+ // ------------------ shadow map creation ------------------ //
+ // --------------------------------------------------------- //
+
+ shadow_map_->set_render_target(direct_3d_->get_device_context());
+ shadow_map_->clear_render_target(direct_3d_->get_device_context(), 1.0f);
+
+ // Configuration de la caméra de lumière
+ light_position_buffer_ = sun_light_->GetPosition();
+ light_rotation_buffer_ = sun_light_->GetDirection();
+
+ // Positionnez la caméra de lumière à une position appropriée pour votre scène
+ float lightDistance = 100.0f;
+ light_camera_->set_position(-light_rotation_buffer_.x * lightDistance, -light_rotation_buffer_.y * lightDistance, -light_rotation_buffer_.z * lightDistance);
+ light_camera_->set_rotation(light_rotation_buffer_.x, light_rotation_buffer_.y, light_rotation_buffer_.z);
+ light_camera_->render();
+
+ // Récupération explicite de la matrice de vue
+ light_camera_->get_view_matrix(light_view_matrix_);
+
+ // Création d'une matrice de projection orthographique avec des valeurs adaptées
+ float lightViewWidth = 200.0f; // Agrandir la zone couverte par l'ombre
+ float lightViewHeight = 200.0f;
+ float lightNearPlane = 1.0f;
+ float lightFarPlane = 300.0f; // Augmenter la profondeur
+ light_projection_matrix_ = XMMatrixOrthographicLH(lightViewWidth, lightViewHeight, lightNearPlane, lightFarPlane);
+
+
+ // Rendu de la scene sans texture juste geometrie.
+ if (!create_shadow_map(render_queues_))
+ {
+ Logger::Get().Log("Error creating shadow map", __FILE__, __LINE__, Logger::LogLevel::Error);
+ }
+
+ shadow_srv_ = shadow_map_->get_shader_resource_view();
+
+ // reset the render target back to the original back buffer and not the render to texture anymore. And reset the viewport back to the original.
+ direct_3d_->set_back_buffer_render_target();
+ direct_3d_->reset_viewport();
+
// -------------------------------------------------------- //
// ------------ render the object in the queue ------------ //
// -------------------------------------------------------- //
+
result = render_pass(render_queues_, diffuseColor, lightPosition, ambientColor, viewMatrix, projectionMatrix);
if (!result)
{
@@ -1871,8 +1934,11 @@ bool application_class::render_pass(const std::vectorGetDiffuseColor(),
sun_light_->GetAmbientColor(),
sun_light_->GetDirection(),
- sun_light_->GetIntensity()
- );
+ sun_light_->GetIntensity(),
+ shadow_srv_,
+ light_view_matrix_,
+ light_projection_matrix_
+ );
if (!result)
{
Logger::Get().Log("Could not render the object model using the sunlight shader", __FILE__, __LINE__, Logger::LogLevel::Error);
@@ -1914,6 +1980,76 @@ bool application_class::render_pass(const std::vector>>& RenderQueues)
+{
+ for (auto& RenderQueue : RenderQueues)
+ {
+
+ if (&RenderQueue.get() == &skybox_)
+ {
+ continue; // Skip rendering the skybox_ for shadow mapping
+ }
+
+ for (auto& object : RenderQueue.get())
+ {
+
+ // Vérification des ressources critiques
+ if (shadow_map_ == nullptr) {
+ Logger::Get().Log("shadow_map_ est nul!", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ if (light_camera_ == nullptr) {
+ Logger::Get().Log("light_camera_ est nul!", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ bool hasNaN = false;
+ for (int i = 0; i < 4; i++) {
+ XMVECTOR row = light_view_matrix_.r[i];
+ if (XMComparisonAnyTrue(XMVector4IsNaN(row))) {
+ hasNaN = true;
+ break;
+ }
+ }
+
+ if (hasNaN) {
+ Logger::Get().Log("light_view_matrix_ contient des valeurs invalides!", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+
+ if (object == nullptr)
+ {
+ Logger::Get().Log("object is null", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ if (!object->IsVisible())
+ {
+ continue;
+ }
+
+ object->Render(direct_3d_->get_device_context());
+
+ bool result = shader_manager_->render_depth_shader(
+ direct_3d_->get_device_context(),
+ object->GetIndexCount(),
+ object->GetTranslateMatrix(),
+ light_view_matrix_,
+ light_projection_matrix_
+ );
+ if (!result)
+ {
+ Logger::Get().Log("Could not render the object model using the shadow map shader", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
void application_class::construct_frustum()
{
XMMATRIX projectionMatrix = direct_3d_->get_projection_matrix();
diff --git a/enginecustom/src/src/system/shadow_map.cpp b/enginecustom/src/src/system/shadow_map.cpp
new file mode 100644
index 0000000..7fe549b
--- /dev/null
+++ b/enginecustom/src/src/system/shadow_map.cpp
@@ -0,0 +1,105 @@
+#include "shadow_map.h"
+
+#include "Logger.h"
+
+shadow_map::shadow_map() :
+ depth_texture_(nullptr),
+ depth_stencil_view_(nullptr),
+ shader_resource_view_(nullptr)
+{
+ ZeroMemory(&viewport_, sizeof(viewport_));
+}
+
+shadow_map::~shadow_map()
+{
+ shutdown();
+}
+
+bool shadow_map::initialize(ID3D11Device* device, int shadow_map_width, int shadow_map_height)
+{
+ D3D11_TEXTURE2D_DESC text_desc = {};
+ text_desc.Width = shadow_map_width;
+ text_desc.Height = shadow_map_height;
+ text_desc.MipLevels = 1;
+ text_desc.ArraySize = 1;
+ text_desc.Format = DXGI_FORMAT_R32_TYPELESS;
+ text_desc.SampleDesc.Count = 1;
+ text_desc.Usage = D3D11_USAGE_DEFAULT;
+ text_desc.BindFlags = D3D11_BIND_DEPTH_STENCIL | D3D11_BIND_SHADER_RESOURCE;
+
+ if (FAILED(device->CreateTexture2D(&text_desc, nullptr, &depth_texture_)))
+ {
+ Logger::Get().Log("Failed to create depth texture", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ D3D11_DEPTH_STENCIL_VIEW_DESC dsv_Desc = {};
+ dsv_Desc.Format = DXGI_FORMAT_D32_FLOAT;
+ dsv_Desc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
+ dsv_Desc.Texture2D.MipSlice = 0;
+
+ if (FAILED(device->CreateDepthStencilView(depth_texture_, &dsv_Desc, &depth_stencil_view_)))
+ {
+ Logger::Get().Log("Failed to create depth texture", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ D3D11_SHADER_RESOURCE_VIEW_DESC srv_desc = {};
+ srv_desc.Format = DXGI_FORMAT_R32_FLOAT;
+ srv_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
+ srv_desc.Texture2D.MostDetailedMip = 0;
+ srv_desc.Texture2D.MipLevels = 1;
+
+ if (FAILED(device->CreateShaderResourceView(depth_texture_, &srv_desc, &shader_resource_view_)))
+ {
+ Logger::Get().Log("Failed to create shader resource view", __FILE__, __LINE__, Logger::LogLevel::Error);
+ return false;
+ }
+
+ viewport_.Width = static_cast(shadow_map_width);
+ viewport_.Height = static_cast(shadow_map_height);
+ viewport_.MinDepth = 0.0f;
+ viewport_.MaxDepth = 1.0f;
+ viewport_.TopLeftX = 0.0f;
+ viewport_.TopLeftY = 0.0f;
+
+ Logger::Get().Log("Shadow map initialized", __FILE__, __LINE__, Logger::LogLevel::Info);
+
+ return true;
+
+}
+
+void shadow_map::set_render_target(ID3D11DeviceContext* context)
+{
+ context->OMSetRenderTargets(0, nullptr, depth_stencil_view_);
+ context->RSSetViewports(1, &viewport_);
+}
+
+void shadow_map::clear_render_target(ID3D11DeviceContext* context, float depth)
+{
+ context->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH, depth, 0);
+}
+
+ID3D11ShaderResourceView* shadow_map::get_shader_resource_view() const
+{
+ return shader_resource_view_;
+}
+
+void shadow_map::shutdown()
+{
+
+ if (shader_resource_view_) {
+ shader_resource_view_->Release();
+ shader_resource_view_ = nullptr;
+ }
+ if (depth_stencil_view_) {
+ depth_stencil_view_->Release();
+ depth_stencil_view_ = nullptr;
+ }
+ if (depth_texture_) {
+ depth_texture_->Release();
+ depth_texture_ = nullptr;
+ }
+
+ Logger::Get().Log("Shadow map shut down", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
+}
\ No newline at end of file