From 20401c1df8ae2082544f723cebb16d4113dcd554 Mon Sep 17 00:00:00 2001 From: CatChow0 Date: Wed, 27 Mar 2024 11:32:50 +0100 Subject: [PATCH] Terrain + Fix ImGui Premier test de Terrain. Fix les attributs des objets dans ImGui --- enginecustom/Systemclass.cpp | 2 +- enginecustom/applicationclass.cpp | 57 ++++++++++++++++++++--- enginecustom/applicationclass.h | 3 ++ enginecustom/enginecustom.vcxproj.filters | 46 +++++++++++++----- enginecustom/imgui.ini | 6 +-- enginecustom/imguiManager.cpp | 37 +++++++++++++-- enginecustom/imguiManager.h | 1 + enginecustom/object.cpp | 6 +-- enginecustom/object.h | 3 ++ 9 files changed, 132 insertions(+), 29 deletions(-) diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index 755205b..1b3eb0a 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -99,7 +99,6 @@ void SystemClass::Run() MSG msg; bool done, result; - // Initialize the message structure. ZeroMemory(&msg, sizeof(MSG)); @@ -165,6 +164,7 @@ bool SystemClass::Frame() m_imguiManager->WidgetFPS(); m_imguiManager->WidgetAddObject(m_Application); m_imguiManager->WidgetObjectWindow(m_Application); + m_imguiManager->WidgetTestTerrain(m_Application); ImGui::End(); diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index bf3f417..9657b7c 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -49,7 +49,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) // Set the initial position of the camera. m_Camera->SetPosition(0.0f, 0.0f, -10.0f); - m_Camera->SetRotation(0.0f, 0.0f, 10.0f); + m_Camera->SetRotation(0.0f, 0.0f, 0.0f); // Set the file name of the model. strcpy_s(modelFilename, "cube.txt"); @@ -80,7 +80,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Light = new LightClass; m_Light->SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); - m_Light->SetDirection(0.0f, 0.0f, 1.0f); + m_Light->SetDirection(0.0f, -1.0f, 1.0f); return true; } @@ -166,7 +166,6 @@ bool ApplicationClass::Render(float rotation) XMMATRIX worldMatrix, rotateMatrix, translateMatrix, scaleMatrix, srMatrix; bool result; - // Clear the buffers to begin the scene. m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f); @@ -181,7 +180,15 @@ bool ApplicationClass::Render(float rotation) cube->Render(m_Direct3D->GetDeviceContext()); scaleMatrix = cube->GetScaleMatrix(); - rotateMatrix = XMMatrixRotationY(rotation); + + if (cube->m_demoSpinning) + rotateMatrix = XMMatrixRotationY(rotation); + else + { + rotateMatrix = cube->GetRotateMatrix(); + } + + translateMatrix = cube->GetTranslateMatrix(); srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); @@ -215,7 +222,7 @@ int ApplicationClass::GetScreenHeight() const return GetSystemMetrics(SM_CYSCREEN); } -void ApplicationClass::AddCube() +void ApplicationClass::GenerateTerrain() { char modelFilename[128]; char textureFilename[128]; @@ -226,10 +233,48 @@ void ApplicationClass::AddCube() // Set the name of the texture file that we will be loading. strcpy_s(textureFilename, "stone01.tga"); + // Create cube objects to fill a 10x10 grid of cubes + for (int i = -10; i < 10; i++) + { + for (int j = -10; j < 10; j++) + { + Object* newCube = new Object(); + newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename); + + newCube->SetTranslateMatrix(XMMatrixTranslation(i * 2.0f, -4.0f, j * 2.0f)); + + m_cubes.push_back(newCube); + } + } + +} + +void ApplicationClass::AddCube() +{ + char modelFilename[128]; + char textureFilename[128]; + + // Set the file name of the model. + strcpy_s(modelFilename, "cube.txt"); + + // Set the name of the texture file that we will be loading. + strcpy_s(textureFilename, "stone01.tga"); + static int cubeCount = 0; + float position = cubeCount * 2.0f; Object* newCube = new Object(); newCube->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename); - newCube->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); + newCube->SetTranslateMatrix(XMMatrixTranslation(position, 0.0f, 0.0f)); m_cubes.push_back(newCube); +} + +void ApplicationClass::DeleteCube(int index) +{ + if (index < m_cubes.size()) + { + m_cubes[index]->Shutdown(); + delete m_cubes[index]; + m_cubes.erase(m_cubes.begin() + index); + } } \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index 8f837f6..2a7674d 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -43,9 +43,12 @@ public: void SetSpeed(float speed) { this->speed = speed; }; void AddCube(); + void DeleteCube(int index); int GetCubeCount() const { return m_cubes.size(); }; std::vector GetCubes() const { return m_cubes; }; + void GenerateTerrain(); + private: bool Render(float); diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index d7c8bfd..ba3e166 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -69,6 +69,21 @@ Fichiers sources\ImGui + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + + + Fichiers sources + @@ -116,6 +131,21 @@ Fichiers d%27en-tête\ImGui + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + + + Fichiers d%27en-tête + @@ -125,21 +155,13 @@ shader - - texture - - - texture - + + - - assets - + - - assets - + \ No newline at end of file diff --git a/enginecustom/imgui.ini b/enginecustom/imgui.ini index 48f8c8b..379c53e 100644 --- a/enginecustom/imgui.ini +++ b/enginecustom/imgui.ini @@ -3,10 +3,10 @@ Pos=60,60 Size=400,400 [Window][Khaotic Engine] -Pos=765,19 -Size=694,367 +Pos=765,20 +Size=694,366 [Window][Objects] -Pos=44,57 +Pos=69,21 Size=492,353 diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index bbb374e..88395ec 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -81,34 +81,63 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) { ImGui::Begin("Objects"); int index = 0; - for (auto object : app->GetCubes()) + for (auto& object : app->GetCubes()) { std::string headerName = "Object " + std::to_string(index); if (ImGui::CollapsingHeader(headerName.c_str())) { + XMVECTOR position = object->GetPosition(); XMVECTOR rotation = object->GetRotation(); XMVECTOR scale = object->GetScale(); float pos[3] = { XMVectorGetX(position), XMVectorGetY(position), XMVectorGetZ(position) }; - if (ImGui::DragFloat3("Position", pos)) + std::string posLabel = "Position##" + std::to_string(index); + if (ImGui::DragFloat3(posLabel.c_str(), pos)) { object->SetPosition(XMVectorSet(pos[0], pos[1], pos[2], 0.0f)); } float rot[3] = { XMVectorGetX(rotation), XMVectorGetY(rotation), XMVectorGetZ(rotation) }; - if (ImGui::DragFloat3("Rotation", rot)) + std::string rotLabel = "Rotation##" + std::to_string(index); + if (ImGui::DragFloat3(rotLabel.c_str(), rot)) { object->SetRotation(XMVectorSet(rot[0], rot[1], rot[2], 0.0f)); } float scl[3] = { XMVectorGetX(scale), XMVectorGetY(scale), XMVectorGetZ(scale) }; - if (ImGui::DragFloat3("Scale", scl)) + std::string sclLabel = "Scale##" + std::to_string(index); + if (ImGui::DragFloat3(sclLabel.c_str(), scl)) { object->SetScale(XMVectorSet(scl[0], scl[1], scl[2], 0.0f)); } + + ImGui::Separator(); + + // Delete button + std::string deleteLabel = "Delete##" + std::to_string(index); + if (ImGui::Button(deleteLabel.c_str())) + { + app->DeleteCube(index); + } + + ImGui::Separator(); + + // Demo spinning + std::string demoLabel = "Demo spinning##" + std::to_string(index); + ImGui::Checkbox(demoLabel.c_str(), &object->m_demoSpinning); } index++; } ImGui::End(); +} + +void imguiManager::WidgetTestTerrain(ApplicationClass* app) +{ + ImGui::Separator(); + + if (ImGui::Button("Generate Terrain")) + { + app->GenerateTerrain(); + } } \ No newline at end of file diff --git a/enginecustom/imguiManager.h b/enginecustom/imguiManager.h index 4b83aa5..eca273b 100644 --- a/enginecustom/imguiManager.h +++ b/enginecustom/imguiManager.h @@ -25,6 +25,7 @@ public: void WidgetButton(); void WidgetFPS(); void WidgetAddObject(ApplicationClass* app); + void WidgetTestTerrain(ApplicationClass* app); void WidgetObjectWindow(ApplicationClass* app); diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp index a299102..b672f3c 100644 --- a/enginecustom/object.cpp +++ b/enginecustom/object.cpp @@ -66,7 +66,7 @@ XMMATRIX Object::GetWorldMatrix() XMVECTOR Object::GetPosition() { XMFLOAT4X4 matrix; - XMStoreFloat4x4(&matrix, m_worldMatrix); + XMStoreFloat4x4(&matrix, m_translateMatrix); return XMVectorSet(matrix._41, matrix._42, matrix._43, 0.0f); } @@ -94,11 +94,11 @@ XMVECTOR Object::GetScale() void Object::SetPosition(XMVECTOR position) { XMFLOAT4X4 matrix; - XMStoreFloat4x4(&matrix, m_worldMatrix); + XMStoreFloat4x4(&matrix, m_translateMatrix); matrix._41 = XMVectorGetX(position); matrix._42 = XMVectorGetY(position); matrix._43 = XMVectorGetZ(position); - m_worldMatrix = XMLoadFloat4x4(&matrix); + m_translateMatrix = XMLoadFloat4x4(&matrix); } void Object::SetRotation(XMVECTOR rotation) diff --git a/enginecustom/object.h b/enginecustom/object.h index a110311..f922197 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -27,6 +27,9 @@ public: XMVECTOR GetRotation(); XMVECTOR GetScale(); +public : + bool m_demoSpinning = false; + private: XMMATRIX m_scaleMatrix; XMMATRIX m_rotateMatrix;