diff --git a/enginecustom/Systemclass.cpp b/enginecustom/Systemclass.cpp index a0c584e..df7f983 100644 --- a/enginecustom/Systemclass.cpp +++ b/enginecustom/Systemclass.cpp @@ -220,10 +220,9 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam m_initialWindowHeight = newHeight; } } - case WM_DROPFILES: { - HDROP hDrop = reinterpret_cast(wparam); + HDROP hDrop = reinterpret_cast(wparam); UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, nullptr, 0); if (numFiles > 0) { @@ -233,13 +232,13 @@ LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam WCHAR filePath[MAX_PATH]; DragQueryFile(hDrop, i, filePath, MAX_PATH); std::wcout << L"File dropped: " << filePath << std::endl; + m_Application->AddKobject(filePath); } } DragFinish(hDrop); return 0; } - // Any other messages send to the default message handler as our application won't make use of them. default: { diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index a1e2946..b6f1941 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -362,6 +362,13 @@ void ApplicationClass::Shutdown() } m_terrainChunk.clear(); + for (auto object : m_object) + { + object->Shutdown(); + delete object; + } + m_object.clear(); + // Release the multitexture shader object. if (m_MultiTextureShader) { @@ -559,10 +566,6 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) lightPosition[i] = m_Lights[i].GetPosition(); } - - - - // Render the model using the multitexture shader. m_Model->Render(m_Direct3D->GetDeviceContext()); @@ -626,6 +629,30 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) } } + for (auto object : m_object) + { + scaleMatrix = object->GetScaleMatrix(); + if (object->m_demoSpinning) + rotateMatrix = XMMatrixRotationY(rotation); + else + { + rotateMatrix = object->GetRotateMatrix(); + } + translateMatrix = object->GetTranslateMatrix(); + srMatrix = XMMatrixMultiply(scaleMatrix, rotateMatrix); + worldMatrix = XMMatrixMultiply(srMatrix, translateMatrix); + + object->Render(m_Direct3D->GetDeviceContext()); + + result = m_LightShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0), + diffuseColor, lightPosition); + + if (!result) + { + return false; + } + } + // Render terrain for (auto chunk : m_terrainChunk) { @@ -717,6 +744,31 @@ void ApplicationClass::GenerateTerrain() } +void ApplicationClass::AddKobject(WCHAR* filepath) +{ + char modelFilename[128]; + char textureFilename[128]; + char textureFilename2[128]; + filesystem::path p(filepath); + string filename = p.stem().string(); + + size_t convertedChars = 0; + wcstombs_s(&convertedChars, modelFilename, sizeof(modelFilename), filepath, _TRUNCATE); + + + // Set the name of the texture file that we will be loading. + strcpy_s(textureFilename, "stone01.tga"); + strcpy_s(textureFilename2, "moss01.tga"); + + Object* newObject = new Object(); + newObject->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), modelFilename, textureFilename, textureFilename2); + + newObject->SetTranslateMatrix(XMMatrixTranslation(0.0f, 0.0f, 0.0f)); + newObject->SetName(filename); + + m_object.push_back(newObject); +} + void ApplicationClass::AddCube() { char modelFilename[128]; @@ -739,13 +791,13 @@ void ApplicationClass::AddCube() m_cubes.push_back(newCube); } -void ApplicationClass::DeleteCube(int index) +void ApplicationClass::DeleteKobject(int index) { - if (index < m_cubes.size()) + if (index < m_object.size()) { - m_cubes[index]->Shutdown(); - delete m_cubes[index]; - m_cubes.erase(m_cubes.begin() + index); + m_object[index]->Shutdown(); + delete m_object[index]; + m_object.erase(m_object.begin() + index); } } diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index c3cf504..ce0e501 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -11,6 +11,7 @@ #include "lightshaderclass.h" #include "lightclass.h" #include +#include #include "multitextureshaderclass.h" #include "bitmapclass.h" @@ -53,11 +54,13 @@ public: void SetSpeed(float speed) { this->speed = speed; }; void AddCube(); - void DeleteCube(int index); + void DeleteKobject(int index); int GetCubeCount() const { return m_cubes.size(); }; int GetTerrainCubeCount() const { return m_terrainChunk.size(); }; std::vector GetCubes() const { return m_cubes; }; std::vector GetTerrainCubes() const { return m_terrainChunk; }; + std::vector GetKobjects() const { return m_object; }; + void AddKobject(WCHAR* filepath); void GenerateTerrain(); void DeleteTerrain(); @@ -89,6 +92,7 @@ private: FpsClass* m_Fps; TextClass* m_FpsString; int m_previousFps; + std::vector m_object; }; #endif \ No newline at end of file diff --git a/enginecustom/imguiManager.cpp b/enginecustom/imguiManager.cpp index c6be46c..a4975ff 100644 --- a/enginecustom/imguiManager.cpp +++ b/enginecustom/imguiManager.cpp @@ -81,15 +81,16 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) { ImGui::Begin("Objects"); int index = 0; - for (auto& object : app->GetCubes()) + for (auto& object : app->GetKobjects()) { - std::string headerName = "Object " + std::to_string(index); + std::string headerName = object->GetName() + " " + 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) }; std::string posLabel = "Position##" + std::to_string(index); if (ImGui::DragFloat3(posLabel.c_str(), pos)) @@ -117,7 +118,7 @@ void imguiManager::WidgetObjectWindow(ApplicationClass* app) std::string deleteLabel = "Delete##" + std::to_string(index); if (ImGui::Button(deleteLabel.c_str())) { - app->DeleteCube(index); + app->DeleteKobject(index); } ImGui::Separator(); diff --git a/enginecustom/object.cpp b/enginecustom/object.cpp index b672f3c..a4304c0 100644 --- a/enginecustom/object.cpp +++ b/enginecustom/object.cpp @@ -117,4 +117,48 @@ void Object::SetScale(XMVECTOR scale) matrix._22 = XMVectorGetY(scale); matrix._33 = XMVectorGetZ(scale); m_scaleMatrix = XMLoadFloat4x4(&matrix); +} + +void Object::UpdateWorldMatrix() +{ + m_worldMatrix = m_scaleMatrix * m_rotateMatrix * m_translateMatrix; +} + +void Object::UpdateSRMatrix() +{ + m_srMatrix = m_scaleMatrix * m_rotateMatrix; +} + +void Object::UpdateTranslateMatrix() +{ + m_translateMatrix = XMMatrixTranslationFromVector(GetPosition()); +} + +void Object::UpdateRotateMatrix() +{ + m_rotateMatrix = XMMatrixRotationRollPitchYawFromVector(GetRotation()); +} + +void Object::UpdateScaleMatrix() +{ + m_scaleMatrix = XMMatrixScalingFromVector(GetScale()); +} + +void Object::Update() +{ + UpdateWorldMatrix(); + UpdateSRMatrix(); + UpdateTranslateMatrix(); + UpdateRotateMatrix(); + UpdateScaleMatrix(); +} + +std::string Object::GetName() +{ + return m_name; +} + +void Object::SetName(std::string name) +{ + m_name = name; } \ No newline at end of file diff --git a/enginecustom/object.h b/enginecustom/object.h index 9d1eb35..c7045f3 100644 --- a/enginecustom/object.h +++ b/enginecustom/object.h @@ -27,6 +27,17 @@ public: XMVECTOR GetRotation(); XMVECTOR GetScale(); + void UpdateWorldMatrix(); + void UpdateSRMatrix(); + void UpdateScaleMatrix(); + void UpdateRotateMatrix(); + void UpdateTranslateMatrix(); + + void Update(); + + std::string GetName(); + void SetName(std::string name); + public : bool m_demoSpinning = false; @@ -37,4 +48,5 @@ private: XMMATRIX m_srMatrix; XMMATRIX m_worldMatrix; + std::string m_name; };