diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 26090aa..a5a9355 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -62,7 +62,6 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_RenderQueues.push_back(std::ref(m_object)); m_RenderQueues.push_back(std::ref(m_cubes)); m_RenderQueues.push_back(std::ref(m_terrainChunk)); - m_RenderQueues.push_back(std::ref(m_Skybox)); m_screenWidth = screenWidth; m_screenHeight = screenHeight; @@ -992,7 +991,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t m_SceneTexture->SetRenderTarget(m_Direct3D->GetDeviceContext()); m_SceneTexture->ClearRenderTarget(m_Direct3D->GetDeviceContext(), 0.0f, 0.0f, 0.0f, 1.0f); - //Add the 3 first value of the first light position to the TrueLightPosition XMFLOAT3 positionX = lightPosition[0].x; positionY = lightPosition[0].y; @@ -1014,6 +1012,23 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0), diffuseColor, lightPosition, ambientColor); + UpdateSkyboxPosition(); // Update the position of the skybox to match the camera position. + + // ------------------------------------------------------------- // + // --------------------- Render the skybox --------------------- // + // ------------------------------------------------------------- // + + m_Direct3D->TurnZBufferOff(); // Disable the Z buffer for rendering the skybox. + + result = RenderSkybox(viewMatrix, projectionMatrix); // Render the skybox. + if (!result) + { + Logger::Get().Log("Could not render the skybox", __FILE__, __LINE__, Logger::LogLevel::Error); + return false; + } + + m_Direct3D->TurnZBufferOn(); // Enable the Z buffer after rendering the skybox. + // -------------------------------------------------------- // // ------------ Render the object in the queue ------------ // // -------------------------------------------------------- // @@ -1169,6 +1184,9 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z, float t return false; } + // turn on the Z buffer + m_Direct3D->TurnZBufferOn(); + // Render the model using the multitexture shader. m_Model->Render(m_Direct3D->GetDeviceContext()); @@ -1824,6 +1842,12 @@ bool ApplicationClass::RenderPass(const std::vectorTurnZBufferOff(); + } + for (auto& object : RenderQueue.get()) { if (object == nullptr) @@ -1891,8 +1915,22 @@ bool ApplicationClass::RenderPass(const std::vectorRenderTextureShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0)); + if (!result) + { + Logger::Get().Log("Could not render the model using the texture shader", __FILE__, __LINE__, Logger::LogLevel::Error); + return false; + } case Object::LIGHTING: + result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, + object->GetTexture(0), diffuse, position, ambient); + if (!result) + { + Logger::Get().Log("Could not render the object model using the light shader", __FILE__, __LINE__, Logger::LogLevel::Error); + return false; + } + break; default: result = m_ShaderManager->RenderlightShader(m_Direct3D->GetDeviceContext(), object->GetIndexCount(), worldMatrix, view, projection, object->GetTexture(0), diffuse, position, ambient); @@ -1912,12 +1950,13 @@ bool ApplicationClass::RenderPass(const std::vector skyboxTexture = { + strcpy_s(modelFilename, "assets/Model/OBJ/plane.obj"); + + // Liste des fichiers de texture pour chaque face du skybox + std::vector skyboxTextures = { L"assets/Skybox/skybox_front.png", L"assets/Skybox/skybox_back.png", L"assets/Skybox/skybox_left.png", @@ -1925,24 +1964,53 @@ void ApplicationClass::ConstructSkybox() { L"assets/Skybox/skybox_top.png", L"assets/Skybox/skybox_bottom.png" }; - textures.clear(); - for (const auto& textureFilename : skyboxTexture) + + // Charger les textures + std::vector textures; + for (const auto& textureFilename : skyboxTextures) { ID3D11ShaderResourceView* texture = nullptr; HRESULT result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); if (FAILED(result)) { - Logger::Get().Log("Failed to load texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); + Logger::Get().Log("Failed to load skybox texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); return; } textures.push_back(texture); } - Object* newSkybox = new Object(); - newSkybox->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textures); - newSkybox->SetScaleMatrix(XMMatrixScaling(100.0f, 100.0f, 100.0f)); - newSkybox->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); - newSkybox->SetType(ObjectType::Cube); - m_object.push_back(newSkybox); + + // Créer les 6 faces du skybox + std::vector translations = { + XMMatrixTranslation(0.0f, 0.0f, 1.0f), // Front + XMMatrixTranslation(0.0f, 0.0f, -1.0f), // Back + XMMatrixTranslation(-1.0f, 0.0f, 0.0f), // Left + XMMatrixTranslation(1.0f, 0.0f, 0.0f), // Right + XMMatrixTranslation(0.0f, 1.0f, 0.0f), // Top + XMMatrixTranslation(0.0f, -1.0f, 0.0f) // Bottom + }; + + std::vector rotations = { + XMMatrixRotationRollPitchYaw(-XM_PIDIV2, 0, 0.0f), // Front + XMMatrixRotationRollPitchYaw(XM_PIDIV2, 0, 0.0f), // Back + XMMatrixRotationRollPitchYaw(XM_PIDIV2, XM_PIDIV2, 0.0f), // Left + XMMatrixRotationRollPitchYaw(XM_PIDIV2, -XM_PIDIV2, 0.0f), // Right + XMMatrixRotationRollPitchYaw(XM_PI, 0.0f, 0.0f), // top + XMMatrixRotationRollPitchYaw(0.0f, 0.0f, 0.0f) // Bottom + }; + + for (int i = 0; i < 6; ++i) + { + Object* face = new Object(); + face->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, { textures[i] }); + face->SetScaleMatrix(XMMatrixScaling(2.01f, 2.01f, 2.01f)); + face->SetRotateMatrix(rotations[i]); + face->SetTranslateMatrix(translations[i]); + face->SetActiveShader(Object::SKYBOX); + m_Skybox.push_back(face); + m_SkyboxInitialTranslations.push_back(translations[i]); + } + + Logger::Get().Log("Skybox initialized", __FILE__, __LINE__, Logger::LogLevel::Initialize); } void ApplicationClass::ConstructFrustum() @@ -2059,29 +2127,43 @@ void ApplicationClass::PhysicsThreadFunction() } } -bool ApplicationClass::LoadSkyboxTextures() -{ - std::vector skyboxTextures = { - L"assets/Skybox/skybox_front.png", - L"assets/Skybox/skybox_back.png", - L"assets/Skybox/skybox_left.png", - L"assets/Skybox/skybox_right.png", - L"assets/Skybox/skybox_top.png", - L"assets/Skybox/skybox_bottom.png" - }; +void ApplicationClass::UpdateSkyboxPosition() { + XMFLOAT3 cameraPositionFloat3 = m_Camera->GetPosition(); + XMVECTOR cameraPosition = XMLoadFloat3(&cameraPositionFloat3); + for (size_t i = 0; i < m_Skybox.size(); ++i) { + Object* face = m_Skybox[i]; + XMMATRIX initialTranslation = m_SkyboxInitialTranslations[i]; + XMVECTOR initialTranslationVector = initialTranslation.r[3]; + XMVECTOR newTranslation = XMVectorAdd(cameraPosition, initialTranslationVector); + XMMATRIX translateMatrix = XMMatrixTranslationFromVector(newTranslation); + face->SetTranslateMatrix(translateMatrix); + } +} - for (const auto& textureFilename : skyboxTextures) - { - ID3D11ShaderResourceView* texture = nullptr; - HRESULT result = DirectX::CreateWICTextureFromFile(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), textureFilename.c_str(), nullptr, &texture); - if (FAILED(result)) - { - Logger::Get().Log("Failed to load skybox texture: " + std::string(textureFilename.begin(), textureFilename.end()), __FILE__, __LINE__, Logger::LogLevel::Error); +bool ApplicationClass::RenderSkybox(XMMATRIX view, XMMATRIX projection) { + + bool result; + XMMATRIX worldMatrix, scaleMatrix, rotateMatrix, translateMatrix, srMatrix; + + for (auto& face : m_Skybox) { + if (face == nullptr) { + Logger::Get().Log("Skybox face is null", __FILE__, __LINE__, Logger::LogLevel::Error); + return false; + } + scaleMatrix = face->GetScaleMatrix(); + rotateMatrix = face->GetRotateMatrix(); + translateMatrix = face->GetTranslateMatrix(); + srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); + worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); + face->Render(m_Direct3D->GetDeviceContext()); + result = m_ShaderManager->RenderTextureShader(m_Direct3D->GetDeviceContext(), face->GetIndexCount(), worldMatrix, view, projection, face->GetTexture(0)); + if (!result) + { + Logger::Get().Log("Could not render the model using the texture shader", __FILE__, __LINE__, Logger::LogLevel::Error); return false; } - m_SkyboxTextures.push_back(texture); } - Logger::Get().Log("Loaded " + std::to_string(m_SkyboxTextures.size()) + " skybox textures", __FILE__, __LINE__, Logger::LogLevel::Info); + return true; } \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index af18fa1..61d78d9 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -146,8 +146,9 @@ private: bool RenderReflectionToTexture(); bool RenderPass(const std::vector>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection); - bool LoadSkyboxTextures(); - void ConstructSkybox(); + void ConstructSkybox(); // Construct the skybox + void UpdateSkyboxPosition(); // Update the skybox position + bool RenderSkybox(XMMATRIX view, XMMATRIX projection); // Render the skybox public : std::vector textures; @@ -178,6 +179,7 @@ private : int m_screenWidth, m_screenHeight; CameraClass* m_Camera; PositionClass* m_Position; + std::vector m_SkyboxInitialTranslations; // ------------------------------------ // // ------------- OBJECTS -------------- // diff --git a/enginecustom/assets/Skybox/imgui.ini b/enginecustom/assets/Skybox/imgui.ini new file mode 100644 index 0000000..3408d17 --- /dev/null +++ b/enginecustom/assets/Skybox/imgui.ini @@ -0,0 +1,91 @@ +[Window][Debug##Default] +Pos=60,60 +Size=400,400 +Collapsed=0 + +[Window][Khaotic Engine] +Pos=1180,27 +Size=396,826 +Collapsed=0 +DockId=0x00000005,0 + +[Window][Objects] +Pos=8,442 +Size=290,411 +Collapsed=0 +DockId=0x0000000A,0 + +[Window][Terrain] +Pos=8,27 +Size=290,413 +Collapsed=0 +DockId=0x00000009,0 + +[Window][Light] +Pos=8,27 +Size=330,487 +Collapsed=0 +DockId=0x00000004,1 + +[Window][Shader Manager] +Pos=8,27 +Size=330,487 +Collapsed=0 +DockId=0x00000004,2 + +[Window][Engine Settings] +Pos=1180,27 +Size=396,826 +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,627 +Size=1568,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 Split=Y Selected=0x393905AB + DockNode ID=0x00000004 Parent=0x00000001 SizeRef=330,487 Selected=0x393905AB + DockNode ID=0x00000006 Parent=0x00000001 SizeRef=330,485 Selected=0x031DC75C + 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/assets/Skybox/skybox_back.png b/enginecustom/assets/Skybox/skybox_back.png index 57965d1..70d2e28 100644 Binary files a/enginecustom/assets/Skybox/skybox_back.png and b/enginecustom/assets/Skybox/skybox_back.png differ diff --git a/enginecustom/assets/Skybox/skybox_bottom.png b/enginecustom/assets/Skybox/skybox_bottom.png index 6c6b976..563136a 100644 Binary files a/enginecustom/assets/Skybox/skybox_bottom.png and b/enginecustom/assets/Skybox/skybox_bottom.png differ diff --git a/enginecustom/assets/Skybox/skybox_front.png b/enginecustom/assets/Skybox/skybox_front.png index 7bd0ed1..547257a 100644 Binary files a/enginecustom/assets/Skybox/skybox_front.png and b/enginecustom/assets/Skybox/skybox_front.png differ diff --git a/enginecustom/assets/Skybox/skybox_left.png b/enginecustom/assets/Skybox/skybox_left.png index ef27a84..db9b4a2 100644 Binary files a/enginecustom/assets/Skybox/skybox_left.png and b/enginecustom/assets/Skybox/skybox_left.png differ diff --git a/enginecustom/assets/Skybox/skybox_right.png b/enginecustom/assets/Skybox/skybox_right.png index f5b6e95..a48c3c3 100644 Binary files a/enginecustom/assets/Skybox/skybox_right.png and b/enginecustom/assets/Skybox/skybox_right.png differ diff --git a/enginecustom/assets/Skybox/skybox_top.png b/enginecustom/assets/Skybox/skybox_top.png index 0623940..1d217ff 100644 Binary files a/enginecustom/assets/Skybox/skybox_top.png and b/enginecustom/assets/Skybox/skybox_top.png differ diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 2444f12..e009bf2 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -10,14 +10,14 @@ Collapsed=0 DockId=0x00000005,0 [Window][Objects] -Pos=8,27 -Size=290,826 +Pos=8,442 +Size=290,411 Collapsed=0 DockId=0x0000000A,0 [Window][Terrain] Pos=8,27 -Size=290,413 +Size=290,826 Collapsed=0 DockId=0x00000009,0 @@ -35,7 +35,7 @@ DockId=0x00000004,2 [Window][Engine Settings] Pos=1180,27 -Size=396,598 +Size=396,826 Collapsed=0 DockId=0x00000005,1 diff --git a/enginecustom/object.h b/enginecustom/object.h index 10a308a..cb9048d 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -66,7 +66,6 @@ public: void SetType(ObjectType type) { m_type = type; }; ObjectType GetType() const { return m_type; }; - bool LoadTexture(ID3D11Device* device, ID3D11DeviceContext* deviceContext, const std::wstring& filename); enum ShaderType { @@ -75,7 +74,9 @@ public: NORMAL_MAPPING, SPECULAR_MAPPING, REFLECTION, - REFRACTION + REFRACTION, + TEXTURE, + SKYBOX }; ShaderType GetActiveShader() const { return m_activeShader; };